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