int cert_updater_init(ProxyContext * const proxy_context) { CertUpdater *cert_updater = &proxy_context->cert_updater; int ar_options_mask; memset(cert_updater, 0, sizeof *cert_updater); if (ares_library_init(ARES_LIB_INIT_ALL) != ARES_SUCCESS) { return -1; } assert(proxy_context->event_loop != NULL); cert_updater->has_cert_timer = 0; cert_updater->query_retry_step = 0U; ar_options_mask = ARES_OPT_SERVERS; cert_updater->ar_options.nservers = 1; cert_updater->ar_options.servers = &((struct sockaddr_in *) &proxy_context->resolver_addr)->sin_addr; if (proxy_context->tcp_only) { ar_options_mask |= ARES_OPT_FLAGS | ARES_OPT_TCP_PORT; cert_updater->ar_options.flags = ARES_FLAG_USEVC; cert_updater->ar_options.tcp_port = htons(proxy_context->resolver_port); } if (uv_ares_init_options(proxy_context->event_loop, &cert_updater->ar_channel, &cert_updater->ar_options, ar_options_mask) != ARES_SUCCESS) { return -1; } return 0; }
int ape_dns_init(ape_global *ape) { struct ares_options opt; int ret; if (ares_library_init(ARES_LIB_INIT_ALL) != 0) { return -1; } opt.sock_state_cb = ares_socket_cb; opt.sock_state_cb_data = ape; /* At the moment we only use one dns channel */ if ((ret = ares_init_options(&ape->dns.channel, &opt, 0x00 | ARES_OPT_SOCK_STATE_CB)) != ARES_SUCCESS) { return -1; } ape->dns.sockets.list = calloc(32, sizeof(struct _ares_sockets) * 32); ape->dns.sockets.size = 32; ape->dns.sockets.used = 0; return 0; }
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; }
int main (int argc, char *argv[]) { struct ev_loop *loop = EV_DEFAULT; if (argc < 2) { fprintf(stderr, "Usage:\n\t%s domain\n",argv[0]); return 1; } char *hostname = argv[1]; char *jabber = calloc(1,strlen(argv[1]) + strlen(JSRV) + 1); // will be freed on exit ;) strcat(jabber,JSRV); strcat(jabber,argv[1]); // Declare resolver struct; ev_ares resolver; printf("Resolving '%s'\n",hostname); // Initialize ares library. int status; if ((status = ares_library_init(ARES_LIB_INIT_ALL) )!= ARES_SUCCESS) { fprintf(stderr,"Ares error: %s\n",ares_strerror(status)); return 1; } //Initialize resolver with timeout 1.3 if (( status = ev_ares_init(&resolver, 1.3) ) != ARES_SUCCESS) { fprintf(stderr,"Ares error: %s\n",ares_strerror(status)); return 1; } // hostname variable must not be freed until resolve callback, since it referenced as result->host ev_ares_soa(loop,&resolver,hostname,0,callback_soa); ev_ares_ns(loop,&resolver,hostname,0,callback_ns); ev_ares_a(loop,&resolver,hostname,0,callback_a); ev_ares_aaaa(loop,&resolver,hostname,0,callback_aaaa); ev_ares_mx(loop,&resolver,hostname,0,callback_mx); ev_ares_srv(loop,&resolver,jabber,0,callback_srv); ev_ares_txt(loop,&resolver,hostname,0,callback_txt); ev_ares_gethostbyaddr(loop,&resolver,"8.8.8.8", 0, callback_hba); ev_ares_gethostbyaddr(loop,&resolver,"2a00:1450:4010:c04::66", 0,callback_hba); // Raw PTR queries ev_ares_ptr(loop,&resolver,"8.8.8.8.in-addr.arpa", 0,callback_ptr); ev_ares_ptr(loop,&resolver,"a.8.0.0.0.0.0.0.0.0.0.0.0.0.0.0.4.0.c.0.0.1.0.4.0.5.4.1.0.0.a.2.ip6.arpa", 0,callback_ptr); //This is the only NAPTR example i found ;) ev_ares_naptr(loop,&resolver,"0.2.0.1.1.6.5.1.0.3.1.loligo.com.",0,callback_naptr); // Run loop ev_run (loop, 0); free(jabber); ev_ares_clean(&resolver); ares_library_cleanup(); }
/* * Curl_resolver_global_init() - the generic low-level asynchronous name * resolve API. Called from curl_global_init() to initialize global resolver * environment. Initializes ares library. */ int Curl_resolver_global_init(void) { #ifdef CARES_HAVE_ARES_LIBRARY_INIT if(ares_library_init(ARES_LIB_INIT_ALL)) { return CURLE_FAILED_INIT; } #endif return CURLE_OK; }
/** * curl_global_init() globally initializes cURL given a bitwise set of the * different features of what to initialize. */ CURLcode curl_global_init(long flags) { if(initialized++) return CURLE_OK; /* Setup the default memory functions here (again) */ Curl_cmalloc = (curl_malloc_callback)malloc; Curl_cfree = (curl_free_callback)free; Curl_crealloc = (curl_realloc_callback)realloc; Curl_cstrdup = (curl_strdup_callback)system_strdup; Curl_ccalloc = (curl_calloc_callback)calloc; if(flags & CURL_GLOBAL_SSL) if(!Curl_ssl_init()) { DEBUGF(fprintf(stderr, "Error: Curl_ssl_init failed\n")); return CURLE_FAILED_INIT; } if(flags & CURL_GLOBAL_WIN32) if(win32_init() != CURLE_OK) { DEBUGF(fprintf(stderr, "Error: win32_init failed\n")); return CURLE_FAILED_INIT; } #ifdef __AMIGA__ if(!amiga_init()) { DEBUGF(fprintf(stderr, "Error: amiga_init failed\n")); return CURLE_FAILED_INIT; } #endif #ifdef NETWARE if(netware_init()) { DEBUGF(fprintf(stderr, "Warning: LONG namespace not available\n")); } #endif #ifdef USE_LIBIDN idna_init(); #endif #ifdef CARES_HAVE_ARES_LIBRARY_INIT if(ares_library_init(ARES_LIB_INIT_ALL)) { DEBUGF(fprintf(stderr, "Error: ares_library_init failed\n")); return CURLE_FAILED_INIT; } #endif init_flags = flags; /* Preset pseudo-random number sequence. */ Curl_srand(); return CURLE_OK; }
TAres() { #if defined (_WIN32) && ! defined (__CYGWIN__) WSADATA p; WSAStartup ((2<<8) | 2, &p); #endif int status = ares_library_init(ARES_LIB_INIT_ALL); if (status != ARES_SUCCESS) { throw UException(std::string("Failed to init ares: ") + ares_strerror(status)); } }
int main(int argc, char *argv[]) { g_assert(st_set_eventsys(ST_EVENTSYS_ALT) == 0); st_init(); int status = ares_library_init(ARES_LIB_INIT_ALL); if (status != ARES_SUCCESS) { fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status)); return 1; } int sock; int n; struct sockaddr_in serv_addr; if ((sock = socket(PF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); } n = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (char *)&n, sizeof(n)) < 0) { perror("setsockopt SO_REUSEADDR"); } memset(&serv_addr, 0, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_port = htons(8080); serv_addr.sin_addr.s_addr = inet_addr("0.0.0.0"); if (bind(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { perror("bind"); } if (listen(sock, 10) < 0) { perror("listen"); } st_netfd_t server_nfd = st_netfd_open_socket(sock); st_netfd_t client_nfd; struct sockaddr_in from; int fromlen = sizeof(from); for (;;) { client_nfd = st_accept(server_nfd, (struct sockaddr *)&from, &fromlen, ST_UTIME_NO_TIMEOUT); printf("accepted\n"); if (st_thread_create(handle_connection, (void *)client_nfd, 0, 1024 * 1024) == NULL) { fprintf(stderr, "st_thread_create error\n"); } } ares_library_cleanup(); return EXIT_SUCCESS; }
DNSEnumService::DNSEnumService(const std::vector<std::string>& dns_servers, const std::string& dns_suffix, const DNSResolverFactory* resolver_factory, CommunicationMonitor* comm_monitor) : _dns_suffix(dns_suffix), _resolver_factory(resolver_factory), _comm_monitor(comm_monitor) { // Initialize the ares library. This might have already been done by curl // but it's safe to do it twice. ares_library_init(ARES_LIB_INIT_ALL); struct addrinfo* res; for (std::vector<std::string>::const_iterator server = dns_servers.begin(); server != dns_servers.end(); server++) { struct IP46Address dns_server_addr; // Parse the DNS server's IP address. if (inet_pton(AF_INET, server->c_str(), &dns_server_addr.addr.ipv4)) { dns_server_addr.af = AF_INET; } else if (inet_pton(AF_INET6, server->c_str(), &dns_server_addr.addr.ipv6)) { dns_server_addr.af = AF_INET6; } else if ((getaddrinfo(server->c_str(), NULL, NULL, &res)) == 0) { dns_server_addr.af = res->ai_family; if (dns_server_addr.af == AF_INET) { dns_server_addr.addr.ipv4 = ((struct sockaddr_in*)res->ai_addr)->sin_addr; } else if (dns_server_addr.af == AF_INET6) { dns_server_addr.addr.ipv6 = ((struct sockaddr_in6*)res->ai_addr)->sin6_addr; } freeaddrinfo(res); } else { TRC_ERROR("Failed to parse '%s' as IP address or resolve host name - defaulting to 127.0.0.1", server->c_str()); dns_server_addr.af = AF_INET; (void)inet_aton("127.0.0.1", &dns_server_addr.addr.ipv4); } _servers.push_back(dns_server_addr); } // We store a DNSResolver in thread-local data, so create the thread-local // store. pthread_key_create(&_thread_local, (void(*)(void*))DNSResolver::destroy); }
int ares_library_init_mem(int flags, void *(*amalloc)(size_t size), void (*afree)(void *ptr), void *(*arealloc)(void *ptr, size_t size)) { if (amalloc) ares_malloc = amalloc; if (arealloc) ares_realloc = arealloc; if (afree) ares_free = afree; return ares_library_init(flags); }
int resolv_init(struct ev_loop *loop, char *nameservers, int ipv6first) { int status; if (ipv6first) resolv_mode = MODE_IPV6_FIRST; else resolv_mode = MODE_IPV4_FIRST; default_loop = loop; if ((status = ares_library_init(ARES_LIB_INIT_ALL)) != ARES_SUCCESS) { LOGE("c-ares error: %s", ares_strerror(status)); FATAL("failed to initialize c-ares"); } memset(&default_ctx, 0, sizeof(struct resolv_ctx)); default_ctx.options.sock_state_cb_data = &default_ctx; default_ctx.options.sock_state_cb = resolv_sock_state_cb; default_ctx.options.timeout = 3000; default_ctx.options.tries = 2; status = ares_init_options(&default_ctx.channel, &default_ctx.options, #if ARES_VERSION_MAJOR >= 1 && ARES_VERSION_MINOR >= 12 ARES_OPT_NOROTATE | #endif ARES_OPT_TIMEOUTMS | ARES_OPT_TRIES | ARES_OPT_SOCK_STATE_CB); if (status != ARES_SUCCESS) { FATAL("failed to initialize c-ares"); } if (nameservers != NULL) { #if ARES_VERSION_MAJOR >= 1 && ARES_VERSION_MINOR >= 11 status = ares_set_servers_ports_csv(default_ctx.channel, nameservers); #else status = ares_set_servers_csv(default_ctx.channel, nameservers); #endif } if (status != ARES_SUCCESS) { FATAL("failed to set nameservers"); } ev_init(&default_ctx.io, resolv_sock_cb); ev_timer_init(&default_ctx.tw, resolv_timeout_cb, 0.0, 0.0); return 0; }
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; }
void dns_library_init(void) { int status; status = ares_library_init(ARES_LIB_INIT_ALL); if (status != ARES_SUCCESS) { errprintf("Cannot initialize ARES library: %s\n", ares_strerror(status)); return; } dns_atype= dns_name_type("A"); dns_aaaatype= dns_name_type("AAAA"); dns_aclass = dns_name_class("IN"); }
void init_lib() { if(init_) { return; } int result = ares_library_init(ARES_LIB_INIT_ALL); if (result != ARES_SUCCESS) { TORNADO_LOG_ERROR("ares_library_init: %s", ares_strerror(result)); throw std::runtime_error( std::string("ares_library_init fail :") + ares_strerror(result) ); } init_ = 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; } }
struct hostent *nssrs_resolver_by_servers(char *name, char *nameserver) { ares_channel channel = NULL; int status, optmask = 0; struct ares_options options; struct hostent *results; status = ares_library_init(ARES_LIB_INIT_ALL); if (status != ARES_SUCCESS) { debug("ares_library_init: %s\n", ares_strerror(status)); return NULL; } optmask = ARES_OPT_SERVERS | ARES_OPT_UDP_PORT; options.servers = NULL; options.nservers = 0; options.flags = ARES_FLAG_NOCHECKRESP; status = ares_init_options(&channel, &options, optmask); if(status != ARES_SUCCESS) { debug("ares_init_options: %s\n", ares_strerror(status)); return NULL; } status = ares_set_servers_csv(channel, nameserver); if (status != ARES_SUCCESS) { debug("ares_set_servers_csv: %s\n", ares_strerror(status)); ares_destroy(channel); ares_library_cleanup(); return NULL; } // Wait resolver results = malloc(sizeof(struct hostent)); ares_gethostbyname(channel, name, AF_INET, &callback, results); wait_ares(channel); ares_destroy(channel); ares_library_cleanup(); if (results->h_name != NULL) { return results; } free(results); return NULL; }
static void do_ares_init(void) { int res = ares_library_init(ARES_LIB_INIT_ALL); atexit(do_ares_fini); if (res) { ph_panic("ares_library_init failed: %s", ares_strerror(res)); } ph_memtype_register_block(sizeof(defs)/sizeof(defs[0]), defs, &mt.chan); // This must be the last thing we do in this function default_channel = create_chan(); if (!default_channel) { ph_panic("failed to create default DNS channel"); } }
int main(int argc, char *argv[]) { int status; st_init(); status = ares_library_init(ARES_LIB_INIT_ALL); if (status != ARES_SUCCESS) { fprintf(stderr, "ares_library_init: %s\n", ares_strerror(status)); return 1; } st_thread_t t = st_thread_create(do_lookup, (void *)"A", 1, 1024 * 128); st_thread_t t2 = st_thread_create(do_lookup, (void *)"B", 1, 1024 * 128); st_thread_join(t, NULL); st_thread_join(t2, NULL); ares_library_cleanup(); return 0; }
static int DNSResolver_tp_init(DNSResolver *self, PyObject *args, PyObject *kwargs) { int r, optmask; struct ares_options options; Loop *loop; PyObject *servers = NULL; static char *kwlist[] = {"loop", "servers", NULL}; if (self->channel) { PyErr_SetString(PyExc_DNSError, "Object already initialized"); return -1; } if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O!|O:__init__", kwlist, &LoopType, &loop, &servers)) { return -1; } Py_INCREF(loop); self->loop = loop; r = ares_library_init(ARES_LIB_INIT_ALL); if (r != 0) { PyErr_SetString(PyExc_DNSError, "error initializing c-ares library"); return -1; } optmask = ARES_OPT_FLAGS; options.flags = ARES_FLAG_USEVC; r = uv_ares_init_options(UV_LOOP(self), &self->channel, &options, optmask); if (r) { PyErr_SetString(PyExc_DNSError, "error c-ares library options"); return -1; } if (servers) { return set_dns_servers(self, servers); } return 0; }
void _mosquitto_net_init(void) { #ifdef WIN32 WSADATA wsaData; WSAStartup(MAKEWORD(2,2), &wsaData); #endif #ifdef WITH_SRV ares_library_init(ARES_LIB_INIT_ALL); #endif #ifdef WITH_TLS SSL_load_error_strings(); SSL_library_init(); OpenSSL_add_all_algorithms(); if(tls_ex_index_mosq == -1){ tls_ex_index_mosq = SSL_get_ex_new_index(0, "client context", NULL, NULL, NULL); } #endif }
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__); }
DnsCachedResolver::DnsCachedResolver(const std::string& dns_server) : _cache_lock(PTHREAD_MUTEX_INITIALIZER), _cache() { LOG_STATUS("Creating Cached Resolver using server %s", dns_server.c_str()); // Initialize the ares library. This might have already been done by curl // but it's safe to do it twice. ares_library_init(ARES_LIB_INIT_ALL); // Parse the DNS server's IP address. if (!inet_aton(dns_server.c_str(), &_dns_server)) { LOG_ERROR("Failed to parse '%s' as IP address - defaulting to 127.0.0.1", dns_server.c_str()); (void)inet_aton("127.0.0.1", &_dns_server); } // We store a DNSResolver in thread-local data, so create the thread-local // store. pthread_key_create(&_thread_local, (void(*)(void*))&destroy_dns_channel); }
// Bad synchronous gethostbyname demo uint32_t tm__sync_gethostbyname (const char *domain) { ares_channel channel; int status; struct ares_options options; int optmask = 0; ipaddr[0] = ipaddr[1] = ipaddr[2] = ipaddr[3] = 0; struct in_addr ns1; inet_aton("8.8.8.8",&ns1); status = ares_library_init(ARES_LIB_INIT_ALL); if (status != ARES_SUCCESS){ printf("ares_library_init: %s\n", ares_strerror(status)); return 1; } options.servers = &ns1; options.nservers = 1; optmask |= ARES_OPT_SERVERS; options.sock_state_cb = state_cb; // options.sock_state_cb_data; optmask |= ARES_OPT_SOCK_STATE_CB; status = ares_init_options(&channel, &options, optmask); if(status != ARES_SUCCESS) { printf("ares_init_options: %s\n", ares_strerror(status)); return 1; } ares_gethostbyname(channel, domain, AF_INET, callback, NULL); wait_ares(channel); ares_destroy(channel); ares_library_cleanup(); // printf("fin\n"); // printf("result => %d.%d.%d.%d\n", ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]); return (ipaddr[0] << 24) | (ipaddr[1] << 16) | (ipaddr[2] << 8) | ipaddr[3]; }
int ecore_con_info_init(void) { struct ares_options opts; if (!info_init) { if (ares_library_init(ARES_LIB_INIT_ALL)) return 0; opts.lookups = "fb"; /* hosts file then dns */ opts.sock_state_cb = _ecore_con_info_cares_state_cb; if (ares_init_options(&info_channel, &opts, ARES_OPT_LOOKUPS | ARES_OPT_SOCK_STATE_CB) != ARES_SUCCESS) { ares_library_cleanup(); return 0; } } info_init++; return info_init; }
int luvit_init(lua_State *L, uv_loop_t* loop, int argc, char *argv[]) { int index, rc; ares_channel channel; struct ares_options options; luvit__suck_in_symbols(); memset(&options, 0, sizeof(options)); rc = ares_library_init(ARES_LIB_INIT_ALL); assert(rc == ARES_SUCCESS); // Pull up the preload table lua_getglobal(L, "package"); lua_getfield(L, -1, "preload"); lua_remove(L, -2); // Register yajl lua_pushcfunction(L, luaopen_yajl); lua_setfield(L, -2, "yajl"); // Register os lua_pushcfunction(L, luaopen_os_binding); lua_setfield(L, -2, "os_binding"); // Register http_parser lua_pushcfunction(L, luaopen_http_parser); lua_setfield(L, -2, "http_parser"); // Register uv lua_pushcfunction(L, luaopen_uv); lua_setfield(L, -2, "uv"); // Register env lua_pushcfunction(L, luaopen_env); lua_setfield(L, -2, "env"); // Register constants lua_pushcfunction(L, luaopen_constants); lua_setfield(L, -2, "constants"); // We're done with preload, put it away lua_pop(L, 1); // Get argv lua_createtable (L, argc, 0); for (index = 0; index < argc; index++) { lua_pushstring (L, argv[index]); lua_rawseti(L, -2, index); } lua_setglobal(L, "argv"); lua_pushcfunction(L, luvit_exit); lua_setglobal(L, "exit_process"); lua_pushcfunction(L, luvit_print_stderr); lua_setglobal(L, "print_stderr"); lua_pushcfunction(L, luvit_getcwd); lua_setglobal(L, "getcwd"); lua_pushstring(L, LUVIT_VERSION); lua_setglobal(L, "VERSION"); lua_pushstring(L, UV_VERSION); lua_setglobal(L, "UV_VERSION"); lua_pushstring(L, LUAJIT_VERSION); lua_setglobal(L, "LUAJIT_VERSION"); lua_pushstring(L, HTTP_VERSION); lua_setglobal(L, "HTTP_VERSION"); lua_pushstring(L, YAJL_VERSIONISH); lua_setglobal(L, "YAJL_VERSION"); // Hold a reference to the main thread in the registry assert(lua_pushthread(L) == 1); lua_setfield(L, LUA_REGISTRYINDEX, "main_thread"); // Store the loop within the registry luv_set_loop(L, loop); // Store the ARES Channel uv_ares_init_options(luv_get_loop(L), &channel, &options, 0); luv_set_ares_channel(L, &channel); return 0; }
/* Init function to start auth worker threads and adns thread. It also initializes the c-ares library and c-ares channel*/ int auth_mgr_init(uint32_t init_flags) { int ret,flags; NKN_MUTEX_INIT(&authreq_mutex, NULL, "authreq_mutex"); /*********************************************************** * We do not need this for now, but will eventually need * to handle the auth taks, key management etcc.. ***********************************************************/ if (init_flags & AUTH_MGR_INIT_START_WORKER_TH) { int32_t i; for (i=0; i<MAX_AM_THREADS; i++) { DBG_LOG(MSG, MOD_AUTHMGR, "Thread %d created for Auth Manager",i); if(pthread_create(&auththr[i], NULL, auth_mgr_input_handler_thread, (void *)&i) != 0) { DBG_LOG(SEVERE, MOD_AUTHMGR, "Failed to create authmgr threads %d",i); DBG_ERR(SEVERE, "Failed to create authmgr threads %d",i); return -1; } } } auth_htbl_list_init(); /*All the c-ares relates stuff goes in here*/ if (adnsd_enabled) { return adns_client_init(); } flags = ARES_LIB_INIT_ALL; ares_library_cleanup(); ret = ares_library_init(flags); if (ret != ARES_SUCCESS) { DBG_LOG(SEVERE, MOD_AUTHMGR,"ares_library_init: %d %s", ret, ares_strerror(ret)); return -1; } optmask = ARES_OPT_FLAGS|ARES_OPT_TIMEOUT|ARES_OPT_TRIES; options.flags = ARES_FLAG_STAYOPEN|ARES_FLAG_IGNTC|ARES_FLAG_NOCHECKRESP; options.timeout = DNS_MAX_TIMEOUT; options.tries = DNS_MAX_RETRIES; ret = ares_init_options(&channel, &options, optmask); if (ret != ARES_SUCCESS) { DBG_LOG(SEVERE, MOD_AUTHMGR,"ares_init: %d %s", ret, ares_strerror(ret) ); return -1; } channel_ready = 1; /* Additionally one more thread needs to handle all the DNS queries, so starting it here*/ if (pthread_create(&adnsthr, NULL, auth_mgr_adns_handler_epoll, NULL) != 0) { DBG_LOG(SEVERE, MOD_AUTHMGR, "Failed to create adns thread"); DBG_ERR(SEVERE,"Failed to create adns thread"); return -1; } return 0; }
static int Channel_tp_init(Channel *self, PyObject *args, PyObject *kwargs) { int r, flags, tries, ndots, tcp_port, udp_port, optmask, ndomains, socket_send_buffer_size, socket_receive_buffer_size; char *lookups; char **c_domains; double timeout; struct ares_options options; PyObject *servers, *domains, *sock_state_cb; static char *kwlist[] = {"flags", "timeout", "tries", "ndots", "tcp_port", "udp_port", "servers", "domains", "lookups", "sock_state_cb", "socket_send_buffer_size", "socket_receive_buffer_size", NULL}; optmask = 0; flags = tries = ndots = tcp_port = udp_port = socket_send_buffer_size = socket_receive_buffer_size = -1; timeout = -1.0; lookups = NULL; c_domains = NULL; servers = domains = sock_state_cb = NULL; if (self->channel) { PyErr_SetString(PyExc_AresError, "Object already initialized"); return -1; } if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|idiiiiOOsOii:__init__", kwlist, &flags, &timeout, &tries, &ndots, &tcp_port, &udp_port, &servers, &domains, &lookups, &sock_state_cb, &socket_send_buffer_size, &socket_receive_buffer_size)) { return -1; } if (sock_state_cb && !PyCallable_Check(sock_state_cb)) { PyErr_SetString(PyExc_TypeError, "sock_state_cb is not callable"); return -1; } r = ares_library_init(ARES_LIB_INIT_ALL); if (r != ARES_SUCCESS) { RAISE_ARES_EXCEPTION(r); return -1; } self->lib_initialized = True; memset(&options, 0, sizeof(struct ares_options)); if (flags != -1) { options.flags = flags; optmask |= ARES_OPT_FLAGS; } if (timeout != -1) { options.timeout = (int)timeout * 1000; optmask |= ARES_OPT_TIMEOUTMS; } if (tries != -1) { options.tries = tries; optmask |= ARES_OPT_TRIES; } if (ndots != -1) { options.ndots = ndots; optmask |= ARES_OPT_NDOTS; } if (tcp_port != -1) { options.tcp_port = tcp_port; optmask |= ARES_OPT_TCP_PORT; } if (udp_port != -1) { options.udp_port = udp_port; optmask |= ARES_OPT_UDP_PORT; } if (socket_send_buffer_size != -1) { options.socket_send_buffer_size = socket_send_buffer_size; optmask |= ARES_OPT_SOCK_SNDBUF; } if (socket_receive_buffer_size != -1) { options.socket_receive_buffer_size = socket_receive_buffer_size; optmask |= ARES_OPT_SOCK_RCVBUF; } if (sock_state_cb) { options.sock_state_cb = ares__sock_state_cb; options.sock_state_cb_data = (void *)self; optmask |= ARES_OPT_SOCK_STATE_CB; Py_INCREF(sock_state_cb); self->sock_state_cb = sock_state_cb; } if (lookups) { options.lookups = lookups; optmask |= ARES_OPT_LOOKUPS; } if (domains) { process_domains(domains, &c_domains, &ndomains); if (ndomains == -1) { goto error; } options.domains = c_domains; options.ndomains = ndomains; optmask |= ARES_OPT_DOMAINS; } r = ares_init_options(&self->channel, &options, optmask); if (r != ARES_SUCCESS) { RAISE_ARES_EXCEPTION(r); goto error; } free_domains(c_domains); if (servers) { return set_nameservers(self, servers); } return 0; error: free_domains(c_domains); Py_XDECREF(sock_state_cb); return -1; }
int main(int argc, char **argv) { ares_channel channel; int c, i, optmask = ARES_OPT_FLAGS, dnsclass = C_IN, type = T_A; int status, nfds, count; struct ares_options options; struct hostent *hostent; fd_set read_fds, write_fds; struct timeval *tvp, tv; struct ares_addr_node *srvr, *servers = NULL; #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; } options.flags = ARES_FLAG_NOCHECKRESP; options.servers = NULL; options.nservers = 0; while ((c = ares_getopt(argc, argv, "df:s:c:t:T:U:")) != -1) { switch (c) { case 'd': #ifdef WATT32 dbug_init(); #endif break; case 'f': /* Add a flag. */ for (i = 0; i < nflags; i++) { if (strcmp(flags[i].name, optarg) == 0) break; } if (i < nflags) options.flags |= flags[i].value; else usage(); break; case 's': /* User-specified name servers override default ones. */ srvr = malloc(sizeof(struct ares_addr_node)); if (!srvr) { fprintf(stderr, "Out of memory!\n"); destroy_addr_list(servers); return 1; } append_addr_list(&servers, srvr); if (ares_inet_pton(AF_INET, optarg, &srvr->addr.addr4) > 0) srvr->family = AF_INET; else if (ares_inet_pton(AF_INET6, optarg, &srvr->addr.addr6) > 0) srvr->family = AF_INET6; else { hostent = gethostbyname(optarg); if (!hostent) { fprintf(stderr, "adig: server %s not found.\n", optarg); destroy_addr_list(servers); return 1; } switch (hostent->h_addrtype) { case AF_INET: srvr->family = AF_INET; memcpy(&srvr->addr.addr4, hostent->h_addr, sizeof(srvr->addr.addr4)); break; case AF_INET6: srvr->family = AF_INET6; memcpy(&srvr->addr.addr6, hostent->h_addr, sizeof(srvr->addr.addr6)); break; default: fprintf(stderr, "adig: server %s unsupported address family.\n", optarg); destroy_addr_list(servers); return 1; } } /* Notice that calling ares_init_options() without servers in the * options struct and with ARES_OPT_SERVERS set simultaneously in * the options mask, results in an initialization with no servers. * When alternative name servers have been specified these are set * later calling ares_set_servers() overriding any existing server * configuration. To prevent initial configuration with default * servers that will be discarded later, ARES_OPT_SERVERS is set. * If this flag is not set here the result shall be the same but * ares_init_options() will do needless work. */ optmask |= ARES_OPT_SERVERS; break; case 'c': /* Set the query class. */ for (i = 0; i < nclasses; i++) { if (strcasecmp(classes[i].name, optarg) == 0) break; } if (i < nclasses) dnsclass = classes[i].value; else usage(); break; case 't': /* Set the query type. */ for (i = 0; i < ntypes; i++) { if (strcasecmp(types[i].name, optarg) == 0) break; } if (i < ntypes) type = types[i].value; else usage(); break; case 'T': /* Set the TCP port number. */ if (!ISDIGIT(*optarg)) usage(); options.tcp_port = (unsigned short)strtol(optarg, NULL, 0); optmask |= ARES_OPT_TCP_PORT; break; case 'U': /* Set the UDP port number. */ if (!ISDIGIT(*optarg)) usage(); options.udp_port = (unsigned short)strtol(optarg, NULL, 0); optmask |= ARES_OPT_UDP_PORT; break; } } argc -= optind; argv += optind; if (argc == 0) usage(); status = ares_init_options(&channel, &options, optmask); if (status != ARES_SUCCESS) { fprintf(stderr, "ares_init_options: %s\n", ares_strerror(status)); return 1; } if(servers) { status = ares_set_servers(channel, servers); destroy_addr_list(servers); if (status != ARES_SUCCESS) { fprintf(stderr, "ares_init_options: %s\n", ares_strerror(status)); return 1; } } /* Initiate the queries, one per command-line argument. If there is * only one query to do, supply NULL as the callback argument; * otherwise, supply the query name as an argument so we can * distinguish responses for the user when printing them out. */ if (argc == 1) ares_query(channel, *argv, dnsclass, type, callback, (char *) NULL); else { for (; *argv; argv++) ares_query(channel, *argv, dnsclass, type, 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); count = select(nfds, &read_fds, &write_fds, NULL, tvp); if (count < 0 && SOCKERRNO != EINVAL) { perror("select"); return 1; } ares_process(channel, &read_fds, &write_fds); } ares_destroy(channel); ares_library_cleanup(); #ifdef USE_WINSOCK WSACleanup(); #endif return 0; }
int main(int argc, char **argv) { struct ares_options options; int optmask = 0; 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 memset(&options, 0, sizeof(options)); 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:hs:")) != -1) { switch (c) { case 'd': #ifdef WATT32 dbug_init(); #endif break; case 's': optmask |= ARES_OPT_DOMAINS; options.ndomains++; options.domains = realloc(options.domains, options.ndomains * sizeof(char *)); options.domains[options.ndomains - 1] = strdup(optarg); break; case 't': if (!strcasecmp(optarg,"a")) addr_family = AF_INET; else if (!strcasecmp(optarg,"aaaa")) addr_family = AF_INET6; else if (!strcasecmp(optarg,"u")) addr_family = AF_UNSPEC; else usage(); break; case 'h': default: usage(); break; } } argc -= optind; argv += optind; if (argc < 1) usage(); status = ares_init_options(&channel, &options, optmask); 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 (;;) { int res; 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); res = select(nfds, &read_fds, &write_fds, NULL, tvp); if (-1 == res) break; ares_process(channel, &read_fds, &write_fds); } ares_destroy(channel); ares_library_cleanup(); #ifdef USE_WINSOCK WSACleanup(); #endif return 0; }