Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 1 | /* |
| 2 | * include/types/dns.h |
| 3 | * This file provides structures and types for DNS. |
| 4 | * |
| 5 | * Copyright (C) 2014 Baptiste Assmann <bedis9@gmail.com> |
| 6 | * |
| 7 | * This library is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU Lesser General Public |
| 9 | * License as published by the Free Software Foundation, version 2.1 |
| 10 | * exclusively. |
| 11 | * |
| 12 | * This library is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public |
| 18 | * License along with this library; if not, write to the Free Software |
| 19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | */ |
| 21 | |
| 22 | #ifndef _TYPES_DNS_H |
| 23 | #define _TYPES_DNS_H |
| 24 | |
| 25 | /*DNS maximum values */ |
| 26 | /* |
| 27 | * Maximum issued from RFC: |
| 28 | * RFC 1035: https://www.ietf.org/rfc/rfc1035.txt chapter 2.3.4 |
| 29 | * RFC 2671: http://tools.ietf.org/html/rfc2671 |
| 30 | */ |
| 31 | #define DNS_MAX_LABEL_SIZE 63 |
| 32 | #define DNS_MAX_NAME_SIZE 255 |
| 33 | #define DNS_MAX_UDP_MESSAGE 4096 |
| 34 | |
| 35 | /* DNS error messages */ |
| 36 | #define DNS_TOO_LONG_FQDN "hostname too long" |
| 37 | #define DNS_LABEL_TOO_LONG "one label too long" |
| 38 | #define DNS_INVALID_CHARACTER "found an invalid character" |
| 39 | |
| 40 | /* dns query class */ |
| 41 | #define DNS_RCLASS_IN 1 /* internet class */ |
| 42 | |
| 43 | /* dns record types (non exhaustive list) */ |
| 44 | #define DNS_RTYPE_A 1 /* IPv4 address */ |
| 45 | #define DNS_RTYPE_CNAME 5 /* canonical name */ |
| 46 | #define DNS_RTYPE_AAAA 28 /* IPv6 address */ |
| 47 | #define DNS_RTYPE_ANY 255 /* all records */ |
| 48 | |
| 49 | /* dns rcode values */ |
| 50 | #define DNS_RCODE_NO_ERROR 0 /* no error */ |
| 51 | #define DNS_RCODE_NX_DOMAIN 3 /* non existent domain */ |
| 52 | #define DNS_RCODE_REFUSED 5 /* query refused */ |
| 53 | |
Baptiste Assmann | 042d0a1 | 2015-09-02 21:52:37 +0200 | [diff] [blame^] | 54 | /* dns flags masks */ |
| 55 | #define DNS_FLAG_TRUNCATED 0x0200 /* mask for truncated flag */ |
| 56 | #define DNS_FLAG_REPLYCODE 0x000F /* mask for reply code */ |
| 57 | |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 58 | /* DNS request or response header structure */ |
| 59 | struct dns_header { |
| 60 | unsigned short id:16; /* identifier */ |
| 61 | unsigned char rd :1; /* recursion desired 0: no, 1: yes */ |
| 62 | unsigned char tc :1; /* truncation 0:no, 1: yes */ |
| 63 | unsigned char aa :1; /* authoritative answer 0: no, 1: yes */ |
| 64 | unsigned char opcode :4; /* operation code */ |
| 65 | unsigned char qr :1; /* query/response 0: query, 1: response */ |
| 66 | unsigned char rcode :4; /* response code */ |
| 67 | unsigned char z :1; /* no used */ |
| 68 | unsigned char ad :1; /* authentic data */ |
| 69 | unsigned char cd :1; /* checking disabled */ |
| 70 | unsigned char ra :1; /* recursion available 0: no, 1: yes */ |
| 71 | unsigned short qdcount :16; /* question count */ |
| 72 | unsigned short ancount :16; /* answer count */ |
| 73 | unsigned short nscount :16; /* authority count */ |
| 74 | unsigned short arcount :16; /* additional count */ |
| 75 | }; |
| 76 | |
| 77 | /* short structure to describe a DNS question */ |
| 78 | struct dns_question { |
| 79 | unsigned short qtype; /* question type */ |
| 80 | unsigned short qclass; /* query class */ |
| 81 | }; |
| 82 | |
| 83 | /* |
| 84 | * resolvers section and parameters. It is linked to the name servers |
| 85 | * servers points to it. |
| 86 | * current resolution are stored in a FIFO list. |
| 87 | */ |
| 88 | struct dns_resolvers { |
| 89 | struct list list; /* resolvers list */ |
| 90 | char *id; /* resolvers unique identifier */ |
| 91 | struct { |
| 92 | const char *file; /* file where the section appears */ |
| 93 | int line; /* line where the section appears */ |
| 94 | } conf; /* config information */ |
| 95 | struct list nameserver_list; /* dns server list */ |
| 96 | int count_nameservers; /* total number of nameservers in a resolvers section */ |
| 97 | int resolve_retries; /* number of retries before giving up */ |
| 98 | struct { /* time to: */ |
| 99 | int retry; /* wait for a response before retrying */ |
| 100 | } timeout; |
| 101 | struct { /* time to hold current data when */ |
| 102 | int valid; /* a response is valid */ |
| 103 | } hold; |
| 104 | struct task *t; /* timeout management */ |
| 105 | struct list curr_resolution; /* current running resolutions */ |
| 106 | struct eb_root query_ids; /* tree to quickly lookup/retrieve query ids currently in use */ |
| 107 | /* used by each nameserver, but stored in resolvers since there must */ |
| 108 | /* be a unique relation between an eb_root and an eb_node (resolution) */ |
| 109 | }; |
| 110 | |
| 111 | /* |
| 112 | * structure describing a name server used during name resolution. |
| 113 | * A name server belongs to a resolvers section. |
| 114 | */ |
| 115 | struct dns_nameserver { |
| 116 | struct list list; /* nameserver chained list */ |
| 117 | char *id; /* nameserver unique identifier */ |
| 118 | struct { |
| 119 | const char *file; /* file where the section appears */ |
| 120 | int line; /* line where the section appears */ |
| 121 | } conf; /* config information */ |
| 122 | struct dns_resolvers *resolvers; |
| 123 | struct dgram_conn *dgram; /* transport layer */ |
| 124 | struct sockaddr_storage addr; /* IP address */ |
| 125 | struct { /* numbers relted to this name server: */ |
| 126 | long int sent; /* - queries sent */ |
| 127 | long int valid; /* - valid response */ |
| 128 | long int update; /* - valid response used to update server's IP */ |
| 129 | long int cname; /* - CNAME response requiring new resolution */ |
| 130 | long int cname_error; /* - error when resolving CNAMEs */ |
| 131 | long int any_err; /* - void response (usually because ANY qtype) */ |
| 132 | long int nx; /* - NX response */ |
| 133 | long int timeout; /* - queries which reached timeout */ |
| 134 | long int refused; /* - queries refused */ |
| 135 | long int other; /* - other type of response */ |
| 136 | long int invalid; /* - malformed DNS response */ |
| 137 | long int too_big; /* - too big response */ |
| 138 | long int outdated; /* - outdated response (server slower than the other ones) */ |
| 139 | } counters; |
| 140 | }; |
| 141 | |
| 142 | /* |
| 143 | * resolution structure associated to single server and used to manage name resolution for |
| 144 | * this server. |
| 145 | * The only link between the resolution and a nameserver is through the query_id. |
| 146 | */ |
| 147 | struct dns_resolution { |
| 148 | struct list list; /* resolution list */ |
| 149 | struct dns_resolvers *resolvers; /* resolvers section associated to this resolution */ |
| 150 | void *requester; /* owner of this name resolution */ |
| 151 | int (*requester_cb)(struct dns_resolution *, struct dns_nameserver *, unsigned char *, int); |
| 152 | /* requester callback for valid response */ |
| 153 | int (*requester_error_cb)(struct dns_resolution *, int); |
| 154 | /* requester callback, for error management */ |
| 155 | char *hostname_dn; /* server hostname in domain name label format */ |
| 156 | int hostname_dn_len; /* server domain name label len */ |
| 157 | int resolver_family_priority; /* which IP family should the resolver use when both are returned */ |
Baptiste Assmann | 189363e | 2015-09-03 10:55:20 +0200 | [diff] [blame] | 158 | unsigned int last_resolution; /* time of the lastest valid resolution */ |
| 159 | unsigned int last_sent_packet; /* time of the latest DNS packet sent */ |
| 160 | unsigned int last_status_change; /* time of the latest DNS resolution status change */ |
Baptiste Assmann | 325137d | 2015-04-13 23:40:55 +0200 | [diff] [blame] | 161 | int query_id; /* DNS query ID dedicated for this resolution */ |
| 162 | struct eb32_node qid; /* ebtree query id */ |
| 163 | int query_type; /* query type to send. By default DNS_RTYPE_ANY */ |
| 164 | int status; /* status of the resolution being processed RSLV_STATUS_* */ |
| 165 | int step; /* */ |
| 166 | int try; /* current resolution try */ |
| 167 | int try_cname; /* number of CNAME requests sent */ |
| 168 | int nb_responses; /* count number of responses received */ |
| 169 | }; |
| 170 | |
| 171 | /* last resolution status code */ |
| 172 | enum { |
| 173 | RSLV_STATUS_NONE = 0, /* no resolution occured yet */ |
| 174 | RSLV_STATUS_VALID, /* no error */ |
| 175 | RSLV_STATUS_INVALID, /* invalid responses */ |
| 176 | RSLV_STATUS_ERROR, /* error */ |
| 177 | RSLV_STATUS_NX, /* NXDOMAIN */ |
| 178 | RSLV_STATUS_REFUSED, /* server refused our query */ |
| 179 | RSLV_STATUS_TIMEOUT, /* no response from DNS servers */ |
| 180 | RSLV_STATUS_OTHER, /* other errors */ |
| 181 | }; |
| 182 | |
| 183 | /* current resolution step */ |
| 184 | enum { |
| 185 | RSLV_STEP_NONE = 0, /* nothing happening currently */ |
| 186 | RSLV_STEP_RUNNING, /* resolution is running */ |
| 187 | }; |
| 188 | |
| 189 | /* return codes after analyzing a DNS response */ |
| 190 | enum { |
| 191 | DNS_RESP_VALID = 0, /* valid response */ |
| 192 | DNS_RESP_INVALID, /* invalid response (various type of errors can trigger it) */ |
| 193 | DNS_RESP_ERROR, /* DNS error code */ |
| 194 | DNS_RESP_NX_DOMAIN, /* resolution unsuccessful */ |
| 195 | DNS_RESP_REFUSED, /* DNS server refused to answer */ |
| 196 | DNS_RESP_ANCOUNT_ZERO, /* no answers in the response */ |
| 197 | DNS_RESP_WRONG_NAME, /* response does not match query name */ |
| 198 | DNS_RESP_CNAME_ERROR, /* error when resolving a CNAME in an atomic response */ |
| 199 | DNS_RESP_TIMEOUT, /* DNS server has not answered in time */ |
| 200 | }; |
| 201 | |
| 202 | /* return codes after searching an IP in a DNS response buffer, using a family preference */ |
| 203 | enum { |
| 204 | DNS_UPD_NO = 1, /* provided IP was found and preference is matched |
| 205 | * OR provided IP found and preference is not matched, but no IP |
| 206 | * matching preference was found */ |
| 207 | DNS_UPD_SRVIP_NOT_FOUND, /* provided IP not found |
| 208 | * OR provided IP found and preference is not match and an IP |
| 209 | * matching preference was found */ |
| 210 | DNS_UPD_CNAME, /* CNAME without any IP provided in the response */ |
| 211 | DNS_UPD_NAME_ERROR, /* name in the response did not match the query */ |
| 212 | }; |
| 213 | |
| 214 | #endif /* _TYPES_DNS_H */ |