net_help.c File Reference

Implementation of net_help.h. More...

#include "config.h"
#include "util/net_help.h"
#include "util/log.h"
#include "util/data/dname.h"
#include <fcntl.h>

Defines

#define MAX_ADDR_STRLEN   128
 max length of an IP address (the address portion) that we allow

Functions

int str_is_ip6 (const char *str)
 See if string is ip4 or ip6.
int write_socket (int s, const void *buf, size_t size)
 Write (blocking) to a nonblocking socket.
int fd_set_nonblock (int s)
 Set fd nonblocking.
int fd_set_block (int s)
 Set fd (back to) blocking.
int is_pow2 (size_t num)
 See if number is a power of 2.
void * memdup (void *data, size_t len)
 Allocate memory and copy over contents.
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.
int extstrtoaddr (const char *str, struct sockaddr_storage *addr, socklen_t *addrlen)
 Convert address string, with "@port" appendix, to sockaddr.
int ipstrtoaddr (const char *ip, int port, struct sockaddr_storage *addr, socklen_t *addrlen)
 Convert ip address string and port to sockaddr.
int netblockstrtoaddr (const char *str, int port, struct sockaddr_storage *addr, socklen_t *addrlen, int *net)
 Convert ip netblock (ip/netsize) string and port to sockaddr.
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.
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.
int sockaddr_cmp (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 Compare two sockaddrs.
int sockaddr_cmp_addr (struct sockaddr_storage *addr1, socklen_t len1, struct sockaddr_storage *addr2, socklen_t len2)
 Compare two sockaddrs.
int addr_is_ip6 (struct sockaddr_storage *addr, socklen_t len)
 Checkout address family.
void addr_mask (struct sockaddr_storage *addr, socklen_t len, int net)
 Make sure the sockaddr ends in zeroes.
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.


Detailed Description

Implementation of net_help.h.


Function Documentation

int str_is_ip6 ( const char *  str  ) 

See if string is ip4 or ip6.

Parameters:
str,: IP specification.
Returns:
: true if string addr is an ip6 specced address.

Referenced by calc_num46(), ipstrtoaddr(), listening_ports_open(), net_test(), netblockstrtoaddr(), outside_network_create(), service(), and ub_ctx_hosts().

int write_socket ( int  s,
const void *  buf,
size_t  size 
)

Write (blocking) to a nonblocking socket.

Parameters:
s,: fd. Is set to be nonblocking at exit.
buf,: data buffer.
size,: length of data to send.
Returns:
: 0 on error. errno is set.

References fd_set_block(), and fd_set_nonblock().

Referenced by worker_send_cmd().

int fd_set_nonblock ( int  s  ) 

Set fd nonblocking.

Parameters:
s,: file descriptor.
Returns:
: 0 on error (error is printed to log).

References log_err().

Referenced by create_tcp_accept_sock(), create_udp_sock(), find_create_proxy(), libworker_read_msg(), libworker_write_msg(), outnet_tcp_take_into_use(), service(), service_tcp_listen(), ub_ctx_create(), worker_create(), and write_socket().

int fd_set_block ( int  s  ) 

Set fd (back to) blocking.

Parameters:
s,: file descriptor.
Returns:
: 0 on error (error is printed to log).

References log_err().

Referenced by libworker_read_msg(), libworker_write_msg(), and write_socket().

int is_pow2 ( size_t  num  ) 

See if number is a power of 2.

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

Referenced by net_test().

void* memdup ( void *  data,
size_t  len 
)

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:
v,: at what verbosity level to print this.
str,: descriptive string printed with it.
addr,: the sockaddr to print. Can be ip4 or ip6.
addrlen,: length of addr.

References log_info(), and verbosity.

Referenced by comm_point_send_udp_msg(), comm_point_send_udp_msg_if(), comm_point_tcp_accept_callback(), comm_point_tcp_handle_read(), comm_point_tcp_handle_write(), delegpt_log(), do_proxy(), outnet_tcp_cb(), outnet_tcp_take_into_use(), outnet_udp_cb(), pending_find_match(), processQueryTargets(), service_recv(), service_send(), service_tcp_listen(), service_tcp_relay(), serviced_callbacks(), serviced_tcp_callback(), tcp_proxy_delete(), and worker_handle_request().

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:
str,: the string
addr,: where to store sockaddr.
addrlen,: length of stored sockaddr is returned.
Returns:
0 on error.

References ipstrtoaddr(), and MAX_ADDR_STRLEN.

Referenced by ah(), main(), open_svr(), read_fwds_addr(), read_stubs_addr(), replay_moment_read(), replay_range_read(), service(), ub_ctx_set_fwd(), and warn_hosts().

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

Convert ip address string and port to sockaddr.

Parameters:
ip,: ip4 or ip6 address string.
port,: port number, host format.
addr,: where to store sockaddr.
addrlen,: length of stored sockaddr is returned.
Returns:
0 on error.

References str_is_ip6().

Referenced by extstrtoaddr(), interfacechecks(), netblockstrtoaddr(), service(), and setup_if().

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.

*SLOW*, does a malloc internally to avoid writing over 'ip' string.

Parameters:
ip,: ip4 or ip6 address string.
port,: port number, host format.
addr,: where to store sockaddr.
addrlen,: length of stored sockaddr is returned.
net,: netblock size is returned.
Returns:
0 on error.

References addr_mask(), ipstrtoaddr(), log_err(), and str_is_ip6().

Referenced by acl_list_str_cfg(), aclchecks(), and donotq_str_cfg().

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

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:
v,: at what verbosity level to print this.
str,: descriptive string printed with it.
zone,: DNS domain name, uncompressed wireformat.
addr,: the sockaddr to print. Can be ip4 or ip6.
addrlen,: length of addr.

References dname_str(), verbose(), and verbosity.

Referenced by process_response(), processQueryTargets(), and serviced_udp_callback().

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:
addr1,: address 1.
len1,: lengths of addr1.
addr2,: address 2.
len2,: lengths of addr2.
Returns:
: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.

References INET6_SIZE, INET_SIZE, and log_assert.

Referenced by answer_check_it(), find_create_proxy(), infra_host_compfunc(), outnet_serviced_query(), pending_cmp(), pending_find_match(), pending_matches_current(), pending_tcp_query(), pending_udp_query(), and serviced_cmp().

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:
addr1,: address 1.
len1,: lengths of addr1.
addr2,: address 2.
len2,: lengths of addr2.
Returns:
: 0 if addr1 == addr2. -1 if addr1 is smaller, +1 if larger.

References INET6_SIZE, INET_SIZE, and log_assert.

Referenced by acl_list_cmp(), and donotq_cmp().

int addr_is_ip6 ( struct sockaddr_storage *  addr,
socklen_t  len 
)

Checkout address family.

Parameters:
addr,: the sockaddr to examine.
len,: the length of addr. return: true if sockaddr is ip6.

Referenced by acl_list_lookup(), addr_in_common(), addr_mask(), donotq_lookup(), iter_filter_unsuitable(), open_svr(), outnet_tcp_take_into_use(), perfsetup(), proxy_list_clear(), randomize_and_send_udp(), service_recv(), service_tcp_listen(), udp_sockport(), and warn_hosts().

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:
addr,: the ip4 or ip6 addr.
len,: length of addr.
net,: number of bits to leave untouched, the rest of the netblock address is zeroed.

References addr_is_ip6().

Referenced by net_test(), and netblockstrtoaddr().

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:
addr1,: first addr.
net1,: netblock size of first addr.
addr2,: second addr.
net2,: netblock size of second addr.
addrlen,: length 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.

References addr_is_ip6(), log_assert, and match().

Referenced by acl_list_init_parents(), acl_list_lookup(), donotq_init_parents(), donotq_lookup(), and net_test().


Generated on Sun Sep 21 16:23:30 2008 for unbound by  doxygen 1.5.6