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, ignore if bind() fails */
201 sin_set_ip(&so, my_ip);
202 bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
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) {
219 return g_io_channel_new(handle);
222 /* Disconnect socket */
223 void net_disconnect(GIOChannel *handle)
225 g_return_if_fail(handle != NULL);
227 g_io_channel_close(handle);
228 g_io_channel_unref(handle);
231 /* Listen for connections on a socket. if `my_ip' is NULL, listen in any
233 GIOChannel *net_listen(IPADDR *my_ip, int *port)
235 union sockaddr_union so;
236 int ret, handle, opt = 1;
239 g_return_val_if_fail(port != NULL, NULL);
241 memset(&so, 0, sizeof(so));
242 sin_set_port(&so, *port);
243 sin_set_ip(&so, my_ip);
245 /* create the socket */
246 handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
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;
253 handle = socket(AF_INET, SOCK_STREAM, 0);
259 /* set socket options */
261 fcntl(handle, F_SETFL, O_NONBLOCK);
263 setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
264 (char *) &opt, sizeof(opt));
265 setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
266 (char *) &opt, sizeof(opt));
268 /* specify the address/port we want to listen in */
269 ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
271 /* get the actual port we started listen */
272 len = SIZEOF_SOCKADDR(so);
273 ret = getsockname(handle, &so.sa, &len);
275 *port = sin_get_port(&so);
277 /* start listening */
278 if (listen(handle, 1) >= 0)
279 return g_io_channel_new(handle);
289 /* Accept a connection on a socket */
290 GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
292 union sockaddr_union so;
296 g_return_val_if_fail(handle != NULL, NULL);
298 addrlen = sizeof(so);
299 ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
304 if (addr != NULL) sin_get_ip(&so, addr);
305 if (port != NULL) *port = sin_get_port(&so);
308 fcntl(ret, F_SETFL, O_NONBLOCK);
310 return g_io_channel_new(ret);
313 /* Read data from socket, return number of bytes read, -1 = error */
314 int net_receive(GIOChannel *handle, char *buf, int len)
319 g_return_val_if_fail(handle != NULL, -1);
320 g_return_val_if_fail(buf != NULL, -1);
322 err = g_io_channel_read(handle, buf, len, &ret);
323 if (err == 0 && ret == 0)
324 return -1; /* disconnected */
326 if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
327 return 0; /* no bytes received */
329 return err == 0 ? (int)ret : -1;
332 /* Transmit data, return number of bytes sent, -1 = error */
333 int net_transmit(GIOChannel *handle, const char *data, int len)
338 g_return_val_if_fail(handle != NULL, -1);
339 g_return_val_if_fail(data != NULL, -1);
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)))
346 return err == 0 ? (int)ret : -1;
349 /* Get socket address/port */
350 int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
352 union sockaddr_union so;
355 g_return_val_if_fail(handle != NULL, -1);
356 g_return_val_if_fail(addr != NULL, -1);
358 addrlen = sizeof(so);
359 if (getsockname(g_io_channel_unix_get_fd(handle),
360 (struct sockaddr *) &so, &addrlen) == -1)
363 sin_get_ip(&so, addr);
364 if (port) *port = sin_get_port(&so);
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)
375 union sockaddr_union *so;
376 struct addrinfo hints, *ai, *origai;
377 char hbuf[NI_MAXHOST];
378 int host_error, count;
383 g_return_val_if_fail(addr != NULL, -1);
385 memset(ip4, 0, sizeof(IPADDR));
386 memset(ip6, 0, sizeof(IPADDR));
389 memset(&hints, 0, sizeof(struct addrinfo));
390 hints.ai_socktype = SOCK_STREAM;
392 /* save error to host_error for later use */
393 host_error = getaddrinfo(addr, NULL, &hints, &ai);
397 if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
398 sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
401 origai = ai; count = 0;
402 while (ai != NULL && count < 2) {
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) {
414 freeaddrinfo(origai);
416 hp = gethostbyname(addr);
417 if (hp == NULL) return h_errno;
419 ip4->family = AF_INET;
420 memcpy(&ip4->ip, hp->h_addr, 4);
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)
431 struct addrinfo req, *ai;
436 char ipname[MAX_IP_LEN];
438 g_return_val_if_fail(ip != NULL, -1);
439 g_return_val_if_fail(name != NULL, -1);
441 net_ip2host(ip, ipname);
445 memset(&req, 0, sizeof(struct addrinfo));
446 req.ai_socktype = SOCK_STREAM;
447 req.ai_flags = AI_CANONNAME;
449 /* save error to host_error for later use */
450 host_error = getaddrinfo(ipname, NULL, &req, &ai);
453 *name = g_strdup(ai->ai_canonname);
457 hp = gethostbyaddr(ipname, strlen(ipname), AF_INET);
458 if (hp == NULL) return -1;
460 *name = g_strdup(hp->h_name);
466 int net_ip2host(IPADDR *ip, char *host)
469 if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
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,
484 int net_host2ip(const char *host, IPADDR *ip)
489 if (strchr(host, ':') != NULL) {
491 ip->family = AF_INET6;
492 if (inet_pton(AF_INET6, host, &ip->ip) == 0)
498 ip->family = AF_INET;
499 #ifdef HAVE_INET_ATON
500 if (inet_aton(host, &ip->ip.s_addr) == 0)
503 addr = inet_addr(host);
504 if (addr == INADDR_NONE)
507 memcpy(&ip->ip, &addr, 4);
514 /* Get socket error */
515 int net_geterror(GIOChannel *handle)
518 socklen_t len = sizeof(data);
520 if (getsockopt(g_io_channel_unix_get_fd(handle),
521 SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
527 /* get error of net_gethostname() */
528 const char *net_gethosterror(int error)
531 g_return_val_if_fail(error != 0, NULL);
534 /* getnameinfo() failed ..
535 FIXME: does strerror return the right error message? */
536 return g_strerror(errno);
539 return gai_strerror(error);
543 return "Host not found";
545 return "No IP address found for name";
547 return "A non-recovable name server error occurred";
549 return "A temporary error on an authoritative name server";
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)
562 return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
564 return error == HOST_NOT_FOUND || error == NO_ADDRESS;
568 /* Get name of TCP service */
569 char *net_getservbyport(int port)
571 struct servent *entry;
573 entry = getservbyport(htons((unsigned short) port), "tcp");
574 return entry == NULL ? NULL : entry->s_name;
577 int is_ipv4_address(const char *host)
579 while (*host != '\0') {
580 if (*host != '.' && !isdigit(*host))
588 int is_ipv6_address(const char *host)
590 while (*host != '\0') {
591 if (*host != ':' && !isxdigit(*host))