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