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
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
33 #include <avahi-common/domain.h>
34 #include <avahi-common/malloc.h>
35 #include <avahi-common/defs.h>
41 #include "domain-util.h"
44 AvahiKey *avahi_key_new(const char *name, uint16_t class, uint16_t type) {
48 if (!(k = avahi_new(AvahiKey, 1))) {
49 avahi_log_error("avahi_new() failed.");
53 if (!(k->name = avahi_normalize_name_strdup(name))) {
54 avahi_log_error("avahi_normalize_name() failed.");
66 AvahiKey *avahi_key_new_cname(AvahiKey *key) {
69 if (key->clazz != AVAHI_DNS_CLASS_IN)
72 if (key->type == AVAHI_DNS_TYPE_CNAME)
75 return avahi_key_new(key->name, key->clazz, AVAHI_DNS_TYPE_CNAME);
78 AvahiKey *avahi_key_ref(AvahiKey *k) {
87 void avahi_key_unref(AvahiKey *k) {
91 if ((--k->ref) <= 0) {
97 AvahiRecord *avahi_record_new(AvahiKey *k, uint32_t ttl) {
102 if (!(r = avahi_new(AvahiRecord, 1))) {
103 avahi_log_error("avahi_new() failed.");
108 r->key = avahi_key_ref(k);
110 memset(&r->data, 0, sizeof(r->data));
112 r->ttl = ttl != (uint32_t) -1 ? ttl : AVAHI_DEFAULT_TTL;
117 AvahiRecord *avahi_record_new_full(const char *name, uint16_t class, uint16_t type, uint32_t ttl) {
123 if (!(k = avahi_key_new(name, class, type))) {
124 avahi_log_error("avahi_key_new() failed.");
128 r = avahi_record_new(k, ttl);
132 avahi_log_error("avahi_record_new() failed.");
139 AvahiRecord *avahi_record_ref(AvahiRecord *r) {
147 void avahi_record_unref(AvahiRecord *r) {
151 if ((--r->ref) <= 0) {
152 switch (r->key->type) {
154 case AVAHI_DNS_TYPE_SRV:
155 avahi_free(r->data.srv.name);
158 case AVAHI_DNS_TYPE_PTR:
159 case AVAHI_DNS_TYPE_CNAME:
160 case AVAHI_DNS_TYPE_NS:
161 avahi_free(r->data.ptr.name);
164 case AVAHI_DNS_TYPE_HINFO:
165 avahi_free(r->data.hinfo.cpu);
166 avahi_free(r->data.hinfo.os);
169 case AVAHI_DNS_TYPE_TXT:
170 avahi_string_list_free(r->data.txt.string_list);
173 case AVAHI_DNS_TYPE_A:
174 case AVAHI_DNS_TYPE_AAAA:
178 avahi_free(r->data.generic.data);
181 avahi_key_unref(r->key);
186 const char *avahi_dns_class_to_string(uint16_t class) {
187 if (class & AVAHI_DNS_CACHE_FLUSH)
191 case AVAHI_DNS_CLASS_IN:
193 case AVAHI_DNS_CLASS_ANY:
200 const char *avahi_dns_type_to_string(uint16_t type) {
202 case AVAHI_DNS_TYPE_CNAME:
204 case AVAHI_DNS_TYPE_A:
206 case AVAHI_DNS_TYPE_AAAA:
208 case AVAHI_DNS_TYPE_PTR:
210 case AVAHI_DNS_TYPE_HINFO:
212 case AVAHI_DNS_TYPE_TXT:
214 case AVAHI_DNS_TYPE_SRV:
216 case AVAHI_DNS_TYPE_ANY:
218 case AVAHI_DNS_TYPE_SOA:
220 case AVAHI_DNS_TYPE_NS:
227 char *avahi_key_to_string(const AvahiKey *k) {
228 char class[16], type[16];
234 /* According to RFC3597 */
236 if (!(c = avahi_dns_class_to_string(k->clazz))) {
237 snprintf(class, sizeof(class), "CLASS%u", k->clazz);
241 if (!(t = avahi_dns_type_to_string(k->type))) {
242 snprintf(type, sizeof(type), "TYPE%u", k->type);
246 return avahi_strdup_printf("%s\t%s\t%s", k->name, c, t);
249 char *avahi_record_to_string(const AvahiRecord *r) {
251 char buf[1024], *t = NULL, *d = NULL;
256 switch (r->key->type) {
257 case AVAHI_DNS_TYPE_A:
258 inet_ntop(AF_INET, &r->data.a.address.address, t = buf, sizeof(buf));
261 case AVAHI_DNS_TYPE_AAAA:
262 inet_ntop(AF_INET6, &r->data.aaaa.address.address, t = buf, sizeof(buf));
265 case AVAHI_DNS_TYPE_PTR:
266 case AVAHI_DNS_TYPE_CNAME:
267 case AVAHI_DNS_TYPE_NS:
269 t = r->data.ptr.name;
272 case AVAHI_DNS_TYPE_TXT:
273 t = d = avahi_string_list_to_string(r->data.txt.string_list);
276 case AVAHI_DNS_TYPE_HINFO:
278 snprintf(t = buf, sizeof(buf), "\"%s\" \"%s\"", r->data.hinfo.cpu, r->data.hinfo.os);
281 case AVAHI_DNS_TYPE_SRV:
283 snprintf(t = buf, sizeof(buf), "%u %u %u %s",
284 r->data.srv.priority,
298 /* According to RFC3597 */
300 snprintf(t = buf, sizeof(buf), "\\# %u", r->data.generic.size);
304 for (c = r->data.generic.data, n = r->data.generic.size, i = 0;
308 sprintf(e, " %02X", *c);
316 p = avahi_key_to_string(r->key);
317 s = avahi_strdup_printf("%s %s ; ttl=%u", p, t, r->ttl);
324 int avahi_key_equal(const AvahiKey *a, const AvahiKey *b) {
331 return avahi_domain_equal(a->name, b->name) &&
332 a->type == b->type &&
333 a->clazz == b->clazz;
336 int avahi_key_pattern_match(const AvahiKey *pattern, const AvahiKey *k) {
340 assert(!avahi_key_is_pattern(k));
345 return avahi_domain_equal(pattern->name, k->name) &&
346 (pattern->type == k->type || pattern->type == AVAHI_DNS_TYPE_ANY) &&
347 (pattern->clazz == k->clazz || pattern->clazz == AVAHI_DNS_CLASS_ANY);
350 int avahi_key_is_pattern(const AvahiKey *k) {
354 k->type == AVAHI_DNS_TYPE_ANY ||
355 k->clazz == AVAHI_DNS_CLASS_ANY;
358 unsigned avahi_key_hash(const AvahiKey *k) {
362 avahi_domain_hash(k->name) +
367 static int rdata_equal(const AvahiRecord *a, const AvahiRecord *b) {
370 assert(a->key->type == b->key->type);
372 switch (a->key->type) {
373 case AVAHI_DNS_TYPE_SRV:
375 a->data.srv.priority == b->data.srv.priority &&
376 a->data.srv.weight == b->data.srv.weight &&
377 a->data.srv.port == b->data.srv.port &&
378 avahi_domain_equal(a->data.srv.name, b->data.srv.name);
380 case AVAHI_DNS_TYPE_PTR:
381 case AVAHI_DNS_TYPE_CNAME:
382 case AVAHI_DNS_TYPE_NS:
383 return avahi_domain_equal(a->data.ptr.name, b->data.ptr.name);
385 case AVAHI_DNS_TYPE_HINFO:
387 !strcmp(a->data.hinfo.cpu, b->data.hinfo.cpu) &&
388 !strcmp(a->data.hinfo.os, b->data.hinfo.os);
390 case AVAHI_DNS_TYPE_TXT:
391 return avahi_string_list_equal(a->data.txt.string_list, b->data.txt.string_list);
393 case AVAHI_DNS_TYPE_A:
394 return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address)) == 0;
396 case AVAHI_DNS_TYPE_AAAA:
397 return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address)) == 0;
400 return a->data.generic.size == b->data.generic.size &&
401 (a->data.generic.size == 0 || memcmp(a->data.generic.data, b->data.generic.data, a->data.generic.size) == 0);
406 int avahi_record_equal_no_ttl(const AvahiRecord *a, const AvahiRecord *b) {
414 avahi_key_equal(a->key, b->key) &&
419 AvahiRecord *avahi_record_copy(AvahiRecord *r) {
422 if (!(copy = avahi_new(AvahiRecord, 1))) {
423 avahi_log_error("avahi_new() failed.");
428 copy->key = avahi_key_ref(r->key);
431 switch (r->key->type) {
432 case AVAHI_DNS_TYPE_PTR:
433 case AVAHI_DNS_TYPE_CNAME:
434 case AVAHI_DNS_TYPE_NS:
435 if (!(copy->data.ptr.name = avahi_strdup(r->data.ptr.name)))
439 case AVAHI_DNS_TYPE_SRV:
440 copy->data.srv.priority = r->data.srv.priority;
441 copy->data.srv.weight = r->data.srv.weight;
442 copy->data.srv.port = r->data.srv.port;
443 if (!(copy->data.srv.name = avahi_strdup(r->data.srv.name)))
447 case AVAHI_DNS_TYPE_HINFO:
448 if (!(copy->data.hinfo.os = avahi_strdup(r->data.hinfo.os)))
451 if (!(copy->data.hinfo.cpu = avahi_strdup(r->data.hinfo.cpu))) {
452 avahi_free(r->data.hinfo.os);
457 case AVAHI_DNS_TYPE_TXT:
458 copy->data.txt.string_list = avahi_string_list_copy(r->data.txt.string_list);
461 case AVAHI_DNS_TYPE_A:
462 copy->data.a.address = r->data.a.address;
465 case AVAHI_DNS_TYPE_AAAA:
466 copy->data.aaaa.address = r->data.aaaa.address;
470 if (!(copy->data.generic.data = avahi_memdup(r->data.generic.data, r->data.generic.size)))
472 copy->data.generic.size = r->data.generic.size;
480 avahi_log_error("Failed to allocate memory");
482 avahi_key_unref(copy->key);
489 size_t avahi_key_get_estimate_size(AvahiKey *k) {
492 return strlen(k->name)+1+4;
495 size_t avahi_record_get_estimate_size(AvahiRecord *r) {
499 n = avahi_key_get_estimate_size(r->key) + 4 + 2;
501 switch (r->key->type) {
502 case AVAHI_DNS_TYPE_PTR:
503 case AVAHI_DNS_TYPE_CNAME:
504 case AVAHI_DNS_TYPE_NS:
505 n += strlen(r->data.ptr.name) + 1;
508 case AVAHI_DNS_TYPE_SRV:
509 n += 6 + strlen(r->data.srv.name) + 1;
512 case AVAHI_DNS_TYPE_HINFO:
513 n += strlen(r->data.hinfo.os) + 1 + strlen(r->data.hinfo.cpu) + 1;
516 case AVAHI_DNS_TYPE_TXT:
517 n += avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
520 case AVAHI_DNS_TYPE_A:
521 n += sizeof(AvahiIPv4Address);
524 case AVAHI_DNS_TYPE_AAAA:
525 n += sizeof(AvahiIPv6Address);
529 n += r->data.generic.size;
535 static int lexicographical_memcmp(const void* a, size_t al, const void* b, size_t bl) {
542 c = al < bl ? al : bl;
543 if ((ret = memcmp(a, b, c)))
549 return al == c ? 1 : -1;
552 static int uint16_cmp(uint16_t a, uint16_t b) {
553 return a == b ? 0 : (a < b ? -1 : 1);
556 int avahi_record_lexicographical_compare(AvahiRecord *a, AvahiRecord *b) {
563 /* t1 = avahi_record_to_string(a); */
564 /* t2 = avahi_record_to_string(b); */
565 /* g_message("lexicocmp: %s %s", t1, t2); */
566 /* avahi_free(t1); */
567 /* avahi_free(t2); */
572 if ((r = uint16_cmp(a->key->clazz, b->key->clazz)) ||
573 (r = uint16_cmp(a->key->type, b->key->type)))
576 switch (a->key->type) {
578 case AVAHI_DNS_TYPE_PTR:
579 case AVAHI_DNS_TYPE_CNAME:
580 case AVAHI_DNS_TYPE_NS:
581 return avahi_binary_domain_cmp(a->data.ptr.name, b->data.ptr.name);
583 case AVAHI_DNS_TYPE_SRV: {
584 if ((r = uint16_cmp(a->data.srv.priority, b->data.srv.priority)) == 0 &&
585 (r = uint16_cmp(a->data.srv.weight, b->data.srv.weight)) == 0 &&
586 (r = uint16_cmp(a->data.srv.port, b->data.srv.port)) == 0)
587 r = avahi_binary_domain_cmp(a->data.srv.name, b->data.srv.name);
592 case AVAHI_DNS_TYPE_HINFO: {
594 if ((r = strcmp(a->data.hinfo.cpu, b->data.hinfo.cpu)) ||
595 (r = strcmp(a->data.hinfo.os, b->data.hinfo.os)))
602 case AVAHI_DNS_TYPE_TXT: {
604 uint8_t *ma = NULL, *mb = NULL;
607 asize = avahi_string_list_serialize(a->data.txt.string_list, NULL, 0);
608 bsize = avahi_string_list_serialize(b->data.txt.string_list, NULL, 0);
610 if (asize > 0 && !(ma = avahi_new(uint8_t, asize)))
613 if (bsize > 0 && !(mb = avahi_new(uint8_t, bsize))) {
618 avahi_string_list_serialize(a->data.txt.string_list, ma, asize);
619 avahi_string_list_serialize(b->data.txt.string_list, mb, bsize);
622 r = lexicographical_memcmp(ma, asize, mb, bsize);
623 else if (asize && !bsize)
625 else if (!asize && bsize)
636 case AVAHI_DNS_TYPE_A:
637 return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address));
639 case AVAHI_DNS_TYPE_AAAA:
640 return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address));
643 return lexicographical_memcmp(a->data.generic.data, a->data.generic.size,
644 b->data.generic.data, b->data.generic.size);
649 avahi_log_error(__FILE__": Out of memory");
650 return -1; /* or whatever ... */
653 int avahi_record_is_goodbye(AvahiRecord *r) {
659 int avahi_key_is_valid(AvahiKey *k) {
662 if (!avahi_is_valid_domain_name(k->name))
668 int avahi_record_is_valid(AvahiRecord *r) {
671 if (!avahi_key_is_valid(r->key))
674 switch (r->key->type) {
676 case AVAHI_DNS_TYPE_PTR:
677 case AVAHI_DNS_TYPE_CNAME:
678 case AVAHI_DNS_TYPE_NS:
679 return avahi_is_valid_domain_name(r->data.ptr.name);
681 case AVAHI_DNS_TYPE_SRV:
682 return avahi_is_valid_domain_name(r->data.srv.name);
684 case AVAHI_DNS_TYPE_HINFO:
686 strlen(r->data.hinfo.os) <= 255 &&
687 strlen(r->data.hinfo.cpu) <= 255;
689 case AVAHI_DNS_TYPE_TXT: {
691 AvahiStringList *strlst;
693 for (strlst = r->data.txt.string_list; strlst; strlst = strlst->next)
694 if (strlst->size > 255 || strlst->size <= 0)