1 #include <netinet/in.h>
10 flxDnsPacket* flx_dns_packet_new(guint max_size) {
14 max_size = FLX_DNS_PACKET_MAX_SIZE;
15 else if (max_size < FLX_DNS_PACKET_HEADER_SIZE)
16 max_size = FLX_DNS_PACKET_HEADER_SIZE;
18 p = g_malloc(sizeof(flxDnsPacket) + max_size);
19 p->size = p->rindex = FLX_DNS_PACKET_HEADER_SIZE;
20 p->max_size = max_size;
23 memset(FLX_DNS_PACKET_DATA(p), 0, p->size);
27 flxDnsPacket* flx_dns_packet_new_query(guint max_size) {
30 p = flx_dns_packet_new(max_size);
31 flx_dns_packet_set_field(p, FLX_DNS_FIELD_FLAGS, FLX_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
35 flxDnsPacket* flx_dns_packet_new_response(guint max_size) {
38 p = flx_dns_packet_new(max_size);
39 flx_dns_packet_set_field(p, FLX_DNS_FIELD_FLAGS, FLX_DNS_FLAGS(1, 0, 0, 0, 0, 0, 0, 0, 0, 0));
43 void flx_dns_packet_free(flxDnsPacket *p) {
47 g_hash_table_destroy(p->name_table);
52 void flx_dns_packet_set_field(flxDnsPacket *p, guint index, guint16 v) {
54 g_assert(index < FLX_DNS_PACKET_HEADER_SIZE);
56 ((guint16*) FLX_DNS_PACKET_DATA(p))[index] = g_htons(v);
59 guint16 flx_dns_packet_get_field(flxDnsPacket *p, guint index) {
61 g_assert(index < FLX_DNS_PACKET_HEADER_SIZE);
63 return g_ntohs(((guint16*) FLX_DNS_PACKET_DATA(p))[index]);
66 guint8* flx_dns_packet_append_name(flxDnsPacket *p, const gchar *name) {
79 /* Check whether we can compress this name. */
81 if (p->name_table && (prev = g_hash_table_lookup(p->name_table, name))) {
84 g_assert(prev >= FLX_DNS_PACKET_DATA(p));
85 index = (guint) (prev - FLX_DNS_PACKET_DATA(p));
87 g_assert(index < p->size);
91 if (!(t = (guint16*) flx_dns_packet_extend(p, sizeof(guint16))))
97 *t = g_htons((0xC000 | index));
102 n = strcspn(name, ".");
106 if (!(d = flx_dns_packet_extend(p, n+1)))
112 memcpy(d+1, name, n);
115 p->name_table = g_hash_table_new_full((GHashFunc) flx_domain_hash, (GEqualFunc) flx_domain_equal, g_free, NULL);
117 g_hash_table_insert(p->name_table, g_strdup(name), d);
121 /* no trailing dot */
132 if (!(d = flx_dns_packet_extend(p, 1)))
140 p->size = saved_size;
144 guint8 *flx_dns_packet_append_name_compressed(flxDnsPacket *p, const gchar *name, guint8 *prev) {
150 return flx_dns_packet_append_name(p, name);
152 k = prev - FLX_DNS_PACKET_DATA(p);
153 if (k < 0 || k >= 0x4000 || (guint) k >= p->size)
154 return flx_dns_packet_append_name(p, name);
156 if (!(d = (guint16*) flx_dns_packet_extend(p, sizeof(guint16))))
159 *d = g_htons((0xC000 | k));
163 guint8* flx_dns_packet_append_uint16(flxDnsPacket *p, guint16 v) {
167 if (!(d = flx_dns_packet_extend(p, sizeof(guint16))))
170 *((guint16*) d) = g_htons(v);
174 guint8 *flx_dns_packet_append_uint32(flxDnsPacket *p, guint32 v) {
178 if (!(d = flx_dns_packet_extend(p, sizeof(guint32))))
181 *((guint32*) d) = g_htonl(v);
186 guint8 *flx_dns_packet_append_bytes(flxDnsPacket *p, gconstpointer b, guint l) {
193 if (!(d = flx_dns_packet_extend(p, l)))
200 guint8* flx_dns_packet_append_string(flxDnsPacket *p, const gchar *s) {
207 if ((k = strlen(s)) >= 255)
210 if (!(d = flx_dns_packet_extend(p, k+1)))
219 guint8 *flx_dns_packet_extend(flxDnsPacket *p, guint l) {
224 if (p->size+l > p->max_size)
227 d = FLX_DNS_PACKET_DATA(p) + p->size;
233 gint flx_dns_packet_check_valid(flxDnsPacket *p) {
240 flags = flx_dns_packet_get_field(p, FLX_DNS_FIELD_FLAGS);
242 if (flags & FLX_DNS_FLAG_OPCODE || flags & FLX_DNS_FLAG_RCODE)
248 gint flx_dns_packet_is_query(flxDnsPacket *p) {
251 return !(flx_dns_packet_get_field(p, FLX_DNS_FIELD_FLAGS) & FLX_DNS_FLAG_QR);
254 static gint consume_labels(flxDnsPacket *p, guint index, gchar *ret_name, guint l) {
258 g_assert(p && ret_name && l);
263 if (index+1 > p->size)
266 n = FLX_DNS_PACKET_DATA(p)[index];
279 } else if (n <= 63) {
280 /* Uncompressed label */
285 if (index + n > p->size)
288 if ((guint) n + 1 > l)
297 memcpy(ret_name, FLX_DNS_PACKET_DATA(p) + index, n);
304 } else if ((n & 0xC0) == 0xC0) {
305 /* Compressed label */
307 if (index+2 > p->size)
310 index = ((guint) (FLX_DNS_PACKET_DATA(p)[index] & ~0xC0)) << 8 | FLX_DNS_PACKET_DATA(p)[index+1];
321 gint flx_dns_packet_consume_name(flxDnsPacket *p, gchar *ret_name, guint l) {
324 if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0)
331 gint flx_dns_packet_consume_uint16(flxDnsPacket *p, guint16 *ret_v) {
335 if (p->rindex + sizeof(guint16) > p->size)
338 *ret_v = g_ntohs(*((guint16*) (FLX_DNS_PACKET_DATA(p) + p->rindex)));
339 p->rindex += sizeof(guint16);
344 gint flx_dns_packet_consume_uint32(flxDnsPacket *p, guint32 *ret_v) {
348 if (p->rindex + sizeof(guint32) > p->size)
351 *ret_v = g_ntohl(*((guint32*) (FLX_DNS_PACKET_DATA(p) + p->rindex)));
352 p->rindex += sizeof(guint32);
357 gint flx_dns_packet_consume_bytes(flxDnsPacket *p, gpointer ret_data, guint l) {
362 if (p->rindex + l > p->size)
365 memcpy(ret_data, FLX_DNS_PACKET_DATA(p) + p->rindex, l);
371 gint flx_dns_packet_consume_string(flxDnsPacket *p, gchar *ret_string, guint l) {
375 g_assert(ret_string);
378 if (p->rindex >= p->size)
381 k = FLX_DNS_PACKET_DATA(p)[p->rindex];
383 if (p->rindex+1+k > p->size)
389 memcpy(ret_string, FLX_DNS_PACKET_DATA(p)+p->rindex+1, l-1);
399 gconstpointer flx_dns_packet_get_rptr(flxDnsPacket *p) {
402 if (p->rindex > p->size)
405 return FLX_DNS_PACKET_DATA(p) + p->rindex;
408 gint flx_dns_packet_skip(flxDnsPacket *p, guint length) {
411 if (p->rindex + length > p->size)
418 flxRecord* flx_dns_packet_consume_record(flxDnsPacket *p, gboolean *ret_cache_flush) {
419 gchar name[257], buf[257];
428 g_assert(ret_cache_flush);
430 /* g_message("consume_record()"); */
432 if (flx_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
433 flx_dns_packet_consume_uint16(p, &type) < 0 ||
434 flx_dns_packet_consume_uint16(p, &class) < 0 ||
435 flx_dns_packet_consume_uint32(p, &ttl) < 0 ||
436 flx_dns_packet_consume_uint16(p, &rdlength) < 0 ||
437 p->rindex + rdlength > p->size)
441 /* g_message("name = %s, rdlength = %u", name, rdlength); */
443 start = flx_dns_packet_get_rptr(p);
445 r = flx_record_new_full(name, class, type);
448 case FLX_DNS_TYPE_PTR:
449 case FLX_DNS_TYPE_CNAME:
451 /* g_message("ptr"); */
453 if (flx_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
456 r->data.ptr.name = g_strdup(buf);
460 case FLX_DNS_TYPE_SRV:
462 /* g_message("srv"); */
464 if (flx_dns_packet_consume_uint16(p, &r->data.srv.priority) < 0 ||
465 flx_dns_packet_consume_uint16(p, &r->data.srv.weight) < 0 ||
466 flx_dns_packet_consume_uint16(p, &r->data.srv.port) < 0 ||
467 flx_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
470 r->data.srv.name = g_strdup(buf);
473 case FLX_DNS_TYPE_HINFO:
475 /* g_message("hinfo"); */
477 if (flx_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
480 r->data.hinfo.cpu = g_strdup(buf);
482 if (flx_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
485 r->data.hinfo.os = g_strdup(buf);
488 case FLX_DNS_TYPE_TXT:
490 /* g_message("txt"); */
493 r->data.txt.string_list = flx_string_list_parse(flx_dns_packet_get_rptr(p), rdlength);
495 if (flx_dns_packet_skip(p, rdlength) < 0)
498 r->data.txt.string_list = NULL;
504 /* g_message("A"); */
506 if (flx_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(flxIPv4Address)) < 0)
511 case FLX_DNS_TYPE_AAAA:
513 /* g_message("aaaa"); */
515 if (flx_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(flxIPv6Address)) < 0)
522 /* g_message("generic"); */
526 r->data.generic.data = g_memdup(flx_dns_packet_get_rptr(p), rdlength);
528 if (flx_dns_packet_skip(p, rdlength) < 0)
535 /* g_message("%i == %u ?", (guint8*) flx_dns_packet_get_rptr(p) - (guint8*) start, rdlength); */
537 /* Check if we read enough data */
538 if ((guint8*) flx_dns_packet_get_rptr(p) - (guint8*) start != rdlength)
541 *ret_cache_flush = !!(class & FLX_DNS_CACHE_FLUSH);
542 class &= ~ FLX_DNS_CACHE_FLUSH;
555 flxKey* flx_dns_packet_consume_key(flxDnsPacket *p) {
561 if (flx_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
562 flx_dns_packet_consume_uint16(p, &type) < 0 ||
563 flx_dns_packet_consume_uint16(p, &class) < 0)
566 class &= ~ FLX_DNS_CACHE_FLUSH;
568 return flx_key_new(name, class, type);
571 guint8* flx_dns_packet_append_key(flxDnsPacket *p, flxKey *k) {
580 if (!(t = flx_dns_packet_append_name(p, k->name)) ||
581 !flx_dns_packet_append_uint16(p, k->type) ||
582 !flx_dns_packet_append_uint16(p, k->class)) {
590 guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cache_flush) {
591 guint8 *t, *l, *start;
599 if (!(t = flx_dns_packet_append_name(p, r->key->name)) ||
600 !flx_dns_packet_append_uint16(p, r->key->type) ||
601 !flx_dns_packet_append_uint16(p, cache_flush ? (r->key->class | FLX_DNS_CACHE_FLUSH) : (r->key->class &~ FLX_DNS_CACHE_FLUSH)) ||
602 !flx_dns_packet_append_uint32(p, r->ttl) ||
603 !(l = flx_dns_packet_append_uint16(p, 0)))
606 start = flx_dns_packet_extend(p, 0);
608 switch (r->key->type) {
610 case FLX_DNS_TYPE_PTR:
611 case FLX_DNS_TYPE_CNAME :
613 if (!(flx_dns_packet_append_name(p, r->data.ptr.name)))
618 case FLX_DNS_TYPE_SRV:
620 if (!flx_dns_packet_append_uint16(p, r->data.srv.priority) ||
621 !flx_dns_packet_append_uint16(p, r->data.srv.weight) ||
622 !flx_dns_packet_append_uint16(p, r->data.srv.port) ||
623 !flx_dns_packet_append_name(p, r->data.srv.name))
628 case FLX_DNS_TYPE_HINFO:
629 if (!flx_dns_packet_append_string(p, r->data.hinfo.cpu) ||
630 !flx_dns_packet_append_string(p, r->data.hinfo.os))
635 case FLX_DNS_TYPE_TXT: {
640 size = flx_string_list_serialize(r->data.txt.string_list, NULL, 0);
642 /* g_message("appending string: %u %p", size, r->data.txt.string_list); */
644 if (!(data = flx_dns_packet_extend(p, size)))
647 flx_string_list_serialize(r->data.txt.string_list, data, size);
654 if (!flx_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address)))
659 case FLX_DNS_TYPE_AAAA:
661 if (!flx_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address)))
668 if (r->data.generic.size &&
669 flx_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
678 size = flx_dns_packet_extend(p, 0) - start;
679 g_assert(size <= 0xFFFF);
681 /* g_message("appended %u", size); */
683 * (guint16*) l = g_htons((guint16) size);
693 gboolean flx_dns_packet_is_empty(flxDnsPacket *p) {
696 return p->size <= FLX_DNS_PACKET_HEADER_SIZE;
699 guint flx_dns_packet_space(flxDnsPacket *p) {
702 g_assert(p->size <= p->max_size);
704 return p->max_size - p->size;