Mercurial > dovecot > core-2.2
annotate src/lib/net.h @ 22955:812e5c961328
fts: Indexing virtual mailbox didn't always index the last mails
author | Timo Sirainen <timo.sirainen@dovecot.fi> |
---|---|
date | Thu, 03 May 2018 18:33:00 +0300 |
parents | 3f83d38ba0b3 |
children |
rev | line source |
---|---|
15187
02451e967a06
Renamed network.[ch] to net.[ch].
Timo Sirainen <tss@iki.fi>
parents:
14688
diff
changeset
|
1 #ifndef NET_H |
02451e967a06
Renamed network.[ch] to net.[ch].
Timo Sirainen <tss@iki.fi>
parents:
14688
diff
changeset
|
2 #define NET_H |
0 | 3 |
4 #ifndef WIN32 | |
5 # include <sys/socket.h> | |
6 # include <netinet/in.h> | |
7 # include <netdb.h> | |
8 # include <arpa/inet.h> | |
9 #endif | |
10 | |
11 #ifdef HAVE_SOCKS_H | |
12 #include <socks.h> | |
13 #endif | |
14 | |
15 #ifndef AF_INET6 | |
16 # ifdef PF_INET6 | |
17 # define AF_INET6 PF_INET6 | |
18 # else | |
19 # define AF_INET6 10 | |
20 # endif | |
21 #endif | |
22 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
23 struct ip_addr { |
0 | 24 unsigned short family; |
5984
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
25 union { |
0 | 26 #ifdef HAVE_IPV6 |
5984
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
27 struct in6_addr ip6; |
0 | 28 #endif |
5984
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
29 struct in_addr ip4; |
74a6130211c2
Changed struct ip_addr to use union for ipv4/ipv6 structs so we don't have
Timo Sirainen <tss@iki.fi>
parents:
5865
diff
changeset
|
30 } u; |
0 | 31 }; |
11503
b7fadc4eac7a
config: local_name foo {} is now for TLS SNI. local foo {} only resolves foo to its IP.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
32 ARRAY_DEFINE_TYPE(ip_addr, struct ip_addr); |
0 | 33 |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
34 struct net_unix_cred { |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
35 uid_t uid; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
36 gid_t gid; |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
13290
diff
changeset
|
37 pid_t pid; |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
38 }; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
39 |
0 | 40 /* maxmimum string length of IP address */ |
41 #ifdef HAVE_IPV6 | |
42 # define MAX_IP_LEN INET6_ADDRSTRLEN | |
43 #else | |
44 # define MAX_IP_LEN 20 | |
45 #endif | |
46 | |
16646
f69e08188622
lib-dns, dns: Don't use IPv4-only error codes
Timo Sirainen <tss@iki.fi>
parents:
16575
diff
changeset
|
47 #ifndef HAVE_IPV6 |
16652
556475a5c1be
dns: Use EAI_NONAME, not EAI_ADDRFAMILY to indicate "no addresses".
Timo Sirainen <tss@iki.fi>
parents:
16646
diff
changeset
|
48 # undef EAI_NONAME |
556475a5c1be
dns: Use EAI_NONAME, not EAI_ADDRFAMILY to indicate "no addresses".
Timo Sirainen <tss@iki.fi>
parents:
16646
diff
changeset
|
49 # define EAI_NONAME NO_ADDRESS |
16646
f69e08188622
lib-dns, dns: Don't use IPv4-only error codes
Timo Sirainen <tss@iki.fi>
parents:
16575
diff
changeset
|
50 # undef EAI_FAIL |
f69e08188622
lib-dns, dns: Don't use IPv4-only error codes
Timo Sirainen <tss@iki.fi>
parents:
16575
diff
changeset
|
51 # define EAI_FAIL NO_RECOVERY |
f69e08188622
lib-dns, dns: Don't use IPv4-only error codes
Timo Sirainen <tss@iki.fi>
parents:
16575
diff
changeset
|
52 #endif |
f69e08188622
lib-dns, dns: Don't use IPv4-only error codes
Timo Sirainen <tss@iki.fi>
parents:
16575
diff
changeset
|
53 |
0 | 54 #define IPADDR_IS_V4(ip) ((ip)->family == AF_INET) |
55 #define IPADDR_IS_V6(ip) ((ip)->family == AF_INET6) | |
13290
d473660bc54d
liblib: Added IPADDR_BITS() macro.
Timo Sirainen <tss@iki.fi>
parents:
12887
diff
changeset
|
56 #define IPADDR_BITS(ip) (IPADDR_IS_V4(ip) ? 32 : 128) |
0 | 57 |
16822
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
58 enum net_listen_flags { |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
59 /* Try to use SO_REUSEPORT if available. If it's not, this flag is |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
60 cleared on return. */ |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
61 NET_LISTEN_FLAG_REUSEPORT = 0x01 |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
62 }; |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
63 |
10789 | 64 /* Returns TRUE if IPs are the same */ |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
65 bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2); |
10789 | 66 /* Returns 0 if IPs are the same, -1 or 1 otherwise. */ |
67 int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2); | |
5865 | 68 unsigned int net_ip_hash(const struct ip_addr *ip); |
0 | 69 |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
16822
diff
changeset
|
70 /* Connect to TCP socket with ip address. The socket and connect() is |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11503
diff
changeset
|
71 non-blocking. */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
72 int net_connect_ip(const struct ip_addr *ip, in_port_t port, |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14233
diff
changeset
|
73 const struct ip_addr *my_ip) ATTR_NULL(3); |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11503
diff
changeset
|
74 /* Like net_connect_ip(), but do a blocking connect(). */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
75 int net_connect_ip_blocking(const struct ip_addr *ip, in_port_t port, |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14233
diff
changeset
|
76 const struct ip_addr *my_ip) ATTR_NULL(3); |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
16822
diff
changeset
|
77 /* Like net_connect_ip(), but open a UDP socket. */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
78 int net_connect_udp(const struct ip_addr *ip, in_port_t port, |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
16822
diff
changeset
|
79 const struct ip_addr *my_ip); |
11340 | 80 /* Returns 0 if we can bind() as given IP, -1 if not. */ |
81 int net_try_bind(const struct ip_addr *ip); | |
0 | 82 /* Connect to named UNIX socket */ |
83 int net_connect_unix(const char *path); | |
10158
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
84 /* Try to connect to UNIX socket for give number of seconds when connect() |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
85 returns EAGAIN or ECONNREFUSED. */ |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
86 int net_connect_unix_with_retries(const char *path, unsigned int msecs); |
0 | 87 /* Disconnect socket */ |
88 void net_disconnect(int fd); | |
89 | |
90 /* Set socket blocking/nonblocking */ | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
91 void net_set_nonblock(int fd, bool nonblock); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
92 /* Set TCP_CORK if supported, ie. don't send out partial frames. |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
93 Returns 0 if ok, -1 if failed. */ |
14688
128c598d2870
Avoid using (void)s by adding ATTR_NOWARN_UNUSED_RESULT attributes and other ways.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
94 int net_set_cork(int fd, bool cork) ATTR_NOWARN_UNUSED_RESULT; |
21041
13a159cfd232
lib: Created net_set_tcp_nodelay(), which enables the TCP_NODELAY socket option.
Stephan Bosch <stephan@dovecot.fi>
parents:
20998
diff
changeset
|
95 /* Set TCP_NODELAY, which disables the Nagle algorithm. */ |
13a159cfd232
lib: Created net_set_tcp_nodelay(), which enables the TCP_NODELAY socket option.
Stephan Bosch <stephan@dovecot.fi>
parents:
20998
diff
changeset
|
96 int net_set_tcp_nodelay(int fd, bool nodelay); |
0 | 97 |
20351
f3e249261abd
lib: Implemented net_set_send_buffer_size() and net_set_recv_buffer_size().
Stephan Bosch <stephan@dovecot.fi>
parents:
19680
diff
changeset
|
98 /* Set socket kernel buffer sizes */ |
f3e249261abd
lib: Implemented net_set_send_buffer_size() and net_set_recv_buffer_size().
Stephan Bosch <stephan@dovecot.fi>
parents:
19680
diff
changeset
|
99 int net_set_send_buffer_size(int fd, size_t size); |
f3e249261abd
lib: Implemented net_set_send_buffer_size() and net_set_recv_buffer_size().
Stephan Bosch <stephan@dovecot.fi>
parents:
19680
diff
changeset
|
100 int net_set_recv_buffer_size(int fd, size_t size); |
f3e249261abd
lib: Implemented net_set_send_buffer_size() and net_set_recv_buffer_size().
Stephan Bosch <stephan@dovecot.fi>
parents:
19680
diff
changeset
|
101 |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
102 /* Set IP to contain INADDR_ANY for IPv4 or IPv6. The IPv6 any address may |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
103 include IPv4 depending on the system (Linux yes, BSD no). */ |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
104 void net_get_ip_any4(struct ip_addr *ip); |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
105 void net_get_ip_any6(struct ip_addr *ip); |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
106 |
0 | 107 /* Listen for connections on a socket */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
108 int net_listen(const struct ip_addr *my_ip, in_port_t *port, int backlog); |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
109 int net_listen_full(const struct ip_addr *my_ip, in_port_t *port, |
16822
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
110 enum net_listen_flags *flags, int backlog); |
0 | 111 /* Listen for connections on an UNIX socket */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2113
diff
changeset
|
112 int net_listen_unix(const char *path, int backlog); |
8219
ec83f6dcb585
Added net_listen_unix_unlink_stale() and use it where needed to avoid code duplication.
Timo Sirainen <tss@iki.fi>
parents:
7919
diff
changeset
|
113 /* Like net_listen_unix(), but if socket already exists, try to connect to it. |
ec83f6dcb585
Added net_listen_unix_unlink_stale() and use it where needed to avoid code duplication.
Timo Sirainen <tss@iki.fi>
parents:
7919
diff
changeset
|
114 If it fails with ECONNREFUSED, unlink the socket and try creating it |
ec83f6dcb585
Added net_listen_unix_unlink_stale() and use it where needed to avoid code duplication.
Timo Sirainen <tss@iki.fi>
parents:
7919
diff
changeset
|
115 again. */ |
ec83f6dcb585
Added net_listen_unix_unlink_stale() and use it where needed to avoid code duplication.
Timo Sirainen <tss@iki.fi>
parents:
7919
diff
changeset
|
116 int net_listen_unix_unlink_stale(const char *path, int backlog); |
5358
a977ad033903
net_listen_unix() API changed a bit. -1 is now returned for errors that can
Timo Sirainen <tss@iki.fi>
parents:
4745
diff
changeset
|
117 /* Accept a connection on a socket. Returns -1 if the connection got closed, |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14233
diff
changeset
|
118 -2 for other failures. For UNIX sockets addr_r->family=port=0. */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
119 int net_accept(int fd, struct ip_addr *addr_r, in_port_t *port_r) |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14233
diff
changeset
|
120 ATTR_NULL(2, 3); |
0 | 121 |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
122 /* Read data from socket, return number of bytes read, |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
123 -1 = error, -2 = disconnected */ |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
124 ssize_t net_receive(int fd, void *buf, size_t len); |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
125 /* Transmit data, return number of bytes sent, -1 = error, -2 = disconnected */ |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
126 ssize_t net_transmit(int fd, const void *data, size_t len); |
0 | 127 |
128 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need | |
129 to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */ | |
4745
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
130 int net_gethostbyname(const char *addr, struct ip_addr **ips, |
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
131 unsigned int *ips_count); |
16575
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
132 /* Return host for the IP address. Returns 0 = ok, others = error code for |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
133 net_gethosterror(). */ |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
134 int net_gethostbyaddr(const struct ip_addr *ip, const char **name_r); |
0 | 135 /* get error of net_gethostname() */ |
7912
81806d402514
Added more consts, ATTR_CONSTs and ATTR_PUREs.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
136 const char *net_gethosterror(int error) ATTR_CONST; |
0 | 137 /* return TRUE if host lookup failed because it didn't exist (ie. not |
138 some error with name server) */ | |
7912
81806d402514
Added more consts, ATTR_CONSTs and ATTR_PUREs.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
139 int net_hosterror_notfound(int error) ATTR_CONST; |
0 | 140 |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
141 /* Get socket local address/port. For UNIX sockets addr->family=port=0. */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
142 int net_getsockname(int fd, struct ip_addr *addr, in_port_t *port) |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14233
diff
changeset
|
143 ATTR_NULL(2, 3); |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
144 /* Get socket remote address/port. For UNIX sockets addr->family=port=0. */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
145 int net_getpeername(int fd, struct ip_addr *addr, in_port_t *port) |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14233
diff
changeset
|
146 ATTR_NULL(2, 3); |
9843 | 147 /* Get UNIX socket name. */ |
148 int net_getunixname(int fd, const char **name_r); | |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
13290
diff
changeset
|
149 /* Get UNIX socket peer process's credentials. The pid may be (pid_t)-1 if |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
13290
diff
changeset
|
150 unavailable. */ |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
151 int net_getunixcred(int fd, struct net_unix_cred *cred_r); |
0 | 152 |
16666
58be84a381e5
net_ip2addr() changed to return "" instead of NULL on failure.
Timo Sirainen <tss@iki.fi>
parents:
16652
diff
changeset
|
153 /* Returns ip_addr as string, or "" if ip isn't valid IPv4 or IPv6 address. */ |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
154 const char *net_ip2addr(const struct ip_addr *ip); |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
155 /* char* -> struct ip_addr translation. */ |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
903
diff
changeset
|
156 int net_addr2ip(const char *addr, struct ip_addr *ip); |
20998
938e5d348eed
lib: Fixed net_str2port*() comments
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20351
diff
changeset
|
157 /* char* -> in_port_t translation */ |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
158 int net_str2port(const char *str, in_port_t *port_r); |
20998
938e5d348eed
lib: Fixed net_str2port*() comments
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
20351
diff
changeset
|
159 /* char* -> in_port_t translation (allows port zero) */ |
19041
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
160 int net_str2port_zero(const char *str, in_port_t *port_r); |
19680
55831f9d2e66
lib: Added net_str2hostport()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19535
diff
changeset
|
161 /* Parse "host", "host:port", "IPv4", "IPv4:port", "IPv6", "[IPv6]" or |
55831f9d2e66
lib: Added net_str2hostport()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19535
diff
changeset
|
162 "[IPv6]:port" to its host and port components. [IPv6] address is returned |
55831f9d2e66
lib: Added net_str2hostport()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19535
diff
changeset
|
163 without []. If no port is given, return default_port. The :port in the |
55831f9d2e66
lib: Added net_str2hostport()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19535
diff
changeset
|
164 parsed string isn't allowed to be zero, but default_port=0 is passed |
55831f9d2e66
lib: Added net_str2hostport()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19535
diff
changeset
|
165 through. */ |
55831f9d2e66
lib: Added net_str2hostport()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19535
diff
changeset
|
166 int net_str2hostport(const char *str, in_port_t default_port, |
55831f9d2e66
lib: Added net_str2hostport()
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19535
diff
changeset
|
167 const char **host_r, in_port_t *port_r); |
21097 | 168 /* Converts ip and port to ipv4:port or [ipv6]:port. Returns -1 if |
169 ip is not valid IPv4 or IPv6 address. */ | |
170 int net_ipport2str(const struct ip_addr *ip, in_port_t port, const char **str_r); | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
171 |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
172 /* Convert IPv6 mapped IPv4 address to an actual IPv4 address. Returns 0 if |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
173 successful, -1 if the source address isn't IPv6 mapped IPv4 address. */ |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
174 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src, |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
3863
diff
changeset
|
175 struct ip_addr *dest); |
0 | 176 |
177 /* Get socket error */ | |
178 int net_geterror(int fd); | |
179 | |
180 /* Get name of TCP service */ | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
17883
diff
changeset
|
181 const char *net_getservbyport(in_port_t port) ATTR_CONST; |
0 | 182 |
7912
81806d402514
Added more consts, ATTR_CONSTs and ATTR_PUREs.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
183 bool is_ipv4_address(const char *addr) ATTR_PURE; |
81806d402514
Added more consts, ATTR_CONSTs and ATTR_PUREs.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
184 bool is_ipv6_address(const char *addr) ATTR_PURE; |
0 | 185 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7912
diff
changeset
|
186 /* Parse network as ip/bits. Returns 0 if successful, -1 if invalid input. */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7912
diff
changeset
|
187 int net_parse_range(const char *network, struct ip_addr *ip_r, |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7912
diff
changeset
|
188 unsigned int *bits_r); |
19535
60e922d0b318
lib: Added unit tests for net_is_in_network() about IPv4-mapped IPv6 addresses
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19041
diff
changeset
|
189 /* Returns TRUE if ip is in net_ip/bits network. IPv4-mapped IPv6 addresses |
60e922d0b318
lib: Added unit tests for net_is_in_network() about IPv4-mapped IPv6 addresses
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19041
diff
changeset
|
190 in "ip" parameter are converted to plain IPv4 addresses before matching. |
60e922d0b318
lib: Added unit tests for net_is_in_network() about IPv4-mapped IPv6 addresses
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19041
diff
changeset
|
191 No conversion is done to net_ip though, so using IPv4-mapped IPv6 addresses |
60e922d0b318
lib: Added unit tests for net_is_in_network() about IPv4-mapped IPv6 addresses
Timo Sirainen <timo.sirainen@dovecot.fi>
parents:
19041
diff
changeset
|
192 there will always fail. Invalid IPs (family=0) never match anything. */ |
7912
81806d402514
Added more consts, ATTR_CONSTs and ATTR_PUREs.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
193 bool net_is_in_network(const struct ip_addr *ip, const struct ip_addr *net_ip, |
81806d402514
Added more consts, ATTR_CONSTs and ATTR_PUREs.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
194 unsigned int bits) ATTR_PURE; |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
6410
diff
changeset
|
195 |
0 | 196 #endif |