Sergey Pastor / 1

Dependents:   Nucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers dns_common.h Source File

dns_common.h

Go to the documentation of this file.
00001 /**
00002  * @file dns_common.h
00003  * @brief Common DNS routines
00004  *
00005  * @section License
00006  *
00007  * Copyright (C) 2010-2017 Oryx Embedded SARL. All rights reserved.
00008  *
00009  * This file is part of CycloneTCP Open.
00010  *
00011  * This program is free software; you can redistribute it and/or
00012  * modify it under the terms of the GNU General Public License
00013  * as published by the Free Software Foundation; either version 2
00014  * of the License, or (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software Foundation,
00023  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024  *
00025  * @author Oryx Embedded SARL (www.oryx-embedded.com)
00026  * @version 1.7.6
00027  **/
00028 
00029 #ifndef _DNS_COMMON_H
00030 #define _DNS_COMMON_H
00031 
00032 //Dependencies
00033 #include "core/net.h"
00034 
00035 //Maximum recursion limit when parsing domain names
00036 #ifndef DNS_NAME_MAX_RECURSION
00037    #define DNS_NAME_MAX_RECURSION 4
00038 #elif (DNS_NAME_MAX_RECURSION < 1 || DNS_NAME_MAX_RECURSION > 8)
00039    #error DNS_NAME_MAX_RECURSION parameter is not valid
00040 #endif
00041 
00042 //Maximum size of DNS messages
00043 #define DNS_MESSAGE_MAX_SIZE 512
00044 //Maximum size of names
00045 #define DNS_NAME_MAX_SIZE 255
00046 //Maximum size of labels
00047 #define DNS_LABEL_MAX_SIZE 63
00048 
00049 //Maximum length of reverse DNS names (IPv4)
00050 #define DNS_MAX_IPV4_REVERSE_NAME_LEN 15
00051 //Maximum length of reverse DNS names (IPv6)
00052 #define DNS_MAX_IPV6_REVERSE_NAME_LEN 63
00053 
00054 //DNS port number
00055 #define DNS_PORT 53
00056 
00057 //Label compression tag
00058 #define DNS_COMPRESSION_TAG 0xC0
00059 
00060 //Macro definition
00061 #define DNS_GET_QUESTION(message, offset) (DnsQuestion *) ((uint8_t *) (message) + (offset))
00062 #define DNS_GET_RESOURCE_RECORD(message, offset) (DnsResourceRecord *) ((uint8_t *) (message) + (offset))
00063 
00064 #define DNS_SET_NSEC_BITMAP(bitmap, type) bitmap[(type) / 8] |= 0x80 >> ((type) % 8)
00065 #define DNS_CLR_NSEC_BITMAP(bitmap, type) bitmap[(type) / 8] &= ~(0x80 >> ((type) % 8))
00066 
00067 
00068 /**
00069  * @brief DNS opcodes
00070  **/
00071 
00072 typedef enum
00073 {
00074    DNS_OPCODE_QUERY         = 0,
00075    DNS_OPCODE_INVERSE_QUERY = 1,
00076    DNS_OPCODE_STATUS        = 2,
00077    DNS_OPCODE_NOTIFY        = 4,
00078    DNS_OPCODE_UPDATE        = 5
00079 } DnsOpcode;
00080 
00081 
00082 /**
00083  * @brief DNS return codes
00084  **/
00085 
00086 typedef enum
00087 {
00088    DNS_RCODE_NO_ERROR        = 0,
00089    DNS_RCODE_FORMAT_ERROR    = 1,
00090    DNS_RCODE_SERVER_FAILURE  = 2,
00091    DNS_RCODE_NAME_ERROR      = 3,
00092    DNS_RCODE_NOT_IMPLEMENTED = 4,
00093    DNS_RCODE_QUERY_REFUSED   = 5
00094 }DnsReturnCode;
00095 
00096 
00097 /**
00098  * @brief DNS resource record classes
00099  **/
00100 
00101 typedef enum
00102 {
00103    DNS_RR_CLASS_IN  = 1,  ///<Internet
00104    DNS_RR_CLASS_CH  = 3,  ///<Chaos
00105    DNS_RR_CLASS_HS  = 4,  ///<Hesiod
00106    DNS_RR_CLASS_ANY = 255 ///<Any class
00107 } DnsResourceRecordClass;
00108 
00109 
00110 /**
00111  * @brief DNS resource record types
00112  **/
00113 
00114 typedef enum
00115 {
00116    DNS_RR_TYPE_A     = 1,   ///<Host address
00117    DNS_RR_TYPE_NS    = 2,   ///<Authoritative name server
00118    DNS_RR_TYPE_CNAME = 5,   ///<Canonical name for an alias
00119    DNS_RR_TYPE_SOA   = 6,   ///<Start of a zone of authority
00120    DNS_RR_TYPE_WKS   = 11,  ///<Well known service description
00121    DNS_RR_TYPE_PTR   = 12,  ///<Domain name pointer
00122    DNS_RR_TYPE_HINFO = 13,  ///<Host information
00123    DNS_RR_TYPE_MINFO = 14,  ///<Mailbox or mail list information
00124    DNS_RR_TYPE_MX    = 15,  ///<Mail exchange
00125    DNS_RR_TYPE_TXT   = 16,  ///<Text strings
00126    DNS_RR_TYPE_AAAA  = 28,  ///<IPv6 address
00127    DNS_RR_TYPE_NB    = 32,  ///<NetBIOS name service
00128    DNS_RR_TYPE_SRV   = 33,  ///<Server selection
00129    DNS_RR_TYPE_NAPTR = 35,  ///<Naming authority pointer
00130    DNS_RR_TYPE_NSEC  = 47,  ///<NSEC record
00131    DNS_RR_TYPE_EUI48 = 108, ///<EUI-48 address
00132    DNS_RR_TYPE_EUI64 = 109, ///<EUI-64 address
00133    DNS_RR_TYPE_AXFR  = 252, ///<Transfer of an entire zone
00134    DNS_RR_TYPE_ANY   = 255, ///<A request for all records
00135    DNS_RR_TYPE_URI   = 256  ///<Uniform resource identifier
00136 } DnsResourceRecordType;
00137 
00138 
00139 //CodeWarrior or Win32 compiler?
00140 #if defined(__CWCC__) || defined(_WIN32)
00141    #pragma pack(push, 1)
00142 #endif
00143 
00144 
00145 /**
00146  * @brief DNS message header
00147  **/
00148 
00149 typedef __start_packed struct
00150 {
00151    uint16_t id;         //0-1
00152 #ifdef _CPU_BIG_ENDIAN
00153    uint16_t qr : 1;     //2
00154    uint16_t opcode : 4;
00155    uint16_t aa : 1;
00156    uint16_t tc : 1;
00157    uint16_t rd : 1;
00158    uint16_t ra : 1;     //3
00159    uint16_t z : 3;
00160    uint16_t rcode : 4;
00161 #else
00162    uint16_t rd : 1;     //2
00163    uint16_t tc : 1;
00164    uint16_t aa : 1;
00165    uint16_t opcode : 4;
00166    uint16_t qr : 1;
00167    uint16_t rcode : 4;  //3
00168    uint16_t z : 3;
00169    uint16_t ra : 1;
00170 #endif
00171    uint16_t qdcount;    //4-5
00172    uint16_t ancount;    //6-7
00173    uint16_t nscount;    //8-9
00174    uint16_t arcount;    //10-11
00175    uint8_t questions[]; //12
00176 } __end_packed DnsHeader;
00177 
00178 
00179 /**
00180  * @brief Question format
00181  **/
00182 
00183 typedef __start_packed struct
00184 {
00185    uint16_t qtype;
00186    uint16_t qclass;
00187 } __end_packed DnsQuestion;
00188 
00189 
00190 /**
00191  * @brief Resource record format
00192  **/
00193 
00194 typedef __start_packed struct
00195 {
00196    uint16_t rtype;    //0-1
00197    uint16_t rclass;   //2-3
00198    uint32_t ttl;      //4-7
00199    uint16_t rdlength; //8-9
00200    uint8_t rdata[];   //10
00201 } __end_packed DnsResourceRecord;
00202 
00203 
00204 /**
00205  * @brief SRV resource record format
00206  **/
00207 
00208 typedef __start_packed struct
00209 {
00210    uint16_t rtype;    //0-1
00211    uint16_t rclass;   //2-3
00212    uint32_t ttl;      //4-7
00213    uint16_t rdlength; //8-9
00214    uint16_t priority; //10-11
00215    uint16_t weight;   //12-13
00216    uint16_t port;     //14-15
00217    uint8_t target[];  //16
00218 } __end_packed DnsSrvResourceRecord;
00219 
00220 
00221 //CodeWarrior or Win32 compiler?
00222 #if defined(__CWCC__) || defined(_WIN32)
00223    #pragma pack(pop)
00224 #endif
00225 
00226 
00227 //DNS related functions
00228 size_t dnsEncodeName(const char_t *src, uint8_t *dest);
00229 
00230 size_t dnsParseName(const DnsHeader *message,
00231    size_t length, size_t pos, char_t *dest, uint_t level);
00232 
00233 int_t dnsCompareName(const DnsHeader *message, size_t length,
00234    size_t pos, const char_t *name, uint_t level);
00235 
00236 int_t dnsCompareEncodedName(const DnsHeader *message1, size_t length1, size_t pos1,
00237    const DnsHeader *message2, size_t length2, size_t pos2, uint_t level);
00238 
00239 #endif
00240