vector<TSrvRecord> GetRecords(const string& host) const { ares_channel channel; int status; status = ares_init(&channel); if(status != ARES_SUCCESS) { throw UException(std::string("Failed to init ares channel: ") + ares_strerror(status)); } TCallbackInfo info; ares_query(channel, host.c_str(), ns_c_in, ns_t_srv, callback, &info); wait_ares(channel); ares_destroy(channel); if (info.Status != ARES_SUCCESS) { throw UException(std::string("Failed to make ares request: ") + ares_strerror(info.Status)); } struct ares_srv_reply* reply; status = ares_parse_srv_reply((const unsigned char*)info.Data.data(), info.Data.length(), &reply); if (info.Status != ARES_SUCCESS) { throw UException(std::string("Failed to parse response: ") + ares_strerror(status)); } vector<TSrvRecord> records; struct ares_srv_reply* next = reply; while (next != NULL) { TSrvRecord record; record.Host = next->host; record.Port = next->port; record.Priority = next->priority; record.Weight = next->weight; records.push_back(record); next = next->next; } ares_free_data(reply); return records; }
int main(int argc, char **argv) { WSADATA wsa; subnet sub; int ret, x; WSAStartup(0x22, &wsa); fprintf(stderr, "CIDR Reverse DNS - William Welna ([email protected])\n\n"); if(argc != 3) { fprintf(stderr, "No Range Specified and/or no output file or too many arguments\n"); fprintf(stderr, "Usage: %s 127.0.0.0/24 localrdns.txt\n", argv[0]); exit(0); } if(ret=ares_library_init(ARES_LIB_INIT_ALL)!=ARES_SUCCESS) { fprintf(stderr, "ares_library_init() failed %i\n", ret); exit(0); } if(ret=ares_init(&ares_chan)!=ARES_SUCCESS) { fprintf(stderr, "ares_init() failed %i\n", ret); exit(0); } calculate_subnet(argv[1], &sub); build_ips(sub.addr_s, sub.addr_e); global_total = sub.total_ips; for(x=0; x < 60 && IPS_P != NULL; ++x, IPS_P=IPS_P->n) ares_gethostbyaddr(ares_chan, &IPS_P->ip, 4, AF_INET, rdns_callback, IPS_P); fprintf(stderr, "Resolving %s to %s (%i)\n", sub.ip_s, sub.ip_e, sub.total_ips); tehloop(); dump_results(argv[2]); return 0; }
// common/ares/rvares.c RvStatus RvAresInit(void) { #if (RV_DNS_TYPE == RV_DNS_ARES) status = ares_init(); status = RvAresCacheDInit(); #endif }
int main(void) { ares_channel channelptr; ares_init(&channelptr); ares_destroy(channelptr); return 0; }
LLAres::LLAres() : chan_(NULL), mInitSuccess(false) { if (ares_init(&chan_) != ARES_SUCCESS) { llwarns << "Could not succesfully initialize ares!" << llendl; return; } mInitSuccess = true; }
/* * Curl_resolver_init() * * Called from curl_easy_init() -> Curl_open() to initialize resolver * URL-state specific environment ('resolver' member of the UrlState * structure). Fills the passed pointer by the initialized ares_channel. */ CURLcode Curl_resolver_init(void **resolver) { int status = ares_init((ares_channel*)resolver); if(status != ARES_SUCCESS) { if(status == ARES_ENOMEM) return CURLE_OUT_OF_MEMORY; else return CURLE_FAILED_INIT; } return CURLE_OK; /* make sure that all other returns from this function should destroy the ares channel before returning error! */ }
/** * Initialize the DNS resolver for use. If this function fails the BNC will not be able to function * properly. */ int p_dns_init() { int ret; if ((ret = ares_init(&resolver)) != ARES_SUCCESS) { p_log(LOG_ERROR, -1, "Failed to initialize ares resolver: %s", ares_strerror(ret)); return 0; } p_log(LOG_INFO, -1, "Asynchronous resolver initialized: c-ares %s", ares_version(NULL)); return 1; }
LLAres::LLAres() : chan_(NULL), mInitSuccess(false), mListener(new LLAresListener(this)) { if (ares_library_init( ARES_LIB_INIT_ALL ) != ARES_SUCCESS || ares_init(&chan_) != ARES_SUCCESS) { llwarns << "Could not succesfully initialize ares!" << llendl; return; } mInitSuccess = true; }
LLAres::LLAres() : chan_(NULL), mInitSuccess(false) { if (ares_library_init( ARES_LIB_INIT_ALL ) != ARES_SUCCESS || ares_init(&chan_) != ARES_SUCCESS) { LL_WARNS() << "Could not succesfully initialize ares!" << LL_ENDL; return; } mListener = boost::shared_ptr< LLAresListener >(new LLAresListener(this)); mInitSuccess = true; }
DNSHandler::DNSHandler (PanicType panic) : stop(false), panic(std::move(panic)) { // Setup the completion callback complete=[this] (Query * q, bool) mutable noexcept { lock.Execute([&] () mutable { queries.erase(q); }); }; // Check panic callback, if it's empty, // default it to std::abort if (!panic) panic=[] (std::exception_ptr) { std::abort(); }; // Initialize libcares auto result=ares_library_init(ARES_LIB_INIT_ALL); if (result!=0) raise(result); try { // Prepare an asynchronous resolver // channel if ((result=ares_init(&channel))!=0) raise(result); try { // Create worker thread thread=Thread([this] () mutable { worker_func(); }); } catch (...) { ares_destroy(channel); throw; } } catch (...) { ares_library_cleanup(); throw; } }
void _ko_resolve_init(void) { char *errmem; int ret; IOMGR_Initialize(); if (LWP_CreateProcess(ares_worker_thread, AFS_LWP_MINSTACKSIZE, 0, 0, "ares resolver daemon", &ares_pid)) errx(1, "Couldn't initialize resolver, helper thread didn't start"); /* XXX use ARES_FLAG_NOSEARCH */ ret = ares_init(&achannel); if (ret != ARES_SUCCESS) errx(1, "Couldn't initialize resolver: %s", ares_strerror(ret, &errmem)); }
int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepalive, const char *bind_address) { #ifdef WITH_SRV char *h; int rc; if(!mosq) return MOSQ_ERR_INVAL; rc = ares_init(&mosq->achan); if(rc != ARES_SUCCESS){ return MOSQ_ERR_UNKNOWN; } if(!host){ // get local domain }else{ #ifdef WITH_TLS if(mosq->tls_cafile || mosq->tls_capath || mosq->tls_psk){ h = _mosquitto_malloc(strlen(host) + strlen("_secure-mqtt._tcp.") + 1); if(!h) return MOSQ_ERR_NOMEM; sprintf(h, "_secure-mqtt._tcp.%s", host); }else{ #endif h = _mosquitto_malloc(strlen(host) + strlen("_mqtt._tcp.") + 1); if(!h) return MOSQ_ERR_NOMEM; sprintf(h, "_mqtt._tcp.%s", host); #ifdef WITH_TLS } #endif ares_search(mosq->achan, h, ns_c_in, ns_t_srv, srv_callback, mosq); _mosquitto_free(h); } pthread_mutex_lock(&mosq->state_mutex); mosq->state = mosq_cs_connect_srv; pthread_mutex_unlock(&mosq->state_mutex); mosq->keepalive = keepalive; return MOSQ_ERR_SUCCESS; #else return MOSQ_ERR_NOT_SUPPORTED; #endif }
void start_resolve() { if( ares ) { ares_destroy(ares); ares = 0; } state = kResolving; std::string actual_url = redirect_url.size() ? redirect_url:url; std::string host = extract_host(actual_url); std::string res = dns_cache->get_ent(host); if( !res.size() ) { ares_init(&ares); ares_gethostbyname(ares,host.c_str(),AF_INET,Request::GotResolveStatic,this); } else { char buf[1024]; sprintf(buf,"%d.%d.%d.%d",res[0],res[1],res[2],res[3]); direct_ip = buf; state = kStartDownload; } }
static aresInit(ares_channel * channel) { if(!aresInitDone) { dbg("%s: ares_library_init\n", __FUNCTION__); ares_library_init(ARES_LIB_INIT_ALL); aresInitDone = 1; he.h_name = f_name; he.h_aliases = &f_empty; he.h_addrtype = 0; he.h_length = 0; he.h_addr_list = f_addrlist; } dbg("%s: ares_init\n", __FUNCTION__); ares_init(channel); dbg("%s: ares_set_servers_csv\n", __FUNCTION__); ares_set_servers_csv(*channel, "8.8.8.8,8.8.4.4"); dbg("%s: done\n", __FUNCTION__); }
int ares_dup(ares_channel *dest, ares_channel src) { return ares_init(dest); }
int main(int argc, char **argv) { ares_channel channel; int status, nfds, c, addr_family = AF_INET; fd_set read_fds, write_fds; struct timeval *tvp, tv; struct in_addr addr4; struct ares_in6_addr addr6; #ifdef USE_WINSOCK WORD wVersionRequested = MAKEWORD(USE_WINSOCK,USE_WINSOCK); WSADATA wsaData; WSAStartup(wVersionRequested, &wsaData); #endif status = ares_library_init(ARES_LIB_INIT_ALL); if (status != ARES_SUCCESS) { fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status)); return 1; } while ((c = ares_getopt(argc,argv,"dt:h")) != -1) { switch (c) { case 'd': #ifdef WATT32 dbug_init(); #endif break; case 't': if (!strcasecmp(optarg,"a")) addr_family = AF_INET; else if (!strcasecmp(optarg,"aaaa")) addr_family = AF_INET6; else usage(); break; case 'h': default: usage(); break; } } argc -= optind; argv += optind; if (argc < 1) usage(); status = ares_init(&channel); if (status != ARES_SUCCESS) { fprintf(stderr, "ares_init: %s\n", ares_strerror(status)); return 1; } /* Initiate the queries, one per command-line argument. */ for ( ; *argv; argv++) { if (ares_inet_pton(AF_INET, *argv, &addr4) == 1) { ares_gethostbyaddr(channel, &addr4, sizeof(addr4), AF_INET, callback, *argv); } else if (ares_inet_pton(AF_INET6, *argv, &addr6) == 1) { ares_gethostbyaddr(channel, &addr6, sizeof(addr6), AF_INET6, callback, *argv); } else { ares_gethostbyname(channel, *argv, addr_family, callback, *argv); } } /* Wait for all queries to complete. */ for (;;) { FD_ZERO(&read_fds); FD_ZERO(&write_fds); nfds = ares_fds(channel, &read_fds, &write_fds); if (nfds == 0) break; tvp = ares_timeout(channel, NULL, &tv); select(nfds, &read_fds, &write_fds, NULL, tvp); ares_process(channel, &read_fds, &write_fds); } ares_destroy(channel); ares_library_cleanup(); #ifdef USE_WINSOCK WSACleanup(); #endif return 0; }
int gethostbyaddr_with_ares(const void *addr, int addrlen, int af, struct hostent *ret_h, char *buf, size_t &buflen, struct hostent **result, int *h_errnop) { int err = 0; if (!h_errnop) h_errnop = &err; cares_async_context ctx; ctx.called = false; ctx.errnop = h_errnop; ctx.ret = 0; ctx.result = result; ctx.buf = buf; ctx.buflen = &buflen; ares_channel c; ares_init(&c); ares_gethostbyaddr(c, addr, addrlen, af, [](void * arg, int status, int timeouts, struct hostent *hostent) { cares_async_context *ctx = (cares_async_context *)arg; ctx->called = true; if (status != ARES_SUCCESS) { *ctx->result = nullptr; switch (status) { case ARES_ENODATA: *ctx->errnop = NO_DATA; break; case ARES_EBADNAME: *ctx->errnop = NO_RECOVERY; break; case ARES_ENOTFOUND: default: *ctx->errnop = HOST_NOT_FOUND; break; } ctx->ret = -1; return ; } if (!hostent_dup(hostent, *ctx->result, ctx->buf, *ctx->buflen)) { *ctx->result = nullptr; *ctx->errnop = ENOEXEC; // 奇怪的错误码. ctx->ret = -1; return ; } *ctx->errnop = 0; ctx->ret = 0; }, &ctx); fd_set readers, writers; FD_ZERO(&readers); FD_ZERO(&writers); int nfds = ares_fds(c, &readers, &writers); if (nfds) { struct timeval tv, *tvp; tvp = ares_timeout(c, NULL, &tv); int count = select(nfds, &readers, &writers, NULL, tvp); if (count > 0) { ares_process(c, &readers, &writers); } } if (ctx.called) return ctx.ret; // No yet invoke callback. *result = nullptr; *h_errnop = HOST_NOT_FOUND; // TODO return -1; }
/* * curl_easy_duphandle() is an external interface to allow duplication of a * given input easy handle. The returned handle will be a new working handle * with all options set exactly as the input source handle. */ CURL *curl_easy_duphandle(CURL *incurl) { bool fail = TRUE; struct SessionHandle *data=(struct SessionHandle *)incurl; struct SessionHandle *outcurl = (struct SessionHandle *) calloc(sizeof(struct SessionHandle), 1); if(NULL == outcurl) return NULL; /* failure */ do { /* * We setup a few buffers we need. We should probably make them * get setup on-demand in the code, as that would probably decrease * the likeliness of us forgetting to init a buffer here in the future. */ outcurl->state.headerbuff=(char*)malloc(HEADERSIZE); if(!outcurl->state.headerbuff) { break; } outcurl->state.headersize=HEADERSIZE; /* copy all userdefined values */ outcurl->set = data->set; outcurl->state.numconnects = data->state.numconnects; outcurl->state.connects = (struct connectdata **) malloc(sizeof(struct connectdata *) * outcurl->state.numconnects); if(!outcurl->state.connects) { break; } memset(outcurl->state.connects, 0, sizeof(struct connectdata *)*outcurl->state.numconnects); outcurl->progress.flags = data->progress.flags; outcurl->progress.callback = data->progress.callback; #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) if(data->cookies) { /* If cookies are enabled in the parent handle, we enable them in the clone as well! */ outcurl->cookies = Curl_cookie_init(data, data->cookies->filename, outcurl->cookies, data->set.cookiesession); if(!outcurl->cookies) { break; } } #endif /* CURL_DISABLE_HTTP */ /* duplicate all values in 'change' */ if(data->change.url) { outcurl->change.url = strdup(data->change.url); if(!outcurl->change.url) break; outcurl->change.url_alloc = TRUE; } if(data->change.proxy) { outcurl->change.proxy = strdup(data->change.proxy); if(!outcurl->change.proxy) break; outcurl->change.proxy_alloc = TRUE; } if(data->change.referer) { outcurl->change.referer = strdup(data->change.referer); if(!outcurl->change.referer) break; outcurl->change.referer_alloc = TRUE; } #ifdef USE_ARES /* If we use ares, we setup a new ares channel for the new handle */ if(ARES_SUCCESS != ares_init(&outcurl->state.areschannel)) break; #endif fail = FALSE; /* we reach this point and thus we are OK */ } while(0); if(fail) { if(outcurl) { if(outcurl->state.connects) free(outcurl->state.connects); if(outcurl->state.headerbuff) free(outcurl->state.headerbuff); if(outcurl->change.proxy) free(outcurl->change.proxy); if(outcurl->change.url) free(outcurl->change.url); if(outcurl->change.referer) free(outcurl->change.referer); free(outcurl); /* free the memory again */ outcurl = NULL; } } return outcurl; }
/* * curl_easy_duphandle() is an external interface to allow duplication of a * given input easy handle. The returned handle will be a new working handle * with all options set exactly as the input source handle. */ CURL *curl_easy_duphandle(CURL *incurl) { bool fail = TRUE; struct SessionHandle *data=(struct SessionHandle *)incurl; struct SessionHandle *outcurl = calloc(1, sizeof(struct SessionHandle)); if(NULL == outcurl) return NULL; /* failure */ do { /* * We setup a few buffers we need. We should probably make them * get setup on-demand in the code, as that would probably decrease * the likeliness of us forgetting to init a buffer here in the future. */ outcurl->state.headerbuff = malloc(HEADERSIZE); if(!outcurl->state.headerbuff) { break; } outcurl->state.headersize=HEADERSIZE; /* copy all userdefined values */ if(Curl_dupset(outcurl, data) != CURLE_OK) break; /* the connection cache is setup on demand */ outcurl->state.connc = NULL; outcurl->state.lastconnect = -1; outcurl->progress.flags = data->progress.flags; outcurl->progress.callback = data->progress.callback; #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) if(data->cookies) { /* If cookies are enabled in the parent handle, we enable them in the clone as well! */ outcurl->cookies = Curl_cookie_init(data, data->cookies->filename, outcurl->cookies, data->set.cookiesession); if(!outcurl->cookies) { break; } } #endif /* CURL_DISABLE_HTTP */ /* duplicate all values in 'change' */ #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) if(data->change.cookielist) { outcurl->change.cookielist = Curl_slist_duplicate(data->change.cookielist); if (!outcurl->change.cookielist) break; } #endif /* CURL_DISABLE_HTTP */ if(data->change.url) { outcurl->change.url = strdup(data->change.url); if(!outcurl->change.url) break; outcurl->change.url_alloc = TRUE; } if(data->change.referer) { outcurl->change.referer = strdup(data->change.referer); if(!outcurl->change.referer) break; outcurl->change.referer_alloc = TRUE; } #ifdef USE_ARES /* If we use ares, we setup a new ares channel for the new handle */ if(ARES_SUCCESS != ares_init(&outcurl->state.areschannel)) break; #endif #if defined(CURL_DOES_CONVERSIONS) && defined(HAVE_ICONV) outcurl->inbound_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, CURL_ICONV_CODESET_OF_NETWORK); outcurl->outbound_cd = iconv_open(CURL_ICONV_CODESET_OF_NETWORK, CURL_ICONV_CODESET_OF_HOST); outcurl->utf8_cd = iconv_open(CURL_ICONV_CODESET_OF_HOST, CURL_ICONV_CODESET_FOR_UTF8); #endif Curl_easy_initHandleData(outcurl); outcurl->magic = CURLEASY_MAGIC_NUMBER; fail = FALSE; /* we reach this point and thus we are OK */ } while(0); if(fail) { if(outcurl) { if(outcurl->state.connc && (outcurl->state.connc->type == CONNCACHE_PRIVATE)) Curl_rm_connc(outcurl->state.connc); if(outcurl->state.headerbuff) free(outcurl->state.headerbuff); #if !defined(CURL_DISABLE_HTTP) && !defined(CURL_DISABLE_COOKIES) if(outcurl->change.cookielist) curl_slist_free_all(outcurl->change.cookielist); #endif if(outcurl->change.url) free(outcurl->change.url); if(outcurl->change.referer) free(outcurl->change.referer); Curl_freeset(outcurl); free(outcurl); /* free the memory again */ outcurl = NULL; } } return outcurl; }
LLAres::LLAres() { ares_init(&chan_); }