X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fnss-myhostname%2Fnss-myhostname.c;h=fed76c62e72f1ce2e915c556e4ff60991d9782bd;hb=dc75168823540076b354135f6e2de7a9a978fbca;hp=4a5bf759a9a2ae6921e13c5997d8fe61ed64abb2;hpb=e9140aff750e4f251f5f09b67412fed995fe9c47;p=elogind.git diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index 4a5bf759a..fed76c62e 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -19,16 +19,12 @@ along with systemd; If not, see . ***/ -#include #include -#include #include #include #include -#include #include #include -#include #include "local-addresses.h" #include "macro.h" @@ -38,7 +34,7 @@ /* We use 127.0.0.2 as IPv4 address. This has the advantage over * 127.0.0.1 that it can be translated back to the local hostname. For * IPv6 we use ::1 which unfortunately will not translate back to the - * hostname but instead something like "localhost6" or so. */ + * hostname but instead something like "localhost" or so. */ #define LOCALADDRESS_IPV4 (htonl(0x7F000002)) #define LOCALADDRESS_IPV6 &in6addr_loopback @@ -47,6 +43,13 @@ NSS_GETHOSTBYNAME_PROTOTYPES(myhostname); NSS_GETHOSTBYADDR_PROTOTYPES(myhostname); +static bool is_gateway(const char *hostname) { + assert(hostname); + + return streq(hostname, "gateway") || + streq(hostname, "gateway."); +} + enum nss_status _nss_myhostname_gethostbyname4_r( const char *name, struct gaih_addrtuple **pat, @@ -78,9 +81,9 @@ enum nss_status _nss_myhostname_gethostbyname4_r( canonical = "localhost"; local_address_ipv4 = htonl(INADDR_LOOPBACK); - } else if (streq(name, "gateway")) { + } else if (is_gateway(name)) { - n_addresses = local_gateways(NULL, 0, &addresses); + n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses); if (n_addresses <= 0) { *errnop = ENOENT; *h_errnop = HOST_NOT_FOUND; @@ -104,7 +107,7 @@ enum nss_status _nss_myhostname_gethostbyname4_r( return NSS_STATUS_NOTFOUND; } - n_addresses = local_addresses(NULL, 0, &addresses); + n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses); if (n_addresses < 0) n_addresses = 0; @@ -348,9 +351,9 @@ enum nss_status _nss_myhostname_gethostbyname3_r( canonical = "localhost"; local_address_ipv4 = htonl(INADDR_LOOPBACK); - } else if (streq(name, "gateway")) { + } else if (is_gateway(name)) { - n_addresses = local_gateways(NULL, af, &addresses); + n_addresses = local_gateways(NULL, 0, af, &addresses); if (n_addresses <= 0) { *errnop = ENOENT; *h_errnop = HOST_NOT_FOUND; @@ -373,7 +376,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r( return NSS_STATUS_NOTFOUND; } - n_addresses = local_addresses(NULL, af, &addresses); + n_addresses = local_addresses(NULL, 0, af, &addresses); if (n_addresses < 0) n_addresses = 0; @@ -408,6 +411,7 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( _cleanup_free_ char *hn = NULL; int n_addresses = 0; struct local_address *a; + bool additional_from_hostname = false; unsigned n; assert(addr); @@ -429,7 +433,6 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( } if (af == AF_INET) { - if ((*(uint32_t*) addr) == LOCALADDRESS_IPV4) goto found; @@ -443,44 +446,33 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( assert(af == AF_INET6); if (memcmp(addr, LOCALADDRESS_IPV6, 16) == 0) { - additional = "localhost"; + canonical = "localhost"; + additional_from_hostname = true; goto found; } - } - n_addresses = local_addresses(NULL, 0, &addresses); + n_addresses = local_addresses(NULL, 0, AF_UNSPEC, &addresses); if (n_addresses > 0) { for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) { if (af != a->family) continue; - if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) { - - hn = gethostname_malloc(); - if (!hn) { - *errnop = ENOMEM; - *h_errnop = NO_RECOVERY; - return NSS_STATUS_TRYAGAIN; - } - - canonical = hn; + if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) goto found; - } } } free(addresses); addresses = NULL; - n_addresses = local_gateways(NULL, 0, &addresses); + n_addresses = local_gateways(NULL, 0, AF_UNSPEC, &addresses); if (n_addresses > 0) { for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) { if (af != a->family) continue; if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) { - canonical = "gateway"; goto found; } @@ -493,6 +485,20 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( return NSS_STATUS_NOTFOUND; found: + if (!canonical || (!additional && additional_from_hostname)) { + hn = gethostname_malloc(); + if (!hn) { + *errnop = ENOMEM; + *h_errnop = NO_RECOVERY; + return NSS_STATUS_TRYAGAIN; + } + + if (!canonical) + canonical = hn; + + if (!additional && additional_from_hostname) + additional = hn; + } return fill_in_hostent( canonical, additional, @@ -504,7 +510,6 @@ found: errnop, h_errnop, ttlp, NULL); - } NSS_GETHOSTBYNAME_FALLBACKS(myhostname);