resolved-dns-packet.h revision b6c5d46b23a28b5b03601ee1e8162b1bc7c7be25
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#pragma once
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering/***
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering This file is part of systemd.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Copyright 2014 Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is free software; you can redistribute it and/or modify it
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering under the terms of the GNU Lesser General Public License as published by
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering (at your option) any later version.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering systemd is distributed in the hope that it will be useful, but
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Lesser General Public License for more details.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering You should have received a copy of the GNU Lesser General Public License
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering ***/
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
a0166609f782da91710dea9183d1bf138538db37Tom Gundersen#include <netinet/ip.h>
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include <netinet/udp.h>
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#include "hashmap.h"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering#include "in-addr-util.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "macro.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "sparse-endian.h"
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringtypedef struct DnsPacketHeader DnsPacketHeader;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poetteringtypedef struct DnsPacket DnsPacket;
7e8e0422aeb16f2a09a40546c61df753d10029b6Lennart Poettering
51323288fc628a5cac50914df915545d685b793eLennart Poettering#include "resolved-def.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "resolved-dns-answer.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "resolved-dns-question.h"
71d35b6b5563817dfbe757ab9e3b9f018b2db491Thomas Hindoe Paaboel Andersen#include "resolved-dns-rr.h"
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringtypedef enum DnsProtocol {
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering DNS_PROTOCOL_DNS,
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering DNS_PROTOCOL_MDNS,
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering DNS_PROTOCOL_LLMNR,
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering _DNS_PROTOCOL_MAX,
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering _DNS_PROTOCOL_INVALID = -1
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering} DnsProtocol;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstruct DnsPacketHeader {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering uint16_t id;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering be16_t flags;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering be16_t qdcount;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering be16_t ancount;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering be16_t nscount;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering be16_t arcount;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering};
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader)
a0166609f782da91710dea9183d1bf138538db37Tom Gundersen#define UDP_PACKET_HEADER_SIZE (sizeof(struct iphdr) + sizeof(struct udphdr))
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering/* The various DNS protocols deviate in how large a packet can grow,
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering but the TCP transport has a 16bit size field, hence that appears to
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering be the absolute maximum. */
c73ce96b569e2f10dff64b7dc0bd271972674c2aLennart Poettering#define DNS_PACKET_SIZE_MAX 0xFFFF
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering/* RFC 1035 say 512 is the maximum, for classic unicast DNS */
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering#define DNS_PACKET_UNICAST_SIZE_MAX 512
e1c959948c0e31d6997bcdfbabfbd077784b2baeLennart Poettering
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen/* With EDNS0 we can use larger packets, default to 4096, which is what is commonly used */
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen#define DNS_PACKET_UNICAST_SIZE_LARGE_MAX 4096
d74fb368b18f0fbd9a4fe6f15691bbea7f3c4a01Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define DNS_PACKET_SIZE_START 512
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstruct DnsPacket {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering int n_ref;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering DnsProtocol protocol;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering size_t size, allocated, rindex;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering void *_data; /* don't access directly, use DNS_PACKET_DATA()! */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering Hashmap *names; /* For name compression */
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poettering size_t opt_start, opt_size;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering /* Parsed data */
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering DnsQuestion *question;
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering DnsAnswer *answer;
d75acfb059ece4512278b8820a9103664996f1e5Lennart Poettering DnsResourceRecord *opt;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
dc61b7e45d89a69f0469ab7b3289cdde7fcc55abTorstein Husebø /* Packet reception metadata */
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering int ifindex;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering int family, ipproto;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering union in_addr_union sender, destination;
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poettering uint16_t sender_port, destination_port;
0dd25fb9f005d8ab7ac4bc10a609d00569f8c56aLennart Poettering uint32_t ttl;
a407657425a3e47fd2b559cd3bc800f791303f63Lennart Poettering
9c491563837983385bf9fa244590e76e142f4fa3Daniel Mack /* For support of truncated packets */
9c491563837983385bf9fa244590e76e142f4fa3Daniel Mack DnsPacket *more;
9c491563837983385bf9fa244590e76e142f4fa3Daniel Mack
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering bool on_stack:1;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering bool extracted:1;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering bool refuse_compression:1;
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poettering bool canonical_form:1;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering};
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringstatic inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering if (_unlikely_(!p))
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return NULL;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering if (p->_data)
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poettering return p->_data;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket));
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering#define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p))
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_AA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1)
ad867662936a4c7ab2c7116d804c272338801231Lennart Poettering#define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_RD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_RA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 7) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_AD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 5) & 1)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_CD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 4) & 1)
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poetteringstatic inline uint16_t DNS_PACKET_RCODE(DnsPacket *p) {
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering uint16_t rcode;
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering if (p->opt)
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering rcode = (uint16_t) (p->opt->ttl >> 24);
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering else
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering rcode = 0;
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering return rcode | (be16toh(DNS_PACKET_HEADER(p)->flags) & 15);
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering}
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack/* LLMNR defines some bits differently */
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_LLMNR_C(p) DNS_PACKET_AA(p)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack#define DNS_PACKET_LLMNR_T(p) DNS_PACKET_RD(p)
8b757a38611006a751c90933d1810cccaa47e1afDaniel Mack
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount)
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering#define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering (((uint16_t) !!(qr) << 15) | \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) ((opcode) & 15) << 11) | \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(aa) << 10) | /* on LLMNR: c */ \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(tc) << 9) | \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(rd) << 8) | /* on LLMNR: t */ \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(ra) << 7) | \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(ad) << 5) | \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) !!(cd) << 4) | \
f0258e473667f44f4656dde49597b2badb9f598aLennart Poettering ((uint16_t) ((rcode) & 15)))
3cb10d3a0b1b6a7c44f307f2abb5215104e16941Lennart Poettering
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poetteringstatic inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) {
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering return
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering (unsigned) DNS_PACKET_ANCOUNT(p) +
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering (unsigned) DNS_PACKET_NSCOUNT(p) +
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering (unsigned) DNS_PACKET_ARCOUNT(p);
322345fdb9865ef2477fba8e4bdde0e1183ef505Lennart Poettering}
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringint dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu);
24710c48ed16be5fa461fbb303a744a907541dafLennart Poetteringint dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu, bool dnssec_checking_disabled);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
dbfbb6e776d613cb9be76d13de076d08450c9d29Daniel Mackvoid dns_packet_set_flags(DnsPacket *p, bool dnssec_checking_disabled, bool truncated);
dbfbb6e776d613cb9be76d13de076d08450c9d29Daniel Mack
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsPacket *dns_packet_ref(DnsPacket *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDnsPacket *dns_packet_unref(DnsPacket *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_validate(DnsPacket *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_validate_reply(DnsPacket *p);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_validate_query(DnsPacket *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
8af5b883227ac8dfa796742b9edcc1647a5d4d6cLennart Poetteringint dns_packet_is_reply_for(DnsPacket *p, const DnsResourceKey *key);
8af5b883227ac8dfa796742b9edcc1647a5d4d6cLennart Poettering
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_append_string(DnsPacket *p, const char *s, size_t *start);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint dns_packet_append_raw_string(DnsPacket *p, const void *s, size_t size, size_t *start);
a3db237b8f1b97867395e1419f39b8ba5749b777Lennart Poetteringint dns_packet_append_label(DnsPacket *p, const char *s, size_t l, bool canonical_candidate, size_t *start);
a3db237b8f1b97867395e1419f39b8ba5749b777Lennart Poetteringint dns_packet_append_name(DnsPacket *p, const char *name, bool allow_compression, bool canonical_candidate, size_t *start);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, size_t *start);
a8812dd7f161a3e459c1730ac92ff2bbc9986ff1Lennart Poetteringint dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start, size_t *rdata_start);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poetteringint dns_packet_append_opt(DnsPacket *p, uint16_t max_udp_size, bool edns0_do, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
9c5e12a4314e7192e834e1b855e5e80111e636a6Tom Gundersenvoid dns_packet_truncate(DnsPacket *p, size_t sz);
519ef04651b07a547f010d6462603669d7fde4e5Lennart Poetteringint dns_packet_truncate_opt(DnsPacket *p);
9c5e12a4314e7192e834e1b855e5e80111e636a6Tom Gundersen
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start);
623a4c97b9175f95c4b1c6fc34e36c56f1e4ddbfLennart Poetteringint dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_read_string(DnsPacket *p, char **ret, size_t *start);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint dns_packet_read_raw_string(DnsPacket *p, const void **ret, size_t *size, size_t *start);
2001c80560e3dae69e14fd994d3978c187af48b8Lennart Poetteringint dns_packet_read_name(DnsPacket *p, char **ret, bool allow_compression, size_t *start);
d2579eec5e1b845b2cf29caddc951dc22f2abb91Lennart Poetteringint dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, bool *ret_cache_flush, size_t *start);
d2579eec5e1b845b2cf29caddc951dc22f2abb91Lennart Poetteringint dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, bool *ret_cache_flush, size_t *start);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poetteringvoid dns_packet_rewind(DnsPacket *p, size_t idx);
8ba9fd9cee0eef572f7b3ed7a8c3ed31160e93d3Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_packet_skip_question(DnsPacket *p);
faa133f3aa7a18f26563dc5d6b95898cb315c37aLennart Poetteringint dns_packet_extract(DnsPacket *p);
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poetteringstatic inline bool DNS_PACKET_SHALL_CACHE(DnsPacket *p) {
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering /* Never cache data originating from localhost, under the
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering * assumption, that it's coming from a locally DNS forwarder
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering * or server, that is caching on its own. */
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering return in_addr_is_localhost(p->family, &p->sender) == 0;
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering}
d830ebbdf67d8cb32d33d8fdd47cf467fd6d3815Lennart Poettering
f3abbe25403444688e1a1a23b9dbcc9aeefc0507Lennart Poettering/* https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml#dns-parameters-6 */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringenum {
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_SUCCESS = 0,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_FORMERR = 1,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_SERVFAIL = 2,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_NXDOMAIN = 3,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_NOTIMP = 4,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_REFUSED = 5,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_YXDOMAIN = 6,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_YXRRSET = 7,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_NXRRSET = 8,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_NOTAUTH = 9,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_NOTZONE = 10,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADVERS = 16,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADSIG = 16, /* duplicate value! */
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADKEY = 17,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADTIME = 18,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADMODE = 19,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADNAME = 20,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADALG = 21,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering DNS_RCODE_BADTRUNC = 22,
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering _DNS_RCODE_MAX_DEFINED
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering};
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poettering
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringconst char* dns_rcode_to_string(int i) _const_;
74b2466e14a1961bf3ac0e8a60cfaceec705bd59Lennart Poetteringint dns_rcode_from_string(const char *s) _pure_;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poetteringconst char* dns_protocol_to_string(DnsProtocol p) _const_;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart PoetteringDnsProtocol dns_protocol_from_string(const char *s) _pure_;
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) })
1716f6dcf54d4c181c2e2558e3d5414f54c8d9caLennart Poettering#define LLMNR_MULTICAST_IPV6_ADDRESS ((struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x03 } })
8db0d2f5c37e7e8f5bfce016cfdad7947a3ea939Zbigniew Jędrzejewski-Szmek
0db4c90afd7d9c7c8884bf8b3ec459edc74a03daDaniel Mack#define MDNS_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 251U) })
b6c5d46b23a28b5b03601ee1e8162b1bc7c7be25Daniel Mack#define MDNS_MULTICAST_IPV6_ADDRESS ((struct in6_addr) { .s6_addr = { 0xFF, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfb } })
0db4c90afd7d9c7c8884bf8b3ec459edc74a03daDaniel Mack
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poetteringstatic inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family, bool authenticated) {
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering uint64_t f;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering /* Converts a protocol + family into a flags field as used in queries and responses */
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering f = authenticated ? SD_RESOLVED_AUTHENTICATED : 0;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack switch (protocol) {
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack case DNS_PROTOCOL_DNS:
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering return f|SD_RESOLVED_DNS;
51323288fc628a5cac50914df915545d685b793eLennart Poettering
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack case DNS_PROTOCOL_LLMNR:
931851e8e492a4d2715e22dcde50a5e7ccef4b49Lennart Poettering return f|(family == AF_INET6 ? SD_RESOLVED_LLMNR_IPV6 : SD_RESOLVED_LLMNR_IPV4);
51323288fc628a5cac50914df915545d685b793eLennart Poettering
4e5bf5e15899de3f9d11c2ddfe9721d9f8b07a37Daniel Mack case DNS_PROTOCOL_MDNS:
4e5bf5e15899de3f9d11c2ddfe9721d9f8b07a37Daniel Mack return family == AF_INET6 ? SD_RESOLVED_MDNS_IPV6 : SD_RESOLVED_MDNS_IPV4;
4e5bf5e15899de3f9d11c2ddfe9721d9f8b07a37Daniel Mack
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack default:
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack break;
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack }
106784ebb7b303ae471851100a773ad2aebf5b80Daniel Mack
51323288fc628a5cac50914df915545d685b793eLennart Poettering return 0;
51323288fc628a5cac50914df915545d685b793eLennart Poettering}