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) {
78 r = g_new(AvahiRecord, 1);
80 r->key = avahi_key_ref(k);
82 memset(&r->data, 0, sizeof(r->data));
84 r->ttl = AVAHI_DEFAULT_TTL;
89 AvahiRecord *avahi_record_new_full(const gchar *name, guint16 class, guint16 type) {
95 k = avahi_key_new(name, class, type);
96 r = avahi_record_new(k);
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)
152 if (class == AVAHI_DNS_CLASS_IN)
158 const gchar *avahi_dns_type_to_string(guint16 type) {
160 case AVAHI_DNS_TYPE_CNAME:
162 case AVAHI_DNS_TYPE_A:
164 case AVAHI_DNS_TYPE_AAAA:
166 case AVAHI_DNS_TYPE_PTR:
168 case AVAHI_DNS_TYPE_HINFO:
170 case AVAHI_DNS_TYPE_TXT:
172 case AVAHI_DNS_TYPE_SRV:
174 case AVAHI_DNS_TYPE_ANY:
182 gchar *avahi_key_to_string(const AvahiKey *k) {
184 g_assert(k->ref >= 1);
186 return g_strdup_printf("%s\t%s\t%s",
188 avahi_dns_class_to_string(k->class),
189 avahi_dns_type_to_string(k->type));
192 gchar *avahi_record_to_string(const AvahiRecord *r) {
194 char buf[257], *t = NULL, *d = NULL;
197 g_assert(r->ref >= 1);
199 switch (r->key->type) {
200 case AVAHI_DNS_TYPE_A:
201 inet_ntop(AF_INET, &r->data.a.address.address, t = buf, sizeof(buf));
204 case AVAHI_DNS_TYPE_AAAA:
205 inet_ntop(AF_INET6, &r->data.aaaa.address.address, t = buf, sizeof(buf));
208 case AVAHI_DNS_TYPE_PTR:
209 case AVAHI_DNS_TYPE_CNAME :
211 t = r->data.ptr.name;
214 case AVAHI_DNS_TYPE_TXT:
215 t = d = avahi_string_list_to_string(r->data.txt.string_list);
218 case AVAHI_DNS_TYPE_HINFO:
220 snprintf(t = buf, sizeof(buf), "\"%s\" \"%s\"", r->data.hinfo.cpu, r->data.hinfo.os);
223 case AVAHI_DNS_TYPE_SRV:
225 snprintf(t = buf, sizeof(buf), "%u %u %u %s",
226 r->data.srv.priority,
234 p = avahi_key_to_string(r->key);
235 s = g_strdup_printf("%s %s ; ttl=%u", p, t ? t : "<unparsable>", r->ttl);
242 gboolean avahi_key_equal(const AvahiKey *a, const AvahiKey *b) {
249 /* g_message("equal: %p %p", a, b); */
251 return avahi_domain_equal(a->name, b->name) &&
252 a->type == b->type &&
253 a->class == b->class;
256 gboolean avahi_key_pattern_match(const AvahiKey *pattern, const AvahiKey *k) {
260 /* g_message("equal: %p %p", a, b); */
262 g_assert(!avahi_key_is_pattern(k));
267 return avahi_domain_equal(pattern->name, k->name) &&
268 (pattern->type == k->type || pattern->type == AVAHI_DNS_TYPE_ANY) &&
269 pattern->class == k->class;
272 gboolean avahi_key_is_pattern(const AvahiKey *k) {
275 return k->type == AVAHI_DNS_TYPE_ANY;
279 guint avahi_key_hash(const AvahiKey *k) {
283 avahi_domain_hash(k->name) +
288 static gboolean rdata_equal(const AvahiRecord *a, const AvahiRecord *b) {
291 g_assert(a->key->type == b->key->type);
293 /* t = avahi_record_to_string(a); */
294 /* g_message("comparing %s", t); */
297 /* t = avahi_record_to_string(b); */
298 /* g_message("and %s", t); */
302 switch (a->key->type) {
303 case AVAHI_DNS_TYPE_SRV:
305 a->data.srv.priority == b->data.srv.priority &&
306 a->data.srv.weight == b->data.srv.weight &&
307 a->data.srv.port == b->data.srv.port &&
308 avahi_domain_equal(a->data.srv.name, b->data.srv.name);
310 case AVAHI_DNS_TYPE_PTR:
311 case AVAHI_DNS_TYPE_CNAME:
312 return avahi_domain_equal(a->data.ptr.name, b->data.ptr.name);
314 case AVAHI_DNS_TYPE_HINFO:
316 !g_utf8_collate(a->data.hinfo.cpu, b->data.hinfo.cpu) &&
317 !g_utf8_collate(a->data.hinfo.os, b->data.hinfo.os);
319 case AVAHI_DNS_TYPE_TXT:
320 return avahi_string_list_equal(a->data.txt.string_list, b->data.txt.string_list);
322 case AVAHI_DNS_TYPE_A:
323 return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address)) == 0;
325 case AVAHI_DNS_TYPE_AAAA:
326 return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address)) == 0;
329 return a->data.generic.size == b->data.generic.size &&
330 (a->data.generic.size == 0 || memcmp(a->data.generic.data, b->data.generic.data, a->data.generic.size) == 0);
335 gboolean avahi_record_equal_no_ttl(const AvahiRecord *a, const AvahiRecord *b) {
343 avahi_key_equal(a->key, b->key) &&
348 AvahiRecord *avahi_record_copy(AvahiRecord *r) {
351 copy = g_new(AvahiRecord, 1);
353 copy->key = avahi_key_ref(r->key);
356 switch (r->key->type) {
357 case AVAHI_DNS_TYPE_PTR:
358 case AVAHI_DNS_TYPE_CNAME:
359 copy->data.ptr.name = g_strdup(r->data.ptr.name);
362 case AVAHI_DNS_TYPE_SRV:
363 copy->data.srv.priority = r->data.srv.priority;
364 copy->data.srv.weight = r->data.srv.weight;
365 copy->data.srv.port = r->data.srv.port;
366 copy->data.srv.name = g_strdup(r->data.srv.name);
369 case AVAHI_DNS_TYPE_HINFO:
370 copy->data.hinfo.os = g_strdup(r->data.hinfo.os);
371 copy->data.hinfo.cpu = g_strdup(r->data.hinfo.cpu);
374 case AVAHI_DNS_TYPE_TXT:
375 copy->data.txt.string_list = avahi_string_list_copy(r->data.txt.string_list);
378 case AVAHI_DNS_TYPE_A:
379 copy->data.a.address = r->data.a.address;
382 case AVAHI_DNS_TYPE_AAAA:
383 copy->data.aaaa.address = r->data.aaaa.address;
387 copy->data.generic.data = g_memdup(r->data.generic.data, r->data.generic.size);
388 copy->data.generic.size = r->data.generic.size;
397 guint avahi_key_get_estimate_size(AvahiKey *k) {
400 return strlen(k->name)+1+4;
403 guint avahi_record_get_estimate_size(AvahiRecord *r) {
407 n = avahi_key_get_estimate_size(r->key) + 4 + 2;
409 switch (r->key->type) {
410 case AVAHI_DNS_TYPE_PTR:
411 case AVAHI_DNS_TYPE_CNAME:
412 n += strlen(r->data.ptr.name) + 1;
415 case AVAHI_DNS_TYPE_SRV:
416 n += 6 + strlen(r->data.srv.name) + 1;
419 case AVAHI_DNS_TYPE_HINFO:
420 n += strlen(r->data.hinfo.os) + 1 + strlen(r->data.hinfo.cpu) + 1;
423 case AVAHI_DNS_TYPE_TXT:
424 n += avahi_string_list_serialize(r->data.txt.string_list, NULL, 0);
427 case AVAHI_DNS_TYPE_A:
428 n += sizeof(AvahiIPv4Address);
431 case AVAHI_DNS_TYPE_AAAA:
432 n += sizeof(AvahiIPv6Address);
436 n += r->data.generic.size;
442 static gint lexicographical_memcmp(gconstpointer a, size_t al, gconstpointer b, size_t bl) {
449 c = al < bl ? al : bl;
450 if ((ret = memcmp(a, b, c)))
456 return al == c ? 1 : -1;
459 static gint uint16_cmp(guint16 a, guint16 b) {
460 return a == b ? 0 : (a < b ? -1 : 1);
463 gint avahi_record_lexicographical_compare(AvahiRecord *a, AvahiRecord *b) {
465 /* gchar *t1, *t2; */
470 /* t1 = avahi_record_to_string(a); */
471 /* t2 = avahi_record_to_string(b); */
472 /* g_message("lexicocmp: %s %s", t1, t2); */
479 if ((r = uint16_cmp(a->key->class, b->key->class)) ||
480 (r = uint16_cmp(a->key->type, b->key->type)))
483 switch (a->key->type) {
485 case AVAHI_DNS_TYPE_PTR:
486 case AVAHI_DNS_TYPE_CNAME:
487 return avahi_binary_domain_cmp(a->data.ptr.name, b->data.ptr.name);
489 case AVAHI_DNS_TYPE_SRV: {
490 if ((r = uint16_cmp(a->data.srv.priority, b->data.srv.priority)) == 0 &&
491 (r = uint16_cmp(a->data.srv.weight, b->data.srv.weight)) == 0 &&
492 (r = uint16_cmp(a->data.srv.port, b->data.srv.port)) == 0)
493 r = avahi_binary_domain_cmp(a->data.srv.name, b->data.srv.name);
498 case AVAHI_DNS_TYPE_HINFO: {
500 if ((r = strcmp(a->data.hinfo.cpu, b->data.hinfo.cpu)) ||
501 (r = strcmp(a->data.hinfo.os, b->data.hinfo.os)))
508 case AVAHI_DNS_TYPE_TXT: {
513 ma = g_new(guint8, asize = avahi_string_list_serialize(a->data.txt.string_list, NULL, 0));
514 mb = g_new(guint8, bsize = avahi_string_list_serialize(b->data.txt.string_list, NULL, 0));
515 avahi_string_list_serialize(a->data.txt.string_list, ma, asize);
516 avahi_string_list_serialize(b->data.txt.string_list, mb, bsize);
519 r = lexicographical_memcmp(ma, asize, mb, bsize);
520 else if (asize && !bsize)
522 else if (!asize && bsize)
533 case AVAHI_DNS_TYPE_A:
534 return memcmp(&a->data.a.address, &b->data.a.address, sizeof(AvahiIPv4Address));
536 case AVAHI_DNS_TYPE_AAAA:
537 return memcmp(&a->data.aaaa.address, &b->data.aaaa.address, sizeof(AvahiIPv6Address));
540 return lexicographical_memcmp(a->data.generic.data, a->data.generic.size,
541 b->data.generic.data, b->data.generic.size);
546 gboolean avahi_record_is_goodbye(AvahiRecord *r) {