]> git.meshlink.io Git - catta/blob - avahi-core/domain-util.c
make sure that the local hostname passes the avahi_is_valid_domain_name() check.
[catta] / avahi-core / domain-util.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <ctype.h>
31 #include <sys/utsname.h>
32 #include <stdio.h>
33
34 #include <avahi-common/malloc.h>
35
36 #include "domain-util.h"
37 #include "util.h"
38
39 static void strip_bad_chars(char *s) {
40     char *p, *d;
41
42     s[strcspn(s, ".")] = 0;
43     
44     for (p = s, d = s; *p; p++) 
45         if (isalnum(*p) || *p == '-')
46             *(d++) = *p;
47
48     *d = 0;
49 }
50
51 char *avahi_get_host_name(char *ret_s, size_t size) {
52     assert(ret_s);
53     assert(size > 0);
54
55     if (gethostname(ret_s, size) >= 0) {
56         ret_s[size-1] = 0;
57         strip_bad_chars(ret_s);
58     } else
59         *ret_s = 0;
60
61     if (*ret_s == 0) {
62         struct utsname utsname;
63         
64         /* No hostname was set, so let's take the OS name */
65
66         if (uname(&utsname) >= 0) {
67             snprintf(ret_s, size, "%s", utsname.sysname);
68             strip_bad_chars(ret_s);
69             avahi_strdown(ret_s);
70         }
71
72         if (*ret_s == 0)
73             snprintf(ret_s, size, "unnamed");
74     }
75
76     if (size >= AVAHI_LABEL_MAX)
77         ret_s[AVAHI_LABEL_MAX-1] = 0;    
78     
79     return ret_s;
80 }
81
82 char *avahi_get_host_name_strdup(void) {
83     char t[AVAHI_DOMAIN_NAME_MAX];
84
85     if (!(avahi_get_host_name(t, sizeof(t))))
86         return NULL;
87
88     return avahi_strdup(t);
89 }
90
91 int avahi_binary_domain_cmp(const char *a, const char *b) {
92     assert(a);
93     assert(b);
94
95     if (a == b)
96         return 0;
97
98     for (;;) {
99         char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *p;
100         int r;
101
102         p = avahi_unescape_label(&a, ca, sizeof(ca));
103         assert(p);
104         p = avahi_unescape_label(&b, cb, sizeof(cb));
105         assert(p);
106
107         if ((r = strcmp(ca, cb)))
108             return r;
109         
110         if (!*a && !*b)
111             return 0;
112     }
113 }
114
115 int avahi_domain_ends_with(const char *domain, const char *suffix) {
116     assert(domain);
117     assert(suffix);
118
119     for (;;) {
120         char dummy[AVAHI_LABEL_MAX], *r;
121
122         if (*domain == 0)
123             return 0;
124         
125         if (avahi_domain_equal(domain, suffix))
126             return 1;
127
128         r = avahi_unescape_label(&domain, dummy, sizeof(dummy));
129         assert(r);
130     } 
131 }
132