Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 11086:260e190306b0 HEAD
Started using str_to_*() functions instead of libc's ones.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 07 Apr 2010 01:49:00 +0300 |
parents | a4666cc8cb7b |
children | 40964653930c |
rev | line source |
---|---|
10582
615eef3139c2
Updated copyright notices to include year 2010.
Timo Sirainen <tss@iki.fi>
parents:
10158
diff
changeset
|
1 /* Copyright (c) 1999-2010 Dovecot authors, see the included COPYING file */ |
0 | 2 |
3 #include "lib.h" | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
4 #include "close-keep-errno.h" |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
5 #include "fd-set-nonblock.h" |
10158
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
6 #include "time-util.h" |
0 | 7 #include "network.h" |
8 | |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
9 #include <stdlib.h> |
0 | 10 #include <unistd.h> |
11 #include <fcntl.h> | |
12 #include <ctype.h> | |
13 #include <sys/un.h> | |
14 #include <netinet/tcp.h> | |
15 | |
16 union sockaddr_union { | |
17 struct sockaddr sa; | |
18 struct sockaddr_in sin; | |
19 #ifdef HAVE_IPV6 | |
20 struct sockaddr_in6 sin6; | |
21 #endif | |
22 }; | |
23 | |
24 #ifdef HAVE_IPV6 | |
25 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \ | |
26 sizeof(so.sin6) : sizeof(so.sin)) | |
27 #else | |
28 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin)) | |
29 #endif | |
30 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
31 bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2) |
0 | 32 { |
10789 | 33 return net_ip_cmp(ip1, ip2) == 0; |
34 } | |
35 | |
36 int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2) | |
37 { | |
0 | 38 if (ip1->family != ip2->family) |
10789 | 39 return ip1->family - ip2->family; |
0 | 40 |
41 #ifdef HAVE_IPV6 | |
10789 | 42 if (ip1->family == AF_INET6) |
43 return memcmp(&ip1->u.ip6, &ip2->u.ip6, sizeof(ip1->u.ip6)); | |
0 | 44 #endif |
45 | |
10789 | 46 return memcmp(&ip1->u.ip4, &ip2->u.ip4, sizeof(ip1->u.ip4)); |
0 | 47 } |
48 | |
5865 | 49 unsigned int net_ip_hash(const struct ip_addr *ip) |
50 { | |
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
|
51 const unsigned char *p; |
5865 | 52 unsigned int len, g, h = 0; |
53 | |
54 #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
|
55 if (ip->family == AF_INET6) { |
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
|
56 p = ip->u.ip6.s6_addr; |
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
|
57 len = sizeof(ip->u.ip6); |
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
|
58 } else |
5865 | 59 #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
|
60 { |
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
|
61 return ip->u.ip4.s_addr; |
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
|
62 } |
5865 | 63 |
64 for (; len > 0; len--, p++) { | |
65 h = (h << 4) + *p; | |
66 if ((g = h & 0xf0000000UL)) { | |
67 h = h ^ (g >> 24); | |
68 h = h ^ g; | |
69 } | |
70 } | |
71 | |
72 return h; | |
73 } | |
0 | 74 |
75 /* copy IP to sockaddr */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
76 static inline void |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
77 sin_set_ip(union sockaddr_union *so, const struct ip_addr *ip) |
0 | 78 { |
79 if (ip == NULL) { | |
80 #ifdef HAVE_IPV6 | |
81 so->sin6.sin6_family = AF_INET6; | |
82 so->sin6.sin6_addr = in6addr_any; | |
83 #else | |
84 so->sin.sin_family = AF_INET; | |
85 so->sin.sin_addr.s_addr = INADDR_ANY; | |
86 #endif | |
87 return; | |
88 } | |
89 | |
90 so->sin.sin_family = ip->family; | |
91 #ifdef HAVE_IPV6 | |
92 if (ip->family == AF_INET6) | |
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
|
93 memcpy(&so->sin6.sin6_addr, &ip->u.ip6, sizeof(ip->u.ip6)); |
0 | 94 else |
95 #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
|
96 memcpy(&so->sin.sin_addr, &ip->u.ip4, sizeof(ip->u.ip4)); |
0 | 97 } |
98 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
99 static inline void |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
100 sin_get_ip(const union sockaddr_union *so, struct ip_addr *ip) |
0 | 101 { |
102 ip->family = so->sin.sin_family; | |
103 | |
104 #ifdef HAVE_IPV6 | |
105 if (ip->family == AF_INET6) | |
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
|
106 memcpy(&ip->u.ip6, &so->sin6.sin6_addr, sizeof(ip->u.ip6)); |
0 | 107 else |
108 #endif | |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
109 if (ip->family == AF_INET) |
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
|
110 memcpy(&ip->u.ip4, &so->sin.sin_addr, sizeof(ip->u.ip4)); |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
111 else |
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
|
112 memset(&ip->u, 0, sizeof(ip->u)); |
0 | 113 } |
114 | |
349 | 115 static inline void sin_set_port(union sockaddr_union *so, unsigned int port) |
0 | 116 { |
117 #ifdef HAVE_IPV6 | |
118 if (so->sin.sin_family == AF_INET6) | |
119 so->sin6.sin6_port = htons((unsigned short) port); | |
120 else | |
121 #endif | |
122 so->sin.sin_port = htons((unsigned short) port); | |
123 } | |
124 | |
349 | 125 static inline unsigned int sin_get_port(union sockaddr_union *so) |
0 | 126 { |
127 #ifdef HAVE_IPV6 | |
128 if (so->sin.sin_family == AF_INET6) | |
129 return ntohs(so->sin6.sin6_port); | |
130 #endif | |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
131 if (so->sin.sin_family == AF_INET) |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
132 return ntohs(so->sin.sin_port); |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
133 |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
134 return 0; |
0 | 135 } |
136 | |
6876
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
137 #ifdef __FreeBSD__ |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
138 static int net_connect_ip_freebsd(const struct ip_addr *ip, unsigned int port, |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
139 const struct ip_addr *my_ip); |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
140 |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
141 int net_connect_ip(const struct ip_addr *ip, unsigned int port, |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
142 const struct ip_addr *my_ip) |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
143 { |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
144 int fd, try; |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
145 |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
146 for (try = 0;;) { |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
147 fd = net_connect_ip_freebsd(ip, port, my_ip); |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
148 if (fd != -1 || ++try == 5 || |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
149 (errno != EADDRINUSE && errno != EACCES)) |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
150 break; |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
151 /* |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
152 This may be just a temporary problem: |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
153 |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
154 EADDRINUSE: busy |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
155 EACCES: pf may cause this if another connection used |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
156 the same port recently |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
157 */ |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
158 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
159 return fd; |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
160 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
161 /* then some kludging: */ |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
162 #define net_connect_ip net_connect_ip_freebsd |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
163 static |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
164 #endif |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
165 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
166 int net_connect_ip(const struct ip_addr *ip, unsigned int port, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
167 const struct ip_addr *my_ip) |
0 | 168 { |
169 union sockaddr_union so; | |
170 int fd, ret, opt = 1; | |
171 | |
172 if (my_ip != NULL && ip->family != my_ip->family) { | |
173 i_warning("net_connect_ip(): ip->family != my_ip->family"); | |
174 my_ip = NULL; | |
175 } | |
176 | |
177 /* create the socket */ | |
178 memset(&so, 0, sizeof(so)); | |
179 so.sin.sin_family = ip->family; | |
180 fd = socket(ip->family, SOCK_STREAM, 0); | |
181 | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
182 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
183 i_error("socket() failed: %m"); |
0 | 184 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
185 } |
0 | 186 |
187 /* set socket options */ | |
188 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
189 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
2639
a307ddd22a3a
net_connect() should be nonblocking (currently unused in Dovecot)
Timo Sirainen <tss@iki.fi>
parents:
2113
diff
changeset
|
190 net_set_nonblock(fd, TRUE); |
0 | 191 |
192 /* set our own address */ | |
193 if (my_ip != NULL) { | |
194 sin_set_ip(&so, my_ip); | |
195 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) { | |
196 /* failed, set it back to INADDR_ANY */ | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
197 i_error("bind(%s) failed: %m", net_ip2addr(my_ip)); |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
198 close_keep_errno(fd); |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
199 return -1; |
0 | 200 } |
201 } | |
202 | |
203 /* connect */ | |
204 sin_set_ip(&so, ip); | |
205 sin_set_port(&so, port); | |
206 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
207 | |
208 #ifndef WIN32 | |
209 if (ret < 0 && errno != EINPROGRESS) | |
210 #else | |
211 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
212 #endif | |
213 { | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
214 close_keep_errno(fd); |
0 | 215 return -1; |
216 } | |
217 | |
218 return fd; | |
219 } | |
220 | |
221 int net_connect_unix(const char *path) | |
222 { | |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
223 union { |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
224 struct sockaddr sa; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
225 struct sockaddr_un un; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
226 } sa; |
0 | 227 int fd, ret; |
228 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
229 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
230 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
231 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 232 /* too long path */ |
233 errno = EINVAL; | |
234 return -1; | |
235 } | |
236 | |
237 /* create the socket */ | |
238 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
239 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
240 i_error("socket(%s) failed: %m", path); |
0 | 241 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
242 } |
0 | 243 |
3076
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
244 net_set_nonblock(fd, TRUE); |
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
245 |
0 | 246 /* connect */ |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
247 ret = connect(fd, &sa.sa, sizeof(sa)); |
0 | 248 if (ret < 0 && errno != EINPROGRESS) { |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
249 close_keep_errno(fd); |
0 | 250 return -1; |
251 } | |
252 | |
253 return fd; | |
254 } | |
255 | |
10158
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
256 int net_connect_unix_with_retries(const char *path, unsigned int msecs) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
257 { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
258 struct timeval start, now; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
259 int fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
260 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
261 if (gettimeofday(&start, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
262 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
263 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
264 do { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
265 fd = net_connect_unix(path); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
266 if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED)) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
267 break; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
268 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
269 /* busy. wait for a while. */ |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
270 usleep(((rand() % 10) + 1) * 10000); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
271 if (gettimeofday(&now, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
272 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
273 } while (timeval_diff_msecs(&now, &start) < (int)msecs); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
274 return fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
275 } |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
276 |
0 | 277 void net_disconnect(int fd) |
278 { | |
388
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
279 if (close(fd) < 0) |
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
280 i_error("net_disconnect() failed: %m"); |
0 | 281 } |
282 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
283 void net_set_nonblock(int fd, bool nonblock) |
0 | 284 { |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
285 if (fd_set_nonblock(fd, nonblock) < 0) |
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
286 i_fatal("fd_set_nonblock(%d) failed: %m", fd); |
0 | 287 } |
288 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5985
diff
changeset
|
289 int net_set_cork(int fd ATTR_UNUSED, bool cork ATTR_UNUSED) |
0 | 290 { |
291 #ifdef TCP_CORK | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
292 int val = cork; |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
293 |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
294 return setsockopt(fd, IPPROTO_TCP, TCP_CORK, &val, sizeof(val)); |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
295 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
296 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
297 return -1; |
0 | 298 #endif |
299 } | |
300 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
301 void net_get_ip_any4(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
|
302 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
303 ip->family = AF_INET; |
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
|
304 ip->u.ip4.s_addr = INADDR_ANY; |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
305 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
306 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
307 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
|
308 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
309 #ifdef HAVE_IPV6 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
310 ip->family = AF_INET6; |
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
|
311 ip->u.ip6 = in6addr_any; |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
312 #else |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
313 memset(ip, 0, sizeof(struct ip_addr)); |
780
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
314 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
315 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
316 |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
317 int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog) |
0 | 318 { |
319 union sockaddr_union so; | |
320 int ret, fd, opt = 1; | |
321 socklen_t len; | |
322 | |
323 memset(&so, 0, sizeof(so)); | |
324 sin_set_port(&so, *port); | |
325 sin_set_ip(&so, my_ip); | |
326 | |
327 /* create the socket */ | |
328 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
329 #ifdef HAVE_IPV6 | |
330 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) { | |
331 /* IPv6 is not supported by OS */ | |
332 so.sin.sin_family = AF_INET; | |
333 so.sin.sin_addr.s_addr = INADDR_ANY; | |
334 | |
335 fd = socket(AF_INET, SOCK_STREAM, 0); | |
336 } | |
337 #endif | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
338 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
339 i_error("socket() failed: %m"); |
0 | 340 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
341 } |
0 | 342 |
343 /* set socket options */ | |
344 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
345 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
346 | |
7274
da971cec0395
[::] listens only for IPv6 addresses now. This gets rid of IPv4-mapped IPv6
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
347 /* If using IPv6, bind only to IPv6 if possible. This avoids |
da971cec0395
[::] listens only for IPv6 addresses now. This gets rid of IPv4-mapped IPv6
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
348 ambiguities with IPv4-mapped IPv6 addresses. */ |
3373
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
349 #ifdef IPV6_V6ONLY |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
350 if (so.sin.sin_family == AF_INET6) { |
7274
da971cec0395
[::] listens only for IPv6 addresses now. This gets rid of IPv4-mapped IPv6
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
351 opt = 1; |
3373
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
352 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
353 } |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
354 #endif |
0 | 355 /* specify the address/port we want to listen in */ |
356 ret = bind(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
3537
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
357 if (ret < 0) { |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
358 if (errno != EADDRINUSE) { |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
359 i_error("bind(%s, %u) failed: %m", |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
360 net_ip2addr(my_ip), *port); |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
361 } |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
362 } else { |
0 | 363 /* get the actual port we started listen */ |
364 len = SIZEOF_SOCKADDR(so); | |
365 ret = getsockname(fd, &so.sa, &len); | |
366 if (ret >= 0) { | |
367 *port = sin_get_port(&so); | |
368 | |
369 /* start listening */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
370 if (listen(fd, backlog) >= 0) |
3537
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
371 return fd; |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
372 |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
373 if (errno != EADDRINUSE) |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
374 i_error("listen() failed: %m"); |
0 | 375 } |
376 } | |
377 | |
378 /* error */ | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
379 close_keep_errno(fd); |
0 | 380 return -1; |
381 } | |
382 | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
383 int net_listen_unix(const char *path, int backlog) |
0 | 384 { |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
385 union { |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
386 struct sockaddr sa; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
387 struct sockaddr_un un; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
388 } sa; |
0 | 389 int fd; |
390 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
391 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
392 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
393 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 394 /* too long path */ |
395 errno = EINVAL; | |
396 return -1; | |
397 } | |
398 | |
399 /* create the socket */ | |
400 fd = socket(PF_UNIX, SOCK_STREAM, 0); | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
401 if (fd == -1) { |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
402 i_error("socket() failed: %m"); |
0 | 403 return -1; |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
404 } |
0 | 405 |
406 /* bind */ | |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
407 if (bind(fd, &sa.sa, sizeof(sa)) < 0) { |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
408 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
409 i_error("bind(%s) failed: %m", path); |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
410 } else { |
0 | 411 /* start listening */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
412 if (listen(fd, backlog) == 0) |
0 | 413 return fd; |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
414 |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
415 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
416 i_error("listen() failed: %m"); |
0 | 417 } |
418 | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
419 close_keep_errno(fd); |
0 | 420 return -1; |
421 } | |
422 | |
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
|
423 int net_listen_unix_unlink_stale(const char *path, int backlog) |
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
|
424 { |
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
|
425 unsigned int i = 0; |
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
|
426 int fd; |
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
|
427 |
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
|
428 while ((fd = net_listen_unix(path, backlog)) == -1) { |
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
|
429 if (errno != EADDRINUSE || ++i == 2) |
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
|
430 return -1; |
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
|
431 |
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
|
432 /* see if it really exists */ |
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
|
433 fd = net_connect_unix(path); |
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
|
434 if (fd != -1 || errno != ECONNREFUSED) { |
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
|
435 if (fd != -1) (void)close(fd); |
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
|
436 errno = EADDRINUSE; |
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
|
437 return -1; |
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
|
438 } |
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
|
439 |
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
|
440 /* delete and try 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
|
441 if (unlink(path) < 0 && errno != ENOENT) { |
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
|
442 i_error("unlink(%s) failed: %m", path); |
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
|
443 errno = EADDRINUSE; |
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
|
444 return -1; |
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
|
445 } |
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
|
446 } |
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
|
447 return fd; |
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
|
448 } |
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
|
449 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
450 int net_accept(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 451 { |
452 union sockaddr_union so; | |
453 int ret; | |
454 socklen_t addrlen; | |
455 | |
456 i_assert(fd >= 0); | |
457 | |
458 addrlen = sizeof(so); | |
459 ret = accept(fd, &so.sa, &addrlen); | |
460 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
461 if (ret < 0) { |
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
|
462 if (errno == EAGAIN || errno == ECONNABORTED) |
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
|
463 return -1; |
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
|
464 else |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
465 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
466 } |
0 | 467 |
468 if (addr != NULL) sin_get_ip(&so, addr); | |
469 if (port != NULL) *port = sin_get_port(&so); | |
470 | |
471 return ret; | |
472 } | |
473 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
474 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 475 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
476 ssize_t ret; |
0 | 477 |
478 i_assert(fd >= 0); | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
479 i_assert(len <= SSIZE_T_MAX); |
0 | 480 |
3681
771cd6776d4b
Use read() instead of recv() in net_receive(), so it works even with
Timo Sirainen <tss@iki.fi>
parents:
3621
diff
changeset
|
481 ret = read(fd, buf, len); |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
482 if (ret == 0) { |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
483 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
484 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
485 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
486 } |
0 | 487 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
488 if (unlikely(ret < 0)) { |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
489 if (errno == EINTR || errno == EAGAIN) |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
490 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
491 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
492 if (errno == ECONNRESET || errno == ETIMEDOUT) { |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
493 /* treat as disconnection */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
494 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
495 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
496 } |
0 | 497 |
498 return ret; | |
499 } | |
500 | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
501 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 502 { |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
503 ssize_t ret; |
0 | 504 |
505 i_assert(fd >= 0); | |
183
4a7ab9e94f25
size_t fixes for lib/. Changed OFF_T_FORMAT to PRIuOFF_T which is more
Timo Sirainen <tss@iki.fi>
parents:
159
diff
changeset
|
506 i_assert(len <= SSIZE_T_MAX); |
0 | 507 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
508 ret = send(fd, data, len, 0); |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
509 if (unlikely(ret == -1 && (errno == EINTR || errno == EAGAIN))) |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
510 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
511 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
512 if (unlikely(errno == EPIPE)) |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
513 return -2; |
0 | 514 |
515 return ret; | |
516 } | |
517 | |
4745
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4699
diff
changeset
|
518 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:
4699
diff
changeset
|
519 unsigned int *ips_count) |
0 | 520 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
521 /* @UNSAFE */ |
0 | 522 #ifdef HAVE_IPV6 |
523 union sockaddr_union *so; | |
524 struct addrinfo hints, *ai, *origai; | |
525 int host_error; | |
526 #else | |
527 struct hostent *hp; | |
528 #endif | |
529 int count; | |
530 | |
531 *ips = NULL; | |
532 *ips_count = 0; | |
533 | |
534 #ifdef HAVE_IPV6 | |
535 memset(&hints, 0, sizeof(struct addrinfo)); | |
536 hints.ai_socktype = SOCK_STREAM; | |
537 | |
538 /* save error to host_error for later use */ | |
539 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
540 if (host_error != 0) | |
541 return host_error; | |
542 | |
543 /* get number of IPs */ | |
544 origai = ai; | |
545 for (count = 0; ai != NULL; ai = ai->ai_next) | |
546 count++; | |
547 | |
548 *ips_count = count; | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
549 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 550 |
551 count = 0; | |
552 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
553 so = (union sockaddr_union *) ai->ai_addr; | |
554 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
555 sin_get_ip(so, &(*ips)[count]); |
0 | 556 } |
557 freeaddrinfo(origai); | |
558 #else | |
559 hp = gethostbyname(addr); | |
560 if (hp == NULL) | |
561 return h_errno; | |
562 | |
563 /* get number of IPs */ | |
564 count = 0; | |
565 while (hp->h_addr_list[count] != NULL) | |
566 count++; | |
567 | |
568 *ips_count = count; | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
569 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 570 |
571 while (count > 0) { | |
572 count--; | |
573 | |
574 (*ips)[count].family = AF_INET; | |
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
|
575 memcpy(&(*ips)[count].u.ip4, hp->h_addr_list[count], |
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
|
576 sizeof((*ips)[count].u.ip4)); |
0 | 577 } |
578 #endif | |
579 | |
580 return 0; | |
581 } | |
582 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
583 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 584 { |
585 union sockaddr_union so; | |
586 socklen_t addrlen; | |
587 | |
588 i_assert(fd >= 0); | |
589 | |
590 addrlen = sizeof(so); | |
4018
989dd95aa8b9
Enable -Wstrict-aliasing=2 warning and fixed a couple of warnings it gave.
Timo Sirainen <tss@iki.fi>
parents:
4011
diff
changeset
|
591 if (getsockname(fd, &so.sa, &addrlen) == -1) |
0 | 592 return -1; |
593 | |
594 if (addr != NULL) sin_get_ip(&so, addr); | |
595 if (port != NULL) *port = sin_get_port(&so); | |
596 | |
597 return 0; | |
598 } | |
599 | |
2098 | 600 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port) |
601 { | |
602 union sockaddr_union so; | |
603 socklen_t addrlen; | |
604 | |
605 i_assert(fd >= 0); | |
606 | |
607 addrlen = sizeof(so); | |
4018
989dd95aa8b9
Enable -Wstrict-aliasing=2 warning and fixed a couple of warnings it gave.
Timo Sirainen <tss@iki.fi>
parents:
4011
diff
changeset
|
608 if (getpeername(fd, &so.sa, &addrlen) == -1) |
2098 | 609 return -1; |
610 | |
611 if (addr != NULL) sin_get_ip(&so, addr); | |
612 if (port != NULL) *port = sin_get_port(&so); | |
613 | |
614 return 0; | |
615 } | |
616 | |
9843 | 617 int net_getunixname(int fd, const char **name_r) |
618 { | |
619 struct sockaddr_un sa; | |
620 socklen_t addrlen = sizeof(sa); | |
621 | |
622 if (getsockname(fd, (void *)&sa, &addrlen) < 0) | |
623 return -1; | |
624 if (sa.sun_family != AF_UNIX) { | |
625 errno = ENOTSOCK; | |
626 return -1; | |
627 } | |
628 *name_r = t_strdup(sa.sun_path); | |
629 return 0; | |
630 } | |
631 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
632 const char *net_ip2addr(const struct ip_addr *ip) |
0 | 633 { |
634 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
635 char addr[MAX_IP_LEN+1]; |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
636 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
637 addr[MAX_IP_LEN] = '\0'; |
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
|
638 if (inet_ntop(ip->family, &ip->u.ip6, addr, MAX_IP_LEN) == NULL) |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
639 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
640 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
641 return t_strdup(addr); |
0 | 642 #else |
643 unsigned long ip4; | |
644 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
645 if (ip->family != AF_INET) |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
646 return NULL; |
0 | 647 |
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
|
648 ip4 = ntohl(ip->u.ip4.s_addr); |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
649 return t_strdup_printf("%lu.%lu.%lu.%lu", |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
650 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
651 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
652 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
653 (ip4 & 0x000000ff)); |
0 | 654 #endif |
655 } | |
656 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
657 int net_addr2ip(const char *addr, struct ip_addr *ip) |
0 | 658 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
659 if (strchr(addr, ':') != NULL) { |
0 | 660 /* IPv6 */ |
661 ip->family = AF_INET6; | |
662 #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
|
663 if (inet_pton(AF_INET6, addr, &ip->u.ip6) == 0) |
0 | 664 return -1; |
665 #else | |
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
|
666 ip->u.ip4.s_addr = 0; |
0 | 667 #endif |
668 } else { | |
669 /* IPv4 */ | |
670 ip->family = AF_INET; | |
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
|
671 if (inet_aton(addr, &ip->u.ip4) == 0) |
0 | 672 return -1; |
673 } | |
674 | |
675 return 0; | |
676 } | |
677 | |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
678 int net_ipv6_mapped_ipv4_convert(const struct ip_addr *src, |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
679 struct ip_addr *dest) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
680 { |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
681 #ifdef HAVE_IPV6 |
4699
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
682 static uint8_t v4_prefix[] = |
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
683 { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff }; |
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
684 |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
685 if (!IPADDR_IS_V6(src)) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
686 return -1; |
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
|
687 if (memcmp(src->u.ip6.s6_addr, v4_prefix, sizeof(v4_prefix)) != 0) |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
688 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
689 |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
690 dest->family = AF_INET; |
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
|
691 memcpy(&dest->u.ip6, &src->u.ip6.s6_addr[3*4], 4); |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
692 return 0; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
693 #else |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
694 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
695 #endif |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
696 } |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
697 |
0 | 698 int net_geterror(int fd) |
699 { | |
700 int data; | |
701 socklen_t len = sizeof(data); | |
702 | |
703 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
704 return -1; | |
705 | |
706 return data; | |
707 } | |
708 | |
709 const char *net_gethosterror(int error) | |
710 { | |
711 #ifdef HAVE_IPV6 | |
712 i_assert(error != 0); | |
713 | |
714 return gai_strerror(error); | |
715 #else | |
716 switch (error) { | |
717 case HOST_NOT_FOUND: | |
718 return "Host not found"; | |
719 case NO_ADDRESS: | |
720 return "No IP address found for name"; | |
721 case NO_RECOVERY: | |
6666 | 722 return "A non-recoverable name server error occurred"; |
0 | 723 case TRY_AGAIN: |
724 return "A temporary error on an authoritative name server"; | |
725 } | |
726 | |
727 /* unknown error */ | |
728 return NULL; | |
729 #endif | |
730 } | |
731 | |
732 int net_hosterror_notfound(int error) | |
733 { | |
734 #ifdef HAVE_IPV6 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
735 #ifdef EAI_NODATA /* NODATA is depricated */ |
0 | 736 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); |
737 #else | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
738 return error != 1 && (error == EAI_NONAME); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
739 #endif |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
740 #else |
0 | 741 return error == HOST_NOT_FOUND || error == NO_ADDRESS; |
742 #endif | |
743 } | |
744 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
745 const char *net_getservbyport(unsigned short port) |
0 | 746 { |
747 struct servent *entry; | |
748 | |
349 | 749 entry = getservbyport(htons(port), "tcp"); |
0 | 750 return entry == NULL ? NULL : entry->s_name; |
751 } | |
752 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
753 bool is_ipv4_address(const char *addr) |
0 | 754 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
755 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
756 if (*addr != '.' && !i_isdigit(*addr)) |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
757 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
758 addr++; |
0 | 759 } |
760 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
761 return TRUE; |
0 | 762 } |
763 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
764 bool is_ipv6_address(const char *addr) |
0 | 765 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
766 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
767 if (*addr != ':' && !i_isxdigit(*addr)) |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
768 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
769 addr++; |
0 | 770 } |
771 | |
3863
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
772 return TRUE; |
0 | 773 } |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
774 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
775 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:
7520
diff
changeset
|
776 unsigned int *bits_r) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
777 { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
778 const char *p; |
11086
260e190306b0
Started using str_to_*() functions instead of libc's ones.
Timo Sirainen <tss@iki.fi>
parents:
10789
diff
changeset
|
779 unsigned int bits, max_bits; |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
780 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
781 p = strchr(network, '/'); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
782 if (p != NULL) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
783 network = t_strdup_until(network, p++); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
784 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
785 if (net_addr2ip(network, ip_r) < 0) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
786 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
787 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
788 max_bits = IPADDR_IS_V4(ip_r) ? 32 : 128; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
789 if (p == NULL) { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
790 /* full IP address must match */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
791 bits = max_bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
792 } else { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
793 /* get the network mask */ |
11086
260e190306b0
Started using str_to_*() functions instead of libc's ones.
Timo Sirainen <tss@iki.fi>
parents:
10789
diff
changeset
|
794 if (str_to_uint(p, &bits) < 0 || bits > max_bits) |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
795 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
796 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
797 *bits_r = bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
798 return 0; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
799 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
800 |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
801 bool net_is_in_network(const struct ip_addr *ip, |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
802 const struct ip_addr *net_ip, unsigned int bits) |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
803 { |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
804 struct ip_addr tmp_ip; |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
805 const uint32_t *ip1, *ip2; |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
806 uint32_t mask, i1, i2; |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
807 unsigned int pos, i; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
808 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
809 if (net_ipv6_mapped_ipv4_convert(ip, &tmp_ip) == 0) { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
810 /* IPv4 address mapped disguised as IPv6 address */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
811 ip = &tmp_ip; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
812 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
813 |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
814 if (IPADDR_IS_V4(ip) != IPADDR_IS_V4(net_ip)) { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
815 /* one is IPv6 and one is IPv4 */ |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
816 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
817 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
818 i_assert(IPADDR_IS_V6(ip) == IPADDR_IS_V6(net_ip)); |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
819 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
820 if (IPADDR_IS_V4(ip)) { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
821 ip1 = &ip->u.ip4.s_addr; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
822 ip2 = &net_ip->u.ip4.s_addr; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
823 } else { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
824 #ifdef HAVE_IPV6 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
825 ip1 = (const void *)&ip->u.ip6; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
826 ip2 = (const void *)&net_ip->u.ip6; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
827 #else |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
828 /* shouldn't get here */ |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
829 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
830 #endif |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
831 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
832 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
833 /* check first the full 32bit ints */ |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
834 for (pos = 0, i = 0; pos + 32 <= bits; pos += 32, i++) { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
835 if (ip1[i] != ip2[i]) |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
836 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
837 } |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
838 i1 = htonl(ip1[i]); |
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
839 i2 = htonl(ip2[i]); |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
840 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
841 /* check the last full bytes */ |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
842 for (mask = 0xff000000; pos + 8 <= bits; pos += 8, mask >>= 8) { |
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
843 if ((i1 & mask) != (i2 & mask)) |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
844 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
845 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
846 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
847 /* check the last bits, they're reversed in bytes */ |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
848 bits -= pos; |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
849 for (mask = 0x80000000 >> (pos % 32); bits > 0; bits--, mask >>= 1) { |
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
850 if ((i1 & mask) != (i2 & mask)) |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
851 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
852 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
853 return TRUE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
854 } |