14181
|
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"
|
|
29 #include "notify.h"
|
|
30 #include "prefs.h"
|
|
31 #include "dnsquery.h"
|
|
32 #include "util.h"
|
|
33
|
|
34 /**************************************************************************
|
|
35 * DNS query API
|
|
36 **************************************************************************/
|
|
37
|
|
38 #if defined(__unix__) || defined(__APPLE__)
|
|
39
|
14182
|
40 #define MAX_DNS_CHILDREN 4
|
|
41
|
14181
|
42 /*
|
|
43 * This structure represents both a pending DNS request and
|
|
44 * a free child process.
|
|
45 */
|
|
46 typedef struct {
|
|
47 char *host;
|
|
48 int port;
|
|
49 GaimProxyDnsConnectFunction callback;
|
|
50 gpointer data;
|
|
51 guint inpa;
|
|
52 int fd_in, fd_out;
|
|
53 pid_t dns_pid;
|
|
54 } pending_dns_request_t;
|
|
55
|
|
56 static GSList *free_dns_children = NULL;
|
|
57 static GQueue *queued_requests = NULL;
|
|
58
|
|
59 static int number_of_dns_children = 0;
|
|
60
|
|
61 typedef struct {
|
|
62 char hostname[512];
|
|
63 int port;
|
|
64 } dns_params_t;
|
|
65
|
|
66 typedef struct {
|
|
67 dns_params_t params;
|
|
68 GaimProxyDnsConnectFunction callback;
|
|
69 gpointer data;
|
|
70 } queued_dns_request_t;
|
|
71
|
|
72 /*
|
|
73 * Begin the DNS resolver child process functions.
|
|
74 */
|
|
75 #ifdef HAVE_SIGNAL_H
|
|
76 static void
|
|
77 trap_gdb_bug()
|
|
78 {
|
|
79 const char *message =
|
|
80 "Gaim's DNS child got a SIGTRAP signal.\n"
|
|
81 "This can be caused by trying to run gaim inside gdb.\n"
|
|
82 "There is a known gdb bug which prevents this. Supposedly gaim\n"
|
|
83 "should have detected you were using gdb and used an ugly hack,\n"
|
|
84 "check cope_with_gdb_brokenness() in dnsquery.c.\n\n"
|
|
85 "For more info about this bug, see http://sources.redhat.com/ml/gdb/2001-07/msg00349.html\n";
|
|
86 fputs("\n* * *\n",stderr);
|
|
87 fputs(message,stderr);
|
|
88 fputs("* * *\n\n",stderr);
|
|
89 execlp("xmessage","xmessage","-center", message, NULL);
|
|
90 _exit(1);
|
|
91 }
|
|
92 #endif
|
|
93
|
|
94 static void
|
|
95 cope_with_gdb_brokenness()
|
|
96 {
|
|
97 #ifdef __linux__
|
|
98 static gboolean already_done = FALSE;
|
|
99 char s[256], e[512];
|
|
100 int n;
|
|
101 pid_t ppid;
|
|
102
|
|
103 if(already_done)
|
|
104 return;
|
|
105 already_done = TRUE;
|
|
106 ppid = getppid();
|
|
107 snprintf(s, sizeof(s), "/proc/%d/exe", ppid);
|
|
108 n = readlink(s, e, sizeof(e));
|
|
109 if(n < 0)
|
|
110 return;
|
|
111
|
|
112 e[MIN(n,sizeof(e)-1)] = '\0';
|
|
113
|
|
114 if(strstr(e,"gdb")) {
|
|
115 gaim_debug_info("dns",
|
|
116 "Debugger detected, performing useless query...\n");
|
|
117 gethostbyname("x.x.x.x.x");
|
|
118 }
|
|
119 #endif
|
|
120 }
|
|
121
|
|
122 static void
|
|
123 gaim_dns_resolverthread(int child_out, int child_in, gboolean show_debug)
|
|
124 {
|
|
125 dns_params_t dns_params;
|
|
126 const size_t zero = 0;
|
|
127 int rc;
|
|
128 #ifdef HAVE_GETADDRINFO
|
|
129 struct addrinfo hints, *res, *tmp;
|
|
130 char servname[20];
|
|
131 #else
|
|
132 struct sockaddr_in sin;
|
|
133 const size_t addrlen = sizeof(sin);
|
|
134 #endif
|
|
135
|
|
136 #ifdef HAVE_SIGNAL_H
|
|
137 signal(SIGHUP, SIG_DFL);
|
|
138 signal(SIGINT, SIG_DFL);
|
|
139 signal(SIGQUIT, SIG_DFL);
|
|
140 signal(SIGCHLD, SIG_DFL);
|
|
141 signal(SIGTERM, SIG_DFL);
|
|
142 signal(SIGTRAP, trap_gdb_bug);
|
|
143 #endif
|
|
144
|
|
145 /*
|
|
146 * We resolve 1 host name for each iteration of this
|
|
147 * while loop.
|
|
148 *
|
|
149 * The top half of this reads in the hostname and port
|
|
150 * number from the socket with our parent. The bottom
|
|
151 * half of this resolves the IP (blocking) and sends
|
|
152 * the result back to our parent, when finished.
|
|
153 */
|
|
154 while (1) {
|
|
155 const char ch = 'Y';
|
|
156 fd_set fds;
|
|
157 struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 };
|
|
158 FD_ZERO(&fds);
|
|
159 FD_SET(child_in, &fds);
|
|
160 rc = select(child_in + 1, &fds, NULL, NULL, &tv);
|
|
161 if (!rc) {
|
|
162 if (show_debug)
|
|
163 printf("dns[%d]: nobody needs me... =(\n", getpid());
|
|
164 break;
|
|
165 }
|
|
166 rc = read(child_in, &dns_params, sizeof(dns_params_t));
|
|
167 if (rc < 0) {
|
|
168 perror("read()");
|
|
169 break;
|
|
170 }
|
|
171 if (rc == 0) {
|
|
172 if (show_debug)
|
|
173 printf("dns[%d]: Oops, father has gone, wait for me, wait...!\n", getpid());
|
|
174 _exit(0);
|
|
175 }
|
|
176 if (dns_params.hostname[0] == '\0') {
|
|
177 printf("dns[%d]: hostname = \"\" (port = %d)!!!\n", getpid(), dns_params.port);
|
|
178 _exit(1);
|
|
179 }
|
|
180 /* Tell our parent that we read the data successfully */
|
|
181 write(child_out, &ch, sizeof(ch));
|
|
182
|
|
183 /* We have the hostname and port, now resolve the IP */
|
|
184
|
|
185 #ifdef HAVE_GETADDRINFO
|
|
186 g_snprintf(servname, sizeof(servname), "%d", dns_params.port);
|
|
187 memset(&hints, 0, sizeof(hints));
|
|
188
|
|
189 /* This is only used to convert a service
|
|
190 * name to a port number. As we know we are
|
|
191 * passing a number already, we know this
|
|
192 * value will not be really used by the C
|
|
193 * library.
|
|
194 */
|
|
195 hints.ai_socktype = SOCK_STREAM;
|
|
196 rc = getaddrinfo(dns_params.hostname, servname, &hints, &res);
|
|
197 write(child_out, &rc, sizeof(rc));
|
|
198 if (rc != 0) {
|
|
199 close(child_out);
|
|
200 if (show_debug)
|
|
201 printf("dns[%d] Error: getaddrinfo returned %d\n",
|
|
202 getpid(), rc);
|
|
203 dns_params.hostname[0] = '\0';
|
|
204 continue;
|
|
205 }
|
|
206 tmp = res;
|
|
207 while (res) {
|
|
208 size_t ai_addrlen = res->ai_addrlen;
|
|
209 write(child_out, &ai_addrlen, sizeof(ai_addrlen));
|
|
210 write(child_out, res->ai_addr, res->ai_addrlen);
|
|
211 res = res->ai_next;
|
|
212 }
|
|
213 freeaddrinfo(tmp);
|
|
214 write(child_out, &zero, sizeof(zero));
|
|
215 #else
|
|
216 if (!inet_aton(dns_params.hostname, &sin.sin_addr)) {
|
|
217 struct hostent *hp;
|
|
218 if (!(hp = gethostbyname(dns_params.hostname))) {
|
|
219 write(child_out, &h_errno, sizeof(int));
|
|
220 close(child_out);
|
|
221 if (show_debug)
|
|
222 printf("DNS Error: %d\n", h_errno);
|
|
223 _exit(0);
|
|
224 }
|
|
225 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
226 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
227 sin.sin_family = hp->h_addrtype;
|
|
228 } else
|
|
229 sin.sin_family = AF_INET;
|
|
230
|
|
231 sin.sin_port = htons(dns_params.port);
|
|
232 write(child_out, &addrlen, sizeof(addrlen));
|
|
233 write(child_out, &sin, addrlen);
|
|
234 write(child_out, &zero, sizeof(zero));
|
|
235 #endif
|
|
236 dns_params.hostname[0] = '\0';
|
|
237 }
|
|
238
|
|
239 close(child_out);
|
|
240 close(child_in);
|
|
241
|
|
242 _exit(0);
|
|
243 }
|
|
244
|
|
245 static pending_dns_request_t *
|
|
246 gaim_dns_new_resolverthread(gboolean show_debug)
|
|
247 {
|
|
248 pending_dns_request_t *req;
|
|
249 int child_out[2], child_in[2];
|
|
250
|
|
251 /* Create pipes for communicating with the child process */
|
|
252 if (pipe(child_out) || pipe(child_in)) {
|
|
253 gaim_debug_error("dns",
|
|
254 "Could not create pipes: %s\n", strerror(errno));
|
|
255 return NULL;
|
|
256 }
|
|
257
|
|
258 req = g_new(pending_dns_request_t, 1);
|
|
259
|
|
260 cope_with_gdb_brokenness();
|
|
261
|
|
262 /* Fork! */
|
|
263 req->dns_pid = fork();
|
|
264
|
|
265 /* If we are the child process... */
|
|
266 if (req->dns_pid == 0) {
|
|
267 /* We should not access the parent's side of the pipes, so close them */
|
|
268 close(child_out[0]);
|
|
269 close(child_in[1]);
|
|
270
|
|
271 gaim_dns_resolverthread(child_out[1], child_in[0], show_debug);
|
|
272 /* The thread calls _exit() rather than returning, so we never get here */
|
|
273 }
|
|
274
|
|
275 /* We should not access the child's side of the pipes, so close them */
|
|
276 close(child_out[1]);
|
|
277 close(child_in[0]);
|
|
278 if (req->dns_pid == -1) {
|
|
279 gaim_debug_error("dns",
|
|
280 "Could not create child process for DNS: %s\n",
|
|
281 strerror(errno));
|
|
282 g_free(req);
|
|
283 return NULL;
|
|
284 }
|
|
285
|
|
286 req->fd_out = child_out[0];
|
|
287 req->fd_in = child_in[1];
|
|
288 number_of_dns_children++;
|
|
289 gaim_debug_info("dns",
|
|
290 "Created new DNS child %d, there are now %d children.\n",
|
|
291 req->dns_pid, number_of_dns_children);
|
|
292
|
|
293 return req;
|
|
294 }
|
|
295 /*
|
|
296 * End the DNS resolver child process functions.
|
|
297 */
|
|
298
|
|
299 /*
|
|
300 * Begin the functions for dealing with the DNS child processes.
|
|
301 */
|
|
302 static void
|
|
303 req_free(pending_dns_request_t *req)
|
|
304 {
|
|
305 g_return_if_fail(req != NULL);
|
|
306
|
|
307 close(req->fd_in);
|
|
308 close(req->fd_out);
|
|
309
|
|
310 g_free(req->host);
|
|
311 g_free(req);
|
|
312
|
|
313 number_of_dns_children--;
|
|
314 }
|
|
315
|
|
316 static int
|
|
317 send_dns_request_to_child(pending_dns_request_t *req, dns_params_t *dns_params)
|
|
318 {
|
|
319 char ch;
|
|
320 int rc;
|
|
321 pid_t pid;
|
|
322
|
|
323 /* This waitpid might return the child's PID if it has recently
|
|
324 * exited, or it might return an error if it exited "long
|
|
325 * enough" ago that it has already been reaped; in either
|
|
326 * instance, we can't use it. */
|
|
327 if ((pid = waitpid (req->dns_pid, NULL, WNOHANG)) > 0) {
|
|
328 gaim_debug_warning("dns",
|
|
329 "DNS child %d no longer exists\n", req->dns_pid);
|
|
330 return -1;
|
|
331 } else if (pid < 0) {
|
|
332 gaim_debug_warning("dns",
|
|
333 "Wait for DNS child %d failed: %s\n",
|
|
334 req->dns_pid, strerror(errno));
|
|
335 return -1;
|
|
336 }
|
|
337
|
|
338 /* Let's contact this lost child! */
|
|
339 rc = write(req->fd_in, dns_params, sizeof(*dns_params));
|
|
340 if (rc < 0) {
|
|
341 gaim_debug_error("dns",
|
|
342 "Unable to write to DNS child %d: %d\n",
|
|
343 req->dns_pid, strerror(errno));
|
|
344 close(req->fd_in);
|
|
345 return -1;
|
|
346 }
|
|
347
|
|
348 g_return_val_if_fail(rc == sizeof(*dns_params), -1);
|
|
349
|
|
350 /* Did you hear me? (This avoids some race conditions) */
|
|
351 rc = read(req->fd_out, &ch, sizeof(ch));
|
|
352 if (rc != 1 || ch != 'Y')
|
|
353 {
|
|
354 gaim_debug_warning("dns",
|
|
355 "DNS child %d not responding. Killing it!\n",
|
|
356 req->dns_pid);
|
|
357 kill(req->dns_pid, SIGKILL);
|
|
358 return -1;
|
|
359 }
|
|
360
|
|
361 gaim_debug_info("dns",
|
|
362 "Successfully sent DNS request to child %d\n", req->dns_pid);
|
|
363
|
|
364 return 0;
|
|
365 }
|
|
366
|
|
367 static void
|
|
368 host_resolved(gpointer data, gint source, GaimInputCondition cond);
|
|
369
|
|
370 static void
|
|
371 release_dns_child(pending_dns_request_t *req)
|
|
372 {
|
|
373 g_free(req->host);
|
|
374 req->host = NULL;
|
|
375
|
|
376 if (queued_requests && !g_queue_is_empty(queued_requests)) {
|
|
377 queued_dns_request_t *r = g_queue_pop_head(queued_requests);
|
|
378 req->host = g_strdup(r->params.hostname);
|
|
379 req->port = r->params.port;
|
|
380 req->callback = r->callback;
|
|
381 req->data = r->data;
|
|
382
|
|
383 gaim_debug_info("dns",
|
|
384 "Processing queued DNS query for '%s' with child %d\n",
|
|
385 req->host, req->dns_pid);
|
|
386
|
|
387 if (send_dns_request_to_child(req, &(r->params)) != 0) {
|
|
388 req_free(req);
|
|
389 req = NULL;
|
|
390
|
|
391 gaim_debug_warning("dns",
|
|
392 "Intent of process queued query of '%s' failed, "
|
|
393 "requeueing...\n", r->params.hostname);
|
|
394 g_queue_push_head(queued_requests, r);
|
|
395 } else {
|
|
396 req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req);
|
|
397 g_free(r);
|
|
398 }
|
|
399
|
|
400 } else {
|
|
401 req->host = NULL;
|
|
402 req->callback = NULL;
|
|
403 req->data = NULL;
|
|
404 free_dns_children = g_slist_append(free_dns_children, req);
|
|
405 }
|
|
406 }
|
|
407
|
|
408 static void
|
|
409 host_resolved(gpointer data, gint source, GaimInputCondition cond)
|
|
410 {
|
|
411 pending_dns_request_t *req = (pending_dns_request_t*)data;
|
|
412 int rc, err;
|
|
413 GSList *hosts = NULL;
|
|
414 struct sockaddr *addr = NULL;
|
|
415 size_t addrlen;
|
|
416
|
|
417 gaim_debug_info("dns", "Got response for '%s'\n", req->host);
|
|
418 gaim_input_remove(req->inpa);
|
|
419
|
|
420 rc = read(req->fd_out, &err, sizeof(err));
|
|
421 if ((rc == 4) && (err != 0))
|
|
422 {
|
|
423 char message[1024];
|
|
424 #ifdef HAVE_GETADDRINFO
|
|
425 g_snprintf(message, sizeof(message), "DNS error: %s (pid=%d)",
|
|
426 gai_strerror(err), req->dns_pid);
|
|
427 #else
|
|
428 g_snprintf(message, sizeof(message), "DNS error: %d (pid=%d)",
|
|
429 err, req->dns_pid);
|
|
430 #endif
|
|
431 gaim_debug_error("dns", "%s\n", message);
|
|
432 req->callback(NULL, req->data, message);
|
|
433 release_dns_child(req);
|
|
434 return;
|
|
435 }
|
|
436 if (rc > 0)
|
|
437 {
|
|
438 while (rc > 0) {
|
|
439 rc = read(req->fd_out, &addrlen, sizeof(addrlen));
|
|
440 if (rc > 0 && addrlen > 0) {
|
|
441 addr = g_malloc(addrlen);
|
|
442 rc = read(req->fd_out, addr, addrlen);
|
|
443 hosts = g_slist_append(hosts, GINT_TO_POINTER(addrlen));
|
|
444 hosts = g_slist_append(hosts, addr);
|
|
445 } else {
|
|
446 break;
|
|
447 }
|
|
448 }
|
|
449 } else if (rc == -1) {
|
|
450 char message[1024];
|
|
451 g_snprintf(message, sizeof(message), "Error reading from DNS child: %s",strerror(errno));
|
|
452 gaim_debug_error("dns", "%s\n", message);
|
|
453 req->callback(NULL, req->data, message);
|
|
454 req_free(req);
|
|
455 return;
|
|
456 } else if (rc == 0) {
|
|
457 char message[1024];
|
|
458 g_snprintf(message, sizeof(message), "EOF reading from DNS child");
|
|
459 close(req->fd_out);
|
|
460 gaim_debug_error("dns", "%s\n", message);
|
|
461 req->callback(NULL, req->data, message);
|
|
462 req_free(req);
|
|
463 return;
|
|
464 }
|
|
465
|
|
466 /* wait4(req->dns_pid, NULL, WNOHANG, NULL); */
|
|
467
|
|
468 req->callback(hosts, req->data, NULL);
|
|
469
|
|
470 release_dns_child(req);
|
|
471 }
|
|
472 /*
|
|
473 * End the functions for dealing with the DNS child processes.
|
|
474 */
|
|
475
|
|
476 int
|
|
477 gaim_gethostbyname_async(const char *hostname, int port, GaimProxyDnsConnectFunction callback, gpointer data)
|
|
478 {
|
|
479 pending_dns_request_t *req = NULL;
|
|
480 dns_params_t dns_params;
|
|
481 gchar *host_temp;
|
|
482 gboolean show_debug;
|
|
483
|
|
484 show_debug = gaim_debug_is_enabled();
|
|
485
|
|
486 host_temp = g_strstrip(g_strdup(hostname));
|
|
487 strncpy(dns_params.hostname, host_temp, sizeof(dns_params.hostname) - 1);
|
|
488 g_free(host_temp);
|
|
489 dns_params.hostname[sizeof(dns_params.hostname) - 1] = '\0';
|
|
490 dns_params.port = port;
|
|
491
|
|
492 /*
|
|
493 * If we have any children, attempt to have them perform the DNS
|
|
494 * query. If we're able to send the query to a child, then req
|
|
495 * will be set to the pending_dns_request_t. Otherwise, req will
|
|
496 * be NULL and we'll need to create a new DNS request child.
|
|
497 */
|
|
498 while (free_dns_children != NULL) {
|
|
499 req = free_dns_children->data;
|
|
500 free_dns_children = g_slist_remove(free_dns_children, req);
|
|
501
|
|
502 if (send_dns_request_to_child(req, &dns_params) == 0)
|
|
503 /* We found an acceptable child, yay */
|
|
504 break;
|
|
505
|
|
506 req_free(req);
|
|
507 req = NULL;
|
|
508 }
|
|
509
|
|
510 /* We need to create a new DNS request child */
|
|
511 if (req == NULL) {
|
|
512 if (number_of_dns_children >= MAX_DNS_CHILDREN) {
|
|
513 queued_dns_request_t *r = g_new(queued_dns_request_t, 1);
|
|
514 memcpy(&(r->params), &dns_params, sizeof(dns_params));
|
|
515 r->callback = callback;
|
|
516 r->data = data;
|
|
517 if (!queued_requests)
|
|
518 queued_requests = g_queue_new();
|
|
519 g_queue_push_tail(queued_requests, r);
|
|
520
|
|
521 gaim_debug_info("dns",
|
|
522 "DNS query for '%s' queued\n", dns_params.hostname);
|
|
523
|
|
524 return 0;
|
|
525 }
|
|
526
|
|
527 req = gaim_dns_new_resolverthread(show_debug);
|
|
528 if (req == NULL)
|
|
529 {
|
|
530 gaim_debug_error("proxy", "oh dear, this is going to explode, I give up\n");
|
|
531 return -1;
|
|
532 }
|
|
533 send_dns_request_to_child(req, &dns_params);
|
|
534 }
|
|
535
|
|
536 req->host = g_strdup(hostname);
|
|
537 req->port = port;
|
|
538 req->callback = callback;
|
|
539 req->data = data;
|
|
540 req->inpa = gaim_input_add(req->fd_out, GAIM_INPUT_READ, host_resolved, req);
|
|
541
|
|
542 return 0;
|
|
543 }
|
|
544
|
|
545 #elif defined _WIN32 /* end __unix__ || __APPLE__ */
|
|
546
|
|
547 typedef struct _dns_tdata {
|
|
548 char *hostname;
|
|
549 int port;
|
|
550 GaimProxyDnsConnectFunction callback;
|
|
551 gpointer data;
|
|
552 GSList *hosts;
|
|
553 char *errmsg;
|
|
554 } dns_tdata;
|
|
555
|
|
556 static gboolean dns_main_thread_cb(gpointer data) {
|
|
557 dns_tdata *td = (dns_tdata*)data;
|
|
558 if (td->errmsg != NULL) {
|
|
559 gaim_debug_info("dns", "%s\n", td->errmsg);
|
|
560 }
|
|
561 td->callback(td->hosts, td->data, td->errmsg);
|
|
562 g_free(td->hostname);
|
|
563 g_free(td->errmsg);
|
|
564 g_free(td);
|
|
565 return FALSE;
|
|
566 }
|
|
567
|
|
568 static gpointer dns_thread(gpointer data) {
|
|
569
|
|
570 #ifdef HAVE_GETADDRINFO
|
|
571 int rc;
|
|
572 struct addrinfo hints, *res, *tmp;
|
|
573 char servname[20];
|
|
574 #else
|
|
575 struct sockaddr_in sin;
|
|
576 struct hostent *hp;
|
|
577 #endif
|
|
578 dns_tdata *td = (dns_tdata*)data;
|
|
579
|
|
580 #ifdef HAVE_GETADDRINFO
|
|
581 g_snprintf(servname, sizeof(servname), "%d", td->port);
|
|
582 memset(&hints,0,sizeof(hints));
|
|
583
|
|
584 /* This is only used to convert a service
|
|
585 * name to a port number. As we know we are
|
|
586 * passing a number already, we know this
|
|
587 * value will not be really used by the C
|
|
588 * library.
|
|
589 */
|
|
590 hints.ai_socktype = SOCK_STREAM;
|
|
591 if ((rc = getaddrinfo(td->hostname, servname, &hints, &res)) == 0) {
|
|
592 tmp = res;
|
|
593 while(res) {
|
|
594 td->hosts = g_slist_append(td->hosts,
|
|
595 GSIZE_TO_POINTER(res->ai_addrlen));
|
|
596 td->hosts = g_slist_append(td->hosts,
|
|
597 g_memdup(res->ai_addr, res->ai_addrlen));
|
|
598 res = res->ai_next;
|
|
599 }
|
|
600 freeaddrinfo(tmp);
|
|
601 } else {
|
|
602 td->errmsg = g_strdup_printf("DNS getaddrinfo(\"%s\", \"%s\") error: %d", td->hostname, servname, rc);
|
|
603 }
|
|
604 #else
|
|
605 if ((hp = gethostbyname(td->hostname))) {
|
|
606 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
607 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
608 sin.sin_family = hp->h_addrtype;
|
|
609 sin.sin_port = htons(td->port);
|
|
610
|
|
611 td->hosts = g_slist_append(td->hosts,
|
|
612 GSIZE_TO_POINTER(sizeof(sin)));
|
|
613 td->hosts = g_slist_append(td->hosts,
|
|
614 g_memdup(&sin, sizeof(sin)));
|
|
615 } else {
|
|
616 td->errmsg = g_strdup_printf("DNS gethostbyname(\"%s\") error: %d", td->hostname, h_errno);
|
|
617 }
|
|
618 #endif
|
|
619 /* back to main thread */
|
|
620 g_idle_add(dns_main_thread_cb, td);
|
|
621 return 0;
|
|
622 }
|
|
623
|
|
624 int
|
|
625 gaim_gethostbyname_async(const char *hostname, int port,
|
|
626 GaimProxyDnsConnectFunction callback, gpointer data)
|
|
627 {
|
|
628 dns_tdata *td;
|
|
629 struct sockaddr_in sin;
|
|
630 GError* err = NULL;
|
|
631
|
|
632 if(inet_aton(hostname, &sin.sin_addr)) {
|
|
633 GSList *hosts = NULL;
|
|
634 sin.sin_family = AF_INET;
|
|
635 sin.sin_port = htons(port);
|
|
636 hosts = g_slist_append(hosts, GINT_TO_POINTER(sizeof(sin)));
|
|
637 hosts = g_slist_append(hosts, g_memdup(&sin, sizeof(sin)));
|
|
638 callback(hosts, data, NULL);
|
|
639 return 0;
|
|
640 }
|
|
641
|
|
642 gaim_debug_info("dns", "DNS Lookup for: %s\n", hostname);
|
|
643 td = g_new0(dns_tdata, 1);
|
|
644 td->hostname = g_strdup(hostname);
|
|
645 td->port = port;
|
|
646 td->callback = callback;
|
|
647 td->data = data;
|
|
648
|
|
649 if(!g_thread_create(dns_thread, td, FALSE, &err)) {
|
|
650 gaim_debug_error("dns", "DNS thread create failure: %s\n", err?err->message:"");
|
|
651 g_error_free(err);
|
|
652 g_free(td->hostname);
|
|
653 g_free(td);
|
|
654 return -1;
|
|
655 }
|
|
656 return 0;
|
|
657 }
|
|
658
|
|
659 #else /* not __unix__ or __APPLE__ or _WIN32 */
|
|
660
|
|
661 typedef struct {
|
|
662 gpointer data;
|
|
663 size_t addrlen;
|
|
664 struct sockaddr *addr;
|
|
665 GaimProxyDnsConnectFunction callback;
|
|
666 } pending_dns_request_t;
|
|
667
|
|
668 static gboolean host_resolved(gpointer data)
|
|
669 {
|
|
670 pending_dns_request_t *req = (pending_dns_request_t*)data;
|
|
671 GSList *hosts = NULL;
|
|
672 hosts = g_slist_append(hosts, GINT_TO_POINTER(req->addrlen));
|
|
673 hosts = g_slist_append(hosts, req->addr);
|
|
674 req->callback(hosts, req->data, NULL);
|
|
675 g_free(req);
|
|
676 return FALSE;
|
|
677 }
|
|
678
|
|
679 int
|
|
680 gaim_gethostbyname_async(const char *hostname, int port,
|
|
681 GaimProxyDnsConnectFunction callback, gpointer data)
|
|
682 {
|
|
683 struct sockaddr_in sin;
|
|
684 pending_dns_request_t *req;
|
|
685
|
|
686 if (!inet_aton(hostname, &sin.sin_addr)) {
|
|
687 struct hostent *hp;
|
|
688 if(!(hp = gethostbyname(hostname))) {
|
|
689 gaim_debug_error("dns",
|
|
690 "gaim_gethostbyname(\"%s\", %d) failed: %d\n",
|
|
691 hostname, port, h_errno);
|
|
692 return -1;
|
|
693 }
|
|
694 memset(&sin, 0, sizeof(struct sockaddr_in));
|
|
695 memcpy(&sin.sin_addr.s_addr, hp->h_addr, hp->h_length);
|
|
696 sin.sin_family = hp->h_addrtype;
|
|
697 } else
|
|
698 sin.sin_family = AF_INET;
|
|
699 sin.sin_port = htons(port);
|
|
700
|
|
701 req = g_new(pending_dns_request_t, 1);
|
|
702 req->addr = (struct sockaddr*) g_memdup(&sin, sizeof(sin));
|
|
703 req->addrlen = sizeof(sin);
|
|
704 req->data = data;
|
|
705 req->callback = callback;
|
|
706 gaim_timeout_add(10, host_resolved, req);
|
|
707 return 0;
|
|
708 }
|
|
709
|
|
710 #endif /* not __unix__ or __APPLE__ or _WIN32 */
|