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