addition of silc.css
[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, ignore if bind() fails */
200         if (my_ip != NULL) {
201                 sin_set_ip(&so, my_ip);
202                 bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
203         }
204
205         /* connect */
206         sin_set_ip(&so, ip);
207         sin_set_port(&so, port);
208         ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
209
210 #ifndef WIN32
211         if (ret < 0 && errno != EINPROGRESS) {
212 #else
213         if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
214 #endif
215                 close(handle);
216                 return NULL;
217         }
218
219         return g_io_channel_new(handle);
220 }
221
222 /* Disconnect socket */
223 void net_disconnect(GIOChannel *handle)
224 {
225         g_return_if_fail(handle != NULL);
226
227         g_io_channel_close(handle);
228         g_io_channel_unref(handle);
229 }
230
231 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
232    address. */
233 GIOChannel *net_listen(IPADDR *my_ip, int *port)
234 {
235         union sockaddr_union so;
236         int ret, handle, opt = 1;
237         socklen_t len;
238
239         g_return_val_if_fail(port != NULL, NULL);
240
241         memset(&so, 0, sizeof(so));
242         sin_set_port(&so, *port);
243         sin_set_ip(&so, my_ip);
244
245         /* create the socket */
246         handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
247 #ifdef HAVE_IPV6
248         if (handle == -1 && errno == EINVAL) {
249                 /* IPv6 is not supported by OS */
250                 so.sin.sin_family = AF_INET;
251                 so.sin.sin_addr.s_addr = INADDR_ANY;
252
253                 handle = socket(AF_INET, SOCK_STREAM, 0);
254         }
255 #endif
256         if (handle == -1)
257                 return NULL;
258
259         /* set socket options */
260 #ifndef WIN32
261         fcntl(handle, F_SETFL, O_NONBLOCK);
262 #endif
263         setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
264                    (char *) &opt, sizeof(opt));
265         setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
266                    (char *) &opt, sizeof(opt));
267
268         /* specify the address/port we want to listen in */
269         ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
270         if (ret >= 0) {
271                 /* get the actual port we started listen */
272                 len = SIZEOF_SOCKADDR(so);
273                 ret = getsockname(handle, &so.sa, &len);
274                 if (ret >= 0) {
275                         *port = sin_get_port(&so);
276
277                         /* start listening */
278                         if (listen(handle, 1) >= 0)
279                                 return g_io_channel_new(handle);
280                 }
281
282         }
283
284         /* error */
285         close(handle);
286         return NULL;
287 }
288
289 /* Accept a connection on a socket */
290 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
291 {
292         union sockaddr_union so;
293         int ret;
294         socklen_t addrlen;
295
296         g_return_val_if_fail(handle != NULL, NULL);
297
298         addrlen = sizeof(so);
299         ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
300
301         if (ret < 0)
302                 return NULL;
303
304         if (addr != NULL) sin_get_ip(&so, addr);
305         if (port != NULL) *port = sin_get_port(&so);
306
307 #ifndef WIN32
308         fcntl(ret, F_SETFL, O_NONBLOCK);
309 #endif
310         return g_io_channel_new(ret);
311 }
312
313 /* Read data from socket, return number of bytes read, -1 = error */
314 int net_receive(GIOChannel *handle, char *buf, int len)
315 {
316         unsigned int ret;
317         int err;
318
319         g_return_val_if_fail(handle != NULL, -1);
320         g_return_val_if_fail(buf != NULL, -1);
321
322         err = g_io_channel_read(handle, buf, len, &ret);
323         if (err == 0 && ret == 0)
324                 return -1; /* disconnected */
325
326         if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
327                 return 0; /* no bytes received */
328
329         return err == 0 ? (int)ret : -1;
330 }
331
332 /* Transmit data, return number of bytes sent, -1 = error */
333 int net_transmit(GIOChannel *handle, const char *data, int len)
334 {
335         unsigned int ret;
336         int err;
337
338         g_return_val_if_fail(handle != NULL, -1);
339         g_return_val_if_fail(data != NULL, -1);
340
341         err = g_io_channel_write(handle, (char *) data, len, &ret);
342         if (err == G_IO_ERROR_AGAIN ||
343             (err != 0 && (errno == EINTR || errno == EPIPE)))
344                 return 0;
345
346         return err == 0 ? (int)ret : -1;
347 }
348
349 /* Get socket address/port */
350 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
351 {
352         union sockaddr_union so;
353         socklen_t addrlen;
354
355         g_return_val_if_fail(handle != NULL, -1);
356         g_return_val_if_fail(addr != NULL, -1);
357
358         addrlen = sizeof(so);
359         if (getsockname(g_io_channel_unix_get_fd(handle),
360                         (struct sockaddr *) &so, &addrlen) == -1)
361                 return -1;
362
363         sin_get_ip(&so, addr);
364         if (port) *port = sin_get_port(&so);
365
366         return 0;
367 }
368
369 /* Get IP addresses for host, both IPv4 and IPv6 if possible.
370    If ip->family is 0, the address wasn't found.
371    Returns 0 = ok, others = error code for net_gethosterror() */
372 int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
373 {
374 #ifdef HAVE_IPV6
375         union sockaddr_union *so;
376         struct addrinfo hints, *ai, *origai;
377         char hbuf[NI_MAXHOST];
378         int host_error, count;
379 #else
380         struct hostent *hp;
381 #endif
382
383         g_return_val_if_fail(addr != NULL, -1);
384
385         memset(ip4, 0, sizeof(IPADDR));
386         memset(ip6, 0, sizeof(IPADDR));
387
388 #ifdef HAVE_IPV6
389         memset(&hints, 0, sizeof(struct addrinfo));
390         hints.ai_socktype = SOCK_STREAM;
391
392         /* save error to host_error for later use */
393         host_error = getaddrinfo(addr, NULL, &hints, &ai);
394         if (host_error != 0)
395                 return host_error;
396
397         if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
398                         sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
399                 return 1;
400
401         origai = ai; count = 0;
402         while (ai != NULL && count < 2) {
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                 ai = ai->ai_next;
413         }
414         freeaddrinfo(origai);
415 #else
416         hp = gethostbyname(addr);
417         if (hp == NULL) return h_errno;
418
419         ip4->family = AF_INET;
420         memcpy(&ip4->ip, hp->h_addr, 4);
421 #endif
422
423         return 0;
424 }
425
426 /* Get name for host, *name should be g_free()'d unless it's NULL.
427    Return values are the same as with net_gethostbyname() */
428 int net_gethostbyaddr(IPADDR *ip, char **name)
429 {
430 #ifdef HAVE_IPV6
431         struct addrinfo req, *ai;
432         int host_error;
433 #else
434         struct hostent *hp;
435 #endif
436         char ipname[MAX_IP_LEN];
437
438         g_return_val_if_fail(ip != NULL, -1);
439         g_return_val_if_fail(name != NULL, -1);
440
441         net_ip2host(ip, ipname);
442
443         *name = NULL;
444 #ifdef HAVE_IPV6
445         memset(&req, 0, sizeof(struct addrinfo));
446         req.ai_socktype = SOCK_STREAM;
447         req.ai_flags = AI_CANONNAME;
448
449         /* save error to host_error for later use */
450         host_error = getaddrinfo(ipname, NULL, &req, &ai);
451         if (host_error != 0)
452                 return host_error;
453         *name = g_strdup(ai->ai_canonname);
454
455         freeaddrinfo(ai);
456 #else
457         hp = gethostbyaddr(ipname, strlen(ipname), AF_INET);
458         if (hp == NULL) return -1;
459
460         *name = g_strdup(hp->h_name);
461 #endif
462
463         return 0;
464 }
465
466 int net_ip2host(IPADDR *ip, char *host)
467 {
468 #ifdef HAVE_IPV6
469         if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
470                 return -1;
471 #else
472         unsigned long ip4;
473
474         ip4 = ntohl(ip->ip.s_addr);
475         g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
476                    (ip4 & 0xff000000UL) >> 24,
477                    (ip4 & 0x00ff0000) >> 16,
478                    (ip4 & 0x0000ff00) >> 8,
479                    (ip4 & 0x000000ff));
480 #endif
481         return 0;
482 }
483
484 int net_host2ip(const char *host, IPADDR *ip)
485 {
486         unsigned long addr;
487
488 #ifdef HAVE_IPV6
489         if (strchr(host, ':') != NULL) {
490                 /* IPv6 */
491                 ip->family = AF_INET6;
492                 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
493                         return -1;
494         } else
495 #endif
496         {
497                 /* IPv4 */
498                 ip->family = AF_INET;
499 #ifdef HAVE_INET_ATON
500                 if (inet_aton(host, &ip->ip.s_addr) == 0)
501                         return -1;
502 #else
503                 addr = inet_addr(host);
504                 if (addr == INADDR_NONE)
505                         return -1;
506
507                 memcpy(&ip->ip, &addr, 4);
508 #endif
509         }
510
511         return 0;
512 }
513
514 /* Get socket error */
515 int net_geterror(GIOChannel *handle)
516 {
517         int data;
518         socklen_t len = sizeof(data);
519
520         if (getsockopt(g_io_channel_unix_get_fd(handle),
521                        SOL_SOCKET, SO_ERROR, (void *) &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         g_return_val_if_fail(error != 0, NULL);
532
533         if (error == 1) {
534                 /* getnameinfo() failed ..
535                    FIXME: does strerror return the right error message? */
536                 return g_strerror(errno);
537         }
538
539         return gai_strerror(error);
540 #else
541         switch (error) {
542         case HOST_NOT_FOUND:
543                 return "Host not found";
544         case NO_ADDRESS:
545                 return "No IP address found for name";
546         case NO_RECOVERY:
547                 return "A non-recovable name server error occurred";
548         case TRY_AGAIN:
549                 return "A temporary error on an authoritative name server";
550         }
551
552         /* unknown error */
553         return NULL;
554 #endif
555 }
556
557 /* return TRUE if host lookup failed because it didn't exist (ie. not
558    some error with name server) */
559 int net_hosterror_notfound(int error)
560 {
561 #ifdef HAVE_IPV6
562         return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
563 #else
564         return error == HOST_NOT_FOUND || error == NO_ADDRESS;
565 #endif
566 }
567
568 /* Get name of TCP service */
569 char *net_getservbyport(int port)
570 {
571         struct servent *entry;
572
573         entry = getservbyport(htons((unsigned short) port), "tcp");
574         return entry == NULL ? NULL : entry->s_name;
575 }
576
577 int is_ipv4_address(const char *host)
578 {
579         while (*host != '\0') {
580                 if (*host != '.' && !isdigit(*host))
581                         return 0;
582                 host++;
583         }
584
585         return 1;
586 }
587
588 int is_ipv6_address(const char *host)
589 {
590         while (*host != '\0') {
591                 if (*host != ':' && !isxdigit(*host))
592                         return 0;
593                 host++;
594         }
595
596         return 1;
597 }