home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 2 / FFMCD02.bin / new / comm / net / amitcp / amitcp-2.2 / src / appl / qwriter / ftp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-21  |  4.8 KB  |  173 lines

  1. /*
  2.  * A testing software for TCP-bitstreams
  3.  * $Header: /u/opi/86/jraja/ohtatcp/src/util/qwriter/RCS/ftp.c,v 2.3 1993/06/04 11:52:45 jraja Exp $
  4.  * $Log: ftp.c,v $
  5.  * Revision 2.3  1993/06/04  11:52:45  jraja
  6.  * Fixes for the first release.
  7.  *
  8.  * Revision 2.2  93/04/19  04:39:40  04:39:40  ppessi (Pekka Pessi)
  9.  * Optimizations with socket options
  10.  * 
  11.  * Revision 2.1  93/04/13  21:46:06  21:46:06  jraja (Jarno Tapio Rajahalme)
  12.  * Made this compile with the newest API.
  13.  * 
  14.  * Revision 2.0  93/03/20  17:31:29  17:31:29  ppessi (Pekka Pessi)
  15.  * initial netlib version..
  16.  * 
  17.  * Revision 1.9  93/03/19  14:44:07  14:44:07  puhuri (Markus Peuhkuri)
  18.  * Modified to use preturn instead of pexit
  19.  * 
  20.  * Revision 1.8  93/03/18  22:29:16  22:29:16  puhuri (Markus Peuhkuri)
  21.  * Fixed a checksum calculation. Again off-by-one(two,three) error.
  22.  * 
  23.  * Revision 1.7  93/03/16  19:13:54  19:13:54  too (Tomi Ollila)
  24.  * code fixes
  25.  * 
  26.  * Revision 1.6  93/03/16  10:42:04  10:42:04  puhuri (Markus Peuhkuri)
  27.  * Added AmiTCP stuff.
  28.  * 
  29.  * Revision 1.5  93/03/15  18:58:42  18:58:42  puhuri (Markus Peuhkuri)
  30.  * Add checksum verification
  31.  */
  32.  
  33.  
  34. #ifdef __STDC__
  35. #include <stdlib.h>
  36. #endif
  37.  
  38. #include <stdio.h>
  39.  
  40. #include <sys/param.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/ioctl.h>
  44.  
  45. #include <sys/time.h>
  46.  
  47. #include <netinet/in.h>
  48. #include <netinet/tcp.h>
  49.  
  50. #include <netdb.h> 
  51.  
  52. #include "qwriter.h"
  53. #include <string.h>        /* after qwriter.h */
  54.  
  55. int ftp_server(UWORD port1, int bufsiz, int chk)
  56. {
  57.   u32   len2, len;
  58.   int   sid_c;
  59.   char *buf;
  60.   u16   fcs = PPPINITFCS;
  61.   
  62.   int s;
  63.  
  64.   if ((sid_c = open_server(NULL, port1, SOCK_STREAM)) < 0)
  65.     return(FAIL);
  66.   
  67.   /* Set NODELAY option */
  68.   s = 1;
  69.   setsockopt(sid_c, IPPROTO_TCP, TCP_NODELAY, (char *)&s, sizeof s);
  70.   /* Allocate 32 kilos for send buffer */
  71.   s = 32*1024;
  72.   setsockopt(sid_c, SOL_SOCKET, SO_SNDBUF, (char *)&s, sizeof s);
  73.  
  74.   if (buf = (char *)malloc(bufsiz)) {    /* Allocate junk-buffer */
  75.  
  76.     if (recv(sid_c, (char *)&len, sizeof(u32), 0) == sizeof(u32)) 
  77.       /* How many bytes wanted? */
  78.       for (len2 = len = ntohl(len); len > 0; len -= min(bufsiz, len)){
  79.     if (chk)
  80.       if (bufsiz < len-2)        /* Still few buffers to go */
  81.         fcs = pppfcs(fcs, (unsigned char *)buf, bufsiz);
  82.       else {
  83.         fcs = pppfcs(fcs, (unsigned char *)buf, max(0,len-3));
  84.         *(buf+len-2) = 0xff & fcs; /* Low byte first */
  85.         if(len < bufsiz)
  86.           *(buf+len-1)= 0xff & (fcs >> 8); /* Hi byte second */
  87.       }
  88.     if((s = send(sid_c, buf, min(bufsiz, len), 0)) < 0){
  89.       preturn("ftp-server: send"); /* Write until finished */
  90.     } else {
  91.       if(chk)
  92.         DP(("fcs=0x%04x ", fcs));
  93.       DP(("s=%ld len=%ld\n",s,len));
  94.     }
  95.       }
  96.     else
  97.       preturn("ftp-server: recv");
  98.     if(recv(sid_c, buf, 1, 0) <= 0)
  99.       preturn("ftp-server: read2");
  100.     CloseSocket(sid_c);
  101.     printf("%ld written, fcs=0x%04x\n",len2, fcs);
  102.     free(buf);
  103.   }
  104.   return 0;
  105. }
  106.  
  107. int ftp_client(char *host, UWORD port1, int length, int bufsiz, int chk)
  108. {
  109.   int sid_s;
  110.   int len_ns, count, len_re=0;
  111.   char *buf;
  112.   struct timeval tv1,tv2;
  113.   double timediff;
  114.   u16 fcs=PPPINITFCS, cfcs=0;
  115.   
  116.   int s, t;
  117.   
  118.   if((sid_s=open_client(host, port1 ,SOCK_STREAM))<0)
  119.     return(FAIL);
  120.  
  121.   /* Set NODELAY option */
  122.   s = 1;
  123.   setsockopt(sid_s, IPPROTO_TCP, TCP_NODELAY, (char *)&s, sizeof s);
  124.   /* Allocate 32 kilos for receive buffer */
  125.   s = 32*1024;
  126.   setsockopt(sid_s, SOL_SOCKET, SO_RCVBUF, (char *)&s, sizeof s);
  127.   
  128.   len_ns=htonl(length);
  129.   if(buf=(char *)malloc(bufsiz)){    /* Allocate buffer */
  130.     gettimeofday(&tv1, NULL);
  131.     if ((s = send(sid_s, (char *)&len_ns, sizeof(len_ns), 0)) == 
  132.     sizeof(len_ns))
  133.       while ((count = recv(sid_s, (char *)buf,
  134.                t = min(bufsiz, length - len_re), 0)) >= 0) {
  135.     len_re += count;        /* How many bytes to get */
  136.     if(chk)
  137.       if(len_re < length-2)        /* Still few buffers to go */
  138.         fcs = pppfcs(fcs, (unsigned char *)buf, count);
  139.       else {
  140.         fcs = pppfcs(fcs, (unsigned char *)buf,
  141.              max(count-(length-len_re+3),0));
  142.         cfcs = 0xff & *(buf+count-(length-len_re+2)); /* Low byte first */
  143.         if(len_re == length)
  144.           cfcs |= 0xff00 & ((*(buf+count-(length-len_re+1))) << 8); 
  145.         /* Hi byte second */
  146.       }      
  147.     DP(("s=%ld c=%ld len=%ld t=%ld fcs=0x%04x\n",s,count,len_re,t,fcs));
  148.     if(len_re == length)
  149.       break;
  150.       } 
  151.     else
  152.       preturn("ftp-client: send");
  153.     if(count<0){
  154.       preturn("ftp-client: recv");
  155.     }
  156.     if(send(sid_s, (char *)buf, 1, 0) <= 0)
  157.       preturn("ftp-client: send");
  158.     gettimeofday(&tv2, NULL);
  159.     free(buf);
  160.   }
  161.   CloseSocket(sid_s);
  162.  
  163.   timediff=(double)(tv2.tv_sec-tv1.tv_sec) +
  164.     ((double)(tv2.tv_usec-tv1.tv_usec))/1E6;
  165.   if(chk)
  166.     printf("Checksum verification 0x%04x 0x%04x\n",cfcs, fcs);
  167.   printf("Send %ld chars, got %ld chars (of %ld wanted)\n",
  168.      sizeof(len_ns),len_re,length);
  169.   printf("Time used: %8.4lf seconds (%lg bytes/s)\n",timediff,len_re/timediff);
  170.   return 0;
  171. }
  172.  
  173.