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
31 #include <sys/types.h>
32 #include <netinet/in.h>
34 #include <avahi-common/defs.h>
35 #include <avahi-common/domain.h>
36 #include <avahi-common/malloc.h>
41 AvahiDnsPacket* avahi_dns_packet_new(unsigned mtu) {
46 max_size = AVAHI_DNS_PACKET_SIZE_MAX;
47 else if (mtu >= AVAHI_DNS_PACKET_EXTRA_SIZE)
48 max_size = mtu - AVAHI_DNS_PACKET_EXTRA_SIZE;
52 if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE)
53 max_size = AVAHI_DNS_PACKET_HEADER_SIZE;
55 if (!(p = avahi_malloc(sizeof(AvahiDnsPacket) + max_size)))
58 p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
59 p->max_size = max_size;
63 memset(AVAHI_DNS_PACKET_DATA(p), 0, p->size);
67 AvahiDnsPacket* avahi_dns_packet_new_query(unsigned mtu) {
70 if (!(p = avahi_dns_packet_new(mtu)))
73 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
77 AvahiDnsPacket* avahi_dns_packet_new_response(unsigned mtu, int aa) {
80 if (!(p = avahi_dns_packet_new(mtu)))
83 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(1, 0, aa, 0, 0, 0, 0, 0, 0, 0));
87 AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, unsigned mtu, int copy_queries, int aa) {
91 if (!(r = avahi_dns_packet_new_response(mtu, aa)))
95 unsigned saved_rindex;
98 saved_rindex = p->rindex;
99 p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
101 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) {
103 int unicast_response;
105 if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) {
106 avahi_dns_packet_append_key(r, k, unicast_response);
111 p->rindex = saved_rindex;
113 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_QDCOUNT, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT));
116 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_ID, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID));
118 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_FLAGS,
119 (avahi_dns_packet_get_field(r, AVAHI_DNS_FIELD_FLAGS) & ~AVAHI_DNS_FLAG_OPCODE) |
120 (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_OPCODE));
126 void avahi_dns_packet_free(AvahiDnsPacket *p) {
130 avahi_hashmap_free(p->name_table);
135 void avahi_dns_packet_set_field(AvahiDnsPacket *p, unsigned idx, uint16_t v) {
137 assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE);
139 ((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx] = htons(v);
142 uint16_t avahi_dns_packet_get_field(AvahiDnsPacket *p, unsigned idx) {
144 assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE);
146 return ntohs(((uint16_t*) AVAHI_DNS_PACKET_DATA(p))[idx]);
149 void avahi_dns_packet_inc_field(AvahiDnsPacket *p, unsigned idx) {
151 assert(idx < AVAHI_DNS_PACKET_HEADER_SIZE);
153 avahi_dns_packet_set_field(p, idx, avahi_dns_packet_get_field(p, idx) + 1);
157 static void name_table_cleanup(void *key, void *value, void *user_data) {
158 AvahiDnsPacket *p = user_data;
160 if ((uint8_t*) value >= AVAHI_DNS_PACKET_DATA(p) + p->size)
161 avahi_hashmap_remove(p->name_table, key);
164 void avahi_dns_packet_cleanup_name_table(AvahiDnsPacket *p) {
166 avahi_hashmap_foreach(p->name_table, name_table_cleanup, p);
169 uint8_t* avahi_dns_packet_append_name(AvahiDnsPacket *p, const char *name) {
170 uint8_t *d, *saved_ptr = NULL;
176 saved_size = p->size;
177 saved_ptr = avahi_dns_packet_extend(p, 0);
184 /* Check whether we can compress this name. */
186 if (p->name_table && (prev = avahi_hashmap_lookup(p->name_table, name))) {
189 assert(prev >= AVAHI_DNS_PACKET_DATA(p));
190 idx = (unsigned) (prev - AVAHI_DNS_PACKET_DATA(p));
192 assert(idx < p->size);
196 if (!(t = (uint8_t*) avahi_dns_packet_extend(p, sizeof(uint16_t))))
199 t[0] = (uint8_t) ((0xC000 | idx) >> 8);
200 t[1] = (uint8_t) idx;
207 if (!(avahi_unescape_label(&name, label, sizeof(label))))
210 if (!(d = avahi_dns_packet_append_string(p, label)))
214 /* This works only for normalized domain names */
215 p->name_table = avahi_hashmap_new(avahi_string_hash, avahi_string_equal, avahi_free, NULL);
217 if (!(u = avahi_strdup(pname)))
218 avahi_log_error("avahi_strdup() failed.");
220 avahi_hashmap_insert(p->name_table, u, d);
223 if (!(d = avahi_dns_packet_extend(p, 1)))
231 p->size = saved_size;
232 avahi_dns_packet_cleanup_name_table(p);
237 uint8_t* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, uint16_t v) {
241 if (!(d = avahi_dns_packet_extend(p, sizeof(uint16_t))))
244 d[0] = (uint8_t) (v >> 8);
249 uint8_t *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, uint32_t v) {
253 if (!(d = avahi_dns_packet_extend(p, sizeof(uint32_t))))
256 d[0] = (uint8_t) (v >> 24);
257 d[1] = (uint8_t) (v >> 16);
258 d[2] = (uint8_t) (v >> 8);
264 uint8_t *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, const void *b, size_t l) {
271 if (!(d = avahi_dns_packet_extend(p, l)))
278 uint8_t* avahi_dns_packet_append_string(AvahiDnsPacket *p, const char *s) {
285 if ((k = strlen(s)) >= 255)
288 if (!(d = avahi_dns_packet_extend(p, k+1)))
297 uint8_t *avahi_dns_packet_extend(AvahiDnsPacket *p, size_t l) {
302 if (p->size+l > p->max_size)
305 d = AVAHI_DNS_PACKET_DATA(p) + p->size;
311 int avahi_dns_packet_check_valid(AvahiDnsPacket *p) {
315 if (p->size < AVAHI_DNS_PACKET_HEADER_SIZE)
318 flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS);
320 if (flags & AVAHI_DNS_FLAG_OPCODE)
326 int avahi_dns_packet_check_valid_multicast(AvahiDnsPacket *p) {
330 if (avahi_dns_packet_check_valid(p) < 0)
333 flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS);
335 if (flags & AVAHI_DNS_FLAG_RCODE)
341 int avahi_dns_packet_is_query(AvahiDnsPacket *p) {
344 return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR);
347 static int consume_labels(AvahiDnsPacket *p, unsigned idx, char *ret_name, size_t l) {
353 assert(p && ret_name && l);
355 for (i = 0; i < AVAHI_DNS_LABELS_MAX; i++) {
361 n = AVAHI_DNS_PACKET_DATA(p)[idx];
374 } else if (n <= 63) {
375 /* Uncompressed label */
380 if (idx + n > p->size)
383 if ((size_t) n + 1 > l)
392 if (!(avahi_escape_label((char*) AVAHI_DNS_PACKET_DATA(p) + idx, n, &ret_name, &l)))
399 } else if ((n & 0xC0) == 0xC0) {
400 /* Compressed label */
405 label_ptr = ((unsigned) (AVAHI_DNS_PACKET_DATA(p)[idx] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[idx+1];
407 if ((label_ptr < AVAHI_DNS_PACKET_HEADER_SIZE) || (label_ptr >= idx))
423 int avahi_dns_packet_consume_name(AvahiDnsPacket *p, char *ret_name, size_t l) {
426 if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0)
433 int avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, uint16_t *ret_v) {
439 if (p->rindex + sizeof(uint16_t) > p->size)
442 d = (uint8_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex);
443 *ret_v = (d[0] << 8) | d[1];
444 p->rindex += sizeof(uint16_t);
449 int avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, uint32_t *ret_v) {
455 if (p->rindex + sizeof(uint32_t) > p->size)
458 d = (uint8_t*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex);
459 *ret_v = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
460 p->rindex += sizeof(uint32_t);
465 int avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, void * ret_data, size_t l) {
470 if (p->rindex + l > p->size)
473 memcpy(ret_data, AVAHI_DNS_PACKET_DATA(p) + p->rindex, l);
479 int avahi_dns_packet_consume_string(AvahiDnsPacket *p, char *ret_string, size_t l) {
486 if (p->rindex >= p->size)
489 k = AVAHI_DNS_PACKET_DATA(p)[p->rindex];
491 if (p->rindex+1+k > p->size)
497 memcpy(ret_string, AVAHI_DNS_PACKET_DATA(p)+p->rindex+1, l-1);
505 const void* avahi_dns_packet_get_rptr(AvahiDnsPacket *p) {
508 if (p->rindex > p->size)
511 return AVAHI_DNS_PACKET_DATA(p) + p->rindex;
514 int avahi_dns_packet_skip(AvahiDnsPacket *p, size_t length) {
517 if (p->rindex + length > p->size)
524 static int parse_rdata(AvahiDnsPacket *p, AvahiRecord *r, uint16_t rdlength) {
525 char buf[AVAHI_DOMAIN_NAME_MAX];
531 start = avahi_dns_packet_get_rptr(p);
533 switch (r->key->type) {
534 case AVAHI_DNS_TYPE_PTR:
535 case AVAHI_DNS_TYPE_CNAME:
536 case AVAHI_DNS_TYPE_NS:
538 if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
541 r->data.ptr.name = avahi_strdup(buf);
545 case AVAHI_DNS_TYPE_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 if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
561 r->data.hinfo.cpu = avahi_strdup(buf);
563 if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
566 r->data.hinfo.os = avahi_strdup(buf);
569 case AVAHI_DNS_TYPE_TXT:
572 if (avahi_string_list_parse(avahi_dns_packet_get_rptr(p), rdlength, &r->data.txt.string_list) < 0)
575 if (avahi_dns_packet_skip(p, rdlength) < 0)
578 r->data.txt.string_list = NULL;
582 case AVAHI_DNS_TYPE_A:
584 /* avahi_log_debug("A"); */
586 if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0)
591 case AVAHI_DNS_TYPE_AAAA:
593 /* avahi_log_debug("aaaa"); */
595 if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0)
602 /* avahi_log_debug("generic"); */
606 r->data.generic.data = avahi_memdup(avahi_dns_packet_get_rptr(p), rdlength);
607 r->data.generic.size = rdlength;
609 if (avahi_dns_packet_skip(p, rdlength) < 0)
616 /* Check if we read enough data */
617 if ((const uint8_t*) avahi_dns_packet_get_rptr(p) - (const uint8_t*) start != rdlength)
623 AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, int *ret_cache_flush) {
624 char name[AVAHI_DOMAIN_NAME_MAX];
625 uint16_t type, class;
628 AvahiRecord *r = NULL;
632 if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
633 avahi_dns_packet_consume_uint16(p, &type) < 0 ||
634 avahi_dns_packet_consume_uint16(p, &class) < 0 ||
635 avahi_dns_packet_consume_uint32(p, &ttl) < 0 ||
636 avahi_dns_packet_consume_uint16(p, &rdlength) < 0 ||
637 p->rindex + rdlength > p->size)
641 *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH);
642 class &= ~AVAHI_DNS_CACHE_FLUSH;
644 if (!(r = avahi_record_new_full(name, class, type, ttl)))
647 if (parse_rdata(p, r, rdlength) < 0)
650 if (!avahi_record_is_valid(r))
657 avahi_record_unref(r);
662 AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, int *ret_unicast_response) {
664 uint16_t type, class;
669 if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
670 avahi_dns_packet_consume_uint16(p, &type) < 0 ||
671 avahi_dns_packet_consume_uint16(p, &class) < 0)
674 if (ret_unicast_response)
675 *ret_unicast_response = !!(class & AVAHI_DNS_UNICAST_RESPONSE);
677 class &= ~AVAHI_DNS_UNICAST_RESPONSE;
679 if (!(k = avahi_key_new(name, class, type)))
682 if (!avahi_key_is_valid(k)) {
690 uint8_t* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, int unicast_response) {
699 if (!(t = avahi_dns_packet_append_name(p, k->name)) ||
700 !avahi_dns_packet_append_uint16(p, k->type) ||
701 !avahi_dns_packet_append_uint16(p, k->clazz | (unicast_response ? AVAHI_DNS_UNICAST_RESPONSE : 0))) {
703 avahi_dns_packet_cleanup_name_table(p);
711 static int append_rdata(AvahiDnsPacket *p, AvahiRecord *r) {
715 switch (r->key->type) {
717 case AVAHI_DNS_TYPE_PTR:
718 case AVAHI_DNS_TYPE_CNAME:
719 case AVAHI_DNS_TYPE_NS:
721 if (!(avahi_dns_packet_append_name(p, r->data.ptr.name)))
726 case AVAHI_DNS_TYPE_SRV:
728 if (!avahi_dns_packet_append_uint16(p, r->data.srv.priority) ||
729 !avahi_dns_packet_append_uint16(p, r->data.srv.weight) ||
730 !avahi_dns_packet_append_uint16(p, r->data.srv.port) ||
731 !avahi_dns_packet_append_name(p, r->data.srv.name))
736 case AVAHI_DNS_TYPE_HINFO:
737 if (!avahi_dns_packet_append_string(p, r->data.hinfo.cpu) ||
738 !avahi_dns_packet_append_string(p, r->data.hinfo.os))
743 case AVAHI_DNS_TYPE_TXT: {
748 n = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
750 if (!(data = avahi_dns_packet_extend(p, n)))
753 avahi_string_list_serialize(r->data.txt.string_list, data, n);
758 case AVAHI_DNS_TYPE_A:
760 if (!avahi_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address)))
765 case AVAHI_DNS_TYPE_AAAA:
767 if (!avahi_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address)))
774 if (r->data.generic.size)
775 if (!avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
785 uint8_t* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, int cache_flush, unsigned max_ttl) {
786 uint8_t *t, *l, *start;
794 if (!(t = avahi_dns_packet_append_name(p, r->key->name)) ||
795 !avahi_dns_packet_append_uint16(p, r->key->type) ||
796 !avahi_dns_packet_append_uint16(p, cache_flush ? (r->key->clazz | AVAHI_DNS_CACHE_FLUSH) : (r->key->clazz &~ AVAHI_DNS_CACHE_FLUSH)) ||
797 !avahi_dns_packet_append_uint32(p, (max_ttl && r->ttl > max_ttl) ? max_ttl : r->ttl) ||
798 !(l = avahi_dns_packet_append_uint16(p, 0)))
801 start = avahi_dns_packet_extend(p, 0);
803 if (append_rdata(p, r) < 0)
806 size = avahi_dns_packet_extend(p, 0) - start;
807 assert(size <= AVAHI_DNS_RDATA_MAX);
809 /* avahi_log_debug("appended %u", size); */
811 l[0] = (uint8_t) ((uint16_t) size >> 8);
812 l[1] = (uint8_t) ((uint16_t) size);
819 avahi_dns_packet_cleanup_name_table(p);
824 int avahi_dns_packet_is_empty(AvahiDnsPacket *p) {
827 return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE;
830 size_t avahi_dns_packet_space(AvahiDnsPacket *p) {
833 assert(p->size <= p->max_size);
835 return p->max_size - p->size;
838 int avahi_rdata_parse(AvahiRecord *record, const void* rdata, size_t size) {
845 p.data = (void*) rdata;
846 p.max_size = p.size = size;
850 ret = parse_rdata(&p, record, size);
852 assert(!p.name_table);
857 size_t avahi_rdata_serialize(AvahiRecord *record, void *rdata, size_t max_size) {
863 assert(max_size > 0);
865 p.data = (void*) rdata;
866 p.max_size = max_size;
867 p.size = p.rindex = 0;
870 ret = append_rdata(&p, record);
873 avahi_hashmap_free(p.name_table);