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.
|
|
|
|
|
2007-05-08 14:41:10 -04:00
|
|
|
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.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2000-04-26 04:03:38 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "module.h"
|
|
|
|
#include "network.h"
|
|
|
|
|
2002-05-19 10:43:16 -04:00
|
|
|
#include <sys/un.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;
|
|
|
|
struct sockaddr_in6 sin6;
|
|
|
|
};
|
|
|
|
|
2016-01-27 08:31:55 -05:00
|
|
|
#define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? \
|
2000-11-11 15:27:37 -05:00
|
|
|
sizeof(so.sin6) : sizeof(so.sin))
|
|
|
|
|
2010-04-03 16:04:15 -04:00
|
|
|
GIOChannel *g_io_channel_new(int handle)
|
|
|
|
{
|
|
|
|
GIOChannel *chan;
|
|
|
|
chan = g_io_channel_unix_new(handle);
|
|
|
|
g_io_channel_set_encoding(chan, NULL, NULL);
|
|
|
|
g_io_channel_set_buffered(chan, FALSE);
|
|
|
|
return chan;
|
|
|
|
}
|
2000-12-04 17:57:18 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* Cygwin need this, don't know others.. */
|
|
|
|
/*#define BLOCKING_SOCKETS 1*/
|
|
|
|
|
2003-07-09 19:34:41 -04:00
|
|
|
IPADDR ip4_any = {
|
|
|
|
AF_INET,
|
2016-01-27 08:31:55 -05:00
|
|
|
#if defined(IN6ADDR_ANY_INIT)
|
2014-07-06 14:56:13 -04:00
|
|
|
IN6ADDR_ANY_INIT
|
|
|
|
#else
|
2014-07-06 13:56:17 -04:00
|
|
|
{ INADDR_ANY }
|
2014-07-06 14:56:13 -04:00
|
|
|
#endif
|
2003-07-09 19:34:41 -04:00
|
|
|
};
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
int net_ip_compare(IPADDR *ip1, IPADDR *ip2)
|
|
|
|
{
|
|
|
|
if (ip1->family != ip2->family)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
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
|
|
|
|
2001-03-03 22:00:35 -05:00
|
|
|
return memcmp(&ip1->ip, &ip2->ip, 4) == 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-15 04:46:35 -04:00
|
|
|
static 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) {
|
|
|
|
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
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
so->sin.sin_family = ip->family;
|
2016-01-27 08:31:55 -05:00
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
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
|
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;
|
|
|
|
|
|
|
|
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
|
2001-03-03 22:00:35 -05:00
|
|
|
memcpy(&ip->ip, &so->sin.sin_addr, 4);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2005-10-15 04:46:35 -04:00
|
|
|
static void sin_set_port(union sockaddr_union *so, int port)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
if (so->sin.sin_family == AF_INET6)
|
2003-07-09 19:34:41 -04:00
|
|
|
so->sin6.sin6_port = htons((unsigned short)port);
|
2000-04-26 04:03:38 -04:00
|
|
|
else
|
2000-10-26 14:57:23 -04:00
|
|
|
so->sin.sin_port = htons((unsigned short)port);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2005-10-15 04:46:35 -04:00
|
|
|
static int sin_get_port(union sockaddr_union *so)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2016-01-27 08:31:55 -05:00
|
|
|
return ntohs((so->sin.sin_family == AF_INET6) ?
|
|
|
|
so->sin6.sin6_port :
|
|
|
|
so->sin.sin_port);
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to socket */
|
2002-08-26 15:05:14 -04:00
|
|
|
GIOChannel *net_connect(const char *addr, int port, IPADDR *my_ip)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2015-09-22 15:59:17 -04:00
|
|
|
IPADDR ip4, ip6, *ip;
|
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
|
|
|
|
2015-09-22 15:59:17 -04: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
|
|
|
|
2015-09-22 15:59:17 -04: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 */
|
2002-08-26 15:05:14 -04: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);
|
|
|
|
|
2002-08-26 15:05:14 -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 */
|
|
|
|
fcntl(handle, F_SETFL, O_NONBLOCK);
|
2007-11-20 08:34:45 -05:00
|
|
|
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
|
|
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &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);
|
2002-11-29 08:38:21 -05:00
|
|
|
if (bind(handle, &so.sa, SIZEOF_SOCKADDR(so)) < 0) {
|
|
|
|
int old_errno = errno;
|
|
|
|
|
|
|
|
close(handle);
|
|
|
|
errno = old_errno;
|
|
|
|
return NULL;
|
2001-11-07 09:35:00 -05:00
|
|
|
}
|
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
|
|
|
|
2002-01-04 16:28:26 -05:00
|
|
|
if (ret < 0 && errno != EINPROGRESS)
|
|
|
|
{
|
2002-08-26 15:05:14 -04:00
|
|
|
int old_errno = errno;
|
2002-05-19 10:43:16 -04:00
|
|
|
close(handle);
|
2002-08-26 15:05:14 -04:00
|
|
|
errno = old_errno;
|
2002-05-19 10:43:16 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_io_channel_new(handle);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to named UNIX socket */
|
2002-08-26 15:05:14 -04:00
|
|
|
GIOChannel *net_connect_unix(const char *path)
|
2002-05-19 10:43:16 -04:00
|
|
|
{
|
|
|
|
struct sockaddr_un sa;
|
|
|
|
int handle, ret;
|
|
|
|
|
|
|
|
/* create the socket */
|
|
|
|
handle = socket(PF_UNIX, SOCK_STREAM, 0);
|
2002-08-26 15:05:14 -04:00
|
|
|
if (handle == -1)
|
2002-05-19 10:43:16 -04:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* set socket options */
|
|
|
|
fcntl(handle, F_SETFL, O_NONBLOCK);
|
|
|
|
|
|
|
|
/* connect */
|
|
|
|
memset(&sa, 0, sizeof(sa));
|
|
|
|
sa.sun_family = AF_UNIX;
|
|
|
|
strncpy(sa.sun_path, path, sizeof(sa.sun_path)-1);
|
|
|
|
sa.sun_path[sizeof(sa.sun_path)-1] = '\0';
|
|
|
|
|
|
|
|
ret = connect(handle, (struct sockaddr *) &sa, sizeof(sa));
|
|
|
|
if (ret < 0 && errno != EINPROGRESS) {
|
2002-08-26 15:05:14 -04:00
|
|
|
int old_errno = errno;
|
2000-04-26 04:03:38 -04:00
|
|
|
close(handle);
|
2002-08-26 15:05:14 -04:00
|
|
|
errno = old_errno;
|
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
|
|
|
|
2014-06-15 15:23:29 -04:00
|
|
|
g_io_channel_shutdown(handle, TRUE, NULL);
|
2000-12-04 17:57:18 -05:00
|
|
|
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_ip(&so, my_ip);
|
2003-07-09 19:34:41 -04:00
|
|
|
sin_set_port(&so, *port);
|
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);
|
2016-01-27 08:31:55 -05:00
|
|
|
|
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);
|
|
|
|
}
|
2016-01-27 08:31:55 -05:00
|
|
|
|
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 */
|
|
|
|
fcntl(handle, F_SETFL, O_NONBLOCK);
|
2007-11-20 08:34:45 -05:00
|
|
|
setsockopt(handle, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));
|
|
|
|
setsockopt(handle, SOL_SOCKET, SO_KEEPALIVE, &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
|
|
|
|
|
|
|
fcntl(ret, F_SETFL, O_NONBLOCK);
|
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
|
|
|
{
|
2003-10-10 21:53:51 -04:00
|
|
|
gsize ret;
|
2010-04-03 16:04:15 -04:00
|
|
|
GIOStatus status;
|
2010-05-16 13:50:31 -04:00
|
|
|
GError *err = NULL;
|
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);
|
|
|
|
|
2010-05-16 13:50:31 -04:00
|
|
|
status = g_io_channel_read_chars(handle, buf, len, &ret, &err);
|
|
|
|
if (err != NULL) {
|
2014-01-10 15:29:42 -05:00
|
|
|
g_warning("%s", err->message);
|
2010-05-16 13:50:31 -04:00
|
|
|
g_error_free(err);
|
|
|
|
}
|
2010-04-03 16:04:15 -04:00
|
|
|
if (status == G_IO_STATUS_ERROR || status == G_IO_STATUS_EOF)
|
2000-04-26 04:03:38 -04:00
|
|
|
return -1; /* disconnected */
|
|
|
|
|
2010-04-03 16:04:15 -04:00
|
|
|
return ret;
|
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
|
|
|
{
|
2003-10-10 21:53:51 -04:00
|
|
|
gsize ret;
|
2010-04-03 16:04:15 -04:00
|
|
|
GIOStatus status;
|
2010-05-16 13:50:31 -04:00
|
|
|
GError *err = NULL;
|
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);
|
|
|
|
|
2010-05-16 13:50:31 -04:00
|
|
|
status = g_io_channel_write_chars(handle, (char *) data, len, &ret, &err);
|
|
|
|
if (err != NULL) {
|
2014-01-10 15:29:42 -05:00
|
|
|
g_warning("%s", err->message);
|
2010-05-16 13:50:31 -04:00
|
|
|
g_error_free(err);
|
|
|
|
}
|
2010-04-03 16:04:15 -04:00
|
|
|
if (status == G_IO_STATUS_ERROR)
|
|
|
|
return -1;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2010-04-03 16:04:15 -04:00
|
|
|
return ret;
|
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() */
|
2015-09-22 15:59:17 -04:00
|
|
|
int net_gethostbyname(const char *addr, IPADDR *ip4, IPADDR *ip6)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
union sockaddr_union *so;
|
2015-09-22 15:59:17 -04:00
|
|
|
struct addrinfo hints, *ai, *ailist;
|
|
|
|
int ret, count_v4, count_v6, use_v4, use_v6;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(addr != NULL, -1);
|
|
|
|
|
2015-09-22 15:59:17 -04:00
|
|
|
memset(ip4, 0, sizeof(IPADDR));
|
|
|
|
memset(ip6, 0, sizeof(IPADDR));
|
2001-02-09 16:26:50 -05:00
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(struct addrinfo));
|
|
|
|
hints.ai_socktype = SOCK_STREAM;
|
2016-01-27 10:16:27 -05:00
|
|
|
hints.ai_flags = AI_ADDRCONFIG;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* save error to host_error for later use */
|
2002-01-29 17:16:40 -05:00
|
|
|
ret = getaddrinfo(addr, NULL, &hints, &ailist);
|
|
|
|
if (ret != 0)
|
|
|
|
return ret;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2015-09-22 15:59:17 -04:00
|
|
|
/* count IPs */
|
|
|
|
count_v4 = count_v6 = 0;
|
|
|
|
for (ai = ailist; ai != NULL; ai = ai->ai_next) {
|
|
|
|
if (ai->ai_family == AF_INET)
|
|
|
|
count_v4++;
|
|
|
|
else if (ai->ai_family == AF_INET6)
|
|
|
|
count_v6++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (count_v4 == 0 && count_v6 == 0)
|
|
|
|
return HOST_NOT_FOUND; /* shouldn't happen? */
|
|
|
|
|
|
|
|
/* if there are multiple addresses, return random one */
|
|
|
|
use_v4 = count_v4 <= 1 ? 0 : rand() % count_v4;
|
|
|
|
use_v6 = count_v6 <= 1 ? 0 : rand() % count_v6;
|
|
|
|
|
|
|
|
count_v4 = count_v6 = 0;
|
|
|
|
for (ai = ailist; ai != NULL; ai = ai->ai_next) {
|
|
|
|
so = (union sockaddr_union *) ai->ai_addr;
|
|
|
|
|
|
|
|
if (ai->ai_family == AF_INET) {
|
|
|
|
if (use_v4 == count_v4)
|
|
|
|
sin_get_ip(so, ip4);
|
|
|
|
count_v4++;
|
|
|
|
} else if (ai->ai_family == AF_INET6) {
|
|
|
|
if (use_v6 == count_v6)
|
|
|
|
sin_get_ip(so, ip6);
|
|
|
|
count_v6++;
|
|
|
|
}
|
|
|
|
}
|
2002-01-29 17:16:40 -05:00
|
|
|
freeaddrinfo(ailist);
|
2015-09-22 15:59:17 -04:00
|
|
|
return 0;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2002-11-28 18:43:42 -05:00
|
|
|
union sockaddr_union so;
|
2000-05-25 07:30:47 -04:00
|
|
|
int host_error;
|
2002-11-28 18:43:42 -05:00
|
|
|
char hostname[NI_MAXHOST];
|
2000-05-25 07:30:47 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(ip != NULL, -1);
|
|
|
|
g_return_val_if_fail(name != NULL, -1);
|
|
|
|
|
|
|
|
*name = NULL;
|
2016-01-27 08:31:55 -05:00
|
|
|
|
2002-11-28 18:43:42 -05:00
|
|
|
memset(&so, 0, sizeof(so));
|
|
|
|
sin_set_ip(&so, ip);
|
2000-05-25 07:30:47 -04:00
|
|
|
|
|
|
|
/* save error to host_error for later use */
|
2016-01-27 09:54:29 -05:00
|
|
|
host_error = getnameinfo((struct sockaddr *)&so, sizeof(so),
|
|
|
|
hostname, sizeof(hostname),
|
|
|
|
NULL, 0,
|
|
|
|
NI_NAMEREQD);
|
2002-11-28 18:43:42 -05:00
|
|
|
if (host_error != 0)
|
|
|
|
return host_error;
|
2000-05-25 07:30:47 -04:00
|
|
|
|
2002-11-28 18:43:42 -05:00
|
|
|
*name = g_strdup(hostname);
|
2000-05-25 07:30:47 -04:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
int net_ip2host(IPADDR *ip, char *host)
|
|
|
|
{
|
2016-01-27 09:04:07 -05:00
|
|
|
return inet_ntop(ip->family, &ip->ip, host, MAX_IP_LEN) ? 0 : -1;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
int net_host2ip(const char *host, IPADDR *ip)
|
|
|
|
{
|
|
|
|
unsigned long addr;
|
|
|
|
|
|
|
|
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;
|
2002-05-26 12:52:36 -04:00
|
|
|
} else {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail(error != 0, NULL);
|
|
|
|
|
|
|
|
return gai_strerror(error);
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
2014-11-08 17:26:53 -05:00
|
|
|
#ifdef EAI_NODATA /* NODATA is deprecated */
|
2000-07-02 07:56:38 -04:00
|
|
|
return error != 1 && (error == EAI_NONAME || error == EAI_NODATA);
|
2004-01-28 09:27:24 -05:00
|
|
|
#else
|
|
|
|
return error != 1 && (error == EAI_NONAME);
|
|
|
|
#endif
|
2000-07-02 07:56:38 -04:00
|
|
|
}
|
|
|
|
|
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') {
|
2002-02-17 14:41:34 -05:00
|
|
|
if (*host != ':' && !i_isxdigit(*host))
|
2000-04-26 04:03:38 -04:00
|
|
|
return 0;
|
|
|
|
host++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|