+
+DnsAnswer *dns_answer_merge(DnsAnswer *a, DnsAnswer *b) {
+ _cleanup_(dns_answer_unrefp) DnsAnswer *ret = NULL;
+ DnsAnswer *k;
+ unsigned i;
+ int r;
+
+ if (a && (!b || b->n_rrs <= 0))
+ return dns_answer_ref(a);
+ if ((!a || a->n_rrs <= 0) && b)
+ return dns_answer_ref(b);
+
+ ret = dns_answer_new((a ? a->n_rrs : 0) + (b ? b->n_rrs : 0));
+ if (!ret)
+ return NULL;
+
+ if (a) {
+ for (i = 0; i < a->n_rrs; i++) {
+ r = dns_answer_add(ret, a->rrs[i]);
+ if (r < 0)
+ return NULL;
+ }
+ }
+
+ if (b) {
+ for (i = 0; i < b->n_rrs; i++) {
+ r = dns_answer_add(ret, b->rrs[i]);
+ if (r < 0)
+ return NULL;
+ }
+ }
+
+ k = ret;
+ ret = NULL;
+
+ return k;
+}
+
+void dns_answer_order_by_scope(DnsAnswer *a, bool prefer_link_local) {
+ DnsResourceRecord **rrs;
+ unsigned i, start, end;
+ assert(a);
+
+ if (a->n_rrs <= 1)
+ return;
+
+ start = 0;
+ end = a->n_rrs-1;
+
+ /* RFC 4795, Section 2.6 suggests we should order entries
+ * depending on whether the sender is a link-local address. */
+
+ rrs = newa(DnsResourceRecord*, a->n_rrs);
+ for (i = 0; i < a->n_rrs; i++) {
+
+ if (a->rrs[i]->key->class == DNS_CLASS_IN &&
+ ((a->rrs[i]->key->type == DNS_TYPE_A && in_addr_is_link_local(AF_INET, (union in_addr_union*) &a->rrs[i]->a.in_addr) != prefer_link_local) ||
+ (a->rrs[i]->key->type == DNS_TYPE_AAAA && in_addr_is_link_local(AF_INET6, (union in_addr_union*) &a->rrs[i]->aaaa.in6_addr) != prefer_link_local)))
+ /* Order address records that are are not preferred to the end of the array */
+ rrs[end--] = a->rrs[i];
+ else
+ /* Order all other records to the beginning of the array */
+ rrs[start++] = a->rrs[i];
+ }
+
+ assert(start == end+1);
+ memcpy(a->rrs, rrs, sizeof(DnsResourceRecord*) * a->n_rrs);
+}