#include <assert.h>
#include <string.h>
#include <unistd.h>
+#include <ctype.h>
+#include <sys/utsname.h>
+#include <stdio.h>
#include <avahi-common/malloc.h>
#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) {
/* 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);
return s;
}
+
+char *avahi_strdown(char *s) {
+ char *c;
+ assert(s);
+
+ for (c = s; *c; c++)
+ *c = (char) tolower(*c);
+
+ return s;
+}
/** 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