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