nameser.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  31. * Copyright (c) 1996-1999 by Internet Software Consortium.
  32. *
  33. * Permission to use, copy, modify, and distribute this software for any
  34. * purpose with or without fee is hereby granted, provided that the above
  35. * copyright notice and this permission notice appear in all copies.
  36. *
  37. * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
  38. * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
  40. * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  41. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  42. * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  43. * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  44. * SOFTWARE.
  45. */
  46. #ifndef _ARPA_NAMESER_H_
  47. #define _ARPA_NAMESER_H_
  48. #include <sys/param.h>
  49. #include <sys/types.h>
  50. #include <stdint.h>
  51. /*
  52. * Define constants based on RFC 883, RFC 1034, RFC 1035
  53. */
  54. #define NS_PACKETSZ 512 /*%< default UDP packet size */
  55. #define NS_MAXDNAME 1025 /*%< maximum domain name */
  56. #define NS_MAXMSG 65535 /*%< maximum message size */
  57. #define NS_MAXCDNAME 255 /*%< maximum compressed domain name */
  58. #define NS_MAXLABEL 63 /*%< maximum length of domain label */
  59. #define NS_HFIXEDSZ 12 /*%< #/bytes of fixed data in header */
  60. #define NS_QFIXEDSZ 4 /*%< #/bytes of fixed data in query */
  61. #define NS_RRFIXEDSZ 10 /*%< #/bytes of fixed data in r record */
  62. #define NS_INT32SZ 4 /*%< #/bytes of data in a uint32_t */
  63. #define NS_INT16SZ 2 /*%< #/bytes of data in a uint16_t */
  64. #define NS_INT8SZ 1 /*%< #/bytes of data in a uint8_t */
  65. #define NS_INADDRSZ 4 /*%< IPv4 T_A */
  66. #define NS_IN6ADDRSZ 16 /*%< IPv6 T_AAAA */
  67. #define NS_CMPRSFLGS 0xc0 /*%< Flag bits indicating name compression. */
  68. #define NS_DEFAULTPORT 53 /*%< For both TCP and UDP. */
  69. /*
  70. * These can be expanded with synonyms, just keep ns_parse.c:ns_parserecord()
  71. * in synch with it.
  72. */
  73. typedef enum __ns_sect {
  74. ns_s_qd = 0, /*%< Query: Question. */
  75. ns_s_zn = 0, /*%< Update: Zone. */
  76. ns_s_an = 1, /*%< Query: Answer. */
  77. ns_s_pr = 1, /*%< Update: Prerequisites. */
  78. ns_s_ns = 2, /*%< Query: Name servers. */
  79. ns_s_ud = 2, /*%< Update: Update. */
  80. ns_s_ar = 3, /*%< Query|Update: Additional records. */
  81. ns_s_max = 4
  82. } ns_sect;
  83. /*%
  84. * This is a message handle. It is caller allocated and has no dynamic data.
  85. * This structure is intended to be opaque to all but ns_parse.c, thus the
  86. * leading _'s on the member names. Use the accessor functions, not the _'s.
  87. */
  88. typedef struct __ns_msg {
  89. const unsigned char *_msg, *_eom;
  90. uint16_t _id, _flags, _counts[ns_s_max];
  91. const unsigned char *_sections[ns_s_max];
  92. ns_sect _sect;
  93. int _rrnum;
  94. const unsigned char *_msg_ptr;
  95. } ns_msg;
  96. /* Private data structure - do not use from outside library. */
  97. struct _ns_flagdata { int mask, shift; };
  98. extern const struct _ns_flagdata _ns_flagdata[];
  99. /* Accessor macros - this is part of the public interface. */
  100. #define ns_msg_id(handle) ((handle)._id + 0)
  101. #define ns_msg_base(handle) ((handle)._msg + 0)
  102. #define ns_msg_end(handle) ((handle)._eom + 0)
  103. #define ns_msg_size(handle) ((handle)._eom - (handle)._msg)
  104. #define ns_msg_count(handle, section) ((handle)._counts[section] + 0)
  105. /*%
  106. * This is a parsed record. It is caller allocated and has no dynamic data.
  107. */
  108. typedef struct __ns_rr {
  109. char name[NS_MAXDNAME];
  110. uint16_t type;
  111. uint16_t rr_class;
  112. uint32_t ttl;
  113. uint16_t rdlength;
  114. const unsigned char * rdata;
  115. } ns_rr;
  116. /* Accessor macros - this is part of the public interface. */
  117. #define ns_rr_name(rr) (((rr).name[0] != '\0') ? (rr).name : ".")
  118. #define ns_rr_type(rr) ((ns_type)((rr).type + 0))
  119. #define ns_rr_class(rr) ((ns_class)((rr).rr_class + 0))
  120. #define ns_rr_ttl(rr) ((rr).ttl + 0)
  121. #define ns_rr_rdlen(rr) ((rr).rdlength + 0)
  122. #define ns_rr_rdata(rr) ((rr).rdata + 0)
  123. /*%
  124. * These don't have to be in the same order as in the packet flags word,
  125. * and they can even overlap in some cases, but they will need to be kept
  126. * in synch with ns_parse.c:ns_flagdata[].
  127. */
  128. typedef enum __ns_flag {
  129. ns_f_qr, /*%< Question/Response. */
  130. ns_f_opcode, /*%< Operation code. */
  131. ns_f_aa, /*%< Authoritative Answer. */
  132. ns_f_tc, /*%< Truncation occurred. */
  133. ns_f_rd, /*%< Recursion Desired. */
  134. ns_f_ra, /*%< Recursion Available. */
  135. ns_f_z, /*%< MBZ. */
  136. ns_f_ad, /*%< Authentic Data (DNSSEC). */
  137. ns_f_cd, /*%< Checking Disabled (DNSSEC). */
  138. ns_f_rcode, /*%< Response code. */
  139. ns_f_max
  140. } ns_flag;
  141. /*%
  142. * Currently defined opcodes.
  143. */
  144. typedef enum __ns_opcode {
  145. ns_o_query = 0, /*%< Standard query. */
  146. ns_o_iquery = 1, /*%< Inverse query (deprecated/unsupported). */
  147. ns_o_status = 2, /*%< Name server status query (unsupported). */
  148. /* Opcode 3 is undefined/reserved. */
  149. ns_o_notify = 4, /*%< Zone change notification. */
  150. ns_o_update = 5, /*%< Zone update message. */
  151. ns_o_max = 6
  152. } ns_opcode;
  153. /*%
  154. * Currently defined response codes.
  155. */
  156. typedef enum __ns_rcode {
  157. ns_r_noerror = 0, /*%< No error occurred. */
  158. ns_r_formerr = 1, /*%< Format error. */
  159. ns_r_servfail = 2, /*%< Server failure. */
  160. ns_r_nxdomain = 3, /*%< Name error. */
  161. ns_r_notimpl = 4, /*%< Unimplemented. */
  162. ns_r_refused = 5, /*%< Operation refused. */
  163. /* these are for BIND_UPDATE */
  164. ns_r_yxdomain = 6, /*%< Name exists */
  165. ns_r_yxrrset = 7, /*%< RRset exists */
  166. ns_r_nxrrset = 8, /*%< RRset does not exist */
  167. ns_r_notauth = 9, /*%< Not authoritative for zone */
  168. ns_r_notzone = 10, /*%< Zone of record different from zone section */
  169. ns_r_max = 11,
  170. /* The following are EDNS extended rcodes */
  171. ns_r_badvers = 16,
  172. /* The following are TSIG errors */
  173. ns_r_badsig = 16,
  174. ns_r_badkey = 17,
  175. ns_r_badtime = 18
  176. } ns_rcode;
  177. /* BIND_UPDATE */
  178. typedef enum __ns_update_operation {
  179. ns_uop_delete = 0,
  180. ns_uop_add = 1,
  181. ns_uop_max = 2
  182. } ns_update_operation;
  183. /*%
  184. * This structure is used for TSIG authenticated messages
  185. */
  186. struct ns_tsig_key {
  187. char name[NS_MAXDNAME], alg[NS_MAXDNAME];
  188. unsigned char *data;
  189. int len;
  190. };
  191. typedef struct ns_tsig_key ns_tsig_key;
  192. /*%
  193. * This structure is used for TSIG authenticated TCP messages
  194. */
  195. struct ns_tcp_tsig_state {
  196. int counter;
  197. struct dst_key *key;
  198. void *ctx;
  199. unsigned char sig[NS_PACKETSZ];
  200. int siglen;
  201. };
  202. typedef struct ns_tcp_tsig_state ns_tcp_tsig_state;
  203. #define NS_TSIG_FUDGE 300
  204. #define NS_TSIG_TCP_COUNT 100
  205. #define NS_TSIG_ALG_HMAC_MD5 "HMAC-MD5.SIG-ALG.REG.INT"
  206. #define NS_TSIG_ERROR_NO_TSIG -10
  207. #define NS_TSIG_ERROR_NO_SPACE -11
  208. #define NS_TSIG_ERROR_FORMERR -12
  209. /*%
  210. * Currently defined type values for resources and queries.
  211. */
  212. typedef enum __ns_type
  213. {
  214. ns_t_invalid = 0,
  215. ns_t_a = 1,
  216. ns_t_ns = 2,
  217. ns_t_md = 3,
  218. ns_t_mf = 4,
  219. ns_t_cname = 5,
  220. ns_t_soa = 6,
  221. ns_t_mb = 7,
  222. ns_t_mg = 8,
  223. ns_t_mr = 9,
  224. ns_t_null = 10,
  225. ns_t_wks = 11,
  226. ns_t_ptr = 12,
  227. ns_t_hinfo = 13,
  228. ns_t_minfo = 14,
  229. ns_t_mx = 15,
  230. ns_t_txt = 16,
  231. ns_t_rp = 17,
  232. ns_t_afsdb = 18,
  233. ns_t_x25 = 19,
  234. ns_t_isdn = 20,
  235. ns_t_rt = 21,
  236. ns_t_nsap = 22,
  237. ns_t_nsap_ptr = 23,
  238. ns_t_sig = 24,
  239. ns_t_key = 25,
  240. ns_t_px = 26,
  241. ns_t_gpos = 27,
  242. ns_t_aaaa = 28,
  243. ns_t_loc = 29,
  244. ns_t_nxt = 30,
  245. ns_t_eid = 31,
  246. ns_t_nimloc = 32,
  247. ns_t_srv = 33,
  248. ns_t_atma = 34,
  249. ns_t_naptr = 35,
  250. ns_t_kx = 36,
  251. ns_t_cert = 37,
  252. ns_t_a6 = 38,
  253. ns_t_dname = 39,
  254. ns_t_sink = 40,
  255. ns_t_opt = 41,
  256. ns_t_apl = 42,
  257. ns_t_ds = 43,
  258. ns_t_sshfp = 44,
  259. ns_t_ipseckey = 45,
  260. ns_t_rrsig = 46,
  261. ns_t_nsec = 47,
  262. ns_t_dnskey = 48,
  263. ns_t_dhcid = 49,
  264. ns_t_nsec3 = 50,
  265. ns_t_nsec3param = 51,
  266. ns_t_tlsa = 52,
  267. ns_t_smimea = 53,
  268. ns_t_hip = 55,
  269. ns_t_ninfo = 56,
  270. ns_t_rkey = 57,
  271. ns_t_talink = 58,
  272. ns_t_cds = 59,
  273. ns_t_cdnskey = 60,
  274. ns_t_openpgpkey = 61,
  275. ns_t_csync = 62,
  276. ns_t_spf = 99,
  277. ns_t_uinfo = 100,
  278. ns_t_uid = 101,
  279. ns_t_gid = 102,
  280. ns_t_unspec = 103,
  281. ns_t_nid = 104,
  282. ns_t_l32 = 105,
  283. ns_t_l64 = 106,
  284. ns_t_lp = 107,
  285. ns_t_eui48 = 108,
  286. ns_t_eui64 = 109,
  287. ns_t_tkey = 249,
  288. ns_t_tsig = 250,
  289. ns_t_ixfr = 251,
  290. ns_t_axfr = 252,
  291. ns_t_mailb = 253,
  292. ns_t_maila = 254,
  293. ns_t_any = 255,
  294. ns_t_uri = 256,
  295. ns_t_caa = 257,
  296. ns_t_avc = 258,
  297. ns_t_ta = 32768,
  298. ns_t_dlv = 32769,
  299. ns_t_max = 65536
  300. } ns_type;
  301. /*%
  302. * Values for class field
  303. */
  304. typedef enum __ns_class {
  305. ns_c_invalid = 0, /*%< Cookie. */
  306. ns_c_in = 1, /*%< Internet. */
  307. ns_c_2 = 2, /*%< unallocated/unsupported. */
  308. ns_c_chaos = 3, /*%< MIT Chaos-net. */
  309. ns_c_hs = 4, /*%< MIT Hesiod. */
  310. /* Query class values which do not appear in resource records */
  311. ns_c_none = 254, /*%< for prereq. sections in update requests */
  312. ns_c_any = 255, /*%< Wildcard match. */
  313. ns_c_max = 65536
  314. } ns_class;
  315. /* Certificate type values in CERT resource records. */
  316. typedef enum __ns_cert_types {
  317. cert_t_pkix = 1, /*%< PKIX (X.509v3) */
  318. cert_t_spki = 2, /*%< SPKI */
  319. cert_t_pgp = 3, /*%< PGP */
  320. cert_t_url = 253, /*%< URL private type */
  321. cert_t_oid = 254 /*%< OID private type */
  322. } ns_cert_types;
  323. /*%
  324. * EDNS0 extended flags and option codes, host order.
  325. */
  326. #define NS_OPT_DNSSEC_OK 0x8000U
  327. #define NS_OPT_NSID 3
  328. /*%
  329. * Inline versions of get/put short/long. Pointer is advanced.
  330. */
  331. #define NS_GET16(s, cp) do { \
  332. const unsigned char *t_cp = (const unsigned char *)(cp); \
  333. (s) = ((uint16_t)t_cp[0] << 8) \
  334. | ((uint16_t)t_cp[1]) \
  335. ; \
  336. (cp) += NS_INT16SZ; \
  337. } while (0)
  338. #define NS_GET32(l, cp) do { \
  339. const unsigned char *t_cp = (const unsigned char *)(cp); \
  340. (l) = ((uint32_t)t_cp[0] << 24) \
  341. | ((uint32_t)t_cp[1] << 16) \
  342. | ((uint32_t)t_cp[2] << 8) \
  343. | ((uint32_t)t_cp[3]) \
  344. ; \
  345. (cp) += NS_INT32SZ; \
  346. } while (0)
  347. #define NS_PUT16(s, cp) do { \
  348. uint16_t t_s = (uint16_t)(s); \
  349. unsigned char *t_cp = (unsigned char *)(cp); \
  350. *t_cp++ = t_s >> 8; \
  351. *t_cp = t_s; \
  352. (cp) += NS_INT16SZ; \
  353. } while (0)
  354. #define NS_PUT32(l, cp) do { \
  355. uint32_t t_l = (uint32_t)(l); \
  356. unsigned char *t_cp = (unsigned char *)(cp); \
  357. *t_cp++ = t_l >> 24; \
  358. *t_cp++ = t_l >> 16; \
  359. *t_cp++ = t_l >> 8; \
  360. *t_cp = t_l; \
  361. (cp) += NS_INT32SZ; \
  362. } while (0)
  363. __BEGIN_DECLS
  364. int ns_msg_getflag (ns_msg, int) __THROW;
  365. unsigned int ns_get16 (const unsigned char *) __THROW;
  366. unsigned long ns_get32 (const unsigned char *) __THROW;
  367. void ns_put16 (unsigned int, unsigned char *) __THROW;
  368. void ns_put32 (unsigned long, unsigned char *) __THROW;
  369. int ns_initparse (const unsigned char *, int, ns_msg *) __THROW;
  370. int ns_skiprr (const unsigned char *, const unsigned char *,
  371. ns_sect, int) __THROW;
  372. int ns_parserr (ns_msg *, ns_sect, int, ns_rr *) __THROW;
  373. int ns_sprintrr (const ns_msg *, const ns_rr *,
  374. const char *, const char *, char *, size_t)
  375. __THROW;
  376. int ns_sprintrrf (const unsigned char *, size_t, const char *,
  377. ns_class, ns_type, unsigned long,
  378. const unsigned char *, size_t, const char *,
  379. const char *, char *, size_t) __THROW;
  380. int ns_format_ttl (unsigned long, char *, size_t) __THROW;
  381. int ns_parse_ttl (const char *, unsigned long *) __THROW;
  382. uint32_t ns_datetosecs (const char *, int *) __THROW;
  383. int ns_name_ntol (const unsigned char *, unsigned char *, size_t)
  384. __THROW;
  385. int ns_name_ntop (const unsigned char *, char *, size_t) __THROW;
  386. int ns_name_pton (const char *, unsigned char *, size_t) __THROW;
  387. int ns_name_unpack (const unsigned char *, const unsigned char *,
  388. const unsigned char *, unsigned char *, size_t)
  389. __THROW;
  390. int ns_name_pack (const unsigned char *, unsigned char *, int,
  391. const unsigned char **, const unsigned char **)
  392. __THROW;
  393. int ns_name_uncompress (const unsigned char *,
  394. const unsigned char *,
  395. const unsigned char *,
  396. char *, size_t) __THROW;
  397. int ns_name_compress (const char *, unsigned char *, size_t,
  398. const unsigned char **,
  399. const unsigned char **) __THROW;
  400. int ns_name_skip (const unsigned char **, const unsigned char *)
  401. __THROW;
  402. void ns_name_rollback (const unsigned char *,
  403. const unsigned char **,
  404. const unsigned char **) __THROW;
  405. int ns_samedomain (const char *, const char *) __THROW;
  406. int ns_subdomain (const char *, const char *) __THROW;
  407. int ns_makecanon (const char *, char *, size_t) __THROW;
  408. int ns_samename (const char *, const char *) __THROW;
  409. __END_DECLS
  410. #include <arpa/nameser_compat.h>
  411. #endif /* !_ARPA_NAMESER_H_ */
  412. /*! \file */