xref: /csrg-svn/usr.bin/tftp/tftpsubs.c (revision 33821)
1 /*
2  * Copyright (c) 1983 Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of California at Berkeley. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12 
13 #ifndef lint
14 static char sccsid[] = "@(#)tftpsubs.c	5.3 (Berkeley) 03/28/88";
15 #endif /* not lint */
16 
17 /* Simple minded read-ahead/write-behind subroutines for tftp user and
18    server.  Written originally with multiple buffers in mind, but current
19    implementation has two buffer logic wired in.
20 
21    Todo:  add some sort of final error check so when the write-buffer
22    is finally flushed, the caller can detect if the disk filled up
23    (or had an i/o error) and return a nak to the other side.
24 
25 			Jim Guyton 10/85
26  */
27 
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <sys/ioctl.h>
31 #include <netinet/in.h>
32 #include <arpa/tftp.h>
33 #include <stdio.h>
34 
35 #define PKTSIZE SEGSIZE+4       /* should be moved to tftp.h */
36 
37 struct bf {
38 	int counter;            /* size of data in buffer, or flag */
39 	char buf[PKTSIZE];      /* room for data packet */
40 } bfs[2];
41 
42 				/* Values for bf.counter  */
43 #define BF_ALLOC -3             /* alloc'd but not yet filled */
44 #define BF_FREE  -2             /* free */
45 /* [-1 .. SEGSIZE] = size of data in the data buffer */
46 
47 static int nextone;     /* index of next buffer to use */
48 static int current;     /* index of buffer in use */
49 
50 			/* control flags for crlf conversions */
51 int newline = 0;        /* fillbuf: in middle of newline expansion */
52 int prevchar = -1;      /* putbuf: previous char (cr check) */
53 
54 struct tftphdr *rw_init();
55 
56 struct tftphdr *w_init() { return rw_init(0); }         /* write-behind */
57 struct tftphdr *r_init() { return rw_init(1); }         /* read-ahead */
58 
59 struct tftphdr *
60 rw_init(x)              /* init for either read-ahead or write-behind */
61 int x;                  /* zero for write-behind, one for read-head */
62 {
63 	newline = 0;            /* init crlf flag */
64 	prevchar = -1;
65 	bfs[0].counter =  BF_ALLOC;     /* pass out the first buffer */
66 	current = 0;
67 	bfs[1].counter = BF_FREE;
68 	nextone = x;                    /* ahead or behind? */
69 	return (struct tftphdr *)bfs[0].buf;
70 }
71 
72 
73 /* Have emptied current buffer by sending to net and getting ack.
74    Free it and return next buffer filled with data.
75  */
76 readit(file, dpp, convert)
77 	FILE *file;                     /* file opened for read */
78 	struct tftphdr **dpp;
79 	int convert;                    /* if true, convert to ascii */
80 {
81 	struct bf *b;
82 
83 	bfs[current].counter = BF_FREE; /* free old one */
84 	current = !current;             /* "incr" current */
85 
86 	b = &bfs[current];              /* look at new buffer */
87 	if (b->counter == BF_FREE)      /* if it's empty */
88 		read_ahead(file, convert);      /* fill it */
89 /*      assert(b->counter != BF_FREE);  /* check */
90 	*dpp = (struct tftphdr *)b->buf;        /* set caller's ptr */
91 	return b->counter;
92 }
93 
94 /*
95  * fill the input buffer, doing ascii conversions if requested
96  * conversions are  lf -> cr,lf  and cr -> cr, nul
97  */
98 read_ahead(file, convert)
99 	FILE *file;                     /* file opened for read */
100 	int convert;                    /* if true, convert to ascii */
101 {
102 	register int i;
103 	register char *p;
104 	register int c;
105 	struct bf *b;
106 	struct tftphdr *dp;
107 
108 	b = &bfs[nextone];              /* look at "next" buffer */
109 	if (b->counter != BF_FREE)      /* nop if not free */
110 		return;
111 	nextone = !nextone;             /* "incr" next buffer ptr */
112 
113 	dp = (struct tftphdr *)b->buf;
114 
115 	if (convert == 0) {
116 		b->counter = read(fileno(file), dp->th_data, SEGSIZE);
117 		return;
118 	}
119 
120 	p = dp->th_data;
121 	for (i = 0 ; i < SEGSIZE; i++) {
122 		if (newline) {
123 			if (prevchar == '\n')
124 				c = '\n';       /* lf to cr,lf */
125 			else    c = '\0';       /* cr to cr,nul */
126 			newline = 0;
127 		}
128 		else {
129 			c = getc(file);
130 			if (c == EOF) break;
131 			if (c == '\n' || c == '\r') {
132 				prevchar = c;
133 				c = '\r';
134 				newline = 1;
135 			}
136 		}
137 	       *p++ = c;
138 	}
139 	b->counter = (int)(p - dp->th_data);
140 }
141 
142 /* Update count associated with the buffer, get new buffer
143    from the queue.  Calls write_behind only if next buffer not
144    available.
145  */
146 writeit(file, dpp, ct, convert)
147 	FILE *file;
148 	struct tftphdr **dpp;
149 	int convert;
150 {
151 	bfs[current].counter = ct;      /* set size of data to write */
152 	current = !current;             /* switch to other buffer */
153 	if (bfs[current].counter != BF_FREE)     /* if not free */
154 		write_behind(file, convert);     /* flush it */
155 	bfs[current].counter = BF_ALLOC;        /* mark as alloc'd */
156 	*dpp =  (struct tftphdr *)bfs[current].buf;
157 	return ct;                      /* this is a lie of course */
158 }
159 
160 /*
161  * Output a buffer to a file, converting from netascii if requested.
162  * CR,NUL -> CR  and CR,LF => LF.
163  * Note spec is undefined if we get CR as last byte of file or a
164  * CR followed by anything else.  In this case we leave it alone.
165  */
166 write_behind(file, convert)
167 	FILE *file;
168 	int convert;
169 {
170 	char *buf;
171 	int count;
172 	register int ct;
173 	register char *p;
174 	register int c;                 /* current character */
175 	struct bf *b;
176 	struct tftphdr *dp;
177 
178 	b = &bfs[nextone];
179 	if (b->counter < -1)            /* anything to flush? */
180 		return 0;               /* just nop if nothing to do */
181 
182 	count = b->counter;             /* remember byte count */
183 	b->counter = BF_FREE;           /* reset flag */
184 	dp = (struct tftphdr *)b->buf;
185 	nextone = !nextone;             /* incr for next time */
186 	buf = dp->th_data;
187 
188 	if (count <= 0) return -1;      /* nak logic? */
189 
190 	if (convert == 0)
191 		return write(fileno(file), buf, count);
192 
193 	p = buf;
194 	ct = count;
195 	while (ct--) {                  /* loop over the buffer */
196 	    c = *p++;                   /* pick up a character */
197 	    if (prevchar == '\r') {     /* if prev char was cr */
198 		if (c == '\n')          /* if have cr,lf then just */
199 		   fseek(file, -1, 1);  /* smash lf on top of the cr */
200 		else
201 		   if (c == '\0')       /* if have cr,nul then */
202 			goto skipit;    /* just skip over the putc */
203 		/* else just fall through and allow it */
204 	    }
205 	    putc(c, file);
206 skipit:
207 	    prevchar = c;
208 	}
209 	return count;
210 }
211 
212 
213 /* When an error has occurred, it is possible that the two sides
214  * are out of synch.  Ie: that what I think is the other side's
215  * response to packet N is really their response to packet N-1.
216  *
217  * So, to try to prevent that, we flush all the input queued up
218  * for us on the network connection on our host.
219  *
220  * We return the number of packets we flushed (mostly for reporting
221  * when trace is active).
222  */
223 
224 int
225 synchnet(f)
226 int	f;		/* socket to flush */
227 {
228 	int i, j = 0;
229 	char rbuf[PKTSIZE];
230 	struct sockaddr_in from;
231 	int fromlen;
232 
233 	while (1) {
234 		(void) ioctl(f, FIONREAD, &i);
235 		if (i) {
236 			j++;
237 			fromlen = sizeof from;
238 			(void) recvfrom(f, rbuf, sizeof (rbuf), 0,
239 				(caddr_t)&from, &fromlen);
240 		} else {
241 			return(j);
242 		}
243 	}
244 }
245