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
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
static int g_io_channel_write_block(GIOChannel *channel, void *data, int len)
|
|
|
|
{
|
2003-10-10 21:59:31 -04:00
|
|
|
gsize ret;
|
2010-04-03 16:04:15 -04:00
|
|
|
int sent;
|
|
|
|
GIOStatus status;
|
2000-12-04 17:57:18 -05:00
|
|
|
|
|
|
|
sent = 0;
|
|
|
|
do {
|
2010-04-03 16:04:15 -04:00
|
|
|
status = g_io_channel_write_chars(channel, (char *) data + sent,
|
|
|
|
len-sent, &ret, NULL);
|
2000-12-04 17:57:18 -05:00
|
|
|
sent += ret;
|
2010-04-03 16:04:15 -04:00
|
|
|
} while (sent < len && status != G_IO_STATUS_ERROR);
|
2000-12-04 17:57:18 -05:00
|
|
|
|
2010-04-03 16:04:15 -04:00
|
|
|
return sent < len ? -1 : 0;
|
2000-12-04 17:57:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static int g_io_channel_read_block(GIOChannel *channel, void *data, int len)
|
|
|
|
{
|
|
|
|
time_t maxwait;
|
2003-10-10 21:59:31 -04:00
|
|
|
gsize ret;
|
2010-04-03 16:04:15 -04:00
|
|
|
int received;
|
|
|
|
GIOStatus status;
|
2000-12-04 17:57:18 -05:00
|
|
|
|
|
|
|
maxwait = time(NULL)+2;
|
|
|
|
received = 0;
|
|
|
|
do {
|
2010-04-03 16:04:15 -04:00
|
|
|
status = g_io_channel_read_chars(channel, (char *) data + received,
|
|
|
|
len-received, &ret, NULL);
|
2000-12-04 17:57:18 -05:00
|
|
|
received += ret;
|
|
|
|
} while (received < len && time(NULL) < maxwait &&
|
2010-04-03 16:04:15 -04:00
|
|
|
status != G_IO_STATUS_ERROR && status != G_IO_STATUS_EOF);
|
2000-12-04 17:57:18 -05:00
|
|
|
|
|
|
|
return received < len ? -1 : 0;
|
|
|
|
}
|
|
|
|
|
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;
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-04-26 04:03:38 -04:00
|
|
|
int pid;
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
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);
|
|
|
|
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-04-26 04:03:38 -04:00
|
|
|
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
|
|
|
}
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
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-21 07:54:13 -04:00
|
|
|
rec.error = net_gethostbyname(addr, &rec.ip);
|
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-21 07:54:13 -04:00
|
|
|
net_gethostbyaddr(&rec.ip, &rec.host);
|
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-21 07:54:13 -04:00
|
|
|
if (rec.host) {
|
|
|
|
len = strlen(rec.host) + 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-21 07:54:13 -04:00
|
|
|
g_io_channel_write_block(pipe, (void *) rec.host,
|
2002-11-28 10:39:39 -05:00
|
|
|
len);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
if (pid == 0)
|
|
|
|
_exit(99);
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
2000-04-26 04:03:38 -04:00
|
|
|
|
|
|
|
/* 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-21 07:54:13 -04:00
|
|
|
rec->host = NULL;
|
2000-04-26 04:03:38 -04:00
|
|
|
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-12-04 17:57:18 -05:00
|
|
|
fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
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-21 07:54:13 -04:00
|
|
|
if (rec->host) {
|
2002-11-28 10:39:39 -05:00
|
|
|
g_io_channel_read_block(pipe, &len, sizeof(int));
|
2015-09-21 07:54:13 -04:00
|
|
|
rec->host = g_malloc0(len);
|
|
|
|
g_io_channel_read_block(pipe, rec->host, 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);
|
|
|
|
|
2000-10-26 14:57:23 -04:00
|
|
|
#ifndef WIN32
|
2000-04-26 04:03:38 -04:00
|
|
|
kill(pid, SIGKILL);
|
2000-10-26 14:57:23 -04:00
|
|
|
#endif
|
2000-04-26 04:03:38 -04:00
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
|
2000-12-04 17:57:18 -05:00
|
|
|
handle = iprec.error == -1 ? NULL :
|
2015-09-21 07:54:13 -04:00
|
|
|
net_connect_ip(&iprec.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
|
|
|
}
|