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 along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "net-nonblock.h"
38 static int g_io_channel_write_block(GIOChannel *channel, void *data, int len)
46 status = g_io_channel_write_chars(channel, (char *) data + sent,
47 len-sent, &ret, NULL);
49 } while (sent < len && status != G_IO_STATUS_ERROR);
51 return sent < len ? -1 : 0;
54 static int g_io_channel_read_block(GIOChannel *channel, void *data, int len)
61 maxwait = time(NULL)+2;
64 status = g_io_channel_read_chars(channel, (char *) data + received,
65 len-received, &ret, NULL);
67 } while (received < len && time(NULL) < maxwait &&
68 status != G_IO_STATUS_ERROR && status != G_IO_STATUS_EOF);
70 return received < len ? -1 : 0;
73 /* nonblocking gethostbyname(), ip (IPADDR) + error (int, 0 = not error) is
74 written to pipe when found PID of the resolver child is returned */
75 int net_gethostbyname_nonblock(const char *addr, GIOChannel *pipe,
85 g_return_val_if_fail(addr != NULL, FALSE);
97 g_warning("net_connect_thread(): fork() failed! "
98 "Using blocking resolving");
105 memset(&rec, 0, sizeof(rec));
106 rec.error = net_gethostbyname(addr, &rec.ip4, &rec.ip6);
107 if (rec.error == 0) {
109 if (reverse_lookup) {
110 /* reverse lookup the IP, ignore any error */
111 if (rec.ip4.family != 0)
112 net_gethostbyaddr(&rec.ip4, &rec.host4);
113 if (rec.ip6.family != 0)
114 net_gethostbyaddr(&rec.ip6, &rec.host6);
117 errorstr = net_gethosterror(rec.error);
118 rec.errlen = errorstr == NULL ? 0 : strlen(errorstr)+1;
121 g_io_channel_write_block(pipe, &rec, sizeof(rec));
123 g_io_channel_write_block(pipe, (void *) errorstr, rec.errlen);
126 len = strlen(rec.host4) + 1;
127 g_io_channel_write_block(pipe, (void *) &len,
129 g_io_channel_write_block(pipe, (void *) rec.host4,
133 len = strlen(rec.host6) + 1;
134 g_io_channel_write_block(pipe, (void *) &len,
136 g_io_channel_write_block(pipe, (void *) rec.host6,
146 /* we used blocking lookup */
150 /* get the resolved IP address */
151 int net_gethostbyname_return(GIOChannel *pipe, RESOLVED_IP_REC *rec)
156 rec->errorstr = NULL;
161 fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);
165 if (g_io_channel_read_block(pipe, rec, sizeof(*rec)) == -1) {
166 rec->errorstr = g_strdup_printf("Host name lookup: %s",
172 /* read error string, if we can't read everything for some
173 reason, just ignore it. */
174 rec->errorstr = g_malloc0(rec->errlen+1);
175 g_io_channel_read_block(pipe, rec->errorstr, rec->errlen);
178 g_io_channel_read_block(pipe, &len, sizeof(int));
179 rec->host4 = g_malloc0(len);
180 g_io_channel_read_block(pipe, rec->host4, len);
183 g_io_channel_read_block(pipe, &len, sizeof(int));
184 rec->host6 = g_malloc0(len);
185 g_io_channel_read_block(pipe, rec->host6, len);
192 /* Get host name, call func when finished */
193 int net_gethostbyaddr_nonblock(IPADDR *ip, NET_HOST_CALLBACK func, void *data)
195 /* FIXME: not implemented */
199 /* Kill the resolver child */
200 void net_disconnect_nonblock(int pid)
202 g_return_if_fail(pid > 0);
209 static void simple_init(SIMPLE_THREAD_REC *rec, GIOChannel *handle)
211 g_return_if_fail(rec != NULL);
213 g_source_remove(rec->tag);
215 if (net_geterror(handle) != 0) {
217 g_io_channel_close(handle);
218 g_io_channel_unref(handle);
222 rec->func(handle, rec->data);
226 static void simple_readpipe(SIMPLE_THREAD_REC *rec, GIOChannel *pipe)
228 RESOLVED_IP_REC iprec;
232 g_return_if_fail(rec != NULL);
234 g_source_remove(rec->tag);
236 net_gethostbyname_return(pipe, &iprec);
237 g_free_not_null(iprec.errorstr);
239 g_io_channel_close(rec->pipes[0]);
240 g_io_channel_unref(rec->pipes[0]);
241 g_io_channel_close(rec->pipes[1]);
242 g_io_channel_unref(rec->pipes[1]);
244 ip = iprec.ip4.family != 0 ? &iprec.ip4 : &iprec.ip6;
245 handle = iprec.error == -1 ? NULL :
246 net_connect_ip(ip, rec->port, rec->my_ip);
248 g_free_not_null(rec->my_ip);
250 if (handle == NULL) {
252 rec->func(NULL, rec->data);
257 rec->tag = g_input_add(handle, G_INPUT_READ | G_INPUT_WRITE,
258 (GInputFunction) simple_init, rec);
261 /* Connect to server, call func when finished */
262 int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
263 NET_CALLBACK func, void *data)
265 SIMPLE_THREAD_REC *rec;
268 g_return_val_if_fail(server != NULL, FALSE);
269 g_return_val_if_fail(func != NULL, FALSE);
272 g_warning("net_connect_nonblock(): pipe() failed.");
276 rec = g_new0(SIMPLE_THREAD_REC, 1);
279 rec->my_ip = g_malloc(sizeof(IPADDR));
280 memcpy(rec->my_ip, my_ip, sizeof(IPADDR));
284 rec->pipes[0] = g_io_channel_new(fd[0]);
285 rec->pipes[1] = g_io_channel_new(fd[1]);
287 /* start nonblocking host name lookup */
288 net_gethostbyname_nonblock(server, rec->pipes[1], 0);
289 rec->tag = g_input_add(rec->pipes[0], G_INPUT_READ,
290 (GInputFunction) simple_readpipe, rec);