summaryrefslogtreecommitdiffstats
path: root/avahi-core/rr.c
blob: 57ddca4e792073f597af64efddd10d98a3299b87 (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
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/* $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 <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <assert.h>

#include <avahi-common/domain.h>
#include <avahi-common/malloc.h>
#include <avahi-common/defs.h>

#include "rr.h"
#include "log.h"
#include "util.h"
#include "hashmap.h"
#include "domain-util.h"
#include "rr-util.h"

AvahiKey *avahi_key_new(const char *name, uint16_t class, uint16_t type) {
    AvahiKey *k;
    assert(name);

    if (!(k = avahi_new(AvahiKey, 1))) {
        avahi_log_error("avahi_new() failed.");
        return NULL;
    }
    
    if (!(k->name = avahi_normalize_name_strdup(name))) {
        avahi_log_error("avahi_normalize_name() failed.");
        avahi_free(k);
        return NULL;
    }
    
    k->ref = 1;
    k->clazz = class;
    k->type = type;

    return k;
}

AvahiKey *avahi_key_new_cname(AvahiKey *key) {
    assert(key);

    if (key->clazz != AVAHI_DNS_CLASS_IN)
        return NULL;

    if (key->type == AVAHI_DNS_TYPE_CNAME)
        return NULL;

    return avahi_key_new(key->name, key->clazz, AVAHI_DNS_TYPE_CNAME);
}

AvahiKey *avahi_key_ref(AvahiKey *k) {
    assert(k);
    assert(k->ref >= 1);

    k->ref++;

    return k;
}

void avahi_key_unref(AvahiKey *k) {
    assert(k);
    assert(k->ref >= 1);
    
    if ((--k->ref) <= 0) {
        avahi_free(k->name);
        avahi_free(k);
    }
}

AvahiRecord *avahi_record_new(AvahiKey *k, uint32_t ttl) {
    AvahiRecord *r;
    
    assert(k);
    
    if (!(r = avahi_new(AvahiRecord, 1))) {
        avahi_log_error("avahi_new() failed.");
        return NULL;
    }
        
    r->ref = 1;
    r->key = avahi_key_ref(k);

    memset(&r->data, 0, sizeof(r->data));

    r->ttl = ttl != (uint32_t) -1 ? ttl : AVAHI_DEFAULT_TTL;

    return r;
}

AvahiRecord *avahi_record_new_full(const char *name, uint16_t class, uint16_t type, uint32_t ttl) {
    AvahiRecord *r;
    AvahiKey *k;

    assert(name);
    
    if (!(k = avahi_key_new(name, class, type))) {
        avahi_log_error("avahi_key_new() failed.");
        return NULL;
    }

    r = avahi_record_new(k, ttl);
    avahi_key_unref(k);

    if (!r) {
        avahi_log_error("avahi_record_new() failed.");
        return NULL;
    }

    return r;
}

AvahiRecord *avahi_record_ref(AvahiRecord *r) {
    assert(r);
    assert(r->ref >= 1);

    r->ref++;
    return r;
}

void avahi_record_unref(AvahiRecord *r) {
    assert(r);
    assert(r->ref >= 1);

    if ((--r->ref) <= 0) {
        switch (r->key->type) {

            case AVAHI_DNS_TYPE_SRV:
                avahi_free(r->data.srv.name);
                break;

            case AVAHI_DNS_TYPE_PTR:
            case AVAHI_DNS_TYPE_CNAME:
            case AVAHI_DNS_TYPE_NS:
                avahi_free(r->data.ptr.name);
                break;

            case AVAHI_DNS_TYPE_HINFO:
                avahi_free(r->data.hinfo.cpu);
                avahi_free(r->data.hinfo.os);
                break;

            case AVAHI_DNS_TYPE_TXT:
                avahi_string_list_free(r->data.txt.string_list);
                break;

            case AVAHI_DNS_TYPE_A:
            case AVAHI_DNS_TYPE_AAAA:
                break;
            
            default:
                avahi_free(r->data.generic.data);
        }
        
        avahi_key_unref(r->key);
        avahi_free(r);
    }
}

const char *avahi_dns_class_to_string(uint16_t class) {
    if (class & AVAHI_DNS_CACHE_FLUSH) 
        return "FLUSH";

    switch (class) {
        case AVAHI_DNS_CLASS_IN:
            return "IN";
        case AVAHI_DNS_CLASS_ANY:
            return "ANY";
        default:
            return NULL;
    }
}

const char *avahi_dns_type_to_string(uint16_t type) {
    switch (type) {
        case AVAHI_DNS_TYPE_CNAME:
            return "CNAME";
        case AVAHI_DNS_TYPE_A:
            return "A";
        case AVAHI_DNS_TYPE_AAAA:
            return "AAAA";
        case AVAHI_DNS_TYPE_PTR:
            return "PTR";
        case AVAHI_DNS_TYPE_HINFO:
            return "HINFO";
        case AVAHI_DNS_TYPE_TXT:
            return "TXT";
        case AVAHI_DNS_TYPE_SRV:
            return "SRV";
        case AVAHI_DNS_TYPE_ANY:
            return "ANY";
        case AVAHI_DNS_TYPE_SOA:
            return "SOA";
        case AVAHI_DNS_TYPE_NS:
            return "NS";
        default:
            return NULL;
    }
}

char *avahi_key_to_string(const AvahiKey *k) {
    char class[16], type[16];
    const char *c, *t;
    
    assert(k);
    assert(k->ref >= 1);

    /* According to RFC3597 */
    
    if (!(c = avahi_dns_class_to_string(k->clazz))) {
        snprintf(class, sizeof(class), "CLASS%u", k->clazz);
        c = class;
    }

    if (!(t = avahi_dns_type_to_string(k->type))) {
        snprintf(type, sizeof(type), "TYPE%u", k->type);
        t = type;
    }
    
    return avahi_strdup_printf("%s\t%s\t%s", k->name, c, t);
}

char *avahi_record_to_string(const AvahiRecord *r) {
    char *p, *s;
    char buf[1024], *t = NULL, *d = NULL;

    assert(r);
    assert(r->ref >= 1);
    
    switch (r->key->type) {
        case AVAHI_DNS_TYPE_A:
            inet_ntop(AF_INET, &r->data.a.address.address, t = buf, sizeof(buf));
            break;
            
        case AVAHI_DNS_TYPE_AAAA:
            inet_ntop(AF_INET6, &r->data.aaaa.address.address, t = buf, sizeof(buf));
            break;
            
        case AVAHI_DNS_TYPE_PTR:
        case AVAHI_DNS_TYPE_CNAME:
        case AVAHI_DNS_TYPE_NS:

            t = r->data.ptr.name;
            break;

        case AVAHI_DNS_TYPE_TXT:
            t = d = avahi_string_list_to_string(r->data.txt.string_list);
            break;

        case AVAHI_DNS_TYPE_HINFO:

            snprintf(t = buf, sizeof(buf), "\"%s\" \"%s\"", r->data.hinfo.cpu, r->data.hinfo.os);
            break;

        case AVAHI_DNS_TYPE_SRV:

            snprintf(t = buf, sizeof(buf), "%u %u %u %s",
                     r->data.srv.priority,
                     r->data.srv.weight,
                     r->data.srv.port,
                     r->data.srv.name);

            break;

        default: {

            uint8_t *c;
            uint16_t n;
            int i;
            char *e;

            /* According to RFC3597 */
            
            snprintf(t = buf, sizeof(buf), "\\# %u", r->data.generic.size);

            e = strchr(t, 0);
            
            for (c = r->data.generic.data, n = r->data.generic.size, i = 0;
                 n > 0 && i < 20;
                 c ++, n --) {

                sprintf(e, " %02X", *c);
                e = strchr(e, 0);
            }

            break;
        }
    }

    p = avahi_key_to_string(r->key);
    s = avahi_strdup_printf("%s %s ; ttl=%u", p, t, r->ttl);
    avahi_free(p);
    avahi_free(d);
    
    return s;
}

int avahi_key_equal(const AvahiKey *a, const AvahiKey *b) {
    assert(a);
    assert(b);

    if (a == b)
        return 1;
    
    return avahi_domain_equal(a->name, b->name) &&
        a->type == b->type &&
        a->clazz == b->clazz;
}

int avahi_key_pattern_match(const AvahiKey *pattern, const AvahiKey *k) {
    assert(pattern);
    assert(k);

    assert(!avahi_key_is_pattern(k));

    if (pattern == k)
        return 1;
    
    return avahi_domain_equal(pattern->name, k->name) &&
        (pattern->type == k->type || pattern->type == AVAHI_DNS_TYPE_ANY) &&
        (pattern->clazz == k->clazz || pattern->clazz == AVAHI_DNS_CLASS_ANY);
}

int avahi_key_is_pattern(const AvahiKey *k) {
    assert(k);

    return
        k->type == AVAHI_DNS_TYPE_ANY ||
        k->clazz == AVAHI_DNS_CLASS_ANY;
}

unsigned avahi_key_hash(const AvahiKey *k) {
    assert(k);

    return
        avahi_domain_hash(k->name) + 
        k->type +
        k->clazz;
}

static int rdata_equal(const AvahiRecord *a, const AvahiRecord *b) {
    assert(a);
    assert(b);
    assert(a->key->type == b->key->type);

    switch (a->key->type) {
        case AVAHI_DNS_TYPE_SRV:
            return
                a->data.srv.priority == b->data.srv.priority &&
                a->data.srv.weight == b->data.srv.weight &&
                a->data.srv.port == b->data.srv.port &&
                avahi_domain_equal(a->data.srv.name, b->data.srv.name);

        case AVAHI_DNS_TYPE_PTR:
        case AVAHI_DNS_TYPE_CNAME:
        case AVAHI_DNS_TYPE_NS:
            return avahi_domain_equal(a->data.ptr.name, b->data.ptr.name);

        case AVAHI_DNS_TYPE_HINFO:
            return
                !strcmp(a->data.hinfo.cpu, b->data.hinfo.cpu) &&
                !strcmp(a->data.hinfo.os, b->data.hinfo.os);

        case AVAHI_DNS_TYPE_TXT:
            return avahi_string_list_equal(a->data.txt.string_list, b->data.txt.string_list);

        case AVAHI_DNS_TYPE_A:
            return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address)) == 0;

        case AVAHI_DNS_TYPE_AAAA:
            return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address)) == 0;

        default:
            return a->data.generic.size == b->data.generic.size &&
                (a->data.generic.size == 0 || memcmp(a->data.generic.data, b->data.generic.data, a->data.generic.size) == 0);
    }
    
}

int avahi_record_equal_no_ttl(const AvahiRecord *a, const AvahiRecord *b) {
    assert(a);
    assert(b);

    if (a == b)
        return 1;

    return
        avahi_key_equal(a->key, b->key) &&
        rdata_equal(a, b);
}


AvahiRecord *avahi_record_copy(AvahiRecord *r) {
    AvahiRecord *copy;

    if (!(copy = avahi_new(AvahiRecord, 1))) {
        avahi_log_error("avahi_new() failed.");
        return NULL;
    }
    
    copy->ref = 1;
    copy->key = avahi_key_ref(r->key);
    copy->ttl = r->ttl;

    switch (r->key->type) {
        case AVAHI_DNS_TYPE_PTR:
        case AVAHI_DNS_TYPE_CNAME:
        case AVAHI_DNS_TYPE_NS:
            if (!(copy->data.ptr.name = avahi_strdup(r->data.ptr.name)))
                goto fail;
            break;

        case AVAHI_DNS_TYPE_SRV:
            copy->data.srv.priority = r->data.srv.priority;
            copy->data.srv.weight = r->data.srv.weight;
            copy->data.srv.port = r->data.srv.port;
            if (!(copy->data.srv.name = avahi_strdup(r->data.srv.name)))
                goto fail;
            break;

        case AVAHI_DNS_TYPE_HINFO:
            if (!(copy->data.hinfo.os = avahi_strdup(r->data.hinfo.os)))
                goto fail;

            if (!(copy->data.hinfo.cpu = avahi_strdup(r->data.hinfo.cpu))) {
                avahi_free(r->data.hinfo.os);
                goto fail;
            }
            break;

        case AVAHI_DNS_TYPE_TXT:
            copy->data.txt.string_list = avahi_string_list_copy(r->data.txt.string_list);
            break;

        case AVAHI_DNS_TYPE_A:
            copy->data.a.address = r->data.a.address;
            break;

        case AVAHI_DNS_TYPE_AAAA:
            copy->data.aaaa.address = r->data.aaaa.address;
            break;

        default:
            if (!(copy->data.generic.data = avahi_memdup(r->data.generic.data, r->data.generic.size)))
                goto fail;
            copy->data.generic.size = r->data.generic.size;
            break;
                
    }

    return copy;

fail:
    avahi_log_error("Failed to allocate memory");

    avahi_key_unref(copy->key);
    avahi_free(copy);
    
    return NULL;
}


size_t avahi_key_get_estimate_size(AvahiKey *k) {
    assert(k);

    return strlen(k->name)+1+4;
}

size_t avahi_record_get_estimate_size(AvahiRecord *r) {
    size_t n;
    assert(r);

    n = avahi_key_get_estimate_size(r->key) + 4 + 2;

    switch (r->key->type) {
        case AVAHI_DNS_TYPE_PTR:
        case AVAHI_DNS_TYPE_CNAME:
        case AVAHI_DNS_TYPE_NS:
            n += strlen(r->data.ptr.name) + 1;
            break;

        case AVAHI_DNS_TYPE_SRV:
            n += 6 + strlen(r->data.srv.name) + 1;
            break;

        case AVAHI_DNS_TYPE_HINFO:
            n += strlen(r->data.hinfo.os) + 1 + strlen(r->data.hinfo.cpu) + 1;
            break;

        case AVAHI_DNS_TYPE_TXT:
            n += avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
            break;

        case AVAHI_DNS_TYPE_A:
            n += sizeof(AvahiIPv4Address);
            break;

        case AVAHI_DNS_TYPE_AAAA:
            n += sizeof(AvahiIPv6Address);
            break;

        default:
            n += r->data.generic.size;
    }

    return n;
}

static int lexicographical_memcmp(const void* a, size_t al, const void* b, size_t bl) {
    size_t c;
    int ret;
    
    assert(a);
    assert(b);

    c = al < bl ? al : bl;
    if ((ret = memcmp(a, b, c)))
        return ret;

    if (al == bl)
        return 0;
    else
        return al == c ? 1 : -1;
}

static int uint16_cmp(uint16_t a, uint16_t b) {
    return a == b ? 0 : (a < b ? -1 : 1);
}

int avahi_record_lexicographical_compare(AvahiRecord *a, AvahiRecord *b) {
    int r;
/*      char *t1, *t2; */

    assert(a);
    assert(b);

/*     t1 = avahi_record_to_string(a); */
/*     t2 = avahi_record_to_string(b); */
/*     g_message("lexicocmp: %s %s", t1, t2); */
/*     avahi_free(t1); */
/*     avahi_free(t2); */

    if (a == b)
        return 0;

    if ((r = uint16_cmp(a->key->clazz, b->key->clazz)) ||
        (r = uint16_cmp(a->key->type, b->key->type)))
        return r;

    switch (a->key->type) {

        case AVAHI_DNS_TYPE_PTR:
        case AVAHI_DNS_TYPE_CNAME:
        case AVAHI_DNS_TYPE_NS:
            return avahi_binary_domain_cmp(a->data.ptr.name, b->data.ptr.name);

        case AVAHI_DNS_TYPE_SRV: {
            if ((r = uint16_cmp(a->data.srv.priority, b->data.srv.priority)) == 0 &&
                (r = uint16_cmp(a->data.srv.weight, b->data.srv.weight)) == 0 &&
                (r = uint16_cmp(a->data.srv.port, b->data.srv.port)) == 0)
                r = avahi_binary_domain_cmp(a->data.srv.name, b->data.srv.name);
            
            return r;
        }

        case AVAHI_DNS_TYPE_HINFO: {

            if ((r = strcmp(a->data.hinfo.cpu, b->data.hinfo.cpu)) ||
                (r = strcmp(a->data.hinfo.os, b->data.hinfo.os)))
                return r;

            return 0;

        }

        case AVAHI_DNS_TYPE_TXT: {

            uint8_t *ma = NULL, *mb = NULL;
            size_t asize, bsize;

            asize = avahi_string_list_serialize(a->data.txt.string_list, NULL, 0);
            bsize = avahi_string_list_serialize(b->data.txt.string_list, NULL, 0);
            
            if (asize > 0 && !(ma = avahi_new(uint8_t, asize)))
                goto fail;
            
            if (bsize > 0 && !(mb = avahi_new(uint8_t, bsize))) {
                avahi_free(ma);
                goto fail;
            }
            
            avahi_string_list_serialize(a->data.txt.string_list, ma, asize);
            avahi_string_list_serialize(b->data.txt.string_list, mb, bsize);

            if (asize && bsize)
                r = lexicographical_memcmp(ma, asize, mb, bsize);
            else if (asize && !bsize)
                r = 1;
            else if (!asize && bsize)
                r = -1;
            else
                r = 0;
            
            avahi_free(ma);
            avahi_free(mb);

            return r;
        }
        
        case AVAHI_DNS_TYPE_A:
            return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address));

        case AVAHI_DNS_TYPE_AAAA:
            return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address));

        default:
            return lexicographical_memcmp(a->data.generic.data, a->data.generic.size,
                                          b->data.generic.data, b->data.generic.size);
    }

    
fail:
    avahi_log_error(__FILE__": Out of memory");
    return -1; /* or whatever ... */
}

int avahi_record_is_goodbye(AvahiRecord *r) {
    assert(r);

    return r->ttl == 0;
}

int avahi_key_is_valid(AvahiKey *k) {
    assert(k);

    if (!avahi_is_valid_domain_name(k->name))
        return 0;
    
    return 1;
}

int avahi_record_is_valid(AvahiRecord *r) {
    assert(r);

    if (!avahi_key_is_valid(r->key))
        return 0;

    switch (r->key->type) {

        case AVAHI_DNS_TYPE_PTR:
        case AVAHI_DNS_TYPE_CNAME:
        case AVAHI_DNS_TYPE_NS:
            return avahi_is_valid_domain_name(r->data.ptr.name);

        case AVAHI_DNS_TYPE_SRV:
            return avahi_is_valid_domain_name(r->data.srv.name);

        case AVAHI_DNS_TYPE_HINFO:
            return
                strlen(r->data.hinfo.os) <= 255 &&
                strlen(r->data.hinfo.cpu) <= 255;
            
        case AVAHI_DNS_TYPE_TXT: {

            AvahiStringList *strlst;

            for (strlst = r->data.txt.string_list; strlst; strlst = strlst->next)
                if (strlst->size > 255)
                    return 0;

            return 1;
        }
    }
            

    return 1;
}