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 {
40
/* maxmimum string length of IP address */
42
# define MAX_IP_LEN INET6_ADDRSTRLEN
44
# define MAX_IP_LEN 20
49
# define EAI_NONAME NO_ADDRESS
51
# define EAI_FAIL NO_RECOVERY
54
#define IPADDR_IS_V4(ip) ((ip)->family == AF_INET)
55
#define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6)
56
#define IPADDR_BITS(ip) (IPADDR_IS_V4(ip) ? 32 : 128)
58
/* Returns TRUE if IPs are the same */
59
bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2);
60
/* Returns 0 if IPs are the same, -1 or 1 otherwise. */
61
int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2);
62
unsigned int net_ip_hash(const struct ip_addr *ip);
64
/* Connect to socket with ip address. The socket and connect() is
66
int net_connect_ip(const struct ip_addr *ip, unsigned int port,
67
const struct ip_addr *my_ip) ATTR_NULL(3);
68
/* Like net_connect_ip(), but do a blocking connect(). */
69
int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port,
70
const struct ip_addr *my_ip) ATTR_NULL(3);
71
/* Returns 0 if we can bind() as given IP, -1 if not. */
72
int net_try_bind(const struct ip_addr *ip);
73
/* Connect to named UNIX socket */
74
int net_connect_unix(const char *path);
75
/* Try to connect to UNIX socket for give number of seconds when connect()
76
returns EAGAIN or ECONNREFUSED. */
77
int net_connect_unix_with_retries(const char *path, unsigned int msecs);
78
/* Disconnect socket */
79
void net_disconnect(int fd);
81
/* Set socket blocking/nonblocking */
82
void net_set_nonblock(int fd, bool nonblock);
83
/* Set TCP_CORK if supported, ie. don't send out partial frames.
84
Returns 0 if ok, -1 if failed. */
85
int net_set_cork(int fd, bool cork) ATTR_NOWARN_UNUSED_RESULT;
87
/* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may
88
include IPv4 depending on the system (Linux yes, BSD no). */
89
void net_get_ip_any4(struct ip_addr *ip);
90
void net_get_ip_any6(struct ip_addr *ip);
92
/* Listen for connections on a socket */
93
int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog);
94
/* Listen for connections on an UNIX socket */
95
int net_listen_unix(const char *path, int backlog);
96
/* Like net_listen_unix(), but if socket already exists, try to connect to it.
97
If it fails with ECONNREFUSED, unlink the socket and try creating it
99
int net_listen_unix_unlink_stale(const char *path, int backlog);
100
/* Accept a connection on a socket. Returns -1 if the connection got closed,
101
-2 for other failures. For UNIX sockets addr_r->family=port=0. */
102
int net_accept(int fd, struct ip_addr *addr_r, unsigned int *port_r)
105
/* Read data from socket, return number of bytes read,
106
-1 = error, -2 = disconnected */
107
ssize_t net_receive(int fd, void *buf, size_t len);
108
/* Transmit data, return number of bytes sent, -1 = error, -2 = disconnected */
109
ssize_t net_transmit(int fd, const void *data, size_t len);
111
/* Get IP addresses for host. ips contains ips_count of IPs, they don't need
112
to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
113
int net_gethostbyname(const char *addr, struct ip_addr **ips,
114
unsigned int *ips_count);
115
/* Return host for the IP address. Returns 0 = ok, others = error code for
116
net_gethosterror(). */
117
int net_gethostbyaddr(const struct ip_addr *ip, const char **name_r);
118
/* get error of net_gethostname() */
119
const char *net_gethosterror(int error) ATTR_CONST;
120
/* return TRUE if host lookup failed because it didn't exist (ie. not
121
some error with name server) */
122
int net_hosterror_notfound(int error) ATTR_CONST;
124
/* Get socket local address/port. For UNIX sockets addr->family=port=0. */
125
int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port)
127
/* Get socket remote address/port. For UNIX sockets addr->family=port=0. */
128
int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port)
130
/* Get UNIX socket name. */
131
int net_getunixname(int fd, const char **name_r);
132
/* Get UNIX socket peer process's credentials. The pid may be (pid_t)-1 if
134
int net_getunixcred(int fd, struct net_unix_cred *cred_r);
136
/* Returns ip_addr as string, or NULL if ip is invalid. */
137
const char *net_ip2addr(const struct ip_addr *ip);
138
/* char* -> struct ip_addr translation. */
139
int net_addr2ip(const char *addr, struct ip_addr *ip);
140
/* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if
141
successful, -1 if the source address isn't IPv6 mapped IPv4 address. */
142
int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src,
143
struct ip_addr *dest);
145
/* Get socket error */
146
int net_geterror(int fd);
148
/* Get name of TCP service */
149
const char *net_getservbyport(unsigned short port) ATTR_CONST;
151
bool is_ipv4_address(const char *addr) ATTR_PURE;
152
bool is_ipv6_address(const char *addr) ATTR_PURE;
154
/* Parse network as ip/bits. Returns 0 if successful, -1 if invalid input. */
155
int net_parse_range(const char *network, struct ip_addr *ip_r,
156
unsigned int *bits_r);
157
/* Returns TRUE if ip is in net_ip/bits network. IPv6 mapped IPv4 addresses
158
are converted to plain IPv4 addresses before matching. Invalid IPs
159
(family=0) never match anything. */
160
bool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip,
161
unsigned int bits) ATTR_PURE;