+static void free_entry(AvahiServer*s, AvahiEntry *e) {
+ AvahiEntry *t;
+
+ g_assert(s);
+ g_assert(e);
+
+ avahi_goodbye_entry(s, e, TRUE);
+
+ /* Remove from linked list */
+ AVAHI_LLIST_REMOVE(AvahiEntry, entries, s->entries, e);
+
+ /* Remove from hash table indexed by name */
+ t = g_hash_table_lookup(s->entries_by_key, e->record->key);
+ AVAHI_LLIST_REMOVE(AvahiEntry, by_key, t, e);
+ if (t)
+ g_hash_table_replace(s->entries_by_key, t->record->key, t);
+ else
+ g_hash_table_remove(s->entries_by_key, e->record->key);
+
+ /* Remove from associated group */
+ if (e->group)
+ AVAHI_LLIST_REMOVE(AvahiEntry, by_group, e->group->entries, e);
+
+ avahi_record_unref(e->record);
+ g_free(e);
+}
+
+static void free_group(AvahiServer *s, AvahiEntryGroup *g) {
+ g_assert(s);
+ g_assert(g);
+
+ while (g->entries)
+ free_entry(s, g->entries);
+
+ AVAHI_LLIST_REMOVE(AvahiEntryGroup, groups, s->groups, g);
+ g_free(g);
+}
+
+static void cleanup_dead(AvahiServer *s) {
+ AvahiEntryGroup *g, *ng;
+ AvahiEntry *e, *ne;
+ g_assert(s);
+
+
+ if (s->need_group_cleanup) {
+ for (g = s->groups; g; g = ng) {
+ ng = g->groups_next;
+
+ if (g->dead)
+ free_group(s, g);
+ }
+
+ s->need_group_cleanup = FALSE;
+ }
+
+ if (s->need_entry_cleanup) {
+ for (e = s->entries; e; e = ne) {
+ ne = e->entries_next;
+
+ if (e->dead)
+ free_entry(s, e);
+ }
+
+ s->need_entry_cleanup = FALSE;
+ }
+}
+
+static void handle_query_key(AvahiServer *s, AvahiKey *k, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast, gboolean unicast_response) {
+ AvahiEntry *e;
+ gchar *txt;
+
+ g_assert(s);
+ g_assert(k);
+ g_assert(i);
+ g_assert(a);
+
+ g_message("Handling query: %s", txt = avahi_key_to_string(k));
+ g_free(txt);
+
+ avahi_packet_scheduler_incoming_query(i->scheduler, k);
+
+ if (k->type == AVAHI_DNS_TYPE_ANY) {
+
+ /* Handle ANY query */
+
+ for (e = s->entries; e; e = e->entries_next)
+ if (!e->dead && avahi_key_pattern_match(k, e->record->key) && avahi_entry_registered(s, e, i))
+ avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, FALSE);
+ } else {
+
+ /* Handle all other queries */
+
+ for (e = g_hash_table_lookup(s->entries_by_key, k); e; e = e->by_key_next)
+ if (!e->dead && avahi_entry_registered(s, e, i))
+ avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, FALSE);
+ }
+}
+
+static void withdraw_entry(AvahiServer *s, AvahiEntry *e) {
+ g_assert(s);
+ g_assert(e);
+
+
+ if (e->group) {
+ AvahiEntry *k;
+
+ for (k = e->group->entries; k; k = k->by_group_next) {
+ avahi_goodbye_entry(s, k, FALSE);
+ k->dead = TRUE;
+ }
+
+ avahi_entry_group_change_state(e->group, AVAHI_ENTRY_GROUP_COLLISION);
+ } else {
+ avahi_goodbye_entry(s, e, FALSE);
+ e->dead = TRUE;
+ }
+
+ s->need_entry_cleanup = TRUE;
+}
+
+static void incoming_probe(AvahiServer *s, AvahiRecord *record, AvahiInterface *i) {
+ AvahiEntry *e, *n;
+ gchar *t;
+
+ g_assert(s);
+ g_assert(record);
+ g_assert(i);
+
+ t = avahi_record_to_string(record);
+
+/* g_message("PROBE: [%s]", t); */
+
+ for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
+ n = e->by_key_next;
+
+ if (e->dead || avahi_record_equal_no_ttl(record, e->record))
+ continue;
+
+ if (avahi_entry_registering(s, e, i)) {
+ gint cmp;
+
+ if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) {
+ withdraw_entry(s, e);
+ g_message("Recieved conflicting probe [%s]. Local host lost. Withdrawing.", t);
+ } else if (cmp < 0)
+ g_message("Recieved conflicting probe [%s]. Local host won.", t);
+
+ }
+ }
+
+ g_free(t);
+}
+
+static void handle_query(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port, gboolean legacy_unicast) {
+ guint n;
+
+ g_assert(s);
+ g_assert(p);
+ g_assert(i);
+ g_assert(a);
+
+ /* Handle the questions */
+ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n --) {
+ AvahiKey *key;
+ gboolean unicast_response = FALSE;
+
+ if (!(key = avahi_dns_packet_consume_key(p, &unicast_response))) {
+ g_warning("Packet too short (1)");
+ return;
+ }
+
+ handle_query_key(s, key, i, a, port, legacy_unicast, unicast_response);
+ avahi_key_unref(key);
+ }
+
+ /* Known Answer Suppression */
+ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT); n > 0; n --) {
+ AvahiRecord *record;
+ gboolean unique = FALSE;
+
+ if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
+ g_warning("Packet too short (2)");
+ return;
+ }
+
+ avahi_packet_scheduler_incoming_known_answer(i->scheduler, record, a);
+ avahi_record_unref(record);
+ }
+
+ /* Probe record */
+ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT); n > 0; n --) {
+ AvahiRecord *record;
+ gboolean unique = FALSE;
+
+ if (!(record = avahi_dns_packet_consume_record(p, &unique))) {
+ g_warning("Packet too short (3)");
+ return;
+ }
+
+ if (record->key->type != AVAHI_DNS_TYPE_ANY)
+ incoming_probe(s, record, i);
+
+ avahi_record_unref(record);
+ }
+}
+
+static gboolean handle_conflict(AvahiServer *s, AvahiInterface *i, AvahiRecord *record, gboolean unique, const AvahiAddress *a) {
+ gboolean valid = TRUE;
+ AvahiEntry *e, *n;
+ gchar *t;
+
+ g_assert(s);
+ g_assert(i);
+ g_assert(record);
+
+ t = avahi_record_to_string(record);
+
+/* g_message("CHECKING FOR CONFLICT: [%s]", t); */
+
+ for (e = g_hash_table_lookup(s->entries_by_key, record->key); e; e = n) {
+ n = e->by_key_next;
+
+ if (e->dead)
+ continue;
+
+ if (avahi_entry_registered(s, e, i)) {
+
+ gboolean equal = avahi_record_equal_no_ttl(record, e->record);
+
+ /* Check whether there is a unique record conflict */
+ if (!equal && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
+ gint cmp;
+
+ /* The lexicographically later data wins. */
+ if ((cmp = avahi_record_lexicographical_compare(record, e->record)) > 0) {
+ g_message("Recieved conflicting record [%s]. Local host lost. Withdrawing.", t);
+ withdraw_entry(s, e);
+ } else if (cmp < 0) {
+ /* Tell the other host that our entry is lexicographically later */
+
+ g_message("Recieved conflicting record [%s]. Local host won. Refreshing.", t);
+
+ valid = FALSE;
+ avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
+ }
+
+ /* Check wheter there is a TTL conflict */
+ } else if (equal && record->ttl <= e->record->ttl/2) {
+ /* Correct the TTL */
+ valid = FALSE;
+ avahi_interface_post_response(i, a, e->record, e->flags & AVAHI_ENTRY_UNIQUE, TRUE);
+ g_message("Recieved record with bad TTL [%s]. Refreshing.", t);
+ }
+
+ } else if (avahi_entry_registering(s, e, i)) {
+
+ if (!avahi_record_equal_no_ttl(record, e->record) && ((e->flags & AVAHI_ENTRY_UNIQUE) || unique)) {
+
+ /* We are currently registering a matching record, but
+ * someone else already claimed it, so let's
+ * withdraw */
+
+ g_message("Recieved conflicting record [%s] with local record to be. Withdrawing.", t);
+ withdraw_entry(s, e);
+ }
+ }
+ }
+
+ g_free(t);
+
+ return valid;
+}
+
+static void handle_response(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a) {
+ guint n;
+
+ g_assert(s);
+ g_assert(p);
+ g_assert(i);
+ g_assert(a);
+
+ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) +
+ avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT); n > 0; n--) {
+ AvahiRecord *record;
+ gboolean cache_flush = FALSE;
+ gchar *txt;
+
+ if (!(record = avahi_dns_packet_consume_record(p, &cache_flush))) {
+ g_warning("Packet too short (4)");
+ return;
+ }
+
+ if (record->key->type != AVAHI_DNS_TYPE_ANY) {
+
+ g_message("Handling response: %s", txt = avahi_record_to_string(record));
+ g_free(txt);
+
+ if (handle_conflict(s, i, record, cache_flush, a)) {
+ avahi_cache_update(i->cache, record, cache_flush, a);
+ avahi_packet_scheduler_incoming_response(i->scheduler, record);
+ }
+ }
+
+ avahi_record_unref(record);
+ }
+}
+
+static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, struct sockaddr *sa, gint iface, gint ttl) {
+ AvahiInterface *i;
+ AvahiAddress a;
+ guint16 port;
+
+ g_assert(s);
+ g_assert(p);
+ g_assert(sa);
+ g_assert(iface > 0);
+
+ if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, sa->sa_family))) {
+ g_warning("Recieved packet from invalid interface.");
+ return;
+ }
+
+ g_message("new packet recieved on interface '%s.%i'.", i->hardware->name, i->protocol);
+
+ if (sa->sa_family == AF_INET6) {
+ static const guint8 ipv4_in_ipv6[] = {
+ 0x00, 0x00, 0x00, 0x00,
+ 0x00, 0x00, 0x00, 0x00,
+ 0xFF, 0xFF, 0xFF, 0xFF };
+
+ /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
+
+ if (memcmp(((struct sockaddr_in6*) sa)->sin6_addr.s6_addr, ipv4_in_ipv6, sizeof(ipv4_in_ipv6)) == 0)
+ return;
+ }
+
+ if (avahi_dns_packet_check_valid(p) < 0) {
+ g_warning("Recieved invalid packet.");
+ return;
+ }
+
+ port = avahi_port_from_sockaddr(sa);
+ avahi_address_from_sockaddr(sa, &a);
+
+ if (avahi_dns_packet_is_query(p)) {
+ gboolean legacy_unicast = FALSE;
+
+ if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) == 0 ||
+ avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
+ g_warning("Invalid query packet.");
+ return;
+ }
+
+ if (port != AVAHI_MDNS_PORT) {
+ /* Legacy Unicast */
+
+ if ((avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) != 0 ||
+ avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0)) {
+ g_warning("Invalid legacy unicast query packet.");
+ return;
+ }
+
+ legacy_unicast = TRUE;
+ }
+
+ handle_query(s, p, i, &a, port, legacy_unicast);
+
+ g_message("Handled query");
+ } else {
+
+ if (port != AVAHI_MDNS_PORT) {
+ g_warning("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
+ return;
+ }
+
+ if (ttl != 255) {
+ g_warning("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
+ if (!s->ignore_bad_ttl)
+ return;
+ }
+
+ if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT) != 0 ||
+ avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ANCOUNT) == 0 ||
+ avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_NSCOUNT) != 0) {
+ g_warning("Invalid response packet.");
+ return;
+ }
+
+ handle_response(s, p, i, &a);
+ g_message("Handled response");
+ }
+}
+
+static void work(AvahiServer *s) {
+ struct sockaddr_in6 sa6;
+ struct sockaddr_in sa;
+ AvahiDnsPacket *p;
+ gint iface = -1;
+ guint8 ttl;
+
+ g_assert(s);
+
+ if (s->pollfd_ipv4.revents & G_IO_IN) {
+ if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &iface, &ttl))) {
+ dispatch_packet(s, p, (struct sockaddr*) &sa, iface, ttl);
+ avahi_dns_packet_free(p);
+ }
+ }
+
+ if (s->pollfd_ipv6.revents & G_IO_IN) {
+ if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &iface, &ttl))) {
+ dispatch_packet(s, p, (struct sockaddr*) &sa6, iface, ttl);
+ avahi_dns_packet_free(p);
+ }
+ }
+}
+
+static gboolean prepare_func(GSource *source, gint *timeout) {
+ g_assert(source);
+ g_assert(timeout);
+
+ *timeout = -1;
+ return FALSE;
+}
+
+static gboolean check_func(GSource *source) {
+ AvahiServer* s;
+ g_assert(source);
+
+ s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
+ g_assert(s);
+
+ return (s->pollfd_ipv4.revents | s->pollfd_ipv6.revents) & (G_IO_IN | G_IO_HUP | G_IO_ERR);
+}
+
+static gboolean dispatch_func(GSource *source, GSourceFunc callback, gpointer user_data) {
+ AvahiServer* s;
+ g_assert(source);
+
+ s = *((AvahiServer**) (((guint8*) source) + sizeof(GSource)));
+ g_assert(s);
+
+ work(s);
+ cleanup_dead(s);
+
+ return TRUE;
+}
+
+static void add_default_entries(AvahiServer *s) {