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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. return Rf_ScalarLogical(R_ExternalPtrAddr(s) == NULL);
  24. }
  25. static void gctx_finalizer(SEXP ptr) {
  26. if(!R_ExternalPtrAddr(ptr)) return;
  27. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(ptr);
  28. if (ptr) getdns_context_destroy(ctxt);
  29. R_ClearExternalPtr(ptr); /* not really needed */
  30. }
  31. //' Internal version of gdns_resolver
  32. //' @keywords internal
  33. // [[Rcpp::export]]
  34. SEXP int_gdns_resolver(std::vector< std::string > resolvers) {
  35. bool ok = false;
  36. SEXP ptr;
  37. getdns_return_t r;
  38. getdns_context *ctxt = NULL;
  39. // TODO Validate we don't need to free these
  40. getdns_dict *resolver_dict = getdns_dict_create();
  41. getdns_list *resolver_list = getdns_list_create();
  42. for (int i = 0; i<resolvers.size(); i++) {
  43. r = getdns_str2dict(resolvers[i].c_str(), &resolver_dict);
  44. r = getdns_list_set_dict(resolver_list, i, resolver_dict);
  45. }
  46. getdns_transport_list_t tls_transport[] = { GETDNS_TRANSPORT_TLS };
  47. if ((r = getdns_context_create(&ctxt, 1))) {
  48. } else if ((r = getdns_context_set_dns_transport_list(ctxt, 1, tls_transport))) {
  49. } else if ((r = getdns_context_set_upstream_recursive_servers(ctxt, resolver_list))) {
  50. } else if ((r = getdns_context_set_resolution_type(ctxt, GETDNS_RESOLUTION_STUB))) {
  51. } else {
  52. ok = true;
  53. }
  54. if (ok) {
  55. ptr = R_MakeExternalPtr(ctxt, Rf_install("gctx"), R_NilValue);
  56. R_RegisterCFinalizerEx(ptr, gctx_finalizer, TRUE);
  57. Rf_setAttrib(ptr, Rf_install("class"), Rf_mkString("gctx"));
  58. return(ptr);
  59. } else {
  60. return(R_NilValue);
  61. }
  62. }
  63. //' Resolve a host to an addrss
  64. //'
  65. //' @param gctx gdns resolver context created with [gdns_resolver()]
  66. //' @param host to lookup
  67. //' @export
  68. //' @examples
  69. //' x <- gdns_resolver()
  70. //' gdns_get_address(x, "yahoo.com")
  71. //' x %>% gdns_get_address("yahoo.com")
  72. // [[Rcpp::export]]
  73. CharacterVector gdns_get_address(SEXP gctx, std::string host) {
  74. uint32_t err;
  75. size_t sz;
  76. getdns_return_t r;
  77. getdns_dict *resp = NULL;
  78. getdns_list *addrs;
  79. std::vector< std::string > out;
  80. bool ok = false;
  81. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  82. if (gctx == NULL) return(CharacterVector());
  83. if ((r = getdns_address_sync(ctxt, host.c_str(), NULL, &resp))) {
  84. } else if ((r = getdns_dict_get_int(resp, "status", &err))) {
  85. } else if (err != GETDNS_RESPSTATUS_GOOD) {
  86. } else if ((r = getdns_dict_get_list(resp, "just_address_answers", &addrs))) {
  87. } else if (r != GETDNS_RETURN_GOOD) {
  88. } else if ((r = getdns_list_get_length(addrs, &sz))) {
  89. } else {
  90. ok = true;
  91. }
  92. if (ok) {
  93. out.reserve(sz);
  94. for (size_t i = 0; i < sz; ++i) {
  95. getdns_dict *cur_addr;
  96. getdns_bindata *address;
  97. // TODO Validate we don't need to free these
  98. r = getdns_list_get_dict(addrs, i, &cur_addr);
  99. r = getdns_dict_get_bindata(cur_addr, "address_data", &address);
  100. if (address->size == 4 || address->size == 16) { // this is unlikely to be bad
  101. char *addr_str = getdns_display_ip_address(address);
  102. out.push_back(addr_str);
  103. if (addr_str) free(addr_str);
  104. }
  105. }
  106. out.shrink_to_fit();
  107. }
  108. if (resp) getdns_dict_destroy(resp);
  109. if (ok) return(wrap(out)); else return(CharacterVector());
  110. }
  111. // [[Rcpp::export]]
  112. CharacterVector int_get_resolvers(SEXP gctx) {
  113. bool ok = false;
  114. size_t sz;
  115. getdns_list *addrs;
  116. std::vector< std::string > out;
  117. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  118. if (gctx == NULL) return(CharacterVector());
  119. getdns_return_t r;
  120. if ((r = getdns_context_get_upstream_recursive_servers(ctxt, &addrs))) {
  121. } else if (r != GETDNS_RETURN_GOOD) {
  122. } else if ((r = getdns_list_get_length(addrs, &sz))) {
  123. } else {
  124. ok = true;
  125. }
  126. if (ok) {
  127. out.reserve(sz);
  128. for (size_t i = 0; i < sz; ++i) {
  129. getdns_dict *cur_addr;
  130. getdns_bindata *address;
  131. // TODO Validate we don't need to free these
  132. r = getdns_list_get_dict(addrs, i, &cur_addr);
  133. r = getdns_dict_get_bindata(cur_addr, "address_data", &address);
  134. if (address->size == 4 || address->size == 16) { // this is unlikely to be bad
  135. char *addr_str = getdns_display_ip_address(address);
  136. out.push_back(addr_str);
  137. if (addr_str) free(addr_str);
  138. }
  139. }
  140. out.shrink_to_fit();
  141. }
  142. if (addrs) getdns_list_destroy(addrs);
  143. if (ok) return(wrap(out)); else return(CharacterVector());
  144. }
  145. // [[Rcpp::export]]
  146. CharacterVector int_gdns_query(SEXP gctx, std::string name, uint16_t rr) {
  147. uint32_t err;
  148. size_t sz;
  149. getdns_return_t r;
  150. getdns_dict *resp = NULL;
  151. getdns_list *results;
  152. std::string out;
  153. bool ok = false;
  154. getdns_context *ctxt = (getdns_context *)R_ExternalPtrAddr(gctx);
  155. if (gctx == NULL) return(CharacterVector());
  156. if ((r = getdns_general_sync(ctxt, name.c_str(), rr, NULL, &resp))) {
  157. } else if ((r = getdns_dict_get_int(resp, "status", &err))) {
  158. } else if (err != GETDNS_RESPSTATUS_GOOD) {
  159. } else {
  160. ok = true;
  161. }
  162. if (ok) {
  163. char *charout = getdns_print_json_dict(resp, 0);
  164. if (charout) {
  165. out = std::string(charout);
  166. free(charout);
  167. } else {
  168. ok = false;
  169. }
  170. }
  171. if (resp) getdns_dict_destroy(resp);
  172. if (ok) return(wrap(out)); else return(CharacterVector());
  173. }