5
# include <sys/socket.h>
6
# include <netinet/in.h>
8
# include <arpa/inet.h>
17
# define AF_INET6 PF_INET6
24
unsigned short family;
32
ARRAY_DEFINE_TYPE(ip_addr, struct ip_addr);
34
struct net_unix_cred {
39
/* maxmimum string length of IP address */
41
# define MAX_IP_LEN INET6_ADDRSTRLEN
43
# define MAX_IP_LEN 20
46
#define IPADDR_IS_V4(ip) ((ip)->family == AF_INET)
47
#define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6)
48
#define IPADDR_BITS(ip) (IPADDR_IS_V4(ip) ? 32 : 128)
50
/* Returns TRUE if IPs are the same */
51
bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2);
52
/* Returns 0 if IPs are the same, -1 or 1 otherwise. */
53
int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2);
54
unsigned int net_ip_hash(const struct ip_addr *ip);
56
/* Connect to socket with ip address. The socket and connect() is
58
int net_connect_ip(const struct ip_addr *ip, unsigned int port,
59
const struct ip_addr *my_ip);
60
/* Like net_connect_ip(), but do a blocking connect(). */
61
int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
62
const struct ip_addr *my_ip);
63
/* Returns 0 if we can bind() as given IP, -1 if not. */
64
int net_try_bind(const struct ip_addr *ip);
65
/* Connect to named UNIX socket */
66
int net_connect_unix(const char *path);
67
/* Try to connect to UNIX socket for give number of seconds when connect()
68
returns EAGAIN or ECONNREFUSED. */
69
int net_connect_unix_with_retries(const char *path, unsigned int msecs);
70
/* Disconnect socket */
71
void net_disconnect(int fd);
73
/* Set socket blocking/nonblocking */
74
void net_set_nonblock(int fd, bool nonblock);
75
/* Set TCP_CORK if supported, ie. don't send out partial frames.
76
Returns 0 if ok, -1 if failed. */
77
int net_set_cork(int fd, bool cork);
79
/* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may
80
include IPv4 depending on the system (Linux yes, BSD no). */
81
void net_get_ip_any4(struct ip_addr *ip);
82
void net_get_ip_any6(struct ip_addr *ip);
84
/* Listen for connections on a socket */
85
int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog);
86
/* Listen for connections on an UNIX socket */
87
int net_listen_unix(const char *path, int backlog);
88
/* Like net_listen_unix(), but if socket already exists, try to connect to it.
89
If it fails with ECONNREFUSED, unlink the socket and try creating it
91
int net_listen_unix_unlink_stale(const char *path, int backlog);
92
/* Accept a connection on a socket. Returns -1 if the connection got closed,
93
-2 for other failures. For UNIX sockets addr->family=port=0. */
94
int net_accept(int fd, struct ip_addr *addr, unsigned int *port);
96
/* Read data from socket, return number of bytes read,
97
-1 = error, -2 = disconnected */
98
ssize_t net_receive(int fd, void *buf, size_t len);
99
/* Transmit data, return number of bytes sent, -1 = error, -2 = disconnected */
100
ssize_t net_transmit(int fd, const void *data, size_t len);
102
/* Get IP addresses for host. ips contains ips_count of IPs, they don't need
103
to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
104
int net_gethostbyname(const char *addr, struct ip_addr **ips,
105
unsigned int *ips_count);
106
/* get error of net_gethostname() */
107
const char *net_gethosterror(int error) ATTR_CONST;
108
/* return TRUE if host lookup failed because it didn't exist (ie. not
109
some error with name server) */
110
int net_hosterror_notfound(int error) ATTR_CONST;
112
/* Get socket local address/port. For UNIX sockets addr->family=port=0. */
113
int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port);
114
/* Get socket remote address/port. For UNIX sockets addr->family=port=0. */
115
int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port);
116
/* Get UNIX socket name. */
117
int net_getunixname(int fd, const char **name_r);
118
/* Get UNIX socket peer process's credentials. */
119
int net_getunixcred(int fd, struct net_unix_cred *cred_r);
121
/* Returns ip_addr as string, or NULL if ip is invalid. */
122
const char *net_ip2addr(const struct ip_addr *ip);
123
/* char* -> struct ip_addr translation. */
124
int net_addr2ip(const char *addr, struct ip_addr *ip);
125
/* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if
126
successful, -1 if the source address isn't IPv6 mapped IPv4 address. */
127
int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
128
struct ip_addr *dest);
130
/* Get socket error */
131
int net_geterror(int fd);
133
/* Get name of TCP service */
134
const char *net_getservbyport(unsigned short port) ATTR_CONST;
136
bool is_ipv4_address(const char *addr) ATTR_PURE;
137
bool is_ipv6_address(const char *addr) ATTR_PURE;
139
/* Parse network as ip/bits. Returns 0 if successful, -1 if invalid input. */
140
int net_parse_range(const char *network, struct ip_addr *ip_r,
141
unsigned int *bits_r);
142
/* Returns TRUE if ip is in net_ip/bits network. IPv6 mapped IPv4 addresses
143
are converted to plain IPv4 addresses before matching. */
144
bool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip,
145
unsigned int bits) ATTR_PURE;