#endif
#include <assert.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
#include <avahi-common/domain.h>
+#include <avahi-common/malloc.h>
#include "dns.h"
#include "log.h"
#include "util.h"
int main(int argc, char *argv[]) {
- char t[256];
+ char t[AVAHI_DOMAIN_NAME_MAX];
const char *a, *b, *c, *d;
AvahiDnsPacket *p;
+ AvahiRecord *r, *r2;
+ uint8_t rdata[AVAHI_DNS_RDATA_MAX];
+ size_t l;
p = avahi_dns_packet_new(0);
- avahi_dns_packet_append_name(p, a = "hello.hello.hello.de.");
- avahi_dns_packet_append_name(p, b = "this is a test.hello.de.");
- avahi_dns_packet_append_name(p, c = "this\\.is\\.a\\.test\\.with\\.dots.hello.de.");
- avahi_dns_packet_append_name(p, d = "this\\\\is another\\ \\test.hello.de.");
+ assert(avahi_dns_packet_append_name(p, a = "Ahello.hello.hello.de."));
+ assert(avahi_dns_packet_append_name(p, b = "Bthis is a test.hello.de."));
+ assert(avahi_dns_packet_append_name(p, c = "Cthis\\.is\\.a\\.test\\.with\\.dots.hello.de."));
+ assert(avahi_dns_packet_append_name(p, d = "Dthis\\\\is another test.hello.de."));
avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), p->size);
- avahi_dns_packet_consume_name(p, t, sizeof(t));
+ assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
avahi_log_debug(">%s<", t);
assert(avahi_domain_equal(a, t));
- avahi_dns_packet_consume_name(p, t, sizeof(t));
+ assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
avahi_log_debug(">%s<", t);
assert(avahi_domain_equal(b, t));
- avahi_dns_packet_consume_name(p, t, sizeof(t));
+ assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
avahi_log_debug(">%s<", t);
assert(avahi_domain_equal(c, t));
- avahi_dns_packet_consume_name(p, t, sizeof(t));
+ assert(avahi_dns_packet_consume_name(p, t, sizeof(t)) == 0);
avahi_log_debug(">%s<", t);
assert(avahi_domain_equal(d, t));
avahi_dns_packet_free(p);
+
+ /* RDATA PARSING AND SERIALIZATION */
+
+ /* Create an AvahiRecord with some usful data */
+ r = avahi_record_new_full("foobar.local", AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL);
+ assert(r);
+ r->data.hinfo.cpu = avahi_strdup("FOO");
+ r->data.hinfo.os = avahi_strdup("BAR");
+
+ /* Serialize it into a blob */
+ assert((l = avahi_rdata_serialize(r, rdata, sizeof(rdata))) != (size_t) -1);
+
+ /* Print it */
+ avahi_hexdump(rdata, l);
+
+ /* Create a new record and fill in the data from the blob */
+ r2 = avahi_record_new(r->key, AVAHI_DEFAULT_TTL);
+ assert(r2);
+ assert(avahi_rdata_parse(r2, rdata, l) >= 0);
+
+ /* Compare both versions */
+ assert(avahi_record_equal_no_ttl(r, r2));
+
+ /* Free the records */
+ avahi_record_unref(r);
+ avahi_record_unref(r2);
+
return 0;
}
p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
p->max_size = max_size;
p->name_table = NULL;
+ p->data = NULL;
memset(AVAHI_DNS_PACKET_DATA(p), 0, p->size);
return p;
return 0;
}
-AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_flush) {
- char name[257], buf[257];
- uint16_t type, class;
- uint32_t ttl;
- uint16_t rdlength;
- AvahiRecord *r = NULL;
+static int parse_rdata(AvahiDnsPacket *p, AvahiRecord *r, uint16_t rdlength) {
+ char buf[AVAHI_DOMAIN_NAME_MAX];
const void* start;
-
+
assert(p);
+ assert(r);
-/* avahi_log_debug("consume_record()"); */
-
- if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
- avahi_dns_packet_consume_uint16(p, &type) < 0 ||
- avahi_dns_packet_consume_uint16(p, &class) < 0 ||
- avahi_dns_packet_consume_uint32(p, &ttl) < 0 ||
- avahi_dns_packet_consume_uint16(p, &rdlength) < 0 ||
- p->rindex + rdlength > p->size)
- goto fail;
-
-/* avahi_log_debug("name = %s, rdlength = %u", name, rdlength); */
-
- if (ret_cache_flush)
- *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH);
- class &= ~AVAHI_DNS_CACHE_FLUSH;
-
start = avahi_dns_packet_get_rptr(p);
-
- if (!(r = avahi_record_new_full(name, class, type, ttl)))
- return NULL;
-
- switch (type) {
+
+ switch (r->key->type) {
case AVAHI_DNS_TYPE_PTR:
case AVAHI_DNS_TYPE_CNAME:
case AVAHI_DNS_TYPE_NS:
-
-/* avahi_log_debug("ptr"); */
if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
- goto fail;
+ return -1;
r->data.ptr.name = avahi_strdup(buf);
break;
case AVAHI_DNS_TYPE_SRV:
-
-/* avahi_log_debug("srv"); */
if (avahi_dns_packet_consume_uint16(p, &r->data.srv.priority) < 0 ||
avahi_dns_packet_consume_uint16(p, &r->data.srv.weight) < 0 ||
avahi_dns_packet_consume_uint16(p, &r->data.srv.port) < 0 ||
avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
- goto fail;
+ return -1;
r->data.srv.name = avahi_strdup(buf);
break;
case AVAHI_DNS_TYPE_HINFO:
-
-/* avahi_log_debug("hinfo"); */
if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
- goto fail;
-
+ return -1;
+
r->data.hinfo.cpu = avahi_strdup(buf);
-
+
if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
- goto fail;
-
+ return -1;
+
r->data.hinfo.os = avahi_strdup(buf);
break;
case AVAHI_DNS_TYPE_TXT:
-/* avahi_log_debug("txt"); */
-
if (rdlength > 0) {
r->data.txt.string_list = avahi_string_list_parse(avahi_dns_packet_get_rptr(p), rdlength);
if (avahi_dns_packet_skip(p, rdlength) < 0)
- goto fail;
+ return -1;
} else
r->data.txt.string_list = NULL;
/* avahi_log_debug("A"); */
if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0)
- goto fail;
+ return -1;
break;
/* avahi_log_debug("aaaa"); */
if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0)
- goto fail;
+ return -1;
break;
r->data.generic.data = avahi_memdup(avahi_dns_packet_get_rptr(p), rdlength);
if (avahi_dns_packet_skip(p, rdlength) < 0)
- goto fail;
+ return -1;
}
break;
}
-/* avahi_log_debug("%i == %u ?", (uint8_t*) avahi_dns_packet_get_rptr(p) - (uint8_t*) start, rdlength); */
-
/* Check if we read enough data */
if ((const uint8_t*) avahi_dns_packet_get_rptr(p) - (const uint8_t*) start != rdlength)
+ return -1;
+
+ return 0;
+}
+
+AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_flush) {
+ char name[AVAHI_DOMAIN_NAME_MAX];
+ uint16_t type, class;
+ uint32_t ttl;
+ uint16_t rdlength;
+ AvahiRecord *r = NULL;
+
+ assert(p);
+
+ if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
+ avahi_dns_packet_consume_uint16(p, &type) < 0 ||
+ avahi_dns_packet_consume_uint16(p, &class) < 0 ||
+ avahi_dns_packet_consume_uint32(p, &ttl) < 0 ||
+ avahi_dns_packet_consume_uint16(p, &rdlength) < 0 ||
+ p->rindex + rdlength > p->size)
goto fail;
+ if (ret_cache_flush)
+ *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH);
+ class &= ~AVAHI_DNS_CACHE_FLUSH;
+
+ if (!(r = avahi_record_new_full(name, class, type, ttl)))
+ goto fail;
+
+ if (parse_rdata(p, r, rdlength) < 0)
+ goto fail;
+
return r;
fail:
return t;
}
-uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int cache_flush, unsigned max_ttl) {
- uint8_t *t, *l, *start;
- size_t size;
-
+static int append_rdata(AvahiDnsPacket *p, AvahiRecord *r) {
assert(p);
assert(r);
-
- size = p->size;
-
- if (!(t = avahi_dns_packet_append_name(p, r->key->name)) ||
- !avahi_dns_packet_append_uint16(p, r->key->type) ||
- !avahi_dns_packet_append_uint16(p, cache_flush ? (r->key->clazz | AVAHI_DNS_CACHE_FLUSH) : (r->key->clazz &~ AVAHI_DNS_CACHE_FLUSH)) ||
- !avahi_dns_packet_append_uint32(p, (max_ttl && r->ttl > max_ttl) ? max_ttl : r->ttl) ||
- !(l = avahi_dns_packet_append_uint16(p, 0)))
- goto fail;
-
- start = avahi_dns_packet_extend(p, 0);
-
+
switch (r->key->type) {
case AVAHI_DNS_TYPE_PTR:
case AVAHI_DNS_TYPE_NS:
if (!(avahi_dns_packet_append_name(p, r->data.ptr.name)))
- goto fail;
+ return -1;
break;
!avahi_dns_packet_append_uint16(p, r->data.srv.weight) ||
!avahi_dns_packet_append_uint16(p, r->data.srv.port) ||
!avahi_dns_packet_append_name(p, r->data.srv.name))
- goto fail;
+ return -1;
break;
case AVAHI_DNS_TYPE_HINFO:
if (!avahi_dns_packet_append_string(p, r->data.hinfo.cpu) ||
!avahi_dns_packet_append_string(p, r->data.hinfo.os))
- goto fail;
+ return -1;
break;
n = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
-/* avahi_log_debug("appending string: %u %p", n, r->data.txt.string_list); */
-
if (!(data = avahi_dns_packet_extend(p, n)))
- goto fail;
+ return -1;
avahi_string_list_serialize(r->data.txt.string_list, data, n);
break;
case AVAHI_DNS_TYPE_A:
if (!avahi_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address)))
- goto fail;
+ return -1;
break;
case AVAHI_DNS_TYPE_AAAA:
if (!avahi_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address)))
- goto fail;
+ return -1;
break;
default:
- if (r->data.generic.size &&
- avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
- goto fail;
+ if (r->data.generic.size)
+ if (avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
+ return -1;
break;
}
+ return 0;
+}
+
+uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int cache_flush, unsigned max_ttl) {
+ uint8_t *t, *l, *start;
+ size_t size;
+
+ assert(p);
+ assert(r);
+
+ size = p->size;
+
+ if (!(t = avahi_dns_packet_append_name(p, r->key->name)) ||
+ !avahi_dns_packet_append_uint16(p, r->key->type) ||
+ !avahi_dns_packet_append_uint16(p, cache_flush ? (r->key->clazz | AVAHI_DNS_CACHE_FLUSH) : (r->key->clazz &~ AVAHI_DNS_CACHE_FLUSH)) ||
+ !avahi_dns_packet_append_uint32(p, (max_ttl && r->ttl > max_ttl) ? max_ttl : r->ttl) ||
+ !(l = avahi_dns_packet_append_uint16(p, 0)))
+ goto fail;
+
+ start = avahi_dns_packet_extend(p, 0);
+ if (append_rdata(p, r) < 0)
+ goto fail;
size = avahi_dns_packet_extend(p, 0) - start;
assert(size <= 0xFFFF);
return p->max_size - p->size;
}
+
+int avahi_rdata_parse(AvahiRecord *record, const void* rdata, size_t size) {
+ int ret;
+ AvahiDnsPacket p;
+
+ assert(record);
+ assert(rdata);
+
+ p.data = (void*) rdata;
+ p.max_size = p.size = size;
+ p.rindex = 0;
+ p.name_table = NULL;
+
+ ret = parse_rdata(&p, record, size);
+
+ assert(!p.name_table);
+
+ return ret;
+}
+
+size_t avahi_rdata_serialize(AvahiRecord *record, void *rdata, size_t max_size) {
+ int ret;
+ AvahiDnsPacket p;
+
+ assert(record);
+ assert(rdata);
+ assert(max_size > 0);
+
+ p.data = (void*) rdata;
+ p.max_size = max_size;
+ p.size = p.rindex = 0;
+ p.name_table = NULL;
+
+ ret = append_rdata(&p, record);
+
+ if (p.name_table)
+ avahi_hashmap_free(p.name_table);
+
+ if (ret < 0)
+ return (size_t) -1;
+
+ return p.size;
+}