blob: 0aced3ce6952077826af101921945629c3754cdc [file] [log] [blame]
Simon Kelleyc47e3ba2014-01-08 17:07:54 +00001/* dnsmasq is Copyright (c) 2000-2014 Simon Kelley
Simon Kelley572b41e2011-02-18 18:11:18 +00002
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; version 2 dated June, 1991, or
6 (at your option) version 3 dated 29 June, 2007.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12
13 You should have received a copy of the GNU General Public License
14 along with this program. If not, see <http://www.gnu.org/licenses/>.
15*/
16
Simon Kelleyc72daea2012-01-05 21:33:27 +000017#define NAMESERVER_PORT 53
18#define TFTP_PORT 69
19
Simon Kelley572b41e2011-02-18 18:11:18 +000020#define IN6ADDRSZ 16
21#define INADDRSZ 4
22
23#define PACKETSZ 512 /* maximum packet size */
24#define MAXDNAME 1025 /* maximum presentation domain name */
25#define RRFIXEDSZ 10 /* #/bytes of fixed data in r record */
26#define MAXLABEL 63 /* maximum length of domain label */
27
28#define NOERROR 0 /* no error */
29#define FORMERR 1 /* format error */
30#define SERVFAIL 2 /* server failure */
31#define NXDOMAIN 3 /* non existent domain */
32#define NOTIMP 4 /* not implemented */
33#define REFUSED 5 /* query refused */
34
35#define QUERY 0 /* opcode */
36
37#define C_IN 1 /* the arpa internet */
38#define C_CHAOS 3 /* for chaos net (MIT) */
Simon Kelleycbf13a22014-01-25 17:59:14 +000039#define C_HESIOD 4 /* hesiod */
Simon Kelley572b41e2011-02-18 18:11:18 +000040#define C_ANY 255 /* wildcard match */
41
42#define T_A 1
Simon Kelley5f8e58f2014-01-09 17:31:19 +000043#define T_NS 2
44#define T_MD 3
45#define T_MF 4
Simon Kelley572b41e2011-02-18 18:11:18 +000046#define T_CNAME 5
47#define T_SOA 6
Simon Kelley5f8e58f2014-01-09 17:31:19 +000048#define T_MB 7
49#define T_MG 8
50#define T_MR 9
Simon Kelley572b41e2011-02-18 18:11:18 +000051#define T_PTR 12
Simon Kelley5f8e58f2014-01-09 17:31:19 +000052#define T_MINFO 14
Simon Kelley572b41e2011-02-18 18:11:18 +000053#define T_MX 15
54#define T_TXT 16
Simon Kelley5f8e58f2014-01-09 17:31:19 +000055#define T_RP 17
56#define T_AFSDB 18
57#define T_RT 21
Simon Kelley572b41e2011-02-18 18:11:18 +000058#define T_SIG 24
Simon Kelley5f8e58f2014-01-09 17:31:19 +000059#define T_PX 26
Simon Kelley572b41e2011-02-18 18:11:18 +000060#define T_AAAA 28
Simon Kelley5f8e58f2014-01-09 17:31:19 +000061#define T_NXT 30
Simon Kelley572b41e2011-02-18 18:11:18 +000062#define T_SRV 33
63#define T_NAPTR 35
Simon Kelley5f8e58f2014-01-09 17:31:19 +000064#define T_KX 36
65#define T_DNAME 39
Simon Kelley572b41e2011-02-18 18:11:18 +000066#define T_OPT 41
Giovanni Bajoa669f012012-04-05 02:47:28 +020067#define T_DS 43
68#define T_RRSIG 46
69#define T_NSEC 47
70#define T_DNSKEY 48
Simon Kelley5107ace2014-02-23 10:48:32 +000071#define T_NSEC3 50
Simon Kelley572b41e2011-02-18 18:11:18 +000072#define T_TKEY 249
73#define T_TSIG 250
Simon Kelleyb75e9362012-12-07 11:50:41 +000074#define T_AXFR 252
Simon Kelley572b41e2011-02-18 18:11:18 +000075#define T_MAILB 253
76#define T_ANY 255
77
Simon Kelleyed4c0762013-10-08 20:46:34 +010078#define EDNS0_OPTION_MAC 65001 /* dyndns.org temporary assignment */
Simon Kelleyc3edf382013-10-10 21:09:15 +010079#define EDNS0_OPTION_CLIENT_SUBNET 8 /* IANA */
Simon Kelleyed4c0762013-10-08 20:46:34 +010080
Simon Kelley572b41e2011-02-18 18:11:18 +000081struct dns_header {
82 u16 id;
83 u8 hb3,hb4;
84 u16 qdcount,ancount,nscount,arcount;
Simon Kelley7de060b2011-08-26 17:24:52 +010085};
Simon Kelley572b41e2011-02-18 18:11:18 +000086
87#define HB3_QR 0x80
88#define HB3_OPCODE 0x78
89#define HB3_AA 0x04
90#define HB3_TC 0x02
91#define HB3_RD 0x01
92
93#define HB4_RA 0x80
94#define HB4_AD 0x20
95#define HB4_CD 0x10
96#define HB4_RCODE 0x0f
97
98#define OPCODE(x) (((x)->hb3 & HB3_OPCODE) >> 3)
Simon Kelley9d633042013-12-13 15:36:55 +000099#define SET_OPCODE(x, code) (x)->hb3 = ((x)->hb3 & ~HB3_OPCODE) | code
100
Simon Kelley572b41e2011-02-18 18:11:18 +0000101#define RCODE(x) ((x)->hb4 & HB4_RCODE)
102#define SET_RCODE(x, code) (x)->hb4 = ((x)->hb4 & ~HB4_RCODE) | code
103
104#define GETSHORT(s, cp) { \
105 unsigned char *t_cp = (unsigned char *)(cp); \
106 (s) = ((u16)t_cp[0] << 8) \
107 | ((u16)t_cp[1]) \
108 ; \
109 (cp) += 2; \
110}
111
112#define GETLONG(l, cp) { \
113 unsigned char *t_cp = (unsigned char *)(cp); \
114 (l) = ((u32)t_cp[0] << 24) \
115 | ((u32)t_cp[1] << 16) \
116 | ((u32)t_cp[2] << 8) \
117 | ((u32)t_cp[3]) \
118 ; \
119 (cp) += 4; \
120}
121
122#define PUTSHORT(s, cp) { \
123 u16 t_s = (u16)(s); \
124 unsigned char *t_cp = (unsigned char *)(cp); \
125 *t_cp++ = t_s >> 8; \
126 *t_cp = t_s; \
127 (cp) += 2; \
128}
129
130#define PUTLONG(l, cp) { \
131 u32 t_l = (u32)(l); \
132 unsigned char *t_cp = (unsigned char *)(cp); \
133 *t_cp++ = t_l >> 24; \
134 *t_cp++ = t_l >> 16; \
135 *t_cp++ = t_l >> 8; \
136 *t_cp = t_l; \
137 (cp) += 4; \
138}
139
Simon Kelleyc3e0b9b2013-12-31 13:50:39 +0000140#define CHECK_LEN(header, pp, plen, len) \
141 ((size_t)((pp) - (unsigned char *)(header) + (len)) <= (plen))
142
143#define ADD_RDLEN(header, pp, plen, len) \
144 (!CHECK_LEN(header, pp, plen, len) ? 0 : (((pp) += (len)), 1))