dns_common.h File Reference

Common DNS routines. More...

#include "core/net.h"

Go to the source code of this file.

Macros

#define DNS_NAME_MAX_RECURSION   4
 
#define DNS_MESSAGE_MAX_SIZE   512
 
#define DNS_NAME_MAX_SIZE   255
 
#define DNS_LABEL_MAX_SIZE   63
 
#define DNS_MAX_IPV4_REVERSE_NAME_LEN   15
 
#define DNS_MAX_IPV6_REVERSE_NAME_LEN   63
 
#define DNS_PORT   53
 
#define DNS_COMPRESSION_TAG   0xC0
 
#define DNS_GET_QUESTION(message, offset)   (DnsQuestion *) ((uint8_t *) (message) + (offset))
 
#define DNS_GET_RESOURCE_RECORD(message, offset)   (DnsResourceRecord *) ((uint8_t *) (message) + (offset))
 
#define DNS_SET_NSEC_BITMAP(bitmap, type)   bitmap[(type) / 8] |= 0x80 >> ((type) % 8)
 
#define DNS_CLR_NSEC_BITMAP(bitmap, type)   bitmap[(type) / 8] &= ~(0x80 >> ((type) % 8))
 

Enumerations

enum  DnsOpcode {
  DNS_OPCODE_QUERY = 0 , DNS_OPCODE_IQUERY = 1 , DNS_OPCODE_STATUS = 2 , DNS_OPCODE_NOTIFY = 4 ,
  DNS_OPCODE_UPDATE = 5
}
 DNS opcodes. More...
 
enum  DnsResponseCode {
  DNS_RCODE_NOERROR = 0 , DNS_RCODE_FORMERR = 1 , DNS_RCODE_SERVFAIL = 2 , DNS_RCODE_NXDOMAIN = 3 ,
  DNS_RCODE_NOTIMP = 4 , DNS_RCODE_REFUSED = 5 , DNS_RCODE_YXDOMAIN = 6 , DNS_RCODE_YXRRSET = 7 ,
  DNS_RCODE_NXRRSET = 8 , DNS_RCODE_NOTAUTH = 9 , DNS_RCODE_NOTZONE = 10 , DNS_RCODE_BADVERS = 16 ,
  DNS_RCODE_BADSIG = 16 , DNS_RCODE_BADKEY = 17 , DNS_RCODE_BADTIME = 18 , DNS_RCODE_BADMODE = 19 ,
  DNS_RCODE_BADNAME = 20 , DNS_RCODE_BADALG = 21 , DNS_RCODE_BADTRUC = 22 , DNS_RCODE_BADCOOKIE = 23
}
 DNS response codes. More...
 
enum  DnsResourceRecordClass { DNS_RR_CLASS_IN = 1 , DNS_RR_CLASS_CH = 3 , DNS_RR_CLASS_HS = 4 , DNS_RR_CLASS_ANY = 255 }
 DNS resource record classes. More...
 
enum  DnsResourceRecordType {
  DNS_RR_TYPE_A = 1 , DNS_RR_TYPE_NS = 2 , DNS_RR_TYPE_CNAME = 5 , DNS_RR_TYPE_SOA = 6 ,
  DNS_RR_TYPE_WKS = 11 , DNS_RR_TYPE_PTR = 12 , DNS_RR_TYPE_HINFO = 13 , DNS_RR_TYPE_MINFO = 14 ,
  DNS_RR_TYPE_MX = 15 , DNS_RR_TYPE_TXT = 16 , DNS_RR_TYPE_AAAA = 28 , DNS_RR_TYPE_NB = 32 ,
  DNS_RR_TYPE_SRV = 33 , DNS_RR_TYPE_NAPTR = 35 , DNS_RR_TYPE_NSEC = 47 , DNS_RR_TYPE_EUI48 = 108 ,
  DNS_RR_TYPE_EUI64 = 109 , DNS_RR_TYPE_AXFR = 252 , DNS_RR_TYPE_ANY = 255 , DNS_RR_TYPE_URI = 256
}
 DNS resource record types. More...
 

Functions

size_t dnsEncodeName (const char_t *src, uint8_t *dest)
 Encode a domain name using the DNS name notation. More...
 
size_t dnsParseName (const DnsHeader *message, size_t length, size_t pos, char_t *dest, uint_t level)
 Decode a domain name that uses the DNS name encoding. More...
 
int_t dnsCompareName (const DnsHeader *message, size_t length, size_t pos, const char_t *name, uint_t level)
 Compare domain names. More...
 
int_t dnsCompareEncodedName (const DnsHeader *message1, size_t length1, size_t pos1, const DnsHeader *message2, size_t length2, size_t pos2, uint_t level)
 Compare domain names encoded with DNS notation. More...
 
void dnsGenerateIpv4ReverseName (Ipv4Addr ipv4Addr, char_t *buffer)
 Generate domain name for reverse DNS lookup (IPv4) More...
 
void dnsGenerateIpv6ReverseName (const Ipv6Addr *ipv6Addr, char_t *buffer)
 Generate domain name for reverse DNS lookup (IPv6) More...
 

Variables

typedef __packed_struct
 DNS message header. More...
 
uint8_t rd
 
uint8_t tc
 
uint8_t aa
 
uint8_t opcode
 
uint8_t qr
 
uint8_t rcode
 
uint8_t z
 
uint8_t ra
 
uint16_t qdcount
 
uint16_t ancount
 
uint16_t nscount
 
uint16_t arcount
 
uint8_t questions []
 
 DnsHeader
 
uint16_t qclass
 
 DnsQuestion
 
uint16_t rclass
 
uint32_t ttl
 
uint16_t rdlength
 
uint8_t rdata []
 
 DnsResourceRecord
 
 DnsIpv4AddrResourceRecord
 
 DnsIpv6AddrResourceRecord
 
uint16_t priority
 
uint16_t weight
 
uint16_t port
 
uint8_t target []
 
 DnsSrvResourceRecord
 

Detailed Description

Common DNS routines.

License

SPDX-License-Identifier: GPL-2.0-or-later

Copyright (C) 2010-2024 Oryx Embedded SARL. All rights reserved.

This file is part of CycloneTCP Open.

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

Author
Oryx Embedded SARL (www.oryx-embedded.com)
Version
2.4.0

Definition in file dns_common.h.

Macro Definition Documentation

◆ DNS_CLR_NSEC_BITMAP

#define DNS_CLR_NSEC_BITMAP (   bitmap,
  type 
)    bitmap[(type) / 8] &= ~(0x80 >> ((type) % 8))

Definition at line 67 of file dns_common.h.

◆ DNS_COMPRESSION_TAG

#define DNS_COMPRESSION_TAG   0xC0

Definition at line 60 of file dns_common.h.

◆ DNS_GET_QUESTION

#define DNS_GET_QUESTION (   message,
  offset 
)    (DnsQuestion *) ((uint8_t *) (message) + (offset))

Definition at line 63 of file dns_common.h.

◆ DNS_GET_RESOURCE_RECORD

#define DNS_GET_RESOURCE_RECORD (   message,
  offset 
)    (DnsResourceRecord *) ((uint8_t *) (message) + (offset))

Definition at line 64 of file dns_common.h.

◆ DNS_LABEL_MAX_SIZE

#define DNS_LABEL_MAX_SIZE   63

Definition at line 49 of file dns_common.h.

◆ DNS_MAX_IPV4_REVERSE_NAME_LEN

#define DNS_MAX_IPV4_REVERSE_NAME_LEN   15

Definition at line 52 of file dns_common.h.

◆ DNS_MAX_IPV6_REVERSE_NAME_LEN

#define DNS_MAX_IPV6_REVERSE_NAME_LEN   63

Definition at line 54 of file dns_common.h.

◆ DNS_MESSAGE_MAX_SIZE

#define DNS_MESSAGE_MAX_SIZE   512

Definition at line 45 of file dns_common.h.

◆ DNS_NAME_MAX_RECURSION

#define DNS_NAME_MAX_RECURSION   4

Definition at line 39 of file dns_common.h.

◆ DNS_NAME_MAX_SIZE

#define DNS_NAME_MAX_SIZE   255

Definition at line 47 of file dns_common.h.

◆ DNS_PORT

#define DNS_PORT   53

Definition at line 57 of file dns_common.h.

◆ DNS_SET_NSEC_BITMAP

#define DNS_SET_NSEC_BITMAP (   bitmap,
  type 
)    bitmap[(type) / 8] |= 0x80 >> ((type) % 8)

Definition at line 66 of file dns_common.h.

Enumeration Type Documentation

◆ DnsOpcode

enum DnsOpcode

DNS opcodes.

Enumerator
DNS_OPCODE_QUERY 

Query.

DNS_OPCODE_IQUERY 

Inverse query.

DNS_OPCODE_STATUS 

Status.

DNS_OPCODE_NOTIFY 

Notify.

DNS_OPCODE_UPDATE 

Update.

Definition at line 79 of file dns_common.h.

◆ DnsResourceRecordClass

DNS resource record classes.

Enumerator
DNS_RR_CLASS_IN 

Internet.

DNS_RR_CLASS_CH 

Chaos.

DNS_RR_CLASS_HS 

Hesiod.

DNS_RR_CLASS_ANY 

Any class.

Definition at line 122 of file dns_common.h.

◆ DnsResourceRecordType

DNS resource record types.

Enumerator
DNS_RR_TYPE_A 

Host address.

DNS_RR_TYPE_NS 

Authoritative name server.

DNS_RR_TYPE_CNAME 

Canonical name for an alias.

DNS_RR_TYPE_SOA 

Start of a zone of authority.

DNS_RR_TYPE_WKS 

Well known service description.

DNS_RR_TYPE_PTR 

Domain name pointer.

DNS_RR_TYPE_HINFO 

Host information.

DNS_RR_TYPE_MINFO 

Mailbox or mail list information.

DNS_RR_TYPE_MX 

Mail exchange.

DNS_RR_TYPE_TXT 

Text strings.

DNS_RR_TYPE_AAAA 

IPv6 address.

DNS_RR_TYPE_NB 

NetBIOS name service.

DNS_RR_TYPE_SRV 

Server selection.

DNS_RR_TYPE_NAPTR 

Naming authority pointer.

DNS_RR_TYPE_NSEC 

NSEC record.

DNS_RR_TYPE_EUI48 

EUI-48 address.

DNS_RR_TYPE_EUI64 

EUI-64 address.

DNS_RR_TYPE_AXFR 

Transfer of an entire zone.

DNS_RR_TYPE_ANY 

A request for all records.

DNS_RR_TYPE_URI 

Uniform resource identifier.

Definition at line 135 of file dns_common.h.

◆ DnsResponseCode

DNS response codes.

Enumerator
DNS_RCODE_NOERROR 

No error.

DNS_RCODE_FORMERR 

Format error.

DNS_RCODE_SERVFAIL 

Server failure.

DNS_RCODE_NXDOMAIN 

Non-existent domain.

DNS_RCODE_NOTIMP 

Not implemented.

DNS_RCODE_REFUSED 

Query refused.

DNS_RCODE_YXDOMAIN 

Name exists when it should not.

DNS_RCODE_YXRRSET 

RR set exists when it should not.

DNS_RCODE_NXRRSET 

RR set that should exist does not.

DNS_RCODE_NOTAUTH 

Server not authoritative for zone.

DNS_RCODE_NOTZONE 

Name not contained in zone.

DNS_RCODE_BADVERS 

Bad OPT version.

DNS_RCODE_BADSIG 

TSIG signature failure.

DNS_RCODE_BADKEY 

Key not recognized.

DNS_RCODE_BADTIME 

Signature out of time window.

DNS_RCODE_BADMODE 

Bad TKEY mode.

DNS_RCODE_BADNAME 

Duplicate key name.

DNS_RCODE_BADALG 

Algorithm not supported.

DNS_RCODE_BADTRUC 

Bad truncation.

DNS_RCODE_BADCOOKIE 

Bad server cookie.

Definition at line 93 of file dns_common.h.

Function Documentation

◆ dnsCompareEncodedName()

int_t dnsCompareEncodedName ( const DnsHeader message1,
size_t  length1,
size_t  pos1,
const DnsHeader message2,
size_t  length2,
size_t  pos2,
uint_t  level 
)

Compare domain names encoded with DNS notation.

Parameters
[in]message1Pointer to the first DNS message
[in]length1Length of the first DNS message
[in]pos1Offset of the encoded domain name within the first message
[in]message2Pointer to the second DNS message
[in]length2Length of the second DNS message
[in]pos2Offset of the encoded domain name within the second message
[in]levelCurrent level of recursion
Returns
The function returns 0 if the domain names match, -1 if the first domain name lexicographically precedes the second name, or 1 if the second domain name lexicographically precedes the first name

Definition at line 341 of file dns_common.c.

◆ dnsCompareName()

int_t dnsCompareName ( const DnsHeader message,
size_t  length,
size_t  pos,
const char_t name,
uint_t  level 
)

Compare domain names.

Parameters
[in]messagePointer to the DNS message
[in]lengthLength of the DNS message
[in]posOffset of the encoded domain name
[in]nameNULL-terminated string that holds a domain name
[in]levelCurrent level of recursion
Returns
The function returns 0 if the domain names match, -1 if the first domain name lexicographically precedes the second name, or 1 if the second domain name lexicographically precedes the first name

Definition at line 242 of file dns_common.c.

◆ dnsEncodeName()

size_t dnsEncodeName ( const char_t src,
uint8_t *  dest 
)

Encode a domain name using the DNS name notation.

Parameters
[in]srcPointer to the domain name to encode
[out]destPointer to the encoded domain name (optional parameter)
Returns
Length of the encoded domain name

Definition at line 58 of file dns_common.c.

◆ dnsGenerateIpv4ReverseName()

void dnsGenerateIpv4ReverseName ( Ipv4Addr  ipv4Addr,
char_t buffer 
)

Generate domain name for reverse DNS lookup (IPv4)

Parameters
[in]ipv4AddrIPv4 address
[out]bufferOutput buffer where to store the resulting domain name

Definition at line 479 of file dns_common.c.

◆ dnsGenerateIpv6ReverseName()

void dnsGenerateIpv6ReverseName ( const Ipv6Addr ipv6Addr,
char_t buffer 
)

Generate domain name for reverse DNS lookup (IPv6)

Parameters
[in]ipv6AddrIPv6 address
[out]bufferOutput buffer where to store the resulting domain name

Definition at line 498 of file dns_common.c.

◆ dnsParseName()

size_t dnsParseName ( const DnsHeader message,
size_t  length,
size_t  pos,
char_t dest,
uint_t  level 
)

Decode a domain name that uses the DNS name encoding.

Parameters
[in]messagePointer to the DNS message
[in]lengthLength of the DNS message
[in]posOffset of the name to decode
[out]destPointer to the decoded name (optional)
[in]levelCurrent level of recursion
Returns
The position of the resource record that immediately follows the domain name

Definition at line 132 of file dns_common.c.

Variable Documentation

◆ __packed_struct

typedef __packed_struct
Initial value:
{
uint16_t id

DNS message header.

SRV resource record format.

AAAA resource record format.

A resource record format.

Resource record format.

Question format.

Definition at line 172 of file dns_common.h.

◆ aa

uint8_t aa

Definition at line 187 of file dns_common.h.

◆ ancount

uint16_t ancount

Definition at line 195 of file dns_common.h.

◆ arcount

uint16_t arcount

Definition at line 197 of file dns_common.h.

◆ DnsHeader

DnsHeader

Definition at line 199 of file dns_common.h.

◆ DnsIpv4AddrResourceRecord

DnsIpv4AddrResourceRecord

Definition at line 238 of file dns_common.h.

◆ DnsIpv6AddrResourceRecord

DnsIpv6AddrResourceRecord

Definition at line 252 of file dns_common.h.

◆ DnsQuestion

DnsQuestion

Definition at line 210 of file dns_common.h.

◆ DnsResourceRecord

DnsResourceRecord

Definition at line 224 of file dns_common.h.

◆ DnsSrvResourceRecord

DnsSrvResourceRecord

Definition at line 269 of file dns_common.h.

◆ nscount

uint16_t nscount

Definition at line 196 of file dns_common.h.

◆ opcode

uint8_t opcode

Definition at line 188 of file dns_common.h.

◆ port

uint16_t port

Definition at line 267 of file dns_common.h.

◆ priority

uint16_t priority

Definition at line 265 of file dns_common.h.

◆ qclass

uint16_t qclass

Definition at line 209 of file dns_common.h.

◆ qdcount

uint16_t qdcount

Definition at line 194 of file dns_common.h.

◆ qr

uint8_t qr

Definition at line 189 of file dns_common.h.

◆ questions

uint8_t questions[]

Definition at line 198 of file dns_common.h.

◆ ra

uint8_t ra

Definition at line 192 of file dns_common.h.

◆ rclass

uint16_t rclass

Definition at line 220 of file dns_common.h.

◆ rcode

uint8_t rcode

Definition at line 190 of file dns_common.h.

◆ rd

uint8_t rd

Definition at line 185 of file dns_common.h.

◆ rdata

uint8_t rdata

Definition at line 223 of file dns_common.h.

◆ rdlength

uint16_t rdlength

Definition at line 222 of file dns_common.h.

◆ target

uint8_t target[]

Definition at line 268 of file dns_common.h.

◆ tc

uint8_t tc

Definition at line 186 of file dns_common.h.

◆ ttl

uint32_t ttl

Definition at line 221 of file dns_common.h.

◆ weight

uint16_t weight

Definition at line 266 of file dns_common.h.

◆ z

uint8_t z

Definition at line 191 of file dns_common.h.