/* 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) { RESOLVED_IP_REC rec; const char *errorstr; #ifndef WIN32 int pid; #endif g_return_val_if_fail(addr != NULL, FALSE); #ifndef WIN32 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"); } #endif /* child */ 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; } g_io_channel_write_block(pipe, &rec, sizeof(rec)); if (rec.errlen != 0) g_io_channel_write_block(pipe, (void *) errorstr, rec.errlen); #ifndef WIN32 if (pid == 0) _exit(99); #endif /* we used blocking lookup */ return 0; }
/* 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; int len; 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; if (reverse_lookup) { /* reverse lookup the IP, ignore any error */ if (rec.ip4.family != 0) net_gethostbyaddr(&rec.ip4, &rec.host4); if (rec.ip6.family != 0) net_gethostbyaddr(&rec.ip6, &rec.host6); } } else { errorstr = net_gethosterror(rec.error); rec.errlen = errorstr == NULL ? 0 : strlen(errorstr)+1; } g_io_channel_write_block(pipe, &rec, sizeof(rec)); if (rec.errlen != 0) g_io_channel_write_block(pipe, (void *) errorstr, rec.errlen); else { if (rec.host4) { len = strlen(rec.host4) + 1; g_io_channel_write_block(pipe, (void *) &len, sizeof(int)); 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, len); } } if (pid == 0) _exit(99); /* we used blocking lookup */ return 0; }