summaryrefslogtreecommitdiffstats
path: root/avahi-common/util.c
blob: a977bb2a04f144666b71735d72a5edc9d254e572 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
/* $Id$ */

/***
  This file is part of avahi.
 
  avahi is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2.1 of the
  License, or (at your option) any later version.
 
  avahi is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
  Public License for more details.
 
  You should have received a copy of the GNU Lesser General Public
  License along with avahi; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  USA.
***/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>

#include "util.h"

gchar *avahi_get_host_name(void) {
#ifdef HOST_NAME_MAX
    char t[HOST_NAME_MAX];
#else
    char t[256];
#endif
    gethostname(t, sizeof(t));
    t[sizeof(t)-1] = 0;
    return avahi_normalize_name(t);
}

static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) {
    gboolean escaped = FALSE;
    
    g_assert(src);
    g_assert(ret_dest);
    g_assert(size > 0);
    
    for (; *src; src++) {

        if (!escaped && *src == '\\')
            escaped = TRUE;
        else if (escaped && (*src == '.' || *src == '\\')) {

            if ((size -= 2) <= 1) break;
            
            *(ret_dest++) = '\\';
            *(ret_dest++) = *src;
            escaped = FALSE;
        } else {
            if (--size <= 1) break;

            *(ret_dest++) = *src;
            escaped = FALSE;
        }

    }

    *ret_dest = 0;
    
    return ret_dest;
}

gchar *avahi_normalize_name(const gchar *s) {
    gchar tmp[256];
    guint l;
    g_assert(s);

    unescape_uneeded(s, tmp, sizeof(tmp));

    l = strlen(tmp);

    while (l > 0 && tmp[l-1] == '.')
        tmp[--l] = 0;

    return g_strdup(tmp);
}

gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
    g_assert(a);
    g_assert(b);

    if (a->tv_sec < b->tv_sec)
        return -1;

    if (a->tv_sec > b->tv_sec)
        return 1;

    if (a->tv_usec < b->tv_usec)
        return -1;

    if (a->tv_usec > b->tv_usec)
        return 1;

    return 0;
}

AvahiUsec avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
    g_assert(a);
    g_assert(b);

    if (avahi_timeval_compare(a, b) < 0)
        return - avahi_timeval_diff(b, a);

    return ((AvahiUsec) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
}

GTimeVal* avahi_timeval_add(GTimeVal *a, AvahiUsec usec) {
    AvahiUsec u;
    g_assert(a);

    u = usec + a->tv_usec;

    if (u < 0) {
        a->tv_usec = (glong) (1000000 + (u % 1000000));
        a->tv_sec += (glong) (-1 + (u / 1000000));
    } else {
        a->tv_usec = (glong) (u % 1000000);
        a->tv_sec += (glong) (u / 1000000);
    }

    return a;
}

AvahiUsec avahi_age(const GTimeVal *a) {
    GTimeVal now;
    
    g_assert(a);

    g_get_current_time(&now);

    return avahi_timeval_diff(&now, a);
}

GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
    g_assert(tv);

    g_get_current_time(tv);

    if (msec)
        avahi_timeval_add(tv, (AvahiUsec) msec*1000);

    if (jitter)
        avahi_timeval_add(tv, (AvahiUsec) g_random_int_range(0, jitter) * 1000);
        
    return tv;
}

gint avahi_set_cloexec(gint fd) {
    gint n;

    g_assert(fd >= 0);
    
    if ((n = fcntl(fd, F_GETFD)) < 0)
        return -1;

    if (n & FD_CLOEXEC)
        return 0;

    return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
}

gint avahi_set_nonblock(gint fd) {
    gint n;

    g_assert(fd >= 0);

    if ((n = fcntl(fd, F_GETFL)) < 0)
        return -1;

    if (n & O_NONBLOCK)
        return 0;

    return fcntl(fd, F_SETFL, n|O_NONBLOCK);
}

gint avahi_wait_for_write(gint fd) {
    fd_set fds;
    gint r;
    
    FD_ZERO(&fds);
    FD_SET(fd, &fds);
    
    if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
        g_message("select() failed: %s", strerror(errno));

        return -1;
    }
    
    g_assert(r > 0);

    return 0;
}

/* Read the first label from string *name, unescape "\" and write it to dest */
gchar *avahi_unescape_label(const gchar **name, gchar *dest, guint size) {
    guint i = 0;
    gchar *d;
    
    g_assert(dest);
    g_assert(size > 0);
    g_assert(name);

    if (!**name)
        return NULL;

    d = dest;
    
    for (;;) {
        if (i >= size)
            return NULL;

        if (**name == '.') {
            (*name)++;
            break;
        }
        
        if (**name == 0)
            break;
        
        if (**name == '\\') {
            (*name) ++;
            
            if (**name == 0)
                break;
        }
        
        *(d++) = *((*name) ++);
        i++;
    }

    g_assert(i < size);

    *d = 0;

    return dest;
}

/* Escape "\" and ".", append \0 */
gchar *avahi_escape_label(const guint8* src, guint src_length, gchar **ret_name, guint *ret_size) {
    gchar *r;

    g_assert(src);
    g_assert(ret_name);
    g_assert(*ret_name);
    g_assert(ret_size);
    g_assert(*ret_size > 0);

    r = *ret_name;

    while (src_length > 0) {
        if (*src == '.' || *src == '\\') {
            if (*ret_size < 3)
                return NULL;
            
            *((*ret_name) ++) = '\\';
            (*ret_size) --;
        }

        if (*ret_size < 2)
            return NULL;
        
        *((*ret_name)++) = *src;
        (*ret_size) --;

        src_length --;
        src++;
    }

    **ret_name = 0;

    return r;
}

gboolean avahi_domain_equal(const gchar *a, const gchar *b) {
    g_assert(a);
    g_assert(b);

    if (a == b)
        return TRUE;
    
    for (;;) {
        gchar ca[65], cb[65], *pa, *pb;

        pa = avahi_unescape_label(&a, ca, sizeof(ca));
        pb = avahi_unescape_label(&b, cb, sizeof(cb));

        if (!pa && !pb)
            return TRUE;
        else if ((pa && !pb) || (!pa && pb))
            return FALSE;

        if (g_ascii_strcasecmp(pa, pb))
            return FALSE;
    }

    return TRUE;
}

gint avahi_binary_domain_cmp(const gchar *a, const gchar *b) {
    g_assert(a);
    g_assert(b);

    if (a == b)
        return 0;

    for (;;) {
        gchar ca[65], cb[65], *pa, *pb;
        gint r;

        pa = avahi_unescape_label(&a, ca, sizeof(ca));
        pb = avahi_unescape_label(&b, cb, sizeof(cb));

        if (!pa && !pb)
            return 0;
        else if (pa && !pb)
            return 1;
        else if (!pa && pb)
            return -1;
        
        if ((r = strcmp(pa, pb)))
            return r;
    }
}

void avahi_hexdump(gconstpointer p, guint size) {
    const guint8 *c = p;
    g_assert(p);

    printf("Dumping %u bytes from %p:\n", size, p);
    
    while (size > 0) {
        guint i;

        for (i = 0; i < 16; i++) { 
            if (i < size)
                printf("%02x ", c[i]);
            else
                printf("   ");
        }

        for (i = 0; i < 16; i++) {
            if (i < size)
                printf("%c", c[i] >= 32 && c[i] < 127 ? c[i] : '.');
            else
                printf(" ");
        }
        
        printf("\n");

        c += 16;

        if (size <= 16)
            break;
        
        size -= 16;
    }
}

guint avahi_domain_hash(const gchar *s) {
    guint hash = 0;
    
    for (;;) {
        gchar c[65], *m;

        if (!avahi_unescape_label(&s, c, sizeof(c)))
            return hash;

        if (!c[0])
            continue;
        
        m = g_ascii_strdown(c, -1);
        hash += g_str_hash(m);
        g_free(m);
    }
}

gchar *avahi_format_mac_address(const guint8* mac, guint size) {
    gchar *r, *t;
    guint i;
    static const gchar hex[] = "0123456789abcdef";

    t = r = g_new(gchar, size > 0 ? size*3 : 1);

    if (size <= 0) {
        *r = 0;
        return r;
    }
    
    for (i = 0; i < size; i++) {
        *(t++) = hex[*mac >> 4];
        *(t++) = hex[*mac & 0xF];
        *(t++) = ':';

        mac++;
    }

    *(--t) = 0;
    return r;
}

gboolean avahi_valid_service_type(const gchar *t) {
    const gchar *p;
    g_assert(t);

    if (strlen(t) < 5)
        return FALSE;
    
    if (*t != '_')
        return FALSE;

    if (!(p = strchr(t, '.')))
        return FALSE;

    if (p - t > 63 || p - t < 2)
        return FALSE;

    if (*(++p) != '_')
        return FALSE;

    if (strchr(p, '.'))
        return FALSE;

    if (strlen(p) > 63 || strlen(p) < 2)
        return FALSE;
    
    return TRUE;
}

gboolean avahi_valid_domain_name(const gchar *t) {
    const gchar *p, *dp;
    gboolean dot = FALSE;
        
    g_assert(t);

    if (*t == 0)
        return FALSE;

    /* Domains may not start with a dot */
    if (*t == '.')
        return FALSE;

    dp = t; 

    for (p = t; *p; p++) {

        if (*p == '.') {
            if (dot) /* Two subsequent dots */
                return FALSE;

            if (p - dp > 63)
                return FALSE;

            dot = TRUE;
            dp = p + 1;
        } else
            dot = FALSE;

    }

    if (p - dp > 63)
        return FALSE;

    /* A trailing dot IS allowed */
    
    return TRUE;
}

gboolean avahi_valid_service_name(const gchar *t) {
    g_assert(t);

    if (*t == 0)
        return FALSE;

    if (strlen(t) > 63)
        return FALSE;

    return TRUE;
}

gboolean avahi_valid_host_name(const gchar *t) {
    g_assert(t);

    if (*t == 0)
        return FALSE;

    if (strlen(t) > 63)
        return FALSE;

    if (strchr(t, '.'))
        return FALSE;

    return TRUE;
}