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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 # define INADDR_NONE INADDR_BROADCAST
30 union 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))
45 GIOChannel *g_io_channel_new(int handle)
49 chan = g_io_channel_win32_new_socket(handle);
51 chan = g_io_channel_unix_new(handle);
53 g_io_channel_set_encoding(chan, NULL, NULL);
54 g_io_channel_set_buffered(chan, FALSE);
58 /* Cygwin need this, don't know others.. */
59 /*#define BLOCKING_SOCKETS 1*/
66 int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
68 if (ip1->family != ip2->family)
72 if (ip1->family == AF_INET6)
73 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0;
76 return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
80 static void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
84 so->sin6.sin6_family = AF_INET6;
85 so->sin6.sin6_addr = in6addr_any;
87 so->sin.sin_family = AF_INET;
88 so->sin.sin_addr.s_addr = INADDR_ANY;
93 so->sin.sin_family = ip->family;
95 if (ip->family == AF_INET6)
96 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
99 memcpy(&so->sin.sin_addr, &ip->ip, 4);
102 void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
104 ip->family = so->sin.sin_family;
107 if (ip->family == AF_INET6)
108 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
111 memcpy(&ip->ip, &so->sin.sin_addr, 4);
114 static void sin_set_port(union sockaddr_union *so, int port)
117 if (so->sin.sin_family == AF_INET6)
118 so->sin6.sin6_port = htons((unsigned short)port);
121 so->sin.sin_port = htons((unsigned short)port);
124 static int sin_get_port(union sockaddr_union *so)
127 if (so->sin.sin_family == AF_INET6)
128 return ntohs(so->sin6.sin6_port);
130 return ntohs(so->sin.sin_port);
133 /* Connect to socket */
134 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
136 IPADDR ip4, ip6, *ip;
138 g_return_val_if_fail(addr != NULL, NULL);
140 if (net_gethostbyname(addr, &ip4, &ip6) == -1)
144 /* prefer IPv4 addresses */
145 ip = ip4.family != 0 ? &ip4 : &ip6;
146 } else if (IPADDR_IS_V6(my_ip)) {
147 /* my_ip is IPv6 address, use it if possible */
155 /* my_ip is IPv4 address, use it if possible */
164 return net_connect_ip(ip, port, my_ip);
167 /* Connect to socket with ip address */
168 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
170 union sockaddr_union so;
171 int handle, ret, opt = 1;
173 if (my_ip != NULL && ip->family != my_ip->family) {
174 g_warning("net_connect_ip(): ip->family != my_ip->family");
178 /* create the socket */
179 memset(&so, 0, sizeof(so));
180 so.sin.sin_family = ip->family;
181 handle = socket(ip->family, SOCK_STREAM, 0);
186 /* set socket options */
188 fcntl(handle, F_SETFL, O_NONBLOCK);
190 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
191 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
193 /* set our own address */
195 sin_set_ip(&so, my_ip);
196 if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
197 int old_errno = errno;
207 sin_set_port(&so, port);
208 ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
211 if (ret < 0 && errno != EINPROGRESS)
213 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
216 int old_errno = errno;
222 return g_io_channel_new(handle);
225 /* Connect to named UNIX socket */
226 GIOChannel *net_connect_unix(const char *path)
228 struct sockaddr_un sa;
231 /* create the socket */
232 handle = socket(PF_UNIX, SOCK_STREAM, 0);
236 /* set socket options */
238 fcntl(handle, F_SETFL, O_NONBLOCK);
242 memset(&sa, 0, sizeof(sa));
243 sa.sun_family = AF_UNIX;
244 strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
245 sa.sun_path[sizeof(sa.sun_path)-1] = '\0';
247 ret = connect(handle, (struct sockaddr *) &sa, sizeof(sa));
248 if (ret < 0 && errno != EINPROGRESS) {
249 int old_errno = errno;
255 return g_io_channel_new(handle);
258 /* Disconnect socket */
259 void net_disconnect(GIOChannel *handle)
261 g_return_if_fail(handle != NULL);
263 g_io_channel_close(handle);
264 g_io_channel_unref(handle);
267 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
269 GIOChannel *net_listen(IPADDR *my_ip, int *port)
271 union sockaddr_union so;
272 int ret, handle, opt = 1;
275 g_return_val_if_fail(port != NULL, NULL);
277 memset(&so, 0, sizeof(so));
278 sin_set_ip(&so, my_ip);
279 sin_set_port(&so, *port);
281 /* create the socket */
282 handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
284 if (handle == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) {
285 /* IPv6 is not supported by OS */
286 so.sin.sin_family = AF_INET;
287 so.sin.sin_addr.s_addr = INADDR_ANY;
289 handle = socket(AF_INET, SOCK_STREAM, 0);
295 /* set socket options */
297 fcntl(handle, F_SETFL, O_NONBLOCK);
299 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
300 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &opt, sizeof(opt));
302 /* specify the address/port we want to listen in */
303 ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
305 /* get the actual port we started listen */
306 len = SIZEOF_SOCKADDR(so);
307 ret = getsockname(handle, &so.sa, &len);
309 *port = sin_get_port(&so);
311 /* start listening */
312 if (listen(handle, 1) >= 0)
313 return g_io_channel_new(handle);
323 /* Accept a connection on a socket */
324 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
326 union sockaddr_union so;
330 g_return_val_if_fail(handle != NULL, NULL);
332 addrlen = sizeof(so);
333 ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
338 if (addr != NULL) sin_get_ip(&so, addr);
339 if (port != NULL) *port = sin_get_port(&so);
342 fcntl(ret, F_SETFL, O_NONBLOCK);
344 return g_io_channel_new(ret);
347 /* Read data from socket, return number of bytes read, -1 = error */
348 int net_receive(GIOChannel *handle, char *buf, int len)
354 g_return_val_if_fail(handle != NULL, -1);
355 g_return_val_if_fail(buf != NULL, -1);
357 status = g_io_channel_read_chars(handle, buf, len, &ret, &err);
359 g_warning(err->message);
362 if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF)
363 return -1; /* disconnected */
368 /* Transmit data, return number of bytes sent, -1 = error */
369 int net_transmit(GIOChannel *handle, const char *data, int len)
375 g_return_val_if_fail(handle != NULL, -1);
376 g_return_val_if_fail(data != NULL, -1);
378 status = g_io_channel_write_chars(handle, (char *) data, len, &ret, &err);
380 g_warning(err->message);
383 if (status == G_IO_STATUS_ERROR)
389 /* Get socket address/port */
390 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
392 union sockaddr_union so;
395 g_return_val_if_fail(handle != NULL, -1);
396 g_return_val_if_fail(addr != NULL, -1);
398 addrlen = sizeof(so);
399 if (getsockname(g_io_channel_unix_get_fd(handle),
400 (struct sockaddr *) &so, &addrlen) == -1)
403 sin_get_ip(&so, addr);
404 if (port) *port = sin_get_port(&so);
409 /* Get IP addresses for host, both IPv4 and IPv6 if possible.
410 If ip->family is 0, the address wasn't found.
411 Returns 0 = ok, others = error code for net_gethosterror() */
412 int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
415 union sockaddr_union *so;
416 struct addrinfo hints, *ai, *ailist;
417 int ret, count_v4, count_v6, use_v4, use_v6;
423 g_return_val_if_fail(addr != NULL, -1);
425 memset(ip4, 0, sizeof(IPADDR));
426 memset(ip6, 0, sizeof(IPADDR));
429 memset(&hints, 0, sizeof(struct addrinfo));
430 hints.ai_socktype = SOCK_STREAM;
432 /* save error to host_error for later use */
433 ret = getaddrinfo(addr, NULL, &hints, &ailist);
438 count_v4 = count_v6 = 0;
439 for (ai = ailist; ai != NULL; ai = ai->ai_next) {
440 if (ai->ai_family == AF_INET)
442 else if (ai->ai_family == AF_INET6)
446 if (count_v4 == 0 && count_v6 == 0)
447 return HOST_NOT_FOUND; /* shouldn't happen? */
449 /* if there are multiple addresses, return random one */
450 use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4;
451 use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6;
453 count_v4 = count_v6 = 0;
454 for (ai = ailist; ai != NULL; ai = ai->ai_next) {
455 so = (union sockaddr_union *) ai->ai_addr;
457 if (ai->ai_family == AF_INET) {
458 if (use_v4 == count_v4)
461 } else if (ai->ai_family == AF_INET6) {
462 if (use_v6 == count_v6)
467 freeaddrinfo(ailist);
470 hp = gethostbyname(addr);
476 while (hp->h_addr_list[count] != NULL)
480 return HOST_NOT_FOUND; /* shouldn't happen? */
482 /* if there are multiple addresses, return random one */
483 ip4->family = AF_INET;
484 memcpy(&ip4->ip, hp->h_addr_list[rand() % count], 4);
490 /* Get name for host, *name should be g_free()'d unless it's NULL.
491 Return values are the same as with net_gethostbyname() */
492 int net_gethostbyaddr(IPADDR *ip, char **name)
495 union sockaddr_union so;
497 char hostname[NI_MAXHOST];
502 g_return_val_if_fail(ip != NULL, -1);
503 g_return_val_if_fail(name != NULL, -1);
507 memset(&so, 0, sizeof(so));
510 /* save error to host_error for later use */
511 host_error = getnameinfo((struct sockaddr *) &so, sizeof(so),
512 hostname, sizeof(hostname), NULL, 0, 0);
516 *name = g_strdup(hostname);
518 if (ip->family != AF_INET) return -1;
519 hp = gethostbyaddr((const char *) &ip->ip, 4, AF_INET);
520 if (hp == NULL) return -1;
522 *name = g_strdup(hp->h_name);
528 int net_ip2host(IPADDR *ip, char *host)
531 if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
536 if (ip->family != AF_INET) {
537 strcpy(host, "0.0.0.0");
539 ip4 = ntohl(ip->ip.s_addr);
540 g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
541 (ip4 & 0xff000000UL) >> 24,
542 (ip4 & 0x00ff0000) >> 16,
543 (ip4 & 0x0000ff00) >> 8,
550 int net_host2ip(const char *host, IPADDR *ip)
554 if (strchr(host, ':') != NULL) {
556 ip->family = AF_INET6;
558 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
565 ip->family = AF_INET;
566 #ifdef HAVE_INET_ATON
567 if (inet_aton(host, &ip->ip.s_addr) == 0)
570 addr = inet_addr(host);
571 if (addr == INADDR_NONE)
574 memcpy(&ip->ip, &addr, 4);
581 /* Get socket error */
582 int net_geterror(GIOChannel *handle)
585 socklen_t len = sizeof(data);
587 if (getsockopt(g_io_channel_unix_get_fd(handle),
588 SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
594 /* get error of net_gethostname() */
595 const char *net_gethosterror(int error)
598 g_return_val_if_fail(error != 0, NULL);
600 return gai_strerror(error);
604 return "Host not found";
606 return "No IP address found for name";
608 return "A non-recovable name server error occurred";
610 return "A temporary error on an authoritative name server";
618 /* return TRUE if host lookup failed because it didn't exist (ie. not
619 some error with name server) */
620 int net_hosterror_notfound(int error)
623 #ifdef EAI_NODATA /* NODATA is depricated */
624 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
626 return error != 1 && (error == EAI_NONAME);
629 return error == HOST_NOT_FOUND || error == NO_ADDRESS;
633 /* Get name of TCP service */
634 char *net_getservbyport(int port)
636 struct servent *entry;
638 entry = getservbyport(htons((unsigned short) port), "tcp");
639 return entry == NULL ? NULL : entry->s_name;
642 int is_ipv4_address(const char *host)
644 while (*host != '\0') {
645 if (*host != '.' && !i_isdigit(*host))
653 int is_ipv6_address(const char *host)
655 while (*host != '\0') {
656 if (*host != ':' && !i_isxdigit(*host))