Added SILC Thread Queue API
[runtime.git] / apps / irssi / src / core / network.c
1 /*
2  network.c : Network stuff
3
4     Copyright (C) 1999-2000 Timo Sirainen
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19 */
20
21 #include "module.h"
22 #include "network.h"
23
24 #include <sys/un.h>
25
26 #ifndef INADDR_NONE
27 #  define INADDR_NONE INADDR_BROADCAST
28 #endif
29
30 union sockaddr_union {
31         struct sockaddr sa;
32         struct sockaddr_in sin;
33 #ifdef HAVE_IPV6
34         struct sockaddr_in6 sin6;
35 #endif
36 };
37
38 #ifdef HAVE_IPV6
39 #  define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
40         sizeof(so.sin6) : sizeof(so.sin))
41 #else
42 #  define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
43 #endif
44
45 #ifdef WIN32
46 #  define g_io_channel_new(handle) g_io_channel_win32_new_stream_socket(handle)
47 #else
48 #  define g_io_channel_new(handle) g_io_channel_unix_new(handle)
49 #endif
50
51 /* Cygwin need this, don't know others.. */
52 /*#define BLOCKING_SOCKETS 1*/
53
54 IPADDR ip4_any = {
55         AF_INET,
56         { INADDR_ANY }
57 };
58
59 int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
60 {
61         if (ip1->family != ip2->family)
62                 return 0;
63
64 #ifdef HAVE_IPV6
65         if (ip1->family == AF_INET6)
66                 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0;
67 #endif
68
69         return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
70 }
71
72
73 static void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
74 {
75         if (ip == NULL) {
76 #ifdef HAVE_IPV6
77                 so->sin6.sin6_family = AF_INET6;
78                 so->sin6.sin6_addr = in6addr_any;
79 #else
80                 so->sin.sin_family = AF_INET;
81                 so->sin.sin_addr.s_addr = INADDR_ANY;
82 #endif
83                 return;
84         }
85
86         so->sin.sin_family = ip->family;
87 #ifdef HAVE_IPV6
88         if (ip->family == AF_INET6)
89                 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
90         else
91 #endif
92                 memcpy(&so->sin.sin_addr, &ip->ip, 4);
93 }
94
95 void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
96 {
97         ip->family = so->sin.sin_family;
98
99 #ifdef HAVE_IPV6
100         if (ip->family == AF_INET6)
101                 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
102         else
103 #endif
104                 memcpy(&ip->ip, &so->sin.sin_addr, 4);
105 }
106
107 static void sin_set_port(union sockaddr_union *so, int port)
108 {
109 #ifdef HAVE_IPV6
110         if (so->sin.sin_family == AF_INET6)
111                 so->sin6.sin6_port = htons((unsigned short)port);
112         else
113 #endif
114                 so->sin.sin_port = htons((unsigned short)port);
115 }
116
117 static int sin_get_port(union sockaddr_union *so)
118 {
119 #ifdef HAVE_IPV6
120         if (so->sin.sin_family == AF_INET6)
121                 return ntohs(so->sin6.sin6_port);
122 #endif
123         return ntohs(so->sin.sin_port);
124 }
125
126 /* Connect to socket */
127 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
128 {
129         IPADDR ip4, ip6, *ip;
130         int family;
131
132         g_return_val_if_fail(addr != NULL, NULL);
133
134         family = my_ip == NULL ? 0 : my_ip->family;
135         if (net_gethostbyname(addr, &ip4, &ip6) == -1)
136                 return NULL;
137
138         if (my_ip == NULL) {
139                 /* prefer IPv4 addresses */
140                 ip = ip4.family != 0 ? &ip4 : &ip6;
141         } else if (IPADDR_IS_V6(my_ip)) {
142                 /* my_ip is IPv6 address, use it if possible */
143                 if (ip6.family != 0)
144                         ip = &ip6;
145                 else {
146                         my_ip = NULL;
147                         ip = &ip4;
148                 }
149         } else {
150                 /* my_ip is IPv4 address, use it if possible */
151                 if (ip4.family != 0)
152                         ip = &ip4;
153                 else {
154                         my_ip = NULL;
155                         ip = &ip6;
156                 }
157         }
158
159         return net_connect_ip(ip, port, my_ip);
160 }
161
162 /* Connect to socket with ip address */
163 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
164 {
165         union sockaddr_union so;
166         int handle, ret, opt = 1;
167
168         if (my_ip != NULL && ip->family != my_ip->family) {
169                 g_warning("net_connect_ip(): ip->family != my_ip->family");
170                 my_ip = NULL;
171         }
172
173         /* create the socket */
174         memset(&so, 0, sizeof(so));
175         so.sin.sin_family = ip->family;
176         handle = socket(ip->family, SOCK_STREAM, 0);
177
178         if (handle == -1)
179                 return NULL;
180
181         /* set socket options */
182 #ifndef WIN32
183         fcntl(handle, F_SETFL, O_NONBLOCK);
184 #endif
185         setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
186                    (char *) &opt, sizeof(opt));
187         setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
188                    (char *) &opt, sizeof(opt));
189
190         /* set our own address */
191         if (my_ip != NULL) {
192                 sin_set_ip(&so, my_ip);
193                 if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
194                         int old_errno = errno;
195
196                         close(handle);
197                         errno = old_errno;
198                         return NULL;
199                 }
200         }
201
202         /* connect */
203         sin_set_ip(&so, ip);
204         sin_set_port(&so, port);
205         ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
206
207 #ifndef WIN32
208         if (ret < 0 && errno != EINPROGRESS)
209 #else
210         if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
211 #endif
212         {
213                 int old_errno = errno;
214                 close(handle);
215                 errno = old_errno;
216                 return NULL;
217         }
218
219         return g_io_channel_new(handle);
220 }
221
222 /* Connect to named UNIX socket */
223 GIOChannel *net_connect_unix(const char *path)
224 {
225         struct sockaddr_un sa;
226         int handle, ret;
227
228         /* create the socket */
229         handle = socket(PF_UNIX, SOCK_STREAM, 0);
230         if (handle == -1)
231                 return NULL;
232
233         /* set socket options */
234 #ifndef WIN32
235         fcntl(handle, F_SETFL, O_NONBLOCK);
236 #endif
237
238         /* connect */
239         memset(&sa, 0, sizeof(sa));
240         sa.sun_family = AF_UNIX;
241         strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
242         sa.sun_path[sizeof(sa.sun_path)-1] = '\0';
243
244         ret = connect(handle, (struct sockaddr *) &sa, sizeof(sa));
245         if (ret < 0 && errno != EINPROGRESS) {
246                 int old_errno = errno;
247                 close(handle);
248                 errno = old_errno;
249                 return NULL;
250         }
251
252         return g_io_channel_new(handle);
253 }
254
255 /* Disconnect socket */
256 void net_disconnect(GIOChannel *handle)
257 {
258         g_return_if_fail(handle != NULL);
259
260         g_io_channel_close(handle);
261         g_io_channel_unref(handle);
262 }
263
264 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
265    address. */
266 GIOChannel *net_listen(IPADDR *my_ip, int *port)
267 {
268         union sockaddr_union so;
269         int ret, handle, opt = 1;
270         socklen_t len;
271
272         g_return_val_if_fail(port != NULL, NULL);
273
274         memset(&so, 0, sizeof(so));
275         sin_set_ip(&so, my_ip);
276         sin_set_port(&so, *port);
277
278         /* create the socket */
279         handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
280 #ifdef HAVE_IPV6
281         if (handle == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) {
282                 /* IPv6 is not supported by OS */
283                 so.sin.sin_family = AF_INET;
284                 so.sin.sin_addr.s_addr = INADDR_ANY;
285
286                 handle = socket(AF_INET, SOCK_STREAM, 0);
287         }
288 #endif
289         if (handle == -1)
290                 return NULL;
291
292         /* set socket options */
293 #ifndef WIN32
294         fcntl(handle, F_SETFL, O_NONBLOCK);
295 #endif
296         setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
297                    (char *) &opt, sizeof(opt));
298         setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
299                    (char *) &opt, sizeof(opt));
300
301         /* specify the address/port we want to listen in */
302         ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
303         if (ret >= 0) {
304                 /* get the actual port we started listen */
305                 len = SIZEOF_SOCKADDR(so);
306                 ret = getsockname(handle, &so.sa, &len);
307                 if (ret >= 0) {
308                         *port = sin_get_port(&so);
309
310                         /* start listening */
311                         if (listen(handle, 1) >= 0)
312                                 return g_io_channel_new(handle);
313                 }
314
315         }
316
317         /* error */
318         close(handle);
319         return NULL;
320 }
321
322 /* Accept a connection on a socket */
323 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
324 {
325         union sockaddr_union so;
326         int ret;
327         socklen_t addrlen;
328
329         g_return_val_if_fail(handle != NULL, NULL);
330
331         addrlen = sizeof(so);
332         ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
333
334         if (ret < 0)
335                 return NULL;
336
337         if (addr != NULL) sin_get_ip(&so, addr);
338         if (port != NULL) *port = sin_get_port(&so);
339
340 #ifndef WIN32
341         fcntl(ret, F_SETFL, O_NONBLOCK);
342 #endif
343         return g_io_channel_new(ret);
344 }
345
346 /* Read data from socket, return number of bytes read, -1 = error */
347 int net_receive(GIOChannel *handle, char *buf, int len)
348 {
349         gsize ret;
350         int err;
351
352         g_return_val_if_fail(handle != NULL, -1);
353         g_return_val_if_fail(buf != NULL, -1);
354
355         err = g_io_channel_read(handle, buf, len, &ret);
356         if (err == 0 && ret == 0)
357                 return -1; /* disconnected */
358
359         if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
360                 return 0; /* no bytes received */
361
362         return err == 0 ? (int)ret : -1;
363 }
364
365 /* Transmit data, return number of bytes sent, -1 = error */
366 int net_transmit(GIOChannel *handle, const char *data, int len)
367 {
368         gsize ret;
369         int err;
370
371         g_return_val_if_fail(handle != NULL, -1);
372         g_return_val_if_fail(data != NULL, -1);
373
374         err = g_io_channel_write(handle, (char *) data, len, &ret);
375         if (err == G_IO_ERROR_AGAIN ||
376             (err != 0 && (errno == EINTR || errno == EPIPE)))
377                 return 0;
378
379         return err == 0 ? (int)ret : -1;
380 }
381
382 /* Get socket address/port */
383 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
384 {
385         union sockaddr_union so;
386         socklen_t addrlen;
387
388         g_return_val_if_fail(handle != NULL, -1);
389         g_return_val_if_fail(addr != NULL, -1);
390
391         addrlen = sizeof(so);
392         if (getsockname(g_io_channel_unix_get_fd(handle),
393                         (struct sockaddr *) &so, &addrlen) == -1)
394                 return -1;
395
396         sin_get_ip(&so, addr);
397         if (port) *port = sin_get_port(&so);
398
399         return 0;
400 }
401
402 /* Get IP addresses for host, both IPv4 and IPv6 if possible.
403    If ip->family is 0, the address wasn't found.
404    Returns 0 = ok, others = error code for net_gethosterror() */
405 int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
406 {
407 #ifdef HAVE_IPV6
408         union sockaddr_union *so;
409         struct addrinfo hints, *ai, *ailist;
410         int ret, count_v4, count_v6, use_v4, use_v6;
411 #else
412         struct hostent *hp;
413         int count;
414 #endif
415
416         g_return_val_if_fail(addr != NULL, -1);
417
418         memset(ip4, 0, sizeof(IPADDR));
419         memset(ip6, 0, sizeof(IPADDR));
420
421 #ifdef HAVE_IPV6
422         memset(&hints, 0, sizeof(struct addrinfo));
423         hints.ai_socktype = SOCK_STREAM;
424
425         /* save error to host_error for later use */
426         ret = getaddrinfo(addr, NULL, &hints, &ailist);
427         if (ret != 0)
428                 return ret;
429
430         /* count IPs */
431         count_v4 = count_v6 = 0;
432         for (ai = ailist; ai != NULL; ai = ai->ai_next) {
433                 if (ai->ai_family == AF_INET)
434                         count_v4++;
435                 else if (ai->ai_family == AF_INET6)
436                         count_v6++;
437         }
438
439         if (count_v4 == 0 && count_v6 == 0)
440                 return HOST_NOT_FOUND; /* shouldn't happen? */
441
442         /* if there are multiple addresses, return random one */
443         use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4;
444         use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6;
445
446         count_v4 = count_v6 = 0;
447         for (ai = ailist; ai != NULL; ai = ai->ai_next) {
448                 so = (union sockaddr_union *) ai->ai_addr;
449
450                 if (ai->ai_family == AF_INET) {
451                         if (use_v4 == count_v4)
452                                 sin_get_ip(so, ip4);
453                         count_v4++;
454                 } else if (ai->ai_family == AF_INET6) {
455                         if (use_v6 == count_v6)
456                                 sin_get_ip(so, ip6);
457                         count_v6++;
458                 }
459         }
460         freeaddrinfo(ailist);
461         return 0;
462 #else
463         hp = gethostbyname(addr);
464         if (hp == NULL)
465                 return h_errno;
466
467         /* count IPs */
468         count = 0;
469         while (hp->h_addr_list[count] != NULL)
470                 count++;
471
472         if (count == 0)
473                 return HOST_NOT_FOUND; /* shouldn't happen? */
474
475         /* if there are multiple addresses, return random one */
476         ip4->family = AF_INET;
477         memcpy(&ip4->ip, hp->h_addr_list[rand() % count], 4);
478
479         return 0;
480 #endif
481 }
482
483 /* Get name for host, *name should be g_free()'d unless it's NULL.
484    Return values are the same as with net_gethostbyname() */
485 int net_gethostbyaddr(IPADDR *ip, char **name)
486 {
487 #ifdef HAVE_IPV6
488         union sockaddr_union so;
489         int host_error;
490         char hostname[NI_MAXHOST];
491 #else
492         struct hostent *hp;
493 #endif
494
495         g_return_val_if_fail(ip != NULL, -1);
496         g_return_val_if_fail(name != NULL, -1);
497
498         *name = NULL;
499 #ifdef HAVE_IPV6
500         memset(&so, 0, sizeof(so));
501         sin_set_ip(&so, ip);
502
503         /* save error to host_error for later use */
504         host_error = getnameinfo((struct sockaddr *) &so, sizeof(so),
505                                  hostname, sizeof(hostname), NULL, 0, 0);
506         if (host_error != 0)
507                 return host_error;
508
509         *name = g_strdup(hostname);
510 #else
511         if (ip->family != AF_INET) return -1;
512         hp = gethostbyaddr((const char *) &ip->ip, 4, AF_INET);
513         if (hp == NULL) return -1;
514
515         *name = g_strdup(hp->h_name);
516 #endif
517
518         return 0;
519 }
520
521 int net_ip2host(IPADDR *ip, char *host)
522 {
523 #ifdef HAVE_IPV6
524         if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
525                 return -1;
526 #else
527         unsigned long ip4;
528
529         if (ip->family != AF_INET) {
530                 strcpy(host, "0.0.0.0");
531         } else {
532                 ip4 = ntohl(ip->ip.s_addr);
533                 g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
534                            (ip4 & 0xff000000UL) >> 24,
535                            (ip4 & 0x00ff0000) >> 16,
536                            (ip4 & 0x0000ff00) >> 8,
537                            (ip4 & 0x000000ff));
538         }
539 #endif
540         return 0;
541 }
542
543 int net_host2ip(const char *host, IPADDR *ip)
544 {
545         unsigned long addr;
546
547         if (strchr(host, ':') != NULL) {
548                 /* IPv6 */
549                 ip->family = AF_INET6;
550 #ifdef HAVE_IPV6
551                 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
552                         return -1;
553 #else
554                 ip->ip.s_addr = 0;
555 #endif
556         } else {
557                 /* IPv4 */
558                 ip->family = AF_INET;
559 #ifdef HAVE_INET_ATON
560                 if (inet_aton(host, &ip->ip.s_addr) == 0)
561                         return -1;
562 #else
563                 addr = inet_addr(host);
564                 if (addr == INADDR_NONE)
565                         return -1;
566
567                 memcpy(&ip->ip, &addr, 4);
568 #endif
569         }
570
571         return 0;
572 }
573
574 /* Get socket error */
575 int net_geterror(GIOChannel *handle)
576 {
577         int data;
578         socklen_t len = sizeof(data);
579
580         if (getsockopt(g_io_channel_unix_get_fd(handle),
581                        SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
582                 return -1;
583
584         return data;
585 }
586
587 /* get error of net_gethostname() */
588 const char *net_gethosterror(int error)
589 {
590 #ifdef HAVE_IPV6
591         g_return_val_if_fail(error != 0, NULL);
592
593         if (error == 1) {
594                 /* getnameinfo() failed ..
595                    FIXME: does strerror return the right error message? */
596                 return g_strerror(errno);
597         }
598
599         return gai_strerror(error);
600 #else
601         switch (error) {
602         case HOST_NOT_FOUND:
603                 return "Host not found";
604         case NO_ADDRESS:
605                 return "No IP address found for name";
606         case NO_RECOVERY:
607                 return "A non-recovable name server error occurred";
608         case TRY_AGAIN:
609                 return "A temporary error on an authoritative name server";
610         }
611
612         /* unknown error */
613         return NULL;
614 #endif
615 }
616
617 /* return TRUE if host lookup failed because it didn't exist (ie. not
618    some error with name server) */
619 int net_hosterror_notfound(int error)
620 {
621 #ifdef HAVE_IPV6
622 #ifdef EAI_NODATA /* NODATA is depricated */
623         return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
624 #else
625         return error != 1 && (error == EAI_NONAME);
626 #endif
627 #else
628         return error == HOST_NOT_FOUND || error == NO_ADDRESS;
629 #endif
630 }
631
632 /* Get name of TCP service */
633 char *net_getservbyport(int port)
634 {
635         struct servent *entry;
636
637         entry = getservbyport(htons((unsigned short) port), "tcp");
638         return entry == NULL ? NULL : entry->s_name;
639 }
640
641 int is_ipv4_address(const char *host)
642 {
643         while (*host != '\0') {
644                 if (*host != '.' && !i_isdigit(*host))
645                         return 0;
646                 host++;
647         }
648
649         return 1;
650 }
651
652 int is_ipv6_address(const char *host)
653 {
654         while (*host != '\0') {
655                 if (*host != ':' && !i_isxdigit(*host))
656                         return 0;
657                 host++;
658         }
659
660         return 1;
661 }