]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nss-myhostname: expose the "outbound" IP addresses under the synthetic "_outbound...
authorLennart Poettering <lennart@poettering.net>
Fri, 26 Mar 2021 17:11:41 +0000 (18:11 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 23 Apr 2021 10:02:11 +0000 (12:02 +0200)
I found myself often looking for a quick way to determine "the local IP
address", and then being lost in the "ip addr" output to find for the
right one to use. This is supposed to help a bit with that. Let's
introduce a new special hostname "_outbound" with semantics similar to
"_gateway" that resolves to addresses that are the closest I could come
up with that maps to "the" local IP address.

src/basic/hostname-util.h
src/nss-myhostname/nss-myhostname.c
src/test/test-nss-hosts.c

index 576ca083e011ef879f4d7b551c6c3a1a1e434354..c3fc6752cbae43369ebfdbfcb64274da817bd358 100644 (file)
@@ -28,3 +28,8 @@ static inline bool is_gateway_hostname(const char *hostname) {
         /* This tries to identify the valid syntaxes for the our synthetic "gateway" host. */
         return STRCASE_IN_SET(hostname, "_gateway", "_gateway.");
 }
+
+static inline bool is_outbound_hostname(const char *hostname) {
+        /* This tries to identify the valid syntaxes for the our synthetic "outbound" host. */
+        return STRCASE_IN_SET(hostname, "_outbound", "_outbound.");
+}
index 6e37966da08c5bcedc67868b43a8c789bda39ed0..3536c5fc8370c5ed403f120d6f5e3a8fc16daf35 100644 (file)
@@ -54,8 +54,7 @@ enum nss_status _nss_myhostname_gethostbyname4_r(
         assert(h_errnop);
 
         if (is_localhost(name)) {
-                /* We respond to 'localhost', so that /etc/hosts
-                 * is optional */
+                /* We respond to 'localhost', so that /etc/hosts is optional */
 
                 canonical = "localhost";
                 local_address_ipv4 = htobe32(INADDR_LOOPBACK);
@@ -68,6 +67,14 @@ enum nss_status _nss_myhostname_gethostbyname4_r(
 
                 canonical = "_gateway";
 
+        } else if (is_outbound_hostname(name)) {
+
+                n_addresses = local_outbounds(NULL, 0, AF_UNSPEC, &addresses);
+                if (n_addresses <= 0)
+                        goto not_found;
+
+                canonical = "_outbound";
+
         } else {
                 hn = gethostname_malloc();
                 if (!hn) {
@@ -343,6 +350,14 @@ enum nss_status _nss_myhostname_gethostbyname3_r(
 
                 canonical = "_gateway";
 
+        } else if (is_outbound_hostname(name)) {
+
+                n_addresses = local_outbounds(NULL, 0, af, &addresses);
+                if (n_addresses <= 0)
+                        goto not_found;
+
+                canonical = "_outbound";
+
         } else {
                 hn = gethostname_malloc();
                 if (!hn) {
index e9bc6ecce61b6c01d253fb091b9379e39a09b89d..42ed53adc35b70bb13d8e0f71cd758ef2dae6682 100644 (file)
@@ -455,8 +455,7 @@ static int parse_argv(int argc, char **argv,
         } else {
                 _cleanup_free_ char *hostname;
                 assert_se(hostname = gethostname_malloc());
-
-                assert_se(names = strv_new("localhost", "_gateway", "foo_no_such_host", hostname));
+                assert_se(names = strv_new("localhost", "_gateway", "_outbound", "foo_no_such_host", hostname));
 
                 n = make_addresses(&addrs);
                 assert_se(n >= 0);