]> git.meshlink.io Git - catta/blob - avahi-core/domain-util.c
strip characters that are not in the set [a-zA-Z0-9-] from the local host name before...
[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 ((*p >= 'a' && *p <= 'z') ||
46             (*p >= 'A' && *p <= 'Z') ||
47             (*p >= '0' && *p <= '9') ||
48             *p == '-')
49             *(d++) = *p;
50
51     *d = 0;
52 }
53
54 char *avahi_get_host_name(char *ret_s, size_t size) {
55     assert(ret_s);
56     assert(size > 0);
57
58     if (gethostname(ret_s, size) >= 0) {
59         ret_s[size-1] = 0;
60         strip_bad_chars(ret_s);
61     } else
62         *ret_s = 0;
63
64     if (*ret_s == 0) {
65         struct utsname utsname;
66         
67         /* No hostname was set, so let's take the OS name */
68
69         if (uname(&utsname) >= 0) {
70             snprintf(ret_s, size, "%s", utsname.sysname);
71             strip_bad_chars(ret_s);
72             avahi_strdown(ret_s);
73         }
74
75         if (*ret_s == 0)
76             snprintf(ret_s, size, "unnamed");
77     }
78
79     if (size >= AVAHI_LABEL_MAX)
80         ret_s[AVAHI_LABEL_MAX-1] = 0;    
81     
82     return ret_s;
83 }
84
85 char *avahi_get_host_name_strdup(void) {
86     char t[AVAHI_DOMAIN_NAME_MAX];
87
88     if (!(avahi_get_host_name(t, sizeof(t))))
89         return NULL;
90
91     return avahi_strdup(t);
92 }
93
94 int avahi_binary_domain_cmp(const char *a, const char *b) {
95     assert(a);
96     assert(b);
97
98     if (a == b)
99         return 0;
100
101     for (;;) {
102         char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *p;
103         int r;
104
105         p = avahi_unescape_label(&a, ca, sizeof(ca));
106         assert(p);
107         p = avahi_unescape_label(&b, cb, sizeof(cb));
108         assert(p);
109
110         if ((r = strcmp(ca, cb)))
111             return r;
112         
113         if (!*a && !*b)
114             return 0;
115     }
116 }
117
118 int avahi_domain_ends_with(const char *domain, const char *suffix) {
119     assert(domain);
120     assert(suffix);
121
122     for (;;) {
123         char dummy[AVAHI_LABEL_MAX], *r;
124
125         if (*domain == 0)
126             return 0;
127         
128         if (avahi_domain_equal(domain, suffix))
129             return 1;
130
131         r = avahi_unescape_label(&domain, dummy, sizeof(dummy));
132         assert(r);
133     } 
134 }
135