From 943a6b93697fc0a7bcd0cc574744d7de609c189c Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 25 Oct 2005 19:24:40 +0000 Subject: [PATCH] * add new core API functions avahi_rdata_serialize() and avahi_rdata_parse() * add new definition AVAHI_DNS_RDATA_MAX * update dns-test.c and add examples for avahi_rdata_{serialize,parse}() git-svn-id: file:///home/lennart/svn/public/avahi/trunk@867 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-core/dns-test.c | 52 ++++++++++--- avahi-core/dns.c | 200 +++++++++++++++++++++++++++++++------------------- avahi-core/dns.h | 5 +- avahi-core/rr.h | 6 ++ 4 files changed, 177 insertions(+), 86 deletions(-) diff --git a/avahi-core/dns-test.c b/avahi-core/dns-test.c index e5369a9..2336f42 100644 --- a/avahi-core/dns-test.c +++ b/avahi-core/dns-test.c @@ -24,43 +24,77 @@ #endif #include +#include +#include +#include #include +#include #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; } diff --git a/avahi-core/dns.c b/avahi-core/dns.c index 8c8b42b..63ea8a0 100644 --- a/avahi-core/dns.c +++ b/avahi-core/dns.c @@ -56,6 +56,7 @@ AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu) { 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; @@ -495,88 +496,58 @@ int avahi_dns_packet_skip(AvahiDnsPacket *p, size_t length) { 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; @@ -587,7 +558,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_f /* avahi_log_debug("A"); */ if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0) - goto fail; + return -1; break; @@ -596,7 +567,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_f /* avahi_log_debug("aaaa"); */ if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0) - goto fail; + return -1; break; @@ -609,18 +580,46 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_f 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: @@ -668,24 +667,10 @@ uint8_t* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, int unicast 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: @@ -693,7 +678,7 @@ uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int c case AVAHI_DNS_TYPE_NS: if (!(avahi_dns_packet_append_name(p, r->data.ptr.name))) - goto fail; + return -1; break; @@ -703,14 +688,14 @@ uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int c !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; @@ -721,10 +706,8 @@ uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int c 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; @@ -734,28 +717,50 @@ uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int c 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); @@ -786,3 +791,46 @@ size_t avahi_dns_packet_space(AvahiDnsPacket *p) { 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; +} diff --git a/avahi-core/dns.h b/avahi-core/dns.h index 61ec189..4696e00 100644 --- a/avahi-core/dns.h +++ b/avahi-core/dns.h @@ -32,9 +32,10 @@ typedef struct AvahiDnsPacket { size_t size, rindex, max_size; AvahiHashmap *name_table; /* for name compression */ + uint8_t *data; } AvahiDnsPacket; -#define AVAHI_DNS_PACKET_DATA(p) (((uint8_t*) p) + sizeof(AvahiDnsPacket)) +#define AVAHI_DNS_PACKET_DATA(p) ((p)->data ? (p)->data : ((uint8_t*) p) + sizeof(AvahiDnsPacket)) AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu); AvahiDnsPacket* avahi_dns_packet_new_query(unsigned mtu); @@ -104,5 +105,7 @@ size_t avahi_dns_packet_space(AvahiDnsPacket *p); #define AVAHI_MDNS_SUFFIX_ADDR_IPV4 "254.169.in-addr.arpa" #define AVAHI_MDNS_SUFFIX_ADDR_IPV6 "0.8.e.f.ip6.arpa" +#define AVAHI_DNS_RDATA_MAX 65535 + #endif diff --git a/avahi-core/rr.h b/avahi-core/rr.h index 7c00152..63dc18d 100644 --- a/avahi-core/rr.h +++ b/avahi-core/rr.h @@ -208,6 +208,12 @@ int avahi_key_is_valid(AvahiKey *k); /** Check whether the specified record is valid */ int avahi_record_is_valid(AvahiRecord *r); +/** Parse a binary rdata object and fill it into *record. This function is actually implemented in dns.c */ +int avahi_rdata_parse(AvahiRecord *record, const void* rdata, size_t size); + +/** Serialize an AvahiRecord object into binary rdata. This function is actually implemented in dns.c */ +size_t avahi_rdata_serialize(AvahiRecord *record, void *rdata, size_t max_size); + AVAHI_C_DECL_END #endif -- 2.7.4