2 * Copyright (c) 1983 Regents of the University of California.
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 static char sccsid
[] = "@(#)tftpsubs.c 5.4 (Berkeley) 6/29/88";
22 /* Simple minded read-ahead/write-behind subroutines for tftp user and
23 server. Written originally with multiple buffers in mind, but current
24 implementation has two buffer logic wired in.
26 Todo: add some sort of final error check so when the write-buffer
27 is finally flushed, the caller can detect if the disk filled up
28 (or had an i/o error) and return a nak to the other side.
34 #include <sys/types.h>
35 #include <sys/socket.h>
36 #include <sys/ioctl.h>
37 #include <netinet/in.h>
38 #include <arpa/tftp.h>
41 #define PKTSIZE (1432+4) /* SEGSIZE+4 */ /* should be moved to tftp.h */
44 int counter
; /* size of data in buffer, or flag */
45 char buf
[PKTSIZE
]; /* room for data packet */
48 /* Values for bf.counter */
49 #define BF_ALLOC -3 /* alloc'd but not yet filled */
50 #define BF_FREE -2 /* free */
51 /* [-1 .. SEGSIZE] = size of data in the data buffer */
55 static int nextone
; /* index of next buffer to use */
56 static int current
; /* index of buffer in use */
58 /* control flags for crlf conversions */
59 int newline
= 0; /* fillbuf: in middle of newline expansion */
60 int prevchar
= -1; /* putbuf: previous char (cr check) */
62 struct tftphdr
*rw_init();
64 struct tftphdr
*w_init() { return rw_init(0); } /* write-behind */
65 struct tftphdr
*r_init() { return rw_init(1); } /* read-ahead */
68 rw_init(x
) /* init for either read-ahead or write-behind */
69 int x
; /* zero for write-behind, one for read-head */
71 newline
= 0; /* init crlf flag */
73 bfs
[0].counter
= BF_ALLOC
; /* pass out the first buffer */
75 bfs
[1].counter
= BF_FREE
;
76 nextone
= x
; /* ahead or behind? */
77 return (struct tftphdr
*)bfs
[0].buf
;
81 /* Have emptied current buffer by sending to net and getting ack.
82 Free it and return next buffer filled with data.
84 readit(file
, dpp
, convert
)
85 FILE *file
; /* file opened for read */
87 int convert
; /* if true, convert to ascii */
91 bfs
[current
].counter
= BF_FREE
; /* free old one */
92 current
= !current
; /* "incr" current */
94 b
= &bfs
[current
]; /* look at new buffer */
95 if (b
->counter
== BF_FREE
) /* if it's empty */
96 read_ahead(file
, convert
); /* fill it */
97 /* assert(b->counter != BF_FREE); /* check */
98 *dpp
= (struct tftphdr
*)b
->buf
; /* set caller's ptr */
103 * fill the input buffer, doing ascii conversions if requested
104 * conversions are lf -> cr,lf and cr -> cr, nul
106 read_ahead(file
, convert
)
107 FILE *file
; /* file opened for read */
108 int convert
; /* if true, convert to ascii */
116 b
= &bfs
[nextone
]; /* look at "next" buffer */
117 if (b
->counter
!= BF_FREE
) /* nop if not free */
119 nextone
= !nextone
; /* "incr" next buffer ptr */
121 dp
= (struct tftphdr
*)b
->buf
;
127 i
= read(fileno(file
), dp
->th_data
+ b
->counter
,
128 segsize
- b
->counter
);
131 } while (i
!= 0 && !(i
< 0 && errno
!= EINTR
) &&
132 b
->counter
< segsize
);
137 for (i
= 0 ; i
< segsize
; i
++) {
139 if (prevchar
== '\n')
140 c
= '\n'; /* lf to cr,lf */
141 else c
= '\0'; /* cr to cr,nul */
147 if (c
== '\n' || c
== '\r') {
155 b
->counter
= (int)(p
- dp
->th_data
);
158 /* Update count associated with the buffer, get new buffer
159 from the queue. Calls write_behind only if next buffer not
162 writeit(file
, dpp
, ct
, convert
)
164 struct tftphdr
**dpp
;
167 bfs
[current
].counter
= ct
; /* set size of data to write */
168 current
= !current
; /* switch to other buffer */
169 if (bfs
[current
].counter
!= BF_FREE
) /* if not free */
170 write_behind(file
, convert
); /* flush it */
171 bfs
[current
].counter
= BF_ALLOC
; /* mark as alloc'd */
172 *dpp
= (struct tftphdr
*)bfs
[current
].buf
;
173 return ct
; /* this is a lie of course */
177 * Output a buffer to a file, converting from netascii if requested.
178 * CR,NUL -> CR and CR,LF => LF.
179 * Note spec is undefined if we get CR as last byte of file or a
180 * CR followed by anything else. In this case we leave it alone.
182 write_behind(file
, convert
)
190 register int c
; /* current character */
195 if (b
->counter
< -1) /* anything to flush? */
196 return 0; /* just nop if nothing to do */
198 count
= b
->counter
; /* remember byte count */
199 b
->counter
= BF_FREE
; /* reset flag */
200 dp
= (struct tftphdr
*)b
->buf
;
201 nextone
= !nextone
; /* incr for next time */
204 if (count
<= 0) return -1; /* nak logic? */
207 return write(fileno(file
), buf
, count
);
211 while (ct
--) { /* loop over the buffer */
212 c
= *p
++; /* pick up a character */
213 if (prevchar
== '\r') { /* if prev char was cr */
214 if (c
== '\n') /* if have cr,lf then just */
215 fseek(file
, -1, 1); /* smash lf on top of the cr */
217 if (c
== '\0') /* if have cr,nul then */
218 goto skipit
; /* just skip over the putc */
219 /* else just fall through and allow it */
229 /* When an error has occurred, it is possible that the two sides
230 * are out of synch. Ie: that what I think is the other side's
231 * response to packet N is really their response to packet N-1.
233 * So, to try to prevent that, we flush all the input queued up
234 * for us on the network connection on our host.
236 * We return the number of packets we flushed (mostly for reporting
237 * when trace is active).
242 int f
; /* socket to flush */
246 struct sockaddr_in from
;
250 (void) ioctl(f
, FIONREAD
, &i
);
253 fromlen
= sizeof from
;
254 (void) recvfrom(f
, rbuf
, sizeof (rbuf
), 0,
255 (struct sockaddr
*)&from
, &fromlen
);