home *** CD-ROM | disk | FTP | other *** search
- /*
- * A testing software for TCP-bitstreams
- * $Header: /u/opi/86/jraja/ohtatcp/src/util/qwriter/RCS/ftp.c,v 2.3 1993/06/04 11:52:45 jraja Exp $
- * $Log: ftp.c,v $
- * Revision 2.3 1993/06/04 11:52:45 jraja
- * Fixes for the first release.
- *
- * Revision 2.2 93/04/19 04:39:40 04:39:40 ppessi (Pekka Pessi)
- * Optimizations with socket options
- *
- * Revision 2.1 93/04/13 21:46:06 21:46:06 jraja (Jarno Tapio Rajahalme)
- * Made this compile with the newest API.
- *
- * Revision 2.0 93/03/20 17:31:29 17:31:29 ppessi (Pekka Pessi)
- * initial netlib version..
- *
- * Revision 1.9 93/03/19 14:44:07 14:44:07 puhuri (Markus Peuhkuri)
- * Modified to use preturn instead of pexit
- *
- * Revision 1.8 93/03/18 22:29:16 22:29:16 puhuri (Markus Peuhkuri)
- * Fixed a checksum calculation. Again off-by-one(two,three) error.
- *
- * Revision 1.7 93/03/16 19:13:54 19:13:54 too (Tomi Ollila)
- * code fixes
- *
- * Revision 1.6 93/03/16 10:42:04 10:42:04 puhuri (Markus Peuhkuri)
- * Added AmiTCP stuff.
- *
- * Revision 1.5 93/03/15 18:58:42 18:58:42 puhuri (Markus Peuhkuri)
- * Add checksum verification
- */
-
-
- #ifdef __STDC__
- #include <stdlib.h>
- #endif
-
- #include <stdio.h>
-
- #include <sys/param.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <sys/ioctl.h>
-
- #include <sys/time.h>
-
- #include <netinet/in.h>
- #include <netinet/tcp.h>
-
- #include <netdb.h>
-
- #include "qwriter.h"
- #include <string.h> /* after qwriter.h */
-
- int ftp_server(UWORD port1, int bufsiz, int chk)
- {
- u32 len2, len;
- int sid_c;
- char *buf;
- u16 fcs = PPPINITFCS;
-
- int s;
-
- if ((sid_c = open_server(NULL, port1, SOCK_STREAM)) < 0)
- return(FAIL);
-
- /* Set NODELAY option */
- s = 1;
- setsockopt(sid_c, IPPROTO_TCP, TCP_NODELAY, (char *)&s, sizeof s);
- /* Allocate 32 kilos for send buffer */
- s = 32*1024;
- setsockopt(sid_c, SOL_SOCKET, SO_SNDBUF, (char *)&s, sizeof s);
-
- if (buf = (char *)malloc(bufsiz)) { /* Allocate junk-buffer */
-
- if (recv(sid_c, (char *)&len, sizeof(u32), 0) == sizeof(u32))
- /* How many bytes wanted? */
- for (len2 = len = ntohl(len); len > 0; len -= min(bufsiz, len)){
- if (chk)
- if (bufsiz < len-2) /* Still few buffers to go */
- fcs = pppfcs(fcs, (unsigned char *)buf, bufsiz);
- else {
- fcs = pppfcs(fcs, (unsigned char *)buf, max(0,len-3));
- *(buf+len-2) = 0xff & fcs; /* Low byte first */
- if(len < bufsiz)
- *(buf+len-1)= 0xff & (fcs >> 8); /* Hi byte second */
- }
- if((s = send(sid_c, buf, min(bufsiz, len), 0)) < 0){
- preturn("ftp-server: send"); /* Write until finished */
- } else {
- if(chk)
- DP(("fcs=0x%04x ", fcs));
- DP(("s=%ld len=%ld\n",s,len));
- }
- }
- else
- preturn("ftp-server: recv");
- if(recv(sid_c, buf, 1, 0) <= 0)
- preturn("ftp-server: read2");
- CloseSocket(sid_c);
- printf("%ld written, fcs=0x%04x\n",len2, fcs);
- free(buf);
- }
- return 0;
- }
-
- int ftp_client(char *host, UWORD port1, int length, int bufsiz, int chk)
- {
- int sid_s;
- int len_ns, count, len_re=0;
- char *buf;
- struct timeval tv1,tv2;
- double timediff;
- u16 fcs=PPPINITFCS, cfcs=0;
-
- int s, t;
-
- if((sid_s=open_client(host, port1 ,SOCK_STREAM))<0)
- return(FAIL);
-
- /* Set NODELAY option */
- s = 1;
- setsockopt(sid_s, IPPROTO_TCP, TCP_NODELAY, (char *)&s, sizeof s);
- /* Allocate 32 kilos for receive buffer */
- s = 32*1024;
- setsockopt(sid_s, SOL_SOCKET, SO_RCVBUF, (char *)&s, sizeof s);
-
- len_ns=htonl(length);
- if(buf=(char *)malloc(bufsiz)){ /* Allocate buffer */
- gettimeofday(&tv1, NULL);
- if ((s = send(sid_s, (char *)&len_ns, sizeof(len_ns), 0)) ==
- sizeof(len_ns))
- while ((count = recv(sid_s, (char *)buf,
- t = min(bufsiz, length - len_re), 0)) >= 0) {
- len_re += count; /* How many bytes to get */
- if(chk)
- if(len_re < length-2) /* Still few buffers to go */
- fcs = pppfcs(fcs, (unsigned char *)buf, count);
- else {
- fcs = pppfcs(fcs, (unsigned char *)buf,
- max(count-(length-len_re+3),0));
- cfcs = 0xff & *(buf+count-(length-len_re+2)); /* Low byte first */
- if(len_re == length)
- cfcs |= 0xff00 & ((*(buf+count-(length-len_re+1))) << 8);
- /* Hi byte second */
- }
- DP(("s=%ld c=%ld len=%ld t=%ld fcs=0x%04x\n",s,count,len_re,t,fcs));
- if(len_re == length)
- break;
- }
- else
- preturn("ftp-client: send");
- if(count<0){
- preturn("ftp-client: recv");
- }
- if(send(sid_s, (char *)buf, 1, 0) <= 0)
- preturn("ftp-client: send");
- gettimeofday(&tv2, NULL);
- free(buf);
- }
- CloseSocket(sid_s);
-
- timediff=(double)(tv2.tv_sec-tv1.tv_sec) +
- ((double)(tv2.tv_usec-tv1.tv_usec))/1E6;
- if(chk)
- printf("Checksum verification 0x%04x 0x%04x\n",cfcs, fcs);
- printf("Send %ld chars, got %ld chars (of %ld wanted)\n",
- sizeof(len_ns),len_re,length);
- printf("Time used: %8.4lf seconds (%lg bytes/s)\n",timediff,len_re/timediff);
- return 0;
- }
-
-