2000-04-26 04:03:38 -04:00
|
|
|
/*
|
|
|
|
net-nonblock.c : Nonblocking net_connect()
|
|
|
|
|
|
|
|
Copyright (C) 1998-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 <signal.h>
|
|
|
|
|
|
|
|
#include "pidwait.h"
|
|
|
|
#include "net-nonblock.h"
|
|
|
|
|
2000-07-16 16:18:05 -04:00
|
|
|
typedef struct {
|
2000-04-26 04:03:38 -04:00
|
|
|
NET_CALLBACK func;
|
|
|
|
void *data;
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
GIOChannel *pipes[2];
|
2000-04-26 04:03:38 -04:00
|
|
|
int port;
|
|
|
|
IPADDR *my_ip;
|
|
|
|
int tag;
|
2000-07-16 16:18:05 -04:00
|
|
|
} SIMPLE_THREAD_REC;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* nonblocking gethostbyname(), ip (IPADDR) + error (int, 0 = not error) is
|
|
|
|
written to pipe when found PID of the resolver child is returned */
|
2002-11-28 10:39:39 -05:00
|
|
|
int net_gethostbyname_nonblock(const char *addr, GIOChannel *pipe,
|
|
|
|
int reverse_lookup)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
RESOLVED_IP_REC rec;
|
|
|
|
const char *errorstr;
|
|
|
|
int pid;
|
2002-11-28 10:39:39 -05:00
|
|
|
int len;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_val_if_fail(addr != NULL, FALSE);
|
|
|
|
|
|
|
|
pid = fork();
|
|
|
|
if (pid > 0) {
|
|
|
|
/* parent */
|
|
|
|
pidwait_add(pid);
|
|
|
|
return pid;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pid != 0) {
|
|
|
|
/* failed! */
|
2000-07-16 16:18:05 -04:00
|
|
|
g_warning("net_connect_thread(): fork() failed! "
|
|
|
|
"Using blocking resolving");
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* child */
|
2006-08-01 16:31:38 -04:00
|
|
|
srand(time(NULL));
|
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
memset(&rec, 0, sizeof(rec));
|
2015-09-22 15:59:17 -04:00
|
|
|
rec.error = net_gethostbyname(addr, &rec.ip4, &rec.ip6);
|
2000-04-26 04:03:38 -04:00
|
|
|
if (rec.error == 0) {
|
|
|
|
errorstr = NULL;
|
2002-11-28 10:39:39 -05:00
|
|
|
if (reverse_lookup) {
|
|
|
|
/* reverse lookup the IP, ignore any error */
|
2015-09-22 15:59:17 -04:00
|
|
|
if (rec.ip4.family != 0)
|
|
|
|
net_gethostbyaddr(&rec.ip4, &rec.host4);
|
|
|
|
if (rec.ip6.family != 0)
|
|
|
|
net_gethostbyaddr(&rec.ip6, &rec.host6);
|
2002-11-28 10:39:39 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
} else {
|
|
|
|
errorstr = net_gethosterror(rec.error);
|
2000-12-05 14:43:12 -05:00
|
|
|
rec.errlen = errorstr == NULL ? 0 : strlen(errorstr)+1;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
g_io_channel_write_block(pipe, &rec, sizeof(rec));
|
2000-12-05 14:43:12 -05:00
|
|
|
if (rec.errlen != 0)
|
2000-12-04 17:57:18 -05:00
|
|
|
g_io_channel_write_block(pipe, (void *) errorstr, rec.errlen);
|
2002-11-28 10:39:39 -05:00
|
|
|
else {
|
2015-09-22 15:59:17 -04:00
|
|
|
if (rec.host4) {
|
|
|
|
len = strlen(rec.host4) + 1;
|
2008-02-04 10:30:38 -05:00
|
|
|
g_io_channel_write_block(pipe, (void *) &len,
|
2002-11-28 10:39:39 -05:00
|
|
|
sizeof(int));
|
2015-09-22 15:59:17 -04:00
|
|
|
g_io_channel_write_block(pipe, (void *) rec.host4,
|
|
|
|
len);
|
|
|
|
}
|
|
|
|
if (rec.host6) {
|
|
|
|
len = strlen(rec.host6) + 1;
|
|
|
|
g_io_channel_write_block(pipe, (void *) &len,
|
|
|
|
sizeof(int));
|
|
|
|
g_io_channel_write_block(pipe, (void *) rec.host6,
|
2002-11-28 10:39:39 -05:00
|
|
|
len);
|
|
|
|
}
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (pid == 0)
|
|
|
|
_exit(99);
|
|
|
|
|
|
|
|
/* we used blocking lookup */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get the resolved IP address */
|
2000-12-04 17:57:18 -05:00
|
|
|
int net_gethostbyname_return(GIOChannel *pipe, RESOLVED_IP_REC *rec)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
2002-11-28 10:39:39 -05:00
|
|
|
int len;
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
rec->error = -1;
|
|
|
|
rec->errorstr = NULL;
|
2015-09-22 15:59:17 -04:00
|
|
|
rec->host4 = NULL;
|
|
|
|
rec->host6 = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2001-02-18 21:13:02 -05:00
|
|
|
/* get ip+error */
|
|
|
|
if (g_io_channel_read_block(pipe, rec, sizeof(*rec)) == -1) {
|
|
|
|
rec->errorstr = g_strdup_printf("Host name lookup: %s",
|
|
|
|
g_strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
if (rec->error) {
|
2000-12-04 17:57:18 -05:00
|
|
|
/* read error string, if we can't read everything for some
|
|
|
|
reason, just ignore it. */
|
|
|
|
rec->errorstr = g_malloc0(rec->errlen+1);
|
|
|
|
g_io_channel_read_block(pipe, rec->errorstr, rec->errlen);
|
2002-11-28 10:39:39 -05:00
|
|
|
} else {
|
2015-09-22 15:59:17 -04:00
|
|
|
if (rec->host4) {
|
|
|
|
g_io_channel_read_block(pipe, &len, sizeof(int));
|
|
|
|
rec->host4 = g_malloc0(len);
|
|
|
|
g_io_channel_read_block(pipe, rec->host4, len);
|
|
|
|
}
|
|
|
|
if (rec->host6) {
|
2002-11-28 10:39:39 -05:00
|
|
|
g_io_channel_read_block(pipe, &len, sizeof(int));
|
2015-09-22 15:59:17 -04:00
|
|
|
rec->host6 = g_malloc0(len);
|
|
|
|
g_io_channel_read_block(pipe, rec->host6, len);
|
2002-11-28 10:39:39 -05:00
|
|
|
}
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2000-05-25 07:30:47 -04:00
|
|
|
/* Get host name, call func when finished */
|
|
|
|
int net_gethostbyaddr_nonblock(IPADDR *ip, NET_HOST_CALLBACK func, void *data)
|
|
|
|
{
|
2000-12-04 17:57:18 -05:00
|
|
|
/* FIXME: not implemented */
|
2000-05-25 07:30:47 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2000-04-26 04:03:38 -04:00
|
|
|
/* Kill the resolver child */
|
|
|
|
void net_disconnect_nonblock(int pid)
|
|
|
|
{
|
|
|
|
g_return_if_fail(pid > 0);
|
|
|
|
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
}
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
static void simple_init(SIMPLE_THREAD_REC *rec, GIOChannel *handle)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
|
|
|
g_source_remove(rec->tag);
|
|
|
|
|
|
|
|
if (net_geterror(handle) != 0) {
|
|
|
|
/* failed */
|
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);
|
|
|
|
handle = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rec->func(handle, rec->data);
|
|
|
|
g_free(rec);
|
|
|
|
}
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
static void simple_readpipe(SIMPLE_THREAD_REC *rec, GIOChannel *pipe)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
RESOLVED_IP_REC iprec;
|
2000-12-04 17:57:18 -05:00
|
|
|
GIOChannel *handle;
|
2015-09-22 15:59:17 -04:00
|
|
|
IPADDR *ip;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_return_if_fail(rec != NULL);
|
|
|
|
|
|
|
|
g_source_remove(rec->tag);
|
|
|
|
|
|
|
|
net_gethostbyname_return(pipe, &iprec);
|
|
|
|
g_free_not_null(iprec.errorstr);
|
|
|
|
|
2014-06-15 15:23:29 -04:00
|
|
|
g_io_channel_shutdown(rec->pipes[0], TRUE, NULL);
|
2000-12-04 17:57:18 -05:00
|
|
|
g_io_channel_unref(rec->pipes[0]);
|
2014-06-15 15:23:29 -04:00
|
|
|
g_io_channel_shutdown(rec->pipes[1], TRUE, NULL);
|
2000-12-04 17:57:18 -05:00
|
|
|
g_io_channel_unref(rec->pipes[1]);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2015-09-22 15:59:17 -04:00
|
|
|
ip = iprec.ip4.family != 0 ? &iprec.ip4 : &iprec.ip6;
|
2000-12-04 17:57:18 -05:00
|
|
|
handle = iprec.error == -1 ? NULL :
|
2015-09-22 15:59:17 -04:00
|
|
|
net_connect_ip(ip, rec->port, rec->my_ip);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
g_free_not_null(rec->my_ip);
|
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
if (handle == NULL) {
|
2000-04-26 04:03:38 -04:00
|
|
|
/* failed */
|
2000-12-04 17:57:18 -05:00
|
|
|
rec->func(NULL, rec->data);
|
2000-04-26 04:03:38 -04:00
|
|
|
g_free(rec);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2000-11-23 18:29:32 -05:00
|
|
|
rec->tag = g_input_add(handle, G_INPUT_READ | G_INPUT_WRITE,
|
2000-04-26 04:03:38 -04:00
|
|
|
(GInputFunction) simple_init, rec);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Connect to server, call func when finished */
|
2000-07-16 16:18:05 -04:00
|
|
|
int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
|
|
|
|
NET_CALLBACK func, void *data)
|
2000-04-26 04:03:38 -04:00
|
|
|
{
|
|
|
|
SIMPLE_THREAD_REC *rec;
|
|
|
|
int fd[2];
|
|
|
|
|
|
|
|
g_return_val_if_fail(server != NULL, FALSE);
|
|
|
|
g_return_val_if_fail(func != NULL, FALSE);
|
|
|
|
|
|
|
|
if (pipe(fd) != 0) {
|
|
|
|
g_warning("net_connect_nonblock(): pipe() failed.");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rec = g_new0(SIMPLE_THREAD_REC, 1);
|
|
|
|
rec->port = port;
|
|
|
|
if (my_ip != NULL) {
|
|
|
|
rec->my_ip = g_malloc(sizeof(IPADDR));
|
|
|
|
memcpy(rec->my_ip, my_ip, sizeof(IPADDR));
|
|
|
|
}
|
|
|
|
rec->func = func;
|
|
|
|
rec->data = data;
|
2010-04-03 16:04:15 -04:00
|
|
|
rec->pipes[0] = g_io_channel_new(fd[0]);
|
|
|
|
rec->pipes[1] = g_io_channel_new(fd[1]);
|
2000-12-04 17:57:18 -05:00
|
|
|
|
|
|
|
/* start nonblocking host name lookup */
|
2002-11-28 10:39:39 -05:00
|
|
|
net_gethostbyname_nonblock(server, rec->pipes[1], 0);
|
2000-12-04 17:57:18 -05:00
|
|
|
rec->tag = g_input_add(rec->pipes[0], G_INPUT_READ,
|
2000-07-16 16:18:05 -04:00
|
|
|
(GInputFunction) simple_readpipe, rec);
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
return TRUE;
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|