net.h revision b270c58fc227c91a88b07316d680418ec75021e9
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen unsigned short family;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* maxmimum string length of IP address */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#define IPADDR_IS_V4(ip) ((ip)->family == AF_INET)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen#define IPADDR_BITS(ip) (IPADDR_IS_V4(ip) ? 32 : 128)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen /* Try to use SO_REUSEPORT if available. If it's not, this flag is
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen cleared on return. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Returns TRUE if IPs are the same */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenbool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Returns 0 if IPs are the same, -1 or 1 otherwise. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenunsigned int net_ip_hash(const struct ip_addr *ip);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Connect to socket with ip address. The socket and connect() is
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen non-blocking. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_connect_ip(const struct ip_addr *ip, unsigned int port,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Like net_connect_ip(), but do a blocking connect(). */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Returns 0 if we can bind() as given IP, -1 if not. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Connect to named UNIX socket */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Try to connect to UNIX socket for give number of seconds when connect()
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen returns EAGAIN or ECONNREFUSED. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_connect_unix_with_retries(const char *path, unsigned int msecs);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Disconnect socket */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Set socket blocking/nonblocking */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Set TCP_CORK if supported, ie. don't send out partial frames.
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen Returns 0 if ok, -1 if failed. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_set_cork(int fd, bool cork) ATTR_NOWARN_UNUSED_RESULT;
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen include IPv4 depending on the system (Linux yes, BSD no). */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Listen for connections on a socket */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_listen_full(const struct ip_addr *my_ip, unsigned int *port,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Listen for connections on an UNIX socket */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_listen_unix(const char *path, int backlog);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Like net_listen_unix(), but if socket already exists, try to connect to it.
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen If it fails with ECONNREFUSED, unlink the socket and try creating it
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_listen_unix_unlink_stale(const char *path, int backlog);
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Accept a connection on a socket. Returns -1 if the connection got closed,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen -2 for other failures. For UNIX sockets addr_r->family=port=0. */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenint net_accept(int fd, struct ip_addr *addr_r, unsigned int *port_r)
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen/* Read data from socket, return number of bytes read,
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainen -1 = error, -2 = disconnected */
cf636afb3826f0d8e15c248aa1fc04ce72820e08Timo Sirainenssize_t net_receive(int fd, void *buf, size_t len);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Transmit data, return number of bytes sent, -1 = error, -2 = disconnected */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenssize_t net_transmit(int fd, const void *data, size_t len);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Get IP addresses for host. ips contains ips_count of IPs, they don't need
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_gethostbyname(const char *addr, struct ip_addr **ips,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen unsigned int *ips_count);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Return host for the IP address. Returns 0 = ok, others = error code for
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen net_gethosterror(). */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_gethostbyaddr(const struct ip_addr *ip, const char **name_r);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* get error of net_gethostname() */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenconst char *net_gethosterror(int error) ATTR_CONST;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* return TRUE if host lookup failed because it didn't exist (ie. not
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen some error with name server) */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_hosterror_notfound(int error) ATTR_CONST;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Get socket local address/port. For UNIX sockets addr->family=port=0. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_getsockname(int fd, struct ip_addr *addr, unsigned int *port)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Get socket remote address/port. For UNIX sockets addr->family=port=0. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_getpeername(int fd, struct ip_addr *addr, unsigned int *port)
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Get UNIX socket name. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_getunixname(int fd, const char **name_r);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Get UNIX socket peer process's credentials. The pid may be (pid_t)-1 if
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen unavailable. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_getunixcred(int fd, struct net_unix_cred *cred_r);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Returns ip_addr as string, or "" if ip isn't valid IPv4 or IPv6 address. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenconst char *net_ip2addr(const struct ip_addr *ip);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* char* -> struct ip_addr translation. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_addr2ip(const char *addr, struct ip_addr *ip);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen successful, -1 if the source address isn't IPv6 mapped IPv4 address. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Get socket error */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Get name of TCP service */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenconst char *net_getservbyport(unsigned short port) ATTR_CONST;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenbool is_ipv4_address(const char *addr) ATTR_PURE;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenbool is_ipv6_address(const char *addr) ATTR_PURE;
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Parse network as ip/bits. Returns 0 if successful, -1 if invalid input. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenint net_parse_range(const char *network, struct ip_addr *ip_r,
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen unsigned int *bits_r);
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen/* Returns TRUE if ip is in net_ip/bits network. IPv6 mapped IPv4 addresses
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen are converted to plain IPv4 addresses before matching. Invalid IPs
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainen (family=0) never match anything. */
baf346e71ebd7b44fcba4b48f4d39845453b778bTimo Sirainenbool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip,