xref: /netbsd-src/usr.bin/tftp/tftpsubs.c (revision 5e4c038a45edbc7d63b7c2daa76e29f88b64a4e3)
1 /*	$NetBSD: tftpsubs.c,v 1.6 1999/07/12 20:19:21 itojun Exp $	*/
2 
3 /*
4  * Copyright (c) 1983, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)tftpsubs.c	8.1 (Berkeley) 6/6/93";
40 #else
41 __RCSID("$NetBSD: tftpsubs.c,v 1.6 1999/07/12 20:19:21 itojun Exp $");
42 #endif
43 #endif /* not lint */
44 
45 /* Simple minded read-ahead/write-behind subroutines for tftp user and
46    server.  Written originally with multiple buffers in mind, but current
47    implementation has two buffer logic wired in.
48 
49    Todo:  add some sort of final error check so when the write-buffer
50    is finally flushed, the caller can detect if the disk filled up
51    (or had an i/o error) and return a nak to the other side.
52 
53 			Jim Guyton 10/85
54  */
55 
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <sys/ioctl.h>
59 #include <netinet/in.h>
60 #include <arpa/tftp.h>
61 
62 #include <stdio.h>
63 #include <unistd.h>
64 
65 #include "tftpsubs.h"
66 
67 #define PKTSIZE SEGSIZE+4       /* should be moved to tftp.h */
68 
69 struct bf {
70 	int counter;            /* size of data in buffer, or flag */
71 	char buf[PKTSIZE];      /* room for data packet */
72 } bfs[2];
73 
74 				/* Values for bf.counter  */
75 #define BF_ALLOC -3             /* alloc'd but not yet filled */
76 #define BF_FREE  -2             /* free */
77 /* [-1 .. SEGSIZE] = size of data in the data buffer */
78 
79 static int nextone;		/* index of next buffer to use */
80 static int current;		/* index of buffer in use */
81 
82 				/* control flags for crlf conversions */
83 int newline = 0;		/* fillbuf: in middle of newline expansion */
84 int prevchar = -1;		/* putbuf: previous char (cr check) */
85 
86 static struct tftphdr *rw_init __P((int));
87 
88 struct tftphdr *w_init() { return rw_init(0); }	/* write-behind */
89 struct tftphdr *r_init() { return rw_init(1); }	/* read-ahead */
90 
91 static struct tftphdr *
92 rw_init(x)			/* init for either read-ahead or write-behind */
93 	int x;			/* zero for write-behind, one for read-head */
94 {
95 	newline = 0;		/* init crlf flag */
96 	prevchar = -1;
97 	bfs[0].counter =  BF_ALLOC;     /* pass out the first buffer */
98 	current = 0;
99 	bfs[1].counter = BF_FREE;
100 	nextone = x;                    /* ahead or behind? */
101 	return (struct tftphdr *)bfs[0].buf;
102 }
103 
104 /* Have emptied current buffer by sending to net and getting ack.
105    Free it and return next buffer filled with data.
106  */
107 int
108 readit(file, dpp, convert)
109 	FILE *file;                     /* file opened for read */
110 	struct tftphdr **dpp;
111 	int convert;                    /* if true, convert to ascii */
112 {
113 	struct bf *b;
114 
115 	bfs[current].counter = BF_FREE; /* free old one */
116 	current = !current;             /* "incr" current */
117 
118 	b = &bfs[current];              /* look at new buffer */
119 	if (b->counter == BF_FREE)      /* if it's empty */
120 		read_ahead(file, convert);      /* fill it */
121 /*      assert(b->counter != BF_FREE);*//* check */
122 	*dpp = (struct tftphdr *)b->buf;        /* set caller's ptr */
123 	return b->counter;
124 }
125 
126 /*
127  * fill the input buffer, doing ascii conversions if requested
128  * conversions are  lf -> cr,lf  and cr -> cr, nul
129  */
130 void
131 read_ahead(file, convert)
132 	FILE *file;                     /* file opened for read */
133 	int convert;                    /* if true, convert to ascii */
134 {
135 	int i;
136 	char *p;
137 	int c;
138 	struct bf *b;
139 	struct tftphdr *dp;
140 
141 	b = &bfs[nextone];              /* look at "next" buffer */
142 	if (b->counter != BF_FREE)      /* nop if not free */
143 		return;
144 	nextone = !nextone;             /* "incr" next buffer ptr */
145 
146 	dp = (struct tftphdr *)b->buf;
147 
148 	if (convert == 0) {
149 		b->counter = read(fileno(file), dp->th_data, SEGSIZE);
150 		return;
151 	}
152 
153 	p = dp->th_data;
154 	for (i = 0 ; i < SEGSIZE; i++) {
155 		if (newline) {
156 			if (prevchar == '\n')
157 				c = '\n';       /* lf to cr,lf */
158 			else    c = '\0';       /* cr to cr,nul */
159 			newline = 0;
160 		}
161 		else {
162 			c = getc(file);
163 			if (c == EOF) break;
164 			if (c == '\n' || c == '\r') {
165 				prevchar = c;
166 				c = '\r';
167 				newline = 1;
168 			}
169 		}
170 	       *p++ = c;
171 	}
172 	b->counter = (int)(p - dp->th_data);
173 }
174 
175 /* Update count associated with the buffer, get new buffer
176    from the queue.  Calls write_behind only if next buffer not
177    available.
178  */
179 int
180 writeit(file, dpp, ct, convert)
181 	FILE *file;
182 	struct tftphdr **dpp;
183 	int ct, convert;
184 {
185 	bfs[current].counter = ct;      /* set size of data to write */
186 	current = !current;             /* switch to other buffer */
187 	if (bfs[current].counter != BF_FREE)     /* if not free */
188 		(void)write_behind(file, convert); /* flush it */
189 	bfs[current].counter = BF_ALLOC;        /* mark as alloc'd */
190 	*dpp =  (struct tftphdr *)bfs[current].buf;
191 	return ct;                      /* this is a lie of course */
192 }
193 
194 /*
195  * Output a buffer to a file, converting from netascii if requested.
196  * CR,NUL -> CR  and CR,LF => LF.
197  * Note spec is undefined if we get CR as last byte of file or a
198  * CR followed by anything else.  In this case we leave it alone.
199  */
200 int
201 write_behind(file, convert)
202 	FILE *file;
203 	int convert;
204 {
205 	char *buf;
206 	int count;
207 	int ct;
208 	char *p;
209 	int c;				/* current character */
210 	struct bf *b;
211 	struct tftphdr *dp;
212 
213 	b = &bfs[nextone];
214 	if (b->counter < -1)            /* anything to flush? */
215 		return 0;               /* just nop if nothing to do */
216 
217 	count = b->counter;             /* remember byte count */
218 	b->counter = BF_FREE;           /* reset flag */
219 	dp = (struct tftphdr *)b->buf;
220 	nextone = !nextone;             /* incr for next time */
221 	buf = dp->th_data;
222 
223 	if (count <= 0) return -1;      /* nak logic? */
224 
225 	if (convert == 0)
226 		return write(fileno(file), buf, count);
227 
228 	p = buf;
229 	ct = count;
230 	while (ct--) {                  /* loop over the buffer */
231 	    c = *p++;                   /* pick up a character */
232 	    if (prevchar == '\r') {     /* if prev char was cr */
233 		if (c == '\n')          /* if have cr,lf then just */
234 		   fseek(file, -1, 1);  /* smash lf on top of the cr */
235 		else
236 		   if (c == '\0')       /* if have cr,nul then */
237 			goto skipit;    /* just skip over the putc */
238 		/* else just fall through and allow it */
239 	    }
240 	    putc(c, file);
241 skipit:
242 	    prevchar = c;
243 	}
244 	return count;
245 }
246 
247 
248 /* When an error has occurred, it is possible that the two sides
249  * are out of synch.  Ie: that what I think is the other side's
250  * response to packet N is really their response to packet N-1.
251  *
252  * So, to try to prevent that, we flush all the input queued up
253  * for us on the network connection on our host.
254  *
255  * We return the number of packets we flushed (mostly for reporting
256  * when trace is active).
257  */
258 
259 int
260 synchnet(f)
261 	int	f;		/* socket to flush */
262 {
263 	int i, j = 0;
264 	char rbuf[PKTSIZE];
265 	struct sockaddr_storage from;
266 	int fromlen;
267 
268 	while (1) {
269 		(void) ioctl(f, FIONREAD, &i);
270 		if (i) {
271 			j++;
272 			fromlen = sizeof from;
273 			(void) recvfrom(f, rbuf, sizeof (rbuf), 0,
274 				(struct sockaddr *)&from, &fromlen);
275 		} else {
276 			return(j);
277 		}
278 	}
279 }
280