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

  1. /*
  2.  * A testing software for TCP-ping-pong
  3.  */
  4.  
  5. /*
  6.  * Telnet routines
  7.  * $Header: telnet.c,v 2.3 93/04/20 18:48:32 puhuri Exp $
  8.  * $Log:    telnet.c,v $
  9.  * Revision 2.3  93/04/20  18:48:32  18:48:32  puhuri (Markus Peuhkuri)
  10.  * Errors in debug printing
  11.  * 
  12.  * Revision 2.2  93/04/19  04:40:30  04:40:30  ppessi (Pekka Pessi)
  13.  * Optimizations with socket options
  14.  * 
  15.  * Revision 2.1  93/04/13  21:46:14  21:46:14  jraja (Jarno Tapio Rajahalme)
  16.  * Made this compile with the newest API.
  17.  * 
  18.  * Revision 2.0  93/03/20  17:31:50  17:31:50  ppessi (Pekka Pessi)
  19.  * initial netlib version..
  20.  * 
  21.  * Revision 1.9  93/03/19  16:48:21  16:48:21  puhuri (Markus Peuhkuri)
  22.  * Add possibility to make non-uniform distribution of length of
  23.  * replied data in telnet-server.
  24.  * 
  25.  * Revision 1.8  93/03/17  21:24:07  21:24:07  puhuri (Markus Peuhkuri)
  26.  * Add checksum test
  27.  * 
  28.  * Revision 1.7  93/03/16  19:15:04  19:15:04  too (Tomi Ollila)
  29.  * code fixes
  30.  * 
  31.  * Revision 1.6  93/03/16  10:42:47  10:42:47  puhuri (Markus Peuhkuri)
  32.  * Added AmiTCP stuff.
  33.  * 
  34.  * Revision 1.5  93/03/15  18:59:52  18:59:52  puhuri (Markus Peuhkuri)
  35.  * Comment fix
  36.  * 
  37.  */
  38.  
  39. #include <sys/types.h>
  40.  
  41. #ifdef AMIGA
  42. #if __SASC
  43. #include <proto/socket.h>
  44. #elif __GNUC__
  45. #include <inline/socket.h>
  46. #else
  47. #include <clib/socket_protos.h>
  48. #endif
  49. #endif /* AMIGA */
  50.  
  51. #ifdef __STDC__
  52. #include <stdlib.h>
  53. #endif
  54.  
  55. #include <stdio.h>
  56.  
  57. #include <sys/types.h>
  58. #include <sys/socket.h>
  59. #include <sys/ioctl.h>
  60. #include <sys/time.h>
  61.  
  62. #include <netinet/in.h>
  63.  
  64. #include <stdio.h>
  65. #include "qwriter.h"
  66. #include <string.h>
  67.  
  68. static u16 givernd(int *dtable);
  69. static int lg2(int i);
  70.  
  71. static int lg2(int i)
  72. {
  73.   int j;
  74.  
  75.   for(j = 0; (1 << j) <= i; j++)
  76.     ;
  77.   return (j-1);
  78. }
  79.  
  80. static u16 givernd(int *dtable)
  81. {
  82.   int i, len = 0, rnd = rand(), lg;
  83.  
  84.   lg = lg2(dtable[0]);
  85.   for(i = 0; lg > i; i++)
  86.     len = len * 2 + ((rnd & (1 << i)) ? 2 : 1);
  87.  
  88.   DP(("len=%ld i=%ld rnd=%04lx lg=%ld dtable[?]=%ld\n",len,i,rnd,lg, len - (1 << lg) +2));
  89.   return max(((rnd >> i) % dtable[len - (1 << lg) +2]), 4);
  90. }
  91.  
  92. int telnet_server(UWORD port1, int *dtable, int chk)
  93. {
  94.   int sid_c,t;
  95.   int len=0;
  96.   u16 *buf, wlen;
  97.  
  98.   int d;
  99.  
  100.   printf("dtable=%ld max=%ld ",dtable[0],dtable[dtable[0]]);
  101.   if((sid_c = open_server(NULL, port1, SOCK_STREAM))<0)
  102.     return(FAIL);
  103.   /* Allocate 32 kilos for send buffer */
  104.   d = 32*1024;
  105.   setsockopt(sid_c, SOL_SOCKET, SO_SNDBUF, (char *)&d, sizeof d);
  106.   
  107.   if(buf = (u16 *)malloc(dtable[dtable[0]])){    /* Allocate buffer */
  108.     while((d = recv(sid_c, (char *)buf, 1, 0)) >= 0 && *(char *)buf != '\0'){
  109.       wlen = givernd(dtable);
  110.       DP(("r=%ld s=%ld ",(long)d,(long)wlen));
  111.       *buf = htons(wlen);
  112.       if(chk)            /* Calculate checkum */
  113.     *(buf+1) = htons(pppfcs(PPPINITFCS, (unsigned char *)(buf+2), 
  114.                 wlen-2*sizeof(u16)));
  115.       if((t = send(sid_c, (char *)buf, wlen, 0)) != wlen){
  116.     preturn("telnet-server: send");
  117.     break;
  118.       }
  119.       len += t;
  120.       DP(("st=%ld len=%ld\n",t,len));
  121.     }
  122.     if(d<0)
  123.       perror("telnet-server: recv");
  124.     CloseSocket(sid_c);
  125.     printf("%ld writen\n",len);
  126.     free(buf);
  127.   }
  128.   return(OK);
  129. }
  130.  
  131. int telnet_client(char *host, UWORD port1, int rounds, int bufsiz, int chk)
  132. {
  133.   int sid_s;
  134.   int i,total,t, t2;
  135.   u16 *buf, sum;
  136.   struct timeval tv1,tv2;
  137.   double timediff;
  138.   unsigned int inlen;
  139.  
  140.   int s,r;
  141.  
  142.   if((sid_s=open_client(host, port1, SOCK_STREAM))<0)
  143.     return(FAIL);
  144.   /* Allocate 32 kilos for receive buffer */
  145.   s = 32*1024;
  146.   setsockopt(sid_s, SOL_SOCKET, SO_RCVBUF, (char *)&s, sizeof s);
  147.  
  148.   if (buf = (u16 *)malloc(bufsiz)){    /* Allocate junk-buffer */
  149.     gettimeofday(&tv1, NULL);
  150.     for (i = total = 0;
  151.      i < rounds && (*(char *)buf = 'a')
  152.      && (s = send(sid_s, (char *)buf, 1, 0)) > 0;
  153.      i++, total += inlen) {
  154.       if ((r = recv(sid_s, (char *)buf, 2*sizeof(u16), 0))<=0 ||
  155.       ((inlen = ntohs(*buf)) == 0 || 
  156.        (inlen > bufsiz)))
  157.     {
  158.       fprintf(stderr, "Requested read: %ld, Size of buffer=%ld\n",
  159.           inlen, bufsiz);
  160.       preturn("telnet-client: read");
  161.       break;
  162.     }
  163.       DP(("s=%ld r=%ld inlen=%ld",s,r,inlen));
  164.  
  165.       for(t = 2* sizeof(u16);t < inlen; t += t2)
  166.     if((t2 = recv(sid_s, ((char *)(buf))+t, inlen-t, 0)) < 0){
  167.       DP((" t2=%ld\n",t2));
  168.       preturn("telnet-client: recv");
  169.     } else
  170.       DP((" t2=%ld",t2));
  171.  
  172.       DP((" t=%ld total=%ld",t,total));
  173.       if(chk)            /* Calculate checkum */
  174.     if(ntohs(*(buf+1)) != 
  175.        (sum = pppfcs(PPPINITFCS, (unsigned char *)(buf+2), 
  176.              t-2*sizeof(u16))))
  177.       fprintf(stderr,"Checksum error in telnet-client: "
  178.           "should be %ld, is %ld\n"
  179.           "Error in recieved bytes %ld-%ld\n",ntohs(*(buf+1)),
  180.           sum, total-t, total);
  181.       DP(((chk)?" sum=%ld psum=%ld\n":"\n",sum,ntohs(*(buf+1))));
  182.     }
  183.     *buf=0;
  184.     if (send(sid_s, (char *)buf, 1, 0) <= 0)
  185.       preturn("telnet-clent: send (last)");
  186.     free(buf);
  187.   }
  188.  
  189.   gettimeofday(&tv2, NULL);
  190.   CloseSocket(sid_s);
  191.  
  192.   timediff=(double)(tv2.tv_sec-tv1.tv_sec)+((double)(tv2.tv_usec-tv1.tv_usec))/1E6;
  193.   printf("Send %ld chars, got %ld chars\n",i,total);
  194.   printf("Time used: %8.4lf seconds (%lg bytes/s)\n",timediff,(i+total)/timediff);
  195.   printf("Avarage round-trip delay: %lg ms\n",timediff/i*1E3);
  196.   return(OK);
  197. }
  198.