summaryrefslogtreecommitdiffstats
path: root/libexec/tftpd/tftp-utils.c
blob: c55f120a3a4dc98374622907f4809edd76abfeec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 
 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/socket.h>
#include <sys/stat.h>

#include <netinet/in.h>
#include <arpa/tftp.h>

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>

#include "tftp-utils.h"
#include "tftp-io.h"

/*
 * Default values, can be changed later via the TFTP Options
 */
int		timeoutpacket = TIMEOUT;
int		timeoutnetwork = MAX_TIMEOUTS * TIMEOUT;
int		maxtimeouts = MAX_TIMEOUTS;
uint16_t	segsize = SEGSIZE;
uint16_t	pktsize = SEGSIZE + 4;

int	acting_as_client;


/*
 * Set timeout values for packet reception. The idea is that you
 * get 'maxtimeouts' of 5 seconds between 'timeoutpacket' (i.e. the
 * first timeout) to 'timeoutnetwork' (i.e. the last timeout)
 */
int
settimeouts(int _timeoutpacket, int _timeoutnetwork, int _maxtimeouts __unused)
{
	int i;
	
	/* We cannot do impossible things */
	if (_timeoutpacket >= _timeoutnetwork)
		return (0);
	
	maxtimeouts = 0;
	i = _timeoutpacket;
	while (i < _timeoutnetwork || maxtimeouts < MIN_TIMEOUTS) {
		maxtimeouts++;
		i += 5;
	}

	timeoutpacket = _timeoutpacket;
	timeoutnetwork = i;
	return (1);
}

/* translate IPv4 mapped IPv6 address to IPv4 address */
void
unmappedaddr(struct sockaddr_in6 *sin6)
{
	struct sockaddr_in *sin4;
	u_int32_t addr;
	int port;

	if (sin6->sin6_family != AF_INET6 ||
	    !IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr))
		return;
	sin4 = (struct sockaddr_in *)sin6;
	memcpy(&addr, &sin6->sin6_addr.s6_addr[12], sizeof(addr));
	port = sin6->sin6_port;
	memset(sin4, 0, sizeof(struct sockaddr_in));
	sin4->sin_addr.s_addr = addr;
	sin4->sin_port = port;
	sin4->sin_family = AF_INET;
	sin4->sin_len = sizeof(struct sockaddr_in);
}

/* Get a field from a \0 separated string */
ssize_t
get_field(int peer, char *buffer, ssize_t size)
{
	char *cp = buffer;

	while (cp < buffer + size) {
		if (*cp == '\0') break;
		cp++;
	}
	if (*cp != '\0') {
		tftp_log(LOG_ERR, "Bad option - no trailing \\0 found");
		send_error(peer, EBADOP);
		exit(1);
	}
	return (cp - buffer + 1);
}

/*
 * Logging functions
 */
static int _tftp_logtostdout = 1;

void
tftp_openlog(const char *ident, int logopt, int facility)
{

	_tftp_logtostdout = (ident == NULL);
	if (_tftp_logtostdout == 0)
		openlog(ident, logopt, facility);
}

void
tftp_closelog(void)
{

	if (_tftp_logtostdout == 0)
		closelog();
}

void
tftp_log(int priority, const char *message, ...)
{
	va_list ap;
	char *s;

	va_start(ap, message);
	if (_tftp_logtostdout == 0) {
		vasprintf(&s, message, ap);
		syslog(priority, "%s", s);
	} else {
		vprintf(message, ap);
		printf("\n");
	}
	va_end(ap);
}

/*
 * Packet types
 */
struct packettypes packettypes[] = {
	{ RRQ,		"RRQ"	},
	{ WRQ,		"WRQ"	},
	{ DATA,		"DATA"	},
	{ ACK,		"ACK"	},
	{ ERROR,	"ERROR"	},
	{ OACK,		"OACK"	},
	{ 0,		NULL	},
};

const char *
packettype(int type)
{
	static char failed[100];
	int i = 0;

	while (packettypes[i].name != NULL) {
		if (packettypes[i].value == type)
			break;
		i++;
	}
	if (packettypes[i].name != NULL)
		return packettypes[i].name;
	sprintf(failed, "unknown (type: %d)", type);
	return (failed);
}

/*
 * Debugs
 */
int	debug = DEBUG_NONE;
struct debugs debugs[] = {
	{ DEBUG_PACKETS,	"packet",	"Packet debugging"	},
	{ DEBUG_SIMPLE,		"simple",	"Simple debugging"	},
	{ DEBUG_OPTIONS,	"options",	"Options debugging"	},
	{ DEBUG_ACCESS,		"access",	"TCPd access debugging"	},
	{ DEBUG_NONE,		NULL,		"No debugging"		},
};
int	packetdroppercentage = 0;

int
debug_find(char *s)
{
	int i = 0;

	while (debugs[i].name != NULL) {
		if (strcasecmp(debugs[i].name, s) == 0)
			break;
		i++;
	}
	return (debugs[i].value);
}

int
debug_finds(char *s)
{
	int i = 0;
	char *ps = s;

	while (s != NULL) {
		ps = strchr(s, ' ');
		if (ps != NULL)
			*ps = '\0';
		i += debug_find(s);
		if (ps != NULL)
			*ps = ' ';
		s = ps;
	}
	return (i);
}

const char *
debug_show(int d)
{
	static char s[100];
	int i = 0;

	s[0] = '\0';
	while (debugs[i].name != NULL) {
		if (d&debugs[i].value) {
			if (s[0] != '\0') 
				strcat(s, " ");
			strcat(s, debugs[i].name);
		}
		i++;
	}
	if (s[0] != '\0')
		return (s);
	return ("none");
}

/*
 * RP_
 */
struct rp_errors rp_errors[] = {
	{ RP_TIMEOUT,		"Network timeout" },
	{ RP_TOOSMALL,		"Not enough data bytes" },
	{ RP_WRONGSOURCE,	"Invalid IP address of UDP port" },
	{ RP_ERROR,		"Error packet" },
	{ RP_RECVFROM,		"recvfrom() complained" },
	{ RP_TOOBIG,		"Too many data bytes" },
	{ RP_NONE,		NULL }
};

char *
rp_strerror(int error)
{
	static char s[100];
	int i = 0;

	while (rp_errors[i].desc != NULL) {
		if (rp_errors[i].error == error) {
			strcpy(s, rp_errors[i].desc);
		}
		i++;
	}
	if (s[0] == '\0')
		sprintf(s, "unknown (error=%d)", error);
	return (s);
}

/*
 * Performance figures
 */

void
stats_init(struct tftp_stats *ts)
{

	ts->amount = 0;
	ts->rollovers = 0;
	ts->retries = 0;
	ts->blocks = 0;
	ts->amount = 0;
	gettimeofday(&(ts->tstart), NULL);
}

void
printstats(const char *direction, int verbose, struct tftp_stats *ts)
{
	double delta;	/* compute delta in 1/10's second units */

	delta = ((ts->tstop.tv_sec*10.)+(ts->tstop.tv_usec/100000)) -
		((ts->tstart.tv_sec*10.)+(ts->tstart.tv_usec/100000));
	delta = delta/10.;      /* back to seconds */

	printf("%s %zu bytes during %.1f seconds in %u blocks",
	    direction, ts->amount, delta, ts->blocks);

	if (ts->rollovers != 0)
		printf(" with %d rollover%s",
		    ts->rollovers, ts->rollovers != 1 ? "s" : "");

	if (verbose)
		printf(" [%.0f bits/sec]", (ts->amount*8.)/delta);
	putchar('\n');
}
OpenPOWER on IntegriCloud