dns.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*************************************************************************
  2. *
  3. * Copyright (C) 2018-2020 Ruilin Peng (Nick) <[email protected]>.
  4. *
  5. * smartdns is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * smartdns is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef _DNS_HEAD_H
  19. #define _DNS_HEAD_H
  20. #define DNS_RR_A_LEN 4
  21. #define DNS_RR_AAAA_LEN 16
  22. #define DNS_MAX_CNAME_LEN 256
  23. #define DNS_MAX_OPT_LEN 256
  24. #define DNS_IN_PACKSIZE (512 * 8)
  25. #define DNS_PACKSIZE (512 * 12)
  26. #define DNS_DEFAULT_PACKET_SIZE 512
  27. #define DNS_ADDR_FAMILY_IP 1
  28. #define DNS_ADDR_FAMILY_IPV6 2
  29. /*
  30. DNS parameters:
  31. https://www.iana.org/assignments/dns-parameters/dns-parameters.xhtml
  32. */
  33. typedef enum dns_qr {
  34. DNS_QR_QUERY = 0,
  35. DNS_QR_ANSWER = 1,
  36. } dns_qr;
  37. typedef enum dns_rr_type {
  38. DNS_RRS_QD = 0,
  39. DNS_RRS_AN = 1,
  40. DNS_RRS_NS = 2,
  41. DNS_RRS_NR = 3,
  42. DNS_RRS_OPT = 4,
  43. DNS_RRS_END,
  44. } dns_rr_type;
  45. typedef enum dns_class {
  46. DNS_C_IN = 1, // DNS C IN
  47. DNS_C_ANY = 255
  48. } dns_class_t;
  49. typedef enum dns_type {
  50. DNS_T_A = 1,
  51. DNS_T_NS = 2,
  52. DNS_T_CNAME = 5,
  53. DNS_T_SOA = 6,
  54. DNS_T_PTR = 12,
  55. DNS_T_MX = 15,
  56. DNS_T_TXT = 16,
  57. DNS_T_AAAA = 28,
  58. DNS_T_SRV = 33,
  59. DNS_T_OPT = 41,
  60. DNS_T_SSHFP = 44,
  61. DNS_T_HTTPS = 65,
  62. DNS_T_SPF = 99,
  63. DNS_T_AXFR = 252,
  64. DNS_T_ALL = 255
  65. } dns_type_t;
  66. typedef enum dns_opt_code {
  67. DNS_OPT_T_ECS = 8, // OPT ECS
  68. DNS_OPT_T_COOKIE = 10, //OPT Cookie
  69. DNS_OPT_T_TCP_KEEPALIVE = 11,
  70. DNS_OPT_T_PADDING = 12,
  71. DNS_OPT_T_ALL = 255
  72. } dns_opt_code_t;
  73. typedef enum dns_opcode {
  74. DNS_OP_QUERY = 0,
  75. DNS_OP_IQUERY = 1,
  76. DNS_OP_STATUS = 2,
  77. DNS_OP_NOTIFY = 4,
  78. DNS_OP_UPDATE = 5,
  79. } dns_opcode_t; /* dns_opcode */
  80. typedef enum dns_rtcode {
  81. DNS_RC_NOERROR = 0,
  82. DNS_RC_FORMERR = 1,
  83. DNS_RC_SERVFAIL = 2,
  84. DNS_RC_NXDOMAIN = 3,
  85. DNS_RC_NOTIMP = 4,
  86. DNS_RC_REFUSED = 5,
  87. DNS_RC_YXDOMAIN = 6,
  88. DNS_RC_YXRRSET = 7,
  89. DNS_RC_NXRRSET = 8,
  90. DNS_RC_NOTAUTH = 9,
  91. DNS_RC_NOTZONE = 10,
  92. /* EDNS(0) extended RCODEs */
  93. DNS_RC_BADVERS = 16,
  94. } dns_rtcode_t; /* dns_rcode */
  95. /* dns packet head */
  96. struct dns_head {
  97. unsigned short id; /* identification number */
  98. unsigned short qr; /* Query/Response Flag */
  99. unsigned short opcode; /* Operation Code */
  100. unsigned char aa; /* Authoritative Answer Flag */
  101. unsigned char tc; /* Truncation Flag */
  102. unsigned char rd; /* Recursion Desired */
  103. unsigned char ra; /* Recursion Available */
  104. unsigned short rcode; /* Response Code */
  105. unsigned short qdcount; /* number of question entries */
  106. unsigned short ancount; /* number of answer entries */
  107. unsigned short nscount; /* number of authority entries */
  108. unsigned short nrcount; /* number of addititional resource entries */
  109. } __attribute__((packed, aligned(2)));
  110. #define DNS_PACKET_DICT_SIZE 16
  111. struct dns_packet_dict_item {
  112. unsigned short pos;
  113. unsigned int hash;
  114. };
  115. struct dns_packet_dict {
  116. short dict_count;
  117. struct dns_packet_dict_item names[DNS_PACKET_DICT_SIZE];
  118. };
  119. /* packet haed */
  120. struct dns_packet {
  121. struct dns_head head;
  122. unsigned short questions;
  123. unsigned short answers;
  124. unsigned short nameservers;
  125. unsigned short additional;
  126. unsigned short optcount;
  127. unsigned short optional;
  128. unsigned short payloadsize;
  129. struct dns_packet_dict namedict;
  130. int size;
  131. int len;
  132. unsigned char data[0];
  133. };
  134. struct dns_rrs {
  135. struct dns_packet *packet;
  136. unsigned short next;
  137. unsigned short len;
  138. int type;
  139. unsigned char data[0];
  140. };
  141. /* packet encode/decode context */
  142. struct dns_context {
  143. struct dns_packet *packet;
  144. struct dns_packet_dict *namedict;
  145. unsigned char *data;
  146. int maxsize;
  147. unsigned char *ptr;
  148. };
  149. /* SOA data */
  150. struct dns_soa {
  151. char mname[DNS_MAX_CNAME_LEN];
  152. char rname[DNS_MAX_CNAME_LEN];
  153. unsigned int serial;
  154. unsigned int refresh;
  155. unsigned int retry;
  156. unsigned int expire;
  157. unsigned int minimum;
  158. } __attribute__((packed));
  159. #define DNS_OPT_ECS_FAMILY_IPV4 1
  160. #define DNS_OPT_ECS_FAMILY_IPV6 2
  161. /* OPT ECS */
  162. struct dns_opt_ecs {
  163. unsigned short family;
  164. unsigned char source_prefix;
  165. unsigned char scope_prefix;
  166. unsigned char addr[DNS_RR_AAAA_LEN];
  167. } __attribute__((packed));;
  168. /* OPT COOLIE */
  169. struct dns_opt_cookie {
  170. char server_cookie_len;
  171. unsigned char client_cookie[8];
  172. unsigned char server_cookie[32];
  173. };
  174. /* OPT */
  175. struct dns_opt {
  176. unsigned short code;
  177. unsigned short length;
  178. unsigned char data[0];
  179. } __attribute__((packed));
  180. struct dns_rrs *dns_get_rrs_next(struct dns_packet *packet, struct dns_rrs *rrs);
  181. struct dns_rrs *dns_get_rrs_start(struct dns_packet *packet, dns_rr_type type, int *count);
  182. /*
  183. * Question
  184. */
  185. int dns_add_domain(struct dns_packet *packet, const char *domain, int qtype, int qclass);
  186. int dns_get_domain(struct dns_rrs *rrs, char *domain, int maxsize, int *qtype, int *qclass);
  187. /*
  188. * Answers
  189. */
  190. int dns_add_CNAME(struct dns_packet *packet, dns_rr_type type, const char *domain, int ttl, const char *cname);
  191. int dns_get_CNAME(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size);
  192. int dns_add_A(struct dns_packet *packet, dns_rr_type type, const char *domain, int ttl, unsigned char addr[DNS_RR_A_LEN]);
  193. int dns_get_A(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[DNS_RR_A_LEN]);
  194. int dns_add_PTR(struct dns_packet *packet, dns_rr_type type, const char *domain, int ttl, char *cname);
  195. int dns_get_PTR(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size);
  196. int dns_add_AAAA(struct dns_packet *packet, dns_rr_type type, const char *domain, int ttl,
  197. unsigned char addr[DNS_RR_AAAA_LEN]);
  198. int dns_get_AAAA(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, unsigned char addr[DNS_RR_AAAA_LEN]);
  199. int dns_add_SOA(struct dns_packet *packet, dns_rr_type type, const char *domain, int ttl, struct dns_soa *soa);
  200. int dns_get_SOA(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, struct dns_soa *soa);
  201. int dns_add_NS(struct dns_packet *packet, dns_rr_type type, const char *domain, int ttl, const char *cname);
  202. int dns_get_NS(struct dns_rrs *rrs, char *domain, int maxsize, int *ttl, char *cname, int cname_size);
  203. int dns_set_OPT_payload_size(struct dns_packet *packet, int payload_size);
  204. int dns_get_OPT_payload_size(struct dns_packet *packet);
  205. int dns_add_OPT_ECS(struct dns_packet *packet, struct dns_opt_ecs *ecs);
  206. int dns_get_OPT_ECS(struct dns_rrs *rrs, unsigned short *opt_code, unsigned short *opt_len, struct dns_opt_ecs *ecs);
  207. int dns_add_OPT_TCP_KEEYALIVE(struct dns_packet *packet, unsigned short timeout);
  208. int dns_get_OPT_TCP_KEEYALIVE(struct dns_rrs *rrs, unsigned short *opt_code, unsigned short *opt_len,
  209. unsigned short *timeout);
  210. /*
  211. * Packet operation
  212. */
  213. int dns_decode(struct dns_packet *packet, int maxsize, unsigned char *data, int size);
  214. int dns_encode(unsigned char *data, int size, struct dns_packet *packet);
  215. int dns_packet_init(struct dns_packet *packet, int size, struct dns_head *head);
  216. struct dns_update_param {
  217. int id;
  218. int ip_ttl;
  219. int cname_ttl;
  220. };
  221. int dns_packet_update(unsigned char *data, int size, struct dns_update_param *param);
  222. #endif