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 $ */
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
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
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
=
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.
59 #include <sys/types.h>
60 #include <sys/socket.h>
61 #include <sys/ioctl.h>
63 #include <netinet/in.h>
64 #include <arpa/tftp.h>
68 #include "../config.h"
70 #define PKTSIZE MAX_SEGSIZE+4 /* should be moved to tftp.h */
72 int segsize
= SEGSIZE
; /* Default segsize */
75 int counter
; /* size of data in buffer, or flag */
76 char buf
[PKTSIZE
]; /* room for data packet */
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
*
101 newline
= 0; /* init crlf flag */
103 bfs
[0].counter
= BF_ALLOC
; /* pass out the first buffer */
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
)
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 */
131 * fill the input buffer, doing ascii conversions if requested
132 * conversions are lf -> cr,lf and cr -> cr, nul
135 read_ahead(FILE *file
, int convert
)
143 b
= &bfs
[nextone
]; /* look at "next" buffer */
144 if (b
->counter
!= BF_FREE
) /* nop if not free */
146 nextone
= !nextone
; /* "incr" next buffer ptr */
148 dp
= (struct tftphdr
*)b
->buf
;
151 b
->counter
= read(fileno(file
), dp
->th_data
, segsize
);
156 for (i
= 0 ; i
< segsize
; i
++) {
158 if (prevchar
== '\n')
159 c
= '\n'; /* lf to cr,lf */
160 else c
= '\0'; /* cr to cr,nul */
166 if (c
== '\n' || c
== '\r') {
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
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
)
206 int c
; /* current character */
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 */
220 if (count
<= 0) return -1; /* nak logic? */
223 return write(fileno(file
), buf
, 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 */
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 */
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 */
261 struct sockaddr_in from
;
264 struct timeval notime
;
267 notime
.tv_sec
= notime
.tv_usec
= 0;
270 FD_SET(f
, &socketset
);
272 if ( select(f
, &socketset
, NULL
, NULL
, ¬ime
) <= 0 )
273 break; /* Nothing to read */
275 /* Otherwise drain the packet */
277 fromlen
= sizeof from
;
278 (void) recvfrom(f
, rbuf
, sizeof (rbuf
), 0,
279 (struct sockaddr
*)&from
, &fromlen
);
282 return pktcount
; /* Return packets drained */