]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/util/net_help.h
54f4c9c0e7cd019fdf6045f08ede2b9dfc7a4771
2 * util/net_help.h - network help functions
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
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.
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.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to perform network related tasks.
48 /** DNS constants for uint16_t style flag manipulation. host byteorder.
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 * +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
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)))
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 */
88 /** byte size of ip6 address */
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
96 /** minimal responses when positive answer */
97 extern int MINIMAL_RESPONSES
;
99 /** rrset order roundrobin */
100 extern int RRSET_ROUNDROBIN
;
103 * See if string is ip4 or ip6.
104 * @param str: IP specification.
105 * @return: true if string addr is an ip6 specced address.
107 int str_is_ip6(const char* str
);
110 * Set fd nonblocking.
111 * @param s: file descriptor.
112 * @return: 0 on error (error is printed to log).
114 int fd_set_nonblock(int s
);
117 * Set fd (back to) blocking.
118 * @param s: file descriptor.
119 * @return: 0 on error (error is printed to log).
121 int fd_set_block(int s
);
124 * See if number is a power of 2.
125 * @param num: the value.
126 * @return: true if the number is a power of 2.
128 int is_pow2(size_t num
);
131 * Allocate memory and copy over contents.
132 * @param data: what to copy over.
133 * @param len: length of data.
134 * @return: NULL on malloc failure, or newly malloced data.
136 void* memdup(void* data
, size_t len
);
139 * Prints the sockaddr in readable format with log_info. Debug helper.
140 * @param v: at what verbosity level to print this.
141 * @param str: descriptive string printed with it.
142 * @param addr: the sockaddr to print. Can be ip4 or ip6.
143 * @param addrlen: length of addr.
145 void log_addr(enum verbosity_value v
, const char* str
,
146 struct sockaddr_storage
* addr
, socklen_t addrlen
);
149 * Prints zone name and sockaddr in readable format with log_info. Debug.
150 * @param v: at what verbosity level to print this.
151 * @param str: descriptive string printed with it.
152 * @param zone: DNS domain name, uncompressed wireformat.
153 * @param addr: the sockaddr to print. Can be ip4 or ip6.
154 * @param addrlen: length of addr.
156 void log_name_addr(enum verbosity_value v
, const char* str
, uint8_t* zone
,
157 struct sockaddr_storage
* addr
, socklen_t addrlen
);
160 * Log errno and addr.
161 * @param str: descriptive string printed with it.
162 * @param err: errno string to print, i.e. strerror(errno).
163 * @param addr: the sockaddr to print. Can be ip4 or ip6.
164 * @param addrlen: length of addr.
166 void log_err_addr(const char* str
, const char* err
,
167 struct sockaddr_storage
* addr
, socklen_t addrlen
);
170 * Convert address string, with "@port" appendix, to sockaddr.
171 * Uses DNS port by default.
172 * @param str: the string
173 * @param addr: where to store sockaddr.
174 * @param addrlen: length of stored sockaddr is returned.
175 * @return 0 on error.
177 int extstrtoaddr(const char* str
, struct sockaddr_storage
* addr
,
181 * Convert ip address string and port to sockaddr.
182 * @param ip: ip4 or ip6 address string.
183 * @param port: port number, host format.
184 * @param addr: where to store sockaddr.
185 * @param addrlen: length of stored sockaddr is returned.
186 * @return 0 on error.
188 int ipstrtoaddr(const char* ip
, int port
, struct sockaddr_storage
* addr
,
192 * Convert ip netblock (ip/netsize) string and port to sockaddr.
193 * *SLOW*, does a malloc internally to avoid writing over 'ip' string.
194 * @param ip: ip4 or ip6 address string.
195 * @param port: port number, host format.
196 * @param addr: where to store sockaddr.
197 * @param addrlen: length of stored sockaddr is returned.
198 * @param net: netblock size is returned.
199 * @return 0 on error.
201 int netblockstrtoaddr(const char* ip
, int port
, struct sockaddr_storage
* addr
,
202 socklen_t
* addrlen
, int* net
);
205 * Print string with neat domain name, type and class.
206 * @param v: at what verbosity level to print this.
207 * @param str: string of message.
208 * @param name: domain name uncompressed wireformat.
209 * @param type: host format RR type.
210 * @param dclass: host format RR class.
212 void log_nametypeclass(enum verbosity_value v
, const char* str
,
213 uint8_t* name
, uint16_t type
, uint16_t dclass
);
216 * Compare two sockaddrs. Imposes an ordering on the addresses.
217 * Compares address and port.
218 * @param addr1: address 1.
219 * @param len1: lengths of addr1.
220 * @param addr2: address 2.
221 * @param len2: lengths of addr2.
222 * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
224 int sockaddr_cmp(struct sockaddr_storage
* addr1
, socklen_t len1
,
225 struct sockaddr_storage
* addr2
, socklen_t len2
);
228 * Compare two sockaddrs. Compares address, not the port.
229 * @param addr1: address 1.
230 * @param len1: lengths of addr1.
231 * @param addr2: address 2.
232 * @param len2: lengths of addr2.
233 * @return: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.
235 int sockaddr_cmp_addr(struct sockaddr_storage
* addr1
, socklen_t len1
,
236 struct sockaddr_storage
* addr2
, socklen_t len2
);
239 * Checkout address family.
240 * @param addr: the sockaddr to examine.
241 * @param len: the length of addr.
242 * @return: true if sockaddr is ip6.
244 int addr_is_ip6(struct sockaddr_storage
* addr
, socklen_t len
);
247 * Make sure the sockaddr ends in zeroes. For tree insertion and subsequent
249 * @param addr: the ip4 or ip6 addr.
250 * @param len: length of addr.
251 * @param net: number of bits to leave untouched, the rest of the netblock
254 void addr_mask(struct sockaddr_storage
* addr
, socklen_t len
, int net
);
257 * See how many bits are shared, equal, between two addrs.
258 * @param addr1: first addr.
259 * @param net1: netblock size of first addr.
260 * @param addr2: second addr.
261 * @param net2: netblock size of second addr.
262 * @param addrlen: length of first addr and of second addr.
263 * They must be of the same length (i.e. same type IP4, IP6).
264 * @return: number of bits the same.
266 int addr_in_common(struct sockaddr_storage
* addr1
, int net1
,
267 struct sockaddr_storage
* addr2
, int net2
, socklen_t addrlen
);
270 * Put address into string, works for IPv4 and IPv6.
271 * @param addr: address
272 * @param addrlen: length of address
273 * @param buf: result string stored here
274 * @param len: length of buf.
275 * On failure a string with "error" is stored inside.
277 void addr_to_str(struct sockaddr_storage
* addr
, socklen_t addrlen
,
278 char* buf
, size_t len
);
281 * See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0"
282 * @param addr: address
283 * @param addrlen: length of address
286 int addr_is_ip4mapped(struct sockaddr_storage
* addr
, socklen_t addrlen
);
289 * See if sockaddr is 255.255.255.255.
290 * @param addr: address
291 * @param addrlen: length of address
294 int addr_is_broadcast(struct sockaddr_storage
* addr
, socklen_t addrlen
);
297 * See if sockaddr is 0.0.0.0 or ::0.
298 * @param addr: address
299 * @param addrlen: length of address
302 int addr_is_any(struct sockaddr_storage
* addr
, socklen_t addrlen
);
305 * Insert new socket list item. If fails logs error.
306 * @param list: pointer to pointer to first item.
307 * @param addr: address or NULL if 'cache'.
308 * @param len: length of addr, or 0 if 'cache'.
309 * @param region: where to allocate
311 void sock_list_insert(struct sock_list
** list
, struct sockaddr_storage
* addr
,
312 socklen_t len
, struct regional
* region
);
315 * Append one list to another. Must both be from same qstate(regional).
316 * @param list: pointer to result list that is modified.
317 * @param add: item(s) to add. They are prepended to list.
319 void sock_list_prepend(struct sock_list
** list
, struct sock_list
* add
);
323 * @param list: to search in
324 * @param addr: address to look for.
325 * @param len: length. Can be 0, look for 'cache entry'.
326 * @return true if found.
328 int sock_list_find(struct sock_list
* list
, struct sockaddr_storage
* addr
,
332 * Merge socklist into another socket list. Allocates the new entries
333 * freshly and copies them over, so also performs a region switchover.
334 * Allocation failures are logged.
335 * @param list: the destination list (checked for duplicates)
336 * @param region: where to allocate
337 * @param add: the list of entries to add.
339 void sock_list_merge(struct sock_list
** list
, struct regional
* region
,
340 struct sock_list
* add
);
343 * Log libcrypto error with descriptive string. Calls log_err().
344 * @param str: what failed.
346 void log_crypto_err(const char* str
);
349 * create SSL listen context
350 * @param key: private key file.
351 * @param pem: public key cert.
352 * @param verifypem: if nonNULL, verifylocation file.
353 * return SSL_CTX* or NULL on failure (logged).
355 void* listen_sslctx_create(char* key
, char* pem
, char* verifypem
);
358 * create SSL connect context
359 * @param key: if nonNULL (also pem nonNULL), the client private key.
360 * @param pem: client public key (or NULL if key is NULL).
361 * @param verifypem: if nonNULL used for verifylocation file.
362 * @return SSL_CTX* or NULL on failure (logged).
364 void* connect_sslctx_create(char* key
, char* pem
, char* verifypem
);
367 * accept a new fd and wrap it in a BIO in SSL
368 * @param sslctx: the SSL_CTX to use (from listen_sslctx_create()).
369 * @param fd: from accept, nonblocking.
370 * @return SSL or NULL on alloc failure.
372 void* incoming_ssl_fd(void* sslctx
, int fd
);
375 * connect a new fd and wrap it in a BIO in SSL
376 * @param sslctx: the SSL_CTX to use (from connect_sslctx_create())
377 * @param fd: from connect.
378 * @return SSL or NULL on alloc failure
380 void* outgoing_ssl_fd(void* sslctx
, int fd
);
383 * Initialize openssl locking for thread safety
384 * @return false on failure (alloc failure).
386 int ub_openssl_lock_init(void);
389 * De-init the allocated openssl locks
391 void ub_openssl_lock_delete(void);
393 #endif /* NET_HELP_H */