# 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)
#endif
void get_line_free(void);
-const char *get_hostname(char *, size_t, int);
extern int clock_monotonic;
int get_monotonic(struct timespec *);
#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)
{
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);
}
}
- 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
* 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 */