Home | History | Annotate | Download | only in avahi-core
      1 /***
      2   This file is part of avahi.
      3 
      4   avahi is free software; you can redistribute it and/or modify it
      5   under the terms of the GNU Lesser General Public License as
      6   published by the Free Software Foundation; either version 2.1 of the
      7   License, or (at your option) any later version.
      8 
      9   avahi is distributed in the hope that it will be useful, but WITHOUT
     10   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     11   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
     12   Public License for more details.
     13 
     14   You should have received a copy of the GNU Lesser General Public
     15   License along with avahi; if not, write to the Free Software
     16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
     17   USA.
     18 ***/
     19 
     20 #ifdef HAVE_CONFIG_H
     21 #include <config.h>
     22 #endif
     23 
     24 #include <assert.h>
     25 #include <sys/types.h>
     26 #include <sys/socket.h>
     27 #include <netinet/in.h>
     28 #include <arpa/inet.h>
     29 
     30 #include <avahi-common/domain.h>
     31 #include <avahi-common/defs.h>
     32 #include "avahi-common/avahi-malloc.h"
     33 
     34 #include "dns.h"
     35 #include "log.h"
     36 #include "util.h"
     37 
     38 int main(AVAHI_GCC_UNUSED int argc, AVAHI_GCC_UNUSED char *argv[]) {
     39     char t[AVAHI_DOMAIN_NAME_MAX], *m;
     40     const char *a, *b, *c, *d;
     41     AvahiDnsPacket *p;
     42     AvahiRecord *r, *r2;
     43     uint8_t rdata[AVAHI_DNS_RDATA_MAX];
     44     size_t l;
     45 
     46     p = avahi_dns_packet_new(0);
     47 
     48     assert(avahi_dns_packet_append_name(p, a = "Ahello.hello.hello.de."));
     49     assert(avahi_dns_packet_append_name(p, b = "Bthis is a test.hello.de."));
     50     assert(avahi_dns_packet_append_name(p, c = "Cthis\\.is\\.a\\.test\\.with\\.dots.hello.de."));
     51     assert(avahi_dns_packet_append_name(p, d = "Dthis\\\\is another test.hello.de."));
     52 
     53     avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), p->size);
     54 
     55     assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
     56     avahi_log_debug(">%s<", t);
     57     assert(avahi_domain_equal(a, t));
     58 
     59     assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
     60     avahi_log_debug(">%s<", t);
     61     assert(avahi_domain_equal(b, t));
     62 
     63     assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
     64     avahi_log_debug(">%s<", t);
     65     assert(avahi_domain_equal(c, t));
     66 
     67     assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
     68     avahi_log_debug(">%s<", t);
     69     assert(avahi_domain_equal(d, t));
     70 
     71     avahi_dns_packet_free(p);
     72 
     73     /* RDATA PARSING AND SERIALIZATION */
     74 
     75     /* Create an AvahiRecord with some usful data */
     76     r = avahi_record_new_full("foobar.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL);
     77     assert(r);
     78     r->data.hinfo.cpu = avahi_strdup("FOO");
     79     r->data.hinfo.os = avahi_strdup("BAR");
     80 
     81     /* Serialize it into a blob */
     82     assert((l = avahi_rdata_serialize(r, rdata, sizeof(rdata))) != (size_t) -1);
     83 
     84     /* Print it */
     85     avahi_hexdump(rdata, l);
     86 
     87     /* Create a new record and fill in the data from the blob */
     88     r2 = avahi_record_new(r->key, AVAHI_DEFAULT_TTL);
     89     assert(r2);
     90     assert(avahi_rdata_parse(r2, rdata, l) >= 0);
     91 
     92     /* Compare both versions */
     93     assert(avahi_record_equal_no_ttl(r, r2));
     94 
     95     /* Free the records */
     96     avahi_record_unref(r);
     97     avahi_record_unref(r2);
     98 
     99     r = avahi_record_new_full("foobar", 77, 77, AVAHI_DEFAULT_TTL);
    100     assert(r);
    101 
    102     assert(r->data.generic.data = avahi_memdup("HALLO", r->data.generic.size = 5));
    103 
    104     m = avahi_record_to_string(r);
    105     assert(m);
    106 
    107     avahi_log_debug(">%s<", m);
    108 
    109     avahi_free(m);
    110     avahi_record_unref(r);
    111 
    112     return 0;
    113 }
    114