From: Lennart Poettering Date: Mon, 6 Jun 2005 09:34:19 +0000 (+0000) Subject: move dns.[ch] back to avahi-core/ X-Git-Url: http://git.meshlink.io/?a=commitdiff_plain;h=e877dd63989740389135513745c92b253fefd3fe;p=catta move dns.[ch] back to avahi-core/ git-svn-id: file:///home/lennart/svn/public/avahi/trunk@104 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/avahi-common/Makefile.am b/avahi-common/Makefile.am index 8c58757..94d373c 100644 --- a/avahi-common/Makefile.am +++ b/avahi-common/Makefile.am @@ -33,12 +33,10 @@ avahi_commoninclude_HEADERS = address.h \ alternative.h \ rr.h \ - util.h \ - dns.h + util.h noinst_PROGRAMS = \ strlst-test \ - dns-test \ domain-test \ alternative-test @@ -50,7 +48,6 @@ libavahi_common_la_SOURCES = \ strlst.c strlst.h \ alternative.c alternative.h \ rr.c rr.h \ - dns.c dns.h \ util.c util.h strlst_test_SOURCES = \ @@ -59,15 +56,6 @@ strlst_test_SOURCES = \ strlst_test_CFLAGS = $(AM_CFLAGS) strlst_test_LDADD = $(AM_LDADD) -dns_test_SOURCES = \ - util.c util.h \ - dns.c dns.h \ - rr.c rr.h \ - strlst.c strlst \ - dns-test.c -dns_test_CFLAGS = $(AM_CFLAGS) -dns_test_LDADD = $(AM_LDADD) - alternative_test_SOURCES = \ alternative.c alternative.h \ alternative-test.c diff --git a/avahi-common/dns-test.c b/avahi-common/dns-test.c deleted file mode 100644 index aa36f2a..0000000 --- a/avahi-common/dns-test.c +++ /dev/null @@ -1,60 +0,0 @@ -/* $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 -#endif - -#include "dns.h" -#include "util.h" - -int main(int argc, char *argv[]) { - gchar t[256], *a, *b, *c, *d; - AvahiDnsPacket *p; - - 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."); - - avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), p->size); - - avahi_dns_packet_consume_name(p, t, sizeof(t)); - g_message(">%s<", t); - g_assert(avahi_domain_equal(a, t)); - - avahi_dns_packet_consume_name(p, t, sizeof(t)); - g_message(">%s<", t); - g_assert(avahi_domain_equal(b, t)); - - avahi_dns_packet_consume_name(p, t, sizeof(t)); - g_message(">%s<", t); - g_assert(avahi_domain_equal(c, t)); - - avahi_dns_packet_consume_name(p, t, sizeof(t)); - g_message(">%s<", t); - g_assert(avahi_domain_equal(d, t)); - - avahi_dns_packet_free(p); - return 0; -} diff --git a/avahi-common/dns.c b/avahi-common/dns.c deleted file mode 100644 index 9e19c9e..0000000 --- a/avahi-common/dns.c +++ /dev/null @@ -1,744 +0,0 @@ -/* $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 -#endif - -#include - -#include -#include -#include - -#include "dns.h" -#include "util.h" - -AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { - AvahiDnsPacket *p; - guint max_size; - - if (mtu <= 0) - max_size = AVAHI_DNS_PACKET_MAX_SIZE; - else if (mtu >= AVAHI_DNS_PACKET_EXTRA_SIZE) - max_size = mtu - AVAHI_DNS_PACKET_EXTRA_SIZE; - else - max_size = 0; - - if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE) - max_size = AVAHI_DNS_PACKET_HEADER_SIZE; - - p = g_malloc(sizeof(AvahiDnsPacket) + max_size); - p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE; - p->max_size = max_size; - p->name_table = NULL; - - memset(AVAHI_DNS_PACKET_DATA(p), 0, p->size); - return p; -} - -AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu) { - AvahiDnsPacket *p; - - p = avahi_dns_packet_new(mtu); - avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); - return p; -} - -AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa) { - AvahiDnsPacket *p; - - p = avahi_dns_packet_new(mtu); - avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(1, 0, aa, 0, 0, 0, 0, 0, 0, 0)); - return p; -} - -AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa) { - AvahiDnsPacket *r; - g_assert(p); - - r = avahi_dns_packet_new_response(mtu, aa); - - if (copy_queries) { - guint n, saved_rindex; - - saved_rindex = p->rindex; - p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE; - - for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) { - AvahiKey *k; - gboolean unicast_response; - - if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) { - avahi_dns_packet_append_key(r, k, unicast_response); - avahi_key_unref(k); - } - } - - p->rindex = saved_rindex; - - avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_QDCOUNT, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT)); - } - - avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_ID, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID)); - - avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_FLAGS, - (avahi_dns_packet_get_field(r, AVAHI_DNS_FIELD_FLAGS) & ~AVAHI_DNS_FLAG_OPCODE) | - (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_OPCODE)); - - return r; -} - - -void avahi_dns_packet_free(AvahiDnsPacket *p) { - g_assert(p); - - if (p->name_table) - g_hash_table_destroy(p->name_table); - - g_free(p); -} - -void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint index, guint16 v) { - g_assert(p); - g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE); - - ((guint16*) AVAHI_DNS_PACKET_DATA(p))[index] = g_htons(v); -} - -guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint index) { - g_assert(p); - g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE); - - return g_ntohs(((guint16*) AVAHI_DNS_PACKET_DATA(p))[index]); -} - -void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint index) { - g_assert(p); - - avahi_dns_packet_set_field(p, index, avahi_dns_packet_get_field(p, index) + 1); -} - -guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) { - guint8 *d, *saved_ptr = NULL; - guint saved_size; - - g_assert(p); - g_assert(name); - - saved_size = p->size; - saved_ptr = avahi_dns_packet_extend(p, 0); - - while (*name) { - guint8* prev; - const gchar *pname; - gchar label[64]; - - /* Check whether we can compress this name. */ - - if (p->name_table && (prev = g_hash_table_lookup(p->name_table, name))) { - guint index; - - g_assert(prev >= AVAHI_DNS_PACKET_DATA(p)); - index = (guint) (prev - AVAHI_DNS_PACKET_DATA(p)); - - g_assert(index < p->size); - - if (index < 0x4000) { - guint16 *t; - if (!(t = (guint16*) avahi_dns_packet_extend(p, sizeof(guint16)))) - return NULL; - - *t = g_htons((0xC000 | index)); - return saved_ptr; - } - } - - pname = name; - - if (!(avahi_unescape_label(&name, label, sizeof(label)))) - goto fail; - - if (!(d = avahi_dns_packet_append_string(p, label))) - goto fail; - - if (!p->name_table) - /* This works only for normalized domain names */ - p->name_table = g_hash_table_new_full((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal, g_free, NULL); - - g_hash_table_insert(p->name_table, g_strdup(pname), d); - } - - if (!(d = avahi_dns_packet_extend(p, 1))) - goto fail; - - *d = 0; - - return saved_ptr; - -fail: - p->size = saved_size; - return NULL; -} - -guint8* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v) { - guint8 *d; - g_assert(p); - - if (!(d = avahi_dns_packet_extend(p, sizeof(guint16)))) - return NULL; - - *((guint16*) d) = g_htons(v); - return d; -} - -guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v) { - guint8 *d; - g_assert(p); - - if (!(d = avahi_dns_packet_extend(p, sizeof(guint32)))) - return NULL; - - *((guint32*) d) = g_htonl(v); - - return d; -} - -guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer b, guint l) { - guint8* d; - - g_assert(p); - g_assert(b); - g_assert(l); - - if (!(d = avahi_dns_packet_extend(p, l))) - return NULL; - - memcpy(d, b, l); - return d; -} - -guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) { - guint8* d; - guint k; - - g_assert(p); - g_assert(s); - - if ((k = strlen(s)) >= 255) - k = 255; - - if (!(d = avahi_dns_packet_extend(p, k+1))) - return NULL; - - *d = (guint8) k; - memcpy(d+1, s, k); - - return d; -} - -guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) { - guint8 *d; - - g_assert(p); - - if (p->size+l > p->max_size) - return NULL; - - d = AVAHI_DNS_PACKET_DATA(p) + p->size; - p->size += l; - - return d; -} - -gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) { - guint16 flags; - g_assert(p); - - if (p->size < 12) - return -1; - - flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS); - - if (flags & AVAHI_DNS_FLAG_OPCODE || flags & AVAHI_DNS_FLAG_RCODE) - return -1; - - return 0; -} - -gint avahi_dns_packet_is_query(AvahiDnsPacket *p) { - g_assert(p); - - return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR); -} - -static gint consume_labels(AvahiDnsPacket *p, guint index, gchar *ret_name, guint l) { - gint ret = 0; - int compressed = 0; - int first_label = 1; - g_assert(p && ret_name && l); - - for (;;) { - guint8 n; - - if (index+1 > p->size) - return -1; - - n = AVAHI_DNS_PACKET_DATA(p)[index]; - - if (!n) { - index++; - if (!compressed) - ret++; - - if (l < 1) - return -1; - *ret_name = 0; - - return ret; - - } else if (n <= 63) { - /* Uncompressed label */ - index++; - if (!compressed) - ret++; - - if (index + n > p->size) - return -1; - - if ((guint) n + 1 > l) - return -1; - - if (!first_label) { - *(ret_name++) = '.'; - l--; - } else - first_label = 0; - - if (!(avahi_escape_label(AVAHI_DNS_PACKET_DATA(p) + index, n, &ret_name, &l))) - return -1; - - index += n; - - if (!compressed) - ret += n; - } else if ((n & 0xC0) == 0xC0) { - /* Compressed label */ - - if (index+2 > p->size) - return -1; - - index = ((guint) (AVAHI_DNS_PACKET_DATA(p)[index] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[index+1]; - - if (!compressed) - ret += 2; - - compressed = 1; - } else - return -1; - } -} - -gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) { - gint r; - - if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0) - return -1; - - p->rindex += r; - return 0; -} - -gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v) { - g_assert(p); - g_assert(ret_v); - - if (p->rindex + sizeof(guint16) > p->size) - return -1; - - *ret_v = g_ntohs(*((guint16*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); - p->rindex += sizeof(guint16); - - return 0; -} - -gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v) { - g_assert(p); - g_assert(ret_v); - - if (p->rindex + sizeof(guint32) > p->size) - return -1; - - *ret_v = g_ntohl(*((guint32*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); - p->rindex += sizeof(guint32); - - return 0; -} - -gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l) { - g_assert(p); - g_assert(ret_data); - g_assert(l > 0); - - if (p->rindex + l > p->size) - return -1; - - memcpy(ret_data, AVAHI_DNS_PACKET_DATA(p) + p->rindex, l); - p->rindex += l; - - return 0; -} - -gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l) { - guint k; - - g_assert(p); - g_assert(ret_string); - g_assert(l > 0); - - if (p->rindex >= p->size) - return -1; - - k = AVAHI_DNS_PACKET_DATA(p)[p->rindex]; - - if (p->rindex+1+k > p->size) - return -1; - - if (l > k+1) - l = k+1; - - memcpy(ret_string, AVAHI_DNS_PACKET_DATA(p)+p->rindex+1, l-1); - ret_string[l-1] = 0; - - - p->rindex += 1+k; - - return 0; - -} - -gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p) { - g_assert(p); - - if (p->rindex > p->size) - return NULL; - - return AVAHI_DNS_PACKET_DATA(p) + p->rindex; -} - -gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length) { - g_assert(p); - - if (p->rindex + length > p->size) - return -1; - - p->rindex += length; - return 0; -} - -AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush) { - gchar name[257], buf[257]; - guint16 type, class; - guint32 ttl; - guint16 rdlength; - AvahiRecord *r = NULL; - gconstpointer start; - - g_assert(p); - g_assert(ret_cache_flush); - -/* g_message("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; - -/* g_message("name = %s, rdlength = %u", name, rdlength); */ - - *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH); - class &= ~AVAHI_DNS_CACHE_FLUSH; - - start = avahi_dns_packet_get_rptr(p); - - r = avahi_record_new_full(name, class, type); - - switch (type) { - case AVAHI_DNS_TYPE_PTR: - case AVAHI_DNS_TYPE_CNAME: - -/* g_message("ptr"); */ - - if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0) - goto fail; - - r->data.ptr.name = g_strdup(buf); - break; - - - case AVAHI_DNS_TYPE_SRV: - -/* g_message("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; - - r->data.srv.name = g_strdup(buf); - break; - - case AVAHI_DNS_TYPE_HINFO: - -/* g_message("hinfo"); */ - - if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) - goto fail; - - r->data.hinfo.cpu = g_strdup(buf); - - if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) - goto fail; - - r->data.hinfo.os = g_strdup(buf); - break; - - case AVAHI_DNS_TYPE_TXT: - -/* g_message("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; - } else - r->data.txt.string_list = NULL; - - break; - - case AVAHI_DNS_TYPE_A: - -/* g_message("A"); */ - - if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0) - goto fail; - - break; - - case AVAHI_DNS_TYPE_AAAA: - -/* g_message("aaaa"); */ - - if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0) - goto fail; - - break; - - default: - -/* g_message("generic"); */ - - if (rdlength > 0) { - - r->data.generic.data = g_memdup(avahi_dns_packet_get_rptr(p), rdlength); - - if (avahi_dns_packet_skip(p, rdlength) < 0) - goto fail; - } - - break; - } - -/* g_message("%i == %u ?", (guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start, rdlength); */ - - /* Check if we read enough data */ - if ((guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start != rdlength) - goto fail; - - r->ttl = ttl; - - return r; - -fail: - if (r) - avahi_record_unref(r); - - return NULL; -} - -AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response) { - gchar name[256]; - guint16 type, class; - - g_assert(p); - g_assert(ret_unicast_response); - - 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) - return NULL; - - *ret_unicast_response = !!(class & AVAHI_DNS_UNICAST_RESPONSE); - class &= ~AVAHI_DNS_UNICAST_RESPONSE; - - return avahi_key_new(name, class, type); -} - -guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response) { - guint8 *t; - guint size; - - g_assert(p); - g_assert(k); - - size = p->size; - - if (!(t = avahi_dns_packet_append_name(p, k->name)) || - !avahi_dns_packet_append_uint16(p, k->type) || - !avahi_dns_packet_append_uint16(p, k->class | (unicast_response ? AVAHI_DNS_UNICAST_RESPONSE : 0))) { - p->size = size; - return NULL; - } - - return t; -} - -guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl) { - guint8 *t, *l, *start; - guint size; - - g_assert(p); - g_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->class | AVAHI_DNS_CACHE_FLUSH) : (r->key->class &~ 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_CNAME : - - if (!(avahi_dns_packet_append_name(p, r->data.ptr.name))) - goto fail; - - break; - - case AVAHI_DNS_TYPE_SRV: - - if (!avahi_dns_packet_append_uint16(p, r->data.srv.priority) || - !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; - - 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; - - break; - - case AVAHI_DNS_TYPE_TXT: { - - guint8 *data; - guint size; - - size = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0); - -/* g_message("appending string: %u %p", size, r->data.txt.string_list); */ - - if (!(data = avahi_dns_packet_extend(p, size))) - goto fail; - - avahi_string_list_serialize(r->data.txt.string_list, data, size); - break; - } - - - case AVAHI_DNS_TYPE_A: - - if (!avahi_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address))) - goto fail; - - break; - - case AVAHI_DNS_TYPE_AAAA: - - if (!avahi_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address))) - goto fail; - - break; - - default: - - if (r->data.generic.size && - avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size)) - goto fail; - - break; - } - - - - - size = avahi_dns_packet_extend(p, 0) - start; - g_assert(size <= 0xFFFF); - -/* g_message("appended %u", size); */ - - * (guint16*) l = g_htons((guint16) size); - - return t; - - -fail: - p->size = size; - return NULL; -} - -gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p) { - g_assert(p); - - return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE; -} - -guint avahi_dns_packet_space(AvahiDnsPacket *p) { - g_assert(p); - - g_assert(p->size <= p->max_size); - - return p->max_size - p->size; -} diff --git a/avahi-common/dns.h b/avahi-common/dns.h deleted file mode 100644 index 03d9798..0000000 --- a/avahi-common/dns.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef foodnshfoo -#define foodnshfoo - -/* $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. -***/ - -#include - -#include "rr.h" - -#define AVAHI_DNS_PACKET_MAX_SIZE 9000 -#define AVAHI_DNS_PACKET_HEADER_SIZE 12 -#define AVAHI_DNS_PACKET_EXTRA_SIZE 48 - -typedef struct AvahiDnsPacket { - guint size, rindex, max_size; - GHashTable *name_table; /* for name compression */ -} AvahiDnsPacket; - -#define AVAHI_DNS_PACKET_DATA(p) (((guint8*) p) + sizeof(AvahiDnsPacket)) - -AvahiDnsPacket* avahi_dns_packet_new(guint mtu); -AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu); -AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa); - -AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa); - -void avahi_dns_packet_free(AvahiDnsPacket *p); -void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint index, guint16 v); -guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint index); -void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint index); - -guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l); - -guint8 *avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v); -guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v); -guint8 *avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name); -guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer, guint l); -guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response); -guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl); -guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s); - -gint avahi_dns_packet_is_query(AvahiDnsPacket *p); -gint avahi_dns_packet_check_valid(AvahiDnsPacket *p); - -gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v); -gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v); -gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l); -gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l); -AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response); -AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush); -gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l); - -gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p); - -gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length); - -gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p); -guint avahi_dns_packet_space(AvahiDnsPacket *p); - -#define AVAHI_DNS_FIELD_ID 0 -#define AVAHI_DNS_FIELD_FLAGS 1 -#define AVAHI_DNS_FIELD_QDCOUNT 2 -#define AVAHI_DNS_FIELD_ANCOUNT 3 -#define AVAHI_DNS_FIELD_NSCOUNT 4 -#define AVAHI_DNS_FIELD_ARCOUNT 5 - -#define AVAHI_DNS_FLAG_QR (1 << 15) -#define AVAHI_DNS_FLAG_OPCODE (15 << 11) -#define AVAHI_DNS_FLAG_RCODE (15) -#define AVAHI_DNS_FLAG_TC (1 << 9) -#define AVAHI_DNS_FLAG_AA (1 << 10) - -#define AVAHI_DNS_FLAGS(qr, opcode, aa, tc, rd, ra, z, ad, cd, rcode) \ - (((guint16) !!qr << 15) | \ - ((guint16) (opcode & 15) << 11) | \ - ((guint16) !!aa << 10) | \ - ((guint16) !!tc << 9) | \ - ((guint16) !!rd << 8) | \ - ((guint16) !!ra << 7) | \ - ((guint16) !!ad << 5) | \ - ((guint16) !!cd << 4) | \ - ((guint16) (rd & 15))) - - -#endif - diff --git a/avahi-common/rr.c b/avahi-common/rr.c index c7f7d2c..7e974c4 100644 --- a/avahi-common/rr.c +++ b/avahi-common/rr.c @@ -31,7 +31,6 @@ #include "util.h" #include "rr.h" -#include "dns.h" AvahiKey *avahi_key_new(const gchar *name, guint16 class, guint16 type) { AvahiKey *k; diff --git a/avahi-core/Makefile.am b/avahi-core/Makefile.am index b692fcc..6920e8c 100644 --- a/avahi-core/Makefile.am +++ b/avahi-core/Makefile.am @@ -21,9 +21,11 @@ AM_CFLAGS=-I$(top_srcdir) -D_GNU_SOURCE # GLIB 2.0 AM_CFLAGS+=$(GLIB20_CFLAGS) -AM_LDADD=$(GLIB20_LIBS) ../avahi-common/libavahi-common.la +AM_LDADD=$(GLIB20_LIBS) -AM_CFLAGS+=-I$(top_srcdir)/avahi-common +# Import stuff from avahi-common +AM_CFLAGS+=-I$(top_srcdir)/avahi-common +AM_LDADD+=../avahi-common/libavahi-common.la # This cool debug trap works on i386/gcc only AM_CFLAGS+='-DDEBUG_TRAP=__asm__("int $$3")' @@ -40,7 +42,8 @@ noinst_PROGRAMS = \ prioq-test \ avahi-test \ conformance-test \ - avahi-reflector + avahi-reflector \ + dns-test libavahi_core_la_SOURCES = \ timeeventq.c timeeventq.h\ @@ -61,7 +64,8 @@ libavahi_core_la_SOURCES = \ browse-domain.c \ browse-service-type.c \ browse-service.c \ - resolve-service.c + resolve-service.c \ + dns.c dns.h prioq_test_SOURCES = \ prioq-test.c \ @@ -87,6 +91,12 @@ avahi_reflector_SOURCES = \ avahi_reflector_CFLAGS = $(AM_CFLAGS) avahi_reflector_LDADD = $(AM_LDADD) +dns_test_SOURCES = \ + dns.c dns.h \ + dns-test.c +dns_test_CFLAGS = $(AM_CFLAGS) +dns_test_LDADD = $(AM_LDADD) + valgrind: avahi-test libtool --mode=execute valgrind ./avahi-test diff --git a/avahi-core/dns-test.c b/avahi-core/dns-test.c new file mode 100644 index 0000000..aa36f2a --- /dev/null +++ b/avahi-core/dns-test.c @@ -0,0 +1,60 @@ +/* $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 +#endif + +#include "dns.h" +#include "util.h" + +int main(int argc, char *argv[]) { + gchar t[256], *a, *b, *c, *d; + AvahiDnsPacket *p; + + 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."); + + avahi_hexdump(AVAHI_DNS_PACKET_DATA(p), p->size); + + avahi_dns_packet_consume_name(p, t, sizeof(t)); + g_message(">%s<", t); + g_assert(avahi_domain_equal(a, t)); + + avahi_dns_packet_consume_name(p, t, sizeof(t)); + g_message(">%s<", t); + g_assert(avahi_domain_equal(b, t)); + + avahi_dns_packet_consume_name(p, t, sizeof(t)); + g_message(">%s<", t); + g_assert(avahi_domain_equal(c, t)); + + avahi_dns_packet_consume_name(p, t, sizeof(t)); + g_message(">%s<", t); + g_assert(avahi_domain_equal(d, t)); + + avahi_dns_packet_free(p); + return 0; +} diff --git a/avahi-core/dns.c b/avahi-core/dns.c new file mode 100644 index 0000000..9e19c9e --- /dev/null +++ b/avahi-core/dns.c @@ -0,0 +1,744 @@ +/* $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 +#endif + +#include + +#include +#include +#include + +#include "dns.h" +#include "util.h" + +AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { + AvahiDnsPacket *p; + guint max_size; + + if (mtu <= 0) + max_size = AVAHI_DNS_PACKET_MAX_SIZE; + else if (mtu >= AVAHI_DNS_PACKET_EXTRA_SIZE) + max_size = mtu - AVAHI_DNS_PACKET_EXTRA_SIZE; + else + max_size = 0; + + if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE) + max_size = AVAHI_DNS_PACKET_HEADER_SIZE; + + p = g_malloc(sizeof(AvahiDnsPacket) + max_size); + p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE; + p->max_size = max_size; + p->name_table = NULL; + + memset(AVAHI_DNS_PACKET_DATA(p), 0, p->size); + return p; +} + +AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu) { + AvahiDnsPacket *p; + + p = avahi_dns_packet_new(mtu); + avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); + return p; +} + +AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa) { + AvahiDnsPacket *p; + + p = avahi_dns_packet_new(mtu); + avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(1, 0, aa, 0, 0, 0, 0, 0, 0, 0)); + return p; +} + +AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa) { + AvahiDnsPacket *r; + g_assert(p); + + r = avahi_dns_packet_new_response(mtu, aa); + + if (copy_queries) { + guint n, saved_rindex; + + saved_rindex = p->rindex; + p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE; + + for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) { + AvahiKey *k; + gboolean unicast_response; + + if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) { + avahi_dns_packet_append_key(r, k, unicast_response); + avahi_key_unref(k); + } + } + + p->rindex = saved_rindex; + + avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_QDCOUNT, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT)); + } + + avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_ID, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID)); + + avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_FLAGS, + (avahi_dns_packet_get_field(r, AVAHI_DNS_FIELD_FLAGS) & ~AVAHI_DNS_FLAG_OPCODE) | + (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_OPCODE)); + + return r; +} + + +void avahi_dns_packet_free(AvahiDnsPacket *p) { + g_assert(p); + + if (p->name_table) + g_hash_table_destroy(p->name_table); + + g_free(p); +} + +void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint index, guint16 v) { + g_assert(p); + g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE); + + ((guint16*) AVAHI_DNS_PACKET_DATA(p))[index] = g_htons(v); +} + +guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint index) { + g_assert(p); + g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE); + + return g_ntohs(((guint16*) AVAHI_DNS_PACKET_DATA(p))[index]); +} + +void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint index) { + g_assert(p); + + avahi_dns_packet_set_field(p, index, avahi_dns_packet_get_field(p, index) + 1); +} + +guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) { + guint8 *d, *saved_ptr = NULL; + guint saved_size; + + g_assert(p); + g_assert(name); + + saved_size = p->size; + saved_ptr = avahi_dns_packet_extend(p, 0); + + while (*name) { + guint8* prev; + const gchar *pname; + gchar label[64]; + + /* Check whether we can compress this name. */ + + if (p->name_table && (prev = g_hash_table_lookup(p->name_table, name))) { + guint index; + + g_assert(prev >= AVAHI_DNS_PACKET_DATA(p)); + index = (guint) (prev - AVAHI_DNS_PACKET_DATA(p)); + + g_assert(index < p->size); + + if (index < 0x4000) { + guint16 *t; + if (!(t = (guint16*) avahi_dns_packet_extend(p, sizeof(guint16)))) + return NULL; + + *t = g_htons((0xC000 | index)); + return saved_ptr; + } + } + + pname = name; + + if (!(avahi_unescape_label(&name, label, sizeof(label)))) + goto fail; + + if (!(d = avahi_dns_packet_append_string(p, label))) + goto fail; + + if (!p->name_table) + /* This works only for normalized domain names */ + p->name_table = g_hash_table_new_full((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal, g_free, NULL); + + g_hash_table_insert(p->name_table, g_strdup(pname), d); + } + + if (!(d = avahi_dns_packet_extend(p, 1))) + goto fail; + + *d = 0; + + return saved_ptr; + +fail: + p->size = saved_size; + return NULL; +} + +guint8* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v) { + guint8 *d; + g_assert(p); + + if (!(d = avahi_dns_packet_extend(p, sizeof(guint16)))) + return NULL; + + *((guint16*) d) = g_htons(v); + return d; +} + +guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v) { + guint8 *d; + g_assert(p); + + if (!(d = avahi_dns_packet_extend(p, sizeof(guint32)))) + return NULL; + + *((guint32*) d) = g_htonl(v); + + return d; +} + +guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer b, guint l) { + guint8* d; + + g_assert(p); + g_assert(b); + g_assert(l); + + if (!(d = avahi_dns_packet_extend(p, l))) + return NULL; + + memcpy(d, b, l); + return d; +} + +guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) { + guint8* d; + guint k; + + g_assert(p); + g_assert(s); + + if ((k = strlen(s)) >= 255) + k = 255; + + if (!(d = avahi_dns_packet_extend(p, k+1))) + return NULL; + + *d = (guint8) k; + memcpy(d+1, s, k); + + return d; +} + +guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) { + guint8 *d; + + g_assert(p); + + if (p->size+l > p->max_size) + return NULL; + + d = AVAHI_DNS_PACKET_DATA(p) + p->size; + p->size += l; + + return d; +} + +gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) { + guint16 flags; + g_assert(p); + + if (p->size < 12) + return -1; + + flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS); + + if (flags & AVAHI_DNS_FLAG_OPCODE || flags & AVAHI_DNS_FLAG_RCODE) + return -1; + + return 0; +} + +gint avahi_dns_packet_is_query(AvahiDnsPacket *p) { + g_assert(p); + + return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR); +} + +static gint consume_labels(AvahiDnsPacket *p, guint index, gchar *ret_name, guint l) { + gint ret = 0; + int compressed = 0; + int first_label = 1; + g_assert(p && ret_name && l); + + for (;;) { + guint8 n; + + if (index+1 > p->size) + return -1; + + n = AVAHI_DNS_PACKET_DATA(p)[index]; + + if (!n) { + index++; + if (!compressed) + ret++; + + if (l < 1) + return -1; + *ret_name = 0; + + return ret; + + } else if (n <= 63) { + /* Uncompressed label */ + index++; + if (!compressed) + ret++; + + if (index + n > p->size) + return -1; + + if ((guint) n + 1 > l) + return -1; + + if (!first_label) { + *(ret_name++) = '.'; + l--; + } else + first_label = 0; + + if (!(avahi_escape_label(AVAHI_DNS_PACKET_DATA(p) + index, n, &ret_name, &l))) + return -1; + + index += n; + + if (!compressed) + ret += n; + } else if ((n & 0xC0) == 0xC0) { + /* Compressed label */ + + if (index+2 > p->size) + return -1; + + index = ((guint) (AVAHI_DNS_PACKET_DATA(p)[index] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[index+1]; + + if (!compressed) + ret += 2; + + compressed = 1; + } else + return -1; + } +} + +gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) { + gint r; + + if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0) + return -1; + + p->rindex += r; + return 0; +} + +gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v) { + g_assert(p); + g_assert(ret_v); + + if (p->rindex + sizeof(guint16) > p->size) + return -1; + + *ret_v = g_ntohs(*((guint16*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); + p->rindex += sizeof(guint16); + + return 0; +} + +gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v) { + g_assert(p); + g_assert(ret_v); + + if (p->rindex + sizeof(guint32) > p->size) + return -1; + + *ret_v = g_ntohl(*((guint32*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); + p->rindex += sizeof(guint32); + + return 0; +} + +gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l) { + g_assert(p); + g_assert(ret_data); + g_assert(l > 0); + + if (p->rindex + l > p->size) + return -1; + + memcpy(ret_data, AVAHI_DNS_PACKET_DATA(p) + p->rindex, l); + p->rindex += l; + + return 0; +} + +gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l) { + guint k; + + g_assert(p); + g_assert(ret_string); + g_assert(l > 0); + + if (p->rindex >= p->size) + return -1; + + k = AVAHI_DNS_PACKET_DATA(p)[p->rindex]; + + if (p->rindex+1+k > p->size) + return -1; + + if (l > k+1) + l = k+1; + + memcpy(ret_string, AVAHI_DNS_PACKET_DATA(p)+p->rindex+1, l-1); + ret_string[l-1] = 0; + + + p->rindex += 1+k; + + return 0; + +} + +gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p) { + g_assert(p); + + if (p->rindex > p->size) + return NULL; + + return AVAHI_DNS_PACKET_DATA(p) + p->rindex; +} + +gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length) { + g_assert(p); + + if (p->rindex + length > p->size) + return -1; + + p->rindex += length; + return 0; +} + +AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush) { + gchar name[257], buf[257]; + guint16 type, class; + guint32 ttl; + guint16 rdlength; + AvahiRecord *r = NULL; + gconstpointer start; + + g_assert(p); + g_assert(ret_cache_flush); + +/* g_message("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; + +/* g_message("name = %s, rdlength = %u", name, rdlength); */ + + *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH); + class &= ~AVAHI_DNS_CACHE_FLUSH; + + start = avahi_dns_packet_get_rptr(p); + + r = avahi_record_new_full(name, class, type); + + switch (type) { + case AVAHI_DNS_TYPE_PTR: + case AVAHI_DNS_TYPE_CNAME: + +/* g_message("ptr"); */ + + if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0) + goto fail; + + r->data.ptr.name = g_strdup(buf); + break; + + + case AVAHI_DNS_TYPE_SRV: + +/* g_message("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; + + r->data.srv.name = g_strdup(buf); + break; + + case AVAHI_DNS_TYPE_HINFO: + +/* g_message("hinfo"); */ + + if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) + goto fail; + + r->data.hinfo.cpu = g_strdup(buf); + + if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) + goto fail; + + r->data.hinfo.os = g_strdup(buf); + break; + + case AVAHI_DNS_TYPE_TXT: + +/* g_message("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; + } else + r->data.txt.string_list = NULL; + + break; + + case AVAHI_DNS_TYPE_A: + +/* g_message("A"); */ + + if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0) + goto fail; + + break; + + case AVAHI_DNS_TYPE_AAAA: + +/* g_message("aaaa"); */ + + if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0) + goto fail; + + break; + + default: + +/* g_message("generic"); */ + + if (rdlength > 0) { + + r->data.generic.data = g_memdup(avahi_dns_packet_get_rptr(p), rdlength); + + if (avahi_dns_packet_skip(p, rdlength) < 0) + goto fail; + } + + break; + } + +/* g_message("%i == %u ?", (guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start, rdlength); */ + + /* Check if we read enough data */ + if ((guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start != rdlength) + goto fail; + + r->ttl = ttl; + + return r; + +fail: + if (r) + avahi_record_unref(r); + + return NULL; +} + +AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response) { + gchar name[256]; + guint16 type, class; + + g_assert(p); + g_assert(ret_unicast_response); + + 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) + return NULL; + + *ret_unicast_response = !!(class & AVAHI_DNS_UNICAST_RESPONSE); + class &= ~AVAHI_DNS_UNICAST_RESPONSE; + + return avahi_key_new(name, class, type); +} + +guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response) { + guint8 *t; + guint size; + + g_assert(p); + g_assert(k); + + size = p->size; + + if (!(t = avahi_dns_packet_append_name(p, k->name)) || + !avahi_dns_packet_append_uint16(p, k->type) || + !avahi_dns_packet_append_uint16(p, k->class | (unicast_response ? AVAHI_DNS_UNICAST_RESPONSE : 0))) { + p->size = size; + return NULL; + } + + return t; +} + +guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl) { + guint8 *t, *l, *start; + guint size; + + g_assert(p); + g_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->class | AVAHI_DNS_CACHE_FLUSH) : (r->key->class &~ 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_CNAME : + + if (!(avahi_dns_packet_append_name(p, r->data.ptr.name))) + goto fail; + + break; + + case AVAHI_DNS_TYPE_SRV: + + if (!avahi_dns_packet_append_uint16(p, r->data.srv.priority) || + !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; + + 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; + + break; + + case AVAHI_DNS_TYPE_TXT: { + + guint8 *data; + guint size; + + size = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0); + +/* g_message("appending string: %u %p", size, r->data.txt.string_list); */ + + if (!(data = avahi_dns_packet_extend(p, size))) + goto fail; + + avahi_string_list_serialize(r->data.txt.string_list, data, size); + break; + } + + + case AVAHI_DNS_TYPE_A: + + if (!avahi_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address))) + goto fail; + + break; + + case AVAHI_DNS_TYPE_AAAA: + + if (!avahi_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address))) + goto fail; + + break; + + default: + + if (r->data.generic.size && + avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size)) + goto fail; + + break; + } + + + + + size = avahi_dns_packet_extend(p, 0) - start; + g_assert(size <= 0xFFFF); + +/* g_message("appended %u", size); */ + + * (guint16*) l = g_htons((guint16) size); + + return t; + + +fail: + p->size = size; + return NULL; +} + +gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p) { + g_assert(p); + + return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE; +} + +guint avahi_dns_packet_space(AvahiDnsPacket *p) { + g_assert(p); + + g_assert(p->size <= p->max_size); + + return p->max_size - p->size; +} diff --git a/avahi-core/dns.h b/avahi-core/dns.h new file mode 100644 index 0000000..03d9798 --- /dev/null +++ b/avahi-core/dns.h @@ -0,0 +1,105 @@ +#ifndef foodnshfoo +#define foodnshfoo + +/* $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. +***/ + +#include + +#include "rr.h" + +#define AVAHI_DNS_PACKET_MAX_SIZE 9000 +#define AVAHI_DNS_PACKET_HEADER_SIZE 12 +#define AVAHI_DNS_PACKET_EXTRA_SIZE 48 + +typedef struct AvahiDnsPacket { + guint size, rindex, max_size; + GHashTable *name_table; /* for name compression */ +} AvahiDnsPacket; + +#define AVAHI_DNS_PACKET_DATA(p) (((guint8*) p) + sizeof(AvahiDnsPacket)) + +AvahiDnsPacket* avahi_dns_packet_new(guint mtu); +AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu); +AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa); + +AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa); + +void avahi_dns_packet_free(AvahiDnsPacket *p); +void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint index, guint16 v); +guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint index); +void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint index); + +guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l); + +guint8 *avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v); +guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v); +guint8 *avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name); +guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer, guint l); +guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response); +guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl); +guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s); + +gint avahi_dns_packet_is_query(AvahiDnsPacket *p); +gint avahi_dns_packet_check_valid(AvahiDnsPacket *p); + +gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v); +gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v); +gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l); +gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l); +AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response); +AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush); +gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l); + +gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p); + +gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length); + +gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p); +guint avahi_dns_packet_space(AvahiDnsPacket *p); + +#define AVAHI_DNS_FIELD_ID 0 +#define AVAHI_DNS_FIELD_FLAGS 1 +#define AVAHI_DNS_FIELD_QDCOUNT 2 +#define AVAHI_DNS_FIELD_ANCOUNT 3 +#define AVAHI_DNS_FIELD_NSCOUNT 4 +#define AVAHI_DNS_FIELD_ARCOUNT 5 + +#define AVAHI_DNS_FLAG_QR (1 << 15) +#define AVAHI_DNS_FLAG_OPCODE (15 << 11) +#define AVAHI_DNS_FLAG_RCODE (15) +#define AVAHI_DNS_FLAG_TC (1 << 9) +#define AVAHI_DNS_FLAG_AA (1 << 10) + +#define AVAHI_DNS_FLAGS(qr, opcode, aa, tc, rd, ra, z, ad, cd, rcode) \ + (((guint16) !!qr << 15) | \ + ((guint16) (opcode & 15) << 11) | \ + ((guint16) !!aa << 10) | \ + ((guint16) !!tc << 9) | \ + ((guint16) !!rd << 8) | \ + ((guint16) !!ra << 7) | \ + ((guint16) !!ad << 5) | \ + ((guint16) !!cd << 4) | \ + ((guint16) (rd & 15))) + + +#endif +