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