4 This file is part of avahi.
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.
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.
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
37 gchar *avahi_get_host_name(void) {
39 char t[HOST_NAME_MAX];
43 gethostname(t, sizeof(t));
45 return avahi_normalize_name(t);
48 static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) {
49 gboolean escaped = FALSE;
57 if (!escaped && *src == '\\')
59 else if (escaped && (*src == '.' || *src == '\\')) {
61 if ((size -= 2) <= 1) break;
67 if (--size <= 1) break;
80 gchar *avahi_normalize_name(const gchar *s) {
86 unescape_uneeded(s, tmp, sizeof(tmp));
88 n = g_utf8_normalize(tmp, -1, G_NORMALIZE_DEFAULT);
90 if ((l = strlen(n)) == 0) {
98 t = g_strdup_printf("%s.", n);
103 gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
107 if (a->tv_sec < b->tv_sec)
110 if (a->tv_sec > b->tv_sec)
113 if (a->tv_usec < b->tv_usec)
116 if (a->tv_usec > b->tv_usec)
122 glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
126 if (avahi_timeval_compare(a, b) < 0)
127 return avahi_timeval_diff(b, a);
129 return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
133 gint avahi_set_cloexec(gint fd) {
138 if ((n = fcntl(fd, F_GETFD)) < 0)
144 return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
147 gint avahi_set_nonblock(gint fd) {
152 if ((n = fcntl(fd, F_GETFL)) < 0)
158 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
161 gint avahi_wait_for_write(gint fd) {
168 if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
169 g_message("select() failed: %s", strerror(errno));
179 GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
182 g_get_current_time(tv);
185 g_time_val_add(tv, msec*1000);
188 g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
193 glong avahi_age(const GTimeVal *a) {
198 g_get_current_time(&now);
200 return avahi_timeval_diff(&now, a);
203 /* Read the first label from string *name, unescape "\" and write it to dest */
204 gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
229 if (**name == '\\') {
236 *(d++) = *((*name) ++);
247 /* Escape "\" and ".", append \0 */
248 gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size) {
255 g_assert(*ret_size > 0);
259 while (src_length > 0) {
260 if (*src == '.' || *src == '\\') {
264 *((*ret_name) ++) = '\\';
271 *((*ret_name)++) = *src;
283 static gint utf8_strcasecmp(const gchar *a, const gchar *b) {
290 ta = g_utf8_casefold(a, -1);
291 tb = g_utf8_casefold(b, -1);
292 r = g_utf8_collate(ta, tb);
299 gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
307 gchar ca[65], cb[65], *pa, *pb;
309 pa = avahi_unescape_label(&a, ca, sizeof(ca));
310 pb = avahi_unescape_label(&b, cb, sizeof(cb));
314 else if ((pa && !pb) || (!pa && pb))
317 if (utf8_strcasecmp(pa, pb))
324 gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) {
332 gchar ca[65], cb[65], *pa, *pb;
335 pa = avahi_unescape_label(&a, ca, sizeof(ca));
336 pb = avahi_unescape_label(&b, cb, sizeof(cb));
345 if ((r = strcmp(pa, pb)))
350 void avahi_hexdump(gconstpointer p, guint size) {
354 printf("Dumping %u bytes from %p:\n", size, p);
359 for (i = 0; i < 16; i++) {
361 printf("%02x ", c[i]);
366 for (i = 0; i < 16; i++) {
368 printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
384 gint avahi_domain_hash(const gchar *s) {
390 if (!avahi_unescape_label(&s, c, sizeof(c)))
396 n = g_utf8_normalize(c, -1, G_NORMALIZE_DEFAULT);
397 m = g_utf8_strdown(n, -1);
399 hash += g_str_hash(m);