2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/utsname.h>
32 #include <avahi-common/malloc.h>
35 #include "domain-util.h"
38 static void strip_bad_chars(char *s) {
41 s[strcspn(s, ".")] = 0;
43 for (p = s, d = s; *p; p++)
44 if ((*p >= 'a' && *p <= 'z') ||
45 (*p >= 'A' && *p <= 'Z') ||
46 (*p >= '0' && *p <= '9') ||
54 static int load_lsb_distrib_id(char *ret_s, size_t size) {
60 if (!(f = fopen("/etc/lsb-release", "r")))
66 if (!fgets(ln, sizeof(ln), f))
69 if (strncmp(ln, "DISTRIB_ID=", 11))
74 p[strcspn(p, "\"")] = 0;
76 snprintf(ret_s, size, "%s", p);
87 char *avahi_get_host_name(char *ret_s, size_t size) {
91 if (gethostname(ret_s, size) >= 0) {
93 strip_bad_chars(ret_s);
97 if (strcmp(ret_s, "localhost") == 0 || strncmp(ret_s, "localhost.", 10) == 0) {
99 avahi_log_warn("System host name is set to 'localhost'. This is not a suitable mDNS host name, looking for alternatives.");
103 /* No hostname was set, so let's take the OS name */
107 /* Try LSB distribution name first */
108 if (load_lsb_distrib_id(ret_s, size) >= 0) {
109 strip_bad_chars(ret_s);
110 avahi_strdown(ret_s);
117 /* Try uname() second */
118 struct utsname utsname;
120 if (uname(&utsname) >= 0) {
121 snprintf(ret_s, size, "%s", utsname.sysname);
122 strip_bad_chars(ret_s);
123 avahi_strdown(ret_s);
128 snprintf(ret_s, size, "unnamed");
132 if (size >= AVAHI_LABEL_MAX)
133 ret_s[AVAHI_LABEL_MAX-1] = 0;
138 char *avahi_get_host_name_strdup(void) {
139 char t[AVAHI_DOMAIN_NAME_MAX];
141 if (!(avahi_get_host_name(t, sizeof(t))))
144 return avahi_strdup(t);
147 int avahi_binary_domain_cmp(const char *a, const char *b) {
155 char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *p;
158 p = avahi_unescape_label(&a, ca, sizeof(ca));
160 p = avahi_unescape_label(&b, cb, sizeof(cb));
163 if ((r = strcmp(ca, cb)))
171 int avahi_domain_ends_with(const char *domain, const char *suffix) {
176 char dummy[AVAHI_LABEL_MAX], *r;
181 if (avahi_domain_equal(domain, suffix))
184 r = avahi_unescape_label(&domain, dummy, sizeof(dummy));