0
|
1 /*
|
|
2 network.c : Network stuff with IPv6 support
|
|
3
|
|
4 Copyright (c) 1999-2002 Timo Sirainen
|
|
5
|
|
6 Permission is hereby granted, free of charge, to any person obtaining
|
|
7 a copy of this software and associated documentation files (the
|
|
8 "Software"), to deal in the Software without restriction, including
|
|
9 without limitation the rights to use, copy, modify, merge, publish,
|
|
10 distribute, sublicense, and/or sell copies of the Software, and to
|
|
11 permit persons to whom the Software is furnished to do so, subject to
|
|
12 the following conditions:
|
|
13
|
|
14 The above copyright notice and this permission notice shall be
|
|
15 included in all copies or substantial portions of the Software.
|
|
16
|
|
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
18 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
20 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
21 CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
22 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
23 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
24 */
|
|
25
|
|
26 #include "lib.h"
|
|
27 #include "network.h"
|
|
28
|
|
29 #include <unistd.h>
|
|
30 #include <fcntl.h>
|
|
31 #include <ctype.h>
|
|
32 #include <sys/un.h>
|
|
33 #include <netinet/tcp.h>
|
|
34
|
|
35 #define LISTEN_BACKLOG 8
|
|
36
|
|
37 union sockaddr_union {
|
|
38 struct sockaddr sa;
|
|
39 struct sockaddr_in sin;
|
|
40 #ifdef HAVE_IPV6
|
|
41 struct sockaddr_in6 sin6;
|
|
42 #endif
|
|
43 };
|
|
44
|
|
45 #ifdef HAVE_IPV6
|
|
46 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
|
|
47 sizeof(so.sin6) : sizeof(so.sin))
|
|
48 #else
|
|
49 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
|
|
50 #endif
|
|
51
|
|
52 int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
|
|
53 {
|
|
54 if (ip1->family != ip2->family)
|
|
55 return 0;
|
|
56
|
|
57 #ifdef HAVE_IPV6
|
|
58 if (ip1->family == AF_INET6)
|
|
59 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0;
|
|
60 #endif
|
|
61
|
|
62 return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
|
|
63 }
|
|
64
|
|
65
|
|
66 /* copy IP to sockaddr */
|
|
67 static inline void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
|
|
68 {
|
|
69 if (ip == NULL) {
|
|
70 #ifdef HAVE_IPV6
|
|
71 so->sin6.sin6_family = AF_INET6;
|
|
72 so->sin6.sin6_addr = in6addr_any;
|
|
73 #else
|
|
74 so->sin.sin_family = AF_INET;
|
|
75 so->sin.sin_addr.s_addr = INADDR_ANY;
|
|
76 #endif
|
|
77 return;
|
|
78 }
|
|
79
|
|
80 so->sin.sin_family = ip->family;
|
|
81 #ifdef HAVE_IPV6
|
|
82 if (ip->family == AF_INET6)
|
|
83 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
|
|
84 else
|
|
85 #endif
|
|
86 memcpy(&so->sin.sin_addr, &ip->ip, 4);
|
|
87 }
|
|
88
|
|
89 static inline void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
|
|
90 {
|
|
91 ip->family = so->sin.sin_family;
|
|
92
|
|
93 #ifdef HAVE_IPV6
|
|
94 if (ip->family == AF_INET6)
|
|
95 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
|
|
96 else
|
|
97 #endif
|
|
98 memcpy(&ip->ip, &so->sin.sin_addr, 4);
|
|
99 }
|
|
100
|
|
101 static inline void sin_set_port(union sockaddr_union *so, int port)
|
|
102 {
|
|
103 #ifdef HAVE_IPV6
|
|
104 if (so->sin.sin_family == AF_INET6)
|
|
105 so->sin6.sin6_port = htons((unsigned short) port);
|
|
106 else
|
|
107 #endif
|
|
108 so->sin.sin_port = htons((unsigned short) port);
|
|
109 }
|
|
110
|
|
111 static inline int sin_get_port(union sockaddr_union *so)
|
|
112 {
|
|
113 #ifdef HAVE_IPV6
|
|
114 if (so->sin.sin_family == AF_INET6)
|
|
115 return ntohs(so->sin6.sin6_port);
|
|
116 #endif
|
|
117 return ntohs(so->sin.sin_port);
|
|
118 }
|
|
119
|
|
120 static inline void close_save_errno(int fd)
|
|
121 {
|
|
122 int old_errno = errno;
|
|
123 close(fd);
|
|
124 errno = old_errno;
|
|
125 }
|
|
126
|
|
127 /* Connect to socket with ip address */
|
|
128 int net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
|
|
129 {
|
|
130 union sockaddr_union so;
|
|
131 int fd, ret, opt = 1;
|
|
132
|
|
133 if (my_ip != NULL && ip->family != my_ip->family) {
|
|
134 i_warning("net_connect_ip(): ip->family != my_ip->family");
|
|
135 my_ip = NULL;
|
|
136 }
|
|
137
|
|
138 /* create the socket */
|
|
139 memset(&so, 0, sizeof(so));
|
|
140 so.sin.sin_family = ip->family;
|
|
141 fd = socket(ip->family, SOCK_STREAM, 0);
|
|
142
|
|
143 if (fd == -1)
|
|
144 return -1;
|
|
145
|
|
146 /* set socket options */
|
|
147 net_set_nonblock(fd, TRUE);
|
|
148 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
149 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
|
|
150
|
|
151 /* set our own address */
|
|
152 if (my_ip != NULL) {
|
|
153 sin_set_ip(&so, my_ip);
|
|
154 if (bind(fd, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
|
|
155 /* failed, set it back to INADDR_ANY */
|
|
156 sin_set_ip(&so, NULL);
|
|
157 bind(fd, &so.sa, SIZEOF_SOCKADDR(so));
|
|
158 }
|
|
159 }
|
|
160
|
|
161 /* connect */
|
|
162 sin_set_ip(&so, ip);
|
|
163 sin_set_port(&so, port);
|
|
164 ret = connect(fd, &so.sa, SIZEOF_SOCKADDR(so));
|
|
165
|
|
166 #ifndef WIN32
|
|
167 if (ret < 0 && errno != EINPROGRESS)
|
|
168 #else
|
|
169 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
|
|
170 #endif
|
|
171 {
|
|
172 close_save_errno(fd);
|
|
173 return -1;
|
|
174 }
|
|
175
|
|
176 return fd;
|
|
177 }
|
|
178
|
|
179 int net_connect_unix(const char *path)
|
|
180 {
|
|
181 struct sockaddr_un sa;
|
|
182 int fd, ret;
|
|
183
|
|
184 if (strlen(path) > sizeof(sa.sun_path)-1) {
|
|
185 /* too long path */
|
|
186 errno = EINVAL;
|
|
187 return -1;
|
|
188 }
|
|
189
|
|
190 /* create the socket */
|
|
191 fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
192 if (fd == -1)
|
|
193 return -1;
|
|
194
|
|
195 /* set socket options */
|
|
196 net_set_nonblock(fd, TRUE);
|
|
197
|
|
198 /* connect */
|
|
199 memset(&sa, 0, sizeof(sa));
|
|
200 sa.sun_family = AF_UNIX;
|
|
201 strcpy(sa.sun_path, path);
|
|
202
|
|
203 ret = connect(fd, (struct sockaddr *) &sa, sizeof(sa));
|
|
204 if (ret < 0 && errno != EINPROGRESS) {
|
|
205 close_save_errno(fd);
|
|
206 return -1;
|
|
207 }
|
|
208
|
|
209 return fd;
|
|
210 }
|
|
211
|
|
212 /* Disconnect socket */
|
|
213 void net_disconnect(int fd)
|
|
214 {
|
|
215 close(fd);
|
|
216 }
|
|
217
|
|
218 /* Set socket blocking/nonblocking */
|
|
219 void net_set_nonblock(int fd, int nonblock)
|
|
220 {
|
|
221 #ifdef HAVE_FCNTL
|
|
222 if (fcntl(fd, F_SETFL, nonblock ? O_NONBLOCK : 0) < 0)
|
|
223 i_fatal("net_send_nonblock() failed: %m");
|
|
224 #endif
|
|
225 }
|
|
226
|
|
227 void net_set_cork(int fd, int cork)
|
|
228 {
|
|
229 #ifdef TCP_CORK
|
|
230 setsockopt(fd, SOL_TCP, TCP_CORK, &cork, sizeof(cork));
|
|
231 #endif
|
|
232 }
|
|
233
|
|
234 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
|
|
235 address. */
|
|
236 int net_listen(IPADDR *my_ip, int *port)
|
|
237 {
|
|
238 union sockaddr_union so;
|
|
239 int ret, fd, opt = 1;
|
|
240 socklen_t len;
|
|
241
|
|
242 i_assert(port != NULL);
|
|
243
|
|
244 memset(&so, 0, sizeof(so));
|
|
245 sin_set_port(&so, *port);
|
|
246 sin_set_ip(&so, my_ip);
|
|
247
|
|
248 /* create the socket */
|
|
249 fd = socket(so.sin.sin_family, SOCK_STREAM, 0);
|
|
250 #ifdef HAVE_IPV6
|
|
251 if (fd == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) {
|
|
252 /* IPv6 is not supported by OS */
|
|
253 so.sin.sin_family = AF_INET;
|
|
254 so.sin.sin_addr.s_addr = INADDR_ANY;
|
|
255
|
|
256 fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
257 }
|
|
258 #endif
|
|
259 if (fd == -1)
|
|
260 return -1;
|
|
261
|
|
262 /* set socket options */
|
|
263 net_set_nonblock(fd, TRUE);
|
|
264 setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
265 setsockopt(fd, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
|
|
266
|
|
267 /* specify the address/port we want to listen in */
|
|
268 ret = bind(fd, &so.sa, SIZEOF_SOCKADDR(so));
|
|
269 if (ret >= 0) {
|
|
270 /* get the actual port we started listen */
|
|
271 len = SIZEOF_SOCKADDR(so);
|
|
272 ret = getsockname(fd, &so.sa, &len);
|
|
273 if (ret >= 0) {
|
|
274 *port = sin_get_port(&so);
|
|
275
|
|
276 /* start listening */
|
|
277 if (listen(fd, LISTEN_BACKLOG) >= 0)
|
|
278 return fd;
|
|
279 }
|
|
280
|
|
281 }
|
|
282
|
|
283 /* error */
|
|
284 close_save_errno(fd);
|
|
285 return -1;
|
|
286 }
|
|
287
|
|
288 int net_listen_unix(const char *path)
|
|
289 {
|
|
290 struct sockaddr_un sa;
|
|
291 int fd;
|
|
292
|
|
293 if (strlen(path) > sizeof(sa.sun_path)-1) {
|
|
294 /* too long path */
|
|
295 errno = EINVAL;
|
|
296 return -1;
|
|
297 }
|
|
298
|
|
299 /* create the socket */
|
|
300 fd = socket(PF_UNIX, SOCK_STREAM, 0);
|
|
301 if (fd == -1)
|
|
302 return -1;
|
|
303
|
|
304 /* set socket options */
|
|
305 net_set_nonblock(fd, TRUE);
|
|
306
|
|
307 /* bind */
|
|
308 memset(&sa, 0, sizeof(sa));
|
|
309 sa.sun_family = AF_UNIX;
|
|
310 strcpy(sa.sun_path, path);
|
|
311
|
|
312 if (bind(fd, (struct sockaddr *) &sa, sizeof(sa)) == 0) {
|
|
313 /* start listening */
|
|
314 if (listen(fd, LISTEN_BACKLOG) == 0)
|
|
315 return fd;
|
|
316 }
|
|
317
|
|
318 close_save_errno(fd);
|
|
319 return -1;
|
|
320 }
|
|
321
|
|
322 /* Accept a connection on a socket */
|
|
323 int net_accept(int fd, IPADDR *addr, int *port)
|
|
324 {
|
|
325 union sockaddr_union so;
|
|
326 int ret;
|
|
327 socklen_t addrlen;
|
|
328
|
|
329 i_assert(fd >= 0);
|
|
330
|
|
331 addrlen = sizeof(so);
|
|
332 ret = accept(fd, &so.sa, &addrlen);
|
|
333
|
|
334 if (ret < 0)
|
|
335 return -1;
|
|
336
|
|
337 if (addr != NULL) sin_get_ip(&so, addr);
|
|
338 if (port != NULL) *port = sin_get_port(&so);
|
|
339
|
|
340 net_set_nonblock(fd, TRUE);
|
|
341 return ret;
|
|
342 }
|
|
343
|
|
344 /* Read data from socket, return number of bytes read, -1 = error */
|
|
345 int net_receive(int fd, void *buf, unsigned int len)
|
|
346 {
|
|
347 int ret;
|
|
348
|
|
349 i_assert(fd >= 0);
|
|
350 i_assert(buf != NULL);
|
|
351 i_assert(len <= INT_MAX);
|
|
352
|
|
353 ret = recv(fd, buf, len, 0);
|
|
354 if (ret == 0)
|
|
355 return -1; /* disconnected */
|
|
356
|
|
357 if (ret < 0 && (errno == EINTR || errno == EAGAIN))
|
|
358 return 0;
|
|
359
|
|
360 return ret;
|
|
361 }
|
|
362
|
|
363 /* Transmit data, return number of bytes sent, -1 = error */
|
|
364 int net_transmit(int fd, const void *data, unsigned int len)
|
|
365 {
|
|
366 int ret;
|
|
367
|
|
368 i_assert(fd >= 0);
|
|
369 i_assert(data != NULL);
|
|
370 i_assert(len <= INT_MAX);
|
|
371
|
|
372 ret = send(fd, (void *) data, len, 0);
|
|
373 if (ret == -1 && (errno == EINTR || errno == EPIPE || errno == EAGAIN))
|
|
374 return 0;
|
|
375
|
|
376 return ret;
|
|
377 }
|
|
378
|
|
379 /* Get IP addresses for host. ips contains ips_count of IPs, they don't need
|
|
380 to be free'd. Returns 0 = ok, others = error code for net_gethosterror() */
|
|
381 int net_gethostbyname(const char *addr, IPADDR **ips, int *ips_count)
|
|
382 {
|
|
383 #ifdef HAVE_IPV6
|
|
384 union sockaddr_union *so;
|
|
385 struct addrinfo hints, *ai, *origai;
|
|
386 char hbuf[NI_MAXHOST];
|
|
387 int host_error;
|
|
388 #else
|
|
389 struct hostent *hp;
|
|
390 #endif
|
|
391 int count;
|
|
392
|
|
393 i_assert(addr != NULL);
|
|
394 i_assert(ips != NULL);
|
|
395 i_assert(ips_count != NULL);
|
|
396
|
|
397 *ips = NULL;
|
|
398 *ips_count = 0;
|
|
399
|
|
400 #ifdef HAVE_IPV6
|
|
401 memset(&hints, 0, sizeof(struct addrinfo));
|
|
402 hints.ai_socktype = SOCK_STREAM;
|
|
403
|
|
404 /* save error to host_error for later use */
|
|
405 host_error = getaddrinfo(addr, NULL, &hints, &ai);
|
|
406 if (host_error != 0)
|
|
407 return host_error;
|
|
408
|
|
409 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
|
|
410 sizeof(hbuf), NULL, 0, NI_NUMERICHOST) != 0)
|
|
411 return 1;
|
|
412
|
|
413
|
|
414 /* get number of IPs */
|
|
415 origai = ai;
|
|
416 for (count = 0; ai != NULL; ai = ai->ai_next)
|
|
417 count++;
|
|
418
|
|
419 *ips_count = count;
|
|
420 *ips = t_malloc(sizeof(IPADDR) * count);
|
|
421
|
|
422 count = 0;
|
|
423 for (ai = origai; ai != NULL; ai = ai->ai_next, count++) {
|
|
424 so = (union sockaddr_union *) ai->ai_addr;
|
|
425
|
|
426 sin_get_ip(so, ips[count]);
|
|
427 }
|
|
428 freeaddrinfo(origai);
|
|
429 #else
|
|
430 hp = gethostbyname(addr);
|
|
431 if (hp == NULL)
|
|
432 return h_errno;
|
|
433
|
|
434 /* get number of IPs */
|
|
435 count = 0;
|
|
436 while (hp->h_addr_list[count] != NULL)
|
|
437 count++;
|
|
438
|
|
439 *ips_count = count;
|
|
440 *ips = t_malloc(sizeof(IPADDR) * count);
|
|
441
|
|
442 while (count > 0) {
|
|
443 count--;
|
|
444
|
|
445 (*ips)[count].family = AF_INET;
|
|
446 memcpy(&(*ips)[count].ip, hp->h_addr_list[count], 4);
|
|
447 }
|
|
448 #endif
|
|
449
|
|
450 return 0;
|
|
451 }
|
|
452
|
|
453 /* Get socket address/port */
|
|
454 int net_getsockname(int fd, IPADDR *addr, int *port)
|
|
455 {
|
|
456 union sockaddr_union so;
|
|
457 socklen_t addrlen;
|
|
458
|
|
459 i_assert(fd >= 0);
|
|
460
|
|
461 addrlen = sizeof(so);
|
|
462 if (getsockname(fd, (struct sockaddr *) &so, &addrlen) == -1)
|
|
463 return -1;
|
|
464
|
|
465 if (addr != NULL) sin_get_ip(&so, addr);
|
|
466 if (port != NULL) *port = sin_get_port(&so);
|
|
467
|
|
468 return 0;
|
|
469 }
|
|
470
|
|
471 int net_ip2host(IPADDR *ip, char *host)
|
|
472 {
|
|
473 #ifdef HAVE_IPV6
|
|
474 if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
|
|
475 return -1;
|
|
476 #else
|
|
477 unsigned long ip4;
|
|
478
|
|
479 if (ip->family != AF_INET) {
|
|
480 strcpy(host, "0.0.0.0");
|
|
481 return -1;
|
|
482 }
|
|
483
|
|
484 ip4 = ntohl(ip->ip.s_addr);
|
|
485 i_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
|
|
486 (ip4 & 0xff000000UL) >> 24,
|
|
487 (ip4 & 0x00ff0000) >> 16,
|
|
488 (ip4 & 0x0000ff00) >> 8,
|
|
489 (ip4 & 0x000000ff));
|
|
490 #endif
|
|
491 return 0;
|
|
492 }
|
|
493
|
|
494 int net_host2ip(const char *host, IPADDR *ip)
|
|
495 {
|
|
496 if (strchr(host, ':') != NULL) {
|
|
497 /* IPv6 */
|
|
498 ip->family = AF_INET6;
|
|
499 #ifdef HAVE_IPV6
|
|
500 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
|
|
501 return -1;
|
|
502 #else
|
|
503 ip->ip.s_addr = 0;
|
|
504 #endif
|
|
505 } else {
|
|
506 /* IPv4 */
|
|
507 ip->family = AF_INET;
|
|
508 if (inet_aton(host, (struct in_addr *) &ip->ip) == 0)
|
|
509 return -1;
|
|
510 }
|
|
511
|
|
512 return 0;
|
|
513 }
|
|
514
|
|
515 /* Get socket error */
|
|
516 int net_geterror(int fd)
|
|
517 {
|
|
518 int data;
|
|
519 socklen_t len = sizeof(data);
|
|
520
|
|
521 if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &data, &len) == -1)
|
|
522 return -1;
|
|
523
|
|
524 return data;
|
|
525 }
|
|
526
|
|
527 /* get error of net_gethostname() */
|
|
528 const char *net_gethosterror(int error)
|
|
529 {
|
|
530 #ifdef HAVE_IPV6
|
|
531 i_assert(error != 0);
|
|
532
|
|
533 if (error == 1) {
|
|
534 /* getnameinfo() failed */
|
|
535 return strerror(errno);
|
|
536 }
|
|
537
|
|
538 return gai_strerror(error);
|
|
539 #else
|
|
540 switch (error) {
|
|
541 case HOST_NOT_FOUND:
|
|
542 return "Host not found";
|
|
543 case NO_ADDRESS:
|
|
544 return "No IP address found for name";
|
|
545 case NO_RECOVERY:
|
|
546 return "A non-recovable name server error occurred";
|
|
547 case TRY_AGAIN:
|
|
548 return "A temporary error on an authoritative name server";
|
|
549 }
|
|
550
|
|
551 /* unknown error */
|
|
552 return NULL;
|
|
553 #endif
|
|
554 }
|
|
555
|
|
556 /* return TRUE if host lookup failed because it didn't exist (ie. not
|
|
557 some error with name server) */
|
|
558 int net_hosterror_notfound(int error)
|
|
559 {
|
|
560 #ifdef HAVE_IPV6
|
|
561 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
|
|
562 #else
|
|
563 return error == HOST_NOT_FOUND || error == NO_ADDRESS;
|
|
564 #endif
|
|
565 }
|
|
566
|
|
567 /* Get name of TCP service */
|
|
568 char *net_getservbyport(int port)
|
|
569 {
|
|
570 struct servent *entry;
|
|
571
|
|
572 entry = getservbyport(htons((unsigned short) port), "tcp");
|
|
573 return entry == NULL ? NULL : entry->s_name;
|
|
574 }
|
|
575
|
|
576 int is_ipv4_address(const char *host)
|
|
577 {
|
|
578 while (*host != '\0') {
|
|
579 if (*host != '.' && !i_isdigit(*host))
|
|
580 return 0;
|
|
581 host++;
|
|
582 }
|
|
583
|
|
584 return 1;
|
|
585 }
|
|
586
|
|
587 int is_ipv6_address(const char *host)
|
|
588 {
|
|
589 while (*host != '\0') {
|
|
590 if (*host != ':' && !i_isxdigit(*host))
|
|
591 return 0;
|
|
592 host++;
|
|
593 }
|
|
594
|
|
595 return 1;
|
|
596 }
|