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 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*/
59 int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
61 if (ip1->family != ip2->family)
65 if (ip1->family == AF_INET6)
66 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0;
69 return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
73 static void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
77 so->sin6.sin6_family = AF_INET6;
78 so->sin6.sin6_addr = in6addr_any;
80 so->sin.sin_family = AF_INET;
81 so->sin.sin_addr.s_addr = INADDR_ANY;
86 so->sin.sin_family = ip->family;
88 if (ip->family == AF_INET6)
89 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
92 memcpy(&so->sin.sin_addr, &ip->ip, 4);
95 void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
97 ip->family = so->sin.sin_family;
100 if (ip->family == AF_INET6)
101 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
104 memcpy(&ip->ip, &so->sin.sin_addr, 4);
107 static void sin_set_port(union sockaddr_union *so, int port)
110 if (so->sin.sin_family == AF_INET6)
111 so->sin6.sin6_port = htons((unsigned short)port);
114 so->sin.sin_port = htons((unsigned short)port);
117 static int sin_get_port(union sockaddr_union *so)
120 if (so->sin.sin_family == AF_INET6)
121 return ntohs(so->sin6.sin6_port);
123 return ntohs(so->sin.sin_port);
126 /* Connect to socket */
127 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
129 IPADDR ip4, ip6, *ip;
132 g_return_val_if_fail(addr != NULL, NULL);
134 family = my_ip == NULL ? 0 : my_ip->family;
135 if (net_gethostbyname(addr, &ip4, &ip6) == -1)
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 */
150 /* my_ip is IPv4 address, use it if possible */
159 return net_connect_ip(ip, port, my_ip);
162 /* Connect to socket with ip address */
163 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
165 union sockaddr_union so;
166 int handle, ret, opt = 1;
168 if (my_ip != NULL && ip->family != my_ip->family) {
169 g_warning("net_connect_ip(): ip->family != my_ip->family");
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);
181 /* set socket options */
183 fcntl(handle, F_SETFL, O_NONBLOCK);
185 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
186 (char *) &opt, sizeof(opt));
187 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
188 (char *) &opt, sizeof(opt));
190 /* set our own address */
192 sin_set_ip(&so, my_ip);
193 if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
194 int old_errno = errno;
204 sin_set_port(&so, port);
205 ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
208 if (ret < 0 && errno != EINPROGRESS)
210 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
213 int old_errno = errno;
219 return g_io_channel_new(handle);
222 /* Connect to named UNIX socket */
223 GIOChannel *net_connect_unix(const char *path)
225 struct sockaddr_un sa;
228 /* create the socket */
229 handle = socket(PF_UNIX, SOCK_STREAM, 0);
233 /* set socket options */
235 fcntl(handle, F_SETFL, O_NONBLOCK);
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';
244 ret = connect(handle, (struct sockaddr *) &sa, sizeof(sa));
245 if (ret < 0 && errno != EINPROGRESS) {
246 int old_errno = errno;
252 return g_io_channel_new(handle);
255 /* Disconnect socket */
256 void net_disconnect(GIOChannel *handle)
258 g_return_if_fail(handle != NULL);
260 g_io_channel_close(handle);
261 g_io_channel_unref(handle);
264 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
266 GIOChannel *net_listen(IPADDR *my_ip, int *port)
268 union sockaddr_union so;
269 int ret, handle, opt = 1;
272 g_return_val_if_fail(port != NULL, NULL);
274 memset(&so, 0, sizeof(so));
275 sin_set_ip(&so, my_ip);
276 sin_set_port(&so, *port);
278 /* create the socket */
279 handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
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;
286 handle = socket(AF_INET, SOCK_STREAM, 0);
292 /* set socket options */
294 fcntl(handle, F_SETFL, O_NONBLOCK);
296 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
297 (char *) &opt, sizeof(opt));
298 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
299 (char *) &opt, sizeof(opt));
301 /* specify the address/port we want to listen in */
302 ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
304 /* get the actual port we started listen */
305 len = SIZEOF_SOCKADDR(so);
306 ret = getsockname(handle, &so.sa, &len);
308 *port = sin_get_port(&so);
310 /* start listening */
311 if (listen(handle, 1) >= 0)
312 return g_io_channel_new(handle);
322 /* Accept a connection on a socket */
323 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
325 union sockaddr_union so;
329 g_return_val_if_fail(handle != NULL, NULL);
331 addrlen = sizeof(so);
332 ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
337 if (addr != NULL) sin_get_ip(&so, addr);
338 if (port != NULL) *port = sin_get_port(&so);
341 fcntl(ret, F_SETFL, O_NONBLOCK);
343 return g_io_channel_new(ret);
346 /* Read data from socket, return number of bytes read, -1 = error */
347 int net_receive(GIOChannel *handle, char *buf, int len)
352 g_return_val_if_fail(handle != NULL, -1);
353 g_return_val_if_fail(buf != NULL, -1);
355 err = g_io_channel_read(handle, buf, len, &ret);
356 if (err == 0 && ret == 0)
357 return -1; /* disconnected */
359 if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
360 return 0; /* no bytes received */
362 return err == 0 ? (int)ret : -1;
365 /* Transmit data, return number of bytes sent, -1 = error */
366 int net_transmit(GIOChannel *handle, const char *data, int len)
371 g_return_val_if_fail(handle != NULL, -1);
372 g_return_val_if_fail(data != NULL, -1);
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)))
379 return err == 0 ? (int)ret : -1;
382 /* Get socket address/port */
383 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
385 union sockaddr_union so;
388 g_return_val_if_fail(handle != NULL, -1);
389 g_return_val_if_fail(addr != NULL, -1);
391 addrlen = sizeof(so);
392 if (getsockname(g_io_channel_unix_get_fd(handle),
393 (struct sockaddr *) &so, &addrlen) == -1)
396 sin_get_ip(&so, addr);
397 if (port) *port = sin_get_port(&so);
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)
408 union sockaddr_union *so;
409 struct addrinfo hints, *ai, *ailist;
410 int ret, count_v4, count_v6, use_v4, use_v6;
416 g_return_val_if_fail(addr != NULL, -1);
418 memset(ip4, 0, sizeof(IPADDR));
419 memset(ip6, 0, sizeof(IPADDR));
422 memset(&hints, 0, sizeof(struct addrinfo));
423 hints.ai_socktype = SOCK_STREAM;
425 /* save error to host_error for later use */
426 ret = getaddrinfo(addr, NULL, &hints, &ailist);
431 count_v4 = count_v6 = 0;
432 for (ai = ailist; ai != NULL; ai = ai->ai_next) {
433 if (ai->ai_family == AF_INET)
435 else if (ai->ai_family == AF_INET6)
439 if (count_v4 == 0 && count_v6 == 0)
440 return HOST_NOT_FOUND; /* shouldn't happen? */
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;
446 count_v4 = count_v6 = 0;
447 for (ai = ailist; ai != NULL; ai = ai->ai_next) {
448 so = (union sockaddr_union *) ai->ai_addr;
450 if (ai->ai_family == AF_INET) {
451 if (use_v4 == count_v4)
454 } else if (ai->ai_family == AF_INET6) {
455 if (use_v6 == count_v6)
460 freeaddrinfo(ailist);
463 hp = gethostbyname(addr);
469 while (hp->h_addr_list[count] != NULL)
473 return HOST_NOT_FOUND; /* shouldn't happen? */
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);
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)
488 union sockaddr_union so;
490 char hostname[NI_MAXHOST];
495 g_return_val_if_fail(ip != NULL, -1);
496 g_return_val_if_fail(name != NULL, -1);
500 memset(&so, 0, sizeof(so));
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);
509 *name = g_strdup(hostname);
511 if (ip->family != AF_INET) return -1;
512 hp = gethostbyaddr((const char *) &ip->ip, 4, AF_INET);
513 if (hp == NULL) return -1;
515 *name = g_strdup(hp->h_name);
521 int net_ip2host(IPADDR *ip, char *host)
524 if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
529 if (ip->family != AF_INET) {
530 strcpy(host, "0.0.0.0");
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,
543 int net_host2ip(const char *host, IPADDR *ip)
547 if (strchr(host, ':') != NULL) {
549 ip->family = AF_INET6;
551 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
558 ip->family = AF_INET;
559 #ifdef HAVE_INET_ATON
560 if (inet_aton(host, &ip->ip.s_addr) == 0)
563 addr = inet_addr(host);
564 if (addr == INADDR_NONE)
567 memcpy(&ip->ip, &addr, 4);
574 /* Get socket error */
575 int net_geterror(GIOChannel *handle)
578 socklen_t len = sizeof(data);
580 if (getsockopt(g_io_channel_unix_get_fd(handle),
581 SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
587 /* get error of net_gethostname() */
588 const char *net_gethosterror(int error)
591 g_return_val_if_fail(error != 0, NULL);
594 /* getnameinfo() failed ..
595 FIXME: does strerror return the right error message? */
596 return g_strerror(errno);
599 return gai_strerror(error);
603 return "Host not found";
605 return "No IP address found for name";
607 return "A non-recovable name server error occurred";
609 return "A temporary error on an authoritative name server";
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)
622 #ifdef EAI_NODATA /* NODATA is depricated */
623 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
625 return error != 1 && (error == EAI_NONAME);
628 return error == HOST_NOT_FOUND || error == NO_ADDRESS;
632 /* Get name of TCP service */
633 char *net_getservbyport(int port)
635 struct servent *entry;
637 entry = getservbyport(htons((unsigned short) port), "tcp");
638 return entry == NULL ? NULL : entry->s_name;
641 int is_ipv4_address(const char *host)
643 while (*host != '\0') {
644 if (*host != '.' && !i_isdigit(*host))
652 int is_ipv6_address(const char *host)
654 while (*host != '\0') {
655 if (*host != ':' && !i_isxdigit(*host))