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