2 net-nonblock.c : Nonblocking net_connect()
4 Copyright (C) 1998-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include "net-nonblock.h"
38 #define is_fatal_error(err) \
39 (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
41 static int g_io_channel_write_block(GIOChannel *channel, void *data, int len)
48 err = g_io_channel_write(channel, (char *) data + sent,
51 } while (sent < len && !is_fatal_error(err));
53 return err != 0 ? -1 : 0;
56 static int g_io_channel_read_block(GIOChannel *channel, void *data, int len)
62 maxwait = time(NULL)+2;
65 err = g_io_channel_read(channel, (char *) data + received,
68 } while (received < len && time(NULL) < maxwait &&
69 (ret != 0 || !is_fatal_error(err)));
71 return received < len ? -1 : 0;
74 /* nonblocking gethostbyname(), ip (IPADDR) + error (int, 0 = not error) is
75 written to pipe when found PID of the resolver child is returned */
76 int net_gethostbyname_nonblock(const char *addr, GIOChannel *pipe,
86 g_return_val_if_fail(addr != NULL, FALSE);
98 g_warning("net_connect_thread(): fork() failed! "
99 "Using blocking resolving");
106 memset(&rec, 0, sizeof(rec));
107 rec.error = net_gethostbyname(addr, &rec.ip4, &rec.ip6);
108 if (rec.error == 0) {
110 if (reverse_lookup) {
111 /* reverse lookup the IP, ignore any error */
112 if (rec.ip4.family != 0)
113 net_gethostbyaddr(&rec.ip4, &rec.host4);
114 if (rec.ip6.family != 0)
115 net_gethostbyaddr(&rec.ip6, &rec.host6);
118 errorstr = net_gethosterror(rec.error);
119 rec.errlen = errorstr == NULL ? 0 : strlen(errorstr)+1;
122 g_io_channel_write_block(pipe, &rec, sizeof(rec));
124 g_io_channel_write_block(pipe, (void *) errorstr, rec.errlen);
127 len = strlen(rec.host4) + 1;
128 g_io_channel_write_block(pipe, (void *) &len,
130 g_io_channel_write_block(pipe, (void *) rec.host4,
134 len = strlen(rec.host6) + 1;
135 g_io_channel_write_block(pipe, (void *) &len,
137 g_io_channel_write_block(pipe, (void *) rec.host6,
147 /* we used blocking lookup */
151 /* get the resolved IP address */
152 int net_gethostbyname_return(GIOChannel *pipe, RESOLVED_IP_REC *rec)
157 rec->errorstr = NULL;
162 fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);
166 if (g_io_channel_read_block(pipe, rec, sizeof(*rec)) == -1) {
167 rec->errorstr = g_strdup_printf("Host name lookup: %s",
173 /* read error string, if we can't read everything for some
174 reason, just ignore it. */
175 rec->errorstr = g_malloc0(rec->errlen+1);
176 g_io_channel_read_block(pipe, rec->errorstr, rec->errlen);
179 g_io_channel_read_block(pipe, &len, sizeof(int));
180 rec->host4 = g_malloc0(len);
181 g_io_channel_read_block(pipe, rec->host4, len);
184 g_io_channel_read_block(pipe, &len, sizeof(int));
185 rec->host6 = g_malloc0(len);
186 g_io_channel_read_block(pipe, rec->host6, len);
193 /* Get host name, call func when finished */
194 int net_gethostbyaddr_nonblock(IPADDR *ip, NET_HOST_CALLBACK func, void *data)
196 /* FIXME: not implemented */
200 /* Kill the resolver child */
201 void net_disconnect_nonblock(int pid)
203 g_return_if_fail(pid > 0);
210 static void simple_init(SIMPLE_THREAD_REC *rec, GIOChannel *handle)
212 g_return_if_fail(rec != NULL);
214 g_source_remove(rec->tag);
216 if (net_geterror(handle) != 0) {
218 g_io_channel_close(handle);
219 g_io_channel_unref(handle);
223 rec->func(handle, rec->data);
227 static void simple_readpipe(SIMPLE_THREAD_REC *rec, GIOChannel *pipe)
229 RESOLVED_IP_REC iprec;
233 g_return_if_fail(rec != NULL);
235 g_source_remove(rec->tag);
237 net_gethostbyname_return(pipe, &iprec);
238 g_free_not_null(iprec.errorstr);
240 g_io_channel_close(rec->pipes[0]);
241 g_io_channel_unref(rec->pipes[0]);
242 g_io_channel_close(rec->pipes[1]);
243 g_io_channel_unref(rec->pipes[1]);
245 ip = iprec.ip4.family != 0 ? &iprec.ip4 : &iprec.ip6;
246 handle = iprec.error == -1 ? NULL :
247 net_connect_ip(ip, rec->port, rec->my_ip);
249 g_free_not_null(rec->my_ip);
251 if (handle == NULL) {
253 rec->func(NULL, rec->data);
258 rec->tag = g_input_add(handle, G_INPUT_READ | G_INPUT_WRITE,
259 (GInputFunction) simple_init, rec);
262 /* Connect to server, call func when finished */
263 int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
264 NET_CALLBACK func, void *data)
266 SIMPLE_THREAD_REC *rec;
269 g_return_val_if_fail(server != NULL, FALSE);
270 g_return_val_if_fail(func != NULL, FALSE);
273 g_warning("net_connect_nonblock(): pipe() failed.");
277 rec = g_new0(SIMPLE_THREAD_REC, 1);
280 rec->my_ip = g_malloc(sizeof(IPADDR));
281 memcpy(rec->my_ip, my_ip, sizeof(IPADDR));
285 rec->pipes[0] = g_io_channel_unix_new(fd[0]);
286 rec->pipes[1] = g_io_channel_unix_new(fd[1]);
288 /* start nonblocking host name lookup */
289 net_gethostbyname_nonblock(server, rec->pipes[1], 0);
290 rec->tag = g_input_add(rec->pipes[0], G_INPUT_READ,
291 (GInputFunction) simple_readpipe, rec);