2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <sys/types.h>
30 #include <netinet/in.h>
32 #include <catta/defs.h>
33 #include <catta/domain.h>
34 #include <catta/malloc.h>
37 #include <catta/log.h>
39 CattaDnsPacket* catta_dns_packet_new(unsigned mtu) {
44 max_size = CATTA_DNS_PACKET_SIZE_MAX;
45 else if (mtu >= CATTA_DNS_PACKET_EXTRA_SIZE)
46 max_size = mtu - CATTA_DNS_PACKET_EXTRA_SIZE;
50 if (max_size < CATTA_DNS_PACKET_HEADER_SIZE)
51 max_size = CATTA_DNS_PACKET_HEADER_SIZE;
53 if (!(p = catta_malloc(sizeof(CattaDnsPacket) + max_size)))
56 p->size = p->rindex = CATTA_DNS_PACKET_HEADER_SIZE;
57 p->max_size = max_size;
61 memset(CATTA_DNS_PACKET_DATA(p), 0, p->size);
65 CattaDnsPacket* catta_dns_packet_new_query(unsigned mtu) {
68 if (!(p = catta_dns_packet_new(mtu)))
71 catta_dns_packet_set_field(p, CATTA_DNS_FIELD_FLAGS, CATTA_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
75 CattaDnsPacket* catta_dns_packet_new_response(unsigned mtu, int aa) {
78 if (!(p = catta_dns_packet_new(mtu)))
81 catta_dns_packet_set_field(p, CATTA_DNS_FIELD_FLAGS, CATTA_DNS_FLAGS(1, 0, aa, 0, 0, 0, 0, 0, 0, 0));
85 CattaDnsPacket* catta_dns_packet_new_reply(CattaDnsPacket* p, unsigned mtu, int copy_queries, int aa) {
89 if (!(r = catta_dns_packet_new_response(mtu, aa)))
93 unsigned saved_rindex;
96 saved_rindex = p->rindex;
97 p->rindex = CATTA_DNS_PACKET_HEADER_SIZE;
99 for (n = catta_dns_packet_get_field(p, CATTA_DNS_FIELD_QDCOUNT); n > 0; n--) {
101 int unicast_response;
103 if ((k = catta_dns_packet_consume_key(p, &unicast_response))) {
104 catta_dns_packet_append_key(r, k, unicast_response);
109 p->rindex = saved_rindex;
111 catta_dns_packet_set_field(r, CATTA_DNS_FIELD_QDCOUNT, catta_dns_packet_get_field(p, CATTA_DNS_FIELD_QDCOUNT));
114 catta_dns_packet_set_field(r, CATTA_DNS_FIELD_ID, catta_dns_packet_get_field(p, CATTA_DNS_FIELD_ID));
116 catta_dns_packet_set_field(r, CATTA_DNS_FIELD_FLAGS,
117 (catta_dns_packet_get_field(r, CATTA_DNS_FIELD_FLAGS) & ~CATTA_DNS_FLAG_OPCODE) |
118 (catta_dns_packet_get_field(p, CATTA_DNS_FIELD_FLAGS) & CATTA_DNS_FLAG_OPCODE));
124 void catta_dns_packet_free(CattaDnsPacket *p) {
128 catta_hashmap_free(p->name_table);
133 void catta_dns_packet_set_field(CattaDnsPacket *p, unsigned idx, uint16_t v) {
135 assert(idx < CATTA_DNS_PACKET_HEADER_SIZE);
137 ((uint16_t*) CATTA_DNS_PACKET_DATA(p))[idx] = htons(v);
140 uint16_t catta_dns_packet_get_field(CattaDnsPacket *p, unsigned idx) {
142 assert(idx < CATTA_DNS_PACKET_HEADER_SIZE);
144 return ntohs(((uint16_t*) CATTA_DNS_PACKET_DATA(p))[idx]);
147 void catta_dns_packet_inc_field(CattaDnsPacket *p, unsigned idx) {
149 assert(idx < CATTA_DNS_PACKET_HEADER_SIZE);
151 catta_dns_packet_set_field(p, idx, catta_dns_packet_get_field(p, idx) + 1);
155 static void name_table_cleanup(void *key, void *value, void *user_data) {
156 CattaDnsPacket *p = user_data;
158 if ((uint8_t*) value >= CATTA_DNS_PACKET_DATA(p) + p->size)
159 catta_hashmap_remove(p->name_table, key);
162 void catta_dns_packet_cleanup_name_table(CattaDnsPacket *p) {
164 catta_hashmap_foreach(p->name_table, name_table_cleanup, p);
167 uint8_t* catta_dns_packet_append_name(CattaDnsPacket *p, const char *name) {
168 uint8_t *d, *saved_ptr = NULL;
174 saved_size = p->size;
175 saved_ptr = catta_dns_packet_extend(p, 0);
182 /* Check whether we can compress this name. */
184 if (p->name_table && (prev = catta_hashmap_lookup(p->name_table, name))) {
187 assert(prev >= CATTA_DNS_PACKET_DATA(p));
188 idx = (unsigned) (prev - CATTA_DNS_PACKET_DATA(p));
190 assert(idx < p->size);
194 if (!(t = (uint8_t*) catta_dns_packet_extend(p, sizeof(uint16_t))))
197 t[0] = (uint8_t) ((0xC000 | idx) >> 8);
198 t[1] = (uint8_t) idx;
205 if (!(catta_unescape_label(&name, label, sizeof(label))))
208 if (!(d = catta_dns_packet_append_string(p, label)))
212 /* This works only for normalized domain names */
213 p->name_table = catta_hashmap_new(catta_string_hash, catta_string_equal, catta_free, NULL);
215 if (!(u = catta_strdup(pname)))
216 catta_log_error("catta_strdup() failed.");
218 catta_hashmap_insert(p->name_table, u, d);
221 if (!(d = catta_dns_packet_extend(p, 1)))
229 p->size = saved_size;
230 catta_dns_packet_cleanup_name_table(p);
235 uint8_t* catta_dns_packet_append_uint16(CattaDnsPacket *p, uint16_t v) {
239 if (!(d = catta_dns_packet_extend(p, sizeof(uint16_t))))
242 d[0] = (uint8_t) (v >> 8);
247 uint8_t *catta_dns_packet_append_uint32(CattaDnsPacket *p, uint32_t v) {
251 if (!(d = catta_dns_packet_extend(p, sizeof(uint32_t))))
254 d[0] = (uint8_t) (v >> 24);
255 d[1] = (uint8_t) (v >> 16);
256 d[2] = (uint8_t) (v >> 8);
262 uint8_t *catta_dns_packet_append_bytes(CattaDnsPacket *p, const void *b, size_t l) {
269 if (!(d = catta_dns_packet_extend(p, l)))
276 uint8_t* catta_dns_packet_append_string(CattaDnsPacket *p, const char *s) {
283 if ((k = strlen(s)) >= 255)
286 if (!(d = catta_dns_packet_extend(p, k+1)))
295 uint8_t *catta_dns_packet_extend(CattaDnsPacket *p, size_t l) {
300 if (p->size+l > p->max_size)
303 d = CATTA_DNS_PACKET_DATA(p) + p->size;
309 int catta_dns_packet_check_valid(CattaDnsPacket *p) {
313 if (p->size < CATTA_DNS_PACKET_HEADER_SIZE)
316 flags = catta_dns_packet_get_field(p, CATTA_DNS_FIELD_FLAGS);
318 if (flags & CATTA_DNS_FLAG_OPCODE)
324 int catta_dns_packet_check_valid_multicast(CattaDnsPacket *p) {
328 if (catta_dns_packet_check_valid(p) < 0)
331 flags = catta_dns_packet_get_field(p, CATTA_DNS_FIELD_FLAGS);
333 if (flags & CATTA_DNS_FLAG_RCODE)
339 int catta_dns_packet_is_query(CattaDnsPacket *p) {
342 return !(catta_dns_packet_get_field(p, CATTA_DNS_FIELD_FLAGS) & CATTA_DNS_FLAG_QR);
345 static int consume_labels(CattaDnsPacket *p, unsigned idx, char *ret_name, size_t l) {
351 assert(p && ret_name && l);
353 for (i = 0; i < CATTA_DNS_LABELS_MAX; i++) {
359 n = CATTA_DNS_PACKET_DATA(p)[idx];
372 } else if (n <= 63) {
373 /* Uncompressed label */
378 if (idx + n > p->size)
381 if ((size_t) n + 1 > l)
390 if (!(catta_escape_label((char*) CATTA_DNS_PACKET_DATA(p) + idx, n, &ret_name, &l)))
397 } else if ((n & 0xC0) == 0xC0) {
398 /* Compressed label */
403 label_ptr = ((unsigned) (CATTA_DNS_PACKET_DATA(p)[idx] & ~0xC0)) << 8 | CATTA_DNS_PACKET_DATA(p)[idx+1];
405 if ((label_ptr < CATTA_DNS_PACKET_HEADER_SIZE) || (label_ptr >= idx))
421 int catta_dns_packet_consume_name(CattaDnsPacket *p, char *ret_name, size_t l) {
424 if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0)
431 int catta_dns_packet_consume_uint16(CattaDnsPacket *p, uint16_t *ret_v) {
437 if (p->rindex + sizeof(uint16_t) > p->size)
440 d = (uint8_t*) (CATTA_DNS_PACKET_DATA(p) + p->rindex);
441 *ret_v = (d[0] << 8) | d[1];
442 p->rindex += sizeof(uint16_t);
447 int catta_dns_packet_consume_uint32(CattaDnsPacket *p, uint32_t *ret_v) {
453 if (p->rindex + sizeof(uint32_t) > p->size)
456 d = (uint8_t*) (CATTA_DNS_PACKET_DATA(p) + p->rindex);
457 *ret_v = (d[0] << 24) | (d[1] << 16) | (d[2] << 8) | d[3];
458 p->rindex += sizeof(uint32_t);
463 int catta_dns_packet_consume_bytes(CattaDnsPacket *p, void * ret_data, size_t l) {
468 if (p->rindex + l > p->size)
471 memcpy(ret_data, CATTA_DNS_PACKET_DATA(p) + p->rindex, l);
477 int catta_dns_packet_consume_string(CattaDnsPacket *p, char *ret_string, size_t l) {
484 if (p->rindex >= p->size)
487 k = CATTA_DNS_PACKET_DATA(p)[p->rindex];
489 if (p->rindex+1+k > p->size)
495 memcpy(ret_string, CATTA_DNS_PACKET_DATA(p)+p->rindex+1, l-1);
503 const void* catta_dns_packet_get_rptr(CattaDnsPacket *p) {
506 if (p->rindex > p->size)
509 return CATTA_DNS_PACKET_DATA(p) + p->rindex;
512 int catta_dns_packet_skip(CattaDnsPacket *p, size_t length) {
515 if (p->rindex + length > p->size)
522 static int parse_rdata(CattaDnsPacket *p, CattaRecord *r, uint16_t rdlength) {
523 char buf[CATTA_DOMAIN_NAME_MAX];
529 start = catta_dns_packet_get_rptr(p);
531 switch (r->key->type) {
532 case CATTA_DNS_TYPE_PTR:
533 case CATTA_DNS_TYPE_CNAME:
534 case CATTA_DNS_TYPE_NS:
536 if (catta_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
539 r->data.ptr.name = catta_strdup(buf);
543 case CATTA_DNS_TYPE_SRV:
545 if (catta_dns_packet_consume_uint16(p, &r->data.srv.priority) < 0 ||
546 catta_dns_packet_consume_uint16(p, &r->data.srv.weight) < 0 ||
547 catta_dns_packet_consume_uint16(p, &r->data.srv.port) < 0 ||
548 catta_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
551 r->data.srv.name = catta_strdup(buf);
554 case CATTA_DNS_TYPE_HINFO:
556 if (catta_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
559 r->data.hinfo.cpu = catta_strdup(buf);
561 if (catta_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
564 r->data.hinfo.os = catta_strdup(buf);
567 case CATTA_DNS_TYPE_TXT:
570 if (catta_string_list_parse(catta_dns_packet_get_rptr(p), rdlength, &r->data.txt.string_list) < 0)
573 if (catta_dns_packet_skip(p, rdlength) < 0)
576 r->data.txt.string_list = NULL;
580 case CATTA_DNS_TYPE_A:
582 /* catta_log_debug("A"); */
584 if (catta_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(CattaIPv4Address)) < 0)
589 case CATTA_DNS_TYPE_AAAA:
591 /* catta_log_debug("aaaa"); */
593 if (catta_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(CattaIPv6Address)) < 0)
600 /* catta_log_debug("generic"); */
604 r->data.generic.data = catta_memdup(catta_dns_packet_get_rptr(p), rdlength);
605 r->data.generic.size = rdlength;
607 if (catta_dns_packet_skip(p, rdlength) < 0)
614 /* Check if we read enough data */
615 if ((const uint8_t*) catta_dns_packet_get_rptr(p) - (const uint8_t*) start != rdlength)
621 CattaRecord* catta_dns_packet_consume_record(CattaDnsPacket *p, int *ret_cache_flush) {
622 char name[CATTA_DOMAIN_NAME_MAX];
623 uint16_t type, class;
626 CattaRecord *r = NULL;
630 if (catta_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
631 catta_dns_packet_consume_uint16(p, &type) < 0 ||
632 catta_dns_packet_consume_uint16(p, &class) < 0 ||
633 catta_dns_packet_consume_uint32(p, &ttl) < 0 ||
634 catta_dns_packet_consume_uint16(p, &rdlength) < 0 ||
635 p->rindex + rdlength > p->size)
639 *ret_cache_flush = !!(class & CATTA_DNS_CACHE_FLUSH);
640 class &= ~CATTA_DNS_CACHE_FLUSH;
642 if (!(r = catta_record_new_full(name, class, type, ttl)))
645 if (parse_rdata(p, r, rdlength) < 0)
648 if (!catta_record_is_valid(r))
655 catta_record_unref(r);
660 CattaKey* catta_dns_packet_consume_key(CattaDnsPacket *p, int *ret_unicast_response) {
662 uint16_t type, class;
667 if (catta_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
668 catta_dns_packet_consume_uint16(p, &type) < 0 ||
669 catta_dns_packet_consume_uint16(p, &class) < 0)
672 if (ret_unicast_response)
673 *ret_unicast_response = !!(class & CATTA_DNS_UNICAST_RESPONSE);
675 class &= ~CATTA_DNS_UNICAST_RESPONSE;
677 if (!(k = catta_key_new(name, class, type)))
680 if (!catta_key_is_valid(k)) {
688 uint8_t* catta_dns_packet_append_key(CattaDnsPacket *p, CattaKey *k, int unicast_response) {
697 if (!(t = catta_dns_packet_append_name(p, k->name)) ||
698 !catta_dns_packet_append_uint16(p, k->type) ||
699 !catta_dns_packet_append_uint16(p, k->clazz | (unicast_response ? CATTA_DNS_UNICAST_RESPONSE : 0))) {
701 catta_dns_packet_cleanup_name_table(p);
709 static int append_rdata(CattaDnsPacket *p, CattaRecord *r) {
713 switch (r->key->type) {
715 case CATTA_DNS_TYPE_PTR:
716 case CATTA_DNS_TYPE_CNAME:
717 case CATTA_DNS_TYPE_NS:
719 if (!(catta_dns_packet_append_name(p, r->data.ptr.name)))
724 case CATTA_DNS_TYPE_SRV:
726 if (!catta_dns_packet_append_uint16(p, r->data.srv.priority) ||
727 !catta_dns_packet_append_uint16(p, r->data.srv.weight) ||
728 !catta_dns_packet_append_uint16(p, r->data.srv.port) ||
729 !catta_dns_packet_append_name(p, r->data.srv.name))
734 case CATTA_DNS_TYPE_HINFO:
735 if (!catta_dns_packet_append_string(p, r->data.hinfo.cpu) ||
736 !catta_dns_packet_append_string(p, r->data.hinfo.os))
741 case CATTA_DNS_TYPE_TXT: {
746 n = catta_string_list_serialize(r->data.txt.string_list, NULL, 0);
748 if (!(data = catta_dns_packet_extend(p, n)))
751 catta_string_list_serialize(r->data.txt.string_list, data, n);
756 case CATTA_DNS_TYPE_A:
758 if (!catta_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address)))
763 case CATTA_DNS_TYPE_AAAA:
765 if (!catta_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address)))
772 if (r->data.generic.size)
773 if (!catta_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
783 uint8_t* catta_dns_packet_append_record(CattaDnsPacket *p, CattaRecord *r, int cache_flush, unsigned max_ttl) {
784 uint8_t *t, *l, *start;
792 if (!(t = catta_dns_packet_append_name(p, r->key->name)) ||
793 !catta_dns_packet_append_uint16(p, r->key->type) ||
794 !catta_dns_packet_append_uint16(p, cache_flush ? (r->key->clazz | CATTA_DNS_CACHE_FLUSH) : (r->key->clazz &~ CATTA_DNS_CACHE_FLUSH)) ||
795 !catta_dns_packet_append_uint32(p, (max_ttl && r->ttl > max_ttl) ? max_ttl : r->ttl) ||
796 !(l = catta_dns_packet_append_uint16(p, 0)))
799 start = catta_dns_packet_extend(p, 0);
801 if (append_rdata(p, r) < 0)
804 size = catta_dns_packet_extend(p, 0) - start;
805 assert(size <= CATTA_DNS_RDATA_MAX);
807 /* catta_log_debug("appended %u", size); */
809 l[0] = (uint8_t) ((uint16_t) size >> 8);
810 l[1] = (uint8_t) ((uint16_t) size);
817 catta_dns_packet_cleanup_name_table(p);
822 int catta_dns_packet_is_empty(CattaDnsPacket *p) {
825 return p->size <= CATTA_DNS_PACKET_HEADER_SIZE;
828 size_t catta_dns_packet_space(CattaDnsPacket *p) {
831 assert(p->size <= p->max_size);
833 return p->max_size - p->size;
836 int catta_rdata_parse(CattaRecord *record, const void* rdata, size_t size) {
843 p.data = (void*) rdata;
844 p.max_size = p.size = size;
848 ret = parse_rdata(&p, record, size);
850 assert(!p.name_table);
855 size_t catta_rdata_serialize(CattaRecord *record, void *rdata, size_t max_size) {
861 assert(max_size > 0);
863 p.data = (void*) rdata;
864 p.max_size = max_size;
865 p.size = p.rindex = 0;
868 ret = append_rdata(&p, record);
871 catta_hashmap_free(p.name_table);