Merged Irssi 0.8.2 from irssi.org cvs.
[silc.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 #ifndef INADDR_NONE
25 #  define INADDR_NONE INADDR_BROADCAST
26 #endif
27
28 union sockaddr_union {
29         struct sockaddr sa;
30         struct sockaddr_in sin;
31 #ifdef HAVE_IPV6
32         struct sockaddr_in6 sin6;
33 #endif
34 };
35
36 #ifdef HAVE_IPV6
37 #  define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
38         sizeof(so.sin6) : sizeof(so.sin))
39 #else
40 #  define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
41 #endif
42
43 #ifdef WIN32
44 #  define g_io_channel_new(handle) g_io_channel_win32_new_stream_socket(handle)
45 #else
46 #  define g_io_channel_new(handle) g_io_channel_unix_new(handle)
47 #endif
48
49 /* Cygwin need this, don't know others.. */
50 /*#define BLOCKING_SOCKETS 1*/
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 #ifdef G_CAN_INLINE
68 G_INLINE_FUNC
69 #else
70 static
71 #endif
72 void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
73 {
74         if (ip == NULL) {
75 #ifdef HAVE_IPV6
76                 so->sin6.sin6_family = AF_INET6;
77                 so->sin6.sin6_addr = in6addr_any;
78 #else
79                 so->sin.sin_family = AF_INET;
80                 so->sin.sin_addr.s_addr = INADDR_ANY;
81 #endif
82                 return;
83         }
84
85         so->sin.sin_family = ip->family;
86 #ifdef HAVE_IPV6
87         if (ip->family == AF_INET6)
88                 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
89         else
90 #endif
91                 memcpy(&so->sin.sin_addr, &ip->ip, 4);
92 }
93
94 void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
95 {
96         ip->family = so->sin.sin_family;
97
98 #ifdef HAVE_IPV6
99         if (ip->family == AF_INET6)
100                 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
101         else
102 #endif
103                 memcpy(&ip->ip, &so->sin.sin_addr, 4);
104 }
105
106 #ifdef G_CAN_INLINE
107 G_INLINE_FUNC
108 #else
109 static
110 #endif
111 void sin_set_port(union sockaddr_union *so, int port)
112 {
113 #ifdef HAVE_IPV6
114         if (so->sin.sin_family == AF_INET6)
115                 so->sin6.sin6_port = htons(port);
116         else
117 #endif
118                 so->sin.sin_port = htons((unsigned short)port);
119 }
120
121 #ifdef G_CAN_INLINE
122 G_INLINE_FUNC
123 #else
124 static
125 #endif
126 int sin_get_port(union sockaddr_union *so)
127 {
128 #ifdef HAVE_IPV6
129         if (so->sin.sin_family == AF_INET6)
130                 return ntohs(so->sin6.sin6_port);
131 #endif
132         return ntohs(so->sin.sin_port);
133 }
134
135 /* Connect to socket */
136 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
137 {
138         IPADDR ip4, ip6, *ip;
139         int family;
140
141         g_return_val_if_fail(addr != NULL, NULL);
142
143         family = my_ip == NULL ? 0 : my_ip->family;
144         if (net_gethostbyname(addr, &ip4, &ip6) == -1)
145                 return NULL;
146
147         if (my_ip == NULL) {
148                 /* prefer IPv4 addresses */
149                 ip = ip4.family != 0 ? &ip4 : &ip6;
150         } else if (IPADDR_IS_V6(my_ip)) {
151                 /* my_ip is IPv6 address, use it if possible */
152                 if (ip6.family != 0)
153                         ip = &ip6;
154                 else {
155                         my_ip = NULL;
156                         ip = &ip4;
157                 }
158         } else {
159                 /* my_ip is IPv4 address, use it if possible */
160                 if (ip4.family != 0)
161                         ip = &ip4;
162                 else {
163                         my_ip = NULL;
164                         ip = &ip6;
165                 }
166         }
167
168         return net_connect_ip(ip, port, my_ip);
169 }
170
171 /* Connect to socket with ip address */
172 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
173 {
174         union sockaddr_union so;
175         int handle, ret, opt = 1;
176
177         if (my_ip != NULL && ip->family != my_ip->family) {
178                 g_warning("net_connect_ip(): ip->family != my_ip->family");
179                 my_ip = NULL;
180         }
181
182         /* create the socket */
183         memset(&so, 0, sizeof(so));
184         so.sin.sin_family = ip->family;
185         handle = socket(ip->family, SOCK_STREAM, 0);
186
187         if (handle == -1)
188                 return NULL;
189
190         /* set socket options */
191 #ifndef WIN32
192         fcntl(handle, F_SETFL, O_NONBLOCK);
193 #endif
194         setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
195                    (char *) &opt, sizeof(opt));
196         setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
197                    (char *) &opt, sizeof(opt));
198
199         /* set our own address */
200         if (my_ip != NULL) {
201                 sin_set_ip(&so, my_ip);
202                 if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
203                         /* failed, set it back to INADDR_ANY */
204                         sin_set_ip(&so, NULL);
205                         bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
206                 }
207         }
208
209         /* connect */
210         sin_set_ip(&so, ip);
211         sin_set_port(&so, port);
212         ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
213
214 #ifndef WIN32
215         if (ret < 0 && errno != EINPROGRESS)
216 #else
217         if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
218 #endif
219         {
220                 close(handle);
221                 return NULL;
222         }
223
224         return g_io_channel_new(handle);
225 }
226
227 /* Disconnect socket */
228 void net_disconnect(GIOChannel *handle)
229 {
230         g_return_if_fail(handle != NULL);
231
232         g_io_channel_close(handle);
233         g_io_channel_unref(handle);
234 }
235
236 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
237    address. */
238 GIOChannel *net_listen(IPADDR *my_ip, int *port)
239 {
240         union sockaddr_union so;
241         int ret, handle, opt = 1;
242         socklen_t len;
243
244         g_return_val_if_fail(port != NULL, NULL);
245
246         memset(&so, 0, sizeof(so));
247         sin_set_port(&so, *port);
248         sin_set_ip(&so, my_ip);
249
250         /* create the socket */
251         handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
252 #ifdef HAVE_IPV6
253         if (handle == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) {
254                 /* IPv6 is not supported by OS */
255                 so.sin.sin_family = AF_INET;
256                 so.sin.sin_addr.s_addr = INADDR_ANY;
257
258                 handle = socket(AF_INET, SOCK_STREAM, 0);
259         }
260 #endif
261         if (handle == -1)
262                 return NULL;
263
264         /* set socket options */
265 #ifndef WIN32
266         fcntl(handle, F_SETFL, O_NONBLOCK);
267 #endif
268         setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
269                    (char *) &opt, sizeof(opt));
270         setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
271                    (char *) &opt, sizeof(opt));
272
273         /* specify the address/port we want to listen in */
274         ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
275         if (ret >= 0) {
276                 /* get the actual port we started listen */
277                 len = SIZEOF_SOCKADDR(so);
278                 ret = getsockname(handle, &so.sa, &len);
279                 if (ret >= 0) {
280                         *port = sin_get_port(&so);
281
282                         /* start listening */
283                         if (listen(handle, 1) >= 0)
284                                 return g_io_channel_new(handle);
285                 }
286
287         }
288
289         /* error */
290         close(handle);
291         return NULL;
292 }
293
294 /* Accept a connection on a socket */
295 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
296 {
297         union sockaddr_union so;
298         int ret;
299         socklen_t addrlen;
300
301         g_return_val_if_fail(handle != NULL, NULL);
302
303         addrlen = sizeof(so);
304         ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
305
306         if (ret < 0)
307                 return NULL;
308
309         if (addr != NULL) sin_get_ip(&so, addr);
310         if (port != NULL) *port = sin_get_port(&so);
311
312 #ifndef WIN32
313         fcntl(ret, F_SETFL, O_NONBLOCK);
314 #endif
315         return g_io_channel_new(ret);
316 }
317
318 /* Read data from socket, return number of bytes read, -1 = error */
319 int net_receive(GIOChannel *handle, char *buf, int len)
320 {
321         unsigned int ret;
322         int err;
323
324         g_return_val_if_fail(handle != NULL, -1);
325         g_return_val_if_fail(buf != NULL, -1);
326
327         err = g_io_channel_read(handle, buf, len, &ret);
328         if (err == 0 && ret == 0)
329                 return -1; /* disconnected */
330
331         if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
332                 return 0; /* no bytes received */
333
334         return err == 0 ? (int)ret : -1;
335 }
336
337 /* Transmit data, return number of bytes sent, -1 = error */
338 int net_transmit(GIOChannel *handle, const char *data, int len)
339 {
340         unsigned int ret;
341         int err;
342
343         g_return_val_if_fail(handle != NULL, -1);
344         g_return_val_if_fail(data != NULL, -1);
345
346         err = g_io_channel_write(handle, (char *) data, len, &ret);
347         if (err == G_IO_ERROR_AGAIN ||
348             (err != 0 && (errno == EINTR || errno == EPIPE)))
349                 return 0;
350
351         return err == 0 ? (int)ret : -1;
352 }
353
354 /* Get socket address/port */
355 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
356 {
357         union sockaddr_union so;
358         socklen_t addrlen;
359
360         g_return_val_if_fail(handle != NULL, -1);
361         g_return_val_if_fail(addr != NULL, -1);
362
363         addrlen = sizeof(so);
364         if (getsockname(g_io_channel_unix_get_fd(handle),
365                         (struct sockaddr *) &so, &addrlen) == -1)
366                 return -1;
367
368         sin_get_ip(&so, addr);
369         if (port) *port = sin_get_port(&so);
370
371         return 0;
372 }
373
374 /* Get IP addresses for host, both IPv4 and IPv6 if possible.
375    If ip->family is 0, the address wasn't found.
376    Returns 0 = ok, others = error code for net_gethosterror() */
377 int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
378 {
379 #ifdef HAVE_IPV6
380         union sockaddr_union *so;
381         struct addrinfo hints, *ai, *ailist;
382         int ret, count;
383 #else
384         struct hostent *hp;
385 #endif
386
387         g_return_val_if_fail(addr != NULL, -1);
388
389         memset(ip4, 0, sizeof(IPADDR));
390         memset(ip6, 0, sizeof(IPADDR));
391
392 #ifdef HAVE_IPV6
393         memset(&hints, 0, sizeof(struct addrinfo));
394         hints.ai_socktype = SOCK_STREAM;
395
396         /* save error to host_error for later use */
397         ret = getaddrinfo(addr, NULL, &hints, &ailist);
398         if (ret != 0)
399                 return ret;
400
401         count = 0;
402         for (ai = ailist; ai != NULL && count < 2; ai = ai->ai_next) {
403                 so = (union sockaddr_union *) ai->ai_addr;
404
405                 if (ai->ai_family == AF_INET6 && ip6->family == 0) {
406                         sin_get_ip(so, ip6);
407                         count++;
408                 } else if (ai->ai_family == AF_INET && ip4->family == 0) {
409                         sin_get_ip(so, ip4);
410                         count++;
411                 }
412         }
413         freeaddrinfo(ailist);
414         return count > 0 ? 0 : 1;
415 #else
416         hp = gethostbyname(addr);
417         if (hp == NULL)
418                 return h_errno;
419
420         ip4->family = AF_INET;
421         memcpy(&ip4->ip, hp->h_addr, 4);
422
423         return 0;
424 #endif
425 }
426
427 /* Get name for host, *name should be g_free()'d unless it's NULL.
428    Return values are the same as with net_gethostbyname() */
429 int net_gethostbyaddr(IPADDR *ip, char **name)
430 {
431 #ifdef HAVE_IPV6
432         struct addrinfo req, *ai;
433         int host_error;
434 #else
435         struct hostent *hp;
436 #endif
437         char ipname[MAX_IP_LEN];
438
439         g_return_val_if_fail(ip != NULL, -1);
440         g_return_val_if_fail(name != NULL, -1);
441
442         net_ip2host(ip, ipname);
443
444         *name = NULL;
445 #ifdef HAVE_IPV6
446         memset(&req, 0, sizeof(struct addrinfo));
447         req.ai_socktype = SOCK_STREAM;
448         req.ai_flags = AI_CANONNAME;
449
450         /* save error to host_error for later use */
451         host_error = getaddrinfo(ipname, NULL, &req, &ai);
452         if (host_error != 0)
453                 return host_error;
454         *name = g_strdup(ai->ai_canonname);
455
456         freeaddrinfo(ai);
457 #else
458         hp = gethostbyaddr(ipname, strlen(ipname), AF_INET);
459         if (hp == NULL) return -1;
460
461         *name = g_strdup(hp->h_name);
462 #endif
463
464         return 0;
465 }
466
467 int net_ip2host(IPADDR *ip, char *host)
468 {
469 #ifdef HAVE_IPV6
470         if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
471                 return -1;
472 #else
473         unsigned long ip4;
474
475         ip4 = ntohl(ip->ip.s_addr);
476         g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
477                    (ip4 & 0xff000000UL) >> 24,
478                    (ip4 & 0x00ff0000) >> 16,
479                    (ip4 & 0x0000ff00) >> 8,
480                    (ip4 & 0x000000ff));
481 #endif
482         return 0;
483 }
484
485 int net_host2ip(const char *host, IPADDR *ip)
486 {
487         unsigned long addr;
488
489 #ifdef HAVE_IPV6
490         if (strchr(host, ':') != NULL) {
491                 /* IPv6 */
492                 ip->family = AF_INET6;
493                 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
494                         return -1;
495         } else
496 #endif
497         {
498                 /* IPv4 */
499                 ip->family = AF_INET;
500 #ifdef HAVE_INET_ATON
501                 if (inet_aton(host, &ip->ip.s_addr) == 0)
502                         return -1;
503 #else
504                 addr = inet_addr(host);
505                 if (addr == INADDR_NONE)
506                         return -1;
507
508                 memcpy(&ip->ip, &addr, 4);
509 #endif
510         }
511
512         return 0;
513 }
514
515 /* Get socket error */
516 int net_geterror(GIOChannel *handle)
517 {
518         int data;
519         socklen_t len = sizeof(data);
520
521         if (getsockopt(g_io_channel_unix_get_fd(handle),
522                        SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
523                 return -1;
524
525         return data;
526 }
527
528 /* get error of net_gethostname() */
529 const char *net_gethosterror(int error)
530 {
531 #ifdef HAVE_IPV6
532         g_return_val_if_fail(error != 0, NULL);
533
534         if (error == 1) {
535                 /* getnameinfo() failed ..
536                    FIXME: does strerror return the right error message? */
537                 return g_strerror(errno);
538         }
539
540         return gai_strerror(error);
541 #else
542         switch (error) {
543         case HOST_NOT_FOUND:
544                 return "Host not found";
545         case NO_ADDRESS:
546                 return "No IP address found for name";
547         case NO_RECOVERY:
548                 return "A non-recovable name server error occurred";
549         case TRY_AGAIN:
550                 return "A temporary error on an authoritative name server";
551         }
552
553         /* unknown error */
554         return NULL;
555 #endif
556 }
557
558 /* return TRUE if host lookup failed because it didn't exist (ie. not
559    some error with name server) */
560 int net_hosterror_notfound(int error)
561 {
562 #ifdef HAVE_IPV6
563         return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
564 #else
565         return error == HOST_NOT_FOUND || error == NO_ADDRESS;
566 #endif
567 }
568
569 /* Get name of TCP service */
570 char *net_getservbyport(int port)
571 {
572         struct servent *entry;
573
574         entry = getservbyport(htons((unsigned short) port), "tcp");
575         return entry == NULL ? NULL : entry->s_name;
576 }
577
578 int is_ipv4_address(const char *host)
579 {
580         while (*host != '\0') {
581                 if (*host != '.' && !i_isdigit(*host))
582                         return 0;
583                 host++;
584         }
585
586         return 1;
587 }
588
589 int is_ipv6_address(const char *host)
590 {
591         while (*host != '\0') {
592                 if (*host != ':' && !i_isxdigit(*host))
593                         return 0;
594                 host++;
595         }
596
597         return 1;
598 }