diff options
Diffstat (limited to 'avahi-core/dns.c')
-rw-r--r-- | avahi-core/dns.c | 310 |
1 files changed, 163 insertions, 147 deletions
diff --git a/avahi-core/dns.c b/avahi-core/dns.c index 9663fc4..9bc7aec 100644 --- a/avahi-core/dns.c +++ b/avahi-core/dns.c @@ -28,13 +28,17 @@ #include <stdlib.h> #include <string.h> #include <stdio.h> +#include <assert.h> #include <avahi-common/domain.h> +#include <avahi-common/malloc.h> + #include "dns.h" +#include "log.h" -AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { +AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu) { AvahiDnsPacket *p; - guint max_size; + size_t max_size; if (mtu <= 0) max_size = AVAHI_DNS_PACKET_MAX_SIZE; @@ -46,7 +50,9 @@ AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE) max_size = AVAHI_DNS_PACKET_HEADER_SIZE; - p = g_malloc(sizeof(AvahiDnsPacket) + max_size); + if (!(p = avahi_malloc(sizeof(AvahiDnsPacket) + max_size))) + return p; + p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE; p->max_size = max_size; p->name_table = NULL; @@ -55,37 +61,43 @@ AvahiDnsPacket* avahi_dns_packet_new(guint mtu) { return p; } -AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu) { +AvahiDnsPacket* avahi_dns_packet_new_query(unsigned mtu) { AvahiDnsPacket *p; - p = avahi_dns_packet_new(mtu); + if (!(p = avahi_dns_packet_new(mtu))) + return NULL; + 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* avahi_dns_packet_new_response(unsigned mtu, int aa) { AvahiDnsPacket *p; - p = avahi_dns_packet_new(mtu); + if (!(p = avahi_dns_packet_new(mtu))) + return NULL; + 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* avahi_dns_packet_new_reply(AvahiDnsPacket* p, unsigned mtu, int copy_queries, int aa) { AvahiDnsPacket *r; - g_assert(p); + assert(p); - r = avahi_dns_packet_new_response(mtu, aa); + if (!(r = avahi_dns_packet_new_response(mtu, aa))) + return NULL; if (copy_queries) { - guint n, saved_rindex; + unsigned saved_rindex; + uint32_t n; 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; + int unicast_response; if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) { avahi_dns_packet_append_key(r, k, unicast_response); @@ -109,66 +121,67 @@ AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolea void avahi_dns_packet_free(AvahiDnsPacket *p) { - g_assert(p); + assert(p); if (p->name_table) - g_hash_table_destroy(p->name_table); + avahi_hashmap_free(p->name_table); - g_free(p); + avahi_free(p); } -void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint idx, guint16 v) { - g_assert(p); - g_assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); +void avahi_dns_packet_set_field(AvahiDnsPacket *p, unsigned idx, uint16_t v) { + assert(p); + assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); - ((guint16*) AVAHI_DNS_PACKET_DATA(p))[idx] = g_htons(v); + ((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx] = htons(v); } -guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint idx) { - g_assert(p); - g_assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); +uint16_t avahi_dns_packet_get_field(AvahiDnsPacket *p, unsigned idx) { + assert(p); + assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); - return g_ntohs(((guint16*) AVAHI_DNS_PACKET_DATA(p))[idx]); + return ntohs(((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx]); } -void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint idx) { - g_assert(p); - g_assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); +void avahi_dns_packet_inc_field(AvahiDnsPacket *p, unsigned idx) { + assert(p); + assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE); avahi_dns_packet_set_field(p, idx, avahi_dns_packet_get_field(p, idx) + 1); } -guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) { - guint8 *d, *saved_ptr = NULL; - guint saved_size; +uint8_t* avahi_dns_packet_append_name(AvahiDnsPacket *p, const char *name) { + uint8_t *d, *saved_ptr = NULL; + size_t saved_size; - g_assert(p); - g_assert(name); + assert(p); + assert(name); saved_size = p->size; saved_ptr = avahi_dns_packet_extend(p, 0); while (*name) { - guint8* prev; - const gchar *pname; - gchar label[64]; + uint8_t* prev; + const char *pname; + char label[64], *u; + /* Check whether we can compress this name. */ - if (p->name_table && (prev = g_hash_table_lookup(p->name_table, name))) { - guint idx; + if (p->name_table && (prev = avahi_hashmap_lookup(p->name_table, name))) { + unsigned idx; - g_assert(prev >= AVAHI_DNS_PACKET_DATA(p)); - idx = (guint) (prev - AVAHI_DNS_PACKET_DATA(p)); + assert(prev >= AVAHI_DNS_PACKET_DATA(p)); + idx = (unsigned) (prev - AVAHI_DNS_PACKET_DATA(p)); - g_assert(idx < p->size); + assert(idx < p->size); if (idx < 0x4000) { - guint16 *t; - if (!(t = (guint16*) avahi_dns_packet_extend(p, sizeof(guint16)))) + uint16_t *t; + if (!(t = (uint16_t*) avahi_dns_packet_extend(p, sizeof(uint16_t)))) return NULL; - *t = g_htons((0xC000 | idx)); + *t = htons((0xC000 | idx)); return saved_ptr; } } @@ -183,9 +196,12 @@ guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) { 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); + p->name_table = avahi_hashmap_new(avahi_string_hash, avahi_string_equal, avahi_free, NULL); - g_hash_table_insert(p->name_table, g_strdup(pname), d); + if (!(u = avahi_strdup(pname))) + avahi_log_error("avahi_strdup() failed."); + else + avahi_hashmap_insert(p->name_table, u, d); } if (!(d = avahi_dns_packet_extend(p, 1))) @@ -200,35 +216,35 @@ fail: return NULL; } -guint8* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v) { - guint8 *d; - g_assert(p); +uint8_t* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, uint16_t v) { + uint8_t *d; + assert(p); - if (!(d = avahi_dns_packet_extend(p, sizeof(guint16)))) + if (!(d = avahi_dns_packet_extend(p, sizeof(uint16_t)))) return NULL; - *((guint16*) d) = g_htons(v); + *((uint16_t*) d) = htons(v); return d; } -guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v) { - guint8 *d; - g_assert(p); +uint8_t *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, uint32_t v) { + uint8_t *d; + assert(p); - if (!(d = avahi_dns_packet_extend(p, sizeof(guint32)))) + if (!(d = avahi_dns_packet_extend(p, sizeof(uint32_t)))) return NULL; - *((guint32*) d) = g_htonl(v); + *((uint32_t*) d) = htonl(v); return d; } -guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer b, guint l) { - guint8* d; +uint8_t *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, const void *b, size_t l) { + uint8_t* d; - g_assert(p); - g_assert(b); - g_assert(l); + assert(p); + assert(b); + assert(l); if (!(d = avahi_dns_packet_extend(p, l))) return NULL; @@ -237,12 +253,12 @@ guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer b, guint return d; } -guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) { - guint8* d; - guint k; +uint8_t* avahi_dns_packet_append_string(AvahiDnsPacket *p, const char *s) { + uint8_t* d; + size_t k; - g_assert(p); - g_assert(s); + assert(p); + assert(s); if ((k = strlen(s)) >= 255) k = 255; @@ -250,16 +266,16 @@ guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) { if (!(d = avahi_dns_packet_extend(p, k+1))) return NULL; - *d = (guint8) k; + *d = (uint8_t) k; memcpy(d+1, s, k); return d; } -guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) { - guint8 *d; +uint8_t *avahi_dns_packet_extend(AvahiDnsPacket *p, size_t l) { + uint8_t *d; - g_assert(p); + assert(p); if (p->size+l > p->max_size) return NULL; @@ -270,9 +286,9 @@ guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) { return d; } -gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) { - guint16 flags; - g_assert(p); +int avahi_dns_packet_is_valid(AvahiDnsPacket *p) { + uint16_t flags; + assert(p); if (p->size < 12) return -1; @@ -285,20 +301,20 @@ gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) { return 0; } -gint avahi_dns_packet_is_query(AvahiDnsPacket *p) { - g_assert(p); +int avahi_dns_packet_is_query(AvahiDnsPacket *p) { + assert(p); return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR); } -static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint l) { - gint ret = 0; +static int consume_labels(AvahiDnsPacket *p, unsigned idx, char *ret_name, size_t l) { + int ret = 0; int compressed = 0; int first_label = 1; - g_assert(p && ret_name && l); + assert(p && ret_name && l); for (;;) { - guint8 n; + uint8_t n; if (idx+1 > p->size) return -1; @@ -325,7 +341,7 @@ static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint if (idx + n > p->size) return -1; - if ((guint) n + 1 > l) + if ((size_t) n + 1 > l) return -1; if (!first_label) { @@ -347,7 +363,7 @@ static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint if (idx+2 > p->size) return -1; - idx = ((guint) (AVAHI_DNS_PACKET_DATA(p)[idx] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[idx+1]; + idx = ((unsigned) (AVAHI_DNS_PACKET_DATA(p)[idx] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[idx+1]; if (!compressed) ret += 2; @@ -358,8 +374,8 @@ static gint consume_labels(AvahiDnsPacket *p, guint idx, gchar *ret_name, guint } } -gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) { - gint r; +int avahi_dns_packet_consume_name(AvahiDnsPacket *p, char *ret_name, size_t l) { + int r; if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0) return -1; @@ -368,36 +384,36 @@ gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) return 0; } -gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v) { - g_assert(p); - g_assert(ret_v); +int avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, uint16_t *ret_v) { + assert(p); + assert(ret_v); - if (p->rindex + sizeof(guint16) > p->size) + if (p->rindex + sizeof(uint16_t) > p->size) return -1; - *ret_v = g_ntohs(*((guint16*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); - p->rindex += sizeof(guint16); + *ret_v = ntohs(*((uint16_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); + p->rindex += sizeof(uint16_t); return 0; } -gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v) { - g_assert(p); - g_assert(ret_v); +int avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, uint32_t *ret_v) { + assert(p); + assert(ret_v); - if (p->rindex + sizeof(guint32) > p->size) + if (p->rindex + sizeof(uint32_t) > p->size) return -1; - *ret_v = g_ntohl(*((guint32*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); - p->rindex += sizeof(guint32); + *ret_v = ntohl(*((uint32_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex))); + p->rindex += sizeof(uint32_t); 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); +int avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, void * ret_data, size_t l) { + assert(p); + assert(ret_data); + assert(l > 0); if (p->rindex + l > p->size) return -1; @@ -408,12 +424,12 @@ gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint return 0; } -gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l) { - guint k; +int avahi_dns_packet_consume_string(AvahiDnsPacket *p, char *ret_string, size_t l) { + size_t k; - g_assert(p); - g_assert(ret_string); - g_assert(l > 0); + assert(p); + assert(ret_string); + assert(l > 0); if (p->rindex >= p->size) return -1; @@ -428,15 +444,14 @@ gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint 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); +const void* avahi_dns_packet_get_rptr(AvahiDnsPacket *p) { + assert(p); if (p->rindex > p->size) return NULL; @@ -444,8 +459,8 @@ gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p) { return AVAHI_DNS_PACKET_DATA(p) + p->rindex; } -gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length) { - g_assert(p); +int avahi_dns_packet_skip(AvahiDnsPacket *p, size_t length) { + assert(p); if (p->rindex + length > p->size) return -1; @@ -454,16 +469,16 @@ gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint 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* 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; - gconstpointer start; + const void* start; - g_assert(p); - g_assert(ret_cache_flush); + assert(p); + assert(ret_cache_flush); /* avahi_log_debug("consume_record()"); */ @@ -482,7 +497,8 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca start = avahi_dns_packet_get_rptr(p); - r = avahi_record_new_full(name, class, type, ttl); + if (!(r = avahi_record_new_full(name, class, type, ttl))) + return NULL; switch (type) { case AVAHI_DNS_TYPE_PTR: @@ -493,7 +509,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0) goto fail; - r->data.ptr.name = g_strdup(buf); + r->data.ptr.name = avahi_strdup(buf); break; @@ -507,7 +523,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0) goto fail; - r->data.srv.name = g_strdup(buf); + r->data.srv.name = avahi_strdup(buf); break; case AVAHI_DNS_TYPE_HINFO: @@ -517,12 +533,12 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) goto fail; - r->data.hinfo.cpu = g_strdup(buf); + r->data.hinfo.cpu = avahi_strdup(buf); if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0) goto fail; - r->data.hinfo.os = g_strdup(buf); + r->data.hinfo.os = avahi_strdup(buf); break; case AVAHI_DNS_TYPE_TXT: @@ -563,7 +579,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca if (rdlength > 0) { - r->data.generic.data = g_memdup(avahi_dns_packet_get_rptr(p), rdlength); + r->data.generic.data = avahi_memdup(avahi_dns_packet_get_rptr(p), rdlength); if (avahi_dns_packet_skip(p, rdlength) < 0) goto fail; @@ -572,10 +588,10 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca break; } -/* avahi_log_debug("%i == %u ?", (guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start, rdlength); */ +/* 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 guint8*) avahi_dns_packet_get_rptr(p) - (const guint8*) start != rdlength) + if ((const uint8_t*) avahi_dns_packet_get_rptr(p) - (const uint8_t*) start != rdlength) goto fail; return r; @@ -587,12 +603,12 @@ fail: return NULL; } -AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response) { - gchar name[256]; - guint16 type, class; +AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, int *ret_unicast_response) { + char name[256]; + uint16_t type, class; - g_assert(p); - g_assert(ret_unicast_response); + assert(p); + assert(ret_unicast_response); if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 || avahi_dns_packet_consume_uint16(p, &type) < 0 || @@ -605,12 +621,12 @@ AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_ return avahi_key_new(name, class, type); } -guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response) { - guint8 *t; - guint size; +uint8_t* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, int unicast_response) { + uint8_t *t; + size_t size; - g_assert(p); - g_assert(k); + assert(p); + assert(k); size = p->size; @@ -624,12 +640,12 @@ guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean uni return t; } -guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl) { - guint8 *t, *l, *start; - guint size; +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; - g_assert(p); - g_assert(r); + assert(p); + assert(r); size = p->size; @@ -671,8 +687,8 @@ guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboole case AVAHI_DNS_TYPE_TXT: { - guint8 *data; - guint n; + uint8_t *data; + size_t n; n = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0); @@ -713,11 +729,11 @@ guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboole size = avahi_dns_packet_extend(p, 0) - start; - g_assert(size <= 0xFFFF); + assert(size <= 0xFFFF); /* avahi_log_debug("appended %u", size); */ - * (guint16*) l = g_htons((guint16) size); + * (uint16_t*) l = htons((uint16_t) size); return t; @@ -727,16 +743,16 @@ fail: return NULL; } -gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p) { - g_assert(p); +int avahi_dns_packet_is_empty(AvahiDnsPacket *p) { + assert(p); return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE; } -guint avahi_dns_packet_space(AvahiDnsPacket *p) { - g_assert(p); +size_t avahi_dns_packet_space(AvahiDnsPacket *p) { + assert(p); - g_assert(p->size <= p->max_size); + assert(p->size <= p->max_size); return p->max_size - p->size; } |