]> git.meshlink.io Git - catta/blob - libavahi-core/util.c
fa97eecedbcc3eb6cb33482c09a0eb6becb98955
[catta] / libavahi-core / 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 <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdio.h>
32
33 #include "util.h"
34
35 gchar *avahi_get_host_name(void) {
36 #ifdef HOST_NAME_MAX
37     char t[HOST_NAME_MAX];
38 #else
39     char t[256];
40 #endif
41     gethostname(t, sizeof(t));
42     t[sizeof(t)-1] = 0;
43     return avahi_normalize_name(t);
44 }
45
46 gchar *avahi_normalize_name(const gchar *s) {
47     size_t l;
48     g_assert(s);
49
50     l = strlen(s);
51
52     if (!l)
53         return g_strdup(".");
54
55     if (s[l-1] == '.')
56         return g_strdup(s);
57     
58     return g_strdup_printf("%s.", s);
59 }
60
61 gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
62     g_assert(a);
63     g_assert(b);
64
65     if (a->tv_sec < b->tv_sec)
66         return -1;
67
68     if (a->tv_sec > b->tv_sec)
69         return 1;
70
71     if (a->tv_usec < b->tv_usec)
72         return -1;
73
74     if (a->tv_usec > b->tv_usec)
75         return 1;
76
77     return 0;
78 }
79
80 glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
81     g_assert(a);
82     g_assert(b);
83
84     if (avahi_timeval_compare(a, b) < 0)
85         return avahi_timeval_diff(b, a);
86
87     return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
88 }
89
90
91 gint avahi_set_cloexec(gint fd) {
92     gint n;
93
94     g_assert(fd >= 0);
95     
96     if ((n = fcntl(fd, F_GETFD)) < 0)
97         return -1;
98
99     if (n & FD_CLOEXEC)
100         return 0;
101
102     return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
103 }
104
105 gint avahi_set_nonblock(gint fd) {
106     gint n;
107
108     g_assert(fd >= 0);
109
110     if ((n = fcntl(fd, F_GETFL)) < 0)
111         return -1;
112
113     if (n & O_NONBLOCK)
114         return 0;
115
116     return fcntl(fd, F_SETFL, n|O_NONBLOCK);
117 }
118
119 gint avahi_wait_for_write(gint fd) {
120     fd_set fds;
121     gint r;
122     
123     FD_ZERO(&fds);
124     FD_SET(fd, &fds);
125     
126     if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
127         g_message("select() failed: %s", strerror(errno));
128
129         return -1;
130     }
131     
132     g_assert(r > 0);
133
134     return 0;
135 }
136
137 GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
138     g_assert(tv);
139
140     g_get_current_time(tv);
141
142     if (msec)
143         g_time_val_add(tv, msec*1000);
144
145     if (jitter)
146         g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
147         
148     return tv;
149 }
150
151 gint avahi_age(const GTimeVal *a) {
152     GTimeVal now;
153     
154     g_assert(a);
155
156     g_get_current_time(&now);
157
158     return avahi_timeval_diff(&now, a);
159 }
160
161 gboolean avahi_domain_cmp(const gchar *a, const gchar *b) {
162     int escaped_a = 0, escaped_b = 0;
163     g_assert(a);
164     g_assert(b);
165
166     for (;;) {
167         /* Check for escape characters "\" */
168         if ((escaped_a = *a == '\\'))
169             a ++;
170
171         if ((escaped_b = *b == '\\'))
172             b++;
173
174         /* Check for string end */
175         if (*a == 0 && *b == 0)
176             return 0;
177         
178         if (*a == 0 && !escaped_b && *b == '.' && *(b+1) == 0)
179             return 0;
180
181         if (!escaped_a && *a == '.' && *(a+1) == 0 && *b == 0)
182             return 0;
183
184         /* Compare characters */
185         if (escaped_a == escaped_b && *a != *b)
186             return *a < *b ? -1 : 1;
187
188         /* Next characters */
189         a++;
190         b++;
191         
192     }
193 }
194
195 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
196     return avahi_domain_cmp(a, b) == 0;
197 }
198
199 guint avahi_domain_hash(const gchar *p) {
200     char t[256];
201     strncpy(t, p, sizeof(t)-1);
202     t[sizeof(t)-1] = 0;
203
204     return g_int_hash(t);
205 }
206
207 void avahi_hexdump(gconstpointer p, guint size) {
208     const guint8 *c = p;
209     g_assert(p);
210
211     printf("Dumping %u bytes from %p:\n", size, p);
212     
213     while (size > 0) {
214         guint i;
215
216         for (i = 0; i < 16; i++) { 
217             if (i < size)
218                 printf("%02x ", c[i]);
219             else
220                 printf("   ");
221         }
222
223         for (i = 0; i < 16; i++) {
224             if (i < size)
225                 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
226             else
227                 printf(" ");
228         }
229         
230         printf("\n");
231
232         c += 16;
233
234         if (size <= 16)
235             break;
236         
237         size -= 16;
238     }
239 }