]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Obey the hostname_short option even for FQDN hostnames passed via config.
authorRoy Marples <roy@marples.name>
Fri, 13 Nov 2015 11:18:44 +0000 (11:18 +0000)
committerRoy Marples <roy@marples.name>
Fri, 13 Nov 2015 11:18:44 +0000 (11:18 +0000)
Fixes [c7c4e51c72] based on a patch by sem@altlinux.org.

common.c
common.h
dhcp-common.c
dhcp-common.h
dhcp.c
dhcp6.c

index f5bae1378c9d5709b66d899b540649e7ee0c207b..f8553da16cd11f7e64e301efe733321292d05d99 100644 (file)
--- a/common.c
+++ b/common.c
 #  define _PATH_DEVNULL "/dev/null"
 #endif
 
-const char *
-get_hostname(char *buf, size_t buflen, int short_hostname)
-{
-       char *p;
-
-       if (gethostname(buf, buflen) != 0)
-               return NULL;
-       buf[buflen - 1] = '\0';
-       if (strcmp(buf, "(none)") == 0 ||
-           strcmp(buf, "localhost") == 0 ||
-           strncmp(buf, "localhost.", strlen("localhost.")) == 0 ||
-           buf[0] == '.')
-               return NULL;
-
-       if (short_hostname) {
-               p = strchr(buf, '.');
-               if (p)
-                       *p = '\0';
-       }
-
-       return buf;
-}
-
 #if USE_LOGFILE
 void
 logger_open(struct dhcpcd_ctx *ctx)
index ebf529d42c9739b698d3ccee0c8a737833023ef7..bb6f5a7e4a037f4e9d538949a2490bea3f0b681b 100644 (file)
--- a/common.h
+++ b/common.h
 #endif
 
 void get_line_free(void);
-const char *get_hostname(char *, size_t, int);
 extern int clock_monotonic;
 int get_monotonic(struct timespec *);
 
index 285e29c0d519e4aae7fdbff24536ffef18cbfefe..870a0da626abc868ba99670354ace88062f74d06 100644 (file)
 #define NS_MAXLABEL MAXLABEL
 #endif
 
+const char *
+dhcp_get_hostname(char *buf, size_t buf_len, const struct if_options *ifo)
+{
+       char *hostname;
+
+       if (ifo->hostname[0] == '\0') {
+               if (gethostname(buf, buf_len) != 0)
+                       return NULL;
+               buf[buf_len - 1] = '\0';
+       } else
+               strlcpy(buf, ifo->hostname, sizeof(buf));
+
+       /* Deny sending of these local hostnames */
+       if (strcmp(buf, "(none)") == 0 ||
+           strcmp(buf, "localhost") == 0 ||
+           strncmp(buf, "localhost.", strlen("localhost.")) == 0 ||
+           buf[0] == '.')
+               return NULL;
+
+       /* Shorten the hostname if required */
+       if (ifo->options & DHCPCD_HOSTNAME_SHORT) {
+               char *hp;
+
+               hp = strchr(buf, '.');
+               if (hp != NULL)
+                       *hp = '\0';
+       }
+
+       return buf;
+}
+
 void
 dhcp_print_option_encoding(const struct dhcp_opt *opt, int cols)
 {
index 37451b06172e8783101ddc4a88b2d1ac9d1cc425..5f9f9e60458b883f32dade587d06bdc142fffdb8 100644 (file)
@@ -88,6 +88,7 @@ struct dhcp_opt {
        size_t encopts_len;
 };
 
+const char *dhcp_get_hostname(char *, size_t, const struct if_options *);
 struct dhcp_opt *vivso_find(uint32_t, const void *);
 
 ssize_t dhcp_vendor(char *, size_t);
diff --git a/dhcp.c b/dhcp.c
index cdfae5b9e88f2d410e81203f4ab279ac3615d80e..92b390553634fd7daed1421186a5ed3ca27830d7 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -883,11 +883,7 @@ make_message(struct dhcp_message **message,
                        }
                }
 
-               if (ifo->hostname[0] == '\0')
-                       hostname = get_hostname(hbuf, sizeof(hbuf),
-                           ifo->options & DHCPCD_HOSTNAME_SHORT ? 1 : 0);
-               else
-                       hostname = ifo->hostname;
+               hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo);
 
                /*
                 * RFC4702 3.1 States that if we send the Client FQDN option
diff --git a/dhcp6.c b/dhcp6.c
index 283563d370a5ea74d8ebe651e447521fc51eed51..d8d72ee554bda313a25e8dfbcc9a8c74323f5f7a 100644 (file)
--- a/dhcp6.c
+++ b/dhcp6.c
@@ -559,13 +559,9 @@ dhcp6_makemessage(struct interface *ifp)
                 * hostname and FQDN according to RFC4702 */
                fqdn = FQDN_BOTH;
        }
-       if (fqdn != FQDN_DISABLE) {
-               if (ifo->hostname[0] == '\0')
-                       hostname = get_hostname(hbuf, sizeof(hbuf),
-                           ifo->options & DHCPCD_HOSTNAME_SHORT ? 1 : 0);
-               else
-                       hostname = ifo->hostname;
-       } else
+       if (fqdn != FQDN_DISABLE)
+               hostname = dhcp_get_hostname(hbuf, sizeof(hbuf), ifo);
+       else
                hostname = NULL; /* appearse gcc */
 
        /* Work out option size first */