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 send_unicast_response_packet(AvahiServer *s, AvahiInterface *i, const AvahiAddress *a, guint16 port) {
109 g_assert(s->unicast_packet);
111 if (avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT) != 0)
112 avahi_interface_send_packet_unicast(i, s->unicast_packet, a, port);
114 avahi_dns_packet_free(s->unicast_packet);
115 s->unicast_packet = NULL;
118 static void post_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, AvahiRecord *r, gboolean flush_cache, gboolean legacy_unicast, gboolean unicast_response) {
124 if (legacy_unicast) {
126 /* Respond with a legacy unicast packet */
128 if (!(s->unicast_packet))
129 s->unicast_packet = avahi_dns_packet_new_reply(p, 512 /* unicast DNS maximum packet size is 512 */ , TRUE, TRUE);
131 if (avahi_dns_packet_append_record(s->unicast_packet, r, FALSE, 10))
133 /* Increment the ANCOUNT field */
135 avahi_dns_packet_set_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT,
136 avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT)+1);
138 /* If there's no space left for this response we simply don't send it */
142 if (!avahi_interface_post_response(i, a, r, flush_cache, FALSE) && unicast_response) {
144 /* Due to some reasons the record has not been scheduled.
145 * The client requested an unicast response in that
146 * case. Therefore we prepare such a response */
150 if (!(s->unicast_packet))
151 s->unicast_packet = avahi_dns_packet_new_reply(p, i->hardware->mtu, FALSE, FALSE);
153 if (avahi_dns_packet_append_record(s->unicast_packet, r, flush_cache, 0)) {
155 /* Appending this record succeeded, so incremeant
156 * the specific header field, and return to the caller */
158 avahi_dns_packet_set_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT,
159 avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT)+1);
164 if (avahi_dns_packet_get_field(s->unicast_packet, AVAHI_DNS_FIELD_ANCOUNT) == 0) {
165 g_warning("Record too large, doesn't fit in any packet!");
169 /* Appending the record didn't succeeed, so let's send this packet, and create a new one */
171 send_unicast_response_packet(s, i, a, port);
173 avahi_dns_packet_free(s->unicast_packet);
174 s->unicast_packet = NULL;
181 static void handle_query_key(AvahiServer *s, AvahiDnsPacket *p, AvahiKey *k, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean unicast_response) {
190 g_message("Handling query: %s", txt = avahi_key_to_string(k));
193 avahi_packet_scheduler_incoming_query(i->scheduler, k);
195 if (k->type == AVAHI_DNS_TYPE_ANY) {
197 /* Handle ANY query */
199 for (e = s->entries; e; e = e->entries_next)
200 if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_registered(s, e, i))
201 post_response(s, p, i, a, port, e->record, e->flags & AVAHI_ENTRY_UNIQUE, legacy_unicast, unicast_response);
205 /* Handle all other queries */
207 for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next)
208 if (!e->dead && avahi_entry_registered(s, e, i))
209 post_response(s, p, i, a, port, e->record, e->flags & AVAHI_ENTRY_UNIQUE, legacy_unicast, unicast_response);
213 static void withdraw_entry(AvahiServer *s, AvahiEntry *e) {
221 for (k = e->group->entries; k; k = k->by_group_next) {
222 avahi_goodbye_entry(s, k, FALSE);
226 avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
228 avahi_goodbye_entry(s, e, FALSE);
232 s->need_entry_cleanup = TRUE;
235 static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) {
243 t = avahi_record_to_string(record);
245 /* g_message("PROBE: [%s]", t); */
247 for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
250 if (e->dead || avahi_record_equal_no_ttl(record, e->record))
253 if (avahi_entry_registering(s, e, i)) {
256 if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) {
257 withdraw_entry(s, e);
258 g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
260 g_message("Recieved conflicting probe [%s]. Local host won.", t);
268 static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
276 g_assert(!s->unicast_packet);
278 /* Handle the questions */
279 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
281 gboolean unicast_response = FALSE;
283 if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
284 g_warning("Packet too short (1)");
288 handle_query_key(s, p, key, i, a, port, legacy_unicast, unicast_response);
289 avahi_key_unref(key);
292 /* Known Answer Suppression */
293 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) {
295 gboolean unique = FALSE;
297 if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
298 g_warning("Packet too short (2)");
302 avahi_packet_scheduler_incoming_known_answer(i->scheduler, record, a);
303 avahi_record_unref(record);
307 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) {
309 gboolean unique = FALSE;
311 if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
312 g_warning("Packet too short (3)");
316 if (record->key->type != AVAHI_DNS_TYPE_ANY)
317 incoming_probe(s, record, i);
319 avahi_record_unref(record);
322 if (s->unicast_packet)
323 send_unicast_response_packet(s, i, a, port);
326 static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) {
327 gboolean valid = TRUE;
335 t = avahi_record_to_string(record);
337 /* g_message("CHECKING FOR CONFLICT: [%s]", t); */
339 for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
345 if (avahi_entry_registered(s, e, i)) {
347 gboolean equal = avahi_record_equal_no_ttl(record, e->record);
349 /* Check whether there is a unique record conflict */
350 if (!equal && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
353 /* The lexicographically later data wins. */
354 if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) {
355 g_message("Recieved conflicting record [%s]. Local host lost. Withdrawing.", t);
356 withdraw_entry(s, e);
357 } else if (cmp < 0) {
358 /* Tell the other host that our entry is lexicographically later */
360 g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t);
363 avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
366 /* Check wheter there is a TTL conflict */
367 } else if (equal && record->ttl <= e->record->ttl/2) {
368 /* Correct the TTL */
370 avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
371 g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
374 } else if (avahi_entry_registering(s, e, i)) {
376 if (!avahi_record_equal_no_ttl(record, e->record) && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
378 /* We are currently registering a matching record, but
379 * someone else already claimed it, so let's
382 g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
383 withdraw_entry(s, e);
393 static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
401 for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
402 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
404 gboolean cache_flush = FALSE;
407 if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
408 g_warning("Packet too short (4)");
412 if (record->key->type != AVAHI_DNS_TYPE_ANY) {
414 g_message("Handling response: %s", txt = avahi_record_to_string(record));
417 if (handle_conflict(s, i, record, cache_flush, a)) {
418 avahi_cache_update(i->cache, record, cache_flush, a);
419 avahi_packet_scheduler_incoming_response(i->scheduler, record);
423 avahi_record_unref(record);
427 static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
437 if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
438 g_warning("Recieved packet from invalid interface.");
442 g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol);
444 if (sa->sa_family == AF_INET6) {
445 static const guint8 ipv4_in_ipv6[] = {
446 0x00, 0x00, 0x00, 0x00,
447 0x00, 0x00, 0x00, 0x00,
448 0xFF, 0xFF, 0xFF, 0xFF };
450 /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
452 if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0)
456 if (avahi_dns_packet_check_valid(p) < 0) {
457 g_warning("Recieved invalid packet.");
461 port = avahi_port_from_sockaddr(sa);
462 avahi_address_from_sockaddr(sa, &a);
464 if (avahi_dns_packet_is_query(p)) {
465 gboolean legacy_unicast = FALSE;
467 if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) == 0 ||
468 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
469 g_warning("Invalid query packet.");
473 if (port != AVAHI_MDNS_PORT) {
476 if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
477 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
478 g_warning("Invalid legacy unicast query packet.");
482 legacy_unicast = TRUE;
485 handle_query(s, p, i, &a, port, legacy_unicast);
487 g_message("Handled query");
490 if (port != AVAHI_MDNS_PORT) {
491 g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
496 g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
497 if (!s->ignore_bad_ttl)
501 if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
502 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
503 avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
504 g_warning("Invalid response packet.");
508 handle_response(s, p, i, &a);
509 g_message("Handled response");
513 static void work(AvahiServer *s) {
514 struct sockaddr_in6 sa6;
515 struct sockaddr_in sa;
522 if (s->pollfd_ipv4.revents & G_IO_IN) {
523 if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
524 dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
525 avahi_dns_packet_free(p);
529 if (s->pollfd_ipv6.revents & G_IO_IN) {
530 if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
531 dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
532 avahi_dns_packet_free(p);
537 static gboolean prepare_func(GSource *source, gint *timeout) {
545 static gboolean check_func(GSource *source) {
549 s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
552 return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
555 static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
559 s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
568 static void add_default_entries(AvahiServer *s) {
569 struct utsname utsname;
575 /* Fill in HINFO rr */
576 r = avahi_record_new_full(s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO);
578 r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine));
579 r->data.hinfo.os = g_strdup(g_strup(utsname.sysname));
580 avahi_server_add(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE, r);
581 avahi_record_unref(r);
583 /* Add localhost entries */
584 avahi_address_parse("127.0.0.1", AF_INET, &a);
585 avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "localhost", &a);
587 avahi_address_parse("::1", AF_INET6, &a);
588 avahi_server_add_address(s, NULL, 0, AF_UNSPEC, AVAHI_ENTRY_UNIQUE|AVAHI_ENTRY_NOPROBE|AVAHI_ENTRY_NOANNOUNCE, "ip6-localhost", &a);
591 AvahiServer *avahi_server_new(GMainContext *c) {
595 static GSourceFuncs source_funcs = {
604 s = g_new(AvahiServer, 1);
606 s->ignore_bad_ttl = FALSE;
607 s->need_entry_cleanup = s->need_group_cleanup = FALSE;
609 s->fd_ipv4 = avahi_open_socket_ipv4();
610 s->fd_ipv6 = avahi_open_socket_ipv6();
612 if (s->fd_ipv6 < 0 && s->fd_ipv4 < 0) {
613 g_critical("Failed to create IP sockets.\n");
619 g_message("Failed to create IPv4 socket, proceeding in IPv6 only mode");
620 else if (s->fd_ipv6 < 0)
621 g_message("Failed to create IPv6 socket, proceeding in IPv4 only mode");
624 g_main_context_ref(s->context = c);
626 s->context = g_main_context_default();
628 AVAHI_LLIST_HEAD_INIT(AvahiEntry, s->entries);
629 s->entries_by_key = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
630 AVAHI_LLIST_HEAD_INIT(AvahiGroup, s->groups);
632 AVAHI_LLIST_HEAD_INIT(AvahiSubscription, s->subscriptions);
633 s->subscription_hashtable = g_hash_table_new((GHashFunc) avahi_key_hash, (GEqualFunc) avahi_key_equal);
636 hn = avahi_get_host_name();
637 hn[strcspn(hn, ".")] = 0;
639 s->hostname = g_strdup_printf("%s.local.", hn);
642 s->unicast_packet = NULL;
644 s->time_event_queue = avahi_time_event_queue_new(s->context, G_PRIORITY_DEFAULT+10); /* Slightly less priority than the FDs */
645 s->monitor = avahi_interface_monitor_new(s);
646 avahi_interface_monitor_sync(s->monitor);
647 add_default_entries(s);
649 /* Prepare IO source registration */
650 s->source = g_source_new(&source_funcs, sizeof(GSource) + sizeof(AvahiServer*));
651 *((AvahiServer**) (((guint8*) s->source) + sizeof(GSource))) = s;
653 memset(&s->pollfd_ipv4, 0, sizeof(s->pollfd_ipv4));
654 s->pollfd_ipv4.fd = s->fd_ipv4;
655 s->pollfd_ipv4.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
656 g_source_add_poll(s->source, &s->pollfd_ipv4);
658 memset(&s->pollfd_ipv6, 0, sizeof(s->pollfd_ipv6));
659 s->pollfd_ipv6.fd = s->fd_ipv6;
660 s->pollfd_ipv6.events = G_IO_IN|G_IO_ERR|G_IO_HUP;
661 g_source_add_poll(s->source, &s->pollfd_ipv6);
663 g_source_attach(s->source, s->context);
668 void avahi_server_free(AvahiServer* s) {
672 free_entry(s, s->entries);
674 avahi_interface_monitor_free(s->monitor);
677 free_group(s, s->groups);
679 while (s->subscriptions)
680 avahi_subscription_free(s->subscriptions);
681 g_hash_table_destroy(s->subscription_hashtable);
683 g_hash_table_destroy(s->entries_by_key);
685 avahi_time_event_queue_free(s->time_event_queue);
694 g_source_destroy(s->source);
695 g_source_unref(s->source);
696 g_main_context_unref(s->context);
698 if (s->unicast_packet)
699 avahi_dns_packet_free(s->unicast_packet);
704 void avahi_server_add(
709 AvahiEntryFlags flags,
716 g_assert(r->key->type != AVAHI_DNS_TYPE_ANY);
718 e = g_new(AvahiEntry, 1);
720 e->record = avahi_record_ref(r);
722 e->interface = interface;
723 e->protocol = protocol;
727 AVAHI_LLIST_HEAD_INIT(AvahiAnnouncement, e->announcements);
729 AVAHI_LLIST_PREPEND(AvahiEntry, entries, s->entries, e);
731 /* Insert into hash table indexed by name */
732 t = g_hash_table_lookup(s->entries_by_key, e->record->key);
733 AVAHI_LLIST_PREPEND(AvahiEntry, by_key, t, e);
734 g_hash_table_replace(s->entries_by_key, e->record->key, t);
736 /* Insert into group list */
738 AVAHI_LLIST_PREPEND(AvahiEntry, by_group, g->entries, e);
740 avahi_announce_entry(s, e);
742 const AvahiRecord *avahi_server_iterate(AvahiServer *s, AvahiEntryGroup *g, void **state) {
743 AvahiEntry **e = (AvahiEntry**) state;
748 *e = g ? g->entries : s->entries;
750 while (*e && (*e)->dead)
751 *e = g ? (*e)->by_group_next : (*e)->entries_next;
756 return avahi_record_ref((*e)->record);
759 void avahi_server_dump(AvahiServer *s, FILE *f) {
764 fprintf(f, "\n;;; ZONE DUMP FOLLOWS ;;;\n");
766 for (e = s->entries; e; e = e->entries_next) {
772 t = avahi_record_to_string(e->record);
773 fprintf(f, "%s ; iface=%i proto=%i\n", t, e->interface, e->protocol);
777 avahi_dump_caches(s->monitor, f);
780 void avahi_server_add_ptr(
785 AvahiEntryFlags flags,
793 r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR);
794 r->data.ptr.name = avahi_normalize_name(dest);
795 avahi_server_add(s, g, interface, protocol, flags, r);
796 avahi_record_unref(r);
799 void avahi_server_add_address(
804 AvahiEntryFlags flags,
812 name = name ? (n = avahi_normalize_name(name)) : s->hostname;
814 if (a->family == AF_INET) {
818 r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A);
819 r->data.a.address = a->data.ipv4;
820 avahi_server_add(s, g, interface, protocol, flags, r);
821 avahi_record_unref(r);
823 reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4);
825 avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
832 r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA);
833 r->data.aaaa.address = a->data.ipv6;
834 avahi_server_add(s, g, interface, protocol, flags, r);
835 avahi_record_unref(r);
837 reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6);
839 avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
842 reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6);
844 avahi_server_add_ptr(s, g, interface, protocol, flags, reverse, name);
851 void avahi_server_add_text_strlst(
856 AvahiEntryFlags flags,
858 AvahiStringList *strlst) {
864 r = avahi_record_new_full(name ? name : s->hostname, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT);
865 r->data.txt.string_list = strlst;
866 avahi_server_add(s, g, interface, protocol, flags, r);
867 avahi_record_unref(r);
870 void avahi_server_add_text_va(
875 AvahiEntryFlags flags,
881 avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va));
884 void avahi_server_add_text(
889 AvahiEntryFlags flags,
898 avahi_server_add_text_va(s, g, interface, protocol, flags, name, va);
902 static void escape_service_name(gchar *d, guint size, const gchar *s) {
907 while (*s && size >= 2) {
908 if (*s == '.' || *s == '\\') {
924 void avahi_server_add_service_strlst(
934 AvahiStringList *strlst) {
936 gchar ptr_name[256], svc_name[256], ename[64], enum_ptr[256];
943 escape_service_name(ename, sizeof(ename), name);
946 while (domain[0] == '.')
954 snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain);
955 snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain);
957 avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name);
959 r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV);
960 r->data.srv.priority = 0;
961 r->data.srv.weight = 0;
962 r->data.srv.port = port;
963 r->data.srv.name = avahi_normalize_name(host);
964 avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r);
965 avahi_record_unref(r);
967 avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst);
969 snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain);
970 avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name);
973 void avahi_server_add_service_va(
989 avahi_server_add_service(s, g, interface, protocol, type, name, domain, host, port, avahi_string_list_new_va(va));
992 void avahi_server_add_service(
1011 avahi_server_add_service_va(s, g, interface, protocol, type, name, domain, host, port, va);
1015 static void post_query_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
1016 AvahiKey *k = userdata;
1022 avahi_interface_post_query(i, k, FALSE);
1025 void avahi_server_post_query(AvahiServer *s, gint interface, guchar protocol, AvahiKey *key) {
1029 avahi_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key);
1033 AvahiRecord *record;
1034 gboolean flush_cache;
1037 static void post_response_callback(AvahiInterfaceMonitor *m, AvahiInterface *i, gpointer userdata) {
1038 struct tmpdata *tmpdata = userdata;
1044 avahi_interface_post_response(i, NULL, tmpdata->record, tmpdata->flush_cache, FALSE);
1047 void avahi_server_post_response(AvahiServer *s, gint interface, guchar protocol, AvahiRecord *record, gboolean flush_cache) {
1048 struct tmpdata tmpdata;
1053 tmpdata.record = record;
1054 tmpdata.flush_cache = flush_cache;
1056 avahi_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, &tmpdata);
1059 void avahi_entry_group_change_state(AvahiEntryGroup *g, AvahiEntryGroupState state) {
1065 g->callback(g->server, g, state, g->userdata);
1070 AvahiEntryGroup *avahi_entry_group_new(AvahiServer *s, AvahiEntryGroupCallback callback, gpointer userdata) {
1075 g = g_new(AvahiEntryGroup, 1);
1077 g->callback = callback;
1078 g->userdata = userdata;
1080 g->state = AVAHI_ENTRY_GROUP_UNCOMMITED;
1082 AVAHI_LLIST_HEAD_INIT(AvahiEntry, g->entries);
1084 AVAHI_LLIST_PREPEND(AvahiEntryGroup, groups, s->groups, g);
1088 void avahi_entry_group_free(AvahiEntryGroup *g) {
1090 g_assert(g->server);
1093 g->server->need_group_cleanup = TRUE;
1096 void avahi_entry_group_commit(AvahiEntryGroup *g) {
1100 if (g->state != AVAHI_ENTRY_GROUP_UNCOMMITED)
1103 avahi_entry_group_change_state(g, AVAHI_ENTRY_GROUP_REGISTERING);
1104 avahi_announce_group(g->server, g);
1105 avahi_entry_group_check_probed(g, FALSE);
1108 gboolean avahi_entry_commited(AvahiEntry *e) {
1113 e->group->state == AVAHI_ENTRY_GROUP_REGISTERING ||
1114 e->group->state == AVAHI_ENTRY_GROUP_ESTABLISHED;
1117 AvahiEntryGroupState avahi_entry_group_get_state(AvahiEntryGroup *g) {