X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=avahi-core%2Fdomain-util.c;h=ab1ec8d0c51dd92100d8f5fb211c512b7c2d9a16;hb=7ba2b1fcc731085127c9976b1bae4aaaa5b8b7a7;hp=d4cc2ad91f02782e7ef07ce181fedf05175c4faf;hpb=c3575017e2137ef664e4735bd6f9ff1209653ef3;p=catta diff --git a/avahi-core/domain-util.c b/avahi-core/domain-util.c index d4cc2ad..ab1ec8d 100644 --- a/avahi-core/domain-util.c +++ b/avahi-core/domain-util.c @@ -27,25 +27,56 @@ #include #include #include +#include +#include +#include #include #include "domain-util.h" +#include "util.h" -char *avahi_get_host_name(char *ret_s, size_t size) { -#ifdef HOST_NAME_MAX - char t[HOST_NAME_MAX]; -#else - char t[256]; -#endif +static void strip_bad_chars(char *s) { + char *p, *d; + + s[strcspn(s, ".")] = 0; + for (p = s, d = s; *p; p++) + if (isalnum(*p) || *p == '-') + *(d++) = *p; + + *d = 0; +} + +char *avahi_get_host_name(char *ret_s, size_t size) { assert(ret_s); assert(size > 0); + + if (gethostname(ret_s, size) >= 0) { + ret_s[size-1] = 0; + strip_bad_chars(ret_s); + } else + *ret_s = 0; + + if (*ret_s == 0) { + struct utsname utsname; + + /* No hostname was set, so let's take the OS name */ + + if (uname(&utsname) >= 0) { + snprintf(ret_s, size, "%s", utsname.sysname); + strip_bad_chars(ret_s); + avahi_strdown(ret_s); + } + + if (*ret_s == 0) + snprintf(ret_s, size, "unnamed"); + } + + if (size >= AVAHI_LABEL_MAX) + ret_s[AVAHI_LABEL_MAX-1] = 0; - gethostname(t, sizeof(t)); - t[sizeof(t)-1] = 0; - - return avahi_normalize_name(t, ret_s, size); + return ret_s; } char *avahi_get_host_name_strdup(void) {