+
+ /* If the incoming response contained a conflicting record, some
+ records have been scheduling for sending. We need to flush them
+ here. */
+ if (!avahi_record_list_empty(s->record_list))
+ avahi_server_generate_response(s, i, NULL, NULL, 0, FALSE, TRUE);
+}
+
+static AvahiLegacyUnicastReflectSlot* allocate_slot(AvahiServer *s) {
+ guint n, idx = (guint) -1;
+ AvahiLegacyUnicastReflectSlot *slot;
+
+ g_assert(s);
+
+ if (!s->legacy_unicast_reflect_slots)
+ s->legacy_unicast_reflect_slots = g_new0(AvahiLegacyUnicastReflectSlot*, AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS);
+
+ for (n = 0; n < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; n++, s->legacy_unicast_reflect_id++) {
+ idx = s->legacy_unicast_reflect_id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+
+ if (!s->legacy_unicast_reflect_slots[idx])
+ break;
+ }
+
+ if (idx == (guint) -1 || s->legacy_unicast_reflect_slots[idx])
+ return NULL;
+
+ slot = s->legacy_unicast_reflect_slots[idx] = g_new(AvahiLegacyUnicastReflectSlot, 1);
+ slot->id = s->legacy_unicast_reflect_id++;
+ slot->server = s;
+ return slot;
+}
+
+static void deallocate_slot(AvahiServer *s, AvahiLegacyUnicastReflectSlot *slot) {
+ guint idx;
+
+ g_assert(s);
+ g_assert(slot);
+
+ idx = slot->id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+
+ g_assert(s->legacy_unicast_reflect_slots[idx] == slot);
+
+ avahi_time_event_queue_remove(s->time_event_queue, slot->time_event);
+
+ g_free(slot);
+ s->legacy_unicast_reflect_slots[idx] = NULL;
+}
+
+static void free_slots(AvahiServer *s) {
+ guint idx;
+ g_assert(s);
+
+ if (!s->legacy_unicast_reflect_slots)
+ return;
+
+ for (idx = 0; idx < AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS; idx ++)
+ if (s->legacy_unicast_reflect_slots[idx])
+ deallocate_slot(s, s->legacy_unicast_reflect_slots[idx]);
+
+ g_free(s->legacy_unicast_reflect_slots);
+ s->legacy_unicast_reflect_slots = NULL;
+}
+
+static AvahiLegacyUnicastReflectSlot* find_slot(AvahiServer *s, guint16 id) {
+ guint idx;
+
+ g_assert(s);
+
+ if (!s->legacy_unicast_reflect_slots)
+ return NULL;
+
+ idx = id % AVAHI_MAX_LEGACY_UNICAST_REFLECT_SLOTS;
+
+ if (!s->legacy_unicast_reflect_slots[idx] || s->legacy_unicast_reflect_slots[idx]->id != id)
+ return NULL;
+
+ return s->legacy_unicast_reflect_slots[idx];
+}
+
+static void legacy_unicast_reflect_slot_timeout(AvahiTimeEvent *e, void *userdata) {
+ AvahiLegacyUnicastReflectSlot *slot = userdata;
+
+ g_assert(e);
+ g_assert(slot);
+ g_assert(slot->time_event == e);
+
+ deallocate_slot(slot->server, slot);
+}
+
+static void reflect_legacy_unicast_query_packet(AvahiServer *s, AvahiDnsPacket *p, AvahiInterface *i, const AvahiAddress *a, guint16 port) {
+ AvahiLegacyUnicastReflectSlot *slot;
+ AvahiInterface *j;
+
+ g_assert(s);
+ g_assert(p);
+ g_assert(i);
+ g_assert(a);
+ g_assert(port > 0);
+ g_assert(i->protocol == a->family);
+
+ if (!s->config.enable_reflector)
+ return;
+
+/* avahi_log_debug("legacy unicast reflector"); */
+
+ /* Reflecting legacy unicast queries is a little more complicated
+ than reflecting normal queries, since we must route the
+ responses back to the right client. Therefore we must store
+ some information for finding the right client contact data for
+ response packets. In contrast to normal queries legacy
+ unicast query and response packets are reflected untouched and
+ are not reassembled into larger packets */
+
+ if (!(slot = allocate_slot(s))) {
+ /* No slot available, we drop this legacy unicast query */
+ avahi_log_warn("No slot available for legacy unicast reflection, dropping query packet.");
+ return;
+ }
+
+ slot->original_id = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID);
+ slot->address = *a;
+ slot->port = port;
+ slot->interface = i->hardware->index;
+
+ avahi_elapse_time(&slot->elapse_time, 2000, 0);
+ slot->time_event = avahi_time_event_queue_add(s->time_event_queue, &slot->elapse_time, legacy_unicast_reflect_slot_timeout, slot);
+
+ /* Patch the packet with our new locally generatedt id */
+ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->id);
+
+ for (j = s->monitor->interfaces; j; j = j->interface_next)
+ if (avahi_interface_relevant(j) &&
+ j != i &&
+ (s->config.reflect_ipv || j->protocol == i->protocol)) {
+
+ if (j->protocol == AVAHI_PROTO_INET && s->fd_legacy_unicast_ipv4 >= 0) {
+ avahi_send_dns_packet_ipv4(s->fd_legacy_unicast_ipv4, j->hardware->index, p, NULL, 0);
+ } else if (j->protocol == AVAHI_PROTO_INET6 && s->fd_legacy_unicast_ipv6 >= 0)
+ avahi_send_dns_packet_ipv6(s->fd_legacy_unicast_ipv6, j->hardware->index, p, NULL, 0);
+ }
+
+ /* Reset the id */
+ avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_ID, slot->original_id);