2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
network.c : Network stuff
|
|
|
|
|
|
|
|
Copyright (C) 1999-2000 Timo Sirainen
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
2000-06-06 15:13:35 -04:00
|
|
|
#ifndef INADDR_NONE
|
2000-12-04 17:57:18 -05:00
|
|
|
# define INADDR_NONE INADDR_BROADCAST
|
|
|
|
#endif
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
union sockaddr_union {
|
|
|
|
struct sockaddr sa;
|
|
|
|
struct sockaddr_in sin;
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2000-11-11 15:27:37 -05:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
# define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
|
|
|
|
sizeof(so.sin6) : sizeof(so.sin))
|
|
|
|
#else
|
|
|
|
# define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
|
|
|
|
#endif
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
#ifdef WIN32
|
|
|
|
# define g_io_channel_new(handle) g_io_channel_win32_new_stream_socket(handle)
|
|
|
|
#else
|
|
|
|
# define g_io_channel_new(handle) g_io_channel_unix_new(handle)
|
|
|
|
#endif
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* Cygwin need this, don't know others.. */
|
|
|
|
/*#define BLOCKING_SOCKETS 1*/
|
|
|
|
|
|
|
|
int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
|
|
|
|
{
|
|
|
|
if (ip1->family != ip2->family)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (ip1->family == AF_INET6)
|
2001-03-03 22:00:35 -05:00
|
|
|
return memcmp(&ip1->ip, &ip2->ip, sizeof(ip1->ip)) == 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
#endif
|
|
|
|
|
2001-03-03 22:00:35 -05:00
|
|
|
return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* copy IP to sockaddr */
|
2000-11-11 15:27:37 -05:00
|
|
|
#ifdef G_CAN_INLINE
|
|
|
|
G_INLINE_FUNC
|
|
|
|
#else
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
void sin_set_ip(union sockaddr_union *so, const IPADDR *ip)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-05-25 07:30:47 -04:00
|
|
|
if (ip == NULL) {
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
so->sin6.sin6_family = AF_INET6;
|
2000-05-25 13:12:44 -04:00
|
|
|
so->sin6.sin6_addr = in6addr_any;
|
2000-05-25 07:30:47 -04:00
|
|
|
#else
|
|
|
|
so->sin.sin_family = AF_INET;
|
|
|
|
so->sin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
#endif
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
so->sin.sin_family = ip->family;
|
2000-04-26 04:03:38 -04:00
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (ip->family == AF_INET6)
|
2001-03-03 22:00:35 -05:00
|
|
|
memcpy(&so->sin6.sin6_addr, &ip->ip, sizeof(ip->ip));
|
2000-04-26 04:03:38 -04:00
|
|
|
else
|
|
|
|
#endif
|
2001-03-03 22:00:35 -05:00
|
|
|
memcpy(&so->sin.sin_addr, &ip->ip, 4);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:27:37 -05:00
|
|
|
void sin_get_ip(const union sockaddr_union *so, IPADDR *ip)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
ip->family = so->sin.sin_family;
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (ip->family == AF_INET6)
|
2001-03-03 22:00:35 -05:00
|
|
|
memcpy(&ip->ip, &so->sin6.sin6_addr, sizeof(ip->ip));
|
2000-04-26 04:03:38 -04:00
|
|
|
else
|
|
|
|
#endif
|
2001-03-03 22:00:35 -05:00
|
|
|
memcpy(&ip->ip, &so->sin.sin_addr, 4);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:27:37 -05:00
|
|
|
#ifdef G_CAN_INLINE
|
|
|
|
G_INLINE_FUNC
|
|
|
|
#else
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
void sin_set_port(union sockaddr_union *so, int port)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (so->sin.sin_family == AF_INET6)
|
|
|
|
so->sin6.sin6_port = htons(port);
|
|
|
|
else
|
|
|
|
#endif
|
2000-10-26 14:57:23 -04:00
|
|
|
so->sin.sin_port = htons((unsigned short)port);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-11-11 15:27:37 -05:00
|
|
|
#ifdef G_CAN_INLINE
|
|
|
|
G_INLINE_FUNC
|
|
|
|
#else
|
|
|
|
static
|
|
|
|
#endif
|
|
|
|
int sin_get_port(union sockaddr_union *so)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (so->sin.sin_family == AF_INET6)
|
|
|
|
return ntohs(so->sin6.sin6_port);
|
|
|
|
#endif
|
|
|
|
return ntohs(so->sin.sin_port);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to socket */
|
2000-12-04 17:57:18 -05:00
|
|
|
GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-03-03 20:47:13 -05:00
|
|
|
IPADDR ip4, ip6, *ip;
|
2001-02-09 16:26:50 -05:00
|
|
|
int family;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_val_if_fail(addr != NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-09 16:26:50 -05:00
|
|
|
family = my_ip == NULL ? 0 : my_ip->family;
|
2001-03-03 20:47:13 -05:00
|
|
|
if (net_gethostbyname(addr, &ip4, &ip6) == -1)
|
2000-12-04 17:57:18 -05:00
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
if (my_ip == NULL) {
|
|
|
|
/* prefer IPv4 addresses */
|
|
|
|
ip = ip4.family != 0 ? &ip4 : &ip6;
|
|
|
|
} else if (IPADDR_IS_V6(my_ip)) {
|
|
|
|
/* my_ip is IPv6 address, use it if possible */
|
|
|
|
if (ip6.family != 0)
|
|
|
|
ip = &ip6;
|
|
|
|
else {
|
|
|
|
my_ip = NULL;
|
|
|
|
ip = &ip4;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* my_ip is IPv4 address, use it if possible */
|
|
|
|
if (ip4.family != 0)
|
|
|
|
ip = &ip4;
|
|
|
|
else {
|
|
|
|
my_ip = NULL;
|
|
|
|
ip = &ip6;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return net_connect_ip(ip, port, my_ip);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to socket with ip address */
|
2000-12-04 17:57:18 -05:00
|
|
|
GIOChannel *net_connect_ip(IPADDR *ip, int port, IPADDR *my_ip)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
union sockaddr_union so;
|
|
|
|
int handle, ret, opt = 1;
|
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
if (my_ip != NULL && ip->family != my_ip->family) {
|
|
|
|
g_warning("net_connect_ip(): ip->family != my_ip->family");
|
|
|
|
my_ip = NULL;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* create the socket */
|
|
|
|
memset(&so, 0, sizeof(so));
|
|
|
|
so.sin.sin_family = ip->family;
|
|
|
|
handle = socket(ip->family, SOCK_STREAM, 0);
|
|
|
|
|
|
|
|
if (handle == -1)
|
2000-12-04 17:57:18 -05:00
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* set socket options */
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-04-26 04:03:38 -04:00
|
|
|
fcntl(handle, F_SETFL, O_NONBLOCK);
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
2000-07-16 16:18:05 -04:00
|
|
|
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(char *) &opt, sizeof(opt));
|
|
|
|
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
|
|
|
|
(char *) &opt, sizeof(opt));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-11-07 09:35:00 -05:00
|
|
|
/* set our own address */
|
2000-04-26 04:03:38 -04:00
|
|
|
if (my_ip != NULL) {
|
|
|
|
sin_set_ip(&so, my_ip);
|
2001-11-07 09:35:00 -05:00
|
|
|
if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) == -1) {
|
|
|
|
/* failed, set it back to INADDR_ANY */
|
|
|
|
sin_set_ip(&so, NULL);
|
|
|
|
bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* connect */
|
|
|
|
sin_set_ip(&so, ip);
|
|
|
|
sin_set_port(&so, port);
|
2000-11-11 15:27:37 -05:00
|
|
|
ret = connect(handle, &so.sa, SIZEOF_SOCKADDR(so));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-11 05:15:34 -05:00
|
|
|
#ifndef WIN32
|
2002-01-04 16:28:26 -05:00
|
|
|
if (ret < 0 && errno != EINPROGRESS)
|
2001-01-11 05:15:34 -05:00
|
|
|
#else
|
2002-01-04 16:28:26 -05:00
|
|
|
if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
|
2001-01-11 05:15:34 -05:00
|
|
|
#endif
|
2002-01-04 16:28:26 -05:00
|
|
|
{
|
2000-04-26 04:03:38 -04:00
|
|
|
close(handle);
|
2000-12-04 17:57:18 -05:00
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
return g_io_channel_new(handle);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Disconnect socket */
|
2000-12-04 17:57:18 -05:00
|
|
|
void net_disconnect(GIOChannel *handle)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_if_fail(handle != NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_io_channel_close(handle);
|
|
|
|
g_io_channel_unref(handle);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
/* Listen for connections on a socket. if `my_ip' is NULL, listen in any
|
|
|
|
address. */
|
2000-12-04 17:57:18 -05:00
|
|
|
GIOChannel *net_listen(IPADDR *my_ip, int *port)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
union sockaddr_union so;
|
|
|
|
int ret, handle, opt = 1;
|
2000-11-11 15:27:37 -05:00
|
|
|
socklen_t len;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_val_if_fail(port != NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
memset(&so, 0, sizeof(so));
|
2000-05-25 07:30:47 -04:00
|
|
|
sin_set_port(&so, *port);
|
|
|
|
sin_set_ip(&so, my_ip);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
/* create the socket */
|
|
|
|
handle = socket(so.sin.sin_family, SOCK_STREAM, 0);
|
2001-03-14 17:26:00 -05:00
|
|
|
#ifdef HAVE_IPV6
|
2001-12-14 17:19:54 -05:00
|
|
|
if (handle == -1 && (errno == EINVAL || errno == EAFNOSUPPORT)) {
|
2001-03-14 17:26:00 -05:00
|
|
|
/* IPv6 is not supported by OS */
|
|
|
|
so.sin.sin_family = AF_INET;
|
|
|
|
so.sin.sin_addr.s_addr = INADDR_ANY;
|
|
|
|
|
|
|
|
handle = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
}
|
|
|
|
#endif
|
2000-04-26 04:03:38 -04:00
|
|
|
if (handle == -1)
|
2000-12-04 17:57:18 -05:00
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* set socket options */
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-04-26 04:03:38 -04:00
|
|
|
fcntl(handle, F_SETFL, O_NONBLOCK);
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
2000-07-16 16:18:05 -04:00
|
|
|
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR,
|
|
|
|
(char *) &opt, sizeof(opt));
|
|
|
|
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE,
|
|
|
|
(char *) &opt, sizeof(opt));
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* specify the address/port we want to listen in */
|
2000-11-11 15:27:37 -05:00
|
|
|
ret = bind(handle, &so.sa, SIZEOF_SOCKADDR(so));
|
2000-12-04 17:57:18 -05:00
|
|
|
if (ret >= 0) {
|
|
|
|
/* get the actual port we started listen */
|
|
|
|
len = SIZEOF_SOCKADDR(so);
|
|
|
|
ret = getsockname(handle, &so.sa, &len);
|
|
|
|
if (ret >= 0) {
|
|
|
|
*port = sin_get_port(&so);
|
|
|
|
|
|
|
|
/* start listening */
|
|
|
|
if (listen(handle, 1) >= 0)
|
|
|
|
return g_io_channel_new(handle);
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
/* error */
|
|
|
|
close(handle);
|
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Accept a connection on a socket */
|
2000-12-04 17:57:18 -05:00
|
|
|
GIOChannel *net_accept(GIOChannel *handle, IPADDR *addr, int *port)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
union sockaddr_union so;
|
|
|
|
int ret;
|
|
|
|
socklen_t addrlen;
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_val_if_fail(handle != NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-01-02 02:05:07 -05:00
|
|
|
addrlen = sizeof(so);
|
2000-12-04 17:57:18 -05:00
|
|
|
ret = accept(g_io_channel_unix_get_fd(handle), &so.sa, &addrlen);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (ret < 0)
|
2000-12-04 17:57:18 -05:00
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
if (addr != NULL) sin_get_ip(&so, addr);
|
|
|
|
if (port != NULL) *port = sin_get_port(&so);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-04-26 04:03:38 -04:00
|
|
|
fcntl(ret, F_SETFL, O_NONBLOCK);
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
2000-12-04 17:57:18 -05:00
|
|
|
return g_io_channel_new(ret);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read data from socket, return number of bytes read, -1 = error */
|
2000-12-04 17:57:18 -05:00
|
|
|
int net_receive(GIOChannel *handle, char *buf, int len)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-01-15 18:36:11 -05:00
|
|
|
unsigned int ret;
|
|
|
|
int err;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_val_if_fail(handle != NULL, -1);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_val_if_fail(buf != NULL, -1);
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
err = g_io_channel_read(handle, buf, len, &ret);
|
|
|
|
if (err == 0 && ret == 0)
|
2000-04-26 04:03:38 -04:00
|
|
|
return -1; /* disconnected */
|
|
|
|
|
2000-12-08 15:13:21 -05:00
|
|
|
if (err == G_IO_ERROR_AGAIN || (err != 0 && errno == EINTR))
|
2000-04-26 04:03:38 -04:00
|
|
|
return 0; /* no bytes received */
|
|
|
|
|
2001-01-15 18:36:11 -05:00
|
|
|
return err == 0 ? (int)ret : -1;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Transmit data, return number of bytes sent, -1 = error */
|
2000-12-04 17:57:18 -05:00
|
|
|
int net_transmit(GIOChannel *handle, const char *data, int len)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2001-01-15 18:36:11 -05:00
|
|
|
unsigned int ret;
|
|
|
|
int err;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_val_if_fail(handle != NULL, -1);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_val_if_fail(data != NULL, -1);
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
err = g_io_channel_write(handle, (char *) data, len, &ret);
|
2000-12-19 12:48:42 -05:00
|
|
|
if (err == G_IO_ERROR_AGAIN ||
|
|
|
|
(err != 0 && (errno == EINTR || errno == EPIPE)))
|
2000-04-26 04:03:38 -04:00
|
|
|
return 0;
|
|
|
|
|
2001-01-15 18:36:11 -05:00
|
|
|
return err == 0 ? (int)ret : -1;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Get socket address/port */
|
2000-12-04 17:57:18 -05:00
|
|
|
int net_getsockname(GIOChannel *handle, IPADDR *addr, int *port)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
union sockaddr_union so;
|
2001-01-02 02:05:07 -05:00
|
|
|
socklen_t addrlen;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_val_if_fail(handle != NULL, -1);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_return_val_if_fail(addr != NULL, -1);
|
|
|
|
|
2001-01-02 02:05:07 -05:00
|
|
|
addrlen = sizeof(so);
|
2000-12-04 17:57:18 -05:00
|
|
|
if (getsockname(g_io_channel_unix_get_fd(handle),
|
2001-01-02 02:05:07 -05:00
|
|
|
(struct sockaddr *) &so, &addrlen) == -1)
|
2000-04-26 04:03:38 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
sin_get_ip(&so, addr);
|
|
|
|
if (port) *port = sin_get_port(&so);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
/* Get IP addresses for host, both IPv4 and IPv6 if possible.
|
|
|
|
If ip->family is 0, the address wasn't found.
|
|
|
|
Returns 0 = ok, others = error code for net_gethosterror() */
|
|
|
|
int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
union sockaddr_union *so;
|
2001-03-03 20:47:13 -05:00
|
|
|
struct addrinfo hints, *ai, *origai;
|
2000-04-26 04:03:38 -04:00
|
|
|
char hbuf[NI_MAXHOST];
|
2001-03-03 20:47:13 -05:00
|
|
|
int host_error, count;
|
2000-04-26 04:03:38 -04:00
|
|
|
#else
|
|
|
|
struct hostent *hp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
g_return_val_if_fail(addr != NULL, -1);
|
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
memset(ip4, 0, sizeof(IPADDR));
|
|
|
|
memset(ip6, 0, sizeof(IPADDR));
|
2001-02-09 16:26:50 -05:00
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
#ifdef HAVE_IPV6
|
2001-02-09 16:26:50 -05:00
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* save error to host_error for later use */
|
2001-02-09 16:26:50 -05:00
|
|
|
host_error = getaddrinfo(addr, NULL, &hints, &ai);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (host_error != 0)
|
|
|
|
return host_error;
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
if (getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf,
|
|
|
|
sizeof(hbuf), NULL, 0, NI_NUMERICHOST))
|
2000-04-26 04:03:38 -04:00
|
|
|
return 1;
|
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
origai = ai; count = 0;
|
|
|
|
while (ai != NULL && count < 2) {
|
|
|
|
so = (union sockaddr_union *) ai->ai_addr;
|
|
|
|
|
2002-01-29 16:26:41 -05:00
|
|
|
if (so != NULL) {
|
|
|
|
if (ai->ai_family == AF_INET6 && ip6->family == 0) {
|
|
|
|
sin_get_ip(so, ip6);
|
|
|
|
count++;
|
|
|
|
} else if (ai->ai_family == AF_INET && ip4->family == 0) {
|
|
|
|
sin_get_ip(so, ip4);
|
|
|
|
count++;
|
|
|
|
}
|
2001-03-03 20:47:13 -05:00
|
|
|
}
|
|
|
|
ai = ai->ai_next;
|
|
|
|
}
|
|
|
|
freeaddrinfo(origai);
|
2000-04-26 04:03:38 -04:00
|
|
|
#else
|
|
|
|
hp = gethostbyname(addr);
|
2000-07-02 07:56:38 -04:00
|
|
|
if (hp == NULL) return h_errno;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-03-03 20:47:13 -05:00
|
|
|
ip4->family = AF_INET;
|
2001-03-03 22:00:35 -05:00
|
|
|
memcpy(&ip4->ip, hp->h_addr, 4);
|
2000-04-26 04:03:38 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
/* Get name for host, *name should be g_free()'d unless it's NULL.
|
|
|
|
Return values are the same as with net_gethostbyname() */
|
|
|
|
int net_gethostbyaddr(IPADDR *ip, char **name)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
struct addrinfo req, *ai;
|
|
|
|
int host_error;
|
|
|
|
#else
|
|
|
|
struct hostent *hp;
|
|
|
|
#endif
|
|
|
|
char ipname[MAX_IP_LEN];
|
|
|
|
|
|
|
|
g_return_val_if_fail(ip != NULL, -1);
|
|
|
|
g_return_val_if_fail(name != NULL, -1);
|
|
|
|
|
2000-05-25 13:12:44 -04:00
|
|
|
net_ip2host(ip, ipname);
|
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
*name = NULL;
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
memset(&req, 0, sizeof(struct addrinfo));
|
|
|
|
req.ai_socktype = SOCK_STREAM;
|
2000-09-27 19:45:17 -04:00
|
|
|
req.ai_flags = AI_CANONNAME;
|
2000-05-25 07:30:47 -04:00
|
|
|
|
|
|
|
/* save error to host_error for later use */
|
2000-05-25 13:12:44 -04:00
|
|
|
host_error = getaddrinfo(ipname, NULL, &req, &ai);
|
2000-05-25 07:30:47 -04:00
|
|
|
if (host_error != 0)
|
|
|
|
return host_error;
|
2000-09-27 19:45:17 -04:00
|
|
|
*name = g_strdup(ai->ai_canonname);
|
2000-05-25 07:30:47 -04:00
|
|
|
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
#else
|
|
|
|
hp = gethostbyaddr(ipname, strlen(ipname), AF_INET);
|
|
|
|
if (hp == NULL) return -1;
|
|
|
|
|
|
|
|
*name = g_strdup(hp->h_name);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
int net_ip2host(IPADDR *ip, char *host)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
2001-03-03 22:00:35 -05:00
|
|
|
if (!inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN))
|
2000-04-26 04:03:38 -04:00
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
unsigned long ip4;
|
|
|
|
|
2001-03-03 22:00:35 -05:00
|
|
|
ip4 = ntohl(ip->ip.s_addr);
|
2000-12-19 12:48:42 -05:00
|
|
|
g_snprintf(host, MAX_IP_LEN, "%lu.%lu.%lu.%lu",
|
|
|
|
(ip4 & 0xff000000UL) >> 24,
|
|
|
|
(ip4 & 0x00ff0000) >> 16,
|
|
|
|
(ip4 & 0x0000ff00) >> 8,
|
|
|
|
(ip4 & 0x000000ff));
|
2000-04-26 04:03:38 -04:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int net_host2ip(const char *host, IPADDR *ip)
|
|
|
|
{
|
|
|
|
unsigned long addr;
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
if (strchr(host, ':') != NULL) {
|
|
|
|
/* IPv6 */
|
|
|
|
ip->family = AF_INET6;
|
2001-03-03 22:00:35 -05:00
|
|
|
if (inet_pton(AF_INET6, host, &ip->ip) == 0)
|
2000-04-26 04:03:38 -04:00
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* IPv4 */
|
|
|
|
ip->family = AF_INET;
|
|
|
|
#ifdef HAVE_INET_ATON
|
2001-03-03 22:00:35 -05:00
|
|
|
if (inet_aton(host, &ip->ip.s_addr) == 0)
|
2000-04-26 04:03:38 -04:00
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
addr = inet_addr(host);
|
|
|
|
if (addr == INADDR_NONE)
|
|
|
|
return -1;
|
|
|
|
|
2001-03-03 22:00:35 -05:00
|
|
|
memcpy(&ip->ip, &addr, 4);
|
2000-04-26 04:03:38 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get socket error */
|
2000-12-04 17:57:18 -05:00
|
|
|
int net_geterror(GIOChannel *handle)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
int data;
|
|
|
|
socklen_t len = sizeof(data);
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
if (getsockopt(g_io_channel_unix_get_fd(handle),
|
|
|
|
SOL_SOCKET, SO_ERROR, (void *) &data, &len) == -1)
|
2000-04-26 04:03:38 -04:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get error of net_gethostname() */
|
|
|
|
const char *net_gethosterror(int error)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
g_return_val_if_fail(error != 0, NULL);
|
|
|
|
|
|
|
|
if (error == 1) {
|
|
|
|
/* getnameinfo() failed ..
|
2000-07-16 16:18:05 -04:00
|
|
|
FIXME: does strerror return the right error message? */
|
2000-04-26 04:03:38 -04:00
|
|
|
return g_strerror(errno);
|
|
|
|
}
|
|
|
|
|
|
|
|
return gai_strerror(error);
|
|
|
|
#else
|
2000-07-02 07:56:38 -04:00
|
|
|
switch (error) {
|
2000-04-26 04:03:38 -04:00
|
|
|
case HOST_NOT_FOUND:
|
2001-05-17 16:13:59 -04:00
|
|
|
return "Host not found";
|
2000-04-26 04:03:38 -04:00
|
|
|
case NO_ADDRESS:
|
2001-05-17 16:13:59 -04:00
|
|
|
return "No IP address found for name";
|
2000-04-26 04:03:38 -04:00
|
|
|
case NO_RECOVERY:
|
2001-05-17 16:13:59 -04:00
|
|
|
return "A non-recovable name server error occurred";
|
2000-04-26 04:03:38 -04:00
|
|
|
case TRY_AGAIN:
|
2001-05-17 16:13:59 -04:00
|
|
|
return "A temporary error on an authoritative name server";
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* unknown error */
|
|
|
|
return NULL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2000-07-02 07:56:38 -04:00
|
|
|
/* return TRUE if host lookup failed because it didn't exist (ie. not
|
|
|
|
some error with name server) */
|
|
|
|
int net_hosterror_notfound(int error)
|
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
|
|
|
|
#else
|
|
|
|
return error == HOST_NOT_FOUND || error == NO_ADDRESS;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2001-01-17 21:23:35 -05:00
|
|
|
/* Get name of TCP service */
|
|
|
|
char *net_getservbyport(int port)
|
|
|
|
{
|
|
|
|
struct servent *entry;
|
|
|
|
|
|
|
|
entry = getservbyport(htons((unsigned short) port), "tcp");
|
|
|
|
return entry == NULL ? NULL : entry->s_name;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
int is_ipv4_address(const char *host)
|
|
|
|
{
|
|
|
|
while (*host != '\0') {
|
2002-01-27 15:45:59 -05:00
|
|
|
if (*host != '.' && !i_isdigit(*host))
|
2000-04-26 04:03:38 -04:00
|
|
|
return 0;
|
|
|
|
host++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_ipv6_address(const char *host)
|
|
|
|
{
|
|
|
|
while (*host != '\0') {
|
|
|
|
if (*host != ':' && !isxdigit(*host))
|
|
|
|
return 0;
|
|
|
|
host++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|