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/socket.h>
34 #include <netinet/in.h>
35 #include <arpa/tftp.h>
43 #include "tftp-utils.h"
47 * Default values, can be changed later via the TFTP Options
49 int timeoutpacket = TIMEOUT;
50 int timeoutnetwork = MAX_TIMEOUTS * TIMEOUT;
51 int maxtimeouts = MAX_TIMEOUTS;
52 uint16_t segsize = SEGSIZE;
53 uint16_t pktsize = SEGSIZE + 4;
54 uint16_t windowsize = WINDOWSIZE;
60 * Set timeout values for packet reception. The idea is that you
61 * get 'maxtimeouts' of 5 seconds between 'timeoutpacket' (i.e. the
62 * first timeout) to 'timeoutnetwork' (i.e. the last timeout)
65 settimeouts(int _timeoutpacket, int _timeoutnetwork, int _maxtimeouts __unused)
69 /* We cannot do impossible things */
70 if (_timeoutpacket >= _timeoutnetwork)
75 while (i < _timeoutnetwork || maxtimeouts < MIN_TIMEOUTS) {
80 timeoutpacket = _timeoutpacket;
85 /* translate IPv4 mapped IPv6 address to IPv4 address */
87 unmappedaddr(struct sockaddr_in6 *sin6)
89 struct sockaddr_in *sin4;
93 if (sin6->sin6_family != AF_INET6 ||
94 !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
96 sin4 = (struct sockaddr_in *)sin6;
97 memcpy(&addr, &sin6->sin6_addr.s6_addr[12], sizeof(addr));
98 port = sin6->sin6_port;
99 memset(sin4, 0, sizeof(struct sockaddr_in));
100 sin4->sin_addr.s_addr = addr;
101 sin4->sin_port = port;
102 sin4->sin_family = AF_INET;
103 sin4->sin_len = sizeof(struct sockaddr_in);
106 /* Get a field from a \0 separated string */
108 get_field(int peer, char *buffer, ssize_t size)
112 while (cp < buffer + size) {
113 if (*cp == '\0') break;
117 tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
118 send_error(peer, EBADOP);
121 return (cp - buffer + 1);
127 static int _tftp_logtostdout = 1;
130 tftp_openlog(const char *ident, int logopt, int facility)
133 _tftp_logtostdout = (ident == NULL);
134 if (_tftp_logtostdout == 0)
135 openlog(ident, logopt, facility);
142 if (_tftp_logtostdout == 0)
147 tftp_log(int priority, const char *message, ...)
152 va_start(ap, message);
153 if (_tftp_logtostdout == 0) {
154 vasprintf(&s, message, ap);
155 syslog(priority, "%s", s);
157 vprintf(message, ap);
166 struct packettypes packettypes[] = {
179 static char failed[100];
182 while (packettypes[i].name != NULL) {
183 if (packettypes[i].value == type)
187 if (packettypes[i].name != NULL)
188 return packettypes[i].name;
189 sprintf(failed, "unknown (type: %d)", type);
196 int debug = DEBUG_NONE;
197 struct debugs debugs[] = {
198 { DEBUG_PACKETS, "packet", "Packet debugging" },
199 { DEBUG_SIMPLE, "simple", "Simple debugging" },
200 { DEBUG_OPTIONS, "options", "Options debugging" },
201 { DEBUG_ACCESS, "access", "TCPd access debugging" },
202 { DEBUG_NONE, NULL, "No debugging" },
204 int packetdroppercentage = 0;
211 while (debugs[i].name != NULL) {
212 if (strcasecmp(debugs[i].name, s) == 0)
216 return (debugs[i].value);
241 size_t space = sizeof(s);
245 while (debugs[i].name != NULL) {
246 if (d&debugs[i].value) {
248 strlcat(s, " ", space);
249 strlcat(s, debugs[i].name, space);
261 struct rp_errors rp_errors[] = {
262 { RP_TIMEOUT, "Network timeout" },
263 { RP_TOOSMALL, "Not enough data bytes" },
264 { RP_WRONGSOURCE, "Invalid IP address of UDP port" },
265 { RP_ERROR, "Error packet" },
266 { RP_RECVFROM, "recvfrom() complained" },
267 { RP_TOOBIG, "Too many data bytes" },
272 rp_strerror(int error)
275 size_t space = sizeof(s);
278 while (rp_errors[i].desc != NULL) {
279 if (rp_errors[i].error == error) {
280 strlcpy(s, rp_errors[i].desc, space);
281 space -= strlen(rp_errors[i].desc);
286 sprintf(s, "unknown (error=%d)", error);
291 * Performance figures
295 stats_init(struct tftp_stats *ts)
303 gettimeofday(&(ts->tstart), NULL);
307 printstats(const char *direction, int verbose, struct tftp_stats *ts)
309 double delta; /* compute delta in 1/10's second units */
311 delta = ((ts->tstop.tv_sec*10.)+(ts->tstop.tv_usec/100000)) -
312 ((ts->tstart.tv_sec*10.)+(ts->tstart.tv_usec/100000));
313 delta = delta/10.; /* back to seconds */
315 printf("%s %zu bytes during %.1f seconds in %u blocks",
316 direction, ts->amount, delta, ts->blocks);
318 if (ts->rollovers != 0)
319 printf(" with %d rollover%s",
320 ts->rollovers, ts->rollovers != 1 ? "s" : "");
323 printf(" [%.0f bits/sec]", (ts->amount*8.)/delta);