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 <assert.h>
27#include <sys/types.h>
28#include <sys/socket.h>
29#include <netinet/in.h>
30#include <arpa/inet.h>
31
32#include <avahi-common/domain.h>
33#include <avahi-common/defs.h>
34#include <avahi-common/malloc.h>
35
36#include "dns.h"
37#include "log.h"
38#include "util.h"
39
40int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
41    char t[AVAHI_DOMAIN_NAME_MAX], *m;
42    const char *a, *b, *c, *d;
43    AvahiDnsPacket *p;
44    AvahiRecord *r, *r2;
45    uint8_t rdata[AVAHI_DNS_RDATA_MAX];
46    size_t l;
47
48    p = avahi_dns_packet_new(0);
49
50    assert(avahi_dns_packet_append_name(p, a = "Ahello.hello.hello.de."));
51    assert(avahi_dns_packet_append_name(p, b = "Bthis is a test.hello.de."));
52    assert(avahi_dns_packet_append_name(p, c = "Cthis\\.is\\.a\\.test\\.with\\.dots.hello.de."));
53    assert(avahi_dns_packet_append_name(p, d = "Dthis\\\\is another test.hello.de."));
54
55    avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), p->size);
56
57    assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
58    avahi_log_debug(">%s<", t);
59    assert(avahi_domain_equal(a, t));
60
61    assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
62    avahi_log_debug(">%s<", t);
63    assert(avahi_domain_equal(b, t));
64
65    assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
66    avahi_log_debug(">%s<", t);
67    assert(avahi_domain_equal(c, t));
68
69    assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
70    avahi_log_debug(">%s<", t);
71    assert(avahi_domain_equal(d, t));
72
73    avahi_dns_packet_free(p);
74
75    /* RDATA PARSING AND SERIALIZATION */
76
77    /* Create an AvahiRecord with some usful data */
78    r = avahi_record_new_full("foobar.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL);
79    assert(r);
80    r->data.hinfo.cpu = avahi_strdup("FOO");
81    r->data.hinfo.os = avahi_strdup("BAR");
82
83    /* Serialize it into a blob */
84    assert((l = avahi_rdata_serialize(r, rdata, sizeof(rdata))) != (size_t) -1);
85
86    /* Print it */
87    avahi_hexdump(rdata, l);
88
89    /* Create a new record and fill in the data from the blob */
90    r2 = avahi_record_new(r->key, AVAHI_DEFAULT_TTL);
91    assert(r2);
92    assert(avahi_rdata_parse(r2, rdata, l) >= 0);
93
94    /* Compare both versions */
95    assert(avahi_record_equal_no_ttl(r, r2));
96
97    /* Free the records */
98    avahi_record_unref(r);
99    avahi_record_unref(r2);
100
101    r = avahi_record_new_full("foobar", 77, 77, AVAHI_DEFAULT_TTL);
102    assert(r);
103
104    assert(r->data.generic.data = avahi_memdup("HALLO", r->data.generic.size = 5));
105
106    m = avahi_record_to_string(r);
107    assert(m);
108
109    avahi_log_debug(">%s<", m);
110
111    avahi_free(m);
112    avahi_record_unref(r);
113
114    return 0;
115}
116