+static int is_mdns_mcast_address(const AvahiAddress *a) {
+ AvahiAddress b;
+ assert(a);
+
+ avahi_address_parse(a->proto == AVAHI_PROTO_INET ? AVAHI_IPV4_MCAST_GROUP : AVAHI_IPV6_MCAST_GROUP, a->proto, &b);
+ return avahi_address_cmp(a, &b) == 0;
+}
+
+static int originates_from_local_iface(AvahiServer *s, AvahiIfIndex iface, const AvahiAddress *a, uint16_t port) {
+ assert(s);
+ assert(iface != AVAHI_IF_UNSPEC);
+ assert(a);
+
+ /* If it isn't the MDNS port it can't be generated by us */
+ if (port != AVAHI_MDNS_PORT)
+ return 0;
+
+ return avahi_interface_has_address(s->monitor, iface, a);
+}
+
+static void dispatch_packet(AvahiServer *s, AvahiDnsPacket *p, const AvahiAddress *src_address, uint16_t port, const AvahiAddress *dst_address, AvahiIfIndex iface, int ttl) {
+ AvahiInterface *i;
+ int from_local_iface = 0;
+
+ assert(s);
+ assert(p);
+ assert(src_address);
+ assert(dst_address);
+ assert(iface > 0);
+ assert(src_address->proto == dst_address->proto);
+
+ if (!(i = avahi_interface_monitor_get_interface(s->monitor, iface, src_address->proto)) ||
+ !i->announcing) {
+ avahi_log_warn("Received packet from invalid interface.");
+ return;
+ }
+
+ if (avahi_address_is_ipv4_in_ipv6(src_address))
+ /* This is an IPv4 address encapsulated in IPv6, so let's ignore it. */
+ return;
+
+ if (originates_from_local_legacy_unicast_socket(s, src_address, port))
+ /* 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, src_address, port);
+
+ if (avahi_dns_packet_check_valid_multicast(p) < 0) {
+ avahi_log_warn("Received 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, src_address, port);
+
+ handle_query_packet(s, p, i, src_address, port, legacy_unicast, from_local_iface);
+
+ } else {
+ if (port != AVAHI_MDNS_PORT) {
+ avahi_log_warn("Received response 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("Received response with invalid TTL %u on interface '%s.%i'.", ttl, i->hardware->name, i->protocol);
+ return;
+ }
+
+ if (!is_mdns_mcast_address(dst_address) &&
+ !avahi_interface_address_on_link(i, src_address)) {
+ 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, src_address, from_local_iface);
+ }
+}
+
+static void dispatch_legacy_unicast_packet(AvahiServer *s, AvahiDnsPacket *p) {
+ AvahiInterface *j;
+ AvahiLegacyUnicastReflectSlot *slot;
+
+ assert(s);
+ assert(p);
+
+ if (avahi_dns_packet_check_valid(p) < 0 || avahi_dns_packet_is_query(p)) {
+ avahi_log_warn("Received invalid packet.");
+ return;
+ }
+
+ if (!(slot = find_slot(s, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID)))) {
+ avahi_log_warn("Received legacy unicast response with unknown id");
+ return;
+ }
+
+ if (!(j = avahi_interface_monitor_get_interface(s->monitor, slot->interface, slot->address.proto)) ||
+ !j->announcing)
+ 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 mcast_socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) {
+ AvahiServer *s = userdata;
+ AvahiAddress dest, src;
+ AvahiDnsPacket *p = NULL;
+ AvahiIfIndex iface;
+ uint16_t port;
+ uint8_t ttl;
+
+ assert(w);
+ assert(fd >= 0);
+ assert(events & AVAHI_WATCH_IN);
+
+ if (fd == s->fd_ipv4) {
+ dest.proto = src.proto = AVAHI_PROTO_INET;
+ p = avahi_recv_dns_packet_ipv4(s->fd_ipv4, &src.data.ipv4, &port, &dest.data.ipv4, &iface, &ttl);
+ } else {
+ assert(fd == s->fd_ipv6);
+ dest.proto = src.proto = AVAHI_PROTO_INET6;
+ p = avahi_recv_dns_packet_ipv6(s->fd_ipv6, &src.data.ipv6, &port, &dest.data.ipv6, &iface, &ttl);
+ }
+
+ if (p) {
+ if (iface == AVAHI_IF_UNSPEC)
+ iface = avahi_find_interface_for_address(s->monitor, &dest);
+
+ if (iface != AVAHI_IF_UNSPEC)
+ dispatch_packet(s, p, &src, port, &dest, iface, ttl);
+ else
+ avahi_log_error("Incoming packet recieved on address that isn't local.");
+
+ avahi_dns_packet_free(p);
+
+ cleanup_dead(s);
+ }
+}
+
+static void legacy_unicast_socket_event(AvahiWatch *w, int fd, AvahiWatchEvent events, void *userdata) {
+ AvahiServer *s = userdata;
+ AvahiDnsPacket *p = NULL;
+
+ assert(w);
+ assert(fd >= 0);
+ assert(events & AVAHI_WATCH_IN);
+
+ if (fd == s->fd_legacy_unicast_ipv4)
+ p = avahi_recv_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, NULL, NULL, NULL, NULL, NULL);
+ else {
+ assert(fd == s->fd_legacy_unicast_ipv6);
+ p = avahi_recv_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, NULL, NULL, NULL, NULL, NULL);
+ }
+
+ if (p) {
+ dispatch_legacy_unicast_packet(s, p);
+ avahi_dns_packet_free(p);
+
+ cleanup_dead(s);
+ }
+}
+
+static void server_set_state(AvahiServer *s, AvahiServerState state) {
+ assert(s);
+
+ if (s->state == state)
+ return;
+
+ s->state = state;
+
+ avahi_interface_monitor_update_rrs(s->monitor, 0);
+
+ 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);