]> git.meshlink.io Git - catta/blob - avahi-core/dns.c
* implement reflection (including legacy unicast reflection)
[catta] / avahi-core / dns.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
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.
10  
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.
15  
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
19   USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <netinet/in.h>
27
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdio.h>
31
32 #include "dns.h"
33 #include "util.h"
34
35 AvahiDnsPacket* avahi_dns_packet_new(guint mtu) {
36     AvahiDnsPacket *p;
37     guint max_size;
38
39     if (mtu <= 0)
40         max_size = AVAHI_DNS_PACKET_MAX_SIZE;
41     else if (mtu >= 48)
42         max_size = mtu - 48;
43     else
44         max_size = 0;
45
46     if (max_size < AVAHI_DNS_PACKET_HEADER_SIZE)
47         max_size = AVAHI_DNS_PACKET_HEADER_SIZE;
48     
49     p = g_malloc(sizeof(AvahiDnsPacket) + max_size);
50     p->size = p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
51     p->max_size = max_size;
52     p->name_table = NULL;
53
54     memset(AVAHI_DNS_PACKET_DATA(p), 0, p->size);
55     return p;
56 }
57
58 AvahiDnsPacket* avahi_dns_packet_new_query(guint mtu) {
59     AvahiDnsPacket *p;
60
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));
63     return p;
64 }
65
66 AvahiDnsPacket* avahi_dns_packet_new_response(guint mtu, gboolean aa) {
67     AvahiDnsPacket *p;
68
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));
71     return p;
72 }
73
74 AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, guint mtu, gboolean copy_queries, gboolean aa) {
75     AvahiDnsPacket *r;
76     g_assert(p);
77
78     r = avahi_dns_packet_new_response(mtu, aa);
79
80     if (copy_queries) {
81         guint n, saved_rindex;
82
83         saved_rindex = p->rindex;
84         p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
85         
86         for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) {
87             AvahiKey *k;
88             gboolean unicast_response;
89
90             if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) {
91                 avahi_dns_packet_append_key(r, k, unicast_response);
92                 avahi_key_unref(k);
93             }
94         }
95
96         p->rindex = saved_rindex;
97
98         avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_QDCOUNT, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT));
99     }
100
101     avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_ID, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID));
102
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));
106
107     return r;
108
109
110
111 void avahi_dns_packet_free(AvahiDnsPacket *p) {
112     g_assert(p);
113
114     if (p->name_table)
115         g_hash_table_destroy(p->name_table);
116     
117     g_free(p);
118 }
119
120 void avahi_dns_packet_set_field(AvahiDnsPacket *p, guint index, guint16 v) {
121     g_assert(p);
122     g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE);
123     
124     ((guint16*) AVAHI_DNS_PACKET_DATA(p))[index] = g_htons(v);
125 }
126
127 guint16 avahi_dns_packet_get_field(AvahiDnsPacket *p, guint index) {
128     g_assert(p);
129     g_assert(index < AVAHI_DNS_PACKET_HEADER_SIZE);
130
131     return g_ntohs(((guint16*) AVAHI_DNS_PACKET_DATA(p))[index]);
132 }
133
134 void avahi_dns_packet_inc_field(AvahiDnsPacket *p, guint index) {
135     g_assert(p);
136
137     avahi_dns_packet_set_field(p, index, avahi_dns_packet_get_field(p, index) + 1);
138 }   
139
140 guint8* avahi_dns_packet_append_name(AvahiDnsPacket *p, const gchar *name) {
141     guint8 *d, *saved_ptr = NULL;
142     guint saved_size;
143     
144     g_assert(p);
145     g_assert(name);
146
147     saved_size = p->size;
148     saved_ptr = avahi_dns_packet_extend(p, 0);
149     
150     while (*name) {
151         guint8* prev;
152         const gchar *pname;
153         gchar label[64];
154
155         /* Check whether we can compress this name. */
156
157         if (p->name_table && (prev = g_hash_table_lookup(p->name_table, name))) {
158             guint index;
159             
160             g_assert(prev >= AVAHI_DNS_PACKET_DATA(p));
161             index = (guint) (prev - AVAHI_DNS_PACKET_DATA(p));
162
163             g_assert(index < p->size);
164
165             if (index < 0x4000) {
166                 guint16 *t;
167                 if (!(t = (guint16*) avahi_dns_packet_extend(p, sizeof(guint16))))
168                     return NULL;
169
170                 *t = g_htons((0xC000 | index));
171                 return saved_ptr;
172             }
173         }
174
175         pname = name;
176         
177         if (!(avahi_unescape_label(&name, label, sizeof(label))))
178             goto fail;
179
180         if (!(d = avahi_dns_packet_append_string(p, label)))
181             goto fail;
182
183         if (!p->name_table)
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);
186
187         g_hash_table_insert(p->name_table, g_strdup(pname), d);
188     }
189
190     if (!(d = avahi_dns_packet_extend(p, 1)))
191         goto fail;
192     
193     *d = 0;
194
195     return saved_ptr;
196
197 fail:
198     p->size = saved_size;
199     return NULL;
200 }
201
202 guint8* avahi_dns_packet_append_uint16(AvahiDnsPacket *p, guint16 v) {
203     guint8 *d;
204     g_assert(p);
205     
206     if (!(d = avahi_dns_packet_extend(p, sizeof(guint16))))
207         return NULL;
208     
209     *((guint16*) d) = g_htons(v);
210     return d;
211 }
212
213 guint8 *avahi_dns_packet_append_uint32(AvahiDnsPacket *p, guint32 v) {
214     guint8 *d;
215     g_assert(p);
216
217     if (!(d = avahi_dns_packet_extend(p, sizeof(guint32))))
218         return NULL;
219     
220     *((guint32*) d) = g_htonl(v);
221
222     return d;
223 }
224
225 guint8 *avahi_dns_packet_append_bytes(AvahiDnsPacket  *p, gconstpointer b, guint l) {
226     guint8* d;
227
228     g_assert(p);
229     g_assert(b);
230     g_assert(l);
231
232     if (!(d = avahi_dns_packet_extend(p, l)))
233         return NULL;
234
235     memcpy(d, b, l);
236     return d;
237 }
238
239 guint8* avahi_dns_packet_append_string(AvahiDnsPacket *p, const gchar *s) {
240     guint8* d;
241     guint k;
242     
243     g_assert(p);
244     g_assert(s);
245
246     if ((k = strlen(s)) >= 255)
247         k = 255;
248     
249     if (!(d = avahi_dns_packet_extend(p, k+1)))
250         return NULL;
251
252     *d = (guint8) k;
253     memcpy(d+1, s, k);
254
255     return d;
256 }
257
258 guint8 *avahi_dns_packet_extend(AvahiDnsPacket *p, guint l) {
259     guint8 *d;
260     
261     g_assert(p);
262
263     if (p->size+l > p->max_size)
264         return NULL;
265     
266     d = AVAHI_DNS_PACKET_DATA(p) + p->size;
267     p->size += l;
268     
269     return d;
270 }
271
272 gint avahi_dns_packet_check_valid(AvahiDnsPacket *p) {
273     guint16 flags;
274     g_assert(p);
275
276     if (p->size < 12)
277         return -1;
278
279     flags = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS);
280
281     if (flags & AVAHI_DNS_FLAG_OPCODE || flags & AVAHI_DNS_FLAG_RCODE)
282         return -1;
283
284     return 0;
285 }
286
287 gint avahi_dns_packet_is_query(AvahiDnsPacket *p) {
288     g_assert(p);
289     
290     return !(avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_QR);
291 }
292
293 static gint consume_labels(AvahiDnsPacket *p, guint index, gchar *ret_name, guint l) {
294     gint ret = 0;
295     int compressed = 0;
296     int first_label = 1;
297     g_assert(p && ret_name && l);
298     
299     for (;;) {
300         guint8 n;
301
302         if (index+1 > p->size)
303             return -1;
304
305         n = AVAHI_DNS_PACKET_DATA(p)[index];
306
307         if (!n) {
308             index++;
309             if (!compressed)
310                 ret++;
311
312             if (l < 1)
313                 return -1;
314             *ret_name = 0;
315             
316             return ret;
317             
318         } else if (n <= 63) {
319             /* Uncompressed label */
320             index++;
321             if (!compressed)
322                 ret++;
323         
324             if (index + n > p->size)
325                 return -1;
326
327             if ((guint) n + 1 > l)
328                 return -1;
329
330             if (!first_label) {
331                 *(ret_name++) = '.';
332                 l--;
333             } else
334                 first_label = 0;
335
336             if (!(avahi_escape_label(AVAHI_DNS_PACKET_DATA(p) + index, n, &ret_name, &l)))
337                 return -1;
338
339             index += n;
340             
341             if (!compressed)
342                 ret += n;
343         } else if ((n & 0xC0) == 0xC0) {
344             /* Compressed label */
345
346             if (index+2 > p->size)
347                 return -1;
348
349             index = ((guint) (AVAHI_DNS_PACKET_DATA(p)[index] & ~0xC0)) << 8 | AVAHI_DNS_PACKET_DATA(p)[index+1];
350
351             if (!compressed)
352                 ret += 2;
353             
354             compressed = 1;
355         } else
356             return -1;
357     }
358 }
359
360 gint avahi_dns_packet_consume_name(AvahiDnsPacket *p, gchar *ret_name, guint l) {
361     gint r;
362     
363     if ((r = consume_labels(p, p->rindex, ret_name, l)) < 0)
364         return -1;
365
366     p->rindex += r;
367     return 0;
368 }
369
370 gint avahi_dns_packet_consume_uint16(AvahiDnsPacket *p, guint16 *ret_v) {
371     g_assert(p);
372     g_assert(ret_v);
373
374     if (p->rindex + sizeof(guint16) > p->size)
375         return -1;
376
377     *ret_v = g_ntohs(*((guint16*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex)));
378     p->rindex += sizeof(guint16);
379
380     return 0;
381 }
382
383 gint avahi_dns_packet_consume_uint32(AvahiDnsPacket *p, guint32 *ret_v) {
384     g_assert(p);
385     g_assert(ret_v);
386
387     if (p->rindex + sizeof(guint32) > p->size)
388         return -1;
389
390     *ret_v = g_ntohl(*((guint32*) (AVAHI_DNS_PACKET_DATA(p) + p->rindex)));
391     p->rindex += sizeof(guint32);
392     
393     return 0;
394 }
395
396 gint avahi_dns_packet_consume_bytes(AvahiDnsPacket *p, gpointer ret_data, guint l) {
397     g_assert(p);
398     g_assert(ret_data);
399     g_assert(l > 0);
400     
401     if (p->rindex + l > p->size)
402         return -1;
403
404     memcpy(ret_data, AVAHI_DNS_PACKET_DATA(p) + p->rindex, l);
405     p->rindex += l;
406
407     return 0;
408 }
409
410 gint avahi_dns_packet_consume_string(AvahiDnsPacket *p, gchar *ret_string, guint l) {
411     guint k;
412     
413     g_assert(p);
414     g_assert(ret_string);
415     g_assert(l > 0);
416
417     if (p->rindex >= p->size)
418         return -1;
419
420     k = AVAHI_DNS_PACKET_DATA(p)[p->rindex];
421
422     if (p->rindex+1+k > p->size)
423         return -1;
424
425     if (l > k+1)
426         l = k+1;
427
428     memcpy(ret_string, AVAHI_DNS_PACKET_DATA(p)+p->rindex+1, l-1);
429     ret_string[l-1] = 0;
430
431     
432     p->rindex += 1+k;
433
434     return 0;
435     
436 }
437
438 gconstpointer avahi_dns_packet_get_rptr(AvahiDnsPacket *p) {
439     g_assert(p);
440     
441     if (p->rindex > p->size)
442         return NULL;
443
444     return AVAHI_DNS_PACKET_DATA(p) + p->rindex;
445 }
446
447 gint avahi_dns_packet_skip(AvahiDnsPacket *p, guint length) {
448     g_assert(p);
449
450     if (p->rindex + length > p->size)
451         return -1;
452
453     p->rindex += length;
454     return 0;
455 }
456
457 AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_cache_flush) {
458     gchar name[257], buf[257];
459     guint16 type, class;
460     guint32 ttl;
461     guint16 rdlength;
462     AvahiRecord *r = NULL;
463     gconstpointer start;
464
465     g_assert(p);
466     g_assert(ret_cache_flush);
467
468 /*     g_message("consume_record()"); */
469
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)
476         goto fail;
477
478 /*     g_message("name = %s, rdlength = %u", name, rdlength); */
479
480     *ret_cache_flush = !!(class & AVAHI_DNS_CACHE_FLUSH);
481     class &= ~AVAHI_DNS_CACHE_FLUSH;
482     
483     start = avahi_dns_packet_get_rptr(p);
484     
485     r = avahi_record_new_full(name, class, type);
486     
487     switch (type) {
488         case AVAHI_DNS_TYPE_PTR:
489         case AVAHI_DNS_TYPE_CNAME:
490
491 /*             g_message("ptr"); */
492             
493             if (avahi_dns_packet_consume_name(p, buf, sizeof(buf)) < 0)
494                 goto fail;
495
496             r->data.ptr.name = g_strdup(buf);
497             break;
498
499             
500         case AVAHI_DNS_TYPE_SRV:
501
502 /*             g_message("srv"); */
503             
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)
508                 goto fail;
509             
510             r->data.srv.name = g_strdup(buf);
511             break;
512
513         case AVAHI_DNS_TYPE_HINFO:
514             
515 /*             g_message("hinfo"); */
516
517             if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
518                 goto fail;
519
520             r->data.hinfo.cpu = g_strdup(buf);
521
522             if (avahi_dns_packet_consume_string(p, buf, sizeof(buf)) < 0)
523                 goto fail;
524
525             r->data.hinfo.os = g_strdup(buf);
526             break;
527
528         case AVAHI_DNS_TYPE_TXT:
529
530 /*             g_message("txt"); */
531
532             if (rdlength > 0) {
533                 r->data.txt.string_list = avahi_string_list_parse(avahi_dns_packet_get_rptr(p), rdlength);
534                 
535                 if (avahi_dns_packet_skip(p, rdlength) < 0)
536                     goto fail;
537             } else
538                 r->data.txt.string_list = NULL;
539             
540             break;
541
542         case AVAHI_DNS_TYPE_A:
543
544 /*             g_message("A"); */
545
546             if (avahi_dns_packet_consume_bytes(p, &r->data.a.address, sizeof(AvahiIPv4Address)) < 0)
547                 goto fail;
548             
549             break;
550
551         case AVAHI_DNS_TYPE_AAAA:
552
553 /*             g_message("aaaa"); */
554             
555             if (avahi_dns_packet_consume_bytes(p, &r->data.aaaa.address, sizeof(AvahiIPv6Address)) < 0)
556                 goto fail;
557             
558             break;
559             
560         default:
561
562 /*             g_message("generic"); */
563             
564             if (rdlength > 0) {
565
566                 r->data.generic.data = g_memdup(avahi_dns_packet_get_rptr(p), rdlength);
567                 
568                 if (avahi_dns_packet_skip(p, rdlength) < 0)
569                     goto fail;
570             }
571
572             break;
573     }
574
575 /*     g_message("%i == %u ?", (guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start, rdlength); */
576     
577     /* Check if we read enough data */
578     if ((guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start != rdlength)
579         goto fail;
580     
581     r->ttl = ttl;
582
583     return r;
584
585 fail:
586     if (r)
587         avahi_record_unref(r);
588
589     return NULL;
590 }
591
592 AvahiKey* avahi_dns_packet_consume_key(AvahiDnsPacket *p, gboolean *ret_unicast_response) {
593     gchar name[256];
594     guint16 type, class;
595
596     g_assert(p);
597     g_assert(ret_unicast_response);
598
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)
602         return NULL;
603
604     *ret_unicast_response = !!(class & AVAHI_DNS_UNICAST_RESPONSE);
605     class &= ~AVAHI_DNS_UNICAST_RESPONSE;
606
607     return avahi_key_new(name, class, type);
608 }
609
610 guint8* avahi_dns_packet_append_key(AvahiDnsPacket *p, AvahiKey *k, gboolean unicast_response) {
611     guint8 *t;
612     guint size;
613     
614     g_assert(p);
615     g_assert(k);
616
617     size = p->size;
618     
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))) {
622         p->size = size;
623         return NULL;
624     }
625
626     return t;
627 }
628
629 guint8* avahi_dns_packet_append_record(AvahiDnsPacket *p, AvahiRecord *r, gboolean cache_flush, guint max_ttl) {
630     guint8 *t, *l, *start;
631     guint size;
632
633     g_assert(p);
634     g_assert(r);
635
636     size = p->size;
637
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)))
643         goto fail;
644
645     start = avahi_dns_packet_extend(p, 0);
646
647     switch (r->key->type) {
648         
649         case AVAHI_DNS_TYPE_PTR:
650         case AVAHI_DNS_TYPE_CNAME :
651
652             if (!(avahi_dns_packet_append_name(p, r->data.ptr.name)))
653                 goto fail;
654             
655             break;
656
657         case AVAHI_DNS_TYPE_SRV:
658
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))
663                 goto fail;
664
665             break;
666
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))
670                 goto fail;
671
672             break;
673
674         case AVAHI_DNS_TYPE_TXT: {
675
676             guint8 *data;
677             guint size;
678
679             size = avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
680
681 /*             g_message("appending string: %u %p", size, r->data.txt.string_list); */
682
683             if (!(data = avahi_dns_packet_extend(p, size)))
684                 goto fail;
685
686             avahi_string_list_serialize(r->data.txt.string_list, data, size);
687             break;
688         }
689
690
691         case AVAHI_DNS_TYPE_A:
692
693             if (!avahi_dns_packet_append_bytes(p, &r->data.a.address, sizeof(r->data.a.address)))
694                 goto fail;
695             
696             break;
697
698         case AVAHI_DNS_TYPE_AAAA:
699             
700             if (!avahi_dns_packet_append_bytes(p, &r->data.aaaa.address, sizeof(r->data.aaaa.address)))
701                 goto fail;
702             
703             break;
704             
705         default:
706
707             if (r->data.generic.size &&
708                 avahi_dns_packet_append_bytes(p, r->data.generic.data, r->data.generic.size))
709                 goto fail;
710
711             break;
712     }
713
714
715
716     
717     size = avahi_dns_packet_extend(p, 0) - start;
718     g_assert(size <= 0xFFFF);
719
720 /*     g_message("appended %u", size); */
721
722     * (guint16*) l = g_htons((guint16) size);
723     
724     return t;
725
726
727 fail:
728     p->size = size;
729     return NULL;
730 }
731
732 gboolean avahi_dns_packet_is_empty(AvahiDnsPacket *p) {
733     g_assert(p);
734
735     return p->size <= AVAHI_DNS_PACKET_HEADER_SIZE;
736 }
737
738 guint avahi_dns_packet_space(AvahiDnsPacket *p) {
739     g_assert(p);
740
741     g_assert(p->size <= p->max_size);
742     
743     return p->max_size - p->size;
744 }