+/* avahi_log_debug("new packet received on interface '%s.%i'.", i->hardware->name, i->protocol); */
+
+ port = avahi_port_from_sockaddr(sa);
+ avahi_address_from_sockaddr(sa, &a);
+
+ if (avahi_address_is_ipv4_in_ipv6(&a))
+ /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
+ return;
+
+ if (originates_from_local_legacy_unicast_socket(s, sa))
+ /* This originates from our local reflector, so let's ignore it */
+ return;
+
+ /* We don't want to reflect local traffic, so we check if this packet is generated locally. */
+ if (s->config.enable_reflector)
+ from_local_iface = originates_from_local_iface(s, iface, &a, port);
+
+ if (avahi_dns_packet_check_valid_multicast(p) < 0) {
+ avahi_log_warn("Recieved invalid packet.");
+ return;
+ }
+
+ if (avahi_dns_packet_is_query(p)) {
+ int legacy_unicast = 0;
+
+ if (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ARCOUNT) != 0) {
+ avahi_log_warn("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)) {
+ avahi_log_warn("Invalid legacy unicast query packet.");
+ return;
+ }
+
+ legacy_unicast = 1;
+ }
+
+ if (legacy_unicast)
+ reflect_legacy_unicast_query_packet(s, p, i, &a, port);
+
+ handle_query_packet(s, p, i, &a, port, legacy_unicast, from_local_iface);
+
+/* avahi_log_debug("Handled query"); */
+ } else {
+ if (port != AVAHI_MDNS_PORT) {
+ avahi_log_warn("Recieved repsonse with invalid source port %u on interface '%s.%i'", port, i->hardware->name, i->protocol);
+ return;
+ }
+
+ if (ttl != 255 && s->config.check_response_ttl) {
+ avahi_log_warn("Recieved response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
+ return;
+ }
+
+ if (!is_mdns_mcast_address(dest) &&
+ !avahi_interface_address_on_link(i, &a)) {
+ avahi_log_warn("Received non-local response on interface '%s.%i'.", i->hardware->name, i->protocol);
+ 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) {
+ avahi_log_warn("Invalid response packet.");
+ return;
+ }
+
+ handle_response_packet(s, p, i, &a, from_local_iface);
+/* avahi_log_debug("Handled response"); */
+ }
+}
+
+static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p, const struct sockaddr *sa, AvahiIfIndex iface) {
+ AvahiInterface *i, *j;
+ AvahiAddress a;
+ AvahiLegacyUnicastReflectSlot *slot;
+
+ assert(s);
+ assert(p);
+ assert(sa);
+ assert(iface > 0);
+
+ if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, avahi_af_to_proto(sa->sa_family))) ||
+ !avahi_interface_is_relevant(i)) {
+ avahi_log_warn("Recieved packet from invalid interface.");
+ return;
+ }
+
+/* avahi_log_debug("new legacy unicast packet received on interface '%s.%i'.", i->hardware->name, i->protocol); */
+
+ avahi_address_from_sockaddr(sa, &a);
+
+ if (avahi_address_is_ipv4_in_ipv6(&a))
+ /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
+ return;
+
+ if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
+ avahi_log_warn("Recieved invalid packet.");
+ return;
+ }
+
+ if (!(slot = find_slot(s, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID)))) {
+ avahi_log_warn("Recieved legacy unicast response with unknown id");
+ return;
+ }
+
+ if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.proto)) ||
+ !avahi_interface_is_relevant(j))
+ return;
+
+ /* Patch the original ID into this response */
+ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id);
+
+ /* Forward the response to the correct client */
+ avahi_interface_send_packet_unicast(j, p, &slot->address, slot->port);
+
+ /* Undo changes to packet */
+ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id);
+}
+
+static void cleanup_dead(AvahiServer *s) {
+ assert(s);
+
+ avahi_cleanup_dead_entries(s);
+ avahi_browser_cleanup(s);
+}
+
+static void socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) {
+ AvahiServer *s = userdata;
+ AvahiAddress dest;
+ AvahiDnsPacket *p;
+ AvahiIfIndex iface;
+ uint8_t ttl;
+ struct sockaddr_in sa;
+ struct sockaddr_in6 sa6;
+
+ assert(w);
+ assert(fd >= 0);
+
+ if (events & AVAHI_WATCH_IN) {
+
+ if (fd == s->fd_ipv4) {
+ dest.proto = AVAHI_PROTO_INET;
+ if ((p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) {
+ dispatch_packet(s, p, (struct sockaddr*) &sa, &dest, iface, ttl);
+ avahi_dns_packet_free(p);
+ }
+ } else if (fd == s->fd_ipv6) {
+ dest.proto = AVAHI_PROTO_INET6;
+
+ if ((p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) {
+ dispatch_packet(s, p, (struct sockaddr*) &sa6, &dest, iface, ttl);
+ avahi_dns_packet_free(p);
+ }
+ } else if (fd == s->fd_legacy_unicast_ipv4) {
+ dest.proto = AVAHI_PROTO_INET;
+
+ if ((p = avahi_recv_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, &sa, &dest.data.ipv4, &iface, &ttl))) {
+ dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa, iface);
+ avahi_dns_packet_free(p);
+ }
+ } else if (fd == s->fd_legacy_unicast_ipv6) {
+ dest.proto = AVAHI_PROTO_INET6;
+
+ if ((p = avahi_recv_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, &sa6, &dest.data.ipv6, &iface, &ttl))) {
+ dispatch_legacy_unicast_packet(s, p, (struct sockaddr*) &sa6, iface);
+ avahi_dns_packet_free(p);
+ }
+ }
+
+ cleanup_dead(s);
+ } else
+ abort();
+}
+
+static void server_set_state(AvahiServer *s, AvahiServerState state) {
+ assert(s);
+
+ if (s->state == state)
+ return;
+
+ s->state = state;
+
+ if (s->callback)
+ s->callback(s, state, s->userdata);
+}
+
+static void withdraw_host_rrs(AvahiServer *s) {
+ assert(s);
+
+ if (s->hinfo_entry_group)
+ avahi_s_entry_group_reset(s->hinfo_entry_group);
+
+ if (s->browse_domain_entry_group)
+ avahi_s_entry_group_reset(s->browse_domain_entry_group);
+
+ avahi_interface_monitor_update_rrs(s->monitor, 1);
+ s->n_host_rr_pending = 0;
+}
+
+void avahi_server_decrease_host_rr_pending(AvahiServer *s) {
+ assert(s);
+
+ assert(s->n_host_rr_pending > 0);
+
+ if (--s->n_host_rr_pending == 0)
+ server_set_state(s, AVAHI_SERVER_RUNNING);
+}
+
+void avahi_host_rr_entry_group_callback(AvahiServer *s, AvahiSEntryGroup *g, AvahiEntryGroupState state, void *userdata) {
+ assert(s);
+ assert(g);
+
+ if (state == AVAHI_ENTRY_GROUP_REGISTERING &&
+ s->state == AVAHI_SERVER_REGISTERING)
+ s->n_host_rr_pending ++;
+