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