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
25 # define INADDR_NONE INADDR_BROADCAST
28 union sockaddr_union {
30 struct sockaddr_in sin;
32 struct sockaddr_in6 sin6;
37 # define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
38 sizeof(so.sin6) : sizeof(so.sin))
40 # define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
44 # define g_io_channel_new(handle) g_io_channel_win32_new_stream_socket(handle)
46 # define g_io_channel_new(handle) g_io_channel_unix_new(handle)
49 /* Cygwin need this, don't know others.. */
50 /*#define BLOCKING_SOCKETS 1*/
52 int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
54 if (ip1->family != ip2->family)
58 if (ip1->family == AF_INET6)
59 return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0;
62 return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
66 /* copy IP to sockaddr */
72 void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
76 so->sin6.sin6_family = AF_INET6;
77 so->sin6.sin6_addr = in6addr_any;
79 so->sin.sin_family = AF_INET;
80 so->sin.sin_addr.s_addr = INADDR_ANY;
85 so->sin.sin_family = ip->family;
87 if (ip->family == AF_INET6)
88 memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
91 memcpy(&so->sin.sin_addr, &ip->ip, 4);
94 void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
96 ip->family = so->sin.sin_family;
99 if (ip->family == AF_INET6)
100 memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
103 memcpy(&ip->ip, &so->sin.sin_addr, 4);
111 void sin_set_port(union sockaddr_union *so, int port)
114 if (so->sin.sin_family == AF_INET6)
115 so->sin6.sin6_port = htons(port);
118 so->sin.sin_port = htons((unsigned short)port);
126 int sin_get_port(union sockaddr_union *so)
129 if (so->sin.sin_family == AF_INET6)
130 return ntohs(so->sin6.sin6_port);
132 return ntohs(so->sin.sin_port);
135 /* Connect to socket */
136 GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
138 IPADDR ip4, ip6, *ip;
141 g_return_val_if_fail(addr != NULL, NULL);
143 family = my_ip == NULL ? 0 : my_ip->family;
144 if (net_gethostbyname(addr, &ip4, &ip6) == -1)
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 */
159 /* my_ip is IPv4 address, use it if possible */
168 return net_connect_ip(ip, port, my_ip);
171 /* Connect to socket with ip address */
172 GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
174 union sockaddr_union so;
175 int handle, ret, opt = 1;
177 if (my_ip != NULL && ip->family != my_ip->family) {
178 g_warning("net_connect_ip(): ip->family != my_ip->family");
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);
190 /* set socket options */
192 fcntl(handle, F_SETFL, O_NONBLOCK);
194 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
195 (char *) &opt, sizeof(opt));
196 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
197 (char *) &opt, sizeof(opt));
199 /* set our own address */
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));
211 sin_set_port(&so, port);
212 ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
215 if (ret < 0 && errno != EINPROGRESS)
217 if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
224 return g_io_channel_new(handle);
227 /* Disconnect socket */
228 void net_disconnect(GIOChannel *handle)
230 g_return_if_fail(handle != NULL);
232 g_io_channel_close(handle);
233 g_io_channel_unref(handle);
236 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
238 GIOChannel *net_listen(IPADDR *my_ip, int *port)
240 union sockaddr_union so;
241 int ret, handle, opt = 1;
244 g_return_val_if_fail(port != NULL, NULL);
246 memset(&so, 0, sizeof(so));
247 sin_set_port(&so, *port);
248 sin_set_ip(&so, my_ip);
250 /* create the socket */
251 handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
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;
258 handle = socket(AF_INET, SOCK_STREAM, 0);
264 /* set socket options */
266 fcntl(handle, F_SETFL, O_NONBLOCK);
268 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
269 (char *) &opt, sizeof(opt));
270 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
271 (char *) &opt, sizeof(opt));
273 /* specify the address/port we want to listen in */
274 ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
276 /* get the actual port we started listen */
277 len = SIZEOF_SOCKADDR(so);
278 ret = getsockname(handle, &so.sa, &len);
280 *port = sin_get_port(&so);
282 /* start listening */
283 if (listen(handle, 1) >= 0)
284 return g_io_channel_new(handle);
294 /* Accept a connection on a socket */
295 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
297 union sockaddr_union so;
301 g_return_val_if_fail(handle != NULL, NULL);
303 addrlen = sizeof(so);
304 ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
309 if (addr != NULL) sin_get_ip(&so, addr);
310 if (port != NULL) *port = sin_get_port(&so);
313 fcntl(ret, F_SETFL, O_NONBLOCK);
315 return g_io_channel_new(ret);
318 /* Read data from socket, return number of bytes read, -1 = error */
319 int net_receive(GIOChannel *handle, char *buf, int len)
324 g_return_val_if_fail(handle != NULL, -1);
325 g_return_val_if_fail(buf != NULL, -1);
327 err = g_io_channel_read(handle, buf, len, &ret);
328 if (err == 0 && ret == 0)
329 return -1; /* disconnected */
331 if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
332 return 0; /* no bytes received */
334 return err == 0 ? (int)ret : -1;
337 /* Transmit data, return number of bytes sent, -1 = error */
338 int net_transmit(GIOChannel *handle, const char *data, int len)
343 g_return_val_if_fail(handle != NULL, -1);
344 g_return_val_if_fail(data != NULL, -1);
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)))
351 return err == 0 ? (int)ret : -1;
354 /* Get socket address/port */
355 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
357 union sockaddr_union so;
360 g_return_val_if_fail(handle != NULL, -1);
361 g_return_val_if_fail(addr != NULL, -1);
363 addrlen = sizeof(so);
364 if (getsockname(g_io_channel_unix_get_fd(handle),
365 (struct sockaddr *) &so, &addrlen) == -1)
368 sin_get_ip(&so, addr);
369 if (port) *port = sin_get_port(&so);
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)
380 union sockaddr_union *so;
381 struct addrinfo hints, *ai, *ailist;
387 g_return_val_if_fail(addr != NULL, -1);
389 memset(ip4, 0, sizeof(IPADDR));
390 memset(ip6, 0, sizeof(IPADDR));
393 memset(&hints, 0, sizeof(struct addrinfo));
394 hints.ai_socktype = SOCK_STREAM;
396 /* save error to host_error for later use */
397 ret = getaddrinfo(addr, NULL, &hints, &ailist);
402 for (ai = ailist; ai != NULL && count < 2; ai = ai->ai_next) {
403 so = (union sockaddr_union *) ai->ai_addr;
405 if (ai->ai_family == AF_INET6 && ip6->family == 0) {
408 } else if (ai->ai_family == AF_INET && ip4->family == 0) {
413 freeaddrinfo(ailist);
414 return count > 0 ? 0 : 1;
416 hp = gethostbyname(addr);
421 ip4->family = AF_INET;
422 memcpy(&ip4->ip, hp->h_addr, 4);
428 /* Get name for host, *name should be g_free()'d unless it's NULL.
429 Return values are the same as with net_gethostbyname() */
430 int net_gethostbyaddr(IPADDR *ip, char **name)
433 struct addrinfo req, *ai;
438 char ipname[MAX_IP_LEN];
440 g_return_val_if_fail(ip != NULL, -1);
441 g_return_val_if_fail(name != NULL, -1);
443 net_ip2host(ip, ipname);
447 memset(&req, 0, sizeof(struct addrinfo));
448 req.ai_socktype = SOCK_STREAM;
449 req.ai_flags = AI_CANONNAME;
451 /* save error to host_error for later use */
452 host_error = getaddrinfo(ipname, NULL, &req, &ai);
455 *name = g_strdup(ai->ai_canonname);
459 hp = gethostbyaddr(ipname, strlen(ipname), AF_INET);
460 if (hp == NULL) return -1;
462 *name = g_strdup(hp->h_name);
468 int net_ip2host(IPADDR *ip, char *host)
471 if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
476 ip4 = ntohl(ip->ip.s_addr);
477 g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
478 (ip4 & 0xff000000UL) >> 24,
479 (ip4 & 0x00ff0000) >> 16,
480 (ip4 & 0x0000ff00) >> 8,
486 int net_host2ip(const char *host, IPADDR *ip)
491 if (strchr(host, ':') != NULL) {
493 ip->family = AF_INET6;
494 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
500 ip->family = AF_INET;
501 #ifdef HAVE_INET_ATON
502 if (inet_aton(host, &ip->ip.s_addr) == 0)
505 addr = inet_addr(host);
506 if (addr == INADDR_NONE)
509 memcpy(&ip->ip, &addr, 4);
516 /* Get socket error */
517 int net_geterror(GIOChannel *handle)
520 socklen_t len = sizeof(data);
522 if (getsockopt(g_io_channel_unix_get_fd(handle),
523 SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
529 /* get error of net_gethostname() */
530 const char *net_gethosterror(int error)
533 g_return_val_if_fail(error != 0, NULL);
536 /* getnameinfo() failed ..
537 FIXME: does strerror return the right error message? */
538 return g_strerror(errno);
541 return gai_strerror(error);
545 return "Host not found";
547 return "No IP address found for name";
549 return "A non-recovable name server error occurred";
551 return "A temporary error on an authoritative name server";
559 /* return TRUE if host lookup failed because it didn't exist (ie. not
560 some error with name server) */
561 int net_hosterror_notfound(int error)
564 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
566 return error == HOST_NOT_FOUND || error == NO_ADDRESS;
570 /* Get name of TCP service */
571 char *net_getservbyport(int port)
573 struct servent *entry;
575 entry = getservbyport(htons((unsigned short) port), "tcp");
576 return entry == NULL ? NULL : entry->s_name;
579 int is_ipv4_address(const char *host)
581 while (*host != '\0') {
582 if (*host != '.' && !i_isdigit(*host))
590 int is_ipv6_address(const char *host)
592 while (*host != '\0') {
593 if (*host != ':' && !isxdigit(*host))