home *** CD-ROM | disk | FTP | other *** search
- /*********************************************************************\
- * Copyright (c) 1991 by Wen-King Su (wen-king@vlsi.cs.caltech.edu) *
- * *
- * You may copy or modify this file in any manner you wish, provided *
- * that this notice is always included, and that you hold the author *
- * harmless for any loss or damage resulting from the installation or *
- * use of this software. *
- \*********************************************************************/
-
- #include "client_def.h"
-
- extern int errno;
-
- static int myfd;
- static struct sockaddr_in server_addr;
- static unsigned short myseq = 0;
- static unsigned short key;
-
- int client_trace = 0;
- int client_intr_state = 0;
-
- UBUF *client_interact(cmd,pos,l1,p1,l2,p2)
- unsigned cmd, l1, l2;
- unsigned long pos;
- unsigned char *p1, *p2;
- {
- struct sockaddr_in from;
- UBUF sbuf;
- static UBUF rbuf;
- unsigned char *s, *t, *d;
- unsigned u, n, sum, mask, mlen;
- int retval, bytes, retry;
-
- sbuf.cmd = cmd;
- sbuf.len = htons(l1);
- sbuf.pos = htonl(pos);
-
- client_intr_state = 1;
-
- for(u = l1, d = (unsigned char *) sbuf.buf; u--; *d++ = *p1++);
- for(u = l2 ; u--; *d++ = *p2++);
- mlen = d - (unsigned char *) &sbuf;
-
- for(retry = 0; ; retry++)
- {
- sbuf.key = key;
- sbuf.seq = myseq;
- sbuf.sum = 0;
-
- for(t = (unsigned char *) &sbuf, sum = n = mlen; n--; sum += *t++);
- sbuf.sum = sum + (sum >> 8);
- if(client_trace && retry) write(2,"R",1);
-
- if(sendto(myfd,&sbuf,mlen,0,&server_addr,sizeof(server_addr)) == -1)
- { perror("sendto"); exit(1); }
- mask = 1 << myfd;
-
- while(1)
- {
- retval = _x_select(&mask, 3000L);
-
- if((retval == -1) && (errno = EINTR)) continue;
-
- if(retval == 1) /* an incoming message is waiting */
- {
- bytes = sizeof(from);
- if((bytes = recvfrom(myfd,(char*)&rbuf,sizeof(rbuf),0,
- &from,&bytes)) < UBUF_HSIZE) continue;
-
- s = (unsigned char *) &rbuf;
- d = s + bytes;
- u = rbuf.sum; rbuf.sum = 0;
- for(t = s, sum = 0; t < d; sum += *t++);
- sum = (sum + (sum >> 8)) & 0xff;
- if(sum != u) continue; /* wrong check sum */
-
- rbuf.len = htons(rbuf.len);
- rbuf.pos = htonl(rbuf.pos);
-
- if(rbuf.seq != myseq) continue; /* wrong seq # */
- if(rbuf.len+UBUF_HSIZE > bytes) continue; /* truncated. */
-
- myseq++; key = rbuf.key; /* update seq and keys. */
-
- if(client_intr_state == 2) { client_done(); exit(1); }
-
- return(&rbuf);
-
- } else break; /* go back to re-transmit buffer again */
- }
- }
- }
-
- init_client(host,port,myport)
- char *host;
- int port;
- int myport;
- {
- if((myfd = _x_udp(&myport)) == -1)
- { perror("socket open"); exit(1); }
-
- if(_x_adr(host,port,&server_addr) == -1)
- { perror("server addr"); exit(1); }
- }
-
- client_done()
- {
- (void) client_interact(CC_BYE,0L,0,NULLP,0,NULLP);
- }
-