2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/types.h>
32 #include <sys/ioctl.h>
33 #include <sys/socket.h>
36 #include <netinet/in.h>
37 #include <arpa/tftp.h>
47 #include "tftp-file.h"
48 #include "tftp-utils.h"
53 static char convbuffer[66000];
57 convert_from_net(char *buffer, size_t count)
62 * Convert all CR/LF to LF and all CR,NUL to CR
66 for (i = 0; i < count; i++) {
69 convbuffer[n++] = buffer[i];
70 gotcr = (buffer[i] == '\r');
75 if (buffer[i] == '\0') {
81 if (buffer[i] == '\n') {
83 if (ftell(file) != 0) {
84 int r = fseek(file, -1, SEEK_END);
86 convbuffer[n++] = '\n';
88 /* This shouldn't happen */
90 "Received LF as first character");
94 convbuffer[n-1] = '\n';
99 /* Everything else just accept as is */
100 convbuffer[n++] = buffer[i];
101 gotcr = (buffer[i] == '\r');
105 return fwrite(convbuffer, 1, n, file);
109 convert_to_net(char *buffer, size_t count, int init)
112 static size_t n = 0, in = 0;
113 static int newline = -1;
123 * Convert all LF to CR,LF and all CR to CR,NUL
128 buffer[i++] = newline;
134 /* When done we're done */
135 if (feof(file)) break;
137 /* Otherwise read another bunch */
138 in = fread(convbuffer, 1, count, file);
144 if (convbuffer[n] == '\r') {
152 if (convbuffer[n] == '\n') {
159 buffer[i++] = convbuffer[n++];
164 * Whoops... that isn't allowed (but it will happen
165 * when there is a CR or LF at the end of the buffer)
167 newline = buffer[i-1];
179 write_init(int fd, FILE *f, const char *mode)
183 file = fdopen(fd, "w");
186 tftp_log(LOG_ERR, "fdopen() failed: %s",
192 convert = !strcmp(mode, "netascii");
197 write_file(char *buffer, int count)
201 return fwrite(buffer, 1, count, file);
203 return convert_from_net(buffer, count);
210 if (fclose(file) != 0) {
211 tftp_log(LOG_ERR, "fclose() failed: %s", strerror(errno));
225 seek_file(off_t offset)
228 return fseeko(file, offset, SEEK_SET);
232 read_init(int fd, FILE *f, const char *mode)
235 convert_to_net(NULL, 0, 1);
237 file = fdopen(fd, "r");
240 tftp_log(LOG_ERR, "fdopen() failed: %s",
246 convert = !strcmp(mode, "netascii");
251 read_file(char *buffer, int count)
255 return fread(buffer, 1, count, file);
257 return convert_to_net(buffer, count, 0);
264 if (fclose(file) != 0) {
265 tftp_log(LOG_ERR, "fclose() failed: %s", strerror(errno));
272 /* When an error has occurred, it is possible that the two sides
273 * are out of synch. Ie: that what I think is the other side's
274 * response to packet N is really their response to packet N-1.
276 * So, to try to prevent that, we flush all the input queued up
277 * for us on the network connection on our host.
279 * We return the number of packets we flushed (mostly for reporting
280 * when trace is active).
284 synchnet(int peer) /* socket to flush */
287 char rbuf[MAXPKTSIZE];
288 struct sockaddr_storage from;
292 (void) ioctl(peer, FIONREAD, &i);
295 fromlen = sizeof from;
296 (void) recvfrom(peer, rbuf, sizeof (rbuf), 0,
297 (struct sockaddr *)&from, &fromlen);