Electroneum
net_help.h File Reference
#include "util/log.h"
Include dependency graph for net_help.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define BIT_CD   0x0010
 
#define BIT_AD   0x0020
 
#define BIT_Z   0x0040
 
#define BIT_RA   0x0080
 
#define BIT_RD   0x0100
 
#define BIT_TC   0x0200
 
#define BIT_AA   0x0400
 
#define BIT_QR   0x8000
 
#define FLAGS_GET_RCODE(f)   ((f) & 0xf)
 
#define FLAGS_SET_RCODE(f, r)   (f = (((f) & 0xfff0) | (r)))
 
#define UDP_AUTH_QUERY_TIMEOUT   3000
 
#define TCP_AUTH_QUERY_TIMEOUT   3000
 
#define EDNS_ADVERTISED_VERSION   0
 
#define EDNS_DO   0x8000 /* Dnssec Ok */
 
#define INET_SIZE   4
 
#define INET6_SIZE   16
 
#define DNSKEY_BIT_ZSK   0x0100
 
#define DNSKEY_BIT_SEP   0x0001
 

Functions

int str_is_ip6 (const char *str)
 
int fd_set_nonblock (int s)
 
int fd_set_block (int s)
 
int is_pow2 (size_t num)
 
void * memdup (void *data, size_t len)
 
void log_addr (enum verbosity_value v, const char *str, struct sockaddr_storage *addr, socklen_t addrlen)
 
void log_name_addr (enum verbosity_value v, const char *str, uint8_t *zone, struct sockaddr_storage *addr, socklen_t addrlen)
 
void log_err_addr (const char *str, const char *err, struct sockaddr_storage *addr, socklen_t addrlen)
 
int extstrtoaddr (const char *str, struct sockaddr_storage *addr, socklen_t *addrlen)
 
int ipstrtoaddr (const char *ip, int port, struct sockaddr_storage *addr, socklen_t *addrlen)
 
int netblockstrtoaddr (const char *ip, int port, struct sockaddr_storage *addr, socklen_t *addrlen, int *net)
 
int authextstrtoaddr (char *str, struct sockaddr_storage *addr, socklen_t *addrlen, char **auth_name)
 
void sockaddr_store_port (struct sockaddr_storage *addr, socklen_t addrlen, int port)
 
void log_nametypeclass (enum verbosity_value v, const char *str, uint8_t *name, uint16_t type, uint16_t dclass)
 
int sockaddr_cmp (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 
int sockaddr_cmp_addr (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 
int addr_is_ip6 (struct sockaddr_storage *addr, socklen_t len)
 
void addr_mask (struct sockaddr_storage *addr, socklen_t len, int net)
 
int addr_in_common (struct sockaddr_storage *addr1, int net1, struct sockaddr_storage *addr2, int net2, socklen_t addrlen)
 
void addr_to_str (struct sockaddr_storage *addr, socklen_t addrlen, char *buf, size_t len)
 
int addr_is_ip4mapped (struct sockaddr_storage *addr, socklen_t addrlen)
 
int addr_is_broadcast (struct sockaddr_storage *addr, socklen_t addrlen)
 
int addr_is_any (struct sockaddr_storage *addr, socklen_t addrlen)
 
void sock_list_insert (struct sock_list **list, struct sockaddr_storage *addr, socklen_t len, struct regional *region)
 
void sock_list_prepend (struct sock_list **list, struct sock_list *add)
 
int sock_list_find (struct sock_list *list, struct sockaddr_storage *addr, socklen_t len)
 
void sock_list_merge (struct sock_list **list, struct regional *region, struct sock_list *add)
 
void log_crypto_err (const char *str)
 
int listen_sslctx_setup (void *ctxt)
 
void listen_sslctx_setup_2 (void *ctxt)
 
void * listen_sslctx_create (char *key, char *pem, char *verifypem)
 
void * connect_sslctx_create (char *key, char *pem, char *verifypem, int wincert)
 
void * incoming_ssl_fd (void *sslctx, int fd)
 
void * outgoing_ssl_fd (void *sslctx, int fd)
 
int ub_openssl_lock_init (void)
 
void ub_openssl_lock_delete (void)
 

Variables

uint16_t EDNS_ADVERTISED_SIZE
 
int MINIMAL_RESPONSES
 
int RRSET_ROUNDROBIN
 

Detailed Description

This file contains functions to perform network related tasks.

Definition in file net_help.h.

Macro Definition Documentation

◆ BIT_AA

#define BIT_AA   0x0400

AA flag

Definition at line 68 of file net_help.h.

◆ BIT_AD

#define BIT_AD   0x0020

AD flag

Definition at line 58 of file net_help.h.

◆ BIT_CD

#define BIT_CD   0x0010

DNS constants for uint16_t style flag manipulation. host byteorder. 1 1 1 1 1 1 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 +–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+ |QR| Opcode |AA|TC|RD|RA| Z|AD|CD| RCODE | +–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+–+CD flag

Definition at line 56 of file net_help.h.

◆ BIT_QR

#define BIT_QR   0x8000

QR flag

Definition at line 70 of file net_help.h.

◆ BIT_RA

#define BIT_RA   0x0080

RA flag

Definition at line 62 of file net_help.h.

◆ BIT_RD

#define BIT_RD   0x0100

RD flag

Definition at line 64 of file net_help.h.

◆ BIT_TC

#define BIT_TC   0x0200

TC flag

Definition at line 66 of file net_help.h.

◆ BIT_Z

#define BIT_Z   0x0040

Z flag

Definition at line 60 of file net_help.h.

◆ DNSKEY_BIT_SEP

#define DNSKEY_BIT_SEP   0x0001

DNSKEY secure entry point, KSK flag

Definition at line 94 of file net_help.h.

◆ DNSKEY_BIT_ZSK

#define DNSKEY_BIT_ZSK   0x0100

DNSKEY zone sign key flag

Definition at line 92 of file net_help.h.

◆ EDNS_ADVERTISED_VERSION

#define EDNS_ADVERTISED_VERSION   0

Advertised version of EDNS capabilities

Definition at line 81 of file net_help.h.

◆ EDNS_DO

#define EDNS_DO   0x8000 /* Dnssec Ok */

bits for EDNS bitfield

Definition at line 85 of file net_help.h.

◆ FLAGS_GET_RCODE

#define FLAGS_GET_RCODE (   f)    ((f) & 0xf)

get RCODE bits from uint16 flags

Definition at line 72 of file net_help.h.

◆ FLAGS_SET_RCODE

#define FLAGS_SET_RCODE (   f,
 
)    (f = (((f) & 0xfff0) | (r)))

set RCODE bits in uint16 flags

Definition at line 74 of file net_help.h.

◆ INET6_SIZE

#define INET6_SIZE   16

byte size of ip6 address

Definition at line 89 of file net_help.h.

◆ INET_SIZE

#define INET_SIZE   4

byte size of ip4 address

Definition at line 87 of file net_help.h.

◆ TCP_AUTH_QUERY_TIMEOUT

#define TCP_AUTH_QUERY_TIMEOUT   3000

timeout in milliseconds for TCP queries to auth servers.

Definition at line 79 of file net_help.h.

◆ UDP_AUTH_QUERY_TIMEOUT

#define UDP_AUTH_QUERY_TIMEOUT   3000

timeout in milliseconds for UDP queries to auth servers.

Definition at line 77 of file net_help.h.

Function Documentation

◆ addr_in_common()

int addr_in_common ( struct sockaddr_storage addr1,
int  net1,
struct sockaddr_storage addr2,
int  net2,
socklen_t  addrlen 
)

See how many bits are shared, equal, between two addrs.

Parameters
addr1first addr.
net1netblock size of first addr.
addr2second addr.
net2netblock size of second addr.
addrlenlength of first addr and of second addr. They must be of the same length (i.e. same type IP4, IP6).
Returns
: number of bits the same.

◆ addr_is_any()

int addr_is_any ( struct sockaddr_storage addr,
socklen_t  addrlen 
)

See if sockaddr is 0.0.0.0 or ::0.

Parameters
addraddress
addrlenlength of address
Returns
true if so

◆ addr_is_broadcast()

int addr_is_broadcast ( struct sockaddr_storage addr,
socklen_t  addrlen 
)

See if sockaddr is 255.255.255.255.

Parameters
addraddress
addrlenlength of address
Returns
true if so

◆ addr_is_ip4mapped()

int addr_is_ip4mapped ( struct sockaddr_storage addr,
socklen_t  addrlen 
)

See if sockaddr is an ipv6 mapped ipv4 address, "::ffff:0.0.0.0"

Parameters
addraddress
addrlenlength of address
Returns
true if so

◆ addr_is_ip6()

int addr_is_ip6 ( struct sockaddr_storage addr,
socklen_t  len 
)

Checkout address family.

Parameters
addrthe sockaddr to examine.
lenthe length of addr.
Returns
: true if sockaddr is ip6.

◆ addr_mask()

void addr_mask ( struct sockaddr_storage addr,
socklen_t  len,
int  net 
)

Make sure the sockaddr ends in zeroes. For tree insertion and subsequent comparison.

Parameters
addrthe ip4 or ip6 addr.
lenlength of addr.
netnumber of bits to leave untouched, the rest of the netblock address is zeroed.

◆ addr_to_str()

void addr_to_str ( struct sockaddr_storage addr,
socklen_t  addrlen,
char *  buf,
size_t  len 
)

Put address into string, works for IPv4 and IPv6.

Parameters
addraddress
addrlenlength of address
bufresult string stored here
lenlength of buf. On failure a string with "error" is stored inside.

◆ authextstrtoaddr()

int authextstrtoaddr ( char *  str,
struct sockaddr_storage addr,
socklen_t *  addrlen,
char **  auth_name 
)

Convert address string, with "@port" appendix, to sockaddr. It can also have an "#tls-auth-name" appendix (after the port). The returned tls-auth-name string is a pointer into the input string. Uses DNS port by default.

Parameters
strthe string
addrwhere to store sockaddr.
addrlenlength of stored sockaddr is returned.
auth_namereturned pointer to tls_auth_name, or NULL if none.
Returns
0 on error.

◆ connect_sslctx_create()

void* connect_sslctx_create ( char *  key,
char *  pem,
char *  verifypem,
int  wincert 
)

create SSL connect context

Parameters
keyif nonNULL (also pem nonNULL), the client private key.
pemclient public key (or NULL if key is NULL).
verifypemif nonNULL used for verifylocation file.
wincertadd system certificate store to ctx (add to verifypem ca certs).
Returns
SSL_CTX* or NULL on failure (logged).

◆ extstrtoaddr()

int extstrtoaddr ( const char *  str,
struct sockaddr_storage addr,
socklen_t *  addrlen 
)

Convert address string, with "@port" appendix, to sockaddr. Uses DNS port by default.

Parameters
strthe string
addrwhere to store sockaddr.
addrlenlength of stored sockaddr is returned.
Returns
0 on error.

◆ fd_set_block()

int fd_set_block ( int  s)

Set fd (back to) blocking.

Parameters
sfile descriptor.
Returns
: 0 on error (error is printed to log).

◆ fd_set_nonblock()

int fd_set_nonblock ( int  s)

Set fd nonblocking.

Parameters
sfile descriptor.
Returns
: 0 on error (error is printed to log).

◆ incoming_ssl_fd()

void* incoming_ssl_fd ( void *  sslctx,
int  fd 
)

accept a new fd and wrap it in a BIO in SSL

Parameters
sslctxthe SSL_CTX to use (from listen_sslctx_create()).
fdfrom accept, nonblocking.
Returns
SSL or NULL on alloc failure.

◆ ipstrtoaddr()

int ipstrtoaddr ( const char *  ip,
int  port,
struct sockaddr_storage addr,
socklen_t *  addrlen 
)

Convert ip address string and port to sockaddr.

Parameters
ipip4 or ip6 address string.
portport number, host format.
addrwhere to store sockaddr.
addrlenlength of stored sockaddr is returned.
Returns
0 on error.

◆ is_pow2()

int is_pow2 ( size_t  num)

See if number is a power of 2.

Parameters
numthe value.
Returns
: true if the number is a power of 2.

◆ listen_sslctx_create()

void* listen_sslctx_create ( char *  key,
char *  pem,
char *  verifypem 
)

create SSL listen context

Parameters
keyprivate key file.
pempublic key cert.
verifypemif nonNULL, verifylocation file. return SSL_CTX* or NULL on failure (logged).

◆ listen_sslctx_setup()

int listen_sslctx_setup ( void *  ctxt)

Set SSL_OP_NOxxx options on SSL context to disable bad crypto

Parameters
ctxtSSL_CTX*
Returns
false on failure.

◆ listen_sslctx_setup_2()

void listen_sslctx_setup_2 ( void *  ctxt)

Further setup of listening SSL context, after keys loaded.

Parameters
ctxtSSL_CTX*

◆ log_addr()

void log_addr ( enum verbosity_value  v,
const char *  str,
struct sockaddr_storage addr,
socklen_t  addrlen 
)

Prints the sockaddr in readable format with log_info. Debug helper.

Parameters
vat what verbosity level to print this.
strdescriptive string printed with it.
addrthe sockaddr to print. Can be ip4 or ip6.
addrlenlength of addr.

◆ log_crypto_err()

void log_crypto_err ( const char *  str)

Log libcrypto error with descriptive string. Calls log_err().

Parameters
strwhat failed.

◆ log_err_addr()

void log_err_addr ( const char *  str,
const char *  err,
struct sockaddr_storage addr,
socklen_t  addrlen 
)

Log errno and addr.

Parameters
strdescriptive string printed with it.
errerrno string to print, i.e. strerror(errno).
addrthe sockaddr to print. Can be ip4 or ip6.
addrlenlength of addr.

◆ log_name_addr()

void log_name_addr ( enum verbosity_value  v,
const char *  str,
uint8_t zone,
struct sockaddr_storage addr,
socklen_t  addrlen 
)

Prints zone name and sockaddr in readable format with log_info. Debug.

Parameters
vat what verbosity level to print this.
strdescriptive string printed with it.
zoneDNS domain name, uncompressed wireformat.
addrthe sockaddr to print. Can be ip4 or ip6.
addrlenlength of addr.

◆ log_nametypeclass()

void log_nametypeclass ( enum verbosity_value  v,
const char *  str,
uint8_t name,
uint16_t  type,
uint16_t  dclass 
)

Print string with neat domain name, type and class.

Parameters
vat what verbosity level to print this.
strstring of message.
namedomain name uncompressed wireformat.
typehost format RR type.
dclasshost format RR class.

◆ memdup()

void* memdup ( void *  data,
size_t  len 
)

Allocate memory and copy over contents.

Parameters
datawhat to copy over.
lenlength of data.
Returns
: NULL on malloc failure, or newly malloced data.

◆ netblockstrtoaddr()

int netblockstrtoaddr ( const char *  ip,
int  port,
struct sockaddr_storage addr,
socklen_t *  addrlen,
int *  net 
)

Convert ip netblock (ip/netsize) string and port to sockaddr. performs a copy internally to avoid writing over 'ip' string.

Parameters
ipip4 or ip6 address string.
portport number, host format.
addrwhere to store sockaddr.
addrlenlength of stored sockaddr is returned.
netnetblock size is returned.
Returns
0 on error.

◆ outgoing_ssl_fd()

void* outgoing_ssl_fd ( void *  sslctx,
int  fd 
)

connect a new fd and wrap it in a BIO in SSL

Parameters
sslctxthe SSL_CTX to use (from connect_sslctx_create())
fdfrom connect.
Returns
SSL or NULL on alloc failure

◆ sock_list_find()

int sock_list_find ( struct sock_list list,
struct sockaddr_storage addr,
socklen_t  len 
)

Find addr in list.

Parameters
listto search in
addraddress to look for.
lenlength. Can be 0, look for 'cache entry'.
Returns
true if found.

◆ sock_list_insert()

void sock_list_insert ( struct sock_list **  list,
struct sockaddr_storage addr,
socklen_t  len,
struct regional region 
)

Insert new socket list item. If fails logs error.

Parameters
listpointer to pointer to first item.
addraddress or NULL if 'cache'.
lenlength of addr, or 0 if 'cache'.
regionwhere to allocate

◆ sock_list_merge()

void sock_list_merge ( struct sock_list **  list,
struct regional region,
struct sock_list add 
)

Merge socklist into another socket list. Allocates the new entries freshly and copies them over, so also performs a region switchover. Allocation failures are logged.

Parameters
listthe destination list (checked for duplicates)
regionwhere to allocate
addthe list of entries to add.

◆ sock_list_prepend()

void sock_list_prepend ( struct sock_list **  list,
struct sock_list add 
)

Append one list to another. Must both be from same qstate(regional).

Parameters
listpointer to result list that is modified.
additem(s) to add. They are prepended to list.

◆ sockaddr_cmp()

int sockaddr_cmp ( struct sockaddr_storage addr1,
socklen_t  len1,
struct sockaddr_storage addr2,
socklen_t  len2 
)

Compare two sockaddrs. Imposes an ordering on the addresses. Compares address and port.

Parameters
addr1address 1.
len1lengths of addr1.
addr2address 2.
len2lengths of addr2.
Returns
: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.

◆ sockaddr_cmp_addr()

int sockaddr_cmp_addr ( struct sockaddr_storage addr1,
socklen_t  len1,
struct sockaddr_storage addr2,
socklen_t  len2 
)

Compare two sockaddrs. Compares address, not the port.

Parameters
addr1address 1.
len1lengths of addr1.
addr2address 2.
len2lengths of addr2.
Returns
: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.

◆ sockaddr_store_port()

void sockaddr_store_port ( struct sockaddr_storage addr,
socklen_t  addrlen,
int  port 
)

Store port number into sockaddr structure

Parameters
addrsockaddr structure, ip4 or ip6.
addrlenlength of addr.
portport number to put into the addr.

◆ str_is_ip6()

int str_is_ip6 ( const char *  str)

See if string is ip4 or ip6.

Parameters
strIP specification.
Returns
: true if string addr is an ip6 specced address.

◆ ub_openssl_lock_delete()

void ub_openssl_lock_delete ( void  )

De-init the allocated openssl locks

◆ ub_openssl_lock_init()

int ub_openssl_lock_init ( void  )

Initialize openssl locking for thread safety

Returns
false on failure (alloc failure).

Variable Documentation

◆ EDNS_ADVERTISED_SIZE

uint16_t EDNS_ADVERTISED_SIZE

Advertised size of EDNS capabilities

◆ MINIMAL_RESPONSES

int MINIMAL_RESPONSES

minimal responses when positive answer

◆ RRSET_ROUNDROBIN

int RRSET_ROUNDROBIN

rrset order roundrobin