diff options
author | Lennart Poettering <lennart@poettering.net> | 2014-12-03 21:42:58 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2014-12-03 21:48:45 +0100 |
commit | e9140aff750e4f251f5f09b67412fed995fe9c47 (patch) | |
tree | 30f2c292e0154cfee8ce91bdf2c47674030255e9 /src/nss-myhostname | |
parent | 144232a8e0ea77eed8c5a456832758681b5b3511 (diff) |
nss-myhostname: always resolve the host name "gateway" to the local default gateway
This is useful inside of containers or local networks to intrdouce a
stable name of the default gateway host (in case of containers usually
the host, in case of LANs usually local router).
Diffstat (limited to 'src/nss-myhostname')
-rw-r--r-- | src/nss-myhostname/nss-myhostname.c | 78 |
1 files changed, 59 insertions, 19 deletions
diff --git a/src/nss-myhostname/nss-myhostname.c b/src/nss-myhostname/nss-myhostname.c index 86e7be2aa1..4a5bf759a9 100644 --- a/src/nss-myhostname/nss-myhostname.c +++ b/src/nss-myhostname/nss-myhostname.c @@ -77,6 +77,18 @@ enum nss_status _nss_myhostname_gethostbyname4_r( canonical = "localhost"; local_address_ipv4 = htonl(INADDR_LOOPBACK); + + } else if (streq(name, "gateway")) { + + n_addresses = local_gateways(NULL, 0, &addresses); + if (n_addresses <= 0) { + *errnop = ENOENT; + *h_errnop = HOST_NOT_FOUND; + return NSS_STATUS_NOTFOUND; + } + + canonical = "gateway"; + } else { hn = gethostname_malloc(); if (!hn) { @@ -314,7 +326,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r( _cleanup_free_ struct local_address *addresses = NULL; const char *canonical, *additional = NULL; _cleanup_free_ char *hn = NULL; - uint32_t local_address_ipv4; + uint32_t local_address_ipv4 = 0; int n_addresses = 0; assert(name); @@ -335,6 +347,18 @@ enum nss_status _nss_myhostname_gethostbyname3_r( if (is_localhost(name)) { canonical = "localhost"; local_address_ipv4 = htonl(INADDR_LOOPBACK); + + } else if (streq(name, "gateway")) { + + n_addresses = local_gateways(NULL, af, &addresses); + if (n_addresses <= 0) { + *errnop = ENOENT; + *h_errnop = HOST_NOT_FOUND; + return NSS_STATUS_NOTFOUND; + } + + canonical = "gateway"; + } else { hn = gethostname_malloc(); if (!hn) { @@ -349,7 +373,7 @@ enum nss_status _nss_myhostname_gethostbyname3_r( return NSS_STATUS_NOTFOUND; } - n_addresses = local_addresses(NULL, 0, &addresses); + n_addresses = local_addresses(NULL, af, &addresses); if (n_addresses < 0) n_addresses = 0; @@ -426,15 +450,41 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( } n_addresses = local_addresses(NULL, 0, &addresses); - if (n_addresses < 0) - n_addresses = 0; + if (n_addresses > 0) { + for (a = addresses, n = 0; (int) n < n_addresses; n++, a++) { + if (af != a->family) + continue; - 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) { - if (memcmp(addr, &a->address, FAMILY_ADDRESS_SIZE(af)) == 0) - goto found; + hn = gethostname_malloc(); + if (!hn) { + *errnop = ENOMEM; + *h_errnop = NO_RECOVERY; + return NSS_STATUS_TRYAGAIN; + } + + canonical = hn; + goto found; + } + } + } + + free(addresses); + addresses = NULL; + + n_addresses = local_gateways(NULL, 0, &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; + } + } } *errnop = ENOENT; @@ -443,16 +493,6 @@ enum nss_status _nss_myhostname_gethostbyaddr2_r( return NSS_STATUS_NOTFOUND; found: - if (!canonical) { - hn = gethostname_malloc(); - if (!hn) { - *errnop = ENOMEM; - *h_errnop = NO_RECOVERY; - return NSS_STATUS_TRYAGAIN; - } - - canonical = hn; - } return fill_in_hostent( canonical, additional, |