/** Extract IPv4 address (A) records from a DNS response packet. */ int dns_ip4_packet(struct dns_result* out, const char* buf, unsigned int len) { if (dns_packet_extract(out, buf, len, DNS_T_A, DNS_C_IN, 0, getit) < 0) return -1; dns_rotateipv4(out->rr.ip4, out->count); return 0; }
void dns_transaction_process_reply(DnsTransaction *t, DnsPacket *p) { usec_t ts; int r; assert(t); assert(p); assert(t->state == DNS_TRANSACTION_PENDING); assert(t->scope); assert(t->scope->manager); /* Note that this call might invalidate the query. Callers * should hence not attempt to access the query or transaction * after calling this function. */ switch (t->scope->protocol) { case DNS_PROTOCOL_LLMNR: assert(t->scope->link); /* For LLMNR we will not accept any packets from other * interfaces */ if (p->ifindex != t->scope->link->ifindex) return; if (p->family != t->scope->family) return; /* Tentative packets are not full responses but still * useful for identifying uniqueness conflicts during * probing. */ if (DNS_PACKET_LLMNR_T(p)) { dns_transaction_tentative(t, p); return; } break; case DNS_PROTOCOL_DNS: break; default: assert_not_reached("Invalid DNS protocol."); } if (t->received != p) { dns_packet_unref(t->received); t->received = dns_packet_ref(p); } if (p->ipproto == IPPROTO_TCP) { if (DNS_PACKET_TC(p)) { /* Truncated via TCP? Somebody must be f*****g with us */ dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); return; } if (DNS_PACKET_ID(p) != t->id) { /* Not the reply to our query? Somebody must be f*****g with us */ dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); return; } } assert_se(sd_event_now(t->scope->manager->event, clock_boottime_or_monotonic(), &ts) >= 0); switch (t->scope->protocol) { case DNS_PROTOCOL_DNS: assert(t->server); dns_server_packet_received(t->server, ts - t->start_usec); break; case DNS_PROTOCOL_LLMNR: case DNS_PROTOCOL_MDNS: dns_scope_packet_received(t->scope, ts - t->start_usec); break; default: break; } if (DNS_PACKET_TC(p)) { /* Response was truncated, let's try again with good old TCP */ r = dns_transaction_open_tcp(t); if (r == -ESRCH) { /* No servers found? Damn! */ dns_transaction_complete(t, DNS_TRANSACTION_NO_SERVERS); return; } if (r < 0) { /* On LLMNR, if we cannot connect to the host, * we immediately give up */ if (t->scope->protocol == DNS_PROTOCOL_LLMNR) { dns_transaction_complete(t, DNS_TRANSACTION_RESOURCES); return; } /* On DNS, couldn't send? Try immediately again, with a new server */ dns_transaction_next_dns_server(t); r = dns_transaction_go(t); if (r < 0) { dns_transaction_complete(t, DNS_TRANSACTION_RESOURCES); return; } return; } } /* Parse and update the cache */ r = dns_packet_extract(p); if (r < 0) { dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); return; } /* Only consider responses with equivalent query section to the request */ if (p->question->n_keys != 1 || dns_resource_key_equal(p->question->keys[0], t->key) <= 0) { dns_transaction_complete(t, DNS_TRANSACTION_INVALID_REPLY); return; } /* According to RFC 4795, section 2.9. only the RRs from the answer section shall be cached */ dns_cache_put(&t->scope->cache, p->question, DNS_PACKET_RCODE(p), p->answer, DNS_PACKET_ANCOUNT(p), 0, p->family, &p->sender); if (DNS_PACKET_RCODE(p) == DNS_RCODE_SUCCESS) dns_transaction_complete(t, DNS_TRANSACTION_SUCCESS); else dns_transaction_complete(t, DNS_TRANSACTION_FAILURE); }