4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <sys/utsname.h>
34 #include <avahi-common/malloc.h>
37 #include "domain-util.h"
40 static void strip_bad_chars(char *s) {
43 s[strcspn(s, ".")] = 0;
45 for (p = s, d = s; *p; p++)
46 if ((*p >= 'a' && *p <= 'z') ||
47 (*p >= 'A' && *p <= 'Z') ||
48 (*p >= '0' && *p <= '9') ||
56 static int load_lsb_distrib_id(char *ret_s, size_t size) {
62 if (!(f = fopen("/etc/lsb-release", "r")))
68 if (!fgets(ln, sizeof(ln), f))
71 if (strncmp(ln, "DISTRIB_ID=", 11))
76 p[strcspn(p, "\"")] = 0;
78 snprintf(ret_s, size, "%s", p);
89 char *avahi_get_host_name(char *ret_s, size_t size) {
93 if (gethostname(ret_s, size) >= 0) {
95 strip_bad_chars(ret_s);
99 if (strcmp(ret_s, "localhost") == 0 || strncmp(ret_s, "localhost.", 10) == 0) {
101 avahi_log_warn("System host name is set to 'localhost'. This is not a suitable mDNS host name, looking for alternatives.");
105 /* No hostname was set, so let's take the OS name */
109 /* Try LSB distribution name first */
110 if (load_lsb_distrib_id(ret_s, size) >= 0) {
111 strip_bad_chars(ret_s);
112 avahi_strdown(ret_s);
119 /* Try uname() second */
120 struct utsname utsname;
122 if (uname(&utsname) >= 0) {
123 snprintf(ret_s, size, "%s", utsname.sysname);
124 strip_bad_chars(ret_s);
125 avahi_strdown(ret_s);
130 snprintf(ret_s, size, "unnamed");
134 if (size >= AVAHI_LABEL_MAX)
135 ret_s[AVAHI_LABEL_MAX-1] = 0;
140 char *avahi_get_host_name_strdup(void) {
141 char t[AVAHI_DOMAIN_NAME_MAX];
143 if (!(avahi_get_host_name(t, sizeof(t))))
146 return avahi_strdup(t);
149 int avahi_binary_domain_cmp(const char *a, const char *b) {
157 char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *p;
160 p = avahi_unescape_label(&a, ca, sizeof(ca));
162 p = avahi_unescape_label(&b, cb, sizeof(cb));
165 if ((r = strcmp(ca, cb)))
173 int avahi_domain_ends_with(const char *domain, const char *suffix) {
178 char dummy[AVAHI_LABEL_MAX], *r;
183 if (avahi_domain_equal(domain, suffix))
186 r = avahi_unescape_label(&domain, dummy, sizeof(dummy));