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