xref: /csrg-svn/usr.bin/tftp/tftpsubs.c (revision 62309)
126093Sminshall /*
2*62309Sbostic  * Copyright (c) 1983, 1993
3*62309Sbostic  *	The Regents of the University of California.  All rights reserved.
433821Sbostic  *
542770Sbostic  * %sccs.include.redist.c%
626093Sminshall  */
726093Sminshall 
826093Sminshall #ifndef lint
9*62309Sbostic static char sccsid[] = "@(#)tftpsubs.c	8.1 (Berkeley) 06/06/93";
1033821Sbostic #endif /* not lint */
1126093Sminshall 
1226093Sminshall /* Simple minded read-ahead/write-behind subroutines for tftp user and
1326093Sminshall    server.  Written originally with multiple buffers in mind, but current
1426093Sminshall    implementation has two buffer logic wired in.
1526093Sminshall 
1626093Sminshall    Todo:  add some sort of final error check so when the write-buffer
1726093Sminshall    is finally flushed, the caller can detect if the disk filled up
1826093Sminshall    (or had an i/o error) and return a nak to the other side.
1926093Sminshall 
2026093Sminshall 			Jim Guyton 10/85
2126093Sminshall  */
2226093Sminshall 
2326093Sminshall #include <sys/types.h>
2426093Sminshall #include <sys/socket.h>
2526114Sminshall #include <sys/ioctl.h>
2626093Sminshall #include <netinet/in.h>
2726093Sminshall #include <arpa/tftp.h>
2860062Storek 
2926093Sminshall #include <stdio.h>
3060062Storek #include <unistd.h>
3126093Sminshall 
3260062Storek #include "tftpsubs.h"
3360062Storek 
3426093Sminshall #define PKTSIZE SEGSIZE+4       /* should be moved to tftp.h */
3526093Sminshall 
3626093Sminshall struct bf {
3726093Sminshall 	int counter;            /* size of data in buffer, or flag */
3826093Sminshall 	char buf[PKTSIZE];      /* room for data packet */
3926093Sminshall } bfs[2];
4026093Sminshall 
4126093Sminshall 				/* Values for bf.counter  */
4226093Sminshall #define BF_ALLOC -3             /* alloc'd but not yet filled */
4326093Sminshall #define BF_FREE  -2             /* free */
4426093Sminshall /* [-1 .. SEGSIZE] = size of data in the data buffer */
4526093Sminshall 
4660062Storek static int nextone;		/* index of next buffer to use */
4760062Storek static int current;		/* index of buffer in use */
4826093Sminshall 
4960062Storek 				/* control flags for crlf conversions */
5060062Storek int newline = 0;		/* fillbuf: in middle of newline expansion */
5160062Storek int prevchar = -1;		/* putbuf: previous char (cr check) */
5226093Sminshall 
5360062Storek static struct tftphdr *rw_init();
5426093Sminshall 
w_init()5526093Sminshall struct tftphdr *w_init() { return rw_init(0); }         /* write-behind */
r_init()5626093Sminshall struct tftphdr *r_init() { return rw_init(1); }         /* read-ahead */
5726093Sminshall 
5860062Storek static struct tftphdr *
rw_init(x)5960062Storek rw_init(x)			/* init for either read-ahead or write-behind */
6060062Storek 	int x;			/* zero for write-behind, one for read-head */
6126093Sminshall {
6260062Storek 	newline = 0;		/* init crlf flag */
6326093Sminshall 	prevchar = -1;
6426093Sminshall 	bfs[0].counter =  BF_ALLOC;     /* pass out the first buffer */
6526093Sminshall 	current = 0;
6626093Sminshall 	bfs[1].counter = BF_FREE;
6726093Sminshall 	nextone = x;                    /* ahead or behind? */
6826093Sminshall 	return (struct tftphdr *)bfs[0].buf;
6926093Sminshall }
7026093Sminshall 
7126093Sminshall 
7226093Sminshall /* Have emptied current buffer by sending to net and getting ack.
7326093Sminshall    Free it and return next buffer filled with data.
7426093Sminshall  */
7560062Storek int
readit(file,dpp,convert)7626093Sminshall readit(file, dpp, convert)
7726093Sminshall 	FILE *file;                     /* file opened for read */
7826093Sminshall 	struct tftphdr **dpp;
7926093Sminshall 	int convert;                    /* if true, convert to ascii */
8026093Sminshall {
8126093Sminshall 	struct bf *b;
8226093Sminshall 
8326093Sminshall 	bfs[current].counter = BF_FREE; /* free old one */
8426093Sminshall 	current = !current;             /* "incr" current */
8526093Sminshall 
8626093Sminshall 	b = &bfs[current];              /* look at new buffer */
8726093Sminshall 	if (b->counter == BF_FREE)      /* if it's empty */
8826093Sminshall 		read_ahead(file, convert);      /* fill it */
8960062Storek /*      assert(b->counter != BF_FREE);*//* check */
9026093Sminshall 	*dpp = (struct tftphdr *)b->buf;        /* set caller's ptr */
9126093Sminshall 	return b->counter;
9226093Sminshall }
9326093Sminshall 
9426093Sminshall /*
9526093Sminshall  * fill the input buffer, doing ascii conversions if requested
9626093Sminshall  * conversions are  lf -> cr,lf  and cr -> cr, nul
9726093Sminshall  */
9860062Storek void
read_ahead(file,convert)9926093Sminshall read_ahead(file, convert)
10026093Sminshall 	FILE *file;                     /* file opened for read */
10126093Sminshall 	int convert;                    /* if true, convert to ascii */
10226093Sminshall {
10326093Sminshall 	register int i;
10426093Sminshall 	register char *p;
10526093Sminshall 	register int c;
10626093Sminshall 	struct bf *b;
10726093Sminshall 	struct tftphdr *dp;
10826093Sminshall 
10926093Sminshall 	b = &bfs[nextone];              /* look at "next" buffer */
11026093Sminshall 	if (b->counter != BF_FREE)      /* nop if not free */
11126093Sminshall 		return;
11226093Sminshall 	nextone = !nextone;             /* "incr" next buffer ptr */
11326093Sminshall 
11426093Sminshall 	dp = (struct tftphdr *)b->buf;
11526093Sminshall 
11626093Sminshall 	if (convert == 0) {
11726093Sminshall 		b->counter = read(fileno(file), dp->th_data, SEGSIZE);
11826093Sminshall 		return;
11926093Sminshall 	}
12026093Sminshall 
12126093Sminshall 	p = dp->th_data;
12226093Sminshall 	for (i = 0 ; i < SEGSIZE; i++) {
12326093Sminshall 		if (newline) {
12426093Sminshall 			if (prevchar == '\n')
12526093Sminshall 				c = '\n';       /* lf to cr,lf */
12626093Sminshall 			else    c = '\0';       /* cr to cr,nul */
12726093Sminshall 			newline = 0;
12826093Sminshall 		}
12926093Sminshall 		else {
13026093Sminshall 			c = getc(file);
13126093Sminshall 			if (c == EOF) break;
13226093Sminshall 			if (c == '\n' || c == '\r') {
13326093Sminshall 				prevchar = c;
13426093Sminshall 				c = '\r';
13526093Sminshall 				newline = 1;
13626093Sminshall 			}
13726093Sminshall 		}
13826093Sminshall 	       *p++ = c;
13926093Sminshall 	}
14026093Sminshall 	b->counter = (int)(p - dp->th_data);
14126093Sminshall }
14226093Sminshall 
14326093Sminshall /* Update count associated with the buffer, get new buffer
14426093Sminshall    from the queue.  Calls write_behind only if next buffer not
14526093Sminshall    available.
14626093Sminshall  */
14760062Storek int
writeit(file,dpp,ct,convert)14826093Sminshall writeit(file, dpp, ct, convert)
14926093Sminshall 	FILE *file;
15026093Sminshall 	struct tftphdr **dpp;
15160062Storek 	int ct, convert;
15226093Sminshall {
15326093Sminshall 	bfs[current].counter = ct;      /* set size of data to write */
15426093Sminshall 	current = !current;             /* switch to other buffer */
15526093Sminshall 	if (bfs[current].counter != BF_FREE)     /* if not free */
15660062Storek 		(void)write_behind(file, convert); /* flush it */
15726093Sminshall 	bfs[current].counter = BF_ALLOC;        /* mark as alloc'd */
15826093Sminshall 	*dpp =  (struct tftphdr *)bfs[current].buf;
15926093Sminshall 	return ct;                      /* this is a lie of course */
16026093Sminshall }
16126093Sminshall 
16226093Sminshall /*
16326093Sminshall  * Output a buffer to a file, converting from netascii if requested.
16426093Sminshall  * CR,NUL -> CR  and CR,LF => LF.
16526093Sminshall  * Note spec is undefined if we get CR as last byte of file or a
16626093Sminshall  * CR followed by anything else.  In this case we leave it alone.
16726093Sminshall  */
16860062Storek int
write_behind(file,convert)16926093Sminshall write_behind(file, convert)
17026093Sminshall 	FILE *file;
17126093Sminshall 	int convert;
17226093Sminshall {
17326093Sminshall 	char *buf;
17426093Sminshall 	int count;
17526093Sminshall 	register int ct;
17626093Sminshall 	register char *p;
17726093Sminshall 	register int c;                 /* current character */
17826093Sminshall 	struct bf *b;
17926093Sminshall 	struct tftphdr *dp;
18026093Sminshall 
18126093Sminshall 	b = &bfs[nextone];
18226093Sminshall 	if (b->counter < -1)            /* anything to flush? */
18326093Sminshall 		return 0;               /* just nop if nothing to do */
18426093Sminshall 
18526093Sminshall 	count = b->counter;             /* remember byte count */
18626093Sminshall 	b->counter = BF_FREE;           /* reset flag */
18726093Sminshall 	dp = (struct tftphdr *)b->buf;
18826093Sminshall 	nextone = !nextone;             /* incr for next time */
18926093Sminshall 	buf = dp->th_data;
19026093Sminshall 
19126093Sminshall 	if (count <= 0) return -1;      /* nak logic? */
19226093Sminshall 
19326093Sminshall 	if (convert == 0)
19426093Sminshall 		return write(fileno(file), buf, count);
19526093Sminshall 
19626093Sminshall 	p = buf;
19726093Sminshall 	ct = count;
19826093Sminshall 	while (ct--) {                  /* loop over the buffer */
19926093Sminshall 	    c = *p++;                   /* pick up a character */
20026093Sminshall 	    if (prevchar == '\r') {     /* if prev char was cr */
20126093Sminshall 		if (c == '\n')          /* if have cr,lf then just */
20226093Sminshall 		   fseek(file, -1, 1);  /* smash lf on top of the cr */
20326093Sminshall 		else
20426093Sminshall 		   if (c == '\0')       /* if have cr,nul then */
20526093Sminshall 			goto skipit;    /* just skip over the putc */
20626093Sminshall 		/* else just fall through and allow it */
20726093Sminshall 	    }
20826093Sminshall 	    putc(c, file);
20926093Sminshall skipit:
21026093Sminshall 	    prevchar = c;
21126093Sminshall 	}
21226093Sminshall 	return count;
21326093Sminshall }
21426093Sminshall 
21526093Sminshall 
21626114Sminshall /* When an error has occurred, it is possible that the two sides
21726114Sminshall  * are out of synch.  Ie: that what I think is the other side's
21826114Sminshall  * response to packet N is really their response to packet N-1.
21926114Sminshall  *
22026114Sminshall  * So, to try to prevent that, we flush all the input queued up
22126114Sminshall  * for us on the network connection on our host.
22226114Sminshall  *
22326114Sminshall  * We return the number of packets we flushed (mostly for reporting
22426114Sminshall  * when trace is active).
22526114Sminshall  */
22626114Sminshall 
22726114Sminshall int
synchnet(f)22826114Sminshall synchnet(f)
22960062Storek 	int	f;		/* socket to flush */
23026114Sminshall {
23126114Sminshall 	int i, j = 0;
23226114Sminshall 	char rbuf[PKTSIZE];
23326114Sminshall 	struct sockaddr_in from;
23426114Sminshall 	int fromlen;
23526114Sminshall 
23626114Sminshall 	while (1) {
23726114Sminshall 		(void) ioctl(f, FIONREAD, &i);
23826114Sminshall 		if (i) {
23926114Sminshall 			j++;
24026114Sminshall 			fromlen = sizeof from;
24126114Sminshall 			(void) recvfrom(f, rbuf, sizeof (rbuf), 0,
24246771Sbostic 				(struct sockaddr *)&from, &fromlen);
24326114Sminshall 		} else {
24426114Sminshall 			return(j);
24526114Sminshall 		}
24626114Sminshall 	}
24726114Sminshall }
248