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