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
|
|
527 g_snprintf(message, sizeof(message), _("Error resolving %s: %s"),
|
|
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) {
|
|
552 g_snprintf(message, sizeof(message), _("Error reading from resolver process: %s"), strerror(errno));
|
|
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
|
|
594 if (!queued_requests)
|
|
595 queued_requests = g_queue_new();
|
|
596 g_queue_push_tail(queued_requests, query_data);
|
|
597
|
|
598 gaim_debug_info("dns", "DNS query for '%s' queued\n", query_data->hostname);
|
|
599
|
|
600 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
601
|
|
602 return query_data;
|
|
603 }
|
|
604
|
|
605 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
|
606
|
|
607 /*
|
|
608 * Windows!
|
|
609 */
|
|
610
|
14238
|
611 static gboolean
|
|
612 dns_main_thread_cb(gpointer data)
|
|
613 {
|
|
614 GaimDnsQueryData *query_data;
|
|
615
|
|
616 query_data = data;
|
14192
|
617
|
14238
|
618 if (query_data->error_message != NULL)
|
|
619 gaim_dnsquery_failed(query_data, query_data->error_message);
|
|
620 else
|
|
621 {
|
|
622 GSList *hosts;
|
14244
|
623
|
14287
|
624 /* We don't want gaim_dns_query_resolved() to free(hosts) */
|
14238
|
625 hosts = query_data->hosts;
|
|
626 query_data->hosts = NULL;
|
|
627 gaim_dnsquery_resolved(query_data, hosts);
|
14192
|
628 }
|
14238
|
629
|
14192
|
630 return FALSE;
|
|
631 }
|
|
632
|
14238
|
633 static gpointer
|
|
634 dns_thread(gpointer data)
|
|
635 {
|
|
636 GaimDnsQueryData *query_data;
|
14287
|
637 #ifdef HAVE_GETADDRINFO
|
14192
|
638 int rc;
|
|
639 struct addrinfo hints, *res, *tmp;
|
|
640 char servname[20];
|
14287
|
641 #else
|
|
642 struct sockaddr_in sin;
|
|
643 struct hostent *hp;
|
|
644 #endif
|
14238
|
645
|
|
646 query_data = data;
|
14192
|
647
|
14287
|
648 #ifdef HAVE_GETADDRINFO
|
14238
|
649 g_snprintf(servname, sizeof(servname), "%d", query_data->port);
|
14192
|
650 memset(&hints,0,sizeof(hints));
|
|
651
|
14238
|
652 /*
|
|
653 * This is only used to convert a service
|
14192
|
654 * name to a port number. As we know we are
|
|
655 * passing a number already, we know this
|
|
656 * value will not be really used by the C
|
|
657 * library.
|
|
658 */
|
|
659 hints.ai_socktype = SOCK_STREAM;
|
14238
|
660 if ((rc = getaddrinfo(query_data->hostname, servname, &hints, &res)) == 0) {
|
14192
|
661 tmp = res;
|
|
662 while(res) {
|
14238
|
663 query_data->hosts = g_slist_append(query_data->hosts,
|
14192
|
664 GSIZE_TO_POINTER(res->ai_addrlen));
|
14238
|
665 query_data->hosts = g_slist_append(query_data->hosts,
|
14192
|
666 g_memdup(res->ai_addr, res->ai_addrlen));
|
|
667 res = res->ai_next;
|
|
668 }
|
|
669 freeaddrinfo(tmp);
|
|
670 } else {
|
14238
|
671 query_data->error_message = g_strdup_printf(_("Error resolving %s: %s"), query_data->hostname, gai_strerror(rc));
|
14192
|
672 }
|
14287
|
673 #else
|
|
674 if ((hp = gethostbyname(query_data->hostname))) {
|
|
675 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
676 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
677 sin.sin_family = hp->h_addrtype;
|
|
678 sin.sin_port = htons(query_data->port);
|
14192
|
679
|
14287
|
680 query_data->hosts = g_slist_append(query_data->hosts,
|
|
681 GSIZE_TO_POINTER(sizeof(sin)));
|
|
682 query_data->hosts = g_slist_append(query_data->hosts,
|
|
683 g_memdup(&sin, sizeof(sin)));
|
|
684 } else {
|
|
685 query_data->error_message = g_strdup_printf(_("Error resolving %s: %d"), query_data->hostname, h_errno);
|
|
686 }
|
|
687 #endif
|
|
688
|
|
689 /* back to main thread */
|
14238
|
690 g_idle_add(dns_main_thread_cb, query_data);
|
|
691
|
14192
|
692 return 0;
|
|
693 }
|
|
694
|
14238
|
695 static gboolean
|
|
696 resolve_host(gpointer data)
|
14192
|
697 {
|
14238
|
698 GaimDnsQueryData *query_data;
|
14192
|
699 struct sockaddr_in sin;
|
14238
|
700 GError *err = NULL;
|
14192
|
701
|
14238
|
702 query_data = data;
|
|
703 query_data->timeout = 0;
|
|
704
|
|
705 if (inet_aton(query_data->hostname, &sin.sin_addr))
|
|
706 {
|
14244
|
707 /*
|
|
708 * The given "hostname" is actually an IP address, so we
|
|
709 * don't need to do anything.
|
|
710 */
|
14192
|
711 GSList *hosts = NULL;
|
|
712 sin.sin_family = AF_INET;
|
14238
|
713 sin.sin_port = htons(query_data->port);
|
14192
|
714 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
|
|
715 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
|
14238
|
716 gaim_dnsquery_resolved(query_data, hosts);
|
|
717 }
|
|
718 else
|
|
719 {
|
14244
|
720 /*
|
|
721 * Spin off a separate thread to perform the DNS lookup so
|
|
722 * that we don't block the UI.
|
|
723 */
|
14238
|
724 query_data->resolver = g_thread_create(dns_thread,
|
|
725 query_data, FALSE, &err);
|
|
726 if (query_data->resolver == NULL)
|
|
727 {
|
|
728 char message[1024];
|
|
729 g_snprintf(message, sizeof(message), _("Thread creation failure: %s"),
|
|
730 err ? err->message : _("Unknown reason"));
|
|
731 g_error_free(err);
|
|
732 gaim_dnsquery_failed(query_data, message);
|
|
733 }
|
14192
|
734 }
|
|
735
|
|
736 return FALSE;
|
|
737 }
|
|
738
|
|
739 GaimDnsQueryData *
|
|
740 gaim_dnsquery_a(const char *hostname, int port,
|
14238
|
741 GaimDnsQueryConnectFunction callback, gpointer data)
|
14192
|
742 {
|
14238
|
743 GaimDnsQueryData *query_data;
|
|
744
|
|
745 g_return_val_if_fail(hostname != NULL, NULL);
|
|
746 g_return_val_if_fail(port != 0, NULL);
|
14358
|
747 g_return_val_if_fail(callback != NULL, NULL);
|
14238
|
748
|
14244
|
749 gaim_debug_info("dnsquery", "Performing DNS lookup for %s\n", hostname);
|
|
750
|
14238
|
751 query_data = g_new(GaimDnsQueryData, 1);
|
|
752 query_data->hostname = g_strdup(hostname);
|
|
753 g_strstrip(query_data->hostname);
|
|
754 query_data->port = port;
|
|
755 query_data->callback = callback;
|
|
756 query_data->data = data;
|
|
757 query_data->error_message = NULL;
|
|
758 query_data->hosts = NULL;
|
|
759
|
|
760 /* Don't call the callback before returning */
|
|
761 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
762
|
|
763 return query_data;
|
|
764 }
|
|
765
|
14287
|
766 #else /* not __unix__ or __APPLE__ or _WIN32 */
|
|
767
|
|
768 /*
|
|
769 * We weren't able to do anything fancier above, so use the
|
|
770 * fail-safe name resolution code, which is blocking.
|
|
771 */
|
|
772
|
|
773 static gboolean
|
|
774 resolve_host(gpointer data)
|
|
775 {
|
|
776 GaimDnsQueryData *query_data;
|
|
777 struct sockaddr_in sin;
|
|
778 GSList *hosts = NULL;
|
|
779
|
|
780 query_data = data;
|
|
781 query_data->timeout = 0;
|
|
782
|
|
783 if (!inet_aton(query_data->hostname, &sin.sin_addr)) {
|
|
784 struct hostent *hp;
|
|
785 if(!(hp = gethostbyname(query_data->hostname))) {
|
|
786 char message[1024];
|
|
787 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
|
|
788 query_data->hostname, h_errno);
|
|
789 gaim_dnsquery_failed(query_data, message);
|
|
790 return FALSE;
|
|
791 }
|
|
792 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
793 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
794 sin.sin_family = hp->h_addrtype;
|
|
795 } else
|
|
796 sin.sin_family = AF_INET;
|
|
797 sin.sin_port = htons(query_data->port);
|
|
798
|
|
799 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
|
|
800 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
|
|
801
|
|
802 gaim_dnsquery_resolved(query_data, hosts);
|
|
803
|
|
804 return FALSE;
|
|
805 }
|
|
806
|
|
807 GaimDnsQueryData *
|
|
808 gaim_dnsquery_a(const char *hostname, int port,
|
|
809 GaimDnsQueryConnectFunction callback, gpointer data)
|
|
810 {
|
|
811 GaimDnsQueryData *query_data;
|
|
812
|
|
813 g_return_val_if_fail(hostname != NULL, NULL);
|
|
814 g_return_val_if_fail(port != 0, NULL);
|
|
815
|
|
816 query_data = g_new(GaimDnsQueryData, 1);
|
|
817 query_data->hostname = g_strdup(hostname);
|
|
818 g_strstrip(query_data->hostname);
|
|
819 query_data->port = port;
|
|
820 query_data->callback = callback;
|
|
821 query_data->data = data;
|
|
822
|
|
823 /* Don't call the callback before returning */
|
|
824 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
825
|
|
826 return query_data;
|
|
827 }
|
|
828
|
|
829 #endif /* not __unix__ or __APPLE__ or _WIN32 */
|
|
830
|
14238
|
831 void
|
|
832 gaim_dnsquery_destroy(GaimDnsQueryData *query_data)
|
|
833 {
|
14287
|
834 #if defined(__unix__) || defined(__APPLE__)
|
|
835 if (query_data->resolver != NULL)
|
|
836 /*
|
|
837 * Ideally we would tell our resolver child to stop resolving
|
|
838 * shit and then we would add it back to the free_dns_children
|
|
839 * linked list. However, it's hard to tell children stuff,
|
|
840 * they just don't listen.
|
|
841 */
|
|
842 gaim_dnsquery_resolver_destroy(query_data->resolver);
|
|
843 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
14238
|
844 if (query_data->resolver != NULL)
|
|
845 {
|
|
846 /*
|
|
847 * It's not really possible to kill a thread. So instead we
|
|
848 * just set the callback to NULL and let the DNS lookup
|
|
849 * finish.
|
|
850 */
|
|
851 query_data->callback = NULL;
|
|
852 return;
|
|
853 }
|
|
854
|
|
855 while (query_data->hosts != NULL)
|
|
856 {
|
|
857 /* Discard the length... */
|
|
858 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
|
|
859 /* Free the address... */
|
|
860 g_free(query_data->hosts->data);
|
|
861 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
|
|
862 }
|
|
863 g_free(query_data->error_message);
|
14287
|
864 #endif
|
14238
|
865
|
|
866 if (query_data->timeout > 0)
|
|
867 gaim_timeout_remove(query_data->timeout);
|
|
868
|
|
869 g_free(query_data->hostname);
|
|
870 g_free(query_data);
|
|
871 }
|
|
872
|
|
873 void
|
|
874 gaim_dnsquery_init(void)
|
|
875 {
|
14287
|
876 #ifdef _WIN32
|
14238
|
877 if (!g_thread_supported())
|
|
878 g_thread_init(NULL);
|
14287
|
879 #endif
|
14238
|
880 }
|
|
881
|
|
882 void
|
|
883 gaim_dnsquery_uninit(void)
|
|
884 {
|
14287
|
885 #if defined(__unix__) || defined(__APPLE__)
|
|
886 while (free_dns_children != NULL)
|
|
887 {
|
|
888 gaim_dnsquery_resolver_destroy(free_dns_children->data);
|
|
889 free_dns_children = g_slist_remove(free_dns_children, free_dns_children->data);
|
|
890 }
|
|
891 #endif
|
14238
|
892 }
|