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