dns-proto.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // -*- mode: c++ -*-
  2. #ifndef DNS_PROTO_H
  3. #define DNS_PROTO_H
  4. // Utilities for processing DNS packet contents
  5. #include "ares_setup.h"
  6. #include "ares.h"
  7. // Include ares internal file for DNS protocol constants
  8. #include "ares_nameser.h"
  9. #include <memory>
  10. #include <string>
  11. #include <vector>
  12. namespace ares {
  13. typedef unsigned char byte;
  14. std::string HexDump(std::vector<byte> data);
  15. std::string HexDump(const byte *data, int len);
  16. std::string HexDump(const char *data, int len);
  17. std::string StatusToString(int status);
  18. std::string RcodeToString(int rcode);
  19. std::string RRTypeToString(int rrtype);
  20. std::string ClassToString(int qclass);
  21. std::string AddressToString(const void* addr, int len);
  22. // Convert DNS protocol data to strings.
  23. // Note that these functions are not defensive; they assume
  24. // a validly formatted input, and so should not be used on
  25. // externally-determined inputs.
  26. std::string PacketToString(const std::vector<byte>& packet);
  27. std::string QuestionToString(const std::vector<byte>& packet,
  28. const byte** data, int* len);
  29. std::string RRToString(const std::vector<byte>& packet,
  30. const byte** data, int* len);
  31. // Manipulate DNS protocol data.
  32. void PushInt32(std::vector<byte>* data, int value);
  33. void PushInt16(std::vector<byte>* data, int value);
  34. std::vector<byte> EncodeString(const std::string& name);
  35. struct DNSQuestion {
  36. DNSQuestion(const std::string& name, int rrtype, int qclass)
  37. : name_(name), rrtype_(rrtype), qclass_(qclass) {}
  38. DNSQuestion(const std::string& name, int rrtype)
  39. : name_(name), rrtype_(rrtype), qclass_(C_IN) {}
  40. virtual ~DNSQuestion() {}
  41. virtual std::vector<byte> data() const;
  42. std::string name_;
  43. int rrtype_;
  44. int qclass_;
  45. };
  46. struct DNSRR : public DNSQuestion {
  47. DNSRR(const std::string& name, int rrtype, int qclass, int ttl)
  48. : DNSQuestion(name, rrtype, qclass), ttl_(ttl) {}
  49. DNSRR(const std::string& name, int rrtype, int ttl)
  50. : DNSQuestion(name, rrtype), ttl_(ttl) {}
  51. virtual ~DNSRR() {}
  52. virtual std::vector<byte> data() const = 0;
  53. int ttl_;
  54. };
  55. struct DNSAddressRR : public DNSRR {
  56. DNSAddressRR(const std::string& name, int rrtype, int ttl,
  57. const byte* addr, int addrlen)
  58. : DNSRR(name, rrtype, ttl), addr_(addr, addr + addrlen) {}
  59. DNSAddressRR(const std::string& name, int rrtype, int ttl,
  60. const std::vector<byte>& addr)
  61. : DNSRR(name, rrtype, ttl), addr_(addr) {}
  62. virtual std::vector<byte> data() const;
  63. std::vector<byte> addr_;
  64. };
  65. struct DNSARR : public DNSAddressRR {
  66. DNSARR(const std::string& name, int ttl, const byte* addr, int addrlen)
  67. : DNSAddressRR(name, T_A, ttl, addr, addrlen) {}
  68. DNSARR(const std::string& name, int ttl, const std::vector<byte>& addr)
  69. : DNSAddressRR(name, T_A, ttl, addr) {}
  70. };
  71. struct DNSAaaaRR : public DNSAddressRR {
  72. DNSAaaaRR(const std::string& name, int ttl, const byte* addr, int addrlen)
  73. : DNSAddressRR(name, T_AAAA, ttl, addr, addrlen) {}
  74. DNSAaaaRR(const std::string& name, int ttl, const std::vector<byte>& addr)
  75. : DNSAddressRR(name, T_AAAA, ttl, addr) {}
  76. };
  77. struct DNSSingleNameRR : public DNSRR {
  78. DNSSingleNameRR(const std::string& name, int rrtype, int ttl,
  79. const std::string& other)
  80. : DNSRR(name, rrtype, ttl), other_(other) {}
  81. virtual std::vector<byte> data() const;
  82. std::string other_;
  83. };
  84. struct DNSCnameRR : public DNSSingleNameRR {
  85. DNSCnameRR(const std::string& name, int ttl, const std::string& other)
  86. : DNSSingleNameRR(name, T_CNAME, ttl, other) {}
  87. };
  88. struct DNSNsRR : public DNSSingleNameRR {
  89. DNSNsRR(const std::string& name, int ttl, const std::string& other)
  90. : DNSSingleNameRR(name, T_NS, ttl, other) {}
  91. };
  92. struct DNSPtrRR : public DNSSingleNameRR {
  93. DNSPtrRR(const std::string& name, int ttl, const std::string& other)
  94. : DNSSingleNameRR(name, T_PTR, ttl, other) {}
  95. };
  96. struct DNSTxtRR : public DNSRR {
  97. DNSTxtRR(const std::string& name, int ttl, const std::vector<std::string>& txt)
  98. : DNSRR(name, T_TXT, ttl), txt_(txt) {}
  99. virtual std::vector<byte> data() const;
  100. std::vector<std::string> txt_;
  101. };
  102. struct DNSMxRR : public DNSRR {
  103. DNSMxRR(const std::string& name, int ttl, int pref, const std::string& other)
  104. : DNSRR(name, T_MX, ttl), pref_(pref), other_(other) {}
  105. virtual std::vector<byte> data() const;
  106. int pref_;
  107. std::string other_;
  108. };
  109. struct DNSSrvRR : public DNSRR {
  110. DNSSrvRR(const std::string& name, int ttl,
  111. int prio, int weight, int port, const std::string& target)
  112. : DNSRR(name, T_SRV, ttl), prio_(prio), weight_(weight), port_(port), target_(target) {}
  113. virtual std::vector<byte> data() const;
  114. int prio_;
  115. int weight_;
  116. int port_;
  117. std::string target_;
  118. };
  119. struct DNSUriRR : public DNSRR {
  120. DNSUriRR(const std::string& name, int ttl,
  121. int prio, int weight, const std::string& target)
  122. : DNSRR(name, T_URI, ttl), prio_(prio), weight_(weight), target_(target) {}
  123. virtual std::vector<byte> data() const;
  124. int prio_;
  125. int weight_;
  126. std::string target_;
  127. };
  128. struct DNSSoaRR : public DNSRR {
  129. DNSSoaRR(const std::string& name, int ttl,
  130. const std::string& nsname, const std::string& rname,
  131. int serial, int refresh, int retry, int expire, int minimum)
  132. : DNSRR(name, T_SOA, ttl), nsname_(nsname), rname_(rname),
  133. serial_(serial), refresh_(refresh), retry_(retry),
  134. expire_(expire), minimum_(minimum) {}
  135. virtual std::vector<byte> data() const;
  136. std::string nsname_;
  137. std::string rname_;
  138. int serial_;
  139. int refresh_;
  140. int retry_;
  141. int expire_;
  142. int minimum_;
  143. };
  144. struct DNSNaptrRR : public DNSRR {
  145. DNSNaptrRR(const std::string& name, int ttl,
  146. int order, int pref,
  147. const std::string& flags,
  148. const std::string& service,
  149. const std::string& regexp,
  150. const std::string& replacement)
  151. : DNSRR(name, T_NAPTR, ttl), order_(order), pref_(pref),
  152. flags_(flags), service_(service), regexp_(regexp), replacement_(replacement) {}
  153. virtual std::vector<byte> data() const;
  154. int order_;
  155. int pref_;
  156. std::string flags_;
  157. std::string service_;
  158. std::string regexp_;
  159. std::string replacement_;
  160. };
  161. struct DNSOption {
  162. int code_;
  163. std::vector<byte> data_;
  164. };
  165. struct DNSOptRR : public DNSRR {
  166. DNSOptRR(int extrcode, int udpsize)
  167. : DNSRR("", T_OPT, static_cast<int>(udpsize), extrcode) {}
  168. virtual std::vector<byte> data() const;
  169. std::vector<DNSOption> opts_;
  170. };
  171. struct DNSPacket {
  172. DNSPacket()
  173. : qid_(0), response_(false), opcode_(O_QUERY),
  174. aa_(false), tc_(false), rd_(false), ra_(false),
  175. z_(false), ad_(false), cd_(false), rcode_(NOERROR) {}
  176. // Convenience functions that take ownership of given pointers.
  177. DNSPacket& add_question(DNSQuestion *q) {
  178. questions_.push_back(std::unique_ptr<DNSQuestion>(q));
  179. return *this;
  180. }
  181. DNSPacket& add_answer(DNSRR *q) {
  182. answers_.push_back(std::unique_ptr<DNSRR>(q));
  183. return *this;
  184. }
  185. DNSPacket& add_auth(DNSRR *q) {
  186. auths_.push_back(std::unique_ptr<DNSRR>(q));
  187. return *this;
  188. }
  189. DNSPacket& add_additional(DNSRR *q) {
  190. adds_.push_back(std::unique_ptr<DNSRR>(q));
  191. return *this;
  192. }
  193. // Chainable setters.
  194. DNSPacket& set_qid(int qid) { qid_ = qid; return *this; }
  195. DNSPacket& set_response(bool v = true) { response_ = v; return *this; }
  196. DNSPacket& set_aa(bool v = true) { aa_ = v; return *this; }
  197. DNSPacket& set_tc(bool v = true) { tc_ = v; return *this; }
  198. DNSPacket& set_rd(bool v = true) { rd_ = v; return *this; }
  199. DNSPacket& set_ra(bool v = true) { ra_ = v; return *this; }
  200. DNSPacket& set_z(bool v = true) { z_ = v; return *this; }
  201. DNSPacket& set_ad(bool v = true) { ad_ = v; return *this; }
  202. DNSPacket& set_cd(bool v = true) { cd_ = v; return *this; }
  203. DNSPacket& set_rcode(int rcode) { rcode_ = rcode; return *this; }
  204. // Return the encoded packet.
  205. std::vector<byte> data() const;
  206. int qid_;
  207. bool response_;
  208. int opcode_;
  209. bool aa_;
  210. bool tc_;
  211. bool rd_;
  212. bool ra_;
  213. bool z_;
  214. bool ad_;
  215. bool cd_;
  216. int rcode_;
  217. std::vector<std::unique_ptr<DNSQuestion>> questions_;
  218. std::vector<std::unique_ptr<DNSRR>> answers_;
  219. std::vector<std::unique_ptr<DNSRR>> auths_;
  220. std::vector<std::unique_ptr<DNSRR>> adds_;
  221. };
  222. } // namespace ares
  223. #endif