14192
|
1 /**
|
|
2 * @file dnsquery.c DNS query API
|
|
3 * @ingroup core
|
|
4 *
|
|
5 * gaim
|
|
6 *
|
|
7 * Gaim is the legal property of its developers, whose names are too numerous
|
|
8 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
9 * source distribution.
|
|
10 *
|
|
11 * This program is free software; you can redistribute it and/or modify
|
|
12 * it under the terms of the GNU General Public License as published by
|
|
13 * the Free Software Foundation; either version 2 of the License, or
|
|
14 * (at your option) any later version.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
24 *
|
|
25 */
|
|
26
|
|
27 #include "internal.h"
|
|
28 #include "debug.h"
|
14238
|
29 #include "dnsquery.h"
|
14192
|
30 #include "notify.h"
|
|
31 #include "prefs.h"
|
|
32 #include "util.h"
|
|
33
|
|
34 /**************************************************************************
|
|
35 * DNS query API
|
|
36 **************************************************************************/
|
|
37
|
14238
|
38 typedef struct _GaimDnsQueryResolverProcess GaimDnsQueryResolverProcess;
|
|
39
|
14192
|
40 struct _GaimDnsQueryData {
|
14238
|
41 char *hostname;
|
|
42 int port;
|
|
43 GaimDnsQueryConnectFunction callback;
|
|
44 gpointer data;
|
|
45 guint timeout;
|
|
46
|
14287
|
47 #if defined(__unix__) || defined(__APPLE__)
|
|
48 GaimDnsQueryResolverProcess *resolver;
|
|
49 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
14238
|
50 GThread *resolver;
|
|
51 GSList *hosts;
|
|
52 gchar *error_message;
|
14287
|
53 #endif
|
14192
|
54 };
|
|
55
|
14287
|
56 #if defined(__unix__) || defined(__APPLE__)
|
|
57
|
|
58 #define MAX_DNS_CHILDREN 4
|
|
59
|
|
60 /*
|
|
61 * This structure keeps a reference to a child resolver process.
|
|
62 */
|
|
63 struct _GaimDnsQueryResolverProcess {
|
|
64 guint inpa;
|
|
65 int fd_in, fd_out;
|
|
66 pid_t dns_pid;
|
|
67 };
|
|
68
|
|
69 static GSList *free_dns_children = NULL;
|
|
70 static GQueue *queued_requests = NULL;
|
|
71
|
|
72 static int number_of_dns_children = 0;
|
|
73
|
|
74 /*
|
|
75 * This is a convenience struct used to pass data to
|
|
76 * the child resolver process.
|
|
77 */
|
|
78 typedef struct {
|
|
79 char hostname[512];
|
|
80 int port;
|
|
81 } dns_params_t;
|
|
82 #endif
|
|
83
|
14238
|
84 static void
|
|
85 gaim_dnsquery_resolved(GaimDnsQueryData *query_data, GSList *hosts)
|
|
86 {
|
14244
|
87 gaim_debug_info("dnsquery", "IP resolved for %s\n", query_data->hostname);
|
14238
|
88 if (query_data->callback != NULL)
|
|
89 query_data->callback(hosts, query_data->data, NULL);
|
14358
|
90 else
|
|
91 {
|
|
92 /*
|
|
93 * Callback is a required parameter, but it can get set to
|
|
94 * NULL if we cancel a thread-based DNS lookup. So we need
|
|
95 * to free hosts.
|
|
96 */
|
|
97 while (hosts != NULL)
|
|
98 {
|
|
99 hosts = g_slist_remove(hosts, hosts->data);
|
|
100 g_free(hosts->data);
|
|
101 hosts = g_slist_remove(hosts, hosts->data);
|
|
102 }
|
|
103 }
|
|
104
|
14238
|
105 gaim_dnsquery_destroy(query_data);
|
|
106 }
|
|
107
|
|
108 static void
|
|
109 gaim_dnsquery_failed(GaimDnsQueryData *query_data, const gchar *error_message)
|
|
110 {
|
|
111 gaim_debug_info("dnsquery", "%s\n", error_message);
|
|
112 if (query_data->callback != NULL)
|
|
113 query_data->callback(NULL, query_data->data, error_message);
|
|
114 gaim_dnsquery_destroy(query_data);
|
|
115 }
|
|
116
|
14287
|
117 #if defined(__unix__) || defined(__APPLE__)
|
|
118
|
|
119 /*
|
|
120 * Unix!
|
|
121 */
|
|
122
|
|
123 /*
|
|
124 * Begin the DNS resolver child process functions.
|
|
125 */
|
|
126 #ifdef HAVE_SIGNAL_H
|
|
127 static void
|
|
128 trap_gdb_bug()
|
|
129 {
|
|
130 const char *message =
|
|
131 "Gaim's DNS child got a SIGTRAP signal.\n"
|
|
132 "This can be caused by trying to run gaim inside gdb.\n"
|
|
133 "There is a known gdb bug which prevents this. Supposedly gaim\n"
|
|
134 "should have detected you were using gdb and used an ugly hack,\n"
|
|
135 "check cope_with_gdb_brokenness() in dnsquery.c.\n\n"
|
|
136 "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n";
|
|
137 fputs("\n* * *\n",stderr);
|
|
138 fputs(message,stderr);
|
|
139 fputs("* * *\n\n",stderr);
|
|
140 execlp("xmessage","xmessage","-center", message, NULL);
|
|
141 _exit(1);
|
|
142 }
|
|
143 #endif
|
|
144
|
|
145 static void
|
|
146 gaim_dnsquery_resolver_run(int child_out, int child_in, gboolean show_debug)
|
|
147 {
|
|
148 dns_params_t dns_params;
|
|
149 const size_t zero = 0;
|
|
150 int rc;
|
|
151 #ifdef HAVE_GETADDRINFO
|
|
152 struct addrinfo hints, *res, *tmp;
|
|
153 char servname[20];
|
|
154 #else
|
|
155 struct sockaddr_in sin;
|
|
156 const size_t addrlen = sizeof(sin);
|
|
157 #endif
|
|
158
|
|
159 #ifdef HAVE_SIGNAL_H
|
|
160 signal(SIGHUP, SIG_DFL);
|
|
161 signal(SIGINT, SIG_DFL);
|
|
162 signal(SIGQUIT, SIG_DFL);
|
|
163 signal(SIGCHLD, SIG_DFL);
|
|
164 signal(SIGTERM, SIG_DFL);
|
|
165 signal(SIGTRAP, trap_gdb_bug);
|
|
166 #endif
|
|
167
|
|
168 /*
|
|
169 * We resolve 1 host name for each iteration of this
|
|
170 * while loop.
|
|
171 *
|
|
172 * The top half of this reads in the hostname and port
|
|
173 * number from the socket with our parent. The bottom
|
|
174 * half of this resolves the IP (blocking) and sends
|
|
175 * the result back to our parent, when finished.
|
|
176 */
|
|
177 while (1) {
|
|
178 const char ch = 'Y';
|
|
179 fd_set fds;
|
|
180 struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 };
|
|
181 FD_ZERO(&fds);
|
|
182 FD_SET(child_in, &fds);
|
|
183 rc = select(child_in + 1, &fds, NULL, NULL, &tv);
|
|
184 if (!rc) {
|
|
185 if (show_debug)
|
|
186 printf("dns[%d]: nobody needs me... =(\n", getpid());
|
|
187 break;
|
|
188 }
|
|
189 rc = read(child_in, &dns_params, sizeof(dns_params_t));
|
|
190 if (rc < 0) {
|
|
191 perror("read()");
|
|
192 break;
|
|
193 }
|
|
194 if (rc == 0) {
|
|
195 if (show_debug)
|
|
196 printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid());
|
|
197 _exit(0);
|
|
198 }
|
|
199 if (dns_params.hostname[0] == '\0') {
|
|
200 printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port);
|
|
201 _exit(1);
|
|
202 }
|
|
203 /* Tell our parent that we read the data successfully */
|
|
204 write(child_out, &ch, sizeof(ch));
|
|
205
|
|
206 /* We have the hostname and port, now resolve the IP */
|
|
207
|
|
208 #ifdef HAVE_GETADDRINFO
|
|
209 g_snprintf(servname, sizeof(servname), "%d", dns_params.port);
|
|
210 memset(&hints, 0, sizeof(hints));
|
|
211
|
|
212 /* This is only used to convert a service
|
|
213 * name to a port number. As we know we are
|
|
214 * passing a number already, we know this
|
|
215 * value will not be really used by the C
|
|
216 * library.
|
|
217 */
|
|
218 hints.ai_socktype = SOCK_STREAM;
|
|
219 rc = getaddrinfo(dns_params.hostname, servname, &hints, &res);
|
|
220 write(child_out, &rc, sizeof(rc));
|
|
221 if (rc != 0) {
|
|
222 close(child_out);
|
|
223 if (show_debug)
|
|
224 printf("dns[%d] Error: getaddrinfo returned %d\n",
|
|
225 getpid(), rc);
|
|
226 dns_params.hostname[0] = '\0';
|
|
227 continue;
|
|
228 }
|
|
229 tmp = res;
|
|
230 while (res) {
|
|
231 size_t ai_addrlen = res->ai_addrlen;
|
|
232 write(child_out, &ai_addrlen, sizeof(ai_addrlen));
|
|
233 write(child_out, res->ai_addr, res->ai_addrlen);
|
|
234 res = res->ai_next;
|
|
235 }
|
|
236 freeaddrinfo(tmp);
|
|
237 write(child_out, &zero, sizeof(zero));
|
|
238 #else
|
|
239 if (!inet_aton(dns_params.hostname, &sin.sin_addr)) {
|
|
240 struct hostent *hp;
|
|
241 if (!(hp = gethostbyname(dns_params.hostname))) {
|
|
242 write(child_out, &h_errno, sizeof(int));
|
|
243 close(child_out);
|
|
244 if (show_debug)
|
|
245 printf("DNS Error: %d\n", h_errno);
|
|
246 _exit(0);
|
|
247 }
|
|
248 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
249 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
250 sin.sin_family = hp->h_addrtype;
|
|
251 } else
|
|
252 sin.sin_family = AF_INET;
|
|
253
|
|
254 sin.sin_port = htons(dns_params.port);
|
|
255 write(child_out, &addrlen, sizeof(addrlen));
|
|
256 write(child_out, &sin, addrlen);
|
|
257 write(child_out, &zero, sizeof(zero));
|
|
258 #endif
|
|
259 dns_params.hostname[0] = '\0';
|
|
260 }
|
|
261
|
|
262 close(child_out);
|
|
263 close(child_in);
|
|
264
|
|
265 _exit(0);
|
|
266 }
|
|
267 /*
|
|
268 * End the DNS resolver child process functions.
|
|
269 */
|
|
270
|
|
271 /*
|
|
272 * Begin the functions for dealing with the DNS child processes.
|
|
273 */
|
|
274 static void
|
|
275 cope_with_gdb_brokenness()
|
|
276 {
|
|
277 #ifdef __linux__
|
|
278 static gboolean already_done = FALSE;
|
|
279 char s[256], e[512];
|
|
280 int n;
|
|
281 pid_t ppid;
|
|
282
|
|
283 if(already_done)
|
|
284 return;
|
|
285 already_done = TRUE;
|
|
286 ppid = getppid();
|
|
287 snprintf(s, sizeof(s), "/proc/%d/exe", ppid);
|
|
288 n = readlink(s, e, sizeof(e));
|
|
289 if(n < 0)
|
|
290 return;
|
|
291
|
|
292 e[MIN(n,sizeof(e)-1)] = '\0';
|
|
293
|
|
294 if(strstr(e,"gdb")) {
|
|
295 gaim_debug_info("dns",
|
|
296 "Debugger detected, performing useless query...\n");
|
|
297 gethostbyname("x.x.x.x.x");
|
|
298 }
|
|
299 #endif
|
|
300 }
|
|
301
|
|
302 static void
|
|
303 gaim_dnsquery_resolver_destroy(GaimDnsQueryResolverProcess *resolver)
|
|
304 {
|
|
305 g_return_if_fail(resolver != NULL);
|
|
306
|
|
307 /*
|
|
308 * We might as well attempt to kill our child process. It really
|
|
309 * doesn't matter if this fails, because children will expire on
|
|
310 * their own after a few seconds.
|
|
311 */
|
|
312 if (resolver->dns_pid > 0)
|
|
313 kill(resolver->dns_pid, SIGKILL);
|
|
314
|
|
315 if (resolver->inpa != 0)
|
|
316 gaim_input_remove(resolver->inpa);
|
|
317
|
|
318 close(resolver->fd_in);
|
|
319 close(resolver->fd_out);
|
|
320
|
|
321 g_free(resolver);
|
|
322
|
|
323 number_of_dns_children--;
|
|
324 }
|
|
325
|
|
326 static GaimDnsQueryResolverProcess *
|
|
327 gaim_dnsquery_resolver_new(gboolean show_debug)
|
|
328 {
|
|
329 GaimDnsQueryResolverProcess *resolver;
|
|
330 int child_out[2], child_in[2];
|
|
331
|
|
332 /* Create pipes for communicating with the child process */
|
|
333 if (pipe(child_out) || pipe(child_in)) {
|
|
334 gaim_debug_error("dns",
|
|
335 "Could not create pipes: %s\n", strerror(errno));
|
|
336 return NULL;
|
|
337 }
|
|
338
|
|
339 resolver = g_new(GaimDnsQueryResolverProcess, 1);
|
|
340 resolver->inpa = 0;
|
|
341
|
|
342 cope_with_gdb_brokenness();
|
|
343
|
|
344 /* "Go fork and multiply." --Tommy Caldwell (Emily's dad, not the climber) */
|
|
345 resolver->dns_pid = fork();
|
|
346
|
|
347 /* If we are the child process... */
|
|
348 if (resolver->dns_pid == 0) {
|
|
349 /* We should not access the parent's side of the pipes, so close them */
|
|
350 close(child_out[0]);
|
|
351 close(child_in[1]);
|
|
352
|
|
353 gaim_dnsquery_resolver_run(child_out[1], child_in[0], show_debug);
|
|
354 /* The thread calls _exit() rather than returning, so we never get here */
|
|
355 }
|
|
356
|
|
357 /* We should not access the child's side of the pipes, so close them */
|
|
358 close(child_out[1]);
|
|
359 close(child_in[0]);
|
|
360 if (resolver->dns_pid == -1) {
|
|
361 gaim_debug_error("dns",
|
|
362 "Could not create child process for DNS: %s\n",
|
|
363 strerror(errno));
|
|
364 gaim_dnsquery_resolver_destroy(resolver);
|
|
365 return NULL;
|
|
366 }
|
|
367
|
|
368 resolver->fd_out = child_out[0];
|
|
369 resolver->fd_in = child_in[1];
|
|
370 number_of_dns_children++;
|
|
371 gaim_debug_info("dns",
|
|
372 "Created new DNS child %d, there are now %d children.\n",
|
|
373 resolver->dns_pid, number_of_dns_children);
|
|
374
|
|
375 return resolver;
|
|
376 }
|
|
377
|
|
378 /**
|
|
379 * @return TRUE if the request was sent succesfully. FALSE
|
|
380 * if the request could not be sent. This isn't
|
|
381 * necessarily an error. If the child has expired,
|
|
382 * for example, we won't be able to send the message.
|
|
383 */
|
|
384 static gboolean
|
|
385 send_dns_request_to_child(GaimDnsQueryData *query_data,
|
|
386 GaimDnsQueryResolverProcess *resolver)
|
|
387 {
|
|
388 pid_t pid;
|
|
389 dns_params_t dns_params;
|
|
390 int rc;
|
|
391 char ch;
|
|
392
|
|
393 /* This waitpid might return the child's PID if it has recently
|
|
394 * exited, or it might return an error if it exited "long
|
|
395 * enough" ago that it has already been reaped; in either
|
|
396 * instance, we can't use it. */
|
|
397 pid = waitpid(resolver->dns_pid, NULL, WNOHANG);
|
|
398 if (pid > 0) {
|
|
399 gaim_debug_warning("dns", "DNS child %d no longer exists\n",
|
|
400 resolver->dns_pid);
|
|
401 gaim_dnsquery_resolver_destroy(resolver);
|
|
402 return FALSE;
|
|
403 } else if (pid < 0) {
|
|
404 gaim_debug_warning("dns", "Wait for DNS child %d failed: %s\n",
|
|
405 resolver->dns_pid, strerror(errno));
|
|
406 gaim_dnsquery_resolver_destroy(resolver);
|
|
407 return FALSE;
|
|
408 }
|
|
409
|
|
410 /* Copy the hostname and port into a single data structure */
|
|
411 strncpy(dns_params.hostname, query_data->hostname, sizeof(dns_params.hostname) - 1);
|
|
412 dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0';
|
|
413 dns_params.port = query_data->port;
|
|
414
|
|
415 /* Send the data structure to the child */
|
|
416 rc = write(resolver->fd_in, &dns_params, sizeof(dns_params));
|
|
417 if (rc < 0) {
|
|
418 gaim_debug_error("dns", "Unable to write to DNS child %d: %d\n",
|
|
419 resolver->dns_pid, strerror(errno));
|
|
420 gaim_dnsquery_resolver_destroy(resolver);
|
|
421 return FALSE;
|
|
422 }
|
|
423
|
|
424 g_return_val_if_fail(rc == sizeof(dns_params), -1);
|
|
425
|
|
426 /* Did you hear me? (This avoids some race conditions) */
|
|
427 rc = read(resolver->fd_out, &ch, sizeof(ch));
|
|
428 if (rc != 1 || ch != 'Y')
|
|
429 {
|
|
430 gaim_debug_warning("dns",
|
|
431 "DNS child %d not responding. Killing it!\n",
|
|
432 resolver->dns_pid);
|
|
433 gaim_dnsquery_resolver_destroy(resolver);
|
|
434 return FALSE;
|
|
435 }
|
|
436
|
|
437 gaim_debug_info("dns",
|
|
438 "Successfully sent DNS request to child %d\n",
|
|
439 resolver->dns_pid);
|
|
440
|
|
441 query_data->resolver = resolver;
|
|
442
|
|
443 return TRUE;
|
|
444 }
|
|
445
|
|
446 static void host_resolved(gpointer data, gint source, GaimInputCondition cond);
|
|
447
|
|
448 static void
|
|
449 handle_next_queued_request()
|
|
450 {
|
|
451 GaimDnsQueryData *query_data;
|
|
452 GaimDnsQueryResolverProcess *resolver;
|
|
453
|
|
454 if ((queued_requests == NULL) || (g_queue_is_empty(queued_requests)))
|
|
455 /* No more DNS queries, yay! */
|
|
456 return;
|
|
457
|
|
458 query_data = g_queue_pop_head(queued_requests);
|
|
459
|
|
460 /*
|
|
461 * If we have any children, attempt to have them perform the DNS
|
|
462 * query. If we're able to send the query then resolver will be
|
|
463 * set to the GaimDnsQueryResolverProcess. Otherwise, resolver
|
|
464 * will be NULL and we'll need to create a new DNS request child.
|
|
465 */
|
|
466 while (free_dns_children != NULL)
|
|
467 {
|
|
468 resolver = free_dns_children->data;
|
|
469 free_dns_children = g_slist_remove(free_dns_children, resolver);
|
|
470
|
|
471 if (send_dns_request_to_child(query_data, resolver))
|
|
472 /* We found an acceptable child, yay */
|
|
473 break;
|
|
474 }
|
|
475
|
|
476 /* We need to create a new DNS request child */
|
|
477 if (query_data->resolver == NULL)
|
|
478 {
|
|
479 if (number_of_dns_children >= MAX_DNS_CHILDREN)
|
|
480 {
|
|
481 /* Apparently all our children are busy */
|
|
482 g_queue_push_head(queued_requests, query_data);
|
|
483 return;
|
|
484 }
|
|
485
|
|
486 resolver = gaim_dnsquery_resolver_new(gaim_debug_is_enabled());
|
|
487 if (resolver == NULL)
|
|
488 {
|
|
489 gaim_dnsquery_failed(query_data, _("Unable to create new resolver process\n"));
|
|
490 return;
|
|
491 }
|
|
492 if (!send_dns_request_to_child(query_data, resolver))
|
|
493 {
|
|
494 gaim_dnsquery_failed(query_data, _("Unable to send request to resolver process\n"));
|
|
495 return;
|
|
496 }
|
|
497 }
|
|
498
|
|
499 query_data->resolver->inpa = gaim_input_add(query_data->resolver->fd_out,
|
|
500 GAIM_INPUT_READ, host_resolved, query_data);
|
|
501 }
|
|
502
|
|
503 /*
|
|
504 * End the functions for dealing with the DNS child processes.
|
|
505 */
|
|
506
|
|
507 static void
|
|
508 host_resolved(gpointer data, gint source, GaimInputCondition cond)
|
|
509 {
|
|
510 GaimDnsQueryData *query_data;
|
|
511 int rc, err;
|
|
512 GSList *hosts = NULL;
|
|
513 struct sockaddr *addr = NULL;
|
|
514 size_t addrlen;
|
|
515 char message[1024];
|
|
516
|
|
517 query_data = data;
|
|
518
|
|
519 gaim_debug_info("dns", "Got response for '%s'\n", query_data->hostname);
|
|
520 gaim_input_remove(query_data->resolver->inpa);
|
|
521 query_data->resolver->inpa = 0;
|
|
522
|
|
523 rc = read(query_data->resolver->fd_out, &err, sizeof(err));
|
|
524 if ((rc == 4) && (err != 0))
|
|
525 {
|
|
526 #ifdef HAVE_GETADDRINFO
|
14392
|
527 g_snprintf(message, sizeof(message), _("Error resolving %s:\n%s"),
|
14287
|
528 query_data->hostname, gai_strerror(err));
|
|
529 #else
|
|
530 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
|
|
531 query_data->hostname, err);
|
|
532 #endif
|
|
533 gaim_dnsquery_failed(query_data, message);
|
|
534
|
|
535 } else if (rc > 0) {
|
|
536 /* Success! */
|
|
537 while (rc > 0) {
|
|
538 rc = read(query_data->resolver->fd_out, &addrlen, sizeof(addrlen));
|
|
539 if (rc > 0 && addrlen > 0) {
|
|
540 addr = g_malloc(addrlen);
|
|
541 rc = read(query_data->resolver->fd_out, addr, addrlen);
|
|
542 hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen));
|
|
543 hosts = g_slist_append(hosts, addr);
|
|
544 } else {
|
|
545 break;
|
|
546 }
|
|
547 }
|
|
548 /* wait4(resolver->dns_pid, NULL, WNOHANG, NULL); */
|
|
549 gaim_dnsquery_resolved(query_data, hosts);
|
|
550
|
|
551 } else if (rc == -1) {
|
14392
|
552 g_snprintf(message, sizeof(message), _("Error reading from resolver process:\n%s"), strerror(errno));
|
14287
|
553 gaim_dnsquery_failed(query_data, message);
|
|
554
|
|
555 } else if (rc == 0) {
|
|
556 g_snprintf(message, sizeof(message), _("EOF while reading from resolver process"));
|
|
557 gaim_dnsquery_failed(query_data, message);
|
|
558 }
|
|
559
|
|
560 handle_next_queued_request();
|
|
561 }
|
|
562
|
|
563 static gboolean
|
|
564 resolve_host(gpointer data)
|
|
565 {
|
|
566 GaimDnsQueryData *query_data;
|
|
567
|
|
568 query_data = data;
|
|
569 query_data->timeout = 0;
|
|
570
|
|
571 handle_next_queued_request();
|
|
572
|
|
573 return FALSE;
|
|
574 }
|
|
575
|
|
576 GaimDnsQueryData *
|
|
577 gaim_dnsquery_a(const char *hostname, int port,
|
|
578 GaimDnsQueryConnectFunction callback, gpointer data)
|
|
579 {
|
|
580 GaimDnsQueryData *query_data;
|
|
581
|
|
582 g_return_val_if_fail(hostname != NULL, NULL);
|
|
583 g_return_val_if_fail(port != 0, NULL);
|
14358
|
584 g_return_val_if_fail(callback != NULL, NULL);
|
14287
|
585
|
|
586 query_data = g_new(GaimDnsQueryData, 1);
|
|
587 query_data->hostname = g_strdup(hostname);
|
|
588 g_strstrip(query_data->hostname);
|
|
589 query_data->port = port;
|
|
590 query_data->callback = callback;
|
|
591 query_data->data = data;
|
|
592 query_data->resolver = NULL;
|
|
593
|
14466
|
594 if (strlen(query_data->hostname) == 0)
|
|
595 {
|
|
596 gaim_dnsquery_destroy(query_data);
|
|
597 g_return_val_if_reached(NULL);
|
|
598 }
|
|
599
|
14287
|
600 if (!queued_requests)
|
|
601 queued_requests = g_queue_new();
|
|
602 g_queue_push_tail(queued_requests, query_data);
|
|
603
|
|
604 gaim_debug_info("dns", "DNS query for '%s' queued\n", query_data->hostname);
|
|
605
|
|
606 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
607
|
|
608 return query_data;
|
|
609 }
|
|
610
|
|
611 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
|
612
|
|
613 /*
|
|
614 * Windows!
|
|
615 */
|
|
616
|
14238
|
617 static gboolean
|
|
618 dns_main_thread_cb(gpointer data)
|
|
619 {
|
|
620 GaimDnsQueryData *query_data;
|
|
621
|
|
622 query_data = data;
|
14192
|
623
|
14238
|
624 if (query_data->error_message != NULL)
|
|
625 gaim_dnsquery_failed(query_data, query_data->error_message);
|
|
626 else
|
|
627 {
|
|
628 GSList *hosts;
|
14244
|
629
|
14287
|
630 /* We don't want gaim_dns_query_resolved() to free(hosts) */
|
14238
|
631 hosts = query_data->hosts;
|
|
632 query_data->hosts = NULL;
|
|
633 gaim_dnsquery_resolved(query_data, hosts);
|
14192
|
634 }
|
14238
|
635
|
14192
|
636 return FALSE;
|
|
637 }
|
|
638
|
14238
|
639 static gpointer
|
|
640 dns_thread(gpointer data)
|
|
641 {
|
|
642 GaimDnsQueryData *query_data;
|
14287
|
643 #ifdef HAVE_GETADDRINFO
|
14192
|
644 int rc;
|
|
645 struct addrinfo hints, *res, *tmp;
|
|
646 char servname[20];
|
14287
|
647 #else
|
|
648 struct sockaddr_in sin;
|
|
649 struct hostent *hp;
|
|
650 #endif
|
14238
|
651
|
|
652 query_data = data;
|
14192
|
653
|
14287
|
654 #ifdef HAVE_GETADDRINFO
|
14238
|
655 g_snprintf(servname, sizeof(servname), "%d", query_data->port);
|
14192
|
656 memset(&hints,0,sizeof(hints));
|
|
657
|
14238
|
658 /*
|
|
659 * This is only used to convert a service
|
14192
|
660 * name to a port number. As we know we are
|
|
661 * passing a number already, we know this
|
|
662 * value will not be really used by the C
|
|
663 * library.
|
|
664 */
|
|
665 hints.ai_socktype = SOCK_STREAM;
|
14238
|
666 if ((rc = getaddrinfo(query_data->hostname, servname, &hints, &res)) == 0) {
|
14192
|
667 tmp = res;
|
|
668 while(res) {
|
14238
|
669 query_data->hosts = g_slist_append(query_data->hosts,
|
14192
|
670 GSIZE_TO_POINTER(res->ai_addrlen));
|
14238
|
671 query_data->hosts = g_slist_append(query_data->hosts,
|
14192
|
672 g_memdup(res->ai_addr, res->ai_addrlen));
|
|
673 res = res->ai_next;
|
|
674 }
|
|
675 freeaddrinfo(tmp);
|
|
676 } else {
|
14392
|
677 query_data->error_message = g_strdup_printf(_("Error resolving %s:\n%s"), query_data->hostname, gai_strerror(rc));
|
14192
|
678 }
|
14287
|
679 #else
|
|
680 if ((hp = gethostbyname(query_data->hostname))) {
|
|
681 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
682 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
683 sin.sin_family = hp->h_addrtype;
|
|
684 sin.sin_port = htons(query_data->port);
|
14192
|
685
|
14287
|
686 query_data->hosts = g_slist_append(query_data->hosts,
|
|
687 GSIZE_TO_POINTER(sizeof(sin)));
|
|
688 query_data->hosts = g_slist_append(query_data->hosts,
|
|
689 g_memdup(&sin, sizeof(sin)));
|
|
690 } else {
|
|
691 query_data->error_message = g_strdup_printf(_("Error resolving %s: %d"), query_data->hostname, h_errno);
|
|
692 }
|
|
693 #endif
|
|
694
|
|
695 /* back to main thread */
|
14238
|
696 g_idle_add(dns_main_thread_cb, query_data);
|
|
697
|
14192
|
698 return 0;
|
|
699 }
|
|
700
|
14238
|
701 static gboolean
|
|
702 resolve_host(gpointer data)
|
14192
|
703 {
|
14238
|
704 GaimDnsQueryData *query_data;
|
14192
|
705 struct sockaddr_in sin;
|
14238
|
706 GError *err = NULL;
|
14192
|
707
|
14238
|
708 query_data = data;
|
|
709 query_data->timeout = 0;
|
|
710
|
|
711 if (inet_aton(query_data->hostname, &sin.sin_addr))
|
|
712 {
|
14244
|
713 /*
|
|
714 * The given "hostname" is actually an IP address, so we
|
|
715 * don't need to do anything.
|
|
716 */
|
14192
|
717 GSList *hosts = NULL;
|
|
718 sin.sin_family = AF_INET;
|
14238
|
719 sin.sin_port = htons(query_data->port);
|
14192
|
720 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
|
|
721 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
|
14238
|
722 gaim_dnsquery_resolved(query_data, hosts);
|
|
723 }
|
|
724 else
|
|
725 {
|
14244
|
726 /*
|
|
727 * Spin off a separate thread to perform the DNS lookup so
|
|
728 * that we don't block the UI.
|
|
729 */
|
14238
|
730 query_data->resolver = g_thread_create(dns_thread,
|
|
731 query_data, FALSE, &err);
|
|
732 if (query_data->resolver == NULL)
|
|
733 {
|
|
734 char message[1024];
|
|
735 g_snprintf(message, sizeof(message), _("Thread creation failure: %s"),
|
|
736 err ? err->message : _("Unknown reason"));
|
|
737 g_error_free(err);
|
|
738 gaim_dnsquery_failed(query_data, message);
|
|
739 }
|
14192
|
740 }
|
|
741
|
|
742 return FALSE;
|
|
743 }
|
|
744
|
|
745 GaimDnsQueryData *
|
|
746 gaim_dnsquery_a(const char *hostname, int port,
|
14238
|
747 GaimDnsQueryConnectFunction callback, gpointer data)
|
14192
|
748 {
|
14238
|
749 GaimDnsQueryData *query_data;
|
|
750
|
|
751 g_return_val_if_fail(hostname != NULL, NULL);
|
|
752 g_return_val_if_fail(port != 0, NULL);
|
14358
|
753 g_return_val_if_fail(callback != NULL, NULL);
|
14238
|
754
|
14244
|
755 gaim_debug_info("dnsquery", "Performing DNS lookup for %s\n", hostname);
|
|
756
|
14238
|
757 query_data = g_new(GaimDnsQueryData, 1);
|
|
758 query_data->hostname = g_strdup(hostname);
|
|
759 g_strstrip(query_data->hostname);
|
|
760 query_data->port = port;
|
|
761 query_data->callback = callback;
|
|
762 query_data->data = data;
|
|
763 query_data->error_message = NULL;
|
|
764 query_data->hosts = NULL;
|
|
765
|
14466
|
766 if (strlen(query_data->hostname) == 0)
|
|
767 {
|
|
768 gaim_dnsquery_destroy(query_data);
|
|
769 g_return_val_if_reached(NULL);
|
|
770 }
|
|
771
|
14238
|
772 /* Don't call the callback before returning */
|
|
773 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
774
|
|
775 return query_data;
|
|
776 }
|
|
777
|
14287
|
778 #else /* not __unix__ or __APPLE__ or _WIN32 */
|
|
779
|
|
780 /*
|
|
781 * We weren't able to do anything fancier above, so use the
|
|
782 * fail-safe name resolution code, which is blocking.
|
|
783 */
|
|
784
|
|
785 static gboolean
|
|
786 resolve_host(gpointer data)
|
|
787 {
|
|
788 GaimDnsQueryData *query_data;
|
|
789 struct sockaddr_in sin;
|
|
790 GSList *hosts = NULL;
|
|
791
|
|
792 query_data = data;
|
|
793 query_data->timeout = 0;
|
|
794
|
|
795 if (!inet_aton(query_data->hostname, &sin.sin_addr)) {
|
|
796 struct hostent *hp;
|
|
797 if(!(hp = gethostbyname(query_data->hostname))) {
|
|
798 char message[1024];
|
|
799 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
|
|
800 query_data->hostname, h_errno);
|
|
801 gaim_dnsquery_failed(query_data, message);
|
|
802 return FALSE;
|
|
803 }
|
|
804 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
805 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
806 sin.sin_family = hp->h_addrtype;
|
|
807 } else
|
|
808 sin.sin_family = AF_INET;
|
|
809 sin.sin_port = htons(query_data->port);
|
|
810
|
|
811 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
|
|
812 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
|
|
813
|
|
814 gaim_dnsquery_resolved(query_data, hosts);
|
|
815
|
|
816 return FALSE;
|
|
817 }
|
|
818
|
|
819 GaimDnsQueryData *
|
|
820 gaim_dnsquery_a(const char *hostname, int port,
|
|
821 GaimDnsQueryConnectFunction callback, gpointer data)
|
|
822 {
|
|
823 GaimDnsQueryData *query_data;
|
|
824
|
|
825 g_return_val_if_fail(hostname != NULL, NULL);
|
|
826 g_return_val_if_fail(port != 0, NULL);
|
14466
|
827 g_return_val_if_fail(callback != NULL, NULL);
|
14287
|
828
|
|
829 query_data = g_new(GaimDnsQueryData, 1);
|
|
830 query_data->hostname = g_strdup(hostname);
|
|
831 g_strstrip(query_data->hostname);
|
|
832 query_data->port = port;
|
|
833 query_data->callback = callback;
|
|
834 query_data->data = data;
|
|
835
|
14466
|
836 if (strlen(query_data->hostname) == 0)
|
|
837 {
|
|
838 gaim_dnsquery_destroy(query_data);
|
|
839 g_return_val_if_reached(NULL);
|
|
840 }
|
|
841
|
14287
|
842 /* Don't call the callback before returning */
|
|
843 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
844
|
|
845 return query_data;
|
|
846 }
|
|
847
|
|
848 #endif /* not __unix__ or __APPLE__ or _WIN32 */
|
|
849
|
14238
|
850 void
|
|
851 gaim_dnsquery_destroy(GaimDnsQueryData *query_data)
|
|
852 {
|
14287
|
853 #if defined(__unix__) || defined(__APPLE__)
|
14510
|
854 g_queue_remove(queued_requests, query_data);
|
|
855
|
14287
|
856 if (query_data->resolver != NULL)
|
|
857 /*
|
|
858 * Ideally we would tell our resolver child to stop resolving
|
|
859 * shit and then we would add it back to the free_dns_children
|
|
860 * linked list. However, it's hard to tell children stuff,
|
|
861 * they just don't listen.
|
|
862 */
|
|
863 gaim_dnsquery_resolver_destroy(query_data->resolver);
|
|
864 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
14238
|
865 if (query_data->resolver != NULL)
|
|
866 {
|
|
867 /*
|
|
868 * It's not really possible to kill a thread. So instead we
|
|
869 * just set the callback to NULL and let the DNS lookup
|
|
870 * finish.
|
|
871 */
|
|
872 query_data->callback = NULL;
|
|
873 return;
|
|
874 }
|
|
875
|
|
876 while (query_data->hosts != NULL)
|
|
877 {
|
|
878 /* Discard the length... */
|
|
879 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
|
|
880 /* Free the address... */
|
|
881 g_free(query_data->hosts->data);
|
|
882 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
|
|
883 }
|
|
884 g_free(query_data->error_message);
|
14287
|
885 #endif
|
14238
|
886
|
|
887 if (query_data->timeout > 0)
|
|
888 gaim_timeout_remove(query_data->timeout);
|
|
889
|
|
890 g_free(query_data->hostname);
|
|
891 g_free(query_data);
|
|
892 }
|
|
893
|
|
894 void
|
|
895 gaim_dnsquery_init(void)
|
|
896 {
|
14287
|
897 #ifdef _WIN32
|
14238
|
898 if (!g_thread_supported())
|
|
899 g_thread_init(NULL);
|
14287
|
900 #endif
|
14238
|
901 }
|
|
902
|
|
903 void
|
|
904 gaim_dnsquery_uninit(void)
|
|
905 {
|
14287
|
906 #if defined(__unix__) || defined(__APPLE__)
|
|
907 while (free_dns_children != NULL)
|
|
908 {
|
|
909 gaim_dnsquery_resolver_destroy(free_dns_children->data);
|
|
910 free_dns_children = g_slist_remove(free_dns_children, free_dns_children->data);
|
|
911 }
|
|
912 #endif
|
14238
|
913 }
|