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/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/tftp.h>
41 #include "tftp-utils.h"
45 * Default values, can be changed later via the TFTP Options
47 int timeoutpacket = TIMEOUT;
48 int timeoutnetwork = MAX_TIMEOUTS * TIMEOUT;
49 int maxtimeouts = MAX_TIMEOUTS;
50 uint16_t segsize = SEGSIZE;
51 uint16_t pktsize = SEGSIZE + 4;
57 * Set timeout values for packet reception. The idea is that you
58 * get 'maxtimeouts' of 5 seconds between 'timeoutpacket' (i.e. the
59 * first timeout) to 'timeoutnetwork' (i.e. the last timeout)
62 settimeouts(int _timeoutpacket, int _timeoutnetwork, int _maxtimeouts __unused)
66 /* We cannot do impossible things */
67 if (_timeoutpacket >= _timeoutnetwork)
72 while (i < _timeoutnetwork || maxtimeouts < MIN_TIMEOUTS) {
77 timeoutpacket = _timeoutpacket;
82 /* translate IPv4 mapped IPv6 address to IPv4 address */
84 unmappedaddr(struct sockaddr_in6 *sin6)
86 struct sockaddr_in *sin4;
90 if (sin6->sin6_family != AF_INET6 ||
91 !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
93 sin4 = (struct sockaddr_in *)sin6;
94 memcpy(&addr, &sin6->sin6_addr.s6_addr[12], sizeof(addr));
95 port = sin6->sin6_port;
96 memset(sin4, 0, sizeof(struct sockaddr_in));
97 sin4->sin_addr.s_addr = addr;
98 sin4->sin_port = port;
99 sin4->sin_family = AF_INET;
100 sin4->sin_len = sizeof(struct sockaddr_in);
103 /* Get a field from a \0 separated string */
105 get_field(int peer, char *buffer, ssize_t size)
109 while (cp < buffer + size) {
110 if (*cp == '\0') break;
114 tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
115 send_error(peer, EBADOP);
118 return (cp - buffer + 1);
124 static int _tftp_logtostdout = 1;
127 tftp_openlog(const char *ident, int logopt, int facility)
130 _tftp_logtostdout = (ident == NULL);
131 if (_tftp_logtostdout == 0)
132 openlog(ident, logopt, facility);
139 if (_tftp_logtostdout == 0)
144 tftp_log(int priority, const char *message, ...)
149 va_start(ap, message);
150 if (_tftp_logtostdout == 0) {
151 vasprintf(&s, message, ap);
152 syslog(priority, "%s", s);
154 vprintf(message, ap);
163 struct packettypes packettypes[] = {
176 static char failed[100];
179 while (packettypes[i].name != NULL) {
180 if (packettypes[i].value == type)
184 if (packettypes[i].name != NULL)
185 return packettypes[i].name;
186 sprintf(failed, "unknown (type: %d)", type);
193 int debug = DEBUG_NONE;
194 struct debugs debugs[] = {
195 { DEBUG_PACKETS, "packet", "Packet debugging" },
196 { DEBUG_SIMPLE, "simple", "Simple debugging" },
197 { DEBUG_OPTIONS, "options", "Options debugging" },
198 { DEBUG_ACCESS, "access", "TCPd access debugging" },
199 { DEBUG_NONE, NULL, "No debugging" },
201 int packetdroppercentage = 0;
208 while (debugs[i].name != NULL) {
209 if (strcasecmp(debugs[i].name, s) == 0)
213 return (debugs[i].value);
241 while (debugs[i].name != NULL) {
242 if (d&debugs[i].value) {
245 strcat(s, debugs[i].name);
257 struct rp_errors rp_errors[] = {
258 { RP_TIMEOUT, "Network timeout" },
259 { RP_TOOSMALL, "Not enough data bytes" },
260 { RP_WRONGSOURCE, "Invalid IP address of UDP port" },
261 { RP_ERROR, "Error packet" },
262 { RP_RECVFROM, "recvfrom() complained" },
263 { RP_TOOBIG, "Too many data bytes" },
268 rp_strerror(int error)
273 while (rp_errors[i].desc != NULL) {
274 if (rp_errors[i].error == error) {
275 strcpy(s, rp_errors[i].desc);
280 sprintf(s, "unknown (error=%d)", error);
285 * Performance figures
289 stats_init(struct tftp_stats *ts)
297 gettimeofday(&(ts->tstart), NULL);
301 printstats(const char *direction, int verbose, struct tftp_stats *ts)
303 double delta; /* compute delta in 1/10's second units */
305 delta = ((ts->tstop.tv_sec*10.)+(ts->tstop.tv_usec/100000)) -
306 ((ts->tstart.tv_sec*10.)+(ts->tstart.tv_usec/100000));
307 delta = delta/10.; /* back to seconds */
309 printf("%s %zu bytes during %.1f seconds in %u blocks",
310 direction, ts->amount, delta, ts->blocks);
312 if (ts->rollovers != 0)
313 printf(" with %d rollover%s",
314 ts->rollovers, ts->rollovers != 1 ? "s" : "");
317 printf(" [%.0f bits/sec]", (ts->amount*8.)/delta);