comparison libpurple/dnsquery.c @ 24099:d31ceef11df1

merge of 'aaddbdd76370264a6f95b6d787b565061e3b40d6' and 'ab9a8e940ca290009deae6e3044bc352a0cd9223'
author Kevin Stange <kevin@simguy.net>
date Thu, 11 Sep 2008 20:58:15 +0000
parents b703102fa0a5
children 5395b18f9f08
comparison
equal deleted inserted replaced
24098:dc25ef2669f6 24099:d31ceef11df1
207 * number from the socket with our parent. The bottom 207 * number from the socket with our parent. The bottom
208 * half of this resolves the IP (blocking) and sends 208 * half of this resolves the IP (blocking) and sends
209 * the result back to our parent, when finished. 209 * the result back to our parent, when finished.
210 */ 210 */
211 while (1) { 211 while (1) {
212 const char ch = 'Y';
213 fd_set fds;
214 struct timeval tv = { .tv_sec = 40 , .tv_usec = 0 };
215 FD_ZERO(&fds);
216 FD_SET(child_in, &fds);
217 rc = select(child_in + 1, &fds, NULL, NULL, &tv);
218 if (!rc) {
219 if (show_debug)
220 printf("dns[%d]: nobody needs me... =(\n", getpid());
221 break;
222 }
223 rc = read(child_in, &dns_params, sizeof(dns_params_t)); 212 rc = read(child_in, &dns_params, sizeof(dns_params_t));
224 if (rc < 0) { 213 if (rc < 0) {
225 fprintf(stderr, "dns[%d]: Error: Could not read dns_params: " 214 fprintf(stderr, "dns[%d]: Error: Could not read dns_params: "
226 "%s\n", getpid(), strerror(errno)); 215 "%s\n", getpid(), strerror(errno));
227 break; 216 break;
235 fprintf(stderr, "dns[%d]: Error: Parent requested resolution " 224 fprintf(stderr, "dns[%d]: Error: Parent requested resolution "
236 "of an empty hostname (port = %d)!!!\n", getpid(), 225 "of an empty hostname (port = %d)!!!\n", getpid(),
237 dns_params.port); 226 dns_params.port);
238 _exit(1); 227 _exit(1);
239 } 228 }
240 /* Tell our parent that we read the data successfully */
241 write_to_parent(child_out, &ch, sizeof(ch));
242 229
243 /* We have the hostname and port, now resolve the IP */ 230 /* We have the hostname and port, now resolve the IP */
244 231
245 #ifdef HAVE_GETADDRINFO 232 #ifdef HAVE_GETADDRINFO
246 g_snprintf(servname, sizeof(servname), "%d", dns_params.port); 233 g_snprintf(servname, sizeof(servname), "%d", dns_params.port);
423 send_dns_request_to_child(PurpleDnsQueryData *query_data, 410 send_dns_request_to_child(PurpleDnsQueryData *query_data,
424 PurpleDnsQueryResolverProcess *resolver) 411 PurpleDnsQueryResolverProcess *resolver)
425 { 412 {
426 pid_t pid; 413 pid_t pid;
427 dns_params_t dns_params; 414 dns_params_t dns_params;
428 int rc; 415 ssize_t rc;
429 char ch;
430 416
431 /* This waitpid might return the child's PID if it has recently 417 /* This waitpid might return the child's PID if it has recently
432 * exited, or it might return an error if it exited "long 418 * exited, or it might return an error if it exited "long
433 * enough" ago that it has already been reaped; in either 419 * enough" ago that it has already been reaped; in either
434 * instance, we can't use it. */ 420 * instance, we can't use it. */
456 purple_debug_error("dns", "Unable to write to DNS child %d: %s\n", 442 purple_debug_error("dns", "Unable to write to DNS child %d: %s\n",
457 resolver->dns_pid, g_strerror(errno)); 443 resolver->dns_pid, g_strerror(errno));
458 purple_dnsquery_resolver_destroy(resolver); 444 purple_dnsquery_resolver_destroy(resolver);
459 return FALSE; 445 return FALSE;
460 } 446 }
461 447 if (rc < sizeof(dns_params)) {
462 g_return_val_if_fail(rc == sizeof(dns_params), -1); 448 purple_debug_error("dns", "Tried to read %" G_GSSIZE_FORMAT
463 449 " bytes from child but only read %" G_GSSIZE_FORMAT "\n",
464 /* Did you hear me? (This avoids some race conditions) */ 450 sizeof(dns_params), rc);
465 rc = read(resolver->fd_out, &ch, sizeof(ch));
466 if (rc != 1 || ch != 'Y')
467 {
468 purple_debug_warning("dns",
469 "DNS child %d not responding. Killing it!\n",
470 resolver->dns_pid);
471 purple_dnsquery_resolver_destroy(resolver); 451 purple_dnsquery_resolver_destroy(resolver);
472 return FALSE; 452 return FALSE;
473 } 453 }
474 454
475 purple_debug_info("dns", 455 purple_debug_info("dns",