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