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