2 * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/ioctl.h>
31 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/tftp.h>
44 #include "tftp-file.h"
45 #include "tftp-utils.h"
50 static char convbuffer[66000];
54 convert_from_net(char *buffer, size_t count)
59 * Convert all CR/LF to LF and all CR,NUL to CR
63 for (i = 0; i < count; i++) {
66 convbuffer[n++] = buffer[i];
67 gotcr = (buffer[i] == '\r');
72 if (buffer[i] == '\0') {
78 if (buffer[i] == '\n') {
80 if (ftell(file) != 0) {
81 fseek(file, -1, SEEK_END);
82 convbuffer[n++] = '\n';
84 /* This shouldn't happen */
86 "Received LF as first character");
90 convbuffer[n-1] = '\n';
95 /* Everything else just accept as is */
96 convbuffer[n++] = buffer[i];
97 gotcr = (buffer[i] == '\r');
101 return fwrite(convbuffer, 1, n, file);
105 convert_to_net(char *buffer, size_t count, int init)
108 static size_t n = 0, in = 0;
109 static int newline = 0;
119 * Convert all LF to CR,LF and all CR to CR,NUL
124 buffer[i++] = newline;
130 /* When done we're done */
131 if (feof(file)) break;
133 /* Otherwise read another bunch */
134 in = fread(convbuffer, 1, count, file);
140 if (convbuffer[n] == '\r') {
148 if (convbuffer[n] == '\n') {
155 buffer[i++] = convbuffer[n++];
160 * Whoops... that isn't alllowed (but it will happen
161 * when there is a CR or LF at the end of the buffer)
163 newline = buffer[i-1];
175 write_init(int fd, FILE *f, const char *mode)
179 file = fdopen(fd, "w");
182 tftp_log(LOG_ERR, "fdopen() failed: %s",
188 convert = !strcmp(mode, "netascii");
193 write_file(char *buffer, int count)
197 return fwrite(buffer, 1, count, file);
199 return convert_from_net(buffer, count);
206 if (fclose(file) != 0) {
207 tftp_log(LOG_ERR, "fclose() failed: %s", strerror(errno));
214 read_init(int fd, FILE *f, const char *mode)
217 convert_to_net(NULL, 0, 1);
219 file = fdopen(fd, "r");
222 tftp_log(LOG_ERR, "fdopen() failed: %s",
228 convert = !strcmp(mode, "netascii");
233 read_file(char *buffer, int count)
237 return fread(buffer, 1, count, file);
239 return convert_to_net(buffer, count, 0);
246 if (fclose(file) != 0) {
247 tftp_log(LOG_ERR, "fclose() failed: %s", strerror(errno));
254 /* When an error has occurred, it is possible that the two sides
255 * are out of synch. Ie: that what I think is the other side's
256 * response to packet N is really their response to packet N-1.
258 * So, to try to prevent that, we flush all the input queued up
259 * for us on the network connection on our host.
261 * We return the number of packets we flushed (mostly for reporting
262 * when trace is active).
266 synchnet(int peer) /* socket to flush */
269 char rbuf[MAXPKTSIZE];
270 struct sockaddr_storage from;
274 (void) ioctl(peer, FIONREAD, &i);
277 fromlen = sizeof from;
278 (void) recvfrom(peer, rbuf, sizeof (rbuf), 0,
279 (struct sockaddr *)&from, &fromlen);