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"
|
|
|
|
#include "net-internal.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)
|
|
|
|
return memcmp(&ip1->addr, &ip2->addr, sizeof(ip1->addr)) == 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return memcmp(&ip1->addr, &ip2->addr, 4) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
memcpy(&so->sin6.sin6_addr, &ip->addr, sizeof(ip->addr.ip6));
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
memcpy(&so->sin.sin_addr, &ip->addr, 4);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
memcpy(&ip->addr, &so->sin6.sin6_addr, sizeof(ip->addr.ip6));
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
memcpy(&ip->addr, &so->sin.sin_addr, 4);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
IPADDR ip;
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_return_val_if_fail(addr != NULL, NULL);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
if (net_gethostbyname(addr, &ip) == -1)
|
2000-12-04 17:57:18 -05:00
|
|
|
return NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
return net_connect_ip(&ip, port, my_ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* set our own address, ignore if bind() fails */
|
|
|
|
if (my_ip != NULL) {
|
|
|
|
sin_set_ip(&so, my_ip);
|
2000-11-11 15:27:37 -05:00
|
|
|
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
|
2000-04-26 04:03:38 -04:00
|
|
|
if (ret < 0 && errno != EINPROGRESS) {
|
2001-01-11 05:15:34 -05:00
|
|
|
#else
|
|
|
|
if (ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK) {
|
|
|
|
#endif
|
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);
|
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
|
|
|
{
|
2000-12-04 17:57:18 -05:00
|
|
|
int ret, 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 */
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
return err == 0 ? 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
|
|
|
{
|
2000-12-04 17:57:18 -05:00
|
|
|
int ret, 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;
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
return err == 0 ? 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get IP address for host, returns 0 = ok,
|
|
|
|
others = error code for net_gethosterror() */
|
2000-05-25 07:30:47 -04:00
|
|
|
int net_gethostbyname(const char *addr, IPADDR *ip)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
union sockaddr_union *so;
|
|
|
|
struct addrinfo req, *ai;
|
|
|
|
char hbuf[NI_MAXHOST];
|
|
|
|
int host_error;
|
|
|
|
#else
|
|
|
|
struct hostent *hp;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
g_return_val_if_fail(addr != NULL, -1);
|
|
|
|
|
|
|
|
#ifdef HAVE_IPV6
|
|
|
|
memset(ip, 0, sizeof(IPADDR));
|
|
|
|
memset(&req, 0, sizeof(struct addrinfo));
|
|
|
|
req.ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
/* save error to host_error for later use */
|
|
|
|
host_error = getaddrinfo(addr, NULL, &req, &ai);
|
|
|
|
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;
|
|
|
|
|
|
|
|
so = (union sockaddr_union *) ai->ai_addr;
|
|
|
|
sin_get_ip(so, ip);
|
|
|
|
freeaddrinfo(ai);
|
|
|
|
#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
|
|
|
|
|
|
|
ip->family = AF_INET;
|
|
|
|
memcpy(&ip->addr, hp->h_addr, 4);
|
|
|
|
#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
|
|
|
|
if (!inet_ntop(ip->family, &ip->addr, host, MAX_IP_LEN))
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
unsigned long ip4;
|
|
|
|
|
|
|
|
ip4 = ntohl(ip->addr.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;
|
|
|
|
if (inet_pton(AF_INET6, host, &ip->addr) == 0)
|
|
|
|
return -1;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* IPv4 */
|
|
|
|
ip->family = AF_INET;
|
|
|
|
#ifdef HAVE_INET_ATON
|
|
|
|
if (inet_aton(host, &ip->addr.ip.s_addr) == 0)
|
|
|
|
return -1;
|
|
|
|
#else
|
|
|
|
addr = inet_addr(host);
|
|
|
|
if (addr == INADDR_NONE)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memcpy(&ip->addr, &addr, 4);
|
|
|
|
#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:
|
|
|
|
return _("Host not found");
|
|
|
|
case NO_ADDRESS:
|
|
|
|
return _("No IP address found for name");
|
|
|
|
case NO_RECOVERY:
|
|
|
|
return _("A non-recovable name server error occurred");
|
|
|
|
case TRY_AGAIN:
|
|
|
|
return _("A temporary error on an authoritative name server");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
int is_ipv4_address(const char *host)
|
|
|
|
{
|
|
|
|
while (*host != '\0') {
|
|
|
|
if (*host != '.' && !isdigit(*host))
|
|
|
|
return 0;
|
|
|
|
host++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int is_ipv6_address(const char *host)
|
|
|
|
{
|
|
|
|
while (*host != '\0') {
|
|
|
|
if (*host != ':' && !isxdigit(*host))
|
|
|
|
return 0;
|
|
|
|
host++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|