Mercurial > dovecot > core-2.2
annotate src/lib/network.c @ 3545:0b6539bc22b9 HEAD
Don't log extra bind() errors for net_listen_unix() either
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Thu, 18 Aug 2005 20:27:04 +0300 |
parents | 5ff3b88db948 |
children | 51d30414b8bd |
rev | line source |
---|---|
1741
9df02b1533b3
Removed most of the license comments from src/lib/*.c. It's just fine to
Timo Sirainen <tss@iki.fi>
parents:
1564
diff
changeset
|
1 /* Copyright (c) 1999-2003 Timo Sirainen */ |
0 | 2 |
3 #include "lib.h" | |
4 #include "network.h" | |
5 | |
6 #include <unistd.h> | |
7 #include <fcntl.h> | |
8 #include <ctype.h> | |
9 #include <sys/un.h> | |
10 #include <netinet/tcp.h> | |
11 | |
12 union sockaddr_union { | |
13 struct sockaddr sa; | |
14 struct sockaddr_in sin; | |
15 #ifdef HAVE_IPV6 | |
16 struct sockaddr_in6 sin6; | |
17 #endif | |
18 }; | |
19 | |
20 #ifdef HAVE_IPV6 | |
21 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \ | |
22 sizeof(so.sin6) : sizeof(so.sin)) | |
23 #else | |
24 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin)) | |
25 #endif | |
26 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
27 int net_ip_compare(const struct ip_addr *ip1, const struct ip_addr *ip2) |
0 | 28 { |
29 if (ip1->family != ip2->family) | |
30 return 0; | |
31 | |
32 #ifdef HAVE_IPV6 | |
33 if (ip1->family == AF_INET6) | |
34 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0; | |
35 #endif | |
36 | |
37 return memcmp(&ip1->ip, &ip2->ip, 4) == 0; | |
38 } | |
39 | |
40 | |
41 /* 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
|
42 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
|
43 sin_set_ip(union sockaddr_union *so, const struct ip_addr *ip) |
0 | 44 { |
45 if (ip == NULL) { | |
46 #ifdef HAVE_IPV6 | |
47 so->sin6.sin6_family = AF_INET6; | |
48 so->sin6.sin6_addr = in6addr_any; | |
49 #else | |
50 so->sin.sin_family = AF_INET; | |
51 so->sin.sin_addr.s_addr = INADDR_ANY; | |
52 #endif | |
53 return; | |
54 } | |
55 | |
56 so->sin.sin_family = ip->family; | |
57 #ifdef HAVE_IPV6 | |
58 if (ip->family == AF_INET6) | |
59 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip)); | |
60 else | |
61 #endif | |
62 memcpy(&so->sin.sin_addr, &ip->ip, 4); | |
63 } | |
64 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
65 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
|
66 sin_get_ip(const union sockaddr_union *so, struct ip_addr *ip) |
0 | 67 { |
68 ip->family = so->sin.sin_family; | |
69 | |
70 #ifdef HAVE_IPV6 | |
71 if (ip->family == AF_INET6) | |
72 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip)); | |
73 else | |
74 #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
|
75 if (ip->family == AF_INET) |
0 | 76 memcpy(&ip->ip, &so->sin.sin_addr, 4); |
3400
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
77 else |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
78 memset(&ip->ip, 0, sizeof(ip->ip)); |
0 | 79 } |
80 | |
349 | 81 static inline void sin_set_port(union sockaddr_union *so, unsigned int port) |
0 | 82 { |
83 #ifdef HAVE_IPV6 | |
84 if (so->sin.sin_family == AF_INET6) | |
85 so->sin6.sin6_port = htons((unsigned short) port); | |
86 else | |
87 #endif | |
88 so->sin.sin_port = htons((unsigned short) port); | |
89 } | |
90 | |
349 | 91 static inline unsigned int sin_get_port(union sockaddr_union *so) |
0 | 92 { |
93 #ifdef HAVE_IPV6 | |
94 if (so->sin.sin_family == AF_INET6) | |
95 return ntohs(so->sin6.sin6_port); | |
96 #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
|
97 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
|
98 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
|
99 |
ddfa507bb74f
Don't return random data if net_getpeername() or net_getsockname() is used
Timo Sirainen <tss@iki.fi>
parents:
3373
diff
changeset
|
100 return 0; |
0 | 101 } |
102 | |
103 static inline void close_save_errno(int fd) | |
104 { | |
105 int old_errno = errno; | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
106 (void)close(fd); |
0 | 107 errno = old_errno; |
108 } | |
109 | |
110 /* Connect to socket with ip address */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
111 int net_connect_ip(const struct ip_addr *ip, unsigned int port, |
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
112 const struct ip_addr *my_ip) |
0 | 113 { |
114 union sockaddr_union so; | |
115 int fd, ret, opt = 1; | |
116 | |
117 if (my_ip != NULL && ip->family != my_ip->family) { | |
118 i_warning("net_connect_ip(): ip->family != my_ip->family"); | |
119 my_ip = NULL; | |
120 } | |
121 | |
122 /* create the socket */ | |
123 memset(&so, 0, sizeof(so)); | |
124 so.sin.sin_family = ip->family; | |
125 fd = socket(ip->family, SOCK_STREAM, 0); | |
126 | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
127 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
|
128 i_error("socket() failed: %m"); |
0 | 129 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
|
130 } |
0 | 131 |
132 /* set socket options */ | |
133 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
134 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
2639
a307ddd22a3a
net_connect() should be nonblocking (currently unused in Dovecot)
Timo Sirainen <tss@iki.fi>
parents:
2113
diff
changeset
|
135 net_set_nonblock(fd, TRUE); |
0 | 136 |
137 /* set our own address */ | |
138 if (my_ip != NULL) { | |
139 sin_set_ip(&so, my_ip); | |
140 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) { | |
141 /* failed, set it back to INADDR_ANY */ | |
3523
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
142 i_error("bind(%s) failed: %m", net_ip2addr(my_ip)); |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
143 close_save_errno(fd); |
d1ed3b3548db
Added some more error messages. Also if net_connect_ip() gives my_ip
Timo Sirainen <tss@iki.fi>
parents:
3400
diff
changeset
|
144 return -1; |
0 | 145 } |
146 } | |
147 | |
148 /* connect */ | |
149 sin_set_ip(&so, ip); | |
150 sin_set_port(&so, port); | |
151 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so)); | |
152 | |
153 #ifndef WIN32 | |
154 if (ret < 0 && errno != EINPROGRESS) | |
155 #else | |
156 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) | |
157 #endif | |
158 { | |
159 close_save_errno(fd); | |
160 return -1; | |
161 } | |
162 | |
163 return fd; | |
164 } | |
165 | |
166 int net_connect_unix(const char *path) | |
167 { | |
168 struct sockaddr_un sa; | |
169 int fd, ret; | |
170 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
171 memset(&sa, 0, sizeof(sa)); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
172 sa.sun_family = AF_UNIX; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
173 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 174 /* too long path */ |
175 errno = EINVAL; | |
176 return -1; | |
177 } | |
178 | |
179 /* create the socket */ | |
180 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
|
181 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
|
182 i_error("socket(%s) failed: %m", path); |
0 | 183 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
|
184 } |
0 | 185 |
3076
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
186 net_set_nonblock(fd, TRUE); |
8d15fea729c2
Use nonblocking connecting for unix sockets.
Timo Sirainen <tss@iki.fi>
parents:
3075
diff
changeset
|
187 |
0 | 188 /* connect */ |
189 ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa)); | |
190 if (ret < 0 && errno != EINPROGRESS) { | |
191 close_save_errno(fd); | |
192 return -1; | |
193 } | |
194 | |
195 return fd; | |
196 } | |
197 | |
198 /* Disconnect socket */ | |
199 void net_disconnect(int fd) | |
200 { | |
388
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
201 if (close(fd) < 0) |
792fc5b3daa4
Send error message if close() fails in net_disconnect().
Timo Sirainen <tss@iki.fi>
parents:
349
diff
changeset
|
202 i_error("net_disconnect() failed: %m"); |
0 | 203 } |
204 | |
205 /* Set socket blocking/nonblocking */ | |
34
a3d77e73f99b
fixed compile warnings with some systems
Timo Sirainen <tss@iki.fi>
parents:
0
diff
changeset
|
206 void net_set_nonblock(int fd __attr_unused__, int nonblock __attr_unused__) |
0 | 207 { |
208 #ifdef HAVE_FCNTL | |
1871
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
209 int flags; |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
210 |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
211 flags = fcntl(fd, F_GETFL, 0); |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
212 if (flags == -1) |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
213 i_fatal("net_set_nonblock() failed: %m"); |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
214 |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
215 if (nonblock) |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
216 flags |= O_NONBLOCK; |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
217 else |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
218 flags &= ~O_NONBLOCK; |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
219 |
638944ab7753
net_set_nonblock(): don't replace flags in fd, change the existing ones
Timo Sirainen <tss@iki.fi>
parents:
1741
diff
changeset
|
220 if (fcntl(fd, F_SETFL, flags) < 0) |
628 | 221 i_fatal("net_set_nonblock() failed: %m"); |
0 | 222 #endif |
223 } | |
224 | |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
225 int net_set_cork(int fd __attr_unused__, int cork __attr_unused__) |
0 | 226 { |
227 #ifdef TCP_CORK | |
410
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
228 return setsockopt(fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork)); |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
229 #else |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
230 errno = ENOPROTOOPT; |
1f0e7229ee58
Split IOBuffer into mmaped IBuffer, file IBuffer, memory data IBuffer and
Timo Sirainen <tss@iki.fi>
parents:
404
diff
changeset
|
231 return -1; |
0 | 232 #endif |
233 } | |
234 | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
235 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
|
236 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
237 struct in_addr *in_ip = (struct in_addr *) &ip->ip; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
238 |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
239 ip->family = AF_INET; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
240 in_ip->s_addr = INADDR_ANY; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
241 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
242 |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
243 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
|
244 { |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
245 #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
|
246 ip->family = AF_INET6; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
247 ip->ip = in6addr_any; |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
248 #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
|
249 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
|
250 #endif |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
251 } |
1cc947617c8b
imap_listen and imaps_listen accepts now "*" as "all IPv4 interfaces" and
Timo Sirainen <tss@iki.fi>
parents:
680
diff
changeset
|
252 |
0 | 253 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any |
254 address. */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
255 int net_listen(const struct ip_addr *my_ip, unsigned int *port, int backlog) |
0 | 256 { |
257 union sockaddr_union so; | |
258 int ret, fd, opt = 1; | |
259 socklen_t len; | |
260 | |
261 i_assert(port != NULL); | |
262 | |
263 memset(&so, 0, sizeof(so)); | |
264 sin_set_port(&so, *port); | |
265 sin_set_ip(&so, my_ip); | |
266 | |
267 /* create the socket */ | |
268 fd = socket(so.sin.sin_family, SOCK_STREAM, 0); | |
269 #ifdef HAVE_IPV6 | |
270 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) { | |
271 /* IPv6 is not supported by OS */ | |
272 so.sin.sin_family = AF_INET; | |
273 so.sin.sin_addr.s_addr = INADDR_ANY; | |
274 | |
275 fd = socket(AF_INET, SOCK_STREAM, 0); | |
276 } | |
277 #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
|
278 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
|
279 i_error("socket() failed: %m"); |
0 | 280 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
|
281 } |
0 | 282 |
283 /* set socket options */ | |
284 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); | |
285 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt)); | |
286 | |
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
|
287 /* if using IPv6, bind both on the IPv4 and IPv6 addresses */ |
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
|
288 #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
|
289 if (so.sin.sin_family == AF_INET6) { |
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
|
290 opt = 0; |
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
|
291 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt)); |
690dcc51cdc1
if using IPv6, bind both on the IPv4 and IPv6 addresses if IPV6_V6ONLY is
Timo Sirainen <tss@iki.fi>
parents:
3076
diff
changeset
|
292 } |
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
|
293 #endif |
0 | 294 /* specify the address/port we want to listen in */ |
295 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
|
296 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
|
297 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
|
298 i_error("bind(%s, %u) failed: %m", |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
299 net_ip2addr(my_ip), *port); |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
300 } |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
301 } else { |
0 | 302 /* get the actual port we started listen */ |
303 len = SIZEOF_SOCKADDR(so); | |
304 ret = getsockname(fd, &so.sa, &len); | |
305 if (ret >= 0) { | |
306 *port = sin_get_port(&so); | |
307 | |
308 /* start listening */ | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
309 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
|
310 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
|
311 |
5ff3b88db948
net_listen(): Print bind() errors only if it's not EADDRINUSE. Also do the
Timo Sirainen <tss@iki.fi>
parents:
3523
diff
changeset
|
312 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
|
313 i_error("listen() failed: %m"); |
0 | 314 } |
315 } | |
316 | |
317 /* error */ | |
318 close_save_errno(fd); | |
319 return -1; | |
320 } | |
321 | |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
322 int net_listen_unix(const char *path, int backlog) |
0 | 323 { |
324 struct sockaddr_un sa; | |
325 int fd; | |
326 | |
807
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
327 memset(&sa, 0, sizeof(sa)); |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
328 sa.sun_family = AF_UNIX; |
35abd7a5d381
Buffer related cleanups. Use PATH_MAX instead of hardcoded 1024 for paths.
Timo Sirainen <tss@iki.fi>
parents:
805
diff
changeset
|
329 if (strocpy(sa.sun_path, path, sizeof(sa.sun_path)) < 0) { |
0 | 330 /* too long path */ |
331 errno = EINVAL; | |
332 return -1; | |
333 } | |
334 | |
335 /* create the socket */ | |
336 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
|
337 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
|
338 i_error("socket() failed: %m"); |
0 | 339 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
|
340 } |
0 | 341 |
342 /* bind */ | |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
343 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) < 0) { |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
344 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
345 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
|
346 } else { |
0 | 347 /* start listening */ |
3075
9cb91ed5a110
Added backlog parameter for net_listen*().
Timo Sirainen <tss@iki.fi>
parents:
2639
diff
changeset
|
348 if (listen(fd, backlog) == 0) |
0 | 349 return fd; |
3545
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
350 |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
351 if (errno != EADDRINUSE) |
0b6539bc22b9
Don't log extra bind() errors for net_listen_unix() either
Timo Sirainen <tss@iki.fi>
parents:
3537
diff
changeset
|
352 i_error("listen() failed: %m"); |
0 | 353 } |
354 | |
355 close_save_errno(fd); | |
356 return -1; | |
357 } | |
358 | |
359 /* Accept a connection on a socket */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
360 int net_accept(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 361 { |
362 union sockaddr_union so; | |
363 int ret; | |
364 socklen_t addrlen; | |
365 | |
366 i_assert(fd >= 0); | |
367 | |
368 addrlen = sizeof(so); | |
369 ret = accept(fd, &so.sa, &addrlen); | |
370 | |
680
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
371 if (ret < 0) { |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
372 if (errno == EBADF || errno == ENOTSOCK || |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
373 errno == EOPNOTSUPP || errno == EFAULT || errno == EINVAL) |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
374 return -2; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
375 else |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
376 return -1; |
84e398270f7f
net_accept() returns now -2 for fatal failures.
Timo Sirainen <tss@iki.fi>
parents:
628
diff
changeset
|
377 } |
0 | 378 |
379 if (addr != NULL) sin_get_ip(&so, addr); | |
380 if (port != NULL) *port = sin_get_port(&so); | |
381 | |
382 return ret; | |
383 } | |
384 | |
385 /* Read data from socket, return number of bytes read, -1 = error */ | |
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
|
386 ssize_t net_receive(int fd, void *buf, size_t len) |
0 | 387 { |
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
|
388 ssize_t ret; |
0 | 389 |
390 i_assert(fd >= 0); | |
391 i_assert(buf != NULL); | |
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
|
392 i_assert(len <= SSIZE_T_MAX); |
0 | 393 |
394 ret = recv(fd, buf, len, 0); | |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
395 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
|
396 /* disconnected */ |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
397 errno = 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
398 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
399 } |
0 | 400 |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
401 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
|
402 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
|
403 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
404 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
405 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
|
406 /* 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
|
407 return -2; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
408 } |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
409 } |
0 | 410 |
411 return ret; | |
412 } | |
413 | |
414 /* Transmit data, return number of bytes sent, -1 = error */ | |
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
|
415 ssize_t net_transmit(int fd, const void *data, size_t len) |
0 | 416 { |
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
|
417 ssize_t ret; |
0 | 418 |
419 i_assert(fd >= 0); | |
420 i_assert(data != NULL); | |
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
|
421 i_assert(len <= SSIZE_T_MAX); |
0 | 422 |
64
83ae914a583a
added t_strdup_noconst() which can be used instead of (char *) t_strdup().
Timo Sirainen <tss@iki.fi>
parents:
34
diff
changeset
|
423 ret = send(fd, data, len, 0); |
781
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
424 if (ret == -1 && (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
|
425 return 0; |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
426 |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
427 if (errno == EPIPE) |
9cb7022749e7
net_receive, net_transmit: Return -2 for regular disconnection errors. Don't
Timo Sirainen <tss@iki.fi>
parents:
780
diff
changeset
|
428 return -2; |
0 | 429 |
430 return ret; | |
431 } | |
432 | |
433 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need | |
434 to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
435 int net_gethostbyname(const char *addr, struct ip_addr **ips, int *ips_count) |
0 | 436 { |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
437 /* @UNSAFE */ |
0 | 438 #ifdef HAVE_IPV6 |
439 union sockaddr_union *so; | |
440 struct addrinfo hints, *ai, *origai; | |
441 char hbuf[NI_MAXHOST]; | |
442 int host_error; | |
443 #else | |
444 struct hostent *hp; | |
445 #endif | |
446 int count; | |
447 | |
448 i_assert(addr != NULL); | |
449 i_assert(ips != NULL); | |
450 i_assert(ips_count != NULL); | |
451 | |
452 *ips = NULL; | |
453 *ips_count = 0; | |
454 | |
455 #ifdef HAVE_IPV6 | |
456 memset(&hints, 0, sizeof(struct addrinfo)); | |
457 hints.ai_socktype = SOCK_STREAM; | |
458 | |
459 /* save error to host_error for later use */ | |
460 host_error = getaddrinfo(addr, NULL, &hints, &ai); | |
461 if (host_error != 0) | |
462 return host_error; | |
463 | |
464 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, | |
465 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0) | |
466 return 1; | |
467 | |
468 | |
469 /* get number of IPs */ | |
470 origai = ai; | |
471 for (count = 0; ai != NULL; ai = ai->ai_next) | |
472 count++; | |
473 | |
474 *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
|
475 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 476 |
477 count = 0; | |
478 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) { | |
479 so = (union sockaddr_union *) ai->ai_addr; | |
480 | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
481 sin_get_ip(so, &(*ips)[count]); |
0 | 482 } |
483 freeaddrinfo(origai); | |
484 #else | |
485 hp = gethostbyname(addr); | |
486 if (hp == NULL) | |
487 return h_errno; | |
488 | |
489 /* get number of IPs */ | |
490 count = 0; | |
491 while (hp->h_addr_list[count] != NULL) | |
492 count++; | |
493 | |
494 *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
|
495 *ips = t_malloc(sizeof(struct ip_addr) * count); |
0 | 496 |
497 while (count > 0) { | |
498 count--; | |
499 | |
500 (*ips)[count].family = AF_INET; | |
501 memcpy(&(*ips)[count].ip, hp->h_addr_list[count], 4); | |
502 } | |
503 #endif | |
504 | |
505 return 0; | |
506 } | |
507 | |
508 /* Get socket address/port */ | |
903
fd8888f6f037
Naming style changes, finally got tired of most of the typedefs. Also the
Timo Sirainen <tss@iki.fi>
parents:
807
diff
changeset
|
509 int net_getsockname(int fd, struct ip_addr *addr, unsigned int *port) |
0 | 510 { |
511 union sockaddr_union so; | |
512 socklen_t addrlen; | |
513 | |
514 i_assert(fd >= 0); | |
515 | |
516 addrlen = sizeof(so); | |
517 if (getsockname(fd, (struct sockaddr *) &so, &addrlen) == -1) | |
518 return -1; | |
519 | |
520 if (addr != NULL) sin_get_ip(&so, addr); | |
521 if (port != NULL) *port = sin_get_port(&so); | |
522 | |
523 return 0; | |
524 } | |
525 | |
2098 | 526 int net_getpeername(int fd, struct ip_addr *addr, unsigned int *port) |
527 { | |
528 union sockaddr_union so; | |
529 socklen_t addrlen; | |
530 | |
531 i_assert(fd >= 0); | |
532 | |
533 addrlen = sizeof(so); | |
534 if (getpeername(fd, (struct sockaddr *) &so, &addrlen) == -1) | |
535 return -1; | |
536 | |
537 if (addr != NULL) sin_get_ip(&so, addr); | |
538 if (port != NULL) *port = sin_get_port(&so); | |
539 | |
540 return 0; | |
541 } | |
542 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
543 const char *net_ip2addr(const struct ip_addr *ip) |
0 | 544 { |
545 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
546 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
|
547 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
548 addr[MAX_IP_LEN] = '\0'; |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
549 if (inet_ntop(ip->family, &ip->ip, addr, MAX_IP_LEN) == NULL) |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
550 return NULL; |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
551 |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
552 return t_strdup(addr); |
0 | 553 #else |
554 unsigned long ip4; | |
555 | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
556 if (ip->family != AF_INET) |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
557 return NULL; |
0 | 558 |
559 ip4 = ntohl(ip->ip.s_addr); | |
805
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
560 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
|
561 (ip4 & 0xff000000UL) >> 24, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
562 (ip4 & 0x00ff0000) >> 16, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
563 (ip4 & 0x0000ff00) >> 8, |
5ac361acb316
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
Timo Sirainen <tss@iki.fi>
parents:
781
diff
changeset
|
564 (ip4 & 0x000000ff)); |
0 | 565 #endif |
566 } | |
567 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
568 int net_addr2ip(const char *addr, struct ip_addr *ip) |
0 | 569 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
570 if (strchr(addr, ':') != NULL) { |
0 | 571 /* IPv6 */ |
572 ip->family = AF_INET6; | |
573 #ifdef HAVE_IPV6 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
574 if (inet_pton(AF_INET6, addr, &ip->ip) == 0) |
0 | 575 return -1; |
576 #else | |
577 ip->ip.s_addr = 0; | |
578 #endif | |
579 } else { | |
580 /* IPv4 */ | |
581 ip->family = AF_INET; | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
582 if (inet_aton(addr, (struct in_addr *) &ip->ip) == 0) |
0 | 583 return -1; |
584 } | |
585 | |
586 return 0; | |
587 } | |
588 | |
589 /* Get socket error */ | |
590 int net_geterror(int fd) | |
591 { | |
592 int data; | |
593 socklen_t len = sizeof(data); | |
594 | |
595 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1) | |
596 return -1; | |
597 | |
598 return data; | |
599 } | |
600 | |
601 /* get error of net_gethostname() */ | |
602 const char *net_gethosterror(int error) | |
603 { | |
604 #ifdef HAVE_IPV6 | |
605 i_assert(error != 0); | |
606 | |
607 if (error == 1) { | |
608 /* getnameinfo() failed */ | |
609 return strerror(errno); | |
610 } | |
611 | |
612 return gai_strerror(error); | |
613 #else | |
614 switch (error) { | |
615 case HOST_NOT_FOUND: | |
616 return "Host not found"; | |
617 case NO_ADDRESS: | |
618 return "No IP address found for name"; | |
619 case NO_RECOVERY: | |
620 return "A non-recovable name server error occurred"; | |
621 case TRY_AGAIN: | |
622 return "A temporary error on an authoritative name server"; | |
623 } | |
624 | |
625 /* unknown error */ | |
626 return NULL; | |
627 #endif | |
628 } | |
629 | |
630 /* return TRUE if host lookup failed because it didn't exist (ie. not | |
631 some error with name server) */ | |
632 int net_hosterror_notfound(int error) | |
633 { | |
634 #ifdef HAVE_IPV6 | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
635 #ifdef EAI_NODATA /* NODATA is depricated */ |
0 | 636 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA); |
637 #else | |
1915
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
638 return error != 1 && (error == EAI_NONAME); |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
639 #endif |
79790750c349
importing new index code. mbox still broken.
Timo Sirainen <tss@iki.fi>
parents:
1871
diff
changeset
|
640 #else |
0 | 641 return error == HOST_NOT_FOUND || error == NO_ADDRESS; |
642 #endif | |
643 } | |
644 | |
645 /* Get name of TCP service */ | |
2113
c5817f302aa6
net_gethostbyname() was broken with IPv6 enabled (wasn't really used with
Timo Sirainen <tss@iki.fi>
parents:
2098
diff
changeset
|
646 const char *net_getservbyport(unsigned short port) |
0 | 647 { |
648 struct servent *entry; | |
649 | |
349 | 650 entry = getservbyport(htons(port), "tcp"); |
0 | 651 return entry == NULL ? NULL : entry->s_name; |
652 } | |
653 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
654 int is_ipv4_address(const char *addr) |
0 | 655 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
656 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
657 if (*addr != '.' && !i_isdigit(*addr)) |
0 | 658 return 0; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
659 addr++; |
0 | 660 } |
661 | |
662 return 1; | |
663 } | |
664 | |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
665 int is_ipv6_address(const char *addr) |
0 | 666 { |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
667 while (*addr != '\0') { |
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
668 if (*addr != ':' && !i_isxdigit(*addr)) |
0 | 669 return 0; |
1485
8c28289a15a1
s/host/addr/ in a few network functions
Timo Sirainen <tss@iki.fi>
parents:
1268
diff
changeset
|
670 addr++; |
0 | 671 } |
672 | |
673 return 1; | |
674 } |