4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <netinet/in.h>
33 #include <avahi-common/domain.h>
34 #include <avahi-common/malloc.h>
39 AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu) {
44 max_size = AVAHI_DNS_PACKET_MAX_SIZE;
45 else if (mtu >= AVAHI_DNS_PACKET_EXTRA_SIZE)
46 max_size = mtu - AVAHI_DNS_PACKET_EXTRA_SIZE;
50 if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE)
51 max_size = AVAHI_DNS_PACKET_HEADER_SIZE;
53 if (!(p = avahi_malloc(sizeof(AvahiDnsPacket) + max_size)))
56 p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
57 p->max_size = max_size;
60 memset(AVAHI_DNS_PACKET_DATA(p), 0, p->size);
64 AvahiDnsPacket* avahi_dns_packet_new_query(unsigned mtu) {
67 if (!(p = avahi_dns_packet_new(mtu)))
70 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
74 AvahiDnsPacket* avahi_dns_packet_new_response(unsigned mtu, int aa) {
77 if (!(p = avahi_dns_packet_new(mtu)))
80 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(1, 0, aa, 0, 0, 0, 0, 0, 0, 0));
84 AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, unsigned mtu, int copy_queries, int aa) {
88 if (!(r = avahi_dns_packet_new_response(mtu, aa)))
92 unsigned saved_rindex;
95 saved_rindex = p->rindex;
96 p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
98 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) {
100 int unicast_response;
102 if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) {
103 avahi_dns_packet_append_key(r, k, unicast_response);
108 p->rindex = saved_rindex;
110 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_QDCOUNT, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT));
113 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_ID, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID));
115 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_FLAGS,
116 (avahi_dns_packet_get_field(r, AVAHI_DNS_FIELD_FLAGS) & ~AVAHI_DNS_FLAG_OPCODE) |
117 (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_OPCODE));
123 void avahi_dns_packet_free(AvahiDnsPacket *p) {
127 avahi_hashmap_free(p->name_table);
132 void avahi_dns_packet_set_field(AvahiDnsPacket *p, unsigned idx, uint16_t v) {
134 assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE);
136 ((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx] = htons(v);
139 uint16_t avahi_dns_packet_get_field(AvahiDnsPacket *p, unsigned idx) {
141 assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE);
143 return ntohs(((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx]);
146 void avahi_dns_packet_inc_field(AvahiDnsPacket *p, unsigned idx) {
148 assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE);
150 avahi_dns_packet_set_field(p, idx, avahi_dns_packet_get_field(p, idx) + 1);
153 uint8_t* avahi_dns_packet_append_name(AvahiDnsPacket *p, const char *name) {
154 uint8_t *d, *saved_ptr = NULL;
160 saved_size = p->size;
161 saved_ptr = avahi_dns_packet_extend(p, 0);
168 /* Check whether we can compress this name. */
170 if (p->name_table && (prev = avahi_hashmap_lookup(p->name_table, name))) {
173 assert(prev >= AVAHI_DNS_PACKET_DATA(p));
174 idx = (unsigned) (prev - AVAHI_DNS_PACKET_DATA(p));
176 assert(idx < p->size);
180 if (!(t = (uint8_t*) avahi_dns_packet_extend(p, sizeof(uint16_t))))
183 t[0] = (uint8_t) ((0xC000 | idx) >> 8);
184 t[1] = (uint8_t) idx;
191 if (!(avahi_unescape_label(&name, label, sizeof(label))))
194 if (!(d = avahi_dns_packet_append_string(p, label)))
198 /* This works only for normalized domain names */
199 p->name_table = avahi_hashmap_new(avahi_string_hash, avahi_string_equal, avahi_free, NULL);
201 if (!(u = avahi_strdup(pname)))
202 avahi_log_error("avahi_strdup() failed.");
204 avahi_hashmap_insert(p->name_table, u, d);
207 if (!(d = avahi_dns_packet_extend(p, 1)))
215 p->size = saved_size;
219 uint8_t* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, uint16_t v) {
223 if (!(d = avahi_dns_packet_extend(p, sizeof(uint16_t))))
226 d[0] = (uint8_t) (v >> 8);
231 uint8_t *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, uint32_t v) {
235 if (!(d = avahi_dns_packet_extend(p, sizeof(uint32_t))))
238 d[0] = (uint8_t) (v >> 24);
239 d[1] = (uint8_t) (v >> 16);
240 d[2] = (uint8_t) (v >> 8);
246 uint8_t *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, const void *b, size_t l) {
253 if (!(d = avahi_dns_packet_extend(p, l)))
260 uint8_t* avahi_dns_packet_append_string(AvahiDnsPacket *p, const char *s) {
267 if ((k = strlen(s)) >= 255)
270 if (!(d = avahi_dns_packet_extend(p, k+1)))
279 uint8_t *avahi_dns_packet_extend(AvahiDnsPacket *p, size_t l) {
284 if (p->size+l > p->max_size)
287 d = AVAHI_DNS_PACKET_DATA(p) + p->size;
293 int avahi_dns_packet_check_valid(AvahiDnsPacket *p) {
297 if (p->size < AVAHI_DNS_PACKET_HEADER_SIZE)
300 flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS);
302 if (flags & AVAHI_DNS_FLAG_OPCODE)
308 int avahi_dns_packet_check_valid_multicast(AvahiDnsPacket *p) {
312 if (avahi_dns_packet_check_valid(p) < 0)
315 flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS);
317 if (flags & AVAHI_DNS_FLAG_RCODE)
324 int avahi_dns_packet_is_query(AvahiDnsPacket *p) {
327 return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR);
330 static int consume_labels(AvahiDnsPacket *p, unsigned idx, char *ret_name, size_t l) {
334 assert(p && ret_name && l);
342 n = AVAHI_DNS_PACKET_DATA(p)[idx];
355 } else if (n <= 63) {
356 /* Uncompressed label */
361 if (idx + n > p->size)
364 if ((size_t) n + 1 > l)
373 if (!(avahi_escape_label((char*) AVAHI_DNS_PACKET_DATA(p) + idx, n, &ret_name, &l)))
380 } else if ((n & 0xC0) == 0xC0) {
381 /* Compressed label */
386 idx = ((unsigned) (AVAHI_DNS_PACKET_DATA(p)[idx] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[idx+1];
397 int avahi_dns_packet_consume_name(AvahiDnsPacket *p, char *ret_name, size_t l) {
400 if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0)
407 int avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, uint16_t *ret_v) {
413 if (p->rindex + sizeof(uint16_t) > p->size)
416 d = (uint8_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex);
417 *ret_v = (d[0] << 8) | d[1];
418 p->rindex += sizeof(uint16_t);
423 int avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, uint32_t *ret_v) {
429 if (p->rindex + sizeof(uint32_t) > p->size)
432 d = (uint8_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex);
433 *ret_v = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
434 p->rindex += sizeof(uint32_t);
439 int avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, void * ret_data, size_t l) {
444 if (p->rindex + l > p->size)
447 memcpy(ret_data, AVAHI_DNS_PACKET_DATA(p) + p->rindex, l);
453 int avahi_dns_packet_consume_string(AvahiDnsPacket *p, char *ret_string, size_t l) {
460 if (p->rindex >= p->size)
463 k = AVAHI_DNS_PACKET_DATA(p)[p->rindex];
465 if (p->rindex+1+k > p->size)
471 memcpy(ret_string, AVAHI_DNS_PACKET_DATA(p)+p->rindex+1, l-1);
479 const void* avahi_dns_packet_get_rptr(AvahiDnsPacket *p) {
482 if (p->rindex > p->size)
485 return AVAHI_DNS_PACKET_DATA(p) + p->rindex;
488 int avahi_dns_packet_skip(AvahiDnsPacket *p, size_t length) {
491 if (p->rindex + length > p->size)
498 AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_flush) {
499 char name[257], buf[257];
500 uint16_t type, class;
503 AvahiRecord *r = NULL;
508 /* avahi_log_debug("consume_record()"); */
510 if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
511 avahi_dns_packet_consume_uint16(p, &type) < 0 ||
512 avahi_dns_packet_consume_uint16(p, &class) < 0 ||
513 avahi_dns_packet_consume_uint32(p, &ttl) < 0 ||
514 avahi_dns_packet_consume_uint16(p, &rdlength) < 0 ||
515 p->rindex + rdlength > p->size)
518 /* avahi_log_debug("name = %s, rdlength = %u", name, rdlength); */
521 *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH);
522 class &= ~AVAHI_DNS_CACHE_FLUSH;
524 start = avahi_dns_packet_get_rptr(p);
526 if (!(r = avahi_record_new_full(name, class, type, ttl)))
530 case AVAHI_DNS_TYPE_PTR:
531 case AVAHI_DNS_TYPE_CNAME:
532 case AVAHI_DNS_TYPE_NS:
534 /* avahi_log_debug("ptr"); */
536 if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
539 r->data.ptr.name = avahi_strdup(buf);
543 case AVAHI_DNS_TYPE_SRV:
545 /* avahi_log_debug("srv"); */
547 if (avahi_dns_packet_consume_uint16(p, &r->data.srv.priority) < 0 ||
548 avahi_dns_packet_consume_uint16(p, &r->data.srv.weight) < 0 ||
549 avahi_dns_packet_consume_uint16(p, &r->data.srv.port) < 0 ||
550 avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
553 r->data.srv.name = avahi_strdup(buf);
556 case AVAHI_DNS_TYPE_HINFO:
558 /* avahi_log_debug("hinfo"); */
560 if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
563 r->data.hinfo.cpu = avahi_strdup(buf);
565 if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
568 r->data.hinfo.os = avahi_strdup(buf);
571 case AVAHI_DNS_TYPE_TXT:
573 /* avahi_log_debug("txt"); */
576 r->data.txt.string_list = avahi_string_list_parse(avahi_dns_packet_get_rptr(p), rdlength);
578 if (avahi_dns_packet_skip(p, rdlength) < 0)
581 r->data.txt.string_list = NULL;
585 case AVAHI_DNS_TYPE_A:
587 /* avahi_log_debug("A"); */
589 if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0)
594 case AVAHI_DNS_TYPE_AAAA:
596 /* avahi_log_debug("aaaa"); */
598 if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0)
605 /* avahi_log_debug("generic"); */
609 r->data.generic.data = avahi_memdup(avahi_dns_packet_get_rptr(p), rdlength);
611 if (avahi_dns_packet_skip(p, rdlength) < 0)
618 /* avahi_log_debug("%i == %u ?", (uint8_t*) avahi_dns_packet_get_rptr(p) - (uint8_t*) start, rdlength); */
620 /* Check if we read enough data */
621 if ((const uint8_t*) avahi_dns_packet_get_rptr(p) - (const uint8_t*) start != rdlength)
628 avahi_record_unref(r);
633 AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, int *ret_unicast_response) {
635 uint16_t type, class;
639 if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
640 avahi_dns_packet_consume_uint16(p, &type) < 0 ||
641 avahi_dns_packet_consume_uint16(p, &class) < 0)
644 if (ret_unicast_response)
645 *ret_unicast_response = !!(class & AVAHI_DNS_UNICAST_RESPONSE);
647 class &= ~AVAHI_DNS_UNICAST_RESPONSE;
649 return avahi_key_new(name, class, type);
652 uint8_t* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, int unicast_response) {
661 if (!(t = avahi_dns_packet_append_name(p, k->name)) ||
662 !avahi_dns_packet_append_uint16(p, k->type) ||
663 !avahi_dns_packet_append_uint16(p, k->clazz | (unicast_response ? AVAHI_DNS_UNICAST_RESPONSE : 0))) {
671 uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int cache_flush, unsigned max_ttl) {
672 uint8_t *t, *l, *start;
680 if (!(t = avahi_dns_packet_append_name(p, r->key->name)) ||
681 !avahi_dns_packet_append_uint16(p, r->key->type) ||
682 !avahi_dns_packet_append_uint16(p, cache_flush ? (r->key->clazz | AVAHI_DNS_CACHE_FLUSH) : (r->key->clazz &~ AVAHI_DNS_CACHE_FLUSH)) ||
683 !avahi_dns_packet_append_uint32(p, (max_ttl && r->ttl > max_ttl) ? max_ttl : r->ttl) ||
684 !(l = avahi_dns_packet_append_uint16(p, 0)))
687 start = avahi_dns_packet_extend(p, 0);
689 switch (r->key->type) {
691 case AVAHI_DNS_TYPE_PTR:
692 case AVAHI_DNS_TYPE_CNAME:
693 case AVAHI_DNS_TYPE_NS:
695 if (!(avahi_dns_packet_append_name(p, r->data.ptr.name)))
700 case AVAHI_DNS_TYPE_SRV:
702 if (!avahi_dns_packet_append_uint16(p, r->data.srv.priority) ||
703 !avahi_dns_packet_append_uint16(p, r->data.srv.weight) ||
704 !avahi_dns_packet_append_uint16(p, r->data.srv.port) ||
705 !avahi_dns_packet_append_name(p, r->data.srv.name))
710 case AVAHI_DNS_TYPE_HINFO:
711 if (!avahi_dns_packet_append_string(p, r->data.hinfo.cpu) ||
712 !avahi_dns_packet_append_string(p, r->data.hinfo.os))
717 case AVAHI_DNS_TYPE_TXT: {
722 n = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
724 /* avahi_log_debug("appending string: %u %p", n, r->data.txt.string_list); */
726 if (!(data = avahi_dns_packet_extend(p, n)))
729 avahi_string_list_serialize(r->data.txt.string_list, data, n);
734 case AVAHI_DNS_TYPE_A:
736 if (!avahi_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address)))
741 case AVAHI_DNS_TYPE_AAAA:
743 if (!avahi_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address)))
750 if (r->data.generic.size &&
751 avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
760 size = avahi_dns_packet_extend(p, 0) - start;
761 assert(size <= 0xFFFF);
763 /* avahi_log_debug("appended %u", size); */
765 l[0] = (uint8_t) ((uint16_t) size >> 8);
766 l[1] = (uint8_t) ((uint16_t) size);
776 int avahi_dns_packet_is_empty(AvahiDnsPacket *p) {
779 return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE;
782 size_t avahi_dns_packet_space(AvahiDnsPacket *p) {
785 assert(p->size <= p->max_size);
787 return p->max_size - p->size;