Perform Secure-by-default or Woefully Insecure ‘DNS’ Queries
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

280 lines
6.8KB

  1. #include <Rcpp.h>
  2. #include <getdns/getdns.h>
  3. #include <getdns/getdns_extra.h>
  4. #include <arpa/inet.h>
  5. using namespace Rcpp;
  6. //' Test whether an object is an external pointer
  7. //'
  8. //' @param x object to test
  9. //' @keywords internal
  10. // [[Rcpp::export]]
  11. void check_is_xptr(SEXP s) {
  12. if (TYPEOF(s) != EXTPTRSXP) {
  13. Rf_error("expected an externalptr");
  14. }
  15. }
  16. //' Test whether an external pointer is null
  17. //'
  18. //' @param x object to test
  19. //' @keywords internal
  20. // [[Rcpp::export]]
  21. SEXP is_null_xptr_(SEXP s) {
  22. check_is_xptr(s);
  23. void *r = (void *)R_ExternalPtrAddr(s);
  24. return wrap(r == NULL);
  25. }
  26. static void gctx_finalizer(SEXP ptr) {
  27. if(!R_ExternalPtrAddr(ptr)) return;
  28. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(ptr);
  29. if (ptr) getdns_context_destroy(ctxt);
  30. R_ClearExternalPtr(ptr); /* not really needed */
  31. }
  32. //' Internal version of gdns_context
  33. //' @keywords internal
  34. // [[Rcpp::export]]
  35. SEXP int_gdns_context(std::vector< std::string > resolvers) {
  36. bool ok = false;
  37. SEXP ptr;
  38. getdns_return_t r;
  39. getdns_context *ctxt = NULL;
  40. // TODO Validate we don't need to free these
  41. getdns_dict *resolver_dict = getdns_dict_create();
  42. getdns_list *resolver_list = getdns_list_create();
  43. for (int i = 0; i<resolvers.size(); i++) {
  44. r = getdns_str2dict(resolvers[i].c_str(), &resolver_dict);
  45. r = getdns_list_set_dict(resolver_list, i, resolver_dict);
  46. }
  47. getdns_transport_list_t tls_transport[] = { GETDNS_TRANSPORT_TLS };
  48. if ((r = getdns_context_create(&ctxt, 1))) {
  49. } else if ((r = getdns_context_set_dns_transport_list(ctxt, 1, tls_transport))) {
  50. } else if ((r = getdns_context_set_upstream_recursive_servers(ctxt, resolver_list))) {
  51. } else if ((r = getdns_context_set_resolution_type(ctxt, GETDNS_RESOLUTION_STUB))) {
  52. } else {
  53. ok = true;
  54. }
  55. if (ok) {
  56. ptr = R_MakeExternalPtr(ctxt, Rf_install("gctx"), R_NilValue);
  57. R_RegisterCFinalizerEx(ptr, gctx_finalizer, TRUE);
  58. Rf_setAttrib(ptr, Rf_install("class"), Rf_mkString("gctx"));
  59. return(ptr);
  60. } else {
  61. return(R_NilValue);
  62. }
  63. }
  64. //' Resolve a host to an addrss
  65. //'
  66. //' @param gctx gdns resolver context created with [gdns_resolver()]
  67. //' @param host to lookup
  68. //' @family query functions
  69. //' @export
  70. //' @examples
  71. //' x <- gdns_resolver()
  72. //' gdns_get_address(x, "yahoo.com")
  73. //' x %>% gdns_get_address("yahoo.com")
  74. // [[Rcpp::export]]
  75. CharacterVector gdns_get_address(SEXP gctx, std::string host) {
  76. uint32_t err;
  77. size_t sz;
  78. getdns_return_t r;
  79. getdns_dict *resp = NULL;
  80. getdns_list *addrs;
  81. std::vector< std::string > out;
  82. bool ok = false;
  83. check_is_xptr(gctx);
  84. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  85. if (gctx == NULL) return(CharacterVector());
  86. if ((r = getdns_address_sync(ctxt, host.c_str(), NULL, &resp))) {
  87. } else if ((r = getdns_dict_get_int(resp, "status", &err))) {
  88. } else if (err != GETDNS_RESPSTATUS_GOOD) {
  89. } else if ((r = getdns_dict_get_list(resp, "just_address_answers", &addrs))) {
  90. } else if (r != GETDNS_RETURN_GOOD) {
  91. } else if ((r = getdns_list_get_length(addrs, &sz))) {
  92. } else {
  93. ok = true;
  94. }
  95. if (ok) {
  96. out.reserve(sz);
  97. for (size_t i = 0; i < sz; ++i) {
  98. getdns_dict *cur_addr;
  99. getdns_bindata *address;
  100. // TODO Validate we don't need to free these
  101. r = getdns_list_get_dict(addrs, i, &cur_addr);
  102. r = getdns_dict_get_bindata(cur_addr, "address_data", &address);
  103. if (address->size == 4 || address->size == 16) { // this is unlikely to be bad
  104. char *addr_str = getdns_display_ip_address(address);
  105. out.push_back(addr_str);
  106. if (addr_str) free(addr_str);
  107. }
  108. }
  109. out.shrink_to_fit();
  110. }
  111. if (resp) getdns_dict_destroy(resp);
  112. if (ok) return(wrap(out)); else return(CharacterVector());
  113. }
  114. // [[Rcpp::export]]
  115. CharacterVector int_get_resolvers(SEXP gctx) {
  116. bool ok = false;
  117. size_t sz;
  118. getdns_list *addrs;
  119. std::vector< std::string > out;
  120. check_is_xptr(gctx);
  121. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  122. if (gctx == NULL) return(CharacterVector());
  123. getdns_return_t r;
  124. if ((r = getdns_context_get_upstream_recursive_servers(ctxt, &addrs))) {
  125. } else if (r != GETDNS_RETURN_GOOD) {
  126. } else if ((r = getdns_list_get_length(addrs, &sz))) {
  127. } else {
  128. ok = true;
  129. }
  130. if (ok) {
  131. out.reserve(sz);
  132. for (size_t i = 0; i < sz; ++i) {
  133. getdns_dict *cur_addr;
  134. getdns_bindata *address;
  135. // TODO Validate we don't need to free these
  136. r = getdns_list_get_dict(addrs, i, &cur_addr);
  137. r = getdns_dict_get_bindata(cur_addr, "address_data", &address);
  138. if (address->size == 4 || address->size == 16) { // this is unlikely to be bad
  139. char *addr_str = getdns_display_ip_address(address);
  140. out.push_back(addr_str);
  141. if (addr_str) free(addr_str);
  142. }
  143. }
  144. out.shrink_to_fit();
  145. }
  146. if (addrs) getdns_list_destroy(addrs);
  147. if (ok) return(wrap(out)); else return(CharacterVector());
  148. }
  149. // [[Rcpp::export]]
  150. CharacterVector int_gdns_query(SEXP gctx, std::string name, uint16_t rr,
  151. bool include_reporting = false) {
  152. uint32_t err;
  153. // size_t sz;
  154. getdns_return_t r;
  155. getdns_dict *resp = NULL;
  156. // getdns_list *results;
  157. std::string out;
  158. bool ok = false;
  159. check_is_xptr(gctx);
  160. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  161. if (gctx == NULL) return(CharacterVector());
  162. getdns_dict *ext_d = getdns_dict_create();
  163. if (include_reporting) {
  164. getdns_dict_set_int(ext_d, "return_call_reporting", GETDNS_EXTENSION_TRUE);
  165. }
  166. // if (rrclass != 1) {
  167. // getdns_dict_set_int(ext_d, "specify_class", (uint32_t)rrclass);
  168. // }
  169. if ((r = getdns_general_sync(ctxt, name.c_str(), rr, ext_d, &resp))) {
  170. Rf_warning("Bad query");
  171. } else if ((r = getdns_dict_get_int(resp, "status", &err))) {
  172. } else if (err != GETDNS_RESPSTATUS_GOOD) {
  173. } else {
  174. ok = true;
  175. }
  176. if (ok) {
  177. char *charout = getdns_print_json_dict(resp, 0);
  178. if (charout) {
  179. out = std::string(charout);
  180. free(charout);
  181. } else {
  182. ok = false;
  183. }
  184. }
  185. if (ext_d) getdns_dict_destroy(ext_d);
  186. if (resp) getdns_dict_destroy(resp);
  187. if (ok) return(wrap(out)); else return(CharacterVector());
  188. }
  189. //' Get the current resolution type setting
  190. //'
  191. //' @param gctx gdns resolver context created with [gdns_resolver()]
  192. //' @export
  193. //' @family context functions
  194. //' @examples
  195. //' x <- gdns_context()
  196. //' gdns_get_resolution_type(x)
  197. // [[Rcpp::export]]
  198. CharacterVector gdns_get_resolution_type(SEXP gctx) {
  199. check_is_xptr(gctx);
  200. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  201. if (gctx == NULL) return(CharacterVector());
  202. getdns_return_t r;
  203. getdns_resolution_t res_type;
  204. if ((r = getdns_context_get_resolution_type(ctxt, &res_type))) {
  205. Rf_error(getdns_get_errorstr_by_id(r));
  206. }
  207. std::string out = res_type == GETDNS_RESOLUTION_STUB ? "stub" : "recursive";
  208. return(wrap(out));
  209. }