xref: /freebsd/libexec/tftpd/tftp-utils.h (revision af23369a6deaaeb612ab266eb88b8bb8d560c322)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (C) 2008 Edwin Groothuis. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30 
31 /*
32  */
33 #define	TIMEOUT		5
34 #define	MAX_TIMEOUTS	5
35 
36 /* Generic values */
37 #define MAXSEGSIZE	65464		/* Maximum size of the data segment */
38 #define	MAXPKTSIZE	(MAXSEGSIZE + 4) /* Maximum size of the packet */
39 
40 /* For the blksize option */
41 #define BLKSIZE_MIN	8		/* Minimum size of the data segment */
42 #define BLKSIZE_MAX	MAXSEGSIZE	/* Maximum size of the data segment */
43 
44 /* For the timeout option */
45 #define TIMEOUT_MIN	0		/* Minimum timeout value */
46 #define TIMEOUT_MAX	255		/* Maximum timeout value */
47 #define MIN_TIMEOUTS	3
48 
49 /* For the windowsize option */
50 #define	WINDOWSIZE	1
51 #define	WINDOWSIZE_MIN	1
52 #define	WINDOWSIZE_MAX	65535
53 
54 extern int	timeoutpacket;
55 extern int	timeoutnetwork;
56 extern int	maxtimeouts;
57 int	settimeouts(int timeoutpacket, int timeoutnetwork, int maxtimeouts);
58 
59 extern uint16_t	segsize;
60 extern uint16_t	pktsize;
61 extern uint16_t	windowsize;
62 
63 extern int	acting_as_client;
64 
65 /*
66  */
67 void	unmappedaddr(struct sockaddr_in6 *sin6);
68 ssize_t	get_field(int peer, char *buffer, ssize_t size);
69 
70 /*
71  * Packet types
72  */
73 struct packettypes {
74 	int	value;
75 	const char *const name;
76 };
77 extern struct packettypes packettypes[];
78 const char *packettype(int);
79 
80 /*
81  * RP_
82  */
83 struct rp_errors {
84 	int	error;
85 	const char *const desc;
86 };
87 extern struct rp_errors rp_errors[];
88 char	*rp_strerror(int error);
89 
90 /*
91  * Debug features
92  */
93 #define	DEBUG_NONE	0x0000
94 #define DEBUG_PACKETS	0x0001
95 #define DEBUG_SIMPLE	0x0002
96 #define DEBUG_OPTIONS	0x0004
97 #define DEBUG_ACCESS	0x0008
98 struct debugs {
99 	int	value;
100 	const char *const name;
101 	const char *const desc;
102 };
103 extern int	debug;
104 extern struct debugs debugs[];
105 extern int	packetdroppercentage;
106 int	debug_find(char *s);
107 int	debug_finds(char *s);
108 const char *debug_show(int d);
109 
110 /*
111  * Log routines
112  */
113 #define DEBUG(s) tftp_log(LOG_DEBUG, "%s", s)
114 extern int tftp_logtostdout;
115 void	tftp_openlog(const char *ident, int logopt, int facility);
116 void	tftp_closelog(void);
117 void	tftp_log(int priority, const char *message, ...) __printflike(2, 3);
118 
119 /*
120  * Performance figures
121  */
122 struct tftp_stats {
123 	size_t		amount;
124 	int		rollovers;
125 	uint32_t	blocks;
126 	int		retries;
127 	struct timeval	tstart;
128 	struct timeval	tstop;
129 };
130 
131 void	stats_init(struct tftp_stats *ts);
132 void	printstats(const char *direction, int verbose, struct tftp_stats *ts);
133