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>
35 AvahiKey *avahi_key_new(const gchar *name, guint16 class, guint16 type) {
39 k = g_new(AvahiKey, 1);
41 k->name = avahi_normalize_name(name);
45 /* g_message("%p %% ref=1", k); */
50 AvahiKey *avahi_key_ref(AvahiKey *k) {
52 g_assert(k->ref >= 1);
56 /* g_message("%p ++ ref=%i", k, k->ref); */
61 void avahi_key_unref(AvahiKey *k) {
63 g_assert(k->ref >= 1);
65 /* g_message("%p -- ref=%i", k, k->ref-1); */
67 if ((--k->ref) <= 0) {
73 AvahiRecord *avahi_record_new(AvahiKey *k, guint32 ttl) {
78 r = g_new(AvahiRecord, 1);
80 r->key = avahi_key_ref(k);
82 memset(&r->data, 0, sizeof(r->data));
84 r->ttl = ttl != (guint32) -1 ? ttl : AVAHI_DEFAULT_TTL;
89 AvahiRecord *avahi_record_new_full(const gchar *name, guint16 class, guint16 type, guint32 ttl) {
95 k = avahi_key_new(name, class, type);
96 r = avahi_record_new(k, ttl);
102 AvahiRecord *avahi_record_ref(AvahiRecord *r) {
104 g_assert(r->ref >= 1);
110 void avahi_record_unref(AvahiRecord *r) {
112 g_assert(r->ref >= 1);
114 if ((--r->ref) <= 0) {
115 switch (r->key->type) {
117 case AVAHI_DNS_TYPE_SRV:
118 g_free(r->data.srv.name);
121 case AVAHI_DNS_TYPE_PTR:
122 case AVAHI_DNS_TYPE_CNAME:
123 g_free(r->data.ptr.name);
126 case AVAHI_DNS_TYPE_HINFO:
127 g_free(r->data.hinfo.cpu);
128 g_free(r->data.hinfo.os);
131 case AVAHI_DNS_TYPE_TXT:
132 avahi_string_list_free(r->data.txt.string_list);
135 case AVAHI_DNS_TYPE_A:
136 case AVAHI_DNS_TYPE_AAAA:
140 g_free(r->data.generic.data);
143 avahi_key_unref(r->key);
148 const gchar *avahi_dns_class_to_string(guint16 class) {
149 if (class & AVAHI_DNS_CACHE_FLUSH)
153 case AVAHI_DNS_CLASS_IN:
155 case AVAHI_DNS_CLASS_ANY:
162 const gchar *avahi_dns_type_to_string(guint16 type) {
164 case AVAHI_DNS_TYPE_CNAME:
166 case AVAHI_DNS_TYPE_A:
168 case AVAHI_DNS_TYPE_AAAA:
170 case AVAHI_DNS_TYPE_PTR:
172 case AVAHI_DNS_TYPE_HINFO:
174 case AVAHI_DNS_TYPE_TXT:
176 case AVAHI_DNS_TYPE_SRV:
178 case AVAHI_DNS_TYPE_ANY:
186 gchar *avahi_key_to_string(const AvahiKey *k) {
188 g_assert(k->ref >= 1);
190 return g_strdup_printf("%s\t%s\t%s",
192 avahi_dns_class_to_string(k->clazz),
193 avahi_dns_type_to_string(k->type));
196 gchar *avahi_record_to_string(const AvahiRecord *r) {
198 char buf[257], *t = NULL, *d = NULL;
201 g_assert(r->ref >= 1);
203 switch (r->key->type) {
204 case AVAHI_DNS_TYPE_A:
205 inet_ntop(AF_INET, &r->data.a.address.address, t = buf, sizeof(buf));
208 case AVAHI_DNS_TYPE_AAAA:
209 inet_ntop(AF_INET6, &r->data.aaaa.address.address, t = buf, sizeof(buf));
212 case AVAHI_DNS_TYPE_PTR:
213 case AVAHI_DNS_TYPE_CNAME :
215 t = r->data.ptr.name;
218 case AVAHI_DNS_TYPE_TXT:
219 t = d = avahi_string_list_to_string(r->data.txt.string_list);
222 case AVAHI_DNS_TYPE_HINFO:
224 snprintf(t = buf, sizeof(buf), "\"%s\" \"%s\"", r->data.hinfo.cpu, r->data.hinfo.os);
227 case AVAHI_DNS_TYPE_SRV:
229 snprintf(t = buf, sizeof(buf), "%u %u %u %s",
230 r->data.srv.priority,
238 p = avahi_key_to_string(r->key);
239 s = g_strdup_printf("%s %s ; ttl=%u", p, t ? t : "<unparsable>", r->ttl);
246 gboolean avahi_key_equal(const AvahiKey *a, const AvahiKey *b) {
253 /* g_message("equal: %p %p", a, b); */
255 return avahi_domain_equal(a->name, b->name) &&
256 a->type == b->type &&
257 a->clazz == b->clazz;
260 gboolean avahi_key_pattern_match(const AvahiKey *pattern, const AvahiKey *k) {
264 /* g_message("equal: %p %p", a, b); */
266 g_assert(!avahi_key_is_pattern(k));
271 return avahi_domain_equal(pattern->name, k->name) &&
272 (pattern->type == k->type || pattern->type == AVAHI_DNS_TYPE_ANY) &&
273 (pattern->clazz == k->clazz || pattern->clazz == AVAHI_DNS_CLASS_ANY);
276 gboolean avahi_key_is_pattern(const AvahiKey *k) {
280 k->type == AVAHI_DNS_TYPE_ANY ||
281 k->clazz == AVAHI_DNS_CLASS_ANY;
284 guint avahi_key_hash(const AvahiKey *k) {
288 avahi_domain_hash(k->name) +
293 static gboolean rdata_equal(const AvahiRecord *a, const AvahiRecord *b) {
296 g_assert(a->key->type == b->key->type);
298 /* t = avahi_record_to_string(a); */
299 /* g_message("comparing %s", t); */
302 /* t = avahi_record_to_string(b); */
303 /* g_message("and %s", t); */
307 switch (a->key->type) {
308 case AVAHI_DNS_TYPE_SRV:
310 a->data.srv.priority == b->data.srv.priority &&
311 a->data.srv.weight == b->data.srv.weight &&
312 a->data.srv.port == b->data.srv.port &&
313 avahi_domain_equal(a->data.srv.name, b->data.srv.name);
315 case AVAHI_DNS_TYPE_PTR:
316 case AVAHI_DNS_TYPE_CNAME:
317 return avahi_domain_equal(a->data.ptr.name, b->data.ptr.name);
319 case AVAHI_DNS_TYPE_HINFO:
321 !strcmp(a->data.hinfo.cpu, b->data.hinfo.cpu) &&
322 !strcmp(a->data.hinfo.os, b->data.hinfo.os);
324 case AVAHI_DNS_TYPE_TXT:
325 return avahi_string_list_equal(a->data.txt.string_list, b->data.txt.string_list);
327 case AVAHI_DNS_TYPE_A:
328 return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address)) == 0;
330 case AVAHI_DNS_TYPE_AAAA:
331 return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address)) == 0;
334 return a->data.generic.size == b->data.generic.size &&
335 (a->data.generic.size == 0 || memcmp(a->data.generic.data, b->data.generic.data, a->data.generic.size) == 0);
340 gboolean avahi_record_equal_no_ttl(const AvahiRecord *a, const AvahiRecord *b) {
348 avahi_key_equal(a->key, b->key) &&
353 AvahiRecord *avahi_record_copy(AvahiRecord *r) {
356 copy = g_new(AvahiRecord, 1);
358 copy->key = avahi_key_ref(r->key);
361 switch (r->key->type) {
362 case AVAHI_DNS_TYPE_PTR:
363 case AVAHI_DNS_TYPE_CNAME:
364 copy->data.ptr.name = g_strdup(r->data.ptr.name);
367 case AVAHI_DNS_TYPE_SRV:
368 copy->data.srv.priority = r->data.srv.priority;
369 copy->data.srv.weight = r->data.srv.weight;
370 copy->data.srv.port = r->data.srv.port;
371 copy->data.srv.name = g_strdup(r->data.srv.name);
374 case AVAHI_DNS_TYPE_HINFO:
375 copy->data.hinfo.os = g_strdup(r->data.hinfo.os);
376 copy->data.hinfo.cpu = g_strdup(r->data.hinfo.cpu);
379 case AVAHI_DNS_TYPE_TXT:
380 copy->data.txt.string_list = avahi_string_list_copy(r->data.txt.string_list);
383 case AVAHI_DNS_TYPE_A:
384 copy->data.a.address = r->data.a.address;
387 case AVAHI_DNS_TYPE_AAAA:
388 copy->data.aaaa.address = r->data.aaaa.address;
392 copy->data.generic.data = g_memdup(r->data.generic.data, r->data.generic.size);
393 copy->data.generic.size = r->data.generic.size;
402 guint avahi_key_get_estimate_size(AvahiKey *k) {
405 return strlen(k->name)+1+4;
408 guint avahi_record_get_estimate_size(AvahiRecord *r) {
412 n = avahi_key_get_estimate_size(r->key) + 4 + 2;
414 switch (r->key->type) {
415 case AVAHI_DNS_TYPE_PTR:
416 case AVAHI_DNS_TYPE_CNAME:
417 n += strlen(r->data.ptr.name) + 1;
420 case AVAHI_DNS_TYPE_SRV:
421 n += 6 + strlen(r->data.srv.name) + 1;
424 case AVAHI_DNS_TYPE_HINFO:
425 n += strlen(r->data.hinfo.os) + 1 + strlen(r->data.hinfo.cpu) + 1;
428 case AVAHI_DNS_TYPE_TXT:
429 n += avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
432 case AVAHI_DNS_TYPE_A:
433 n += sizeof(AvahiIPv4Address);
436 case AVAHI_DNS_TYPE_AAAA:
437 n += sizeof(AvahiIPv6Address);
441 n += r->data.generic.size;
447 static gint lexicographical_memcmp(gconstpointer a, size_t al, gconstpointer b, size_t bl) {
454 c = al < bl ? al : bl;
455 if ((ret = memcmp(a, b, c)))
461 return al == c ? 1 : -1;
464 static gint uint16_cmp(guint16 a, guint16 b) {
465 return a == b ? 0 : (a < b ? -1 : 1);
468 gint avahi_record_lexicographical_compare(AvahiRecord *a, AvahiRecord *b) {
470 /* gchar *t1, *t2; */
475 /* t1 = avahi_record_to_string(a); */
476 /* t2 = avahi_record_to_string(b); */
477 /* g_message("lexicocmp: %s %s", t1, t2); */
484 if ((r = uint16_cmp(a->key->clazz, b->key->clazz)) ||
485 (r = uint16_cmp(a->key->type, b->key->type)))
488 switch (a->key->type) {
490 case AVAHI_DNS_TYPE_PTR:
491 case AVAHI_DNS_TYPE_CNAME:
492 return avahi_binary_domain_cmp(a->data.ptr.name, b->data.ptr.name);
494 case AVAHI_DNS_TYPE_SRV: {
495 if ((r = uint16_cmp(a->data.srv.priority, b->data.srv.priority)) == 0 &&
496 (r = uint16_cmp(a->data.srv.weight, b->data.srv.weight)) == 0 &&
497 (r = uint16_cmp(a->data.srv.port, b->data.srv.port)) == 0)
498 r = avahi_binary_domain_cmp(a->data.srv.name, b->data.srv.name);
503 case AVAHI_DNS_TYPE_HINFO: {
505 if ((r = strcmp(a->data.hinfo.cpu, b->data.hinfo.cpu)) ||
506 (r = strcmp(a->data.hinfo.os, b->data.hinfo.os)))
513 case AVAHI_DNS_TYPE_TXT: {
518 ma = g_new(guint8, asize = avahi_string_list_serialize(a->data.txt.string_list, NULL, 0));
519 mb = g_new(guint8, bsize = avahi_string_list_serialize(b->data.txt.string_list, NULL, 0));
520 avahi_string_list_serialize(a->data.txt.string_list, ma, asize);
521 avahi_string_list_serialize(b->data.txt.string_list, mb, bsize);
524 r = lexicographical_memcmp(ma, asize, mb, bsize);
525 else if (asize && !bsize)
527 else if (!asize && bsize)
538 case AVAHI_DNS_TYPE_A:
539 return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address));
541 case AVAHI_DNS_TYPE_AAAA:
542 return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address));
545 return lexicographical_memcmp(a->data.generic.data, a->data.generic.size,
546 b->data.generic.data, b->data.generic.size);
551 gboolean avahi_record_is_goodbye(AvahiRecord *r) {