2 network.c : Network stuff
4 Copyright (C) 1999-2000 Timo Sirainen
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.
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.
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
27 # define INADDR_NONE INADDR_BROADCAST
30 union irssi_sockaddr_union {
32 struct sockaddr_in sin;
34 struct sockaddr_in6 sin6;
39 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
40 sizeof(so.sin6) : sizeof(so.sin))
42 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
46 # define g_io_channel_new(handle) g_io_channel_win32_new_stream_socket(handle)
48 # define g_io_channel_new(handle) g_io_channel_unix_new(handle)
51 /* Cygwin need this, don't know others.. */
52 /*#define BLOCKING_SOCKETS 1*/
54 int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
56 if (ip1->family != ip2->family)
60 if (ip1->family == AF_INET6)
61 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0;
64 return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
68 /* copy IP to sockaddr */
74 void sin_set_ip(union irssi_sockaddr_union *so, const IPADDR *ip)
78 so->sin6.sin6_family = AF_INET6;
79 so->sin6.sin6_addr = in6addr_any;
81 so->sin.sin_family = AF_INET;
82 so->sin.sin_addr.s_addr = INADDR_ANY;
87 so->sin.sin_family = ip->family;
89 if (ip->family == AF_INET6)
90 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
93 memcpy(&so->sin.sin_addr, &ip->ip, 4);
96 void sin_get_ip(const union irssi_sockaddr_union *so, IPADDR *ip)
98 ip->family = so->sin.sin_family;
101 if (ip->family == AF_INET6)
102 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
105 memcpy(&ip->ip, &so->sin.sin_addr, 4);
113 void sin_set_port(union irssi_sockaddr_union *so, int port)
116 if (so->sin.sin_family == AF_INET6)
117 so->sin6.sin6_port = htons(port);
120 so->sin.sin_port = htons((unsigned short)port);
128 int sin_get_port(union irssi_sockaddr_union *so)
131 if (so->sin.sin_family == AF_INET6)
132 return ntohs(so->sin6.sin6_port);
134 return ntohs(so->sin.sin_port);
137 /* Connect to socket */
138 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
140 IPADDR ip4, ip6, *ip;
143 g_return_val_if_fail(addr != NULL, NULL);
145 family = my_ip == NULL ? 0 : my_ip->family;
146 if (net_gethostbyname(addr, &ip4, &ip6) == -1)
150 /* prefer IPv4 addresses */
151 ip = ip4.family != 0 ? &ip4 : &ip6;
152 } else if (IPADDR_IS_V6(my_ip)) {
153 /* my_ip is IPv6 address, use it if possible */
161 /* my_ip is IPv4 address, use it if possible */
170 return net_connect_ip(ip, port, my_ip);
173 /* Connect to socket with ip address */
174 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
176 union irssi_sockaddr_union so;
177 int handle, ret, opt = 1;
179 if (my_ip != NULL && ip->family != my_ip->family) {
180 g_warning("net_connect_ip(): ip->family != my_ip->family");
184 /* create the socket */
185 memset(&so, 0, sizeof(so));
186 so.sin.sin_family = ip->family;
187 handle = socket(ip->family, SOCK_STREAM, 0);
192 /* set socket options */
194 fcntl(handle, F_SETFL, O_NONBLOCK);
196 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
197 (char *) &opt, sizeof(opt));
198 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
199 (char *) &opt, sizeof(opt));
201 /* set our own address */
203 sin_set_ip(&so, my_ip);
204 if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
205 /* failed, set it back to INADDR_ANY */
206 sin_set_ip(&so, NULL);
207 bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
213 sin_set_port(&so, port);
214 ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
217 if (ret < 0 && errno != EINPROGRESS)
219 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
222 int old_errno = errno;
228 return g_io_channel_new(handle);
231 /* Connect to named UNIX socket */
232 GIOChannel *net_connect_unix(const char *path)
234 struct sockaddr_un sa;
237 /* create the socket */
238 handle = socket(PF_UNIX, SOCK_STREAM, 0);
242 /* set socket options */
244 fcntl(handle, F_SETFL, O_NONBLOCK);
248 memset(&sa, 0, sizeof(sa));
249 sa.sun_family = AF_UNIX;
250 strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
251 sa.sun_path[sizeof(sa.sun_path)-1] = '\0';
253 ret = connect(handle, (struct sockaddr *) &sa, sizeof(sa));
254 if (ret < 0 && errno != EINPROGRESS) {
255 int old_errno = errno;
261 return g_io_channel_new(handle);
264 /* Disconnect socket */
265 void net_disconnect(GIOChannel *handle)
267 g_return_if_fail(handle != NULL);
269 g_io_channel_close(handle);
270 g_io_channel_unref(handle);
273 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
275 GIOChannel *net_listen(IPADDR *my_ip, int *port)
277 union irssi_sockaddr_union so;
278 int ret, handle, opt = 1;
281 g_return_val_if_fail(port != NULL, NULL);
283 memset(&so, 0, sizeof(so));
284 sin_set_port(&so, *port);
285 sin_set_ip(&so, my_ip);
287 /* create the socket */
288 handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
290 if (handle == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) {
291 /* IPv6 is not supported by OS */
292 so.sin.sin_family = AF_INET;
293 so.sin.sin_addr.s_addr = INADDR_ANY;
295 handle = socket(AF_INET, SOCK_STREAM, 0);
301 /* set socket options */
303 fcntl(handle, F_SETFL, O_NONBLOCK);
305 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
306 (char *) &opt, sizeof(opt));
307 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
308 (char *) &opt, sizeof(opt));
310 /* specify the address/port we want to listen in */
311 ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
313 /* get the actual port we started listen */
314 len = SIZEOF_SOCKADDR(so);
315 ret = getsockname(handle, &so.sa, &len);
317 *port = sin_get_port(&so);
319 /* start listening */
320 if (listen(handle, 1) >= 0)
321 return g_io_channel_new(handle);
331 /* Accept a connection on a socket */
332 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
334 union irssi_sockaddr_union so;
338 g_return_val_if_fail(handle != NULL, NULL);
340 addrlen = sizeof(so);
341 ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
346 if (addr != NULL) sin_get_ip(&so, addr);
347 if (port != NULL) *port = sin_get_port(&so);
350 fcntl(ret, F_SETFL, O_NONBLOCK);
352 return g_io_channel_new(ret);
355 /* Read data from socket, return number of bytes read, -1 = error */
356 int net_receive(GIOChannel *handle, char *buf, int len)
361 g_return_val_if_fail(handle != NULL, -1);
362 g_return_val_if_fail(buf != NULL, -1);
364 err = g_io_channel_read(handle, buf, len, &ret);
365 if (err == 0 && ret == 0)
366 return -1; /* disconnected */
368 if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
369 return 0; /* no bytes received */
371 return err == 0 ? (int)ret : -1;
374 /* Transmit data, return number of bytes sent, -1 = error */
375 int net_transmit(GIOChannel *handle, const char *data, int len)
380 g_return_val_if_fail(handle != NULL, -1);
381 g_return_val_if_fail(data != NULL, -1);
383 err = g_io_channel_write(handle, (char *) data, len, &ret);
384 if (err == G_IO_ERROR_AGAIN ||
385 (err != 0 && (errno == EINTR || errno == EPIPE)))
388 return err == 0 ? (int)ret : -1;
391 /* Get socket address/port */
392 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
394 union irssi_sockaddr_union so;
397 g_return_val_if_fail(handle != NULL, -1);
398 g_return_val_if_fail(addr != NULL, -1);
400 addrlen = sizeof(so);
401 if (getsockname(g_io_channel_unix_get_fd(handle),
402 (struct sockaddr *) &so, &addrlen) == -1)
405 sin_get_ip(&so, addr);
406 if (port) *port = sin_get_port(&so);
411 /* Get IP addresses for host, both IPv4 and IPv6 if possible.
412 If ip->family is 0, the address wasn't found.
413 Returns 0 = ok, others = error code for net_gethosterror() */
414 int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
417 union irssi_sockaddr_union *so;
418 struct addrinfo hints, *ai, *ailist;
424 g_return_val_if_fail(addr != NULL, -1);
426 memset(ip4, 0, sizeof(IPADDR));
427 memset(ip6, 0, sizeof(IPADDR));
430 memset(&hints, 0, sizeof(struct addrinfo));
431 hints.ai_socktype = SOCK_STREAM;
433 /* save error to host_error for later use */
434 ret = getaddrinfo(addr, NULL, &hints, &ailist);
439 for (ai = ailist; ai != NULL && count < 2; ai = ai->ai_next) {
440 so = (union irssi_sockaddr_union *) ai->ai_addr;
442 if (ai->ai_family == AF_INET6 && ip6->family == 0) {
445 } else if (ai->ai_family == AF_INET && ip4->family == 0) {
450 freeaddrinfo(ailist);
451 return count > 0 ? 0 : 1;
453 hp = gethostbyname(addr);
457 ip4->family = AF_INET;
458 memcpy(&ip4->ip, hp->h_addr, 4);
464 /* Get name for host, *name should be g_free()'d unless it's NULL.
465 Return values are the same as with net_gethostbyname() */
466 int net_gethostbyaddr(IPADDR *ip, char **name)
469 struct addrinfo req, *ai;
471 char hostname[NI_MAXHOST];
472 char ipname[MAX_IP_LEN];
477 g_return_val_if_fail(ip != NULL, -1);
478 g_return_val_if_fail(name != NULL, -1);
482 net_ip2host(ip, ipname);
484 memset(&req, 0, sizeof(struct addrinfo));
485 req.ai_socktype = SOCK_STREAM;
486 req.ai_flags = AI_CANONNAME;
488 /* save error to host_error for later use */
489 host_error = getaddrinfo(ipname, NULL, &req, &ai);
492 host_error = getnameinfo(ai->ai_addr, ai->ai_addrlen,
493 hostname, NI_MAXHOST, NULL, 0, 0);
494 if (host_error != 0) {
499 *name = g_strdup(hostname);
503 if (ip->family != AF_INET) return -1;
504 hp = gethostbyaddr(&ip->ip, 4, AF_INET);
505 if (hp == NULL) return -1;
507 *name = g_strdup(hp->h_name);
513 int net_ip2host(IPADDR *ip, char *host)
516 if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
521 if (ip->family != AF_INET) {
522 strcpy(host, "0.0.0.0");
524 ip4 = ntohl(ip->ip.s_addr);
525 g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
526 (ip4 & 0xff000000UL) >> 24,
527 (ip4 & 0x00ff0000) >> 16,
528 (ip4 & 0x0000ff00) >> 8,
535 int net_host2ip(const char *host, IPADDR *ip)
539 if (strchr(host, ':') != NULL) {
541 ip->family = AF_INET6;
543 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
550 ip->family = AF_INET;
551 #ifdef HAVE_INET_ATON
552 if (inet_aton(host, &ip->ip.s_addr) == 0)
555 addr = inet_addr(host);
556 if (addr == INADDR_NONE)
559 memcpy(&ip->ip, &addr, 4);
566 /* Get socket error */
567 int net_geterror(GIOChannel *handle)
570 socklen_t len = sizeof(data);
572 if (getsockopt(g_io_channel_unix_get_fd(handle),
573 SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
579 /* get error of net_gethostname() */
580 const char *net_gethosterror(int error)
583 g_return_val_if_fail(error != 0, NULL);
586 /* getnameinfo() failed ..
587 FIXME: does strerror return the right error message? */
588 return g_strerror(errno);
591 return gai_strerror(error);
595 return "Host not found";
597 return "No IP address found for name";
599 return "A non-recovable name server error occurred";
601 return "A temporary error on an authoritative name server";
609 /* return TRUE if host lookup failed because it didn't exist (ie. not
610 some error with name server) */
611 int net_hosterror_notfound(int error)
614 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
616 return error == HOST_NOT_FOUND || error == NO_ADDRESS;
620 /* Get name of TCP service */
621 char *net_getservbyport(int port)
623 struct servent *entry;
625 entry = getservbyport(htons((unsigned short) port), "tcp");
626 return entry == NULL ? NULL : entry->s_name;
629 int is_ipv4_address(const char *host)
631 while (*host != '\0') {
632 if (*host != '.' && !i_isdigit(*host))
640 int is_ipv6_address(const char *host)
642 while (*host != '\0') {
643 if (*host != ':' && !i_isxdigit(*host))