No Description
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.

resolver.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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_resolver
  33. //' @keywords internal
  34. // [[Rcpp::export]]
  35. SEXP int_gdns_resolver(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. //' @export
  69. //' @examples
  70. //' x <- gdns_resolver()
  71. //' gdns_get_address(x, "yahoo.com")
  72. //' x %>% gdns_get_address("yahoo.com")
  73. // [[Rcpp::export]]
  74. CharacterVector gdns_get_address(SEXP gctx, std::string host) {
  75. uint32_t err;
  76. size_t sz;
  77. getdns_return_t r;
  78. getdns_dict *resp = NULL;
  79. getdns_list *addrs;
  80. std::vector< std::string > out;
  81. bool ok = false;
  82. check_is_xptr(gctx);
  83. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  84. if (gctx == NULL) return(CharacterVector());
  85. if ((r = getdns_address_sync(ctxt, host.c_str(), NULL, &resp))) {
  86. } else if ((r = getdns_dict_get_int(resp, "status", &err))) {
  87. } else if (err != GETDNS_RESPSTATUS_GOOD) {
  88. } else if ((r = getdns_dict_get_list(resp, "just_address_answers", &addrs))) {
  89. } else if (r != GETDNS_RETURN_GOOD) {
  90. } else if ((r = getdns_list_get_length(addrs, &sz))) {
  91. } else {
  92. ok = true;
  93. }
  94. if (ok) {
  95. out.reserve(sz);
  96. for (size_t i = 0; i < sz; ++i) {
  97. getdns_dict *cur_addr;
  98. getdns_bindata *address;
  99. // TODO Validate we don't need to free these
  100. r = getdns_list_get_dict(addrs, i, &cur_addr);
  101. r = getdns_dict_get_bindata(cur_addr, "address_data", &address);
  102. if (address->size == 4 || address->size == 16) { // this is unlikely to be bad
  103. char *addr_str = getdns_display_ip_address(address);
  104. out.push_back(addr_str);
  105. if (addr_str) free(addr_str);
  106. }
  107. }
  108. out.shrink_to_fit();
  109. }
  110. if (resp) getdns_dict_destroy(resp);
  111. if (ok) return(wrap(out)); else return(CharacterVector());
  112. }
  113. // [[Rcpp::export]]
  114. CharacterVector int_get_resolvers(SEXP gctx) {
  115. bool ok = false;
  116. size_t sz;
  117. getdns_list *addrs;
  118. std::vector< std::string > out;
  119. check_is_xptr(gctx);
  120. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  121. if (gctx == NULL) return(CharacterVector());
  122. getdns_return_t r;
  123. if ((r = getdns_context_get_upstream_recursive_servers(ctxt, &addrs))) {
  124. } else if (r != GETDNS_RETURN_GOOD) {
  125. } else if ((r = getdns_list_get_length(addrs, &sz))) {
  126. } else {
  127. ok = true;
  128. }
  129. if (ok) {
  130. out.reserve(sz);
  131. for (size_t i = 0; i < sz; ++i) {
  132. getdns_dict *cur_addr;
  133. getdns_bindata *address;
  134. // TODO Validate we don't need to free these
  135. r = getdns_list_get_dict(addrs, i, &cur_addr);
  136. r = getdns_dict_get_bindata(cur_addr, "address_data", &address);
  137. if (address->size == 4 || address->size == 16) { // this is unlikely to be bad
  138. char *addr_str = getdns_display_ip_address(address);
  139. out.push_back(addr_str);
  140. if (addr_str) free(addr_str);
  141. }
  142. }
  143. out.shrink_to_fit();
  144. }
  145. if (addrs) getdns_list_destroy(addrs);
  146. if (ok) return(wrap(out)); else return(CharacterVector());
  147. }
  148. // [[Rcpp::export]]
  149. CharacterVector int_gdns_query(SEXP gctx, std::string name, uint16_t rr) {
  150. uint32_t err;
  151. // size_t sz;
  152. getdns_return_t r;
  153. getdns_dict *resp = NULL;
  154. // getdns_list *results;
  155. std::string out;
  156. bool ok = false;
  157. check_is_xptr(gctx);
  158. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  159. if (gctx == NULL) return(CharacterVector());
  160. if ((r = getdns_general_sync(ctxt, name.c_str(), rr, NULL, &resp))) {
  161. } else if ((r = getdns_dict_get_int(resp, "status", &err))) {
  162. } else if (err != GETDNS_RESPSTATUS_GOOD) {
  163. } else {
  164. ok = true;
  165. }
  166. if (ok) {
  167. char *charout = getdns_print_json_dict(resp, 0);
  168. if (charout) {
  169. out = std::string(charout);
  170. free(charout);
  171. } else {
  172. ok = false;
  173. }
  174. }
  175. if (resp) getdns_dict_destroy(resp);
  176. if (ok) return(wrap(out)); else return(CharacterVector());
  177. }