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