xref: /openbsd-src/usr.sbin/unbound/util/net_help.h (revision 4c1e55dc91edd6e69ccc60ce855900fbc12cf34f)
1 /*
2  * util/net_help.h - network help functions
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * Neither the name of the NLNET LABS nor the names of its contributors may
20  * be used to endorse or promote products derived from this software without
21  * specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions to perform network related tasks.
40  */
41 
42 #ifndef NET_HELP_H
43 #define NET_HELP_H
44 #include "util/log.h"
45 struct sock_list;
46 struct regional;
47 
48 /** DNS constants for uint16_t style flag manipulation. host byteorder.
49  *                                1  1  1  1  1  1
50  *  0  1  2  3  4  5  6  7  8  9  0  1  2  3  4  5
51  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
52  * |QR|   Opcode  |AA|TC|RD|RA| Z|AD|CD|   RCODE   |
53  * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
54  */
55 /** CD flag */
56 #define BIT_CD 0x0010
57 /** AD flag */
58 #define BIT_AD 0x0020
59 /** Z flag */
60 #define BIT_Z  0x0040
61 /** RA flag */
62 #define BIT_RA 0x0080
63 /** RD flag */
64 #define BIT_RD 0x0100
65 /** TC flag */
66 #define BIT_TC 0x0200
67 /** AA flag */
68 #define BIT_AA 0x0400
69 /** QR flag */
70 #define BIT_QR 0x8000
71 /** get RCODE bits from uint16 flags */
72 #define FLAGS_GET_RCODE(f) ((f) & 0xf)
73 /** set RCODE bits in uint16 flags */
74 #define FLAGS_SET_RCODE(f, r) (f = (((f) & 0xfff0) | (r)))
75 
76 /** timeout in seconds for UDP queries to auth servers. */
77 #define UDP_AUTH_QUERY_TIMEOUT 4
78 /** timeout in seconds for TCP queries to auth servers. */
79 #define TCP_AUTH_QUERY_TIMEOUT 30
80 /** Advertised version of EDNS capabilities */
81 #define EDNS_ADVERTISED_VERSION         0
82 /** Advertised size of EDNS capabilities */
83 extern uint16_t EDNS_ADVERTISED_SIZE;
84 /** bits for EDNS bitfield */
85 #define EDNS_DO 0x8000 /* Dnssec Ok */
86 /** byte size of ip4 address */
87 #define INET_SIZE 4
88 /** byte size of ip6 address */
89 #define INET6_SIZE 16
90 
91 /** DNSKEY zone sign key flag */
92 #define DNSKEY_BIT_ZSK 0x0100
93 /** DNSKEY secure entry point, KSK flag */
94 #define DNSKEY_BIT_SEP 0x0001
95 
96 /**
97  * See if string is ip4 or ip6.
98  * @param str: IP specification.
99  * @return: true if string addr is an ip6 specced address.
100  */
101 int str_is_ip6(const char* str);
102 
103 /**
104  * Set fd nonblocking.
105  * @param s: file descriptor.
106  * @return: 0 on error (error is printed to log).
107  */
108 int fd_set_nonblock(int s);
109 
110 /**
111  * Set fd (back to) blocking.
112  * @param s: file descriptor.
113  * @return: 0 on error (error is printed to log).
114  */
115 int fd_set_block(int s);
116 
117 /**
118  * See if number is a power of 2.
119  * @param num: the value.
120  * @return: true if the number is a power of 2.
121  */
122 int is_pow2(size_t num);
123 
124 /**
125  * Allocate memory and copy over contents.
126  * @param data: what to copy over.
127  * @param len: length of data.
128  * @return: NULL on malloc failure, or newly malloced data.
129  */
130 void* memdup(void* data, size_t len);
131 
132 /**
133  * Prints the sockaddr in readable format with log_info. Debug helper.
134  * @param v: at what verbosity level to print this.
135  * @param str: descriptive string printed with it.
136  * @param addr: the sockaddr to print. Can be ip4 or ip6.
137  * @param addrlen: length of addr.
138  */
139 void log_addr(enum verbosity_value v, const char* str,
140 	struct sockaddr_storage* addr, socklen_t addrlen);
141 
142 /**
143  * Prints zone name and sockaddr in readable format with log_info. Debug.
144  * @param v: at what verbosity level to print this.
145  * @param str: descriptive string printed with it.
146  * @param zone: DNS domain name, uncompressed wireformat.
147  * @param addr: the sockaddr to print. Can be ip4 or ip6.
148  * @param addrlen: length of addr.
149  */
150 void log_name_addr(enum verbosity_value v, const char* str, uint8_t* zone,
151 	struct sockaddr_storage* addr, socklen_t addrlen);
152 
153 /**
154  * Convert address string, with "@port" appendix, to sockaddr.
155  * Uses DNS port by default.
156  * @param str: the string
157  * @param addr: where to store sockaddr.
158  * @param addrlen: length of stored sockaddr is returned.
159  * @return 0 on error.
160  */
161 int extstrtoaddr(const char* str, struct sockaddr_storage* addr,
162 	socklen_t* addrlen);
163 
164 /**
165  * Convert ip address string and port to sockaddr.
166  * @param ip: ip4 or ip6 address string.
167  * @param port: port number, host format.
168  * @param addr: where to store sockaddr.
169  * @param addrlen: length of stored sockaddr is returned.
170  * @return 0 on error.
171  */
172 int ipstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr,
173 	socklen_t* addrlen);
174 
175 /**
176  * Convert ip netblock (ip/netsize) string and port to sockaddr.
177  * *SLOW*, does a malloc internally to avoid writing over 'ip' string.
178  * @param ip: ip4 or ip6 address string.
179  * @param port: port number, host format.
180  * @param addr: where to store sockaddr.
181  * @param addrlen: length of stored sockaddr is returned.
182  * @param net: netblock size is returned.
183  * @return 0 on error.
184  */
185 int netblockstrtoaddr(const char* ip, int port, struct sockaddr_storage* addr,
186 	socklen_t* addrlen, int* net);
187 
188 /**
189  * Print string with neat domain name, type and class.
190  * @param v: at what verbosity level to print this.
191  * @param str: string of message.
192  * @param name: domain name uncompressed wireformat.
193  * @param type: host format RR type.
194  * @param dclass: host format RR class.
195  */
196 void log_nametypeclass(enum verbosity_value v, const char* str,
197 	uint8_t* name, uint16_t type, uint16_t dclass);
198 
199 /**
200  * Compare two sockaddrs. Imposes an ordering on the addresses.
201  * Compares address and port.
202  * @param addr1: address 1.
203  * @param len1: lengths of addr1.
204  * @param addr2: address 2.
205  * @param len2: lengths of addr2.
206  * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
207  */
208 int sockaddr_cmp(struct sockaddr_storage* addr1, socklen_t len1,
209 	struct sockaddr_storage* addr2, socklen_t len2);
210 
211 /**
212  * Compare two sockaddrs. Compares address, not the port.
213  * @param addr1: address 1.
214  * @param len1: lengths of addr1.
215  * @param addr2: address 2.
216  * @param len2: lengths of addr2.
217  * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
218  */
219 int sockaddr_cmp_addr(struct sockaddr_storage* addr1, socklen_t len1,
220 	struct sockaddr_storage* addr2, socklen_t len2);
221 
222 /**
223  * Checkout address family.
224  * @param addr: the sockaddr to examine.
225  * @param len: the length of addr.
226  * @return: true if sockaddr is ip6.
227  */
228 int addr_is_ip6(struct sockaddr_storage* addr, socklen_t len);
229 
230 /**
231  * Make sure the sockaddr ends in zeroes. For tree insertion and subsequent
232  * comparison.
233  * @param addr: the ip4 or ip6 addr.
234  * @param len: length of addr.
235  * @param net: number of bits to leave untouched, the rest of the netblock
236  * 	address is zeroed.
237  */
238 void addr_mask(struct sockaddr_storage* addr, socklen_t len, int net);
239 
240 /**
241  * See how many bits are shared, equal, between two addrs.
242  * @param addr1: first addr.
243  * @param net1: netblock size of first addr.
244  * @param addr2: second addr.
245  * @param net2: netblock size of second addr.
246  * @param addrlen: length of first addr and of second addr.
247  * 	They must be of the same length (i.e. same type IP4, IP6).
248  * @return: number of bits the same.
249  */
250 int addr_in_common(struct sockaddr_storage* addr1, int net1,
251 	struct sockaddr_storage* addr2, int net2, socklen_t addrlen);
252 
253 /**
254  * Put address into string, works for IPv4 and IPv6.
255  * @param addr: address
256  * @param addrlen: length of address
257  * @param buf: result string stored here
258  * @param len: length of buf.
259  * On failure a string with "error" is stored inside.
260  */
261 void addr_to_str(struct sockaddr_storage* addr, socklen_t addrlen,
262 	char* buf, size_t len);
263 
264 /**
265  * See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0"
266  * @param addr: address
267  * @param addrlen: length of address
268  * @return true if so
269  */
270 int addr_is_ip4mapped(struct sockaddr_storage* addr, socklen_t addrlen);
271 
272 /**
273  * See if sockaddr is 255.255.255.255.
274  * @param addr: address
275  * @param addrlen: length of address
276  * @return true if so
277  */
278 int addr_is_broadcast(struct sockaddr_storage* addr, socklen_t addrlen);
279 
280 /**
281  * See if sockaddr is 0.0.0.0 or ::0.
282  * @param addr: address
283  * @param addrlen: length of address
284  * @return true if so
285  */
286 int addr_is_any(struct sockaddr_storage* addr, socklen_t addrlen);
287 
288 /**
289  * Insert new socket list item. If fails logs error.
290  * @param list: pointer to pointer to first item.
291  * @param addr: address or NULL if 'cache'.
292  * @param len: length of addr, or 0 if 'cache'.
293  * @param region: where to allocate
294  */
295 void sock_list_insert(struct sock_list** list, struct sockaddr_storage* addr,
296 	socklen_t len, struct regional* region);
297 
298 /**
299  * Append one list to another.  Must both be from same qstate(regional).
300  * @param list: pointer to result list that is modified.
301  * @param add: item(s) to add.  They are prepended to list.
302  */
303 void sock_list_prepend(struct sock_list** list, struct sock_list* add);
304 
305 /**
306  * Find addr in list.
307  * @param list: to search in
308  * @param addr: address to look for.
309  * @param len: length. Can be 0, look for 'cache entry'.
310  * @return true if found.
311  */
312 int sock_list_find(struct sock_list* list, struct sockaddr_storage* addr,
313         socklen_t len);
314 
315 /**
316  * Merge socklist into another socket list.  Allocates the new entries
317  * freshly and copies them over, so also performs a region switchover.
318  * Allocation failures are logged.
319  * @param list: the destination list (checked for duplicates)
320  * @param region: where to allocate
321  * @param add: the list of entries to add.
322  */
323 void sock_list_merge(struct sock_list** list, struct regional* region,
324 	struct sock_list* add);
325 
326 /**
327  * Log libcrypto error with descriptive string. Calls log_err().
328  * @param str: what failed.
329  */
330 void log_crypto_err(const char* str);
331 
332 /**
333  * create SSL listen context
334  * @param key: private key file.
335  * @param pem: public key cert.
336  * @param verifypem: if nonNULL, verifylocation file.
337  * return SSL_CTX* or NULL on failure (logged).
338  */
339 void* listen_sslctx_create(char* key, char* pem, char* verifypem);
340 
341 /**
342  * create SSL connect context
343  * @param key: if nonNULL (also pem nonNULL), the client private key.
344  * @param pem: client public key (or NULL if key is NULL).
345  * @param verifypem: if nonNULL used for verifylocation file.
346  * @return SSL_CTX* or NULL on failure (logged).
347  */
348 void* connect_sslctx_create(char* key, char* pem, char* verifypem);
349 
350 /**
351  * accept a new fd and wrap it in a BIO in SSL
352  * @param sslctx: the SSL_CTX to use (from listen_sslctx_create()).
353  * @param fd: from accept, nonblocking.
354  * @return SSL or NULL on alloc failure.
355  */
356 void* incoming_ssl_fd(void* sslctx, int fd);
357 
358 /**
359  * connect a new fd and wrap it in a BIO in SSL
360  * @param sslctx: the SSL_CTX to use (from connect_sslctx_create())
361  * @param fd: from connect.
362  * @return SSL or NULL on alloc failure
363  */
364 void* outgoing_ssl_fd(void* sslctx, int fd);
365 
366 #endif /* NET_HELP_H */
367