2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * From: @(#)tftpsubs.c 5.6 (Berkeley) 2/28/91
38 "$Id: tftpsubs.c,v 1.5 1996/08/29 22:25:27 dholland Exp $";
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.
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.
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>
61 #define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */
64 int counter; /* size of data in buffer, or flag */
65 char buf[PKTSIZE]; /* room for data packet */
68 /* Values for bf.counter */
69 #define BF_ALLOC -3 /* alloc'd but not yet filled */
70 #define BF_FREE -2 /* free */
71 /* [-1 .. SEGSIZE] = size of data in the data buffer */
73 static int nextone; /* index of next buffer to use */
74 static int current; /* index of buffer in use */
76 /* control flags for crlf conversions */
77 int newline = 0; /* fillbuf: in middle of newline expansion */
78 int prevchar = -1; /* putbuf: previous char (cr check) */
80 void read_ahead(FILE *file, int convert /* if true, convert to ascii */);
81 int write_behind(FILE *file, int convert);
82 struct tftphdr *rw_init(int);
84 struct tftphdr *w_init(void) { return rw_init(0); } /* write-behind */
85 struct tftphdr *r_init(void) { return rw_init(1); } /* read-ahead */
88 rw_init(int x) /* init for either read-ahead or write-behind */
89 { /* zero for write-behind, one for read-head */
90 newline = 0; /* init crlf flag */
92 bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
94 bfs[1].counter = BF_FREE;
95 nextone = x; /* ahead or behind? */
96 return (struct tftphdr *)bfs[0].buf;
100 /* Have emptied current buffer by sending to net and getting ack.
101 Free it and return next buffer filled with data.
104 readit(FILE *file, struct tftphdr **dpp,
105 int convert /* if true, convert to ascii */)
109 bfs[current].counter = BF_FREE; /* free old one */
110 current = !current; /* "incr" current */
112 b = &bfs[current]; /* look at new buffer */
113 if (b->counter == BF_FREE) /* if it's empty */
114 read_ahead(file, convert); /* fill it */
115 /* assert(b->counter != BF_FREE); */ /* check */
116 *dpp = (struct tftphdr *)b->buf; /* set caller's ptr */
121 * fill the input buffer, doing ascii conversions if requested
122 * conversions are lf -> cr,lf and cr -> cr, nul
125 read_ahead(FILE *file, int convert /* if true, convert to ascii */)
133 b = &bfs[nextone]; /* look at "next" buffer */
134 if (b->counter != BF_FREE) /* nop if not free */
136 nextone = !nextone; /* "incr" next buffer ptr */
138 dp = (struct tftphdr *)b->buf;
141 b->counter = read(fileno(file), dp->th_data, SEGSIZE);
146 for (i = 0 ; i < SEGSIZE; i++) {
148 if (prevchar == '\n')
149 c = '\n'; /* lf to cr,lf */
150 else c = '\0'; /* cr to cr,nul */
156 if (c == '\n' || c == '\r') {
164 b->counter = (int)(p - dp->th_data);
167 /* Update count associated with the buffer, get new buffer
168 from the queue. Calls write_behind only if next buffer not
172 writeit(FILE *file, struct tftphdr **dpp, int ct, int convert)
174 bfs[current].counter = ct; /* set size of data to write */
175 current = !current; /* switch to other buffer */
176 if (bfs[current].counter != BF_FREE) /* if not free */
177 write_behind(file, convert); /* flush it */
178 bfs[current].counter = BF_ALLOC; /* mark as alloc'd */
179 *dpp = (struct tftphdr *)bfs[current].buf;
180 return ct; /* this is a lie of course */
184 * Output a buffer to a file, converting from netascii if requested.
185 * CR,NUL -> CR and CR,LF => LF.
186 * Note spec is undefined if we get CR as last byte of file or a
187 * CR followed by anything else. In this case we leave it alone.
190 write_behind(FILE *file, int convert)
196 register int c; /* current character */
201 if (b->counter < -1) /* anything to flush? */
202 return 0; /* just nop if nothing to do */
204 count = b->counter; /* remember byte count */
205 b->counter = BF_FREE; /* reset flag */
206 dp = (struct tftphdr *)b->buf;
207 nextone = !nextone; /* incr for next time */
210 if (count <= 0) return -1; /* nak logic? */
213 return write(fileno(file), buf, count);
217 while (ct--) { /* loop over the buffer */
218 c = *p++; /* pick up a character */
219 if (prevchar == '\r') { /* if prev char was cr */
220 if (c == '\n') /* if have cr,lf then just */
221 fseek(file, -1, 1); /* smash lf on top of the cr */
223 if (c == '\0') /* if have cr,nul then */
224 goto skipit; /* just skip over the putc */
225 /* else just fall through and allow it */
235 /* When an error has occurred, it is possible that the two sides
236 * are out of synch. Ie: that what I think is the other side's
237 * response to packet N is really their response to packet N-1.
239 * So, to try to prevent that, we flush all the input queued up
240 * for us on the network connection on our host.
242 * We return the number of packets we flushed (mostly for reporting
243 * when trace is active).
247 synchnet(int f /* socket to flush */)
251 struct sockaddr_in from;
255 (void) ioctl(f, FIONREAD, &i);
258 fromlen = sizeof from;
259 (void) recvfrom(f, rbuf, sizeof (rbuf), 0,
260 (struct sockaddr *)&from, &fromlen);