mirror of
https://github.com/irssi/irssi.git
synced 2026-08-16 07:02:12 +02:00
use GIO resolver
change: rename resolve_prefer_ipv6 -> irssiproxy_prefer_ipv6
This commit is contained in:
parent
d5be7d7756
commit
51f2a4f7fb
18 changed files with 313 additions and 339 deletions
|
|
@ -22,86 +22,54 @@
|
|||
|
||||
#include <signal.h>
|
||||
|
||||
#include <irssi/src/core/pidwait.h>
|
||||
#include <irssi/src/core/network.h>
|
||||
#include <irssi/src/core/net-nonblock.h>
|
||||
|
||||
/* nonblocking gethostbyname(), ip (IPADDR) + error (int, 0 = not error) is
|
||||
written to pipe when found PID of the resolver child is returned */
|
||||
int net_gethostbyname_nonblock(const char *addr, GIOChannel *pipe, int reverse_lookup)
|
||||
{
|
||||
RESOLVED_IP_REC rec;
|
||||
const char *errorstr;
|
||||
int pid;
|
||||
typedef struct {
|
||||
NetGethostbynameContinuationFunc cont;
|
||||
void *cont_data;
|
||||
} NET_GETHOSTBYNAME_CALLBACK_DATA;
|
||||
|
||||
(void) reverse_lookup; /* Kept for API backward compatibility */
|
||||
static void net_gethostbyname_callback(GResolver *resolver, GAsyncResult *result,
|
||||
NET_GETHOSTBYNAME_CALLBACK_DATA *data)
|
||||
{
|
||||
/* GList<GInetAddress> */
|
||||
GList *ailist;
|
||||
GError *error;
|
||||
RESOLVED_IP_REC *iprec;
|
||||
|
||||
error = NULL;
|
||||
ailist = g_resolver_lookup_by_name_with_flags_finish(resolver, result, &error);
|
||||
iprec = g_new0(RESOLVED_IP_REC, 1);
|
||||
if (error != NULL) {
|
||||
iprec->error = error;
|
||||
} else {
|
||||
iprec->ailist = ailist;
|
||||
}
|
||||
g_object_unref(resolver);
|
||||
resolved_ip_ref(iprec);
|
||||
|
||||
data->cont(iprec, data->cont_data);
|
||||
g_free(data);
|
||||
}
|
||||
|
||||
/* nonblocking gethostbyname() */
|
||||
GCancellable *net_gethostbyname_nonblock(const char *addr, GResolverNameLookupFlags flags,
|
||||
NetGethostbynameContinuationFunc cont, void *cont_data)
|
||||
{
|
||||
GResolver *resolver;
|
||||
GCancellable *cancellable;
|
||||
NET_GETHOSTBYNAME_CALLBACK_DATA *data;
|
||||
|
||||
g_return_val_if_fail(addr != NULL, FALSE);
|
||||
|
||||
pid = fork();
|
||||
if (pid > 0) {
|
||||
/* parent */
|
||||
pidwait_add(pid);
|
||||
return pid;
|
||||
}
|
||||
|
||||
if (pid != 0) {
|
||||
/* failed! */
|
||||
g_warning("net_connect_thread(): fork() failed! "
|
||||
"Using blocking resolving");
|
||||
}
|
||||
|
||||
/* child */
|
||||
srand(time(NULL));
|
||||
|
||||
memset(&rec, 0, sizeof(rec));
|
||||
rec.error = net_gethostbyname(addr, &rec.ip4, &rec.ip6);
|
||||
if (rec.error == 0) {
|
||||
errorstr = NULL;
|
||||
} else {
|
||||
errorstr = net_gethosterror(rec.error);
|
||||
rec.errlen = errorstr == NULL ? 0 : strlen(errorstr)+1;
|
||||
}
|
||||
|
||||
i_io_channel_write_block(pipe, &rec, sizeof(rec));
|
||||
if (rec.errlen != 0)
|
||||
i_io_channel_write_block(pipe, (void *) errorstr, rec.errlen);
|
||||
|
||||
if (pid == 0)
|
||||
_exit(99);
|
||||
|
||||
/* we used blocking lookup */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* get the resolved IP address */
|
||||
int net_gethostbyname_return(GIOChannel *pipe, RESOLVED_IP_REC *rec)
|
||||
{
|
||||
rec->error = -1;
|
||||
rec->errorstr = NULL;
|
||||
|
||||
fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);
|
||||
|
||||
/* get ip+error */
|
||||
if (i_io_channel_read_block(pipe, rec, sizeof(*rec)) == -1) {
|
||||
rec->errorstr = g_strdup_printf("Host name lookup: %s",
|
||||
g_strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (rec->error) {
|
||||
/* read error string, if we can't read everything for some
|
||||
reason, just ignore it. */
|
||||
rec->errorstr = g_malloc0(rec->errlen+1);
|
||||
i_io_channel_read_block(pipe, rec->errorstr, rec->errlen);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Kill the resolver child */
|
||||
void net_disconnect_nonblock(int pid)
|
||||
{
|
||||
g_return_if_fail(pid > 0);
|
||||
|
||||
kill(pid, SIGKILL);
|
||||
resolver = g_resolver_get_default();
|
||||
cancellable = g_cancellable_new();
|
||||
data = g_new0(NET_GETHOSTBYNAME_CALLBACK_DATA, 1);
|
||||
data->cont = cont;
|
||||
data->cont_data = cont_data;
|
||||
g_resolver_lookup_by_name_with_flags_async(resolver, addr, flags, cancellable,
|
||||
(GAsyncReadyCallback) net_gethostbyname_callback,
|
||||
data);
|
||||
return cancellable;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue