Merged from silc_1_0_branch.
[silc.git] / apps / irssi / src / core / net-nonblock.c
1 /*
2  net-nonblock.c : Nonblocking net_connect()
3
4     Copyright (C) 1998-2000 Timo Sirainen
5
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.
10
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.
15
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
19 */
20
21 #include "module.h"
22
23 #include <signal.h>
24
25 #include "pidwait.h"
26 #include "net-nonblock.h"
27
28 typedef struct {
29         NET_CALLBACK func;
30         void *data;
31
32         GIOChannel *pipes[2];
33         int port;
34         IPADDR *my_ip;
35         int tag;
36 } SIMPLE_THREAD_REC;
37
38 #define is_fatal_error(err) \
39         (err != 0 && err != G_IO_ERROR_AGAIN && errno != EINTR)
40
41 static int g_io_channel_write_block(GIOChannel *channel, void *data, int len)
42 {
43         unsigned int ret;
44         int err, sent;
45
46         sent = 0;
47         do {
48                 err = g_io_channel_write(channel, (char *) data + sent,
49                                          len-sent, &ret);
50                 sent += ret;
51         } while (sent < len && !is_fatal_error(err));
52
53         return err != 0 ? -1 : 0;
54 }
55
56 static int g_io_channel_read_block(GIOChannel *channel, void *data, int len)
57 {
58         time_t maxwait;
59         unsigned int ret;
60         int err, received;
61
62         maxwait = time(NULL)+2;
63         received = 0;
64         do {
65                 err = g_io_channel_read(channel, (char *) data + received,
66                                         len-received, &ret);
67                 received += ret;
68         } while (received < len && time(NULL) < maxwait &&
69                  (ret != 0 || !is_fatal_error(err)));
70
71         return received < len ? -1 : 0;
72 }
73
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,
77                                int reverse_lookup)
78 {
79         RESOLVED_IP_REC rec;
80         const char *errorstr;
81 #ifndef WIN32
82         int pid;
83 #endif
84         int len;
85
86         g_return_val_if_fail(addr != NULL, FALSE);
87
88 #ifndef WIN32
89         pid = fork();
90         if (pid > 0) {
91                 /* parent */
92                 pidwait_add(pid);
93                 return pid;
94         }
95
96         if (pid != 0) {
97                 /* failed! */
98                 g_warning("net_connect_thread(): fork() failed! "
99                           "Using blocking resolving");
100         }
101 #endif
102
103         /* child */
104         memset(&rec, 0, sizeof(rec));
105         rec.error = net_gethostbyname(addr, &rec.ip4, &rec.ip6);
106         if (rec.error == 0) {
107                 errorstr = NULL;
108                 if (reverse_lookup) {
109                         /* reverse lookup the IP, ignore any error */
110                         if (rec.ip4.family != 0)
111                                 net_gethostbyaddr(&rec.ip4, &rec.host4);
112                         if (rec.ip6.family != 0)
113                                 net_gethostbyaddr(&rec.ip6, &rec.host6);
114                 }
115         } else {
116                 errorstr = net_gethosterror(rec.error);
117                 rec.errlen = errorstr == NULL ? 0 : strlen(errorstr)+1;
118         }
119
120         g_io_channel_write_block(pipe, &rec, sizeof(rec));
121         if (rec.errlen != 0)
122                 g_io_channel_write_block(pipe, (void *) errorstr, rec.errlen);
123         else {
124                 if (rec.host4) {
125                         len = strlen(rec.host4) + 1;
126                         g_io_channel_write_block(pipe, (void *) &len, 
127                                                        sizeof(int));
128                         g_io_channel_write_block(pipe, (void *) rec.host4,
129                                                        len);
130                 }
131                 if (rec.host6) {
132                         len = strlen(rec.host6) + 1;
133                         g_io_channel_write_block(pipe, (void *) &len,
134                                                        sizeof(int));
135                         g_io_channel_write_block(pipe, (void *) rec.host6,
136                                                        len);
137                 }
138         }
139
140 #ifndef WIN32
141         if (pid == 0)
142                 _exit(99);
143 #endif
144
145         /* we used blocking lookup */
146         return 0;
147 }
148
149 /* get the resolved IP address */
150 int net_gethostbyname_return(GIOChannel *pipe, RESOLVED_IP_REC *rec)
151 {
152         int len;
153
154         rec->error = -1;
155         rec->errorstr = NULL;
156         rec->host4 = NULL;
157         rec->host6 = NULL;
158
159 #ifndef WIN32
160         fcntl(g_io_channel_unix_get_fd(pipe), F_SETFL, O_NONBLOCK);
161 #endif
162
163         /* get ip+error */
164         if (g_io_channel_read_block(pipe, rec, sizeof(*rec)) == -1) {
165                 rec->errorstr = g_strdup_printf("Host name lookup: %s",
166                                                 g_strerror(errno));
167                 return -1;
168         }
169
170         if (rec->error) {
171                 /* read error string, if we can't read everything for some
172                    reason, just ignore it. */
173                 rec->errorstr = g_malloc0(rec->errlen+1);
174                 g_io_channel_read_block(pipe, rec->errorstr, rec->errlen);
175         } else {
176                 if (rec->host4) {
177                         g_io_channel_read_block(pipe, &len, sizeof(int));
178                         rec->host4 = g_malloc0(len);
179                         g_io_channel_read_block(pipe, rec->host4, len);
180                 }
181                 if (rec->host6) {
182                         g_io_channel_read_block(pipe, &len, sizeof(int));
183                         rec->host6 = g_malloc0(len);
184                         g_io_channel_read_block(pipe, rec->host6, len);
185                 }
186         }
187
188         return 0;
189 }
190
191 /* Get host name, call func when finished */
192 int net_gethostbyaddr_nonblock(IPADDR *ip, NET_HOST_CALLBACK func, void *data)
193 {
194         /* FIXME: not implemented */
195         return FALSE;
196 }
197
198 /* Kill the resolver child */
199 void net_disconnect_nonblock(int pid)
200 {
201         g_return_if_fail(pid > 0);
202
203 #ifndef WIN32
204         kill(pid, SIGKILL);
205 #endif
206 }
207
208 static void simple_init(SIMPLE_THREAD_REC *rec, GIOChannel *handle)
209 {
210         g_return_if_fail(rec != NULL);
211
212         g_source_remove(rec->tag);
213
214         if (net_geterror(handle) != 0) {
215                 /* failed */
216                 g_io_channel_close(handle);
217                 g_io_channel_unref(handle);
218                 handle = NULL;
219         }
220
221         rec->func(handle, rec->data);
222         g_free(rec);
223 }
224
225 static void simple_readpipe(SIMPLE_THREAD_REC *rec, GIOChannel *pipe)
226 {
227         RESOLVED_IP_REC iprec;
228         GIOChannel *handle;
229         IPADDR *ip;
230
231         g_return_if_fail(rec != NULL);
232
233         g_source_remove(rec->tag);
234
235         net_gethostbyname_return(pipe, &iprec);
236         g_free_not_null(iprec.errorstr);
237
238         g_io_channel_close(rec->pipes[0]);
239         g_io_channel_unref(rec->pipes[0]);
240         g_io_channel_close(rec->pipes[1]);
241         g_io_channel_unref(rec->pipes[1]);
242
243         ip = iprec.ip4.family != 0 ? &iprec.ip4 : &iprec.ip6;
244         handle = iprec.error == -1 ? NULL :
245                 net_connect_ip(ip, rec->port, rec->my_ip);
246
247         g_free_not_null(rec->my_ip);
248
249         if (handle == NULL) {
250                 /* failed */
251                 rec->func(NULL, rec->data);
252                 g_free(rec);
253                 return;
254         }
255
256         rec->tag = g_input_add(handle, G_INPUT_READ | G_INPUT_WRITE,
257                                (GInputFunction) simple_init, rec);
258 }
259
260 /* Connect to server, call func when finished */
261 int net_connect_nonblock(const char *server, int port, const IPADDR *my_ip,
262                          NET_CALLBACK func, void *data)
263 {
264         SIMPLE_THREAD_REC *rec;
265         int fd[2];
266
267         g_return_val_if_fail(server != NULL, FALSE);
268         g_return_val_if_fail(func != NULL, FALSE);
269
270         if (pipe(fd) != 0) {
271                 g_warning("net_connect_nonblock(): pipe() failed.");
272                 return FALSE;
273         }
274
275         rec = g_new0(SIMPLE_THREAD_REC, 1);
276         rec->port = port;
277         if (my_ip != NULL) {
278                 rec->my_ip = g_malloc(sizeof(IPADDR));
279                 memcpy(rec->my_ip, my_ip, sizeof(IPADDR));
280         }
281         rec->func = func;
282         rec->data = data;
283         rec->pipes[0] = g_io_channel_unix_new(fd[0]);
284         rec->pipes[1] = g_io_channel_unix_new(fd[1]);
285
286         /* start nonblocking host name lookup */
287         net_gethostbyname_nonblock(server, rec->pipes[1], 0);
288         rec->tag = g_input_add(rec->pipes[0], G_INPUT_READ,
289                                (GInputFunction) simple_readpipe, rec);
290
291         return TRUE;
292 }