+AvahiDnsPacket* avahi_dns_packet_new_reply(AvahiDnsPacket* p, unsigned mtu, int copy_queries, int aa) {
+ AvahiDnsPacket *r;
+ assert(p);
+
+ if (!(r = avahi_dns_packet_new_response(mtu, aa)))
+ return NULL;
+
+ if (copy_queries) {
+ unsigned saved_rindex;
+ uint32_t n;
+
+ saved_rindex = p->rindex;
+ p->rindex = AVAHI_DNS_PACKET_HEADER_SIZE;
+
+ for (n = avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT); n > 0; n--) {
+ AvahiKey *k;
+ int unicast_response;
+
+ if ((k = avahi_dns_packet_consume_key(p, &unicast_response))) {
+ avahi_dns_packet_append_key(r, k, unicast_response);
+ avahi_key_unref(k);
+ }
+ }
+
+ p->rindex = saved_rindex;
+
+ avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_QDCOUNT, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_QDCOUNT));
+ }
+
+ avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_ID, avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_ID));
+
+ avahi_dns_packet_set_field(r, AVAHI_DNS_FIELD_FLAGS,
+ (avahi_dns_packet_get_field(r, AVAHI_DNS_FIELD_FLAGS) & ~AVAHI_DNS_FLAG_OPCODE) |
+ (avahi_dns_packet_get_field(p, AVAHI_DNS_FIELD_FLAGS) & AVAHI_DNS_FLAG_OPCODE));
+
+ return r;
+}
+
+