nameser.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 1983, 1989, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. All advertising materials mentioning features or use of this software
  14. * must display the following acknowledgement:
  15. * This product includes software developed by the University of
  16. * California, Berkeley and its contributors.
  17. * 4. Neither the name of the University nor the names of its contributors
  18. * may be used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31. * SUCH DAMAGE.
  32. *
  33. * @(#)nameser.h 8.2 (Berkeley) 2/16/94
  34. * -
  35. * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36. *
  37. * Permission to use, copy, modify, and distribute this software for any
  38. * purpose with or without fee is hereby granted, provided that the above
  39. * copyright notice and this permission notice appear in all copies, and that
  40. * the name of Digital Equipment Corporation not be used in advertising or
  41. * publicity pertaining to distribution of the document or software without
  42. * specific, written prior permission.
  43. *
  44. * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45. * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL DIGITAL EQUIPMENT
  47. * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51. * SOFTWARE.
  52. * -
  53. * --Copyright--
  54. */
  55. #ifndef _NAMESER_H_
  56. #define _NAMESER_H_
  57. #include <sys/types.h>
  58. /*
  59. * Define constants based on rfc883
  60. */
  61. #define PACKETSZ 512 /* maximum packet size */
  62. #define MAXDNAME 256 /* maximum domain name */
  63. #define MAXCDNAME 255 /* maximum compressed domain name */
  64. #define MAXLABEL 63 /* maximum length of domain label */
  65. /* Number of bytes of fixed size data in query structure */
  66. #define QFIXEDSZ 4
  67. /* number of bytes of fixed size data in resource record */
  68. #define RRFIXEDSZ 10
  69. /*
  70. * Currently defined opcodes
  71. */
  72. #define QUERY 0x0 /* standard query */
  73. #define IQUERY 0x1 /* inverse query */
  74. #define STATUS 0x2 /* nameserver status query */
  75. #if 0
  76. #define xxx 0x3 /* 0x3 reserved */
  77. #endif
  78. /* non standard - supports ALLOW_UPDATES stuff from Mike Schwartz */
  79. #define UPDATEA 0x9 /* add resource record */
  80. #define UPDATED 0xa /* delete a specific resource record */
  81. #define UPDATEDA 0xb /* delete all named resource record */
  82. #define UPDATEM 0xc /* modify a specific resource record */
  83. #define UPDATEMA 0xd /* modify all named resource record */
  84. #define ZONEINIT 0xe /* initial zone transfer */
  85. #define ZONEREF 0xf /* incremental zone referesh */
  86. /*
  87. * Undefine various #defines from various System V-flavored OSes (Solaris,
  88. * SINIX, HP-UX) so the compiler doesn't whine that we redefine them.
  89. */
  90. #ifdef T_NULL
  91. #undef T_NULL
  92. #endif
  93. #ifdef T_OPT
  94. #undef T_OPT
  95. #endif
  96. #ifdef T_UNSPEC
  97. #undef T_UNSPEC
  98. #endif
  99. #ifdef NOERROR
  100. #undef NOERROR
  101. #endif
  102. /*
  103. * Currently defined response codes
  104. */
  105. #define NOERROR 0 /* no error */
  106. #define FORMERR 1 /* format error */
  107. #define SERVFAIL 2 /* server failure */
  108. #define NXDOMAIN 3 /* non existent domain */
  109. #define NOTIMP 4 /* not implemented */
  110. #define REFUSED 5 /* query refused */
  111. /* non standard */
  112. #define NOCHANGE 0xf /* update failed to change db */
  113. /*
  114. * Type values for resources and queries
  115. */
  116. #define T_A 1 /* host address */
  117. #define T_NS 2 /* authoritative server */
  118. #define T_MD 3 /* mail destination */
  119. #define T_MF 4 /* mail forwarder */
  120. #define T_CNAME 5 /* connonical name */
  121. #define T_SOA 6 /* start of authority zone */
  122. #define T_MB 7 /* mailbox domain name */
  123. #define T_MG 8 /* mail group member */
  124. #define T_MR 9 /* mail rename name */
  125. #define T_NULL 10 /* null resource record */
  126. #define T_WKS 11 /* well known service */
  127. #define T_PTR 12 /* domain name pointer */
  128. #define T_HINFO 13 /* host information */
  129. #define T_MINFO 14 /* mailbox information */
  130. #define T_MX 15 /* mail routing information */
  131. #define T_TXT 16 /* text strings */
  132. #define T_RP 17 /* responsible person */
  133. #define T_AFSDB 18 /* AFS cell database */
  134. #define T_X25 19 /* X_25 calling address */
  135. #define T_ISDN 20 /* ISDN calling address */
  136. #define T_RT 21 /* router */
  137. #define T_NSAP 22 /* NSAP address */
  138. #define T_NSAP_PTR 23 /* reverse lookup for NSAP */
  139. #define T_SIG 24 /* security signature */
  140. #define T_KEY 25 /* security key */
  141. #define T_PX 26 /* X.400 mail mapping */
  142. #define T_GPOS 27 /* geographical position (withdrawn) */
  143. #define T_AAAA 28 /* IP6 Address */
  144. #define T_LOC 29 /* Location Information */
  145. #define T_NXT 30 /* Next Valid Name in Zone */
  146. #define T_EID 31 /* Endpoint identifier */
  147. #define T_NIMLOC 32 /* Nimrod locator */
  148. #define T_SRV 33 /* Server selection */
  149. #define T_ATMA 34 /* ATM Address */
  150. #define T_NAPTR 35 /* Naming Authority PoinTeR */
  151. #define T_KX 36 /* Key Exchanger */
  152. #define T_CERT 37 /* Certificates in the DNS */
  153. #define T_A6 38 /* IP6 address */
  154. #define T_DNAME 39 /* non-terminal redirection */
  155. #define T_SINK 40 /* unknown */
  156. #define T_OPT 41 /* EDNS0 option (meta-RR) */
  157. #define T_APL 42 /* lists of address prefixes */
  158. #define T_DS 43 /* Delegation Signer */
  159. #define T_SSHFP 44 /* SSH Fingerprint */
  160. #define T_IPSECKEY 45 /* IPsec keying material */
  161. #define T_RRSIG 46 /* new security signature */
  162. #define T_NSEC 47 /* provable insecure information */
  163. #define T_DNSKEY 48 /* new security key */
  164. /* non standard */
  165. #define T_SPF 99 /* sender policy framework */
  166. #define T_UINFO 100 /* user (finger) information */
  167. #define T_UID 101 /* user ID */
  168. #define T_GID 102 /* group ID */
  169. #define T_UNSPEC 103 /* Unspecified format (binary data) */
  170. #define T_UNSPECA 104 /* "unspecified ascii". Ugly MIT hack */
  171. /* Query type values which do not appear in resource records */
  172. #define T_TKEY 249 /* Transaction Key [RFC2930] */
  173. #define T_TSIG 250 /* Transaction Signature [RFC2845] */
  174. #define T_IXFR 251 /* incremental transfer [RFC1995] */
  175. #define T_AXFR 252 /* transfer zone of authority */
  176. #define T_MAILB 253 /* transfer mailbox records */
  177. #define T_MAILA 254 /* transfer mail agent records */
  178. #define T_ANY 255 /* wildcard match */
  179. /*
  180. * Values for class field
  181. */
  182. #define C_IN 1 /* the arpa internet */
  183. #define C_CHAOS 3 /* for chaos net (MIT) */
  184. #define C_HS 4 /* for Hesiod name server (MIT) (XXX) */
  185. /* Query class values which do not appear in resource records */
  186. #define C_ANY 255 /* wildcard match */
  187. #define C_QU 0x8000 /* mDNS QU flag in queries */
  188. #define C_CACHE_FLUSH 0x8000 /* mDNS cache flush flag in replies */
  189. /*
  190. * Status return codes for T_UNSPEC conversion routines
  191. */
  192. #define CONV_SUCCESS 0
  193. #define CONV_OVERFLOW -1
  194. #define CONV_BADFMT -2
  195. #define CONV_BADCKSUM -3
  196. #define CONV_BADBUFLEN -4
  197. /*
  198. * Structure for query header.
  199. */
  200. typedef struct {
  201. uint16_t id; /* query identification number */
  202. uint8_t flags1; /* first byte of flags */
  203. uint8_t flags2; /* second byte of flags */
  204. uint16_t qdcount; /* number of question entries */
  205. uint16_t ancount; /* number of answer entries */
  206. uint16_t nscount; /* number of authority entries */
  207. uint16_t arcount; /* number of resource entries */
  208. } HEADER;
  209. /*
  210. * Macros for subfields of flag fields.
  211. */
  212. #define DNS_QR(np) ((np)->flags1 & 0x80) /* response flag */
  213. #define DNS_OPCODE(np) ((((np)->flags1) >> 3) & 0xF) /* purpose of message */
  214. #define DNS_AA(np) ((np)->flags1 & 0x04) /* authoritative answer */
  215. #define DNS_TC(np) ((np)->flags1 & 0x02) /* truncated message */
  216. #define DNS_RD(np) ((np)->flags1 & 0x01) /* recursion desired */
  217. #define DNS_RA(np) ((np)->flags2 & 0x80) /* recursion available */
  218. #define DNS_AD(np) ((np)->flags2 & 0x20) /* authentic data from named */
  219. #define DNS_CD(np) ((np)->flags2 & 0x10) /* checking disabled by resolver */
  220. #define DNS_RCODE(np) ((np)->flags2 & 0xF) /* response code */
  221. /*
  222. * Defines for handling compressed domain names, EDNS0 labels, etc.
  223. */
  224. #define INDIR_MASK 0xc0 /* 11.... */
  225. #define EDNS0_MASK 0x40 /* 01.... */
  226. # define EDNS0_ELT_BITLABEL 0x01
  227. /*
  228. * Structure for passing resource records around.
  229. */
  230. struct rrec {
  231. int16_t r_zone; /* zone number */
  232. int16_t r_class; /* class number */
  233. int16_t r_type; /* type number */
  234. uint32_t r_ttl; /* time to live */
  235. int r_size; /* size of data area */
  236. char *r_data; /* pointer to data */
  237. };
  238. /*
  239. * Inline versions of get/put short/long. Pointer is advanced.
  240. * We also assume that a "uint16_t" holds 2 "chars"
  241. * and that a "uint32_t" holds 4 "chars".
  242. *
  243. * These macros demonstrate the property of C whereby it can be
  244. * portable or it can be elegant but never both.
  245. */
  246. #define GETSHORT(s, cp) { \
  247. register u_char *t_cp = (u_char *)(cp); \
  248. (s) = ((uint16_t)t_cp[0] << 8) | (uint16_t)t_cp[1]; \
  249. (cp) += 2; \
  250. }
  251. #define GETLONG(l, cp) { \
  252. register u_char *t_cp = (u_char *)(cp); \
  253. (l) = (((uint32_t)t_cp[0]) << 24) \
  254. | (((uint32_t)t_cp[1]) << 16) \
  255. | (((uint32_t)t_cp[2]) << 8) \
  256. | (((uint32_t)t_cp[3])); \
  257. (cp) += 4; \
  258. }
  259. #define PUTSHORT(s, cp) { \
  260. register uint16_t t_s = (uint16_t)(s); \
  261. register u_char *t_cp = (u_char *)(cp); \
  262. *t_cp++ = t_s >> 8; \
  263. *t_cp = t_s; \
  264. (cp) += 2; \
  265. }
  266. /*
  267. * Warning: PUTLONG --no-longer-- destroys its first argument. if you
  268. * were depending on this "feature", you will lose.
  269. */
  270. #define PUTLONG(l, cp) { \
  271. register uint32_t t_l = (uint32_t)(l); \
  272. register u_char *t_cp = (u_char *)(cp); \
  273. *t_cp++ = t_l >> 24; \
  274. *t_cp++ = t_l >> 16; \
  275. *t_cp++ = t_l >> 8; \
  276. *t_cp = t_l; \
  277. (cp) += 4; \
  278. }
  279. #endif /* !_NAMESER_H_ */