]>
git.saurik.com Git - apple/network_cmds.git/blob - tftp.tproj/tftpsubs.c
38a5e1ced0e1eb68b80c4bbfd0324505e87c9207
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 * Copyright (c) 1983, 1993
27 * The Regents of the University of California. All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
32 * 1. Redistributions of source code must retain the above copyright
33 * notice, this list of conditions and the following disclaimer.
34 * 2. Redistributions in binary form must reproduce the above copyright
35 * notice, this list of conditions and the following disclaimer in the
36 * documentation and/or other materials provided with the distribution.
37 * 3. All advertising materials mentioning features or use of this software
38 * must display the following acknowledgement:
39 * This product includes software developed by the University of
40 * California, Berkeley and its contributors.
41 * 4. Neither the name of the University nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
45 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 /* Simple minded read-ahead/write-behind subroutines for tftp user and
60 server. Written originally with multiple buffers in mind, but current
61 implementation has two buffer logic wired in.
63 Todo: add some sort of final error check so when the write-buffer
64 is finally flushed, the caller can detect if the disk filled up
65 (or had an i/o error) and return a nak to the other side.
70 #include <sys/types.h>
71 #include <sys/socket.h>
72 #include <sys/ioctl.h>
73 #include <netinet/in.h>
74 #include <arpa/tftp.h>
81 #define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */
84 int counter
; /* size of data in buffer, or flag */
85 char buf
[PKTSIZE
]; /* room for data packet */
88 /* Values for bf.counter */
89 #define BF_ALLOC -3 /* alloc'd but not yet filled */
90 #define BF_FREE -2 /* free */
91 /* [-1 .. SEGSIZE] = size of data in the data buffer */
93 static int nextone
; /* index of next buffer to use */
94 static int current
; /* index of buffer in use */
96 /* control flags for crlf conversions */
97 int newline
= 0; /* fillbuf: in middle of newline expansion */
98 int prevchar
= -1; /* putbuf: previous char (cr check) */
100 static struct tftphdr
*rw_init();
102 struct tftphdr
*w_init() { return rw_init(0); } /* write-behind */
103 struct tftphdr
*r_init() { return rw_init(1); } /* read-ahead */
105 static struct tftphdr
*
106 rw_init(x
) /* init for either read-ahead or write-behind */
107 int x
; /* zero for write-behind, one for read-head */
109 newline
= 0; /* init crlf flag */
111 bfs
[0].counter
= BF_ALLOC
; /* pass out the first buffer */
113 bfs
[1].counter
= BF_FREE
;
114 nextone
= x
; /* ahead or behind? */
115 return (struct tftphdr
*)bfs
[0].buf
;
119 /* Have emptied current buffer by sending to net and getting ack.
120 Free it and return next buffer filled with data.
123 readit(file
, dpp
, convert
)
124 FILE *file
; /* file opened for read */
125 struct tftphdr
**dpp
;
126 int convert
; /* if true, convert to ascii */
130 bfs
[current
].counter
= BF_FREE
; /* free old one */
131 current
= !current
; /* "incr" current */
133 b
= &bfs
[current
]; /* look at new buffer */
134 if (b
->counter
== BF_FREE
) /* if it's empty */
135 read_ahead(file
, convert
); /* fill it */
136 /* assert(b->counter != BF_FREE);*//* check */
137 *dpp
= (struct tftphdr
*)b
->buf
; /* set caller's ptr */
142 * fill the input buffer, doing ascii conversions if requested
143 * conversions are lf -> cr,lf and cr -> cr, nul
146 read_ahead(file
, convert
)
147 FILE *file
; /* file opened for read */
148 int convert
; /* if true, convert to ascii */
156 b
= &bfs
[nextone
]; /* look at "next" buffer */
157 if (b
->counter
!= BF_FREE
) /* nop if not free */
159 nextone
= !nextone
; /* "incr" next buffer ptr */
161 dp
= (struct tftphdr
*)b
->buf
;
164 b
->counter
= read(fileno(file
), dp
->th_data
, SEGSIZE
);
169 for (i
= 0 ; i
< SEGSIZE
; i
++) {
171 if (prevchar
== '\n')
172 c
= '\n'; /* lf to cr,lf */
173 else c
= '\0'; /* cr to cr,nul */
179 if (c
== '\n' || c
== '\r') {
187 b
->counter
= (int)(p
- dp
->th_data
);
190 /* Update count associated with the buffer, get new buffer
191 from the queue. Calls write_behind only if next buffer not
195 writeit(file
, dpp
, ct
, convert
)
197 struct tftphdr
**dpp
;
200 bfs
[current
].counter
= ct
; /* set size of data to write */
201 current
= !current
; /* switch to other buffer */
202 if (bfs
[current
].counter
!= BF_FREE
) /* if not free */
203 (void)write_behind(file
, convert
); /* flush it */
204 bfs
[current
].counter
= BF_ALLOC
; /* mark as alloc'd */
205 *dpp
= (struct tftphdr
*)bfs
[current
].buf
;
206 return ct
; /* this is a lie of course */
210 * Output a buffer to a file, converting from netascii if requested.
211 * CR,NUL -> CR and CR,LF => LF.
212 * Note spec is undefined if we get CR as last byte of file or a
213 * CR followed by anything else. In this case we leave it alone.
216 write_behind(file
, convert
)
224 register int c
; /* current character */
229 if (b
->counter
< -1) /* anything to flush? */
230 return 0; /* just nop if nothing to do */
232 count
= b
->counter
; /* remember byte count */
233 b
->counter
= BF_FREE
; /* reset flag */
234 dp
= (struct tftphdr
*)b
->buf
;
235 nextone
= !nextone
; /* incr for next time */
238 if (count
<= 0) return -1; /* nak logic? */
241 return write(fileno(file
), buf
, count
);
245 while (ct
--) { /* loop over the buffer */
246 c
= *p
++; /* pick up a character */
247 if (prevchar
== '\r') { /* if prev char was cr */
248 if (c
== '\n') /* if have cr,lf then just */
249 fseek(file
, -1, 1); /* smash lf on top of the cr */
251 if (c
== '\0') /* if have cr,nul then */
252 goto skipit
; /* just skip over the putc */
253 /* else just fall through and allow it */
263 /* When an error has occurred, it is possible that the two sides
264 * are out of synch. Ie: that what I think is the other side's
265 * response to packet N is really their response to packet N-1.
267 * So, to try to prevent that, we flush all the input queued up
268 * for us on the network connection on our host.
270 * We return the number of packets we flushed (mostly for reporting
271 * when trace is active).
276 int f
; /* socket to flush */
280 struct sockaddr_in from
;
284 (void) ioctl(f
, FIONREAD
, &i
);
287 fromlen
= sizeof from
;
288 (void) recvfrom(f
, rbuf
, sizeof (rbuf
), 0,
289 (struct sockaddr
*)&from
, &fromlen
);