#ifndef NETWORK_H
#define NETWORK_H
#ifndef WIN32
# include <sys/socket.h>
# include <netinet/in.h>
# include <netdb.h>
# include <arpa/inet.h>
#endif
#ifdef HAVE_SOCKS_H
#include <socks.h>
#endif
#ifndef AF_INET6
# ifdef PF_INET6
# define AF_INET6 PF_INET6
# else
# define AF_INET6 10
# endif
#endif
struct ip_addr {
unsigned short family;
union {
#ifdef HAVE_IPV6
struct in6_addr ip6;
#endif
struct in_addr ip4;
} u;
};
ARRAY_DEFINE_TYPE(ip_addr, struct ip_addr);
#ifdef HAVE_IPV6
# define MAX_IP_LEN INET6_ADDRSTRLEN
#else
# define MAX_IP_LEN 20
#endif
#define IPADDR_IS_V4(ip) ((ip)->family == AF_INET)
#define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6)
bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2);
int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2);
unsigned int net_ip_hash(const struct ip_addr *ip);
int net_connect_ip(const struct ip_addr *ip, unsigned int port,
const struct ip_addr *my_ip);
int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
const struct ip_addr *my_ip);
int net_try_bind(const struct ip_addr *ip);
int net_connect_unix(const char *path);
int net_connect_unix_with_retries(const char *path, unsigned int msecs);
void net_disconnect(int fd);
void net_set_nonblock(int fd, bool nonblock);
int net_set_cork(int fd, bool cork);
void net_get_ip_any4(struct ip_addr *ip);
void net_get_ip_any6(struct ip_addr *ip);
int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog);
int net_listen_unix(const char *path, int backlog);
int net_listen_unix_unlink_stale(const char *path, int backlog);
int net_accept(int fd, struct ip_addr *addr, unsigned int *port);
ssize_t net_receive(int fd, void *buf, size_t len);
ssize_t net_transmit(int fd, const void *data, size_t len);
int net_gethostbyname(const char *addr, struct ip_addr **ips,
unsigned int *ips_count);
const char *net_gethosterror(int error) ATTR_CONST;
int net_hosterror_notfound(int error) ATTR_CONST;
int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port);
int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port);
int net_getunixname(int fd, const char **name_r);
const char *net_ip2addr(const struct ip_addr *ip);
int net_addr2ip(const char *addr, struct ip_addr *ip);
int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
struct ip_addr *dest);
int net_geterror(int fd);
const char *net_getservbyport(unsigned short port) ATTR_CONST;
bool is_ipv4_address(const char *addr) ATTR_PURE;
bool is_ipv6_address(const char *addr) ATTR_PURE;
int net_parse_range(const char *network, struct ip_addr *ip_r,
unsigned int *bits_r);
bool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip,
unsigned int bits) ATTR_PURE;
#endif