From: Lennart Poettering Date: Tue, 8 Nov 2005 23:16:16 +0000 (+0000) Subject: * deal with gethostname() failing, use utsname.sysname[] instead in that case X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=55357e3c75438ce83553d1c43d47e53eb7919d74;p=catta * deal with gethostname() failing, use utsname.sysname[] instead in that case * deal with utsname() failing when creating HINFO records git-svn-id: file:///home/lennart/svn/public/avahi/trunk@938 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-core/domain-util.c b/avahi-core/domain-util.c index d4cc2ad..f541226 100644 --- a/avahi-core/domain-util.c +++ b/avahi-core/domain-util.c @@ -27,25 +27,53 @@ #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"); + } - 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) { diff --git a/avahi-core/server.c b/avahi-core/server.c index 0cc4d4e..e42692f 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -1133,13 +1133,20 @@ static void register_hinfo(AvahiServer *s) { /* Fill in HINFO rr */ if ((r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL_HOST_NAME))) { - uname(&utsname); - r->data.hinfo.cpu = avahi_strdup(avahi_strup(utsname.machine)); - r->data.hinfo.os = avahi_strdup(avahi_strup(utsname.sysname)); - if (avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UNIQUE, r) < 0) { - avahi_log_warn("Failed to add HINFO RR: %s", avahi_strerror(s->error)); - return; + if (uname(&utsname) < 0) + avahi_log_warn("uname() failed: %s\n", avahi_strerror(errno)); + else { + + r->data.hinfo.cpu = avahi_strdup(avahi_strup(utsname.machine)); + r->data.hinfo.os = avahi_strdup(avahi_strup(utsname.sysname)); + + avahi_log_info("Adding HINFO record with values '%s/%s'", r->data.hinfo.cpu, r->data.hinfo.os); + + if (avahi_server_add(s, s->hinfo_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, AVAHI_PUBLISH_UNIQUE, r) < 0) { + avahi_log_warn("Failed to add HINFO RR: %s", avahi_strerror(s->error)); + return; + } } avahi_record_unref(r); diff --git a/avahi-core/util.c b/avahi-core/util.c index 41229c5..a5e9f52 100644 --- a/avahi-core/util.c +++ b/avahi-core/util.c @@ -101,3 +101,13 @@ char *avahi_strup(char *s) { return s; } + +char *avahi_strdown(char *s) { + char *c; + assert(s); + + for (c = s; *c; c++) + *c = (char) tolower(*c); + + return s; +} diff --git a/avahi-core/util.h b/avahi-core/util.h index aa38693..1a07363 100644 --- a/avahi-core/util.h +++ b/avahi-core/util.h @@ -35,6 +35,9 @@ char *avahi_format_mac_address(const uint8_t* mac, size_t size); /** Change every character in the string to upper case (ASCII), return a pointer to the string */ char *avahi_strup(char *s); +/** Change every character in the string to lower case (ASCII), return a pointer to the string */ +char *avahi_strdown(char *s); + AVAHI_C_DECL_END #endif