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
26 #include <sys/socket.h>
27 #include <arpa/inet.h>
29 #include <sys/utsname.h>
36 #include "subscribe.h"
38 static void free_entry(AvahiServer*s, AvahiEntry *e) {
44 avahi_goodbye_entry(s, e, TRUE);
46 /* Remove from linked list */
47 AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
49 /* Remove from hash table indexed by name */
50 t = g_hash_table_lookup(s->entries_by_key, e->record->key);
51 AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
53 g_hash_table_replace(s->entries_by_key, t->record->key, t);
55 g_hash_table_remove(s->entries_by_key, e->record->key);
57 /* Remove from associated group */
59 AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
61 avahi_record_unref(e->record);
65 static void free_group(AvahiServer *s, AvahiEntryGroup *g) {
70 free_entry(s, g->entries);
72 AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g);
76 static void cleanup_dead(AvahiServer *s) {
77 AvahiEntryGroup *g, *ng;
82 if (s->need_group_cleanup) {
83 for (g = s->groups; g; g = ng) {
90 s->need_group_cleanup = FALSE;
93 if (s->need_entry_cleanup) {
94 for (e = s->entries; e; e = ne) {
101 s->need_entry_cleanup = FALSE;
105 static void add_aux_records(AvahiServer *s, AvahiInterface *i, const gchar *name, guint16 type, gboolean unicast_response) {
112 k = avahi_key_new(name, AVAHI_DNS_CLASS_IN, type);
113 avahi_server_prepare_matching_responses(s, i, k, unicast_response);
117 void avahi_server_prepare_response(AvahiServer *s, AvahiInterface *i, AvahiEntry *e, gboolean unicast_response) {
122 /* Append a record to a packet and all the records referred by it */
124 avahi_record_list_push(s->record_list, e->record, e->flags & AVAHI_ENTRY_UNIQUE, unicast_response);
126 if (e->record->key->class == AVAHI_DNS_CLASS_IN) {
127 if (e->record->key->type == AVAHI_DNS_TYPE_PTR) {
128 add_aux_records(s, i, e->record->data.ptr.name, AVAHI_DNS_TYPE_SRV, unicast_response);
129 add_aux_records(s, i, e->record->data.ptr.name, AVAHI_DNS_TYPE_TXT, unicast_response);
130 } else if (e->record->key->type == AVAHI_DNS_TYPE_SRV) {
131 add_aux_records(s, i, e->record->data.srv.name, AVAHI_DNS_TYPE_A, unicast_response);
132 add_aux_records(s, i, e->record->data.srv.name, AVAHI_DNS_TYPE_AAAA, unicast_response);
137 void avahi_server_prepare_matching_responses(AvahiServer *s, AvahiInterface *i, AvahiKey *k, gboolean unicast_response) {
145 g_message("Posting responses matching [%s]", txt = avahi_key_to_string(k));
148 if (avahi_key_is_pattern(k)) {
150 /* Handle ANY query */
152 for (e = s->entries; e; e = e->entries_next)
153 if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_registered(s, e, i))
154 avahi_server_prepare_response(s, i, e, unicast_response);
158 /* Handle all other queries */
160 for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next)
161 if (!e->dead && avahi_entry_registered(s, e, i))
162 avahi_server_prepare_response(s, i, e, unicast_response);
166 static void withdraw_entry(AvahiServer *s, AvahiEntry *e) {
173 for (k = e->group->entries; k; k = k->by_group_next) {
174 avahi_goodbye_entry(s, k, FALSE);
178 avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
180 avahi_goodbye_entry(s, e, FALSE);
184 s->need_entry_cleanup = TRUE;
187 static void withdraw_rrset(AvahiServer *s, AvahiKey *key) {
193 for (e = g_hash_table_lookup(s->entries_by_key, key); e; e = e->by_key_next)
194 withdraw_entry(s, e);
197 static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) {
200 gboolean ours = FALSE, won = FALSE, lost = FALSE;
206 for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
210 if (e->dead || !avahi_entry_probing(s, e, i))
213 if ((cmp = avahi_record_lexicographical_compare(e->record, record)) == 0) {
222 t = avahi_record_to_string(record);
227 g_message("Recieved conflicting probe [%s]. Local host won.", t);
229 g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
230 withdraw_rrset(s, record->key);
237 static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) {
238 gboolean valid = TRUE, ours = FALSE, conflict = FALSE, withdraw_immediately = FALSE;
239 AvahiEntry *e, *n, *conflicting_entry = NULL;
246 t = avahi_record_to_string(record);
248 g_message("CHECKING FOR CONFLICT: [%s]", t);
250 for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
253 if (e->dead || (!(e->flags & AVAHI_ENTRY_UNIQUE) && !unique))
256 /* Either our entry or the other is intended to be unique, so let's check */
258 if (avahi_entry_registered(s, e, i)) {
260 if (avahi_record_equal_no_ttl(e->record, record)) {
261 ours = TRUE; /* We have an identical record, so this is no conflict */
263 /* Check wheter there is a TTL conflict */
264 if (record->ttl <= e->record->ttl/2) {
266 g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
267 avahi_interface_post_response(i, e->record, FALSE, TRUE, NULL);
271 /* There's no need to check the other entries of this RRset */
274 /* A conflict => we have to return to probe mode */
276 conflicting_entry = e;
279 } else if (avahi_entry_probing(s, e, i)) {
281 if (!avahi_record_equal_no_ttl(record, e->record)) {
283 /* We are currently registering a matching record, but
284 * someone else already claimed it, so let's
287 withdraw_immediately = TRUE;
292 /* g_message("ours=%i conflict=%i", ours, conflict); */
294 if (!ours && conflict) {
297 if (withdraw_immediately) {
298 g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
299 withdraw_rrset(s, record->key);
301 g_assert(conflicting_entry);
302 g_message("Recieved conflicting record [%s]. Resetting our record.", t);
303 avahi_entry_return_to_initial_state(s, conflicting_entry, i);
305 /* Local unique records are returned to probin
306 * state. Local shared records are reannounced. */
315 void avahi_server_generate_response(AvahiServer *s, AvahiInterface *i, AvahiDnsPacket *p, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
319 g_assert(!legacy_unicast || (a && port > 0 && p));
321 if (legacy_unicast) {
322 AvahiDnsPacket *reply;
325 reply = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , TRUE, TRUE);
327 while ((r = avahi_record_list_pop(s->record_list, NULL, NULL))) {
329 if (avahi_dns_packet_append_record(reply, r, FALSE, 10))
330 avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
332 gchar *t = avahi_record_to_string(r);
333 g_warning("Record [%s] not fitting in legacy unicast packet, dropping.", t);
337 avahi_record_unref(r);
340 if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0)
341 avahi_interface_send_packet_unicast(i, reply, a, port);
343 avahi_dns_packet_free(reply);
346 gboolean unicast_response, flush_cache;
347 AvahiDnsPacket *reply = NULL;
350 while ((r = avahi_record_list_pop(s->record_list, &flush_cache, &unicast_response))) {
352 if (!avahi_interface_post_response(i, r, flush_cache, FALSE, a) && unicast_response) {
354 /* Due to some reasons the record has not been scheduled.
355 * The client requested an unicast response in that
356 * case. Therefore we prepare such a response */
362 reply = avahi_dns_packet_new_reply(p, i->hardware->mtu, FALSE, FALSE);
365 if (avahi_dns_packet_append_record(reply, r, flush_cache, 0)) {
367 /* Appending this record succeeded, so incremeant
368 * the specific header field, and return to the caller */
370 avahi_dns_packet_inc_field(reply, AVAHI_DNS_FIELD_ANCOUNT);
375 if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) == 0) {
376 gchar *t = avahi_record_to_string(r);
377 g_warning("Record [%s] too large, doesn't fit in any packet!", t);
382 /* Appending the record didn't succeeed, so let's send this packet, and create a new one */
383 avahi_interface_send_packet_unicast(i, reply, a, port);
384 avahi_dns_packet_free(reply);
389 avahi_record_unref(r);
393 if (avahi_dns_packet_get_field(reply, AVAHI_DNS_FIELD_ANCOUNT) != 0)
394 avahi_interface_send_packet_unicast(i, reply, a, port);
395 avahi_dns_packet_free(reply);
400 static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
408 /* g_message("query"); */
410 g_assert(avahi_record_list_empty(s->record_list));
412 /* Handle the questions */
413 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
415 gboolean unicast_response = FALSE;
417 if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
418 g_warning("Packet too short (1)");
419 avahi_record_list_flush(s->record_list);
423 avahi_packet_scheduler_incoming_query(i->scheduler, key);
424 avahi_server_prepare_matching_responses(s, i, key, unicast_response);
425 avahi_key_unref(key);
428 if (!avahi_record_list_empty(s->record_list))
429 avahi_server_generate_response(s, i, p, a, port, legacy_unicast);
431 /* Known Answer Suppression */
432 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) {
434 gboolean unique = FALSE;
436 if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
437 g_warning("Packet too short (2)");
441 if (handle_conflict(s, i, record, unique, a))
442 avahi_packet_scheduler_incoming_known_answer(i->scheduler, record, a);
444 avahi_record_unref(record);
448 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) {
450 gboolean unique = FALSE;
452 if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
453 g_warning("Packet too short (3)");
457 if (record->key->type != AVAHI_DNS_TYPE_ANY)
458 incoming_probe(s, record, i);
460 avahi_record_unref(record);
464 static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
472 /* g_message("response"); */
474 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
475 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
477 gboolean cache_flush = FALSE;
480 if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
481 g_warning("Packet too short (4)");
485 if (record->key->type != AVAHI_DNS_TYPE_ANY) {
487 g_message("Handling response: %s", txt = avahi_record_to_string(record));
490 if (handle_conflict(s, i, record, cache_flush, a)) {
491 avahi_cache_update(i->cache, record, cache_flush, a);
492 avahi_packet_scheduler_incoming_response(i->scheduler, record, cache_flush);
496 avahi_record_unref(record);
500 static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
510 if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family)) ||
511 !avahi_interface_relevant(i)) {
512 g_warning("Recieved packet from invalid interface.");
516 g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol);
518 if (sa->sa_family == AF_INET6) {
519 static const guint8 ipv4_in_ipv6[] = {
520 0x00, 0x00, 0x00, 0x00,
521 0x00, 0x00, 0x00, 0x00,
522 0xFF, 0xFF, 0xFF, 0xFF };
524 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
526 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0)
530 if (avahi_dns_packet_check_valid(p) < 0) {
531 g_warning("Recieved invalid packet.");
535 port = avahi_port_from_sockaddr(sa);
536 avahi_address_from_sockaddr(sa, &a);
538 if (avahi_dns_packet_is_query(p)) {
539 gboolean legacy_unicast = FALSE;
541 if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
542 g_warning("Invalid query packet.");
546 if (port != AVAHI_MDNS_PORT) {
549 if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
550 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
551 g_warning("Invalid legacy unicast query packet.");
555 legacy_unicast = TRUE;
558 handle_query(s, p, i, &a, port, legacy_unicast);
560 g_message("Handled query");
563 if (port != AVAHI_MDNS_PORT) {
564 g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
569 g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
570 if (!s->ignore_bad_ttl)
574 if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
575 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
576 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
577 g_warning("Invalid response packet.");
581 handle_response(s, p, i, &a);
582 g_message("Handled response");
586 static void work(AvahiServer *s) {
587 struct sockaddr_in6 sa6;
588 struct sockaddr_in sa;
595 if (s->pollfd_ipv4.revents & G_IO_IN) {
596 if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
597 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
598 avahi_dns_packet_free(p);
602 if (s->pollfd_ipv6.revents & G_IO_IN) {
603 if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
604 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
605 avahi_dns_packet_free(p);
610 static gboolean prepare_func(GSource *source, gint *timeout) {
618 static gboolean check_func(GSource *source) {
622 s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
625 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
628 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
632 s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
641 static void add_default_entries(AvahiServer *s) {
642 struct utsname utsname;
650 /* Fill in HINFO rr */
651 r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO);
653 r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
654 r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
655 avahi_server_add(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r);
656 avahi_record_unref(r);
658 /* Add localhost entries */
659 avahi_address_parse("127.0.0.1", AF_INET, &a);
660 avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
662 avahi_address_parse("::1", AF_INET6, &a);
663 avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
666 AvahiServer *avahi_server_new(GMainContext *c) {
669 static GSourceFuncs source_funcs = {
678 s = g_new(AvahiServer, 1);
680 s->ignore_bad_ttl = FALSE;
681 s->need_entry_cleanup = s->need_group_cleanup = FALSE;
683 s->fd_ipv4 = avahi_open_socket_ipv4();
684 s->fd_ipv6 = -1 /* avahi_open_socket_ipv6() */ ;
686 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
687 g_critical("Failed to create IP sockets.\n");
693 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
694 else if (s->fd_ipv6 < 0)
695 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
698 g_main_context_ref(s->context = c);
700 s->context = g_main_context_default();
702 AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries);
703 s->entries_by_key = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
704 AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups);
706 AVAHI_LLIST_HEAD_INIT(AvahiSubscription, s->subscriptions);
707 s->subscription_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
710 s->host_name = avahi_get_host_name();
711 s->host_name[strcspn(s->host_name, ".")] = 0;
713 s->domain = avahi_normalize_name("local.");
715 s->host_name_fqdn = g_strdup_printf("%s.%s", s->host_name, s->domain);
717 s->record_list = avahi_record_list_new();
719 s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */
720 s->monitor = avahi_interface_monitor_new(s);
721 avahi_interface_monitor_sync(s->monitor);
722 add_default_entries(s);
724 /* Prepare IO source registration */
725 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*));
726 *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
728 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
729 s->pollfd_ipv4.fd = s->fd_ipv4;
730 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
731 g_source_add_poll(s->source, &s->pollfd_ipv4);
733 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
734 s->pollfd_ipv6.fd = s->fd_ipv6;
735 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
736 g_source_add_poll(s->source, &s->pollfd_ipv6);
738 g_source_attach(s->source, s->context);
743 void avahi_server_free(AvahiServer* s) {
747 free_entry(s, s->entries);
749 avahi_interface_monitor_free(s->monitor);
752 free_group(s, s->groups);
754 while (s->subscriptions)
755 avahi_subscription_free(s->subscriptions);
756 g_hash_table_destroy(s->subscription_hashtable);
758 g_hash_table_destroy(s->entries_by_key);
760 avahi_time_event_queue_free(s->time_event_queue);
762 avahi_record_list_free(s->record_list);
769 g_free(s->host_name);
771 g_free(s->host_name_fqdn);
773 g_source_destroy(s->source);
774 g_source_unref(s->source);
775 g_main_context_unref(s->context);
780 void avahi_server_add(
785 AvahiEntryFlags flags,
792 g_assert(r->key->type != AVAHI_DNS_TYPE_ANY);
794 e = g_new(AvahiEntry, 1);
796 e->record = avahi_record_ref(r);
798 e->interface = interface;
799 e->protocol = protocol;
803 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements);
805 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
807 /* Insert into hash table indexed by name */
808 t = g_hash_table_lookup(s->entries_by_key, e->record->key);
809 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
810 g_hash_table_replace(s->entries_by_key, e->record->key, t);
812 /* Insert into group list */
814 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
816 avahi_announce_entry(s, e);
818 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) {
819 AvahiEntry **e = (AvahiEntry**) state;
824 *e = g ? g->entries : s->entries;
826 while (*e && (*e)->dead)
827 *e = g ? (*e)->by_group_next : (*e)->entries_next;
832 return avahi_record_ref((*e)->record);
835 void avahi_server_dump(AvahiServer *s, FILE *f) {
840 fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
842 for (e = s->entries; e; e = e->entries_next) {
848 t = avahi_record_to_string(e->record);
849 fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol);
853 avahi_dump_caches(s->monitor, f);
856 void avahi_server_add_ptr(
861 AvahiEntryFlags flags,
869 r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
870 r->data.ptr.name = avahi_normalize_name(dest);
871 avahi_server_add(s, g, interface, protocol, flags, r);
872 avahi_record_unref(r);
875 void avahi_server_add_address(
880 AvahiEntryFlags flags,
888 name = name ? (n = avahi_normalize_name(name)) : s->host_name_fqdn;
890 if (a->family == AF_INET) {
894 r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
895 r->data.a.address = a->data.ipv4;
896 avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
897 avahi_record_unref(r);
899 reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4);
900 avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
907 r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
908 r->data.aaaa.address = a->data.ipv6;
909 avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, r);
910 avahi_record_unref(r);
912 reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
913 avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
916 reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6);
917 avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name);
924 void avahi_server_add_text_strlst(
929 AvahiEntryFlags flags,
931 AvahiStringList *strlst) {
937 r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
938 r->data.txt.string_list = strlst;
939 avahi_server_add(s, g, interface, protocol, flags, r);
940 avahi_record_unref(r);
943 void avahi_server_add_text_va(
948 AvahiEntryFlags flags,
954 avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va));
957 void avahi_server_add_text(
962 AvahiEntryFlags flags,
971 avahi_server_add_text_va(s, g, interface, protocol, flags, name, va);
975 static void escape_service_name(gchar *d, guint size, const gchar *s) {
980 while (*s && size >= 2) {
981 if (*s == '.' || *s == '\\') {
997 void avahi_server_add_service_strlst(
1004 const gchar *domain,
1007 AvahiStringList *strlst) {
1009 gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
1016 escape_service_name(ename, sizeof(ename), name);
1019 while (domain[0] == '.')
1025 host = s->host_name_fqdn;
1027 snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
1028 snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
1030 avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name);
1032 r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
1033 r->data.srv.priority = 0;
1034 r->data.srv.weight = 0;
1035 r->data.srv.port = port;
1036 r->data.srv.name = avahi_normalize_name(host);
1037 avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
1038 avahi_record_unref(r);
1040 avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst);
1042 snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
1043 avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name);
1046 void avahi_server_add_service_va(
1053 const gchar *domain,
1062 avahi_server_add_service_strlst(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va));
1065 void avahi_server_add_service(
1072 const gchar *domain,
1084 avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va);
1088 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
1089 AvahiKey *k = userdata;
1095 avahi_interface_post_query(i, k, FALSE);
1098 void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) {
1102 avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
1105 void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) {
1108 if (g->state == state)
1114 g->callback(g->server, g, state, g->userdata);
1119 AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) {
1124 g = g_new(AvahiEntryGroup, 1);
1126 g->callback = callback;
1127 g->userdata = userdata;
1129 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
1131 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
1133 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g);
1137 void avahi_entry_group_free(AvahiEntryGroup *g) {
1139 g_assert(g->server);
1142 g->server->need_group_cleanup = TRUE;
1145 void avahi_entry_group_commit(AvahiEntryGroup *g) {
1149 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)
1152 avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1153 avahi_announce_group(g->server, g);
1154 avahi_entry_group_check_probed(g, FALSE);
1157 gboolean avahi_entry_commited(AvahiEntry *e) {
1162 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1163 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1166 AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) {
1173 const gchar* avahi_server_get_domain(AvahiServer *s) {
1179 const gchar* avahi_server_get_host_name(AvahiServer *s) {
1182 return s->host_name_fqdn;