Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 14691:3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 28 Jun 2012 00:27:13 +0300 |
parents | 7c058aa05b0a |
children |
rev | line source |
---|---|
14133
ba770cba5598
Updated copyright notices to include year 2012.
Timo Sirainen <tss@iki.fi>
parents:
13557
diff
changeset
|
1 /* Copyright (c) 1999-2012 Dovecot authors, see the included COPYING file */ |
0 | 2 |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
3 #define _GNU_SOURCE /* For Linux's struct ucred */ |
0 | 4 #include "lib.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> | |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
15 #if defined(HAVE_UCRED_H) |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
16 # include <ucred.h> /* for getpeerucred() */ |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
17 #elif defined(HAVE_SYS_UCRED_H) |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
18 # include <sys/ucred.h> /* for FreeBSD struct xucred */ |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
19 #endif |
0 | 20 |
21 union sockaddr_union { | |
22 struct sockaddr sa; | |
23 struct sockaddr_in sin; | |
24 #ifdef HAVE_IPV6 | |
25 struct sockaddr_in6 sin6; | |
26 #endif | |
27 }; | |
28 | |
13363 | 29 union sockaddr_union_unix { |
30 struct sockaddr sa; | |
13557 | 31 struct sockaddr_un un; |
13363 | 32 }; |
33 | |
0 | 34 #ifdef HAVE_IPV6 |
35 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \ | |
36 sizeof(so.sin6) : sizeof(so.sin)) | |
37 #else | |
38 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin)) | |
39 #endif | |
40 | |
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
|
41 bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2) |
0 | 42 { |
10789 | 43 return net_ip_cmp(ip1, ip2) == 0; |
44 } | |
45 | |
46 int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2) | |
47 { | |
0 | 48 if (ip1->family != ip2->family) |
10789 | 49 return ip1->family - ip2->family; |
0 | 50 |
51 #ifdef HAVE_IPV6 | |
10789 | 52 if (ip1->family == AF_INET6) |
53 return memcmp(&ip1->u.ip6, &ip2->u.ip6, sizeof(ip1->u.ip6)); | |
0 | 54 #endif |
55 | |
10789 | 56 return memcmp(&ip1->u.ip4, &ip2->u.ip4, sizeof(ip1->u.ip4)); |
0 | 57 } |
58 | |
5865 | 59 unsigned int net_ip_hash(const struct ip_addr *ip) |
60 { | |
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
|
61 const unsigned char *p; |
5865 | 62 unsigned int len, g, h = 0; |
63 | |
64 #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
|
65 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
|
66 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
|
67 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
|
68 } else |
5865 | 69 #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
|
70 { |
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
|
71 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
|
72 } |
5865 | 73 |
74 for (; len > 0; len--, p++) { | |
75 h = (h << 4) + *p; | |
76 if ((g = h & 0xf0000000UL)) { | |
77 h = h ^ (g >> 24); | |
78 h = h ^ g; | |
79 } | |
80 } | |
81 | |
82 return h; | |
83 } | |
0 | 84 |
85 /* 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
|
86 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
|
87 sin_set_ip(union sockaddr_union *so, const struct ip_addr *ip) |
0 | 88 { |
89 if (ip == NULL) { | |
90 #ifdef HAVE_IPV6 | |
91 so->sin6.sin6_family = AF_INET6; | |
92 so->sin6.sin6_addr = in6addr_any; | |
93 #else | |
94 so->sin.sin_family = AF_INET; | |
95 so->sin.sin_addr.s_addr = INADDR_ANY; | |
96 #endif | |
97 return; | |
98 } | |
99 | |
100 so->sin.sin_family = ip->family; | |
101 #ifdef HAVE_IPV6 | |
102 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
|
103 memcpy(&so->sin6.sin6_addr, &ip->u.ip6, sizeof(ip->u.ip6)); |
0 | 104 else |
105 #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
|
106 memcpy(&so->sin.sin_addr, &ip->u.ip4, sizeof(ip->u.ip4)); |
0 | 107 } |
108 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
109 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
|
110 sin_get_ip(const union sockaddr_union *so, struct ip_addr *ip) |
0 | 111 { |
12138
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
112 /* IP structs may be sent across processes. Clear the whole struct |
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
113 first to make sure it won't leak any data across processes. */ |
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
114 memset(ip, 0, sizeof(*ip)); |
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
115 |
0 | 116 ip->family = so->sin.sin_family; |
117 | |
118 #ifdef HAVE_IPV6 | |
119 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
|
120 memcpy(&ip->u.ip6, &so->sin6.sin6_addr, sizeof(ip->u.ip6)); |
0 | 121 else |
122 #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
|
123 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
|
124 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
|
125 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
|
126 memset(&ip->u, 0, sizeof(ip->u)); |
0 | 127 } |
128 | |
349 | 129 static inline void sin_set_port(union sockaddr_union *so, unsigned int port) |
0 | 130 { |
131 #ifdef HAVE_IPV6 | |
132 if (so->sin.sin_family == AF_INET6) | |
133 so->sin6.sin6_port = htons((unsigned short) port); | |
134 else | |
135 #endif | |
136 so->sin.sin_port = htons((unsigned short) port); | |
137 } | |
138 | |
349 | 139 static inline unsigned int sin_get_port(union sockaddr_union *so) |
0 | 140 { |
141 #ifdef HAVE_IPV6 | |
142 if (so->sin.sin_family == AF_INET6) | |
143 return ntohs(so->sin6.sin6_port); | |
144 #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
|
145 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
|
146 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
|
147 |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
148 return 0; |
0 | 149 } |
150 | |
6876
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
151 #ifdef __FreeBSD__ |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
152 static int |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
153 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
|
154 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
|
155 |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
156 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
|
157 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
|
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 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
|
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 for (try = 0;;) { |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
162 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
|
163 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
|
164 (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
|
165 break; |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
166 /* |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
167 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
|
168 |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
169 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
|
170 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
|
171 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
|
172 */ |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
173 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
174 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
|
175 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
176 /* then some kludging: */ |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
177 #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
|
178 #endif |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
179 |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
180 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
|
181 const struct ip_addr *my_ip, bool blocking) |
0 | 182 { |
183 union sockaddr_union so; | |
184 int fd, ret, opt = 1; | |
185 | |
186 if (my_ip != NULL && ip->family != my_ip->family) { | |
187 i_warning("net_connect_ip(): ip->family != my_ip->family"); | |
188 my_ip = NULL; | |
189 } | |
190 | |
191 /* create the socket */ | |
192 memset(&so, 0, sizeof(so)); | |
193 so.sin.sin_family = ip->family; | |
194 fd = socket(ip->family, SOCK_STREAM, 0); | |
195 | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
196 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
|
197 i_error("socket() failed: %m"); |
0 | 198 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
|
199 } |
0 | 200 |
201 /* set socket options */ | |
202 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
203 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
|
204 if (!blocking) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
205 net_set_nonblock(fd, TRUE); |
0 | 206 |
207 /* set our own address */ | |
208 if (my_ip != NULL) { | |
209 sin_set_ip(&so, my_ip); | |
210 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
|
211 i_error("bind(%s) failed: %m", net_ip2addr(my_ip)); |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
212 i_close_fd(&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
|
213 return -1; |
0 | 214 } |
215 } | |
216 | |
217 /* connect */ | |
218 sin_set_ip(&so, ip); | |
219 sin_set_port(&so, port); | |
220 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
221 | |
222 #ifndef WIN32 | |
223 if (ret < 0 && errno != EINPROGRESS) | |
224 #else | |
225 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
226 #endif | |
227 { | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
228 i_close_fd(&fd); |
0 | 229 return -1; |
230 } | |
231 | |
232 return fd; | |
233 } | |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
234 #ifdef __FreeBSD__ |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
235 # undef net_connect_ip_full |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
236 #endif |
0 | 237 |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
238 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
|
239 const struct ip_addr *my_ip) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
240 { |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
241 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
|
242 } |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
243 |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
244 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
|
245 const struct ip_addr *my_ip) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
246 { |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
247 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
|
248 } |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
249 |
11340 | 250 int net_try_bind(const struct ip_addr *ip) |
251 { | |
252 union sockaddr_union so; | |
253 int fd; | |
254 | |
255 /* create the socket */ | |
256 memset(&so, 0, sizeof(so)); | |
257 so.sin.sin_family = ip->family; | |
258 fd = socket(ip->family, SOCK_STREAM, 0); | |
259 if (fd == -1) { | |
260 i_error("socket() failed: %m"); | |
261 return -1; | |
262 } | |
263 | |
264 sin_set_ip(&so, ip); | |
265 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) { | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
266 i_close_fd(&fd); |
11340 | 267 return -1; |
268 } | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
269 i_close_fd(&fd); |
11340 | 270 return 0; |
271 } | |
272 | |
0 | 273 int net_connect_unix(const char *path) |
274 { | |
13557 | 275 union sockaddr_union_unix sa; |
0 | 276 int fd, ret; |
277 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
278 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
279 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
280 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 281 /* too long path */ |
282 errno = EINVAL; | |
283 return -1; | |
284 } | |
285 | |
286 /* create the socket */ | |
287 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
|
288 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
|
289 i_error("socket(%s) failed: %m", path); |
0 | 290 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
|
291 } |
0 | 292 |
3076
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
293 net_set_nonblock(fd, TRUE); |
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
294 |
0 | 295 /* connect */ |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
296 ret = connect(fd, &sa.sa, sizeof(sa)); |
0 | 297 if (ret < 0 && errno != EINPROGRESS) { |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
298 i_close_fd(&fd); |
0 | 299 return -1; |
300 } | |
301 | |
302 return fd; | |
303 } | |
304 | |
10158
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
305 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
|
306 { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
307 struct timeval start, now; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
308 int fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
309 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
310 if (gettimeofday(&start, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
311 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
312 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
313 do { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
314 fd = net_connect_unix(path); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
315 if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED)) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
316 break; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
317 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
318 /* busy. wait for a while. */ |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
319 usleep(((rand() % 10) + 1) * 10000); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
320 if (gettimeofday(&now, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
321 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
322 } while (timeval_diff_msecs(&now, &start) < (int)msecs); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
323 return fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
324 } |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
325 |
0 | 326 void net_disconnect(int fd) |
327 { | |
11564
c24ee1ebb159
net_disconnect(): Ignore ECONNRESET error.
Timo Sirainen <tss@iki.fi>
parents:
11448
diff
changeset
|
328 /* 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
|
329 data in transmit buffer. We don't care. */ |
c24ee1ebb159
net_disconnect(): Ignore ECONNRESET error.
Timo Sirainen <tss@iki.fi>
parents:
11448
diff
changeset
|
330 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
|
331 i_error("net_disconnect() failed: %m"); |
0 | 332 } |
333 | |
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
|
334 void net_set_nonblock(int fd, bool nonblock) |
0 | 335 { |
14634
a65006d95d53
fd_set_nonblock() API changed to i_fatal() on failure.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
336 fd_set_nonblock(fd, nonblock); |
0 | 337 } |
338 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5985
diff
changeset
|
339 int net_set_cork(int fd ATTR_UNUSED, bool cork ATTR_UNUSED) |
0 | 340 { |
341 #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
|
342 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
|
343 |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
344 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
|
345 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
346 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
347 return -1; |
0 | 348 #endif |
349 } | |
350 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
351 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
|
352 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
353 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
|
354 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
|
355 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
356 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
357 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
|
358 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
359 #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
|
360 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
|
361 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
|
362 #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
|
363 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
|
364 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
365 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
366 |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
367 int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog) |
0 | 368 { |
369 union sockaddr_union so; | |
370 int ret, fd, opt = 1; | |
371 socklen_t len; | |
372 | |
373 memset(&so, 0, sizeof(so)); | |
374 sin_set_port(&so, *port); | |
375 sin_set_ip(&so, my_ip); | |
376 | |
377 /* create the socket */ | |
378 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
379 #ifdef HAVE_IPV6 | |
12563
cde0d403c6ff
net_listen(): If IPv6 isn't supported, don't fallback to IPv4 if my_ip is given.
Timo Sirainen <tss@iki.fi>
parents:
12138
diff
changeset
|
380 if (fd == -1 && my_ip == NULL && |
cde0d403c6ff
net_listen(): If IPv6 isn't supported, don't fallback to IPv4 if my_ip is given.
Timo Sirainen <tss@iki.fi>
parents:
12138
diff
changeset
|
381 (errno == EINVAL || errno == EAFNOSUPPORT)) { |
0 | 382 /* IPv6 is not supported by OS */ |
383 so.sin.sin_family = AF_INET; | |
384 so.sin.sin_addr.s_addr = INADDR_ANY; | |
385 | |
386 fd = socket(AF_INET, SOCK_STREAM, 0); | |
387 } | |
388 #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
|
389 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
|
390 i_error("socket() failed: %m"); |
0 | 391 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
|
392 } |
0 | 393 |
394 /* set socket options */ | |
395 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
396 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
397 | |
7274
da971cec0395
[::] listens only for IPv6 addresses now. This gets rid of IPv4-mapped IPv6
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
398 /* 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
|
399 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
|
400 #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
|
401 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
|
402 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
|
403 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
|
404 } |
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
|
405 #endif |
0 | 406 /* specify the address/port we want to listen in */ |
407 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
|
408 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
|
409 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
|
410 i_error("bind(%s, %u) failed: %m", |
14387
cd7e15b2d575
net_listen(): If bind(ip=any) fails with non-EADDRINUSE, don't crash.
Timo Sirainen <tss@iki.fi>
parents:
14377
diff
changeset
|
411 my_ip == NULL ? "" : net_ip2addr(my_ip), *port); |
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
|
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 } else { |
0 | 414 /* get the actual port we started listen */ |
415 len = SIZEOF_SOCKADDR(so); | |
416 ret = getsockname(fd, &so.sa, &len); | |
417 if (ret >= 0) { | |
418 *port = sin_get_port(&so); | |
419 | |
420 /* start listening */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
421 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
|
422 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
|
423 |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
424 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
|
425 i_error("listen() failed: %m"); |
0 | 426 } |
427 } | |
428 | |
429 /* error */ | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
430 i_close_fd(&fd); |
0 | 431 return -1; |
432 } | |
433 | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
434 int net_listen_unix(const char *path, int backlog) |
0 | 435 { |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
436 union { |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
437 struct sockaddr sa; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
438 struct sockaddr_un un; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
439 } sa; |
0 | 440 int fd; |
441 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
442 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
443 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
444 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 445 /* too long path */ |
446 errno = EINVAL; | |
447 return -1; | |
448 } | |
449 | |
450 /* create the socket */ | |
451 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
|
452 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
|
453 i_error("socket() failed: %m"); |
0 | 454 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
|
455 } |
0 | 456 |
457 /* bind */ | |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
458 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
|
459 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
460 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
|
461 } else { |
0 | 462 /* start listening */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
463 if (listen(fd, backlog) == 0) |
0 | 464 return fd; |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
465 |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
466 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
467 i_error("listen() failed: %m"); |
0 | 468 } |
469 | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
470 i_close_fd(&fd); |
0 | 471 return -1; |
472 } | |
473 | |
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
|
474 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
|
475 { |
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 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
|
477 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
|
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 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
|
480 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
|
481 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
|
482 |
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 /* 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
|
484 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
|
485 if (fd != -1 || errno != ECONNREFUSED) { |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
486 if (fd != -1) i_close_fd(&fd); |
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
|
487 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
|
488 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
|
489 } |
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
|
490 |
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
|
491 /* 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
|
492 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
|
493 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
|
494 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
|
495 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
|
496 } |
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
|
497 } |
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
|
498 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
|
499 } |
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
|
500 |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14576
diff
changeset
|
501 int net_accept(int fd, struct ip_addr *addr_r, unsigned int *port_r) |
0 | 502 { |
503 union sockaddr_union so; | |
504 int ret; | |
505 socklen_t addrlen; | |
506 | |
507 i_assert(fd >= 0); | |
508 | |
509 addrlen = sizeof(so); | |
510 ret = accept(fd, &so.sa, &addrlen); | |
511 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
512 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
|
513 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
|
514 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
|
515 else |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
516 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
517 } |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
518 if (so.sin.sin_family == AF_UNIX) { |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14576
diff
changeset
|
519 if (addr_r != NULL) |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14576
diff
changeset
|
520 memset(addr_r, 0, sizeof(*addr_r)); |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14576
diff
changeset
|
521 if (port_r != NULL) *port_r = 0; |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
522 } else { |
14629
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14576
diff
changeset
|
523 if (addr_r != NULL) sin_get_ip(&so, addr_r); |
c93ca5e46a8a
Marked functions parameters that are allowed to be NULL. Some APIs were also changed.
Timo Sirainen <tss@iki.fi>
parents:
14576
diff
changeset
|
524 if (port_r != NULL) *port_r = sin_get_port(&so); |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
525 } |
0 | 526 return ret; |
527 } | |
528 | |
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
|
529 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 530 { |
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
|
531 ssize_t ret; |
0 | 532 |
533 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
|
534 i_assert(len <= SSIZE_T_MAX); |
0 | 535 |
3681
771cd6776d4b
Use read() instead of recv() in net_receive(), so it works even with
Timo Sirainen <tss@iki.fi>
parents:
3621
diff
changeset
|
536 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
|
537 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
|
538 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
539 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
540 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
541 } |
0 | 542 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
543 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
|
544 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
|
545 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
546 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
547 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
|
548 /* 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
|
549 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
550 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
551 } |
0 | 552 |
553 return ret; | |
554 } | |
555 | |
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
|
556 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 557 { |
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
|
558 ssize_t ret; |
0 | 559 |
560 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
|
561 i_assert(len <= SSIZE_T_MAX); |
0 | 562 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
563 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
|
564 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
|
565 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
566 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
567 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
|
568 return -2; |
0 | 569 |
570 return ret; | |
571 } | |
572 | |
4745
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4699
diff
changeset
|
573 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
|
574 unsigned int *ips_count) |
0 | 575 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
576 /* @UNSAFE */ |
0 | 577 #ifdef HAVE_IPV6 |
578 union sockaddr_union *so; | |
579 struct addrinfo hints, *ai, *origai; | |
580 int host_error; | |
581 #else | |
582 struct hostent *hp; | |
583 #endif | |
584 int count; | |
585 | |
586 *ips = NULL; | |
587 *ips_count = 0; | |
588 | |
589 #ifdef HAVE_IPV6 | |
590 memset(&hints, 0, sizeof(struct addrinfo)); | |
591 hints.ai_socktype = SOCK_STREAM; | |
592 | |
593 /* save error to host_error for later use */ | |
594 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
595 if (host_error != 0) | |
596 return host_error; | |
597 | |
598 /* get number of IPs */ | |
599 origai = ai; | |
600 for (count = 0; ai != NULL; ai = ai->ai_next) | |
601 count++; | |
602 | |
603 *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
|
604 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 605 |
606 count = 0; | |
607 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
608 so = (union sockaddr_union *) ai->ai_addr; | |
609 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
610 sin_get_ip(so, &(*ips)[count]); |
0 | 611 } |
612 freeaddrinfo(origai); | |
613 #else | |
614 hp = gethostbyname(addr); | |
615 if (hp == NULL) | |
616 return h_errno; | |
617 | |
618 /* get number of IPs */ | |
619 count = 0; | |
620 while (hp->h_addr_list[count] != NULL) | |
621 count++; | |
622 | |
623 *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
|
624 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 625 |
626 while (count > 0) { | |
627 count--; | |
628 | |
629 (*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
|
630 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
|
631 sizeof((*ips)[count].u.ip4)); |
0 | 632 } |
633 #endif | |
634 | |
635 return 0; | |
636 } | |
637 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
638 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 639 { |
640 union sockaddr_union so; | |
641 socklen_t addrlen; | |
642 | |
643 i_assert(fd >= 0); | |
644 | |
645 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
|
646 if (getsockname(fd, &so.sa, &addrlen) == -1) |
0 | 647 return -1; |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
648 if (so.sin.sin_family == AF_UNIX) { |
12138
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
649 if (addr != NULL) |
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
650 memset(addr, 0, sizeof(*addr)); |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
651 if (port != NULL) *port = 0; |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
652 } else { |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
653 if (addr != NULL) sin_get_ip(&so, addr); |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
654 if (port != NULL) *port = sin_get_port(&so); |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
655 } |
0 | 656 return 0; |
657 } | |
658 | |
2098 | 659 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port) |
660 { | |
661 union sockaddr_union so; | |
662 socklen_t addrlen; | |
663 | |
664 i_assert(fd >= 0); | |
665 | |
666 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
|
667 if (getpeername(fd, &so.sa, &addrlen) == -1) |
2098 | 668 return -1; |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
669 if (so.sin.sin_family == AF_UNIX) { |
12138
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
670 if (addr != NULL) |
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
671 memset(addr, 0, sizeof(*addr)); |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
672 if (port != NULL) *port = 0; |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
673 } else { |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
674 if (addr != NULL) sin_get_ip(&so, addr); |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
675 if (port != NULL) *port = sin_get_port(&so); |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
676 } |
2098 | 677 return 0; |
678 } | |
679 | |
9843 | 680 int net_getunixname(int fd, const char **name_r) |
681 { | |
13363 | 682 union sockaddr_union_unix so; |
683 socklen_t addrlen = sizeof(so); | |
9843 | 684 |
13363 | 685 if (getsockname(fd, &so.sa, &addrlen) < 0) |
9843 | 686 return -1; |
13557 | 687 if (so.un.sun_family != AF_UNIX) { |
9843 | 688 errno = ENOTSOCK; |
689 return -1; | |
690 } | |
13557 | 691 *name_r = t_strdup(so.un.sun_path); |
9843 | 692 return 0; |
693 } | |
694 | |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
695 int net_getunixcred(int fd, struct net_unix_cred *cred_r) |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
696 { |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
697 #if defined(SO_PEERCRED) |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
698 # if defined(HAVE_STRUCT_SOCKPEERCRED) |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
699 /* OpenBSD (may also provide getpeereid, but we also want pid) */ |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
700 struct sockpeercred ucred; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
701 # else |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
702 /* Linux */ |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
703 struct ucred ucred; |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
704 # endif |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
705 socklen_t len = sizeof(ucred); |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
706 |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
707 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) { |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
708 i_error("getsockopt(SO_PEERCRED) failed: %m"); |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
709 return -1; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
710 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
711 cred_r->uid = ucred.uid; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
712 cred_r->gid = ucred.gid; |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
713 cred_r->pid = ucred.pid; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
714 return 0; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
715 #elif defined(LOCAL_PEEREID) |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
716 /* NetBSD (may also provide getpeereid, but we also want pid) */ |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
717 struct unpcbid ucred; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
718 socklen_t len = sizeof(ucred); |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
719 |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
720 if (getsockopt(s, 0, LOCAL_PEEREID, &ucred, &len) < 0) { |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
721 i_error("getsockopt(LOCAL_PEEREID) failed: %m"); |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
722 return -1; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
723 } |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
724 |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
725 cred_r->uid = ucred.unp_euid; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
726 cred_r->gid = ucred.unp_egid; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
727 cred_r->pid = ucred.unp_pid; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
728 return 0; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
729 #elif defined(HAVE_GETPEEREID) |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
730 /* OSX 10.4+, FreeBSD 4.6+, OpenBSD 3.0+, NetBSD 5.0+ */ |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
731 if (getpeereid(fd, &cred_r->uid, &cred_r->gid) < 0) { |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
732 i_error("getpeereid() failed: %m"); |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
733 return -1; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
734 } |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
735 cred_r->pid = (pid_t)-1; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
736 return 0; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
737 #elif defined(LOCAL_PEERCRED) |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
738 /* Older FreeBSD */ |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
739 struct xucred ucred; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
740 socklen_t len = sizeof(ucred); |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
741 |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
742 if (getsockopt(fd, 0, LOCAL_PEERCRED, &ucred, &len) < 0) { |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
743 i_error("getsockopt(LOCAL_PEERCRED) failed: %m"); |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
744 return -1; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
745 } |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
746 |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
747 if (ucred.cr_version != XUCRED_VERSION) { |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
748 errno = EINVAL; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
749 return -1; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
750 } |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
751 |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
752 cred_r->uid = ucred.cr_uid; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
753 cred_r->gid = ucred.cr_gid; |
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
754 cred_r->pid = (pid_t)-1; |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
755 return 0; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
756 #elif defined(HAVE_GETPEERUCRED) |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
757 /* Solaris */ |
14377
98fd46f8d1ab
net_getunixcred(): Fixed Solaris to use getpeerucred() properly.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
758 ucred_t *ucred = NULL; |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
759 |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
760 if (getpeerucred(fd, &ucred) < 0) { |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
761 i_error("getpeerucred() failed: %m"); |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
762 return -1; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
763 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
764 cred_r->uid = ucred_geteuid(ucred); |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
765 cred_r->gid = ucred_getrgid(ucred); |
14233
80688ab1ea3d
Extends struct net_unix_cred with pid field and modifies net_getunixcred() to fill it in if possible.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
766 cred_r->pid = ucred_getpid(ucred); |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
767 ucred_free(ucred); |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
768 |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
769 if (cred_r->uid == (uid_t)-1 || |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
770 cred_r->gid == (gid_t)-1) { |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
771 errno = EINVAL; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
772 return -1; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
773 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
774 return 0; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
775 #else |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
776 errno = EINVAL; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
777 return -1; |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
778 #endif |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
779 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
780 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
781 const char *net_ip2addr(const struct ip_addr *ip) |
0 | 782 { |
783 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
784 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
|
785 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
786 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
|
787 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
|
788 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
789 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
790 return t_strdup(addr); |
0 | 791 #else |
792 unsigned long ip4; | |
793 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
794 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
|
795 return NULL; |
0 | 796 |
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
|
797 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
|
798 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
|
799 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
800 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
801 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
802 (ip4 & 0x000000ff)); |
0 | 803 #endif |
804 } | |
805 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
806 int net_addr2ip(const char *addr, struct ip_addr *ip) |
0 | 807 { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
808 int ret; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
809 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
810 if (strchr(addr, ':') != NULL) { |
0 | 811 /* IPv6 */ |
812 ip->family = AF_INET6; | |
813 #ifdef HAVE_IPV6 | |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
814 T_BEGIN { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
815 if (addr[0] == '[') { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
816 /* allow [ipv6 addr] */ |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
817 unsigned int len = strlen(addr); |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
818 if (addr[len-1] == ']') |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
819 addr = t_strndup(addr+1, len-2); |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
820 } |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
821 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
|
822 } T_END; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
823 if (ret == 0) |
0 | 824 return -1; |
825 #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
|
826 ip->u.ip4.s_addr = 0; |
0 | 827 #endif |
828 } else { | |
829 /* IPv4 */ | |
830 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
|
831 if (inet_aton(addr, &ip->u.ip4) == 0) |
0 | 832 return -1; |
833 } | |
834 | |
835 return 0; | |
836 } | |
837 | |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
838 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
|
839 struct ip_addr *dest) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
840 { |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
841 #ifdef HAVE_IPV6 |
4699
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
842 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
|
843 { 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
|
844 |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
845 if (!IPADDR_IS_V6(src)) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
846 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
|
847 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
|
848 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
849 |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
850 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
|
851 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
|
852 return 0; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
853 #else |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
854 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
855 #endif |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
856 } |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
857 |
0 | 858 int net_geterror(int fd) |
859 { | |
860 int data; | |
861 socklen_t len = sizeof(data); | |
862 | |
863 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
864 return -1; | |
865 | |
866 return data; | |
867 } | |
868 | |
869 const char *net_gethosterror(int error) | |
870 { | |
871 #ifdef HAVE_IPV6 | |
872 i_assert(error != 0); | |
873 | |
874 return gai_strerror(error); | |
875 #else | |
876 switch (error) { | |
877 case HOST_NOT_FOUND: | |
878 return "Host not found"; | |
879 case NO_ADDRESS: | |
880 return "No IP address found for name"; | |
881 case NO_RECOVERY: | |
6666 | 882 return "A non-recoverable name server error occurred"; |
0 | 883 case TRY_AGAIN: |
884 return "A temporary error on an authoritative name server"; | |
885 } | |
886 | |
887 /* unknown error */ | |
888 return NULL; | |
889 #endif | |
890 } | |
891 | |
892 int net_hosterror_notfound(int error) | |
893 { | |
894 #ifdef HAVE_IPV6 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
895 #ifdef EAI_NODATA /* NODATA is depricated */ |
0 | 896 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); |
897 #else | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
898 return error != 1 && (error == EAI_NONAME); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
899 #endif |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
900 #else |
0 | 901 return error == HOST_NOT_FOUND || error == NO_ADDRESS; |
902 #endif | |
903 } | |
904 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
905 const char *net_getservbyport(unsigned short port) |
0 | 906 { |
907 struct servent *entry; | |
908 | |
349 | 909 entry = getservbyport(htons(port), "tcp"); |
0 | 910 return entry == NULL ? NULL : entry->s_name; |
911 } | |
912 | |
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
|
913 bool is_ipv4_address(const char *addr) |
0 | 914 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
915 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
916 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
|
917 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
918 addr++; |
0 | 919 } |
920 | |
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
|
921 return TRUE; |
0 | 922 } |
923 | |
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
|
924 bool is_ipv6_address(const char *addr) |
0 | 925 { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
926 bool have_prefix = FALSE; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
927 |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
928 if (*addr == '[') { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
929 have_prefix = TRUE; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
930 addr++; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
931 } |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
932 while (*addr != '\0') { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
933 if (*addr != ':' && !i_isxdigit(*addr)) { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
934 if (have_prefix && *addr == ']' && addr[1] == '\0') |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
935 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
|
936 return FALSE; |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
937 } |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
938 addr++; |
0 | 939 } |
940 | |
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
|
941 return TRUE; |
0 | 942 } |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
943 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
944 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
|
945 unsigned int *bits_r) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
946 { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
947 const char *p; |
11086
260e190306b0
Started using str_to_*() functions instead of libc's ones.
Timo Sirainen <tss@iki.fi>
parents:
10789
diff
changeset
|
948 unsigned int bits, max_bits; |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
949 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
950 p = strchr(network, '/'); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
951 if (p != NULL) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
952 network = t_strdup_until(network, p++); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
953 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
954 if (net_addr2ip(network, ip_r) < 0) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
955 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
956 |
13290
d473660bc54d
liblib: Added IPADDR_BITS() macro.
Timo Sirainen <tss@iki.fi>
parents:
12887
diff
changeset
|
957 max_bits = IPADDR_BITS(ip_r); |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
958 if (p == NULL) { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
959 /* full IP address must match */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
960 bits = max_bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
961 } else { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
962 /* 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
|
963 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
|
964 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
965 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
966 *bits_r = bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
967 return 0; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
968 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
969 |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
970 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
|
971 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
|
972 { |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
973 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
|
974 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
|
975 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
|
976 unsigned int pos, i; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
977 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
978 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
|
979 /* IPv4 address mapped disguised as IPv6 address */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
980 ip = &tmp_ip; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
981 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
982 |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
983 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
|
984 /* 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
|
985 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
986 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
987 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
|
988 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
989 if (IPADDR_IS_V4(ip)) { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
990 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
|
991 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
|
992 } else { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
993 #ifdef HAVE_IPV6 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
994 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
|
995 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
|
996 #else |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
997 /* shouldn't get here */ |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
998 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
999 #endif |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1000 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1001 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1002 /* 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
|
1003 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
|
1004 if (ip1[i] != ip2[i]) |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1005 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1006 } |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
1007 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
|
1008 i2 = htonl(ip2[i]); |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1009 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1010 /* 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
|
1011 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
|
1012 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
|
1013 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1014 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1015 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1016 /* 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
|
1017 bits -= pos; |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
1018 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
|
1019 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
|
1020 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1021 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1022 return TRUE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1023 } |