resolved-dns-packet.h revision 8b757a38611006a751c90933d1810cccaa47e1af
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering This file is part of systemd.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Copyright 2014 Lennart Poettering
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is free software; you can redistribute it and/or modify it
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering under the terms of the GNU Lesser General Public License as published by
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering (at your option) any later version.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering systemd is distributed in the hope that it will be useful, but
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Lesser General Public License for more details.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering You should have received a copy of the GNU Lesser General Public License
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringtypedef struct DnsPacketHeader DnsPacketHeader;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_HEADER_SIZE sizeof(DnsPacketHeader)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/* The various DNS protocols deviate in how large a packet can grow,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering but the TCP transport has a 16bit size field, hence that appears to
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering be the absolute maximum. */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/* RFC 1035 say 512 is the maximum, for classic unicast DNS */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering void *_data; /* don't access directly, use DNS_PACKET_DATA()! */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering Hashmap *names; /* For name compression */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Parsed data */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Packet reception meta data */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic inline uint8_t* DNS_PACKET_DATA(DnsPacket *p) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return ((uint8_t*) p) + ALIGN(sizeof(DnsPacket));
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_HEADER(p) ((DnsPacketHeader*) DNS_PACKET_DATA(p))
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_ID(p) DNS_PACKET_HEADER(p)->id
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_QR(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 15) & 1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_OPCODE(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 11) & 15)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_AA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 10) & 1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_TC(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 9) & 1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_RD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 8) & 1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_RA(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 7) & 1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_AD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 5) & 1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_CD(p) ((be16toh(DNS_PACKET_HEADER(p)->flags) >> 4) & 1)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_RCODE(p) (be16toh(DNS_PACKET_HEADER(p)->flags) & 15)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/* LLMNR defines some bits differently */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_LLMNR_C(p) DNS_PACKET_AA(p)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_LLMNR_T(p) DNS_PACKET_RD(p)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_QDCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->qdcount)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_ANCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->ancount)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_NSCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->nscount)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_ARCOUNT(p) be16toh(DNS_PACKET_HEADER(p)->arcount)
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define DNS_PACKET_MAKE_FLAGS(qr, opcode, aa, tc, rd, ra, ad, cd, rcode) \
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic inline unsigned DNS_PACKET_RRCOUNT(DnsPacket *p) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_new(DnsPacket **p, DnsProtocol protocol, size_t mtu);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_new_query(DnsPacket **p, DnsProtocol protocol, size_t mtu);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringDnsPacket *dns_packet_unref(DnsPacket *p);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringDEFINE_TRIVIAL_CLEANUP_FUNC(DnsPacket*, dns_packet_unref);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_validate_reply(DnsPacket *p);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_validate_query(DnsPacket *p);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_blob(DnsPacket *p, const void *d, size_t sz, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_uint8(DnsPacket *p, uint8_t v, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_uint16(DnsPacket *p, uint16_t v, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_uint32(DnsPacket *p, uint32_t v, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_string(DnsPacket *p, const char *s, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_label(DnsPacket *p, const char *s, size_t l, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_name(DnsPacket *p, const char *name,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_key(DnsPacket *p, const DnsResourceKey *key, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_append_rr(DnsPacket *p, const DnsResourceRecord *rr, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read(DnsPacket *p, size_t sz, const void **ret, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_blob(DnsPacket *p, void *d, size_t sz, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_uint8(DnsPacket *p, uint8_t *ret, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_uint16(DnsPacket *p, uint16_t *ret, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_uint32(DnsPacket *p, uint32_t *ret, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_string(DnsPacket *p, char **ret, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_name(DnsPacket *p, char **ret,
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_key(DnsPacket *p, DnsResourceKey **ret, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_read_rr(DnsPacket *p, DnsResourceRecord **ret, size_t *start);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringvoid dns_packet_rewind(DnsPacket *p, size_t idx);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_packet_skip_question(DnsPacket *p);
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering DNS_RCODE_BADSIG = 16, /* duplicate value! */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringconst char* dns_rcode_to_string(int i) _const_;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dns_rcode_from_string(const char *s) _pure_;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringconst char* dns_protocol_to_string(DnsProtocol p) _const_;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart PoetteringDnsProtocol dns_protocol_from_string(const char *s) _pure_;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering#define LLMNR_MULTICAST_IPV4_ADDRESS ((struct in_addr) { .s_addr = htobe32(224U << 24 | 252U) })
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart 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 } })
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic inline uint16_t dnskey_to_flags(const DnsResourceRecord *rr) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering return (rr->dnskey.zone_key_flag * DNSKEY_FLAG_ZONE_KEY |
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering/* http://tools.ietf.org/html/rfc4034#appendix-A.1 */
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringconst char* dnssec_algorithm_to_string(int i) _const_;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringint dnssec_algorithm_from_string(const char *s) _pure_;
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poetteringstatic inline uint64_t SD_RESOLVED_FLAGS_MAKE(DnsProtocol protocol, int family) {
2087a7aff26ea5d1bc2c7c29add3275328f36baaLennart Poettering /* Converts a protocol + family into a flags field as used in queries */