]> git.saurik.com Git - apple/network_cmds.git/blame - tftp.tproj/tftpsubs.c
network_cmds-76.tar.gz
[apple/network_cmds.git] / tftp.tproj / tftpsubs.c
CommitLineData
b7080c8e
A
1/*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
12 * this file.
13 *
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
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24/*
25 * Copyright (c) 1983, 1993
26 * The Regents of the University of California. All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
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.
43 *
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
54 * SUCH DAMAGE.
55 */
56
57
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.
61
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.
65
66 Jim Guyton 10/85
67 */
68
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>
74
75#include <stdio.h>
76#include <unistd.h>
77
78#include "tftpsubs.h"
79
80#define PKTSIZE SEGSIZE+4 /* should be moved to tftp.h */
81
82struct bf {
83 int counter; /* size of data in buffer, or flag */
84 char buf[PKTSIZE]; /* room for data packet */
85} bfs[2];
86
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 */
91
92static int nextone; /* index of next buffer to use */
93static int current; /* index of buffer in use */
94
95 /* control flags for crlf conversions */
96int newline = 0; /* fillbuf: in middle of newline expansion */
97int prevchar = -1; /* putbuf: previous char (cr check) */
98
99static struct tftphdr *rw_init();
100
101struct tftphdr *w_init() { return rw_init(0); } /* write-behind */
102struct tftphdr *r_init() { return rw_init(1); } /* read-ahead */
103
104static struct tftphdr *
105rw_init(x) /* init for either read-ahead or write-behind */
106 int x; /* zero for write-behind, one for read-head */
107{
108 newline = 0; /* init crlf flag */
109 prevchar = -1;
110 bfs[0].counter = BF_ALLOC; /* pass out the first buffer */
111 current = 0;
112 bfs[1].counter = BF_FREE;
113 nextone = x; /* ahead or behind? */
114 return (struct tftphdr *)bfs[0].buf;
115}
116
117
118/* Have emptied current buffer by sending to net and getting ack.
119 Free it and return next buffer filled with data.
120 */
121int
122readit(file, dpp, convert)
123 FILE *file; /* file opened for read */
124 struct tftphdr **dpp;
125 int convert; /* if true, convert to ascii */
126{
127 struct bf *b;
128
129 bfs[current].counter = BF_FREE; /* free old one */
130 current = !current; /* "incr" current */
131
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 */
137 return b->counter;
138}
139
140/*
141 * fill the input buffer, doing ascii conversions if requested
142 * conversions are lf -> cr,lf and cr -> cr, nul
143 */
144void
145read_ahead(file, convert)
146 FILE *file; /* file opened for read */
147 int convert; /* if true, convert to ascii */
148{
149 register int i;
150 register char *p;
151 register int c;
152 struct bf *b;
153 struct tftphdr *dp;
154
155 b = &bfs[nextone]; /* look at "next" buffer */
156 if (b->counter != BF_FREE) /* nop if not free */
157 return;
158 nextone = !nextone; /* "incr" next buffer ptr */
159
160 dp = (struct tftphdr *)b->buf;
161
162 if (convert == 0) {
163 b->counter = read(fileno(file), dp->th_data, SEGSIZE);
164 return;
165 }
166
167 p = dp->th_data;
168 for (i = 0 ; i < SEGSIZE; i++) {
169 if (newline) {
170 if (prevchar == '\n')
171 c = '\n'; /* lf to cr,lf */
172 else c = '\0'; /* cr to cr,nul */
173 newline = 0;
174 }
175 else {
176 c = getc(file);
177 if (c == EOF) break;
178 if (c == '\n' || c == '\r') {
179 prevchar = c;
180 c = '\r';
181 newline = 1;
182 }
183 }
184 *p++ = c;
185 }
186 b->counter = (int)(p - dp->th_data);
187}
188
189/* Update count associated with the buffer, get new buffer
190 from the queue. Calls write_behind only if next buffer not
191 available.
192 */
193int
194writeit(file, dpp, ct, convert)
195 FILE *file;
196 struct tftphdr **dpp;
197 int ct, convert;
198{
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 */
206}
207
208/*
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.
213 */
214int
215write_behind(file, convert)
216 FILE *file;
217 int convert;
218{
219 char *buf;
220 int count;
221 register int ct;
222 register char *p;
223 register int c; /* current character */
224 struct bf *b;
225 struct tftphdr *dp;
226
227 b = &bfs[nextone];
228 if (b->counter < -1) /* anything to flush? */
229 return 0; /* just nop if nothing to do */
230
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 */
235 buf = dp->th_data;
236
237 if (count <= 0) return -1; /* nak logic? */
238
239 if (convert == 0)
240 return write(fileno(file), buf, count);
241
242 p = buf;
243 ct = 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 */
249 else
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 */
253 }
254 putc(c, file);
255skipit:
256 prevchar = c;
257 }
258 return count;
259}
260
261
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.
265 *
266 * So, to try to prevent that, we flush all the input queued up
267 * for us on the network connection on our host.
268 *
269 * We return the number of packets we flushed (mostly for reporting
270 * when trace is active).
271 */
272
273int
274synchnet(f)
275 int f; /* socket to flush */
276{
277 int i, j = 0;
278 char rbuf[PKTSIZE];
279 struct sockaddr_in from;
280 int fromlen;
281
282 while (1) {
283 (void) ioctl(f, FIONREAD, &i);
284 if (i) {
285 j++;
286 fromlen = sizeof from;
287 (void) recvfrom(f, rbuf, sizeof (rbuf), 0,
288 (struct sockaddr *)&from, &fromlen);
289 } else {
290 return(j);
291 }
292 }
293}