Mercurial > dovecot > core-2.2
annotate src/lib/net.c @ 19136:fefaa6d09a81
Replaced unlink() calls with i_unlink*() wherever possible.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 08 Sep 2015 19:07:02 +0300 |
parents | c53ccafc4a8e |
children | 9e120590e0ef |
rev | line source |
---|---|
18137 | 1 /* Copyright (c) 1999-2015 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" |
15187
02451e967a06
Renamed network.[ch] to net.[ch].
Timo Sirainen <tss@iki.fi>
parents:
14691
diff
changeset
|
7 #include "net.h" |
0 | 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 | |
16276
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
41 #if !defined(HAVE_GETPEEREID) && !defined(SO_PEERCRED) && !defined(HAVE_GETPEERUCRED) && defined(MSG_WAITALL) && defined(LOCAL_CREDS) |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
42 # define NEEDS_LOCAL_CREDS 1 |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
43 #endif |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
44 |
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
|
45 bool net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2) |
0 | 46 { |
10789 | 47 return net_ip_cmp(ip1, ip2) == 0; |
48 } | |
49 | |
50 int net_ip_cmp(const struct ip_addr *ip1, const struct ip_addr *ip2) | |
51 { | |
0 | 52 if (ip1->family != ip2->family) |
10789 | 53 return ip1->family - ip2->family; |
0 | 54 |
55 #ifdef HAVE_IPV6 | |
10789 | 56 if (ip1->family == AF_INET6) |
57 return memcmp(&ip1->u.ip6, &ip2->u.ip6, sizeof(ip1->u.ip6)); | |
0 | 58 #endif |
59 | |
10789 | 60 return memcmp(&ip1->u.ip4, &ip2->u.ip4, sizeof(ip1->u.ip4)); |
0 | 61 } |
62 | |
5865 | 63 unsigned int net_ip_hash(const struct ip_addr *ip) |
64 { | |
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 const unsigned char *p; |
5865 | 66 unsigned int len, g, h = 0; |
67 | |
68 #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
|
69 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
|
70 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
|
71 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
|
72 } else |
5865 | 73 #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
|
74 { |
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
|
75 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
|
76 } |
5865 | 77 |
78 for (; len > 0; len--, p++) { | |
79 h = (h << 4) + *p; | |
80 if ((g = h & 0xf0000000UL)) { | |
81 h = h ^ (g >> 24); | |
82 h = h ^ g; | |
83 } | |
84 } | |
85 | |
86 return h; | |
87 } | |
0 | 88 |
89 /* 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
|
90 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
|
91 sin_set_ip(union sockaddr_union *so, const struct ip_addr *ip) |
0 | 92 { |
93 if (ip == NULL) { | |
94 #ifdef HAVE_IPV6 | |
95 so->sin6.sin6_family = AF_INET6; | |
96 so->sin6.sin6_addr = in6addr_any; | |
97 #else | |
98 so->sin.sin_family = AF_INET; | |
99 so->sin.sin_addr.s_addr = INADDR_ANY; | |
100 #endif | |
101 return; | |
102 } | |
103 | |
104 so->sin.sin_family = ip->family; | |
105 #ifdef HAVE_IPV6 | |
106 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
|
107 memcpy(&so->sin6.sin6_addr, &ip->u.ip6, sizeof(ip->u.ip6)); |
0 | 108 else |
109 #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
|
110 memcpy(&so->sin.sin_addr, &ip->u.ip4, sizeof(ip->u.ip4)); |
0 | 111 } |
112 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
113 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
|
114 sin_get_ip(const union sockaddr_union *so, struct ip_addr *ip) |
0 | 115 { |
12138
e403f4dc95ea
Make sure no data is leaked across processes via struct ip_addr fields.
Timo Sirainen <tss@iki.fi>
parents:
11926
diff
changeset
|
116 /* 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
|
117 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
|
118 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
|
119 |
0 | 120 ip->family = so->sin.sin_family; |
121 | |
122 #ifdef HAVE_IPV6 | |
123 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
|
124 memcpy(&ip->u.ip6, &so->sin6.sin6_addr, sizeof(ip->u.ip6)); |
0 | 125 else |
126 #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
|
127 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
|
128 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
|
129 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
|
130 memset(&ip->u, 0, sizeof(ip->u)); |
0 | 131 } |
132 | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
133 static inline void sin_set_port(union sockaddr_union *so, in_port_t port) |
0 | 134 { |
135 #ifdef HAVE_IPV6 | |
136 if (so->sin.sin_family == AF_INET6) | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
137 so->sin6.sin6_port = htons(port); |
0 | 138 else |
139 #endif | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
140 so->sin.sin_port = htons(port); |
0 | 141 } |
142 | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
143 static inline in_port_t sin_get_port(union sockaddr_union *so) |
0 | 144 { |
145 #ifdef HAVE_IPV6 | |
146 if (so->sin.sin_family == AF_INET6) | |
147 return ntohs(so->sin6.sin6_port); | |
148 #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
|
149 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
|
150 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
|
151 |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
152 return 0; |
0 | 153 } |
154 | |
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 #ifdef __FreeBSD__ |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
156 static int |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
157 net_connect_ip_full_freebsd(const struct ip_addr *ip, in_port_t port, |
17914
f0015ab0efb1
lib: Compiling fix for FreeBSD
Timo Sirainen <tss@iki.fi>
parents:
17883
diff
changeset
|
158 const struct ip_addr *my_ip, int sock_type, |
f0015ab0efb1
lib: Compiling fix for FreeBSD
Timo Sirainen <tss@iki.fi>
parents:
17883
diff
changeset
|
159 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
|
160 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
161 static int net_connect_ip_full(const struct ip_addr *ip, in_port_t port, |
17914
f0015ab0efb1
lib: Compiling fix for FreeBSD
Timo Sirainen <tss@iki.fi>
parents:
17883
diff
changeset
|
162 const struct ip_addr *my_ip, int sock_type, |
f0015ab0efb1
lib: Compiling fix for FreeBSD
Timo Sirainen <tss@iki.fi>
parents:
17883
diff
changeset
|
163 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
|
164 { |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
165 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
|
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 for (try = 0;;) { |
17914
f0015ab0efb1
lib: Compiling fix for FreeBSD
Timo Sirainen <tss@iki.fi>
parents:
17883
diff
changeset
|
168 fd = net_connect_ip_full_freebsd(ip, port, my_ip, sock_type, 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
|
169 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
|
170 (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
|
171 break; |
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 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
|
174 |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
175 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
|
176 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
|
177 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
|
178 */ |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
179 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
180 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
|
181 } |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
182 /* then some kludging: */ |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
183 #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
|
184 #endif |
ed6693fc06d6
FreeBSD: If connect() fails with EADDRINUSE or EACCES, try again max. 3 times.
Timo Sirainen <tss@iki.fi>
parents:
6825
diff
changeset
|
185 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
186 static int net_connect_ip_full(const struct ip_addr *ip, in_port_t port, |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
187 const struct ip_addr *my_ip, int sock_type, bool blocking) |
0 | 188 { |
189 union sockaddr_union so; | |
190 int fd, ret, opt = 1; | |
191 | |
192 if (my_ip != NULL && ip->family != my_ip->family) { | |
193 i_warning("net_connect_ip(): ip->family != my_ip->family"); | |
194 my_ip = NULL; | |
195 } | |
196 | |
197 /* create the socket */ | |
198 memset(&so, 0, sizeof(so)); | |
199 so.sin.sin_family = ip->family; | |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
200 fd = socket(ip->family, sock_type, 0); |
0 | 201 |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
202 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
|
203 i_error("socket() failed: %m"); |
0 | 204 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
|
205 } |
0 | 206 |
207 /* set socket options */ | |
17459
e279bd060c28
lib: Added (void) prefixes to some setsockopt() calls which we don't care if they fail.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
208 (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
209 if (sock_type == SOCK_STREAM) |
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
210 (void)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
|
211 if (!blocking) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
212 net_set_nonblock(fd, TRUE); |
0 | 213 |
214 /* set our own address */ | |
215 if (my_ip != NULL) { | |
216 sin_set_ip(&so, my_ip); | |
217 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
|
218 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
|
219 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
|
220 return -1; |
0 | 221 } |
222 } | |
223 | |
224 /* connect */ | |
225 sin_set_ip(&so, ip); | |
226 sin_set_port(&so, port); | |
227 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
228 | |
229 #ifndef WIN32 | |
230 if (ret < 0 && errno != EINPROGRESS) | |
231 #else | |
232 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
233 #endif | |
234 { | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
235 i_close_fd(&fd); |
0 | 236 return -1; |
237 } | |
238 | |
239 return fd; | |
240 } | |
11879
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
241 #ifdef __FreeBSD__ |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
242 # undef net_connect_ip_full |
4fb7e5327efc
net_connect_ip*(): Fixed FreeBSD kludging.
Timo Sirainen <tss@iki.fi>
parents:
11794
diff
changeset
|
243 #endif |
0 | 244 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
245 int net_connect_ip(const struct ip_addr *ip, in_port_t port, |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
246 const struct ip_addr *my_ip) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
247 { |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
248 return net_connect_ip_full(ip, port, my_ip, SOCK_STREAM, FALSE); |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
249 } |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
250 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
251 int net_connect_ip_blocking(const struct ip_addr *ip, in_port_t port, |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
252 const struct ip_addr *my_ip) |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
253 { |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
254 return net_connect_ip_full(ip, port, my_ip, SOCK_STREAM, TRUE); |
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
255 } |
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
256 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
257 int net_connect_udp(const struct ip_addr *ip, in_port_t port, |
17883
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
258 const struct ip_addr *my_ip) |
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
259 { |
95ac50948e39
lib: Added support for connecting UDP sockets.
Stephan Bosch <stephan@rename-it.nl>
parents:
17461
diff
changeset
|
260 return net_connect_ip_full(ip, port, my_ip, SOCK_DGRAM, FALSE); |
11794
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
261 } |
5ea9394b5ced
liblib: Added net_connect_ip_blocking().
Timo Sirainen <tss@iki.fi>
parents:
11564
diff
changeset
|
262 |
11340 | 263 int net_try_bind(const struct ip_addr *ip) |
264 { | |
265 union sockaddr_union so; | |
266 int fd; | |
267 | |
268 /* create the socket */ | |
269 memset(&so, 0, sizeof(so)); | |
270 so.sin.sin_family = ip->family; | |
271 fd = socket(ip->family, SOCK_STREAM, 0); | |
272 if (fd == -1) { | |
273 i_error("socket() failed: %m"); | |
274 return -1; | |
275 } | |
276 | |
277 sin_set_ip(&so, ip); | |
278 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
|
279 i_close_fd(&fd); |
11340 | 280 return -1; |
281 } | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
282 i_close_fd(&fd); |
11340 | 283 return 0; |
284 } | |
285 | |
0 | 286 int net_connect_unix(const char *path) |
287 { | |
13557 | 288 union sockaddr_union_unix sa; |
0 | 289 int fd, ret; |
290 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
291 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
292 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
293 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 294 /* too long path */ |
15601
e4cc3f2b66e3
net_connect_unix(): If path is too long, return ENAMETOOLONG if it's available.
Timo Sirainen <tss@iki.fi>
parents:
14387
diff
changeset
|
295 #ifdef ENAMETOOLONG |
e4cc3f2b66e3
net_connect_unix(): If path is too long, return ENAMETOOLONG if it's available.
Timo Sirainen <tss@iki.fi>
parents:
14387
diff
changeset
|
296 errno = ENAMETOOLONG; |
e4cc3f2b66e3
net_connect_unix(): If path is too long, return ENAMETOOLONG if it's available.
Timo Sirainen <tss@iki.fi>
parents:
14387
diff
changeset
|
297 #else |
0 | 298 errno = EINVAL; |
15601
e4cc3f2b66e3
net_connect_unix(): If path is too long, return ENAMETOOLONG if it's available.
Timo Sirainen <tss@iki.fi>
parents:
14387
diff
changeset
|
299 #endif |
0 | 300 return -1; |
301 } | |
302 | |
303 /* create the socket */ | |
304 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
|
305 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
|
306 i_error("socket(%s) failed: %m", path); |
0 | 307 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
|
308 } |
0 | 309 |
3076
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
310 net_set_nonblock(fd, TRUE); |
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
311 |
0 | 312 /* connect */ |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
313 ret = connect(fd, &sa.sa, sizeof(sa)); |
0 | 314 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
|
315 i_close_fd(&fd); |
0 | 316 return -1; |
317 } | |
318 | |
16276
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
319 #ifdef NEEDS_LOCAL_CREDS |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
320 { |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
321 int on = 1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
322 if (setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof on)) { |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
323 i_error("setsockopt(LOCAL_CREDS) failed: %m"); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
324 return -1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
325 } |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
326 } |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
327 #endif |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
328 |
0 | 329 return fd; |
330 } | |
331 | |
10158
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
332 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
|
333 { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
334 struct timeval start, now; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
335 int fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
336 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
337 if (gettimeofday(&start, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
338 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
339 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
340 do { |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
341 fd = net_connect_unix(path); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
342 if (fd != -1 || (errno != EAGAIN && errno != ECONNREFUSED)) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
343 break; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
344 |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
345 /* busy. wait for a while. */ |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
346 usleep(((rand() % 10) + 1) * 10000); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
347 if (gettimeofday(&now, NULL) < 0) |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
348 i_panic("gettimeofday() failed: %m"); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
349 } while (timeval_diff_msecs(&now, &start) < (int)msecs); |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
350 return fd; |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
351 } |
1bc88aa1373f
Added net_connect_unix_with_retries().
Timo Sirainen <tss@iki.fi>
parents:
9843
diff
changeset
|
352 |
0 | 353 void net_disconnect(int fd) |
354 { | |
11564
c24ee1ebb159
net_disconnect(): Ignore ECONNRESET error.
Timo Sirainen <tss@iki.fi>
parents:
11448
diff
changeset
|
355 /* 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
|
356 data in transmit buffer. We don't care. */ |
c24ee1ebb159
net_disconnect(): Ignore ECONNRESET error.
Timo Sirainen <tss@iki.fi>
parents:
11448
diff
changeset
|
357 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
|
358 i_error("net_disconnect() failed: %m"); |
0 | 359 } |
360 | |
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
|
361 void net_set_nonblock(int fd, bool nonblock) |
0 | 362 { |
14634
a65006d95d53
fd_set_nonblock() API changed to i_fatal() on failure.
Timo Sirainen <tss@iki.fi>
parents:
14629
diff
changeset
|
363 fd_set_nonblock(fd, nonblock); |
0 | 364 } |
365 | |
6411
6a64e64fa3a3
Renamed __attr_*__ to ATTR_*. Renamed __attrs_used__ to ATTRS_DEFINED.
Timo Sirainen <tss@iki.fi>
parents:
5985
diff
changeset
|
366 int net_set_cork(int fd ATTR_UNUSED, bool cork ATTR_UNUSED) |
0 | 367 { |
368 #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
|
369 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
|
370 |
55df57c028d4
Added "bool" type and changed all ints that were used as booleans to bool.
Timo Sirainen <tss@iki.fi>
parents:
3681
diff
changeset
|
371 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
|
372 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
373 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
374 return -1; |
0 | 375 #endif |
376 } | |
377 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
378 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
|
379 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
380 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
|
381 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
|
382 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
383 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
384 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
|
385 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
386 #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
|
387 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
|
388 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
|
389 #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
|
390 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
|
391 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
392 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
393 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
394 int net_listen(const struct ip_addr *my_ip, in_port_t *port, int backlog) |
0 | 395 { |
16822
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
396 enum net_listen_flags flags = 0; |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
397 |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
398 return net_listen_full(my_ip, port, &flags, backlog); |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
399 } |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
400 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
401 int net_listen_full(const struct ip_addr *my_ip, in_port_t *port, |
16822
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
402 enum net_listen_flags *flags, int backlog) |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
403 { |
0 | 404 union sockaddr_union so; |
405 int ret, fd, opt = 1; | |
406 socklen_t len; | |
407 | |
408 memset(&so, 0, sizeof(so)); | |
409 sin_set_port(&so, *port); | |
410 sin_set_ip(&so, my_ip); | |
411 | |
412 /* create the socket */ | |
413 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
414 #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
|
415 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
|
416 (errno == EINVAL || errno == EAFNOSUPPORT)) { |
0 | 417 /* IPv6 is not supported by OS */ |
418 so.sin.sin_family = AF_INET; | |
419 so.sin.sin_addr.s_addr = INADDR_ANY; | |
420 | |
421 fd = socket(AF_INET, SOCK_STREAM, 0); | |
422 } | |
423 #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
|
424 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
|
425 i_error("socket() failed: %m"); |
0 | 426 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
|
427 } |
0 | 428 |
429 /* set socket options */ | |
17459
e279bd060c28
lib: Added (void) prefixes to some setsockopt() calls which we don't care if they fail.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
430 (void)setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); |
e279bd060c28
lib: Added (void) prefixes to some setsockopt() calls which we don't care if they fail.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
431 (void)setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); |
0 | 432 |
16822
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
433 if ((*flags & NET_LISTEN_FLAG_REUSEPORT) != 0) { |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
434 #ifdef SO_REUSEPORT |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
435 if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
436 &opt, sizeof(opt)) < 0) |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
437 #endif |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
438 *flags &= ~NET_LISTEN_FLAG_REUSEPORT; |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
439 } |
6a814345f16c
lib: Added net_listen_full() with a flag to set SO_REUSEPORT on the socket if available.
Timo Sirainen <tss@iki.fi>
parents:
16806
diff
changeset
|
440 |
7274
da971cec0395
[::] listens only for IPv6 addresses now. This gets rid of IPv4-mapped IPv6
Timo Sirainen <tss@iki.fi>
parents:
7086
diff
changeset
|
441 /* 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
|
442 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
|
443 #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
|
444 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
|
445 opt = 1; |
17459
e279bd060c28
lib: Added (void) prefixes to some setsockopt() calls which we don't care if they fail.
Timo Sirainen <tss@iki.fi>
parents:
17130
diff
changeset
|
446 (void)setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); |
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
|
447 } |
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
|
448 #endif |
0 | 449 /* specify the address/port we want to listen in */ |
450 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
|
451 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
|
452 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
|
453 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
|
454 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
|
455 } |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
456 } else { |
0 | 457 /* get the actual port we started listen */ |
458 len = SIZEOF_SOCKADDR(so); | |
459 ret = getsockname(fd, &so.sa, &len); | |
460 if (ret >= 0) { | |
461 *port = sin_get_port(&so); | |
462 | |
463 /* start listening */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
464 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
|
465 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
|
466 |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
467 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
|
468 i_error("listen() failed: %m"); |
0 | 469 } |
470 } | |
471 | |
472 /* 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
|
473 i_close_fd(&fd); |
0 | 474 return -1; |
475 } | |
476 | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
477 int net_listen_unix(const char *path, int backlog) |
0 | 478 { |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
479 union { |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
480 struct sockaddr sa; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
481 struct sockaddr_un un; |
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
482 } sa; |
0 | 483 int fd; |
484 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
485 memset(&sa, 0, sizeof(sa)); |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
486 sa.un.sun_family = AF_UNIX; |
6422
18173a52f721
Renamed strocpy() to i_strocpy().
Timo Sirainen <tss@iki.fi>
parents:
6411
diff
changeset
|
487 if (i_strocpy(sa.un.sun_path, path, sizeof(sa.un.sun_path)) < 0) { |
0 | 488 /* too long path */ |
16806
07cc79e0041d
net_listen_unix(): If path is too long, return EOVERFLOW instead of EINVAL.
Timo Sirainen <tss@iki.fi>
parents:
16666
diff
changeset
|
489 errno = EOVERFLOW; |
0 | 490 return -1; |
491 } | |
492 | |
493 /* create the socket */ | |
494 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
|
495 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
|
496 i_error("socket() failed: %m"); |
0 | 497 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
|
498 } |
0 | 499 |
16276
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
500 #ifdef NEEDS_LOCAL_CREDS |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
501 { |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
502 int on = 1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
503 if (setsockopt(fd, 0, LOCAL_CREDS, &on, sizeof on)) { |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
504 i_error("setsockopt(LOCAL_CREDS) failed: %m"); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
505 return -1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
506 } |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
507 } |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
508 #endif |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
509 |
0 | 510 /* bind */ |
5985
8ae4b8d78a25
More union usage to avoid casting.
Timo Sirainen <tss@iki.fi>
parents:
5984
diff
changeset
|
511 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
|
512 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
513 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
|
514 } else { |
0 | 515 /* start listening */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
516 if (listen(fd, backlog) == 0) |
0 | 517 return fd; |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
518 |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
519 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
520 i_error("listen() failed: %m"); |
0 | 521 } |
522 | |
14691
3945a3646c67
Changed i_close_fd() API to set the fd to -1 after closing.
Timo Sirainen <tss@iki.fi>
parents:
14687
diff
changeset
|
523 i_close_fd(&fd); |
0 | 524 return -1; |
525 } | |
526 | |
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
|
527 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
|
528 { |
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
|
529 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
|
530 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
|
531 |
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
|
532 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
|
533 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
|
534 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
|
535 |
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
|
536 /* 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
|
537 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
|
538 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
|
539 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
|
540 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
|
541 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
|
542 } |
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
|
543 |
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
|
544 /* delete and try again */ |
19136
fefaa6d09a81
Replaced unlink() calls with i_unlink*() wherever possible.
Timo Sirainen <tss@iki.fi>
parents:
19041
diff
changeset
|
545 if (i_unlink_if_exists(path) < 0) { |
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
|
546 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
|
547 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
|
548 } |
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
|
549 } |
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
|
550 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
|
551 } |
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
|
552 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
553 int net_accept(int fd, struct ip_addr *addr_r, in_port_t *port_r) |
0 | 554 { |
555 union sockaddr_union so; | |
556 int ret; | |
557 socklen_t addrlen; | |
558 | |
559 i_assert(fd >= 0); | |
560 | |
561 addrlen = sizeof(so); | |
562 ret = accept(fd, &so.sa, &addrlen); | |
563 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
564 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
|
565 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
|
566 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
|
567 else |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
568 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
569 } |
11926
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
570 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
|
571 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
|
572 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
|
573 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
|
574 } 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
|
575 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
|
576 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
|
577 } |
0 | 578 return ret; |
579 } | |
580 | |
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
|
581 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 582 { |
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
|
583 ssize_t ret; |
0 | 584 |
585 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
|
586 i_assert(len <= SSIZE_T_MAX); |
0 | 587 |
3681
771cd6776d4b
Use read() instead of recv() in net_receive(), so it works even with
Timo Sirainen <tss@iki.fi>
parents:
3621
diff
changeset
|
588 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
|
589 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
|
590 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
591 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
592 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
593 } |
0 | 594 |
6825
85385079b044
Use likely() and unlikely() macros to make commonly checked error handling
Timo Sirainen <tss@iki.fi>
parents:
6666
diff
changeset
|
595 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
|
596 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
|
597 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
598 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
599 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
|
600 /* 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
|
601 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
602 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
603 } |
0 | 604 |
605 return ret; | |
606 } | |
607 | |
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
|
608 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 609 { |
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
|
610 ssize_t ret; |
0 | 611 |
612 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
|
613 i_assert(len <= SSIZE_T_MAX); |
0 | 614 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
615 ret = send(fd, data, len, 0); |
17120
93cf0434dda4
net_transmit() may have wrongly returned EPIPE error.
Timo Sirainen <tss@iki.fi>
parents:
16822
diff
changeset
|
616 if (ret == -1) { |
93cf0434dda4
net_transmit() may have wrongly returned EPIPE error.
Timo Sirainen <tss@iki.fi>
parents:
16822
diff
changeset
|
617 if (errno == EINTR || errno == EAGAIN) |
93cf0434dda4
net_transmit() may have wrongly returned EPIPE error.
Timo Sirainen <tss@iki.fi>
parents:
16822
diff
changeset
|
618 return 0; |
93cf0434dda4
net_transmit() may have wrongly returned EPIPE error.
Timo Sirainen <tss@iki.fi>
parents:
16822
diff
changeset
|
619 if (errno == EPIPE) |
93cf0434dda4
net_transmit() may have wrongly returned EPIPE error.
Timo Sirainen <tss@iki.fi>
parents:
16822
diff
changeset
|
620 return -2; |
93cf0434dda4
net_transmit() may have wrongly returned EPIPE error.
Timo Sirainen <tss@iki.fi>
parents:
16822
diff
changeset
|
621 } |
0 | 622 return ret; |
623 } | |
624 | |
4745
7ee0bb1b2d97
Changed ips_count to unsigned int in net_gethostbyname(). Also removed
Timo Sirainen <tss@iki.fi>
parents:
4699
diff
changeset
|
625 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
|
626 unsigned int *ips_count) |
0 | 627 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
628 /* @UNSAFE */ |
0 | 629 #ifdef HAVE_IPV6 |
630 union sockaddr_union *so; | |
631 struct addrinfo hints, *ai, *origai; | |
632 int host_error; | |
633 #else | |
634 struct hostent *hp; | |
635 #endif | |
636 int count; | |
637 | |
638 *ips = NULL; | |
639 *ips_count = 0; | |
640 | |
641 #ifdef HAVE_IPV6 | |
642 memset(&hints, 0, sizeof(struct addrinfo)); | |
643 hints.ai_socktype = SOCK_STREAM; | |
644 | |
645 /* save error to host_error for later use */ | |
646 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
647 if (host_error != 0) | |
648 return host_error; | |
649 | |
650 /* get number of IPs */ | |
651 origai = ai; | |
652 for (count = 0; ai != NULL; ai = ai->ai_next) | |
653 count++; | |
654 | |
655 *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
|
656 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 657 |
658 count = 0; | |
659 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
660 so = (union sockaddr_union *) ai->ai_addr; | |
661 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
662 sin_get_ip(so, &(*ips)[count]); |
0 | 663 } |
664 freeaddrinfo(origai); | |
665 #else | |
666 hp = gethostbyname(addr); | |
667 if (hp == NULL) | |
668 return h_errno; | |
669 | |
670 /* get number of IPs */ | |
671 count = 0; | |
672 while (hp->h_addr_list[count] != NULL) | |
673 count++; | |
674 | |
675 *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
|
676 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 677 |
678 while (count > 0) { | |
679 count--; | |
680 | |
681 (*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
|
682 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
|
683 sizeof((*ips)[count].u.ip4)); |
0 | 684 } |
685 #endif | |
686 | |
687 return 0; | |
688 } | |
689 | |
16575
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
690 int net_gethostbyaddr(const struct ip_addr *ip, const char **name_r) |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
691 { |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
692 union sockaddr_union so; |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
693 socklen_t addrlen = sizeof(so); |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
694 char hbuf[NI_MAXHOST]; |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
695 int ret; |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
696 |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
697 memset(&so, 0, sizeof(so)); |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
698 sin_set_ip(&so, ip); |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
699 ret = getnameinfo(&so.sa, addrlen, hbuf, sizeof(hbuf), NULL, 0, |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
700 NI_NAMEREQD); |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
701 if (ret != 0) |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
702 return ret; |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
703 |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
704 *name_r = t_strdup(hbuf); |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
705 return 0; |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
706 } |
eaa712530f3f
Added net_gethostbyaddr() for DNS PTR lookups.
Timo Sirainen <tss@iki.fi>
parents:
16277
diff
changeset
|
707 |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
708 int net_getsockname(int fd, struct ip_addr *addr, in_port_t *port) |
0 | 709 { |
710 union sockaddr_union so; | |
711 socklen_t addrlen; | |
712 | |
713 i_assert(fd >= 0); | |
714 | |
715 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
|
716 if (getsockname(fd, &so.sa, &addrlen) == -1) |
0 | 717 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
|
718 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
|
719 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
|
720 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
|
721 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
|
722 } else { |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
723 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
|
724 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
|
725 } |
0 | 726 return 0; |
727 } | |
728 | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
729 int net_getpeername(int fd, struct ip_addr *addr, in_port_t *port) |
2098 | 730 { |
731 union sockaddr_union so; | |
732 socklen_t addrlen; | |
733 | |
734 i_assert(fd >= 0); | |
735 | |
736 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
|
737 if (getpeername(fd, &so.sa, &addrlen) == -1) |
2098 | 738 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
|
739 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
|
740 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
|
741 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
|
742 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
|
743 } else { |
10c4c9d5fb5b
net_accept(), net_getsock/peername(): Return UNIX sockets with family=port=0.
Timo Sirainen <tss@iki.fi>
parents:
11879
diff
changeset
|
744 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
|
745 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
|
746 } |
2098 | 747 return 0; |
748 } | |
749 | |
9843 | 750 int net_getunixname(int fd, const char **name_r) |
751 { | |
13363 | 752 union sockaddr_union_unix so; |
753 socklen_t addrlen = sizeof(so); | |
9843 | 754 |
13363 | 755 if (getsockname(fd, &so.sa, &addrlen) < 0) |
9843 | 756 return -1; |
13557 | 757 if (so.un.sun_family != AF_UNIX) { |
9843 | 758 errno = ENOTSOCK; |
759 return -1; | |
760 } | |
13557 | 761 *name_r = t_strdup(so.un.sun_path); |
9843 | 762 return 0; |
763 } | |
764 | |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
765 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
|
766 { |
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
|
767 #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
|
768 # 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
|
769 /* 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
|
770 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
|
771 # 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
|
772 /* Linux */ |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
773 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
|
774 # 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
|
775 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
|
776 |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
777 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
|
778 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
|
779 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
|
780 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
781 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
|
782 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
|
783 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
|
784 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
|
785 #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
|
786 /* 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
|
787 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
|
788 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
|
789 |
16276
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
790 if (getsockopt(fd, 0, LOCAL_PEEREID, &ucred, &len) < 0) { |
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
|
791 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
|
792 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
|
793 } |
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
|
794 |
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
|
795 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
|
796 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
|
797 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
|
798 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
|
799 #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
|
800 /* 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
|
801 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
|
802 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
|
803 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
|
804 } |
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
|
805 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
|
806 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
|
807 #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
|
808 /* 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
|
809 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
|
810 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
|
811 |
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
|
812 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
|
813 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
|
814 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
|
815 } |
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
|
816 |
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
|
817 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
|
818 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
|
819 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
|
820 } |
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
|
821 |
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
|
822 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
|
823 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
|
824 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
|
825 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
|
826 #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
|
827 /* Solaris */ |
14377
98fd46f8d1ab
net_getunixcred(): Fixed Solaris to use getpeerucred() properly.
Timo Sirainen <tss@iki.fi>
parents:
14133
diff
changeset
|
828 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
|
829 |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
830 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
|
831 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
|
832 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
|
833 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
834 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
|
835 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
|
836 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
|
837 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
|
838 |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
839 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
|
840 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
|
841 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
|
842 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
|
843 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
844 return 0; |
16276
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
845 #elif NEEDS_LOCAL_CREDS |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
846 /* NetBSD < 5 */ |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
847 int i, n, on; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
848 struct iovec iov; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
849 struct msghdr msg; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
850 struct { |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
851 struct cmsghdr ch; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
852 char buf[110]; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
853 } cdata; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
854 struct sockcred *sc; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
855 |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
856 iov.iov_base = (char *)&on; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
857 iov.iov_len = 1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
858 |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
859 sc = (struct sockcred *)cdata.buf; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
860 sc->sc_uid = sc->sc_euid = sc->sc_gid = sc->sc_egid = -1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
861 memset(&cdata.ch, 0, sizeof cdata.ch); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
862 |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
863 memset(&msg, 0, sizeof msg); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
864 |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
865 msg.msg_iov = &iov; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
866 msg.msg_iovlen = 1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
867 msg.msg_control = &cdata; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
868 msg.msg_controllen = sizeof(cdata.ch) + sizeof(cdata.buf); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
869 |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
870 for (i = 0; i < 10; i++) { |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
871 n = recvmsg(fd, &msg, MSG_WAITALL | MSG_PEEK); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
872 if (n >= 0 || errno != EAGAIN) |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
873 break; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
874 usleep(100); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
875 } |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
876 if (n < 0) { |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
877 i_error("recvmsg() failed: %m"); |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
878 return -1; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
879 } |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
880 cred_r->uid = sc->sc_euid; |
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
881 cred_r->gid = sc->sc_egid; |
16277
c802c0b960e2
net_getunixcred(): Fix to previous NetBSD<5 support: return pid as -1.
Timo Sirainen <tss@iki.fi>
parents:
16276
diff
changeset
|
882 cred_r->pid = (pid_t)-1; |
16276
d594ce839da3
net_getunixcred() support for NetBSD <v5.0. Fixed also building with other NetBSDs.
Timo Sirainen <tss@iki.fi>
parents:
16043
diff
changeset
|
883 return 0; |
12887
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
884 #else |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
885 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
|
886 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
|
887 #endif |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
888 } |
352999078d83
Added net_getunixcred() to get UNIX socket peer process's UID and GID.
Timo Sirainen <tss@iki.fi>
parents:
12782
diff
changeset
|
889 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
890 const char *net_ip2addr(const struct ip_addr *ip) |
0 | 891 { |
892 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
893 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
|
894 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
895 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
|
896 if (inet_ntop(ip->family, &ip->u.ip6, addr, MAX_IP_LEN) == NULL) |
16666
58be84a381e5
net_ip2addr() changed to return "" instead of NULL on failure.
Timo Sirainen <tss@iki.fi>
parents:
16647
diff
changeset
|
897 return ""; |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
898 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
899 return t_strdup(addr); |
0 | 900 #else |
901 unsigned long ip4; | |
902 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
903 if (ip->family != AF_INET) |
16666
58be84a381e5
net_ip2addr() changed to return "" instead of NULL on failure.
Timo Sirainen <tss@iki.fi>
parents:
16647
diff
changeset
|
904 return ""; |
0 | 905 |
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
|
906 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
|
907 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
|
908 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
909 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
910 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
911 (ip4 & 0x000000ff)); |
0 | 912 #endif |
913 } | |
914 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
915 int net_addr2ip(const char *addr, struct ip_addr *ip) |
0 | 916 { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
917 int ret; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
918 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
919 if (strchr(addr, ':') != NULL) { |
0 | 920 /* IPv6 */ |
921 #ifdef HAVE_IPV6 | |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
922 T_BEGIN { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
923 if (addr[0] == '[') { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
924 /* allow [ipv6 addr] */ |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
925 unsigned int len = strlen(addr); |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
926 if (addr[len-1] == ']') |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
927 addr = t_strndup(addr+1, len-2); |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
928 } |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
929 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
|
930 } T_END; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
931 if (ret == 0) |
0 | 932 return -1; |
933 #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
|
934 ip->u.ip4.s_addr = 0; |
0 | 935 #endif |
18500
4903ab967807
lib: net_addr2ip() sets the family now only after address is successfully converted.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
936 ip->family = AF_INET6; |
0 | 937 } else { |
938 /* IPv4 */ | |
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
|
939 if (inet_aton(addr, &ip->u.ip4) == 0) |
0 | 940 return -1; |
18500
4903ab967807
lib: net_addr2ip() sets the family now only after address is successfully converted.
Timo Sirainen <tss@iki.fi>
parents:
18137
diff
changeset
|
941 ip->family = AF_INET; |
0 | 942 } |
943 return 0; | |
944 } | |
945 | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
946 int net_str2port(const char *str, in_port_t *port_r) |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
947 { |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
948 uintmax_t l; |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
949 |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
950 if (str_to_uintmax(str, &l) < 0) |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
951 return -1; |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
952 |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
953 if (l == 0 || l > (in_port_t)-1) |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
954 return -1; |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
955 *port_r = (in_port_t)l; |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
956 return 0; |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
957 } |
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
958 |
19041
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
959 int net_str2port_zero(const char *str, in_port_t *port_r) |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
960 { |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
961 uintmax_t l; |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
962 |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
963 if (str_to_uintmax(str, &l) < 0) |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
964 return -1; |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
965 |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
966 if (l > (in_port_t)-1) |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
967 return -1; |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
968 *port_r = (in_port_t)l; |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
969 return 0; |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
970 } |
c53ccafc4a8e
Earlier in_port_t fix created problems with service listener configuration.
Stephan Bosch <stephan@rename-it.nl>
parents:
19035
diff
changeset
|
971 |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
972 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
|
973 struct ip_addr *dest) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
974 { |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
975 #ifdef HAVE_IPV6 |
4699
9e4cc68b71b2
Fix net_ipv6_mapped_ipv4_convert() to be actually portable.
Timo Sirainen <tss@iki.fi>
parents:
4684
diff
changeset
|
976 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
|
977 { 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
|
978 |
4684
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
979 if (!IPADDR_IS_V6(src)) |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
980 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
|
981 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
|
982 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
983 |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
984 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
|
985 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
|
986 return 0; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
987 #else |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
988 return -1; |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
989 #endif |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
990 } |
4f1df4b90500
Added net_ipv6_mapped_ipv4_convert().
Timo Sirainen <tss@iki.fi>
parents:
4018
diff
changeset
|
991 |
0 | 992 int net_geterror(int fd) |
993 { | |
994 int data; | |
995 socklen_t len = sizeof(data); | |
996 | |
17461
2e239d925c09
lib: Changed net_geterror() to return errno instead of -1 if getsockopt() fails.
Timo Sirainen <tss@iki.fi>
parents:
17459
diff
changeset
|
997 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) { |
2e239d925c09
lib: Changed net_geterror() to return errno instead of -1 if getsockopt() fails.
Timo Sirainen <tss@iki.fi>
parents:
17459
diff
changeset
|
998 /* we're now really returning the getsockopt()'s error code |
2e239d925c09
lib: Changed net_geterror() to return errno instead of -1 if getsockopt() fails.
Timo Sirainen <tss@iki.fi>
parents:
17459
diff
changeset
|
999 instead of the socket's, but normally we should never get |
2e239d925c09
lib: Changed net_geterror() to return errno instead of -1 if getsockopt() fails.
Timo Sirainen <tss@iki.fi>
parents:
17459
diff
changeset
|
1000 here anyway. */ |
2e239d925c09
lib: Changed net_geterror() to return errno instead of -1 if getsockopt() fails.
Timo Sirainen <tss@iki.fi>
parents:
17459
diff
changeset
|
1001 return errno; |
2e239d925c09
lib: Changed net_geterror() to return errno instead of -1 if getsockopt() fails.
Timo Sirainen <tss@iki.fi>
parents:
17459
diff
changeset
|
1002 } |
0 | 1003 |
1004 return data; | |
1005 } | |
1006 | |
1007 const char *net_gethosterror(int error) | |
1008 { | |
1009 #ifdef HAVE_IPV6 | |
1010 i_assert(error != 0); | |
1011 | |
1012 return gai_strerror(error); | |
1013 #else | |
1014 switch (error) { | |
1015 case HOST_NOT_FOUND: | |
1016 return "Host not found"; | |
1017 case NO_ADDRESS: | |
1018 return "No IP address found for name"; | |
1019 case NO_RECOVERY: | |
6666 | 1020 return "A non-recoverable name server error occurred"; |
0 | 1021 case TRY_AGAIN: |
1022 return "A temporary error on an authoritative name server"; | |
16647
dc60e46f0cb5
net_gethosterror(): Never return NULL.
Timo Sirainen <tss@iki.fi>
parents:
16575
diff
changeset
|
1023 default: |
dc60e46f0cb5
net_gethosterror(): Never return NULL.
Timo Sirainen <tss@iki.fi>
parents:
16575
diff
changeset
|
1024 return t_strdup_printf("Unknown error %d", error); |
0 | 1025 } |
1026 #endif | |
1027 } | |
1028 | |
1029 int net_hosterror_notfound(int error) | |
1030 { | |
1031 #ifdef HAVE_IPV6 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
1032 #ifdef EAI_NODATA /* NODATA is depricated */ |
0 | 1033 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); |
1034 #else | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
1035 return error != 1 && (error == EAI_NONAME); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
1036 #endif |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
1037 #else |
0 | 1038 return error == HOST_NOT_FOUND || error == NO_ADDRESS; |
1039 #endif | |
1040 } | |
1041 | |
19035
aabfe48db1cf
Changed type of internet port values to in_port_t everywhere.
Stephan Bosch <stephan@rename-it.nl>
parents:
18500
diff
changeset
|
1042 const char *net_getservbyport(in_port_t port) |
0 | 1043 { |
1044 struct servent *entry; | |
1045 | |
349 | 1046 entry = getservbyport(htons(port), "tcp"); |
0 | 1047 return entry == NULL ? NULL : entry->s_name; |
1048 } | |
1049 | |
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
|
1050 bool is_ipv4_address(const char *addr) |
0 | 1051 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
1052 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
1053 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
|
1054 return FALSE; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
1055 addr++; |
0 | 1056 } |
1057 | |
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
|
1058 return TRUE; |
0 | 1059 } |
1060 | |
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
|
1061 bool is_ipv6_address(const char *addr) |
0 | 1062 { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1063 bool have_prefix = FALSE; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1064 |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1065 if (*addr == '[') { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1066 have_prefix = TRUE; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1067 addr++; |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1068 } |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
1069 while (*addr != '\0') { |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1070 if (*addr != ':' && !i_isxdigit(*addr)) { |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1071 if (have_prefix && *addr == ']' && addr[1] == '\0') |
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1072 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
|
1073 return FALSE; |
11448
bdb46ba87203
Added support for [] around ipv6 addresses everywhere.
Timo Sirainen <tss@iki.fi>
parents:
11340
diff
changeset
|
1074 } |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
1075 addr++; |
0 | 1076 } |
1077 | |
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
|
1078 return TRUE; |
0 | 1079 } |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1080 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1081 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
|
1082 unsigned int *bits_r) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1083 { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1084 const char *p; |
11086
260e190306b0
Started using str_to_*() functions instead of libc's ones.
Timo Sirainen <tss@iki.fi>
parents:
10789
diff
changeset
|
1085 unsigned int bits, max_bits; |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1086 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1087 p = strchr(network, '/'); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1088 if (p != NULL) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1089 network = t_strdup_until(network, p++); |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1090 |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1091 if (net_addr2ip(network, ip_r) < 0) |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1092 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1093 |
13290
d473660bc54d
liblib: Added IPADDR_BITS() macro.
Timo Sirainen <tss@iki.fi>
parents:
12887
diff
changeset
|
1094 max_bits = IPADDR_BITS(ip_r); |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1095 if (p == NULL) { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1096 /* full IP address must match */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1097 bits = max_bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1098 } else { |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1099 /* 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
|
1100 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
|
1101 return -1; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1102 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1103 *bits_r = bits; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1104 return 0; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1105 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1106 |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1107 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
|
1108 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
|
1109 { |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1110 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
|
1111 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
|
1112 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
|
1113 unsigned int pos, i; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1114 |
7919
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1115 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
|
1116 /* IPv4 address mapped disguised as IPv6 address */ |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1117 ip = &tmp_ip; |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1118 } |
423b8e3fedbb
Created net_parse_range() from auth code.
Timo Sirainen <tss@iki.fi>
parents:
7520
diff
changeset
|
1119 |
16043
790bb5dfadc6
net_is_in_network(): Don't assert-crash with invalid IP.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
1120 if (ip->family == 0) { |
790bb5dfadc6
net_is_in_network(): Don't assert-crash with invalid IP.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
1121 /* non-IPv4/IPv6 address (e.g. UNIX socket) never matches |
790bb5dfadc6
net_is_in_network(): Don't assert-crash with invalid IP.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
1122 anything */ |
790bb5dfadc6
net_is_in_network(): Don't assert-crash with invalid IP.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
1123 return FALSE; |
790bb5dfadc6
net_is_in_network(): Don't assert-crash with invalid IP.
Timo Sirainen <tss@iki.fi>
parents:
15715
diff
changeset
|
1124 } |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1125 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
|
1126 /* 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
|
1127 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1128 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1129 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
|
1130 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1131 if (IPADDR_IS_V4(ip)) { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1132 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
|
1133 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
|
1134 } else { |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1135 #ifdef HAVE_IPV6 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1136 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
|
1137 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
|
1138 #else |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1139 /* shouldn't get here */ |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1140 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1141 #endif |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1142 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1143 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1144 /* 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
|
1145 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
|
1146 if (ip1[i] != ip2[i]) |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1147 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1148 } |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
1149 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
|
1150 i2 = htonl(ip2[i]); |
7517
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1151 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1152 /* 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
|
1153 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
|
1154 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
|
1155 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1156 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1157 |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1158 /* 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
|
1159 bits -= pos; |
7520
85203e817f90
net_is_in_network(): Fixed to work with big endian machines.
Timo Sirainen <tss@iki.fi>
parents:
7517
diff
changeset
|
1160 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
|
1161 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
|
1162 return FALSE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1163 } |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1164 return TRUE; |
e0007da64cdc
Moved net_is_in_network() to lib/network.c.
Timo Sirainen <tss@iki.fi>
parents:
7274
diff
changeset
|
1165 } |