xref: /csrg-svn/usr.bin/tftp/tftpsubs.c (revision 26114)
126093Sminshall /*
226093Sminshall  * Copyright (c) 1985 Regents of the University of California.
326093Sminshall  * All rights reserved.  The Berkeley software License Agreement
426093Sminshall  * specifies the terms and conditions for redistribution.
526093Sminshall  */
626093Sminshall 
726093Sminshall #ifndef lint
8*26114Sminshall static char sccsid[] = "@(#)tftpsubs.c	5.2 (Berkeley) 02/07/86";
926093Sminshall #endif not lint
1026093Sminshall 
1126093Sminshall /* Simple minded read-ahead/write-behind subroutines for tftp user and
1226093Sminshall    server.  Written originally with multiple buffers in mind, but current
1326093Sminshall    implementation has two buffer logic wired in.
1426093Sminshall 
1526093Sminshall    Todo:  add some sort of final error check so when the write-buffer
1626093Sminshall    is finally flushed, the caller can detect if the disk filled up
1726093Sminshall    (or had an i/o error) and return a nak to the other side.
1826093Sminshall 
1926093Sminshall 			Jim Guyton 10/85
2026093Sminshall  */
2126093Sminshall 
2226093Sminshall #include <sys/types.h>
2326093Sminshall #include <sys/socket.h>
24*26114Sminshall #include <sys/ioctl.h>
2526093Sminshall #include <netinet/in.h>
2626093Sminshall #include <arpa/tftp.h>
2726093Sminshall #include <stdio.h>
2826093Sminshall 
2926093Sminshall #define PKTSIZE SEGSIZE+4       /* should be moved to tftp.h */
3026093Sminshall 
3126093Sminshall struct bf {
3226093Sminshall 	int counter;            /* size of data in buffer, or flag */
3326093Sminshall 	char buf[PKTSIZE];      /* room for data packet */
3426093Sminshall } bfs[2];
3526093Sminshall 
3626093Sminshall 				/* Values for bf.counter  */
3726093Sminshall #define BF_ALLOC -3             /* alloc'd but not yet filled */
3826093Sminshall #define BF_FREE  -2             /* free */
3926093Sminshall /* [-1 .. SEGSIZE] = size of data in the data buffer */
4026093Sminshall 
4126093Sminshall static int nextone;     /* index of next buffer to use */
4226093Sminshall static int current;     /* index of buffer in use */
4326093Sminshall 
4426093Sminshall 			/* control flags for crlf conversions */
4526093Sminshall int newline = 0;        /* fillbuf: in middle of newline expansion */
4626093Sminshall int prevchar = -1;      /* putbuf: previous char (cr check) */
4726093Sminshall 
4826093Sminshall struct tftphdr *rw_init();
4926093Sminshall 
5026093Sminshall struct tftphdr *w_init() { return rw_init(0); }         /* write-behind */
5126093Sminshall struct tftphdr *r_init() { return rw_init(1); }         /* read-ahead */
5226093Sminshall 
5326093Sminshall struct tftphdr *
5426093Sminshall rw_init(x)              /* init for either read-ahead or write-behind */
5526093Sminshall int x;                  /* zero for write-behind, one for read-head */
5626093Sminshall {
5726093Sminshall 	newline = 0;            /* init crlf flag */
5826093Sminshall 	prevchar = -1;
5926093Sminshall 	bfs[0].counter =  BF_ALLOC;     /* pass out the first buffer */
6026093Sminshall 	current = 0;
6126093Sminshall 	bfs[1].counter = BF_FREE;
6226093Sminshall 	nextone = x;                    /* ahead or behind? */
6326093Sminshall 	return (struct tftphdr *)bfs[0].buf;
6426093Sminshall }
6526093Sminshall 
6626093Sminshall 
6726093Sminshall /* Have emptied current buffer by sending to net and getting ack.
6826093Sminshall    Free it and return next buffer filled with data.
6926093Sminshall  */
7026093Sminshall readit(file, dpp, convert)
7126093Sminshall 	FILE *file;                     /* file opened for read */
7226093Sminshall 	struct tftphdr **dpp;
7326093Sminshall 	int convert;                    /* if true, convert to ascii */
7426093Sminshall {
7526093Sminshall 	struct bf *b;
7626093Sminshall 
7726093Sminshall 	bfs[current].counter = BF_FREE; /* free old one */
7826093Sminshall 	current = !current;             /* "incr" current */
7926093Sminshall 
8026093Sminshall 	b = &bfs[current];              /* look at new buffer */
8126093Sminshall 	if (b->counter == BF_FREE)      /* if it's empty */
8226093Sminshall 		read_ahead(file, convert);      /* fill it */
8326093Sminshall /*      assert(b->counter != BF_FREE);  /* check */
8426093Sminshall 	*dpp = (struct tftphdr *)b->buf;        /* set caller's ptr */
8526093Sminshall 	return b->counter;
8626093Sminshall }
8726093Sminshall 
8826093Sminshall /*
8926093Sminshall  * fill the input buffer, doing ascii conversions if requested
9026093Sminshall  * conversions are  lf -> cr,lf  and cr -> cr, nul
9126093Sminshall  */
9226093Sminshall read_ahead(file, convert)
9326093Sminshall 	FILE *file;                     /* file opened for read */
9426093Sminshall 	int convert;                    /* if true, convert to ascii */
9526093Sminshall {
9626093Sminshall 	register int i;
9726093Sminshall 	register char *p;
9826093Sminshall 	register int c;
9926093Sminshall 	struct bf *b;
10026093Sminshall 	struct tftphdr *dp;
10126093Sminshall 
10226093Sminshall 	b = &bfs[nextone];              /* look at "next" buffer */
10326093Sminshall 	if (b->counter != BF_FREE)      /* nop if not free */
10426093Sminshall 		return;
10526093Sminshall 	nextone = !nextone;             /* "incr" next buffer ptr */
10626093Sminshall 
10726093Sminshall 	dp = (struct tftphdr *)b->buf;
10826093Sminshall 
10926093Sminshall 	if (convert == 0) {
11026093Sminshall 		b->counter = read(fileno(file), dp->th_data, SEGSIZE);
11126093Sminshall 		return;
11226093Sminshall 	}
11326093Sminshall 
11426093Sminshall 	p = dp->th_data;
11526093Sminshall 	for (i = 0 ; i < SEGSIZE; i++) {
11626093Sminshall 		if (newline) {
11726093Sminshall 			if (prevchar == '\n')
11826093Sminshall 				c = '\n';       /* lf to cr,lf */
11926093Sminshall 			else    c = '\0';       /* cr to cr,nul */
12026093Sminshall 			newline = 0;
12126093Sminshall 		}
12226093Sminshall 		else {
12326093Sminshall 			c = getc(file);
12426093Sminshall 			if (c == EOF) break;
12526093Sminshall 			if (c == '\n' || c == '\r') {
12626093Sminshall 				prevchar = c;
12726093Sminshall 				c = '\r';
12826093Sminshall 				newline = 1;
12926093Sminshall 			}
13026093Sminshall 		}
13126093Sminshall 	       *p++ = c;
13226093Sminshall 	}
13326093Sminshall 	b->counter = (int)(p - dp->th_data);
13426093Sminshall }
13526093Sminshall 
13626093Sminshall /* Update count associated with the buffer, get new buffer
13726093Sminshall    from the queue.  Calls write_behind only if next buffer not
13826093Sminshall    available.
13926093Sminshall  */
14026093Sminshall writeit(file, dpp, ct, convert)
14126093Sminshall 	FILE *file;
14226093Sminshall 	struct tftphdr **dpp;
14326093Sminshall 	int convert;
14426093Sminshall {
14526093Sminshall 	bfs[current].counter = ct;      /* set size of data to write */
14626093Sminshall 	current = !current;             /* switch to other buffer */
14726093Sminshall 	if (bfs[current].counter != BF_FREE)     /* if not free */
14826093Sminshall 		write_behind(file, convert);     /* flush it */
14926093Sminshall 	bfs[current].counter = BF_ALLOC;        /* mark as alloc'd */
15026093Sminshall 	*dpp =  (struct tftphdr *)bfs[current].buf;
15126093Sminshall 	return ct;                      /* this is a lie of course */
15226093Sminshall }
15326093Sminshall 
15426093Sminshall /*
15526093Sminshall  * Output a buffer to a file, converting from netascii if requested.
15626093Sminshall  * CR,NUL -> CR  and CR,LF => LF.
15726093Sminshall  * Note spec is undefined if we get CR as last byte of file or a
15826093Sminshall  * CR followed by anything else.  In this case we leave it alone.
15926093Sminshall  */
16026093Sminshall write_behind(file, convert)
16126093Sminshall 	FILE *file;
16226093Sminshall 	int convert;
16326093Sminshall {
16426093Sminshall 	char *buf;
16526093Sminshall 	int count;
16626093Sminshall 	register int ct;
16726093Sminshall 	register char *p;
16826093Sminshall 	register int c;                 /* current character */
16926093Sminshall 	struct bf *b;
17026093Sminshall 	struct tftphdr *dp;
17126093Sminshall 
17226093Sminshall 	b = &bfs[nextone];
17326093Sminshall 	if (b->counter < -1)            /* anything to flush? */
17426093Sminshall 		return 0;               /* just nop if nothing to do */
17526093Sminshall 
17626093Sminshall 	count = b->counter;             /* remember byte count */
17726093Sminshall 	b->counter = BF_FREE;           /* reset flag */
17826093Sminshall 	dp = (struct tftphdr *)b->buf;
17926093Sminshall 	nextone = !nextone;             /* incr for next time */
18026093Sminshall 	buf = dp->th_data;
18126093Sminshall 
18226093Sminshall 	if (count <= 0) return -1;      /* nak logic? */
18326093Sminshall 
18426093Sminshall 	if (convert == 0)
18526093Sminshall 		return write(fileno(file), buf, count);
18626093Sminshall 
18726093Sminshall 	p = buf;
18826093Sminshall 	ct = count;
18926093Sminshall 	while (ct--) {                  /* loop over the buffer */
19026093Sminshall 	    c = *p++;                   /* pick up a character */
19126093Sminshall 	    if (prevchar == '\r') {     /* if prev char was cr */
19226093Sminshall 		if (c == '\n')          /* if have cr,lf then just */
19326093Sminshall 		   fseek(file, -1, 1);  /* smash lf on top of the cr */
19426093Sminshall 		else
19526093Sminshall 		   if (c == '\0')       /* if have cr,nul then */
19626093Sminshall 			goto skipit;    /* just skip over the putc */
19726093Sminshall 		/* else just fall through and allow it */
19826093Sminshall 	    }
19926093Sminshall 	    putc(c, file);
20026093Sminshall skipit:
20126093Sminshall 	    prevchar = c;
20226093Sminshall 	}
20326093Sminshall 	return count;
20426093Sminshall }
20526093Sminshall 
20626093Sminshall 
207*26114Sminshall /* When an error has occurred, it is possible that the two sides
208*26114Sminshall  * are out of synch.  Ie: that what I think is the other side's
209*26114Sminshall  * response to packet N is really their response to packet N-1.
210*26114Sminshall  *
211*26114Sminshall  * So, to try to prevent that, we flush all the input queued up
212*26114Sminshall  * for us on the network connection on our host.
213*26114Sminshall  *
214*26114Sminshall  * We return the number of packets we flushed (mostly for reporting
215*26114Sminshall  * when trace is active).
216*26114Sminshall  */
217*26114Sminshall 
218*26114Sminshall int
219*26114Sminshall synchnet(f)
220*26114Sminshall int	f;		/* socket to flush */
221*26114Sminshall {
222*26114Sminshall 	int i, j = 0;
223*26114Sminshall 	char rbuf[PKTSIZE];
224*26114Sminshall 	struct sockaddr_in from;
225*26114Sminshall 	int fromlen;
226*26114Sminshall 
227*26114Sminshall 	while (1) {
228*26114Sminshall 		(void) ioctl(f, FIONREAD, &i);
229*26114Sminshall 		if (i) {
230*26114Sminshall 			j++;
231*26114Sminshall 			fromlen = sizeof from;
232*26114Sminshall 			(void) recvfrom(f, rbuf, sizeof (rbuf), 0,
233*26114Sminshall 				(caddr_t)&from, &fromlen);
234*26114Sminshall 		} else {
235*26114Sminshall 			return(j);
236*26114Sminshall 		}
237*26114Sminshall 	}
238*26114Sminshall }
239