comparison src/sysdep.c @ 86212:777f1d31b403

(init_system_name): Use getaddrinfo if available.
author Jan Djärv <jan.h.d@swipnet.se>
date Sun, 18 Nov 2007 17:33:55 +0000
parents 29a2854dd601
children ec0ef12211d9
comparison
equal deleted inserted replaced
86211:28ea5d5d04ca 86212:777f1d31b403
2516 #ifndef CANNOT_DUMP 2516 #ifndef CANNOT_DUMP
2517 if (initialized) 2517 if (initialized)
2518 #endif /* not CANNOT_DUMP */ 2518 #endif /* not CANNOT_DUMP */
2519 if (! index (hostname, '.')) 2519 if (! index (hostname, '.'))
2520 { 2520 {
2521 struct hostent *hp;
2522 int count; 2521 int count;
2522 #ifdef HAVE_GETADDRINFO
2523 struct addrinfo *res;
2524 struct addrinfo hints;
2525 int ret;
2526
2527 memset (&hints, 0, sizeof(hints));
2528 hints.ai_socktype = SOCK_STREAM;
2529 hints.ai_flags = AI_CANONNAME;
2530
2523 for (count = 0;; count++) 2531 for (count = 0;; count++)
2524 { 2532 {
2533 if ((ret = getaddrinfo (hostname, NULL, &hints, &res)) == 0
2534 || ret != EAI_AGAIN)
2535 break;
2536
2537 if (count >= 5)
2538 break;
2539 Fsleep_for (make_number (1), Qnil);
2540 }
2541
2542 if (ret == 0)
2543 {
2544 struct addrinfo *it = res;
2545 while (it)
2546 {
2547 char *fqdn = it->ai_canonname;
2548 if (fqdn && index (fqdn, '.')
2549 && strcmp (fqdn, "localhost.localdomain") != 0)
2550 break;
2551 it = it->ai_next;
2552 }
2553 if (it)
2554 {
2555 hostname = alloca (strlen (it->ai_canonname) + 1);
2556 strcpy (hostname, it->ai_canonname);
2557 }
2558 freeaddrinfo (res);
2559 }
2560 #else /* !HAVE_GETADDRINFO */
2561 struct hostent *hp;
2562 for (count = 0;; count++)
2563 {
2564
2525 #ifdef TRY_AGAIN 2565 #ifdef TRY_AGAIN
2526 h_errno = 0; 2566 h_errno = 0;
2527 #endif 2567 #endif
2528 hp = gethostbyname (hostname); 2568 hp = gethostbyname (hostname);
2529 #ifdef TRY_AGAIN 2569 #ifdef TRY_AGAIN
2530 if (! (hp == 0 && h_errno == TRY_AGAIN)) 2570 if (! (hp == 0 && h_errno == TRY_AGAIN))
2531 #endif 2571 #endif
2572
2532 break; 2573 break;
2574
2533 if (count >= 5) 2575 if (count >= 5)
2534 break; 2576 break;
2535 Fsleep_for (make_number (1), Qnil); 2577 Fsleep_for (make_number (1), Qnil);
2536 } 2578 }
2579
2537 if (hp) 2580 if (hp)
2538 { 2581 {
2539 char *fqdn = (char *) hp->h_name; 2582 char *fqdn = (char *) hp->h_name;
2540 #if 0 2583 #if 0
2541 char *p; 2584 char *p;
2565 *p += 'a' - 'A'; 2608 *p += 'a' - 'A';
2566 p++; 2609 p++;
2567 } 2610 }
2568 #endif 2611 #endif
2569 } 2612 }
2613 #endif /* !HAVE_GETADDRINFO */
2570 } 2614 }
2571 #endif /* HAVE_SOCKETS */ 2615 #endif /* HAVE_SOCKETS */
2572 /* We used to try using getdomainname here, 2616 /* We used to try using getdomainname here,
2573 but NIIBE Yutaka <gniibe@etl.go.jp> says that 2617 but NIIBE Yutaka <gniibe@etl.go.jp> says that
2574 getdomainname gets the NIS/YP domain which often is not the same 2618 getdomainname gets the NIS/YP domain which often is not the same