Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 11879:4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 21 Jul 2010 00:15:34 +0100 |
parents | 5ea9394b5ced |
children | 10c4c9d5fb5b |
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__ |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
138 static int |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
139 net_connect_ip_full_freebsd(const struct ip_addr *ip, unsigned int port, |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
140 const struct ip_addr *my_ip, bool blocking); |
6876
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
141 |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
142 static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port, |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
143 const struct ip_addr *my_ip, bool blocking) |
6876
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
144 { |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
145 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
|
146 |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
147 for (try = 0;;) { |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
148 fd = net_connect_ip_full_freebsd(ip, port, my_ip, blocking); |
6876
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
149 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
|
150 (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
|
151 break; |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
152 /* |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
153 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
|
154 |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
155 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
|
156 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
|
157 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
|
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 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
160 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
|
161 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
162 /* then some kludging: */ |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
163 #define net_connect_ip_full net_connect_ip_full_freebsd |
6876
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 |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
166 static int net_connect_ip_full(const struct ip_addr *ip, unsigned int port, |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
167 const struct ip_addr *my_ip, bool blocking) |
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)); | |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
190 if (!blocking) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
191 net_set_nonblock(fd, TRUE); |
0 | 192 |
193 /* set our own address */ | |
194 if (my_ip != NULL) { | |
195 sin_set_ip(&so, my_ip); | |
196 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -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
|
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 } | |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
220 #ifdef __FreeBSD__ |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
221 # undef net_connect_ip_full |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
222 #endif |
0 | 223 |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
224 int net_connect_ip(const struct ip_addr *ip, unsigned int port, |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
225 const struct ip_addr *my_ip) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
226 { |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
227 return net_connect_ip_full(ip, port, my_ip, FALSE); |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
228 } |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
229 |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
230 int net_connect_ip_blocking(const struct ip_addr *ip, unsigned int port, |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
231 const struct ip_addr *my_ip) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
232 { |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
233 return net_connect_ip_full(ip, port, my_ip, TRUE); |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
234 } |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
235 |
11340 | 236 int net_try_bind(const struct ip_addr *ip) |
237 { | |
238 union sockaddr_union so; | |
239 int fd; | |
240 | |
241 /* create the socket */ | |
242 memset(&so, 0, sizeof(so)); | |
243 so.sin.sin_family = ip->family; | |
244 fd = socket(ip->family, SOCK_STREAM, 0); | |
245 if (fd == -1) { | |
246 i_error("socket() failed: %m"); | |
247 return -1; | |
248 } | |
249 | |
250 sin_set_ip(&so, ip); | |
251 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) { | |
252 close_keep_errno(fd); | |
253 return -1; | |
254 } | |
255 (void)close(fd); | |
256 return 0; | |
257 } | |
258 | |
0 | 259 int net_connect_unix(const char *path) |
260 { | |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
261 union { |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
262 struct sockaddr sa; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
263 struct sockaddr_un un; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
264 } sa; |
0 | 265 int fd, ret; |
266 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
267 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
268 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
269 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 270 /* too long path */ |
271 errno = EINVAL; | |
272 return -1; | |
273 } | |
274 | |
275 /* create the socket */ | |
276 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
|
277 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
|
278 i_error("socket(%s) failed: %m", path); |
0 | 279 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
|
280 } |
0 | 281 |
3076
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
282 net_set_nonblock(fd, TRUE); |
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
283 |
0 | 284 /* connect */ |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
285 ret = connect(fd, &sa.sa, sizeof(sa)); |
0 | 286 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
|
287 close_keep_errno(fd); |
0 | 288 return -1; |
289 } | |
290 | |
291 return fd; | |
292 } | |
293 | |
10158
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
294 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
|
295 { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
296 struct timeval start, now; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
297 int fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
298 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
299 if (gettimeofday(&start, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
300 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
301 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
302 do { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
303 fd = net_connect_unix(path); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
304 if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED)) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
305 break; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
306 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
307 /* busy. wait for a while. */ |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
308 usleep(((rand() % 10) + 1) * 10000); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
309 if (gettimeofday(&now, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
310 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
311 } while (timeval_diff_msecs(&now, &start) < (int)msecs); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
312 return fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
313 } |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
314 |
0 | 315 void net_disconnect(int fd) |
316 { | |
11564
c24ee1ebb159
net_disconnect(): Ignore ECONNRESET error.
Timo Sirainen <tss@iki.fi>
parents:
11448
diff
changeset
|
317 /* FreeBSD's close() fails with ECONNRESET if socket still has unsent |
c24ee1ebb159
net_disconnect(): Ignore ECONNRESET error.
Timo Sirainen <tss@iki.fi>
parents:
11448
diff
changeset
|
318 data in transmit buffer. We don't care. */ |
c24ee1ebb159
net_disconnect(): Ignore ECONNRESET error.
Timo Sirainen <tss@iki.fi>
parents:
11448
diff
changeset
|
319 if (close(fd) < 0 && errno != ECONNRESET) |
388
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
320 i_error("net_disconnect() failed: %m"); |
0 | 321 } |
322 | |
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
|
323 void net_set_nonblock(int fd, bool nonblock) |
0 | 324 { |
3621
3ae2df67459c
Added fd_set_nonblock() and changed net_set_nonblock() to use it.
Timo Sirainen <tss@iki.fi>
parents:
3577
diff
changeset
|
325 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
|
326 i_fatal("fd_set_nonblock(%d) failed: %m", fd); |
0 | 327 } |
328 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5985
diff
changeset
|
329 int net_set_cork(int fd ATTR_UNUSED, bool cork ATTR_UNUSED) |
0 | 330 { |
331 #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
|
332 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
|
333 |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
334 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
|
335 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
336 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
337 return -1; |
0 | 338 #endif |
339 } | |
340 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
341 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
|
342 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
343 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
|
344 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
|
345 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
346 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
347 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
|
348 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
349 #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
|
350 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
|
351 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
|
352 #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
|
353 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
|
354 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
355 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
356 |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
357 int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog) |
0 | 358 { |
359 union sockaddr_union so; | |
360 int ret, fd, opt = 1; | |
361 socklen_t len; | |
362 | |
363 memset(&so, 0, sizeof(so)); | |
364 sin_set_port(&so, *port); | |
365 sin_set_ip(&so, my_ip); | |
366 | |
367 /* create the socket */ | |
368 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
369 #ifdef HAVE_IPV6 | |
370 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) { | |
371 /* IPv6 is not supported by OS */ | |
372 so.sin.sin_family = AF_INET; | |
373 so.sin.sin_addr.s_addr = INADDR_ANY; | |
374 | |
375 fd = socket(AF_INET, SOCK_STREAM, 0); | |
376 } | |
377 #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
|
378 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
|
379 i_error("socket() failed: %m"); |
0 | 380 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
|
381 } |
0 | 382 |
383 /* set socket options */ | |
384 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
385 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
386 | |
7274
da971cec0395
[::] listens only for IPv6 addresses now. This gets rid of IPv4-mapped IPv6
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
387 /* 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
|
388 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
|
389 #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
|
390 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
|
391 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
|
392 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
|
393 } |
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
|
394 #endif |
0 | 395 /* specify the address/port we want to listen in */ |
396 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
|
397 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
|
398 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
|
399 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
|
400 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
|
401 } |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
402 } else { |
0 | 403 /* get the actual port we started listen */ |
404 len = SIZEOF_SOCKADDR(so); | |
405 ret = getsockname(fd, &so.sa, &len); | |
406 if (ret >= 0) { | |
407 *port = sin_get_port(&so); | |
408 | |
409 /* start listening */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
410 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
|
411 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
|
412 |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
413 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
|
414 i_error("listen() failed: %m"); |
0 | 415 } |
416 } | |
417 | |
418 /* 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
|
419 close_keep_errno(fd); |
0 | 420 return -1; |
421 } | |
422 | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
423 int net_listen_unix(const char *path, int backlog) |
0 | 424 { |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
425 union { |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
426 struct sockaddr sa; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
427 struct sockaddr_un un; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
428 } sa; |
0 | 429 int fd; |
430 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
431 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
432 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
433 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 434 /* too long path */ |
435 errno = EINVAL; | |
436 return -1; | |
437 } | |
438 | |
439 /* create the socket */ | |
440 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
|
441 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
|
442 i_error("socket() failed: %m"); |
0 | 443 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
|
444 } |
0 | 445 |
446 /* bind */ | |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
447 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
|
448 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
449 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
|
450 } else { |
0 | 451 /* start listening */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
452 if (listen(fd, backlog) == 0) |
0 | 453 return fd; |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
454 |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
455 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
456 i_error("listen() failed: %m"); |
0 | 457 } |
458 | |
4011
323c09be5f00
Moved dupliated close_save_errno() code to public close_keep_errno() function.
Timo Sirainen <timo.sirainen@movial.fi>
parents:
3863
diff
changeset
|
459 close_keep_errno(fd); |
0 | 460 return -1; |
461 } | |
462 | |
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
|
463 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
|
464 { |
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
|
465 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
|
466 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
|
467 |
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
|
468 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
|
469 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
|
470 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
|
471 |
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
|
472 /* 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
|
473 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
|
474 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
|
475 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
|
476 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
|
477 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
|
478 } |
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
|
479 |
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
|
480 /* 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
|
481 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
|
482 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
|
483 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
|
484 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
|
485 } |
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
|
486 } |
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
|
487 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
|
488 } |
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
|
489 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
490 int net_accept(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 491 { |
492 union sockaddr_union so; | |
493 int ret; | |
494 socklen_t addrlen; | |
495 | |
496 i_assert(fd >= 0); | |
497 | |
498 addrlen = sizeof(so); | |
499 ret = accept(fd, &so.sa, &addrlen); | |
500 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
501 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
|
502 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
|
503 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
|
504 else |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
505 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
506 } |
0 | 507 |
508 if (addr != NULL) sin_get_ip(&so, addr); | |
509 if (port != NULL) *port = sin_get_port(&so); | |
510 | |
511 return ret; | |
512 } | |
513 | |
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
|
514 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 515 { |
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
|
516 ssize_t ret; |
0 | 517 |
518 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
|
519 i_assert(len <= SSIZE_T_MAX); |
0 | 520 |
3681
771cd6776d4b
Use read() instead of recv() in net_receive(), so it works even with
Timo Sirainen <tss@iki.fi>
parents:
3621
diff
changeset
|
521 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
|
522 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
|
523 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
524 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
525 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
526 } |
0 | 527 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
528 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
|
529 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
|
530 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
531 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
532 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
|
533 /* 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
|
534 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
535 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
536 } |
0 | 537 |
538 return ret; | |
539 } | |
540 | |
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
|
541 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 542 { |
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
|
543 ssize_t ret; |
0 | 544 |
545 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
|
546 i_assert(len <= SSIZE_T_MAX); |
0 | 547 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
548 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
|
549 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
|
550 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
551 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
552 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
|
553 return -2; |
0 | 554 |
555 return ret; | |
556 } | |
557 | |
4745
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4699
diff
changeset
|
558 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
|
559 unsigned int *ips_count) |
0 | 560 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
561 /* @UNSAFE */ |
0 | 562 #ifdef HAVE_IPV6 |
563 union sockaddr_union *so; | |
564 struct addrinfo hints, *ai, *origai; | |
565 int host_error; | |
566 #else | |
567 struct hostent *hp; | |
568 #endif | |
569 int count; | |
570 | |
571 *ips = NULL; | |
572 *ips_count = 0; | |
573 | |
574 #ifdef HAVE_IPV6 | |
575 memset(&hints, 0, sizeof(struct addrinfo)); | |
576 hints.ai_socktype = SOCK_STREAM; | |
577 | |
578 /* save error to host_error for later use */ | |
579 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
580 if (host_error != 0) | |
581 return host_error; | |
582 | |
583 /* get number of IPs */ | |
584 origai = ai; | |
585 for (count = 0; ai != NULL; ai = ai->ai_next) | |
586 count++; | |
587 | |
588 *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
|
589 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 590 |
591 count = 0; | |
592 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
593 so = (union sockaddr_union *) ai->ai_addr; | |
594 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
595 sin_get_ip(so, &(*ips)[count]); |
0 | 596 } |
597 freeaddrinfo(origai); | |
598 #else | |
599 hp = gethostbyname(addr); | |
600 if (hp == NULL) | |
601 return h_errno; | |
602 | |
603 /* get number of IPs */ | |
604 count = 0; | |
605 while (hp->h_addr_list[count] != NULL) | |
606 count++; | |
607 | |
608 *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
|
609 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 610 |
611 while (count > 0) { | |
612 count--; | |
613 | |
614 (*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
|
615 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
|
616 sizeof((*ips)[count].u.ip4)); |
0 | 617 } |
618 #endif | |
619 | |
620 return 0; | |
621 } | |
622 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
623 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 624 { |
625 union sockaddr_union so; | |
626 socklen_t addrlen; | |
627 | |
628 i_assert(fd >= 0); | |
629 | |
630 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
|
631 if (getsockname(fd, &so.sa, &addrlen) == -1) |
0 | 632 return -1; |
633 | |
634 if (addr != NULL) sin_get_ip(&so, addr); | |
635 if (port != NULL) *port = sin_get_port(&so); | |
636 | |
637 return 0; | |
638 } | |
639 | |
2098 | 640 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port) |
641 { | |
642 union sockaddr_union so; | |
643 socklen_t addrlen; | |
644 | |
645 i_assert(fd >= 0); | |
646 | |
647 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
|
648 if (getpeername(fd, &so.sa, &addrlen) == -1) |
2098 | 649 return -1; |
650 | |
651 if (addr != NULL) sin_get_ip(&so, addr); | |
652 if (port != NULL) *port = sin_get_port(&so); | |
653 | |
654 return 0; | |
655 } | |
656 | |
9843 | 657 int net_getunixname(int fd, const char **name_r) |
658 { | |
659 struct sockaddr_un sa; | |
660 socklen_t addrlen = sizeof(sa); | |
661 | |
662 if (getsockname(fd, (void *)&sa, &addrlen) < 0) | |
663 return -1; | |
664 if (sa.sun_family != AF_UNIX) { | |
665 errno = ENOTSOCK; | |
666 return -1; | |
667 } | |
668 *name_r = t_strdup(sa.sun_path); | |
669 return 0; | |
670 } | |
671 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
672 const char *net_ip2addr(const struct ip_addr *ip) |
0 | 673 { |
674 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
675 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
|
676 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
677 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
|
678 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
|
679 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
680 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
681 return t_strdup(addr); |
0 | 682 #else |
683 unsigned long ip4; | |
684 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
685 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
|
686 return NULL; |
0 | 687 |
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
|
688 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
|
689 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
|
690 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
691 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
692 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
693 (ip4 & 0x000000ff)); |
0 | 694 #endif |
695 } | |
696 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
697 int net_addr2ip(const char *addr, struct ip_addr *ip) |
0 | 698 { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
699 int ret; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
700 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
701 if (strchr(addr, ':') != NULL) { |
0 | 702 /* IPv6 */ |
703 ip->family = AF_INET6; | |
704 #ifdef HAVE_IPV6 | |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
705 T_BEGIN { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
706 if (addr[0] == '[') { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
707 /* allow [ipv6 addr] */ |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
708 unsigned int len = strlen(addr); |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
709 if (addr[len-1] == ']') |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
710 addr = t_strndup(addr+1, len-2); |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
711 } |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
712 ret = inet_pton(AF_INET6, addr, &ip->u.ip6); |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
713 } T_END; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
714 if (ret == 0) |
0 | 715 return -1; |
716 #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
|
717 ip->u.ip4.s_addr = 0; |
0 | 718 #endif |
719 } else { | |
720 /* IPv4 */ | |
721 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
|
722 if (inet_aton(addr, &ip->u.ip4) == 0) |
0 | 723 return -1; |
724 } | |
725 | |
726 return 0; | |
727 } | |
728 | |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
729 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
|
730 struct ip_addr *dest) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
731 { |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
732 #ifdef HAVE_IPV6 |
4699
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
733 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
|
734 { 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
|
735 |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
736 if (!IPADDR_IS_V6(src)) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
737 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
|
738 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
|
739 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
740 |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
741 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
|
742 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
|
743 return 0; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
744 #else |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
745 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
746 #endif |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
747 } |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
748 |
0 | 749 int net_geterror(int fd) |
750 { | |
751 int data; | |
752 socklen_t len = sizeof(data); | |
753 | |
754 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
755 return -1; | |
756 | |
757 return data; | |
758 } | |
759 | |
760 const char *net_gethosterror(int error) | |
761 { | |
762 #ifdef HAVE_IPV6 | |
763 i_assert(error != 0); | |
764 | |
765 return gai_strerror(error); | |
766 #else | |
767 switch (error) { | |
768 case HOST_NOT_FOUND: | |
769 return "Host not found"; | |
770 case NO_ADDRESS: | |
771 return "No IP address found for name"; | |
772 case NO_RECOVERY: | |
6666 | 773 return "A non-recoverable name server error occurred"; |
0 | 774 case TRY_AGAIN: |
775 return "A temporary error on an authoritative name server"; | |
776 } | |
777 | |
778 /* unknown error */ | |
779 return NULL; | |
780 #endif | |
781 } | |
782 | |
783 int net_hosterror_notfound(int error) | |
784 { | |
785 #ifdef HAVE_IPV6 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
786 #ifdef EAI_NODATA /* NODATA is depricated */ |
0 | 787 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); |
788 #else | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
789 return error != 1 && (error == EAI_NONAME); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
790 #endif |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
791 #else |
0 | 792 return error == HOST_NOT_FOUND || error == NO_ADDRESS; |
793 #endif | |
794 } | |
795 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
796 const char *net_getservbyport(unsigned short port) |
0 | 797 { |
798 struct servent *entry; | |
799 | |
349 | 800 entry = getservbyport(htons(port), "tcp"); |
0 | 801 return entry == NULL ? NULL : entry->s_name; |
802 } | |
803 | |
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
|
804 bool is_ipv4_address(const char *addr) |
0 | 805 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
806 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
807 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
|
808 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
809 addr++; |
0 | 810 } |
811 | |
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
|
812 return TRUE; |
0 | 813 } |
814 | |
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
|
815 bool is_ipv6_address(const char *addr) |
0 | 816 { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
817 bool have_prefix = FALSE; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
818 |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
819 if (*addr == '[') { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
820 have_prefix = TRUE; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
821 addr++; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
822 } |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
823 while (*addr != '\0') { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
824 if (*addr != ':' && !i_isxdigit(*addr)) { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
825 if (have_prefix && *addr == ']' && addr[1] == '\0') |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
826 break; |
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
|
827 return FALSE; |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
828 } |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
829 addr++; |
0 | 830 } |
831 | |
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
|
832 return TRUE; |
0 | 833 } |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
834 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
835 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
|
836 unsigned int *bits_r) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
837 { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
838 const char *p; |
11086
260e190306b0
Started using str_to_*() functions instead of libc's ones.
Timo Sirainen <tss@iki.fi>
parents:
10789
diff
changeset
|
839 unsigned int bits, max_bits; |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
840 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
841 p = strchr(network, '/'); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
842 if (p != NULL) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
843 network = t_strdup_until(network, p++); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
844 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
845 if (net_addr2ip(network, ip_r) < 0) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
846 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
847 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
848 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
|
849 if (p == NULL) { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
850 /* full IP address must match */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
851 bits = max_bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
852 } else { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
853 /* 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
|
854 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
|
855 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
856 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
857 *bits_r = bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
858 return 0; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
859 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
860 |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
861 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
|
862 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
|
863 { |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
864 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
|
865 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
|
866 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
|
867 unsigned int pos, i; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
868 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
869 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
|
870 /* IPv4 address mapped disguised as IPv6 address */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
871 ip = &tmp_ip; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
872 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
873 |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
874 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
|
875 /* 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
|
876 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
877 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
878 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
|
879 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
880 if (IPADDR_IS_V4(ip)) { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
881 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
|
882 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
|
883 } else { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
884 #ifdef HAVE_IPV6 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
885 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
|
886 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
|
887 #else |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
888 /* shouldn't get here */ |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
889 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
890 #endif |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
891 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
892 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
893 /* 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
|
894 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
|
895 if (ip1[i] != ip2[i]) |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
896 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
897 } |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
898 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
|
899 i2 = htonl(ip2[i]); |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
900 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
901 /* 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
|
902 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
|
903 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
|
904 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
905 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
906 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
907 /* 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
|
908 bits -= pos; |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
909 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
|
910 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
|
911 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
912 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
913 return TRUE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
914 } |