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;
|
14649
|
70 static GSList *queued_requests = NULL;
|
14287
|
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
|
14649
|
454 if (queued_requests == NULL)
|
14287
|
455 /* No more DNS queries, yay! */
|
|
456 return;
|
|
457
|
14649
|
458 query_data = queued_requests->data;
|
|
459 queued_requests = g_slist_delete_link(queued_requests, queued_requests);
|
14287
|
460
|
|
461 /*
|
|
462 * If we have any children, attempt to have them perform the DNS
|
|
463 * query. If we're able to send the query then resolver will be
|
|
464 * set to the GaimDnsQueryResolverProcess. Otherwise, resolver
|
|
465 * will be NULL and we'll need to create a new DNS request child.
|
|
466 */
|
|
467 while (free_dns_children != NULL)
|
|
468 {
|
|
469 resolver = free_dns_children->data;
|
|
470 free_dns_children = g_slist_remove(free_dns_children, resolver);
|
|
471
|
|
472 if (send_dns_request_to_child(query_data, resolver))
|
|
473 /* We found an acceptable child, yay */
|
|
474 break;
|
|
475 }
|
|
476
|
|
477 /* We need to create a new DNS request child */
|
|
478 if (query_data->resolver == NULL)
|
|
479 {
|
|
480 if (number_of_dns_children >= MAX_DNS_CHILDREN)
|
|
481 {
|
|
482 /* Apparently all our children are busy */
|
14649
|
483 queued_requests = g_slist_prepend(queued_requests, query_data);
|
14287
|
484 return;
|
|
485 }
|
|
486
|
|
487 resolver = gaim_dnsquery_resolver_new(gaim_debug_is_enabled());
|
|
488 if (resolver == NULL)
|
|
489 {
|
|
490 gaim_dnsquery_failed(query_data, _("Unable to create new resolver process\n"));
|
|
491 return;
|
|
492 }
|
|
493 if (!send_dns_request_to_child(query_data, resolver))
|
|
494 {
|
|
495 gaim_dnsquery_failed(query_data, _("Unable to send request to resolver process\n"));
|
|
496 return;
|
|
497 }
|
|
498 }
|
|
499
|
|
500 query_data->resolver->inpa = gaim_input_add(query_data->resolver->fd_out,
|
|
501 GAIM_INPUT_READ, host_resolved, query_data);
|
|
502 }
|
|
503
|
|
504 /*
|
|
505 * End the functions for dealing with the DNS child processes.
|
|
506 */
|
|
507
|
|
508 static void
|
|
509 host_resolved(gpointer data, gint source, GaimInputCondition cond)
|
|
510 {
|
|
511 GaimDnsQueryData *query_data;
|
|
512 int rc, err;
|
|
513 GSList *hosts = NULL;
|
|
514 struct sockaddr *addr = NULL;
|
|
515 size_t addrlen;
|
|
516 char message[1024];
|
|
517
|
|
518 query_data = data;
|
|
519
|
|
520 gaim_debug_info("dns", "Got response for '%s'\n", query_data->hostname);
|
|
521 gaim_input_remove(query_data->resolver->inpa);
|
|
522 query_data->resolver->inpa = 0;
|
|
523
|
|
524 rc = read(query_data->resolver->fd_out, &err, sizeof(err));
|
|
525 if ((rc == 4) && (err != 0))
|
|
526 {
|
|
527 #ifdef HAVE_GETADDRINFO
|
14392
|
528 g_snprintf(message, sizeof(message), _("Error resolving %s:\n%s"),
|
14287
|
529 query_data->hostname, gai_strerror(err));
|
|
530 #else
|
|
531 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
|
|
532 query_data->hostname, err);
|
|
533 #endif
|
|
534 gaim_dnsquery_failed(query_data, message);
|
|
535
|
|
536 } else if (rc > 0) {
|
|
537 /* Success! */
|
|
538 while (rc > 0) {
|
|
539 rc = read(query_data->resolver->fd_out, &addrlen, sizeof(addrlen));
|
|
540 if (rc > 0 && addrlen > 0) {
|
|
541 addr = g_malloc(addrlen);
|
|
542 rc = read(query_data->resolver->fd_out, addr, addrlen);
|
|
543 hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen));
|
|
544 hosts = g_slist_append(hosts, addr);
|
|
545 } else {
|
|
546 break;
|
|
547 }
|
|
548 }
|
|
549 /* wait4(resolver->dns_pid, NULL, WNOHANG, NULL); */
|
|
550 gaim_dnsquery_resolved(query_data, hosts);
|
|
551
|
|
552 } else if (rc == -1) {
|
14392
|
553 g_snprintf(message, sizeof(message), _("Error reading from resolver process:\n%s"), strerror(errno));
|
14287
|
554 gaim_dnsquery_failed(query_data, message);
|
|
555
|
|
556 } else if (rc == 0) {
|
|
557 g_snprintf(message, sizeof(message), _("EOF while reading from resolver process"));
|
|
558 gaim_dnsquery_failed(query_data, message);
|
|
559 }
|
|
560
|
|
561 handle_next_queued_request();
|
|
562 }
|
|
563
|
|
564 static gboolean
|
|
565 resolve_host(gpointer data)
|
|
566 {
|
|
567 GaimDnsQueryData *query_data;
|
|
568
|
|
569 query_data = data;
|
|
570 query_data->timeout = 0;
|
|
571
|
|
572 handle_next_queued_request();
|
|
573
|
|
574 return FALSE;
|
|
575 }
|
|
576
|
|
577 GaimDnsQueryData *
|
|
578 gaim_dnsquery_a(const char *hostname, int port,
|
|
579 GaimDnsQueryConnectFunction callback, gpointer data)
|
|
580 {
|
|
581 GaimDnsQueryData *query_data;
|
|
582
|
|
583 g_return_val_if_fail(hostname != NULL, NULL);
|
|
584 g_return_val_if_fail(port != 0, NULL);
|
14358
|
585 g_return_val_if_fail(callback != NULL, NULL);
|
14287
|
586
|
|
587 query_data = g_new(GaimDnsQueryData, 1);
|
|
588 query_data->hostname = g_strdup(hostname);
|
|
589 g_strstrip(query_data->hostname);
|
|
590 query_data->port = port;
|
|
591 query_data->callback = callback;
|
|
592 query_data->data = data;
|
|
593 query_data->resolver = NULL;
|
|
594
|
14466
|
595 if (strlen(query_data->hostname) == 0)
|
|
596 {
|
|
597 gaim_dnsquery_destroy(query_data);
|
|
598 g_return_val_if_reached(NULL);
|
|
599 }
|
|
600
|
14649
|
601 queued_requests = g_slist_append(queued_requests, query_data);
|
14287
|
602
|
|
603 gaim_debug_info("dns", "DNS query for '%s' queued\n", query_data->hostname);
|
|
604
|
|
605 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
606
|
|
607 return query_data;
|
|
608 }
|
|
609
|
|
610 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
|
611
|
|
612 /*
|
|
613 * Windows!
|
|
614 */
|
|
615
|
14238
|
616 static gboolean
|
|
617 dns_main_thread_cb(gpointer data)
|
|
618 {
|
|
619 GaimDnsQueryData *query_data;
|
|
620
|
|
621 query_data = data;
|
14192
|
622
|
14238
|
623 if (query_data->error_message != NULL)
|
|
624 gaim_dnsquery_failed(query_data, query_data->error_message);
|
|
625 else
|
|
626 {
|
|
627 GSList *hosts;
|
14244
|
628
|
14287
|
629 /* We don't want gaim_dns_query_resolved() to free(hosts) */
|
14238
|
630 hosts = query_data->hosts;
|
|
631 query_data->hosts = NULL;
|
|
632 gaim_dnsquery_resolved(query_data, hosts);
|
14192
|
633 }
|
14238
|
634
|
14192
|
635 return FALSE;
|
|
636 }
|
|
637
|
14238
|
638 static gpointer
|
|
639 dns_thread(gpointer data)
|
|
640 {
|
|
641 GaimDnsQueryData *query_data;
|
14287
|
642 #ifdef HAVE_GETADDRINFO
|
14192
|
643 int rc;
|
|
644 struct addrinfo hints, *res, *tmp;
|
|
645 char servname[20];
|
14287
|
646 #else
|
|
647 struct sockaddr_in sin;
|
|
648 struct hostent *hp;
|
|
649 #endif
|
14238
|
650
|
|
651 query_data = data;
|
14192
|
652
|
14287
|
653 #ifdef HAVE_GETADDRINFO
|
14238
|
654 g_snprintf(servname, sizeof(servname), "%d", query_data->port);
|
14192
|
655 memset(&hints,0,sizeof(hints));
|
|
656
|
14238
|
657 /*
|
|
658 * This is only used to convert a service
|
14192
|
659 * name to a port number. As we know we are
|
|
660 * passing a number already, we know this
|
|
661 * value will not be really used by the C
|
|
662 * library.
|
|
663 */
|
|
664 hints.ai_socktype = SOCK_STREAM;
|
14238
|
665 if ((rc = getaddrinfo(query_data->hostname, servname, &hints, &res)) == 0) {
|
14192
|
666 tmp = res;
|
|
667 while(res) {
|
14238
|
668 query_data->hosts = g_slist_append(query_data->hosts,
|
14192
|
669 GSIZE_TO_POINTER(res->ai_addrlen));
|
14238
|
670 query_data->hosts = g_slist_append(query_data->hosts,
|
14192
|
671 g_memdup(res->ai_addr, res->ai_addrlen));
|
|
672 res = res->ai_next;
|
|
673 }
|
|
674 freeaddrinfo(tmp);
|
|
675 } else {
|
14392
|
676 query_data->error_message = g_strdup_printf(_("Error resolving %s:\n%s"), query_data->hostname, gai_strerror(rc));
|
14192
|
677 }
|
14287
|
678 #else
|
|
679 if ((hp = gethostbyname(query_data->hostname))) {
|
|
680 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
681 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
682 sin.sin_family = hp->h_addrtype;
|
|
683 sin.sin_port = htons(query_data->port);
|
14192
|
684
|
14287
|
685 query_data->hosts = g_slist_append(query_data->hosts,
|
|
686 GSIZE_TO_POINTER(sizeof(sin)));
|
|
687 query_data->hosts = g_slist_append(query_data->hosts,
|
|
688 g_memdup(&sin, sizeof(sin)));
|
|
689 } else {
|
|
690 query_data->error_message = g_strdup_printf(_("Error resolving %s: %d"), query_data->hostname, h_errno);
|
|
691 }
|
|
692 #endif
|
|
693
|
|
694 /* back to main thread */
|
14238
|
695 g_idle_add(dns_main_thread_cb, query_data);
|
|
696
|
14192
|
697 return 0;
|
|
698 }
|
|
699
|
14238
|
700 static gboolean
|
|
701 resolve_host(gpointer data)
|
14192
|
702 {
|
14238
|
703 GaimDnsQueryData *query_data;
|
14192
|
704 struct sockaddr_in sin;
|
14238
|
705 GError *err = NULL;
|
14192
|
706
|
14238
|
707 query_data = data;
|
|
708 query_data->timeout = 0;
|
|
709
|
|
710 if (inet_aton(query_data->hostname, &sin.sin_addr))
|
|
711 {
|
14244
|
712 /*
|
|
713 * The given "hostname" is actually an IP address, so we
|
|
714 * don't need to do anything.
|
|
715 */
|
14192
|
716 GSList *hosts = NULL;
|
|
717 sin.sin_family = AF_INET;
|
14238
|
718 sin.sin_port = htons(query_data->port);
|
14192
|
719 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
|
|
720 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
|
14238
|
721 gaim_dnsquery_resolved(query_data, hosts);
|
|
722 }
|
|
723 else
|
|
724 {
|
14244
|
725 /*
|
|
726 * Spin off a separate thread to perform the DNS lookup so
|
|
727 * that we don't block the UI.
|
|
728 */
|
14238
|
729 query_data->resolver = g_thread_create(dns_thread,
|
|
730 query_data, FALSE, &err);
|
|
731 if (query_data->resolver == NULL)
|
|
732 {
|
|
733 char message[1024];
|
|
734 g_snprintf(message, sizeof(message), _("Thread creation failure: %s"),
|
|
735 err ? err->message : _("Unknown reason"));
|
|
736 g_error_free(err);
|
|
737 gaim_dnsquery_failed(query_data, message);
|
|
738 }
|
14192
|
739 }
|
|
740
|
|
741 return FALSE;
|
|
742 }
|
|
743
|
|
744 GaimDnsQueryData *
|
|
745 gaim_dnsquery_a(const char *hostname, int port,
|
14238
|
746 GaimDnsQueryConnectFunction callback, gpointer data)
|
14192
|
747 {
|
14238
|
748 GaimDnsQueryData *query_data;
|
|
749
|
|
750 g_return_val_if_fail(hostname != NULL, NULL);
|
|
751 g_return_val_if_fail(port != 0, NULL);
|
14358
|
752 g_return_val_if_fail(callback != NULL, NULL);
|
14238
|
753
|
14244
|
754 gaim_debug_info("dnsquery", "Performing DNS lookup for %s\n", hostname);
|
|
755
|
14238
|
756 query_data = g_new(GaimDnsQueryData, 1);
|
|
757 query_data->hostname = g_strdup(hostname);
|
|
758 g_strstrip(query_data->hostname);
|
|
759 query_data->port = port;
|
|
760 query_data->callback = callback;
|
|
761 query_data->data = data;
|
|
762 query_data->error_message = NULL;
|
|
763 query_data->hosts = NULL;
|
|
764
|
14466
|
765 if (strlen(query_data->hostname) == 0)
|
|
766 {
|
|
767 gaim_dnsquery_destroy(query_data);
|
|
768 g_return_val_if_reached(NULL);
|
|
769 }
|
|
770
|
14238
|
771 /* Don't call the callback before returning */
|
|
772 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
773
|
|
774 return query_data;
|
|
775 }
|
|
776
|
14287
|
777 #else /* not __unix__ or __APPLE__ or _WIN32 */
|
|
778
|
|
779 /*
|
|
780 * We weren't able to do anything fancier above, so use the
|
|
781 * fail-safe name resolution code, which is blocking.
|
|
782 */
|
|
783
|
|
784 static gboolean
|
|
785 resolve_host(gpointer data)
|
|
786 {
|
|
787 GaimDnsQueryData *query_data;
|
|
788 struct sockaddr_in sin;
|
|
789 GSList *hosts = NULL;
|
|
790
|
|
791 query_data = data;
|
|
792 query_data->timeout = 0;
|
|
793
|
|
794 if (!inet_aton(query_data->hostname, &sin.sin_addr)) {
|
|
795 struct hostent *hp;
|
|
796 if(!(hp = gethostbyname(query_data->hostname))) {
|
|
797 char message[1024];
|
|
798 g_snprintf(message, sizeof(message), _("Error resolving %s: %d"),
|
|
799 query_data->hostname, h_errno);
|
|
800 gaim_dnsquery_failed(query_data, message);
|
|
801 return FALSE;
|
|
802 }
|
|
803 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
804 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
805 sin.sin_family = hp->h_addrtype;
|
|
806 } else
|
|
807 sin.sin_family = AF_INET;
|
|
808 sin.sin_port = htons(query_data->port);
|
|
809
|
|
810 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
|
|
811 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
|
|
812
|
|
813 gaim_dnsquery_resolved(query_data, hosts);
|
|
814
|
|
815 return FALSE;
|
|
816 }
|
|
817
|
|
818 GaimDnsQueryData *
|
|
819 gaim_dnsquery_a(const char *hostname, int port,
|
|
820 GaimDnsQueryConnectFunction callback, gpointer data)
|
|
821 {
|
|
822 GaimDnsQueryData *query_data;
|
|
823
|
|
824 g_return_val_if_fail(hostname != NULL, NULL);
|
|
825 g_return_val_if_fail(port != 0, NULL);
|
14466
|
826 g_return_val_if_fail(callback != NULL, NULL);
|
14287
|
827
|
|
828 query_data = g_new(GaimDnsQueryData, 1);
|
|
829 query_data->hostname = g_strdup(hostname);
|
|
830 g_strstrip(query_data->hostname);
|
|
831 query_data->port = port;
|
|
832 query_data->callback = callback;
|
|
833 query_data->data = data;
|
|
834
|
14466
|
835 if (strlen(query_data->hostname) == 0)
|
|
836 {
|
|
837 gaim_dnsquery_destroy(query_data);
|
|
838 g_return_val_if_reached(NULL);
|
|
839 }
|
|
840
|
14287
|
841 /* Don't call the callback before returning */
|
|
842 query_data->timeout = gaim_timeout_add(0, resolve_host, query_data);
|
|
843
|
|
844 return query_data;
|
|
845 }
|
|
846
|
|
847 #endif /* not __unix__ or __APPLE__ or _WIN32 */
|
|
848
|
14238
|
849 void
|
|
850 gaim_dnsquery_destroy(GaimDnsQueryData *query_data)
|
|
851 {
|
14287
|
852 #if defined(__unix__) || defined(__APPLE__)
|
14649
|
853 queued_requests = g_slist_remove(queued_requests, query_data);
|
14510
|
854
|
14287
|
855 if (query_data->resolver != NULL)
|
|
856 /*
|
|
857 * Ideally we would tell our resolver child to stop resolving
|
|
858 * shit and then we would add it back to the free_dns_children
|
|
859 * linked list. However, it's hard to tell children stuff,
|
|
860 * they just don't listen.
|
|
861 */
|
|
862 gaim_dnsquery_resolver_destroy(query_data->resolver);
|
|
863 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
14238
|
864 if (query_data->resolver != NULL)
|
|
865 {
|
|
866 /*
|
|
867 * It's not really possible to kill a thread. So instead we
|
|
868 * just set the callback to NULL and let the DNS lookup
|
|
869 * finish.
|
|
870 */
|
|
871 query_data->callback = NULL;
|
|
872 return;
|
|
873 }
|
|
874
|
|
875 while (query_data->hosts != NULL)
|
|
876 {
|
|
877 /* Discard the length... */
|
|
878 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
|
|
879 /* Free the address... */
|
|
880 g_free(query_data->hosts->data);
|
|
881 query_data->hosts = g_slist_remove(query_data->hosts, query_data->hosts->data);
|
|
882 }
|
|
883 g_free(query_data->error_message);
|
14287
|
884 #endif
|
14238
|
885
|
|
886 if (query_data->timeout > 0)
|
|
887 gaim_timeout_remove(query_data->timeout);
|
|
888
|
|
889 g_free(query_data->hostname);
|
|
890 g_free(query_data);
|
|
891 }
|
|
892
|
|
893 void
|
|
894 gaim_dnsquery_init(void)
|
|
895 {
|
|
896 }
|
|
897
|
|
898 void
|
|
899 gaim_dnsquery_uninit(void)
|
|
900 {
|
14287
|
901 #if defined(__unix__) || defined(__APPLE__)
|
|
902 while (free_dns_children != NULL)
|
|
903 {
|
|
904 gaim_dnsquery_resolver_destroy(free_dns_children->data);
|
|
905 free_dns_children = g_slist_remove(free_dns_children, free_dns_children->data);
|
|
906 }
|
|
907 #endif
|
14238
|
908 }
|