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>
35 AvahiDnsPacket* avahi_dns_packet_new(guint mtu) {
40 max_size = AVAHI_DNS_PACKET_MAX_SIZE;
41 else if (mtu >= AVAHI_DNS_PACKET_EXTRA_SIZE)
42 max_size = mtu - AVAHI_DNS_PACKET_EXTRA_SIZE;
46 if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE)
47 max_size = AVAHI_DNS_PACKET_HEADER_SIZE;
49 p = g_malloc(sizeof(AvahiDnsPacket) + max_size);
50 p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
51 p->max_size = max_size;
54 memset(AVAHI_DNS_PACKET_DATA(p), 0, p->size);
58 AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu) {
61 p = avahi_dns_packet_new(mtu);
62 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
66 AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa) {
69 p = avahi_dns_packet_new(mtu);
70 avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_FLAGS, AVAHI_DNS_FLAGS(1, 0, aa, 0, 0, 0, 0, 0, 0, 0));
74 AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa) {
78 r = avahi_dns_packet_new_response(mtu, aa);
81 guint n, saved_rindex;
83 saved_rindex = p->rindex;
84 p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
86 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) {
88 gboolean unicast_response;
90 if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) {
91 avahi_dns_packet_append_key(r, k, unicast_response);
96 p->rindex = saved_rindex;
98 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_QDCOUNT, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT));
101 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_ID, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID));
103 avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_FLAGS,
104 (avahi_dns_packet_get_field(r, AVAHI_DNS_FIELD_FLAGS) & ~AVAHI_DNS_FLAG_OPCODE) |
105 (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_OPCODE));
111 void avahi_dns_packet_free(AvahiDnsPacket *p) {
115 g_hash_table_destroy(p->name_table);
120 void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint index, guint16 v) {
122 g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE);
124 ((guint16*) AVAHI_DNS_PACKET_DATA(p))[index] = g_htons(v);
127 guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint index) {
129 g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE);
131 return g_ntohs(((guint16*) AVAHI_DNS_PACKET_DATA(p))[index]);
134 void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint index) {
137 avahi_dns_packet_set_field(p, index, avahi_dns_packet_get_field(p, index) + 1);
140 guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) {
141 guint8 *d, *saved_ptr = NULL;
147 saved_size = p->size;
148 saved_ptr = avahi_dns_packet_extend(p, 0);
155 /* Check whether we can compress this name. */
157 if (p->name_table && (prev = g_hash_table_lookup(p->name_table, name))) {
160 g_assert(prev >= AVAHI_DNS_PACKET_DATA(p));
161 index = (guint) (prev - AVAHI_DNS_PACKET_DATA(p));
163 g_assert(index < p->size);
165 if (index < 0x4000) {
167 if (!(t = (guint16*) avahi_dns_packet_extend(p, sizeof(guint16))))
170 *t = g_htons((0xC000 | index));
177 if (!(avahi_unescape_label(&name, label, sizeof(label))))
180 if (!(d = avahi_dns_packet_append_string(p, label)))
184 /* This works only for normalized domain names */
185 p->name_table = g_hash_table_new_full((GHashFunc) g_str_hash, (GEqualFunc) g_str_equal, g_free, NULL);
187 g_hash_table_insert(p->name_table, g_strdup(pname), d);
190 if (!(d = avahi_dns_packet_extend(p, 1)))
198 p->size = saved_size;
202 guint8* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v) {
206 if (!(d = avahi_dns_packet_extend(p, sizeof(guint16))))
209 *((guint16*) d) = g_htons(v);
213 guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v) {
217 if (!(d = avahi_dns_packet_extend(p, sizeof(guint32))))
220 *((guint32*) d) = g_htonl(v);
225 guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket *p, gconstpointer b, guint l) {
232 if (!(d = avahi_dns_packet_extend(p, l)))
239 guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) {
246 if ((k = strlen(s)) >= 255)
249 if (!(d = avahi_dns_packet_extend(p, k+1)))
258 guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) {
263 if (p->size+l > p->max_size)
266 d = AVAHI_DNS_PACKET_DATA(p) + p->size;
272 gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) {
279 flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS);
281 if (flags & AVAHI_DNS_FLAG_OPCODE || flags & AVAHI_DNS_FLAG_RCODE)
287 gint avahi_dns_packet_is_query(AvahiDnsPacket *p) {
290 return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR);
293 static gint consume_labels(AvahiDnsPacket *p, guint index, gchar *ret_name, guint l) {
297 g_assert(p && ret_name && l);
302 if (index+1 > p->size)
305 n = AVAHI_DNS_PACKET_DATA(p)[index];
318 } else if (n <= 63) {
319 /* Uncompressed label */
324 if (index + n > p->size)
327 if ((guint) n + 1 > l)
336 if (!(avahi_escape_label(AVAHI_DNS_PACKET_DATA(p) + index, n, &ret_name, &l)))
343 } else if ((n & 0xC0) == 0xC0) {
344 /* Compressed label */
346 if (index+2 > p->size)
349 index = ((guint) (AVAHI_DNS_PACKET_DATA(p)[index] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[index+1];
360 gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) {
363 if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0)
370 gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v) {
374 if (p->rindex + sizeof(guint16) > p->size)
377 *ret_v = g_ntohs(*((guint16*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex)));
378 p->rindex += sizeof(guint16);
383 gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v) {
387 if (p->rindex + sizeof(guint32) > p->size)
390 *ret_v = g_ntohl(*((guint32*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex)));
391 p->rindex += sizeof(guint32);
396 gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l) {
401 if (p->rindex + l > p->size)
404 memcpy(ret_data, AVAHI_DNS_PACKET_DATA(p) + p->rindex, l);
410 gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l) {
414 g_assert(ret_string);
417 if (p->rindex >= p->size)
420 k = AVAHI_DNS_PACKET_DATA(p)[p->rindex];
422 if (p->rindex+1+k > p->size)
428 memcpy(ret_string, AVAHI_DNS_PACKET_DATA(p)+p->rindex+1, l-1);
438 gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p) {
441 if (p->rindex > p->size)
444 return AVAHI_DNS_PACKET_DATA(p) + p->rindex;
447 gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length) {
450 if (p->rindex + length > p->size)
457 AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush) {
458 gchar name[257], buf[257];
462 AvahiRecord *r = NULL;
466 g_assert(ret_cache_flush);
468 /* g_message("consume_record()"); */
470 if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
471 avahi_dns_packet_consume_uint16(p, &type) < 0 ||
472 avahi_dns_packet_consume_uint16(p, &class) < 0 ||
473 avahi_dns_packet_consume_uint32(p, &ttl) < 0 ||
474 avahi_dns_packet_consume_uint16(p, &rdlength) < 0 ||
475 p->rindex + rdlength > p->size)
478 /* g_message("name = %s, rdlength = %u", name, rdlength); */
480 *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH);
481 class &= ~AVAHI_DNS_CACHE_FLUSH;
483 start = avahi_dns_packet_get_rptr(p);
485 r = avahi_record_new_full(name, class, type);
488 case AVAHI_DNS_TYPE_PTR:
489 case AVAHI_DNS_TYPE_CNAME:
491 /* g_message("ptr"); */
493 if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
496 r->data.ptr.name = g_strdup(buf);
500 case AVAHI_DNS_TYPE_SRV:
502 /* g_message("srv"); */
504 if (avahi_dns_packet_consume_uint16(p, &r->data.srv.priority) < 0 ||
505 avahi_dns_packet_consume_uint16(p, &r->data.srv.weight) < 0 ||
506 avahi_dns_packet_consume_uint16(p, &r->data.srv.port) < 0 ||
507 avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
510 r->data.srv.name = g_strdup(buf);
513 case AVAHI_DNS_TYPE_HINFO:
515 /* g_message("hinfo"); */
517 if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
520 r->data.hinfo.cpu = g_strdup(buf);
522 if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
525 r->data.hinfo.os = g_strdup(buf);
528 case AVAHI_DNS_TYPE_TXT:
530 /* g_message("txt"); */
533 r->data.txt.string_list = avahi_string_list_parse(avahi_dns_packet_get_rptr(p), rdlength);
535 if (avahi_dns_packet_skip(p, rdlength) < 0)
538 r->data.txt.string_list = NULL;
542 case AVAHI_DNS_TYPE_A:
544 /* g_message("A"); */
546 if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0)
551 case AVAHI_DNS_TYPE_AAAA:
553 /* g_message("aaaa"); */
555 if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0)
562 /* g_message("generic"); */
566 r->data.generic.data = g_memdup(avahi_dns_packet_get_rptr(p), rdlength);
568 if (avahi_dns_packet_skip(p, rdlength) < 0)
575 /* g_message("%i == %u ?", (guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start, rdlength); */
577 /* Check if we read enough data */
578 if ((guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start != rdlength)
587 avahi_record_unref(r);
592 AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response) {
597 g_assert(ret_unicast_response);
599 if (avahi_dns_packet_consume_name(p, name, sizeof(name)) < 0 ||
600 avahi_dns_packet_consume_uint16(p, &type) < 0 ||
601 avahi_dns_packet_consume_uint16(p, &class) < 0)
604 *ret_unicast_response = !!(class & AVAHI_DNS_UNICAST_RESPONSE);
605 class &= ~AVAHI_DNS_UNICAST_RESPONSE;
607 return avahi_key_new(name, class, type);
610 guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response) {
619 if (!(t = avahi_dns_packet_append_name(p, k->name)) ||
620 !avahi_dns_packet_append_uint16(p, k->type) ||
621 !avahi_dns_packet_append_uint16(p, k->class | (unicast_response ? AVAHI_DNS_UNICAST_RESPONSE : 0))) {
629 guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl) {
630 guint8 *t, *l, *start;
638 if (!(t = avahi_dns_packet_append_name(p, r->key->name)) ||
639 !avahi_dns_packet_append_uint16(p, r->key->type) ||
640 !avahi_dns_packet_append_uint16(p, cache_flush ? (r->key->class | AVAHI_DNS_CACHE_FLUSH) : (r->key->class &~ AVAHI_DNS_CACHE_FLUSH)) ||
641 !avahi_dns_packet_append_uint32(p, (max_ttl && r->ttl > max_ttl) ? max_ttl : r->ttl) ||
642 !(l = avahi_dns_packet_append_uint16(p, 0)))
645 start = avahi_dns_packet_extend(p, 0);
647 switch (r->key->type) {
649 case AVAHI_DNS_TYPE_PTR:
650 case AVAHI_DNS_TYPE_CNAME :
652 if (!(avahi_dns_packet_append_name(p, r->data.ptr.name)))
657 case AVAHI_DNS_TYPE_SRV:
659 if (!avahi_dns_packet_append_uint16(p, r->data.srv.priority) ||
660 !avahi_dns_packet_append_uint16(p, r->data.srv.weight) ||
661 !avahi_dns_packet_append_uint16(p, r->data.srv.port) ||
662 !avahi_dns_packet_append_name(p, r->data.srv.name))
667 case AVAHI_DNS_TYPE_HINFO:
668 if (!avahi_dns_packet_append_string(p, r->data.hinfo.cpu) ||
669 !avahi_dns_packet_append_string(p, r->data.hinfo.os))
674 case AVAHI_DNS_TYPE_TXT: {
679 size = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
681 /* g_message("appending string: %u %p", size, r->data.txt.string_list); */
683 if (!(data = avahi_dns_packet_extend(p, size)))
686 avahi_string_list_serialize(r->data.txt.string_list, data, size);
691 case AVAHI_DNS_TYPE_A:
693 if (!avahi_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address)))
698 case AVAHI_DNS_TYPE_AAAA:
700 if (!avahi_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address)))
707 if (r->data.generic.size &&
708 avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
717 size = avahi_dns_packet_extend(p, 0) - start;
718 g_assert(size <= 0xFFFF);
720 /* g_message("appended %u", size); */
722 * (guint16*) l = g_htons((guint16) size);
732 gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p) {
735 return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE;
738 guint avahi_dns_packet_space(AvahiDnsPacket *p) {
741 g_assert(p->size <= p->max_size);
743 return p->max_size - p->size;