]> git.meshlink.io Git - catta/blob - avahi-core/domain-util.c
* deal with gethostname() failing, use utsname.sysname[] instead in that case
[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     return ret_s;
77 }
78
79 char *avahi_get_host_name_strdup(void) {
80     char t[AVAHI_DOMAIN_NAME_MAX];
81
82     if (!(avahi_get_host_name(t, sizeof(t))))
83         return NULL;
84
85     return avahi_strdup(t);
86 }
87
88 int avahi_binary_domain_cmp(const char *a, const char *b) {
89     assert(a);
90     assert(b);
91
92     if (a == b)
93         return 0;
94
95     for (;;) {
96         char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *p;
97         int r;
98
99         p = avahi_unescape_label(&a, ca, sizeof(ca));
100         assert(p);
101         p = avahi_unescape_label(&b, cb, sizeof(cb));
102         assert(p);
103
104         if ((r = strcmp(ca, cb)))
105             return r;
106         
107         if (!*a && !*b)
108             return 0;
109     }
110 }
111
112 int avahi_domain_ends_with(const char *domain, const char *suffix) {
113     assert(domain);
114     assert(suffix);
115
116     for (;;) {
117         char dummy[AVAHI_LABEL_MAX], *r;
118
119         if (*domain == 0)
120             return 0;
121         
122         if (avahi_domain_equal(domain, suffix))
123             return 1;
124
125         r = avahi_unescape_label(&domain, dummy, sizeof(dummy));
126         assert(r);
127     } 
128 }
129