summaryrefslogtreecommitdiffstats
path: root/avahi-core/dns.c
diff options
context:
space:
mode:
Diffstat (limited to 'avahi-core/dns.c')
-rw-r--r--avahi-core/dns.c772
1 files changed, 772 insertions, 0 deletions
diff --git a/avahi-core/dns.c b/avahi-core/dns.c
new file mode 100644
index 0000000..cc1463a
--- /dev/null
+++ b/avahi-core/dns.c
@@ -0,0 +1,772 @@
+/* $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 <config.h>
+#endif
+
+#include <netinet/in.h>
+
+#include <stdlib.h>
+#include <string.h>
+#include <stdio.h>
+
+#include "dns.h"
+#include "util.h"
+
+AvahiDnsPacket* avahi_dns_packet_new(guint max_size) {
+ AvahiDnsPacket *p;
+
+ if (max_size <= 0)
+ max_size = AVAHI_DNS_PACKET_MAX_SIZE;
+ else 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 max_size) {
+ AvahiDnsPacket *p;
+
+ p = avahi_dns_packet_new(max_size);
+ 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 max_size) {
+ AvahiDnsPacket *p;
+
+ p = avahi_dns_packet_new(max_size);
+ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
+ return p;
+}
+
+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]);
+}
+
+/* Read the first label from string *name, unescape "\" and write it to dest */
+gchar *avahi_unescape_label(gchar *dest, guint size, const gchar **name) {
+ guint i = 0;
+ gchar *d;
+
+ g_assert(dest);
+ g_assert(size > 0);
+ g_assert(name);
+ g_assert(*name);
+
+ d = dest;
+
+ for (;;) {
+ if (i >= size)
+ return NULL;
+
+ if (**name == '.') {
+ (*name)++;
+ break;
+ }
+
+ if (**name == 0)
+ break;
+
+ if (**name == '\\') {
+ (*name) ++;
+
+ if (**name == 0)
+ break;
+ }
+
+ *(d++) = *((*name) ++);
+ i++;
+ }
+
+ g_assert(i < size);
+
+ *d = 0;
+
+ return dest;
+}
+
+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(label, sizeof(label), &name)))
+ goto fail;
+
+ if (!(d = avahi_dns_packet_append_string(p, label)))
+ goto fail;
+
+ if (!p->name_table)
+ p->name_table = g_hash_table_new_full((GHashFunc) avahi_domain_hash, (GEqualFunc) avahi_domain_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);
+}
+
+/* Read a label from a DNS packet, escape "\" and ".", append \0 */
+static gchar *escape_label(guint8* src, guint src_length, gchar **ret_name, guint *ret_name_length) {
+ gchar *r;
+
+ g_assert(src);
+ g_assert(ret_name);
+ g_assert(*ret_name);
+ g_assert(ret_name_length);
+ g_assert(*ret_name_length > 0);
+
+ r = *ret_name;
+
+ while (src_length > 0) {
+ if (*src == '.' || *src == '\\') {
+ if (*ret_name_length < 3)
+ return NULL;
+
+ *((*ret_name) ++) = '\\';
+ (*ret_name_length) --;
+ }
+
+ if (*ret_name_length < 2)
+ return NULL;
+
+ *((*ret_name)++) = *src;
+ (*ret_name_length) --;
+
+ src_length --;
+ src++;
+ }
+
+ **ret_name = 0;
+
+ return r;
+}
+
+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 (!(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) {
+ 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, 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;
+}