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