summaryrefslogtreecommitdiff
path: root/src/core/net-nonblock.c
blob: d6e767b6096fa7c7272ea24a5161e9f7504c63a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
 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.

    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.
*/

#include "module.h"

#include <signal.h>

#include "pidwait.h"
#include "net-nonblock.h"

typedef struct {
	NET_CALLBACK func;
	void *data;

	GIOChannel *pipes[2];
	int port;
	IPADDR *my_ip;
	int tag;
} SIMPLE_THREAD_REC;

static int g_io_channel_write_block(GIOChannel *channel, void *data, int len)
{
        gsize ret;
	int sent;
	GIOStatus status;

	sent = 0;
	do {
		status = g_io_channel_write_chars(channel, (char *) data + sent,
						  len-sent, &ret, NULL);
                sent += ret;
	} while (sent < len && status != G_IO_STATUS_ERROR);

	return sent < len ? -1 : 0;
}

static int g_io_channel_read_block(GIOChannel *channel, void *data, int len)
{
	time_t maxwait;
        gsize ret;
	int received;
	GIOStatus status;

	maxwait = time(NULL)+2;
	received = 0;
	do {
		status = g_io_channel_read_chars(channel, (char *) data + received,
						 len-received, &ret, NULL);
		received += ret;
	} while (received < len && time(NULL) < maxwait &&
		 status != G_IO_STATUS_ERROR && status != G_IO_STATUS_EOF);

	return received < len ? -1 : 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;
}

/* get the resolved IP address */
int net_gethostbyname_return(GIOChannel *pipe, RESOLVED_IP_REC *rec)
{
	int len;

	rec->error = -1;
	rec->errorstr = NULL;
	rec->host4 = NULL;
	rec->host6 = NULL;

	fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);

	/* 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;
	}

	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);
                g_io_channel_read_block(pipe, rec->errorstr, rec->errlen);
	} else {
		if (rec->host4) {
			g_io_channel_read_block(pipe, &len, sizeof(int));
			rec->host4 = g_malloc0(len);
			g_io_channel_read_block(pipe, rec->host4, len);
		}
		if (rec->host6) {
			g_io_channel_read_block(pipe, &len, sizeof(int));
			rec->host6 = g_malloc0(len);
			g_io_channel_read_block(pipe, rec->host6, len);
		}
	}

	return 0;
}

/* Get host name, call func when finished */
int net_gethostbyaddr_nonblock(IPADDR *ip, NET_HOST_CALLBACK func, void *data)
{
	/* FIXME: not implemented */
	return FALSE;
}

/* Kill the resolver child */
void net_disconnect_nonblock(int pid)
{
	g_return_if_fail(pid > 0);

	kill(pid, SIGKILL);
}

static void simple_init(SIMPLE_THREAD_REC *rec, GIOChannel *handle)
{
	g_return_if_fail(rec != NULL);

	g_source_remove(rec->tag);

	if (net_geterror(handle) != 0) {
		/* failed */
		g_io_channel_shutdown(handle, TRUE, NULL);
                g_io_channel_unref(handle);
		handle = NULL;
	}

	rec->func(handle, rec->data);
	g_free(rec);
}

static void simple_readpipe(SIMPLE_THREAD_REC *rec, GIOChannel *pipe)
{
	RESOLVED_IP_REC iprec;
	GIOChannel *handle;
	IPADDR *ip;

	g_return_if_fail(rec != NULL);

	g_source_remove(rec->tag);

	net_gethostbyname_return(pipe, &iprec);
	g_free_not_null(iprec.errorstr);

	g_io_channel_shutdown(rec->pipes[0], TRUE, NULL);
	g_io_channel_unref(rec->pipes[0]);
	g_io_channel_shutdown(rec->pipes[1], TRUE, NULL);
	g_io_channel_unref(rec->pipes[1]);

	ip = iprec.ip4.family != 0 ? &iprec.ip4 : &iprec.ip6;
	handle = iprec.error == -1 ? NULL :
		net_connect_ip(ip, rec->port, rec->my_ip);

	g_free_not_null(rec->my_ip);

	if (handle == NULL) {
		/* failed */
		rec->func(NULL, rec->data);
		g_free(rec);
		return;
	}

	rec->tag = g_input_add(handle, G_INPUT_READ | G_INPUT_WRITE,
			       (GInputFunction) simple_init, rec);
}

/* Connect to server, call func when finished */
int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
			 NET_CALLBACK func, void *data)
{
	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;
	rec->pipes[0] = g_io_channel_new(fd[0]);
	rec->pipes[1] = g_io_channel_new(fd[1]);

	/* start nonblocking host name lookup */
	net_gethostbyname_nonblock(server, rec->pipes[1], 0);
	rec->tag = g_input_add(rec->pipes[0], G_INPUT_READ,
			       (GInputFunction) simple_readpipe, rec);

	return TRUE;
}