home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / fsp / part01 / client_lib.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-12  |  2.9 KB  |  110 lines

  1.     /*********************************************************************\
  2.     *  Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu)   *
  3.     *                                                                     *
  4.     *  You may copy or modify this file in any manner you wish, provided  *
  5.     *  that this notice is always included, and that you hold the author  *
  6.     *  harmless for any loss or damage resulting from the installation or *
  7.     *  use of this software.                                              *
  8.     \*********************************************************************/
  9.  
  10. #include "client_def.h"
  11.  
  12. extern int errno;
  13.  
  14. static int myfd;
  15. static struct sockaddr_in server_addr;
  16. static unsigned short myseq = 0;
  17. static unsigned short key;
  18.  
  19. int client_trace      = 0;
  20. int client_intr_state = 0;
  21.  
  22. UBUF *client_interact(cmd,pos,l1,p1,l2,p2)
  23.     unsigned cmd, l1, l2;
  24.     unsigned long pos;
  25.     unsigned char *p1, *p2;
  26. {
  27.     struct sockaddr_in from;
  28.     UBUF sbuf;
  29.     static UBUF rbuf;
  30.     unsigned char *s, *t, *d;
  31.     unsigned u, n, sum, mask, mlen;
  32.     int retval, bytes, retry;
  33.  
  34.     sbuf.cmd = cmd;
  35.     sbuf.len = htons(l1);
  36.     sbuf.pos = htonl(pos);
  37.  
  38.     client_intr_state = 1;
  39.  
  40.     for(u = l1, d = (unsigned char *) sbuf.buf; u--; *d++ = *p1++);
  41.     for(u = l2                      ; u--; *d++ = *p2++);
  42.     mlen = d - (unsigned char *) &sbuf;
  43.  
  44.     for(retry = 0; ; retry++)
  45.     {
  46.     sbuf.key = key;
  47.     sbuf.seq = myseq;
  48.     sbuf.sum = 0;
  49.  
  50.     for(t = (unsigned char *) &sbuf, sum = n = mlen; n--; sum += *t++);
  51.     sbuf.sum = sum + (sum >> 8);
  52.     if(client_trace && retry) write(2,"R",1);
  53.  
  54.     if(sendto(myfd,&sbuf,mlen,0,&server_addr,sizeof(server_addr)) == -1)
  55.                         { perror("sendto"); exit(1); }
  56.     mask = 1 << myfd;
  57.  
  58.     while(1)
  59.     {
  60.         retval = _x_select(&mask, 3000L);
  61.  
  62.         if((retval == -1) && (errno = EINTR)) continue;
  63.  
  64.         if(retval == 1)    /* an incoming message is waiting */
  65.         {
  66.         bytes = sizeof(from);
  67.         if((bytes = recvfrom(myfd,(char*)&rbuf,sizeof(rbuf),0,
  68.                     &from,&bytes)) < UBUF_HSIZE) continue;
  69.  
  70.         s = (unsigned char *) &rbuf;
  71.         d = s + bytes;
  72.         u = rbuf.sum; rbuf.sum = 0;
  73.         for(t = s, sum = 0; t < d; sum += *t++);
  74.         sum = (sum + (sum >> 8)) & 0xff;
  75.         if(sum != u) continue;  /* wrong check sum */
  76.  
  77.         rbuf.len = htons(rbuf.len);
  78.         rbuf.pos = htonl(rbuf.pos);
  79.  
  80.         if(rbuf.seq           != myseq) continue;  /* wrong seq # */
  81.         if(rbuf.len+UBUF_HSIZE > bytes) continue;  /* truncated.  */
  82.  
  83.         myseq++; key = rbuf.key;    /* update seq and keys.   */
  84.  
  85.         if(client_intr_state == 2) { client_done(); exit(1); }
  86.  
  87.         return(&rbuf);
  88.  
  89.         } else break;   /* go back to re-transmit buffer again */
  90.     }
  91.     }
  92. }
  93.  
  94. init_client(host,port,myport)
  95.     char *host;
  96.     int   port;
  97.     int myport;
  98. {
  99.     if((myfd = _x_udp(&myport)) == -1)
  100.         { perror("socket open"); exit(1); }
  101.  
  102.     if(_x_adr(host,port,&server_addr) == -1)
  103.         { perror("server addr"); exit(1); } 
  104. }
  105.  
  106. client_done()
  107. {
  108.     (void) client_interact(CC_BYE,0L,0,NULLP,0,NULLP);
  109. }
  110.