123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703 |
- #include "curl_setup.h"
- #ifdef CURLRES_ARES
- #include <limits.h>
- #ifdef HAVE_NETINET_IN_H
- #include <netinet/in.h>
- #endif
- #ifdef HAVE_NETDB_H
- #include <netdb.h>
- #endif
- #ifdef HAVE_ARPA_INET_H
- #include <arpa/inet.h>
- #endif
- #ifdef __VMS
- #include <in.h>
- #include <inet.h>
- #endif
- #ifdef HAVE_PROCESS_H
- #include <process.h>
- #endif
- #if (defined(NETWARE) && defined(__NOVELL_LIBC__))
- #undef in_addr_t
- #define in_addr_t unsigned long
- #endif
- #include "urldata.h"
- #include "sendf.h"
- #include "hostip.h"
- #include "hash.h"
- #include "share.h"
- #include "strerror.h"
- #include "url.h"
- #include "multiif.h"
- #include "inet_pton.h"
- #include "connect.h"
- #include "select.h"
- #include "progress.h"
- # if defined(CURL_STATICLIB) && !defined(CARES_STATICLIB) && \
- (defined(WIN32) || defined(_WIN32) || defined(__SYMBIAN32__))
- # define CARES_STATICLIB
- # endif
- # include <ares.h>
- # include <ares_version.h> /* really old c-ares didn't include this by
- itself */
- #if ARES_VERSION >= 0x010500
- #define HAVE_CARES_CALLBACK_TIMEOUTS 1
- #endif
- #include "curl_printf.h"
- #include "curl_memory.h"
- #include "memdebug.h"
- struct ResolverResults {
- int num_pending;
- Curl_addrinfo *temp_ai;
- int last_status;
- };
- 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;
- }
- void Curl_resolver_global_cleanup(void)
- {
- #ifdef CARES_HAVE_ARES_LIBRARY_CLEANUP
- ares_library_cleanup();
- #endif
- }
- 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;
-
- }
- void Curl_resolver_cleanup(void *resolver)
- {
- ares_destroy((ares_channel)resolver);
- }
- int Curl_resolver_duphandle(void **to, void *from)
- {
-
- if(ARES_SUCCESS != ares_dup((ares_channel*)to, (ares_channel)from))
- return CURLE_FAILED_INIT;
- return CURLE_OK;
- }
- static void destroy_async_data(struct Curl_async *async);
- void Curl_resolver_cancel(struct connectdata *conn)
- {
- if(conn->data && conn->data->state.resolver)
- ares_cancel((ares_channel)conn->data->state.resolver);
- destroy_async_data(&conn->async);
- }
- static void destroy_async_data(struct Curl_async *async)
- {
- free(async->hostname);
- if(async->os_specific) {
- struct ResolverResults *res = (struct ResolverResults *)async->os_specific;
- if(res) {
- if(res->temp_ai) {
- Curl_freeaddrinfo(res->temp_ai);
- res->temp_ai = NULL;
- }
- free(res);
- }
- async->os_specific = NULL;
- }
- async->hostname = NULL;
- }
- int Curl_resolver_getsock(struct connectdata *conn,
- curl_socket_t *socks,
- int numsocks)
- {
- struct timeval maxtime;
- struct timeval timebuf;
- struct timeval *timeout;
- long milli;
- int max = ares_getsock((ares_channel)conn->data->state.resolver,
- (ares_socket_t *)socks, numsocks);
- maxtime.tv_sec = CURL_TIMEOUT_RESOLVE;
- maxtime.tv_usec = 0;
- timeout = ares_timeout((ares_channel)conn->data->state.resolver, &maxtime,
- &timebuf);
- milli = (timeout->tv_sec * 1000) + (timeout->tv_usec/1000);
- if(milli == 0)
- milli += 10;
- Curl_expire(conn->data, milli, EXPIRE_ASYNC_NAME);
- return max;
- }
- static int waitperform(struct connectdata *conn, int timeout_ms)
- {
- struct Curl_easy *data = conn->data;
- int nfds;
- int bitmask;
- ares_socket_t socks[ARES_GETSOCK_MAXNUM];
- struct pollfd pfd[ARES_GETSOCK_MAXNUM];
- int i;
- int num = 0;
- bitmask = ares_getsock((ares_channel)data->state.resolver, socks,
- ARES_GETSOCK_MAXNUM);
- for(i = 0; i < ARES_GETSOCK_MAXNUM; i++) {
- pfd[i].events = 0;
- pfd[i].revents = 0;
- if(ARES_GETSOCK_READABLE(bitmask, i)) {
- pfd[i].fd = socks[i];
- pfd[i].events |= POLLRDNORM|POLLIN;
- }
- if(ARES_GETSOCK_WRITABLE(bitmask, i)) {
- pfd[i].fd = socks[i];
- pfd[i].events |= POLLWRNORM|POLLOUT;
- }
- if(pfd[i].events != 0)
- num++;
- else
- break;
- }
- if(num)
- nfds = Curl_poll(pfd, num, timeout_ms);
- else
- nfds = 0;
- if(!nfds)
-
- ares_process_fd((ares_channel)data->state.resolver, ARES_SOCKET_BAD,
- ARES_SOCKET_BAD);
- else {
-
- for(i = 0; i < num; i++)
- ares_process_fd((ares_channel)data->state.resolver,
- pfd[i].revents & (POLLRDNORM|POLLIN)?
- pfd[i].fd:ARES_SOCKET_BAD,
- pfd[i].revents & (POLLWRNORM|POLLOUT)?
- pfd[i].fd:ARES_SOCKET_BAD);
- }
- return nfds;
- }
- CURLcode Curl_resolver_is_resolved(struct connectdata *conn,
- struct Curl_dns_entry **dns)
- {
- struct Curl_easy *data = conn->data;
- struct ResolverResults *res = (struct ResolverResults *)
- conn->async.os_specific;
- CURLcode result = CURLE_OK;
- if(dns)
- *dns = NULL;
- waitperform(conn, 0);
- if(res && !res->num_pending) {
- if(dns) {
- (void)Curl_addrinfo_callback(conn, res->last_status, res->temp_ai);
-
- res->temp_ai = NULL;
- }
- if(!conn->async.dns) {
- failf(data, "Could not resolve: %s (%s)",
- conn->async.hostname, ares_strerror(conn->async.status));
- result = conn->bits.proxy?CURLE_COULDNT_RESOLVE_PROXY:
- CURLE_COULDNT_RESOLVE_HOST;
- }
- else if(dns)
- *dns = conn->async.dns;
- destroy_async_data(&conn->async);
- }
- return result;
- }
- CURLcode Curl_resolver_wait_resolv(struct connectdata *conn,
- struct Curl_dns_entry **entry)
- {
- CURLcode result = CURLE_OK;
- struct Curl_easy *data = conn->data;
- timediff_t timeout;
- struct curltime now = Curl_now();
- struct Curl_dns_entry *temp_entry;
- if(entry)
- *entry = NULL;
- timeout = Curl_timeleft(data, &now, TRUE);
- if(timeout < 0) {
-
- connclose(conn, "Timed out before name resolve started");
- return CURLE_OPERATION_TIMEDOUT;
- }
- if(!timeout)
- timeout = CURL_TIMEOUT_RESOLVE * 1000;
-
- while(!result) {
- struct timeval *tvp, tv, store;
- int itimeout;
- int timeout_ms;
- itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
- store.tv_sec = itimeout/1000;
- store.tv_usec = (itimeout%1000)*1000;
- tvp = ares_timeout((ares_channel)data->state.resolver, &store, &tv);
-
- if(!tvp->tv_sec)
- timeout_ms = (int)(tvp->tv_usec/1000);
- else
- timeout_ms = 1000;
- waitperform(conn, timeout_ms);
- result = Curl_resolver_is_resolved(conn, entry?&temp_entry:NULL);
- if(result || conn->async.done)
- break;
- if(Curl_pgrsUpdate(conn))
- result = CURLE_ABORTED_BY_CALLBACK;
- else {
- struct curltime now2 = Curl_now();
- timediff_t timediff = Curl_timediff(now2, now);
- if(timediff <= 0)
- timeout -= 1;
- else if(timediff > timeout)
- timeout = -1;
- else
- timeout -= (long)timediff;
- now = now2;
- }
- if(timeout < 0)
- result = CURLE_OPERATION_TIMEDOUT;
- }
- if(result)
-
- ares_cancel((ares_channel)data->state.resolver);
-
- if(entry)
- *entry = conn->async.dns;
- if(result)
-
- connclose(conn, "c-ares resolve failed");
- return result;
- }
- static void compound_results(struct ResolverResults *res,
- Curl_addrinfo *ai)
- {
- Curl_addrinfo *ai_tail;
- if(!ai)
- return;
- ai_tail = ai;
- while(ai_tail->ai_next)
- ai_tail = ai_tail->ai_next;
-
- ai_tail->ai_next = res->temp_ai;
- res->temp_ai = ai;
- }
- static void query_completed_cb(void *arg,
- int status,
- #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
- int timeouts,
- #endif
- struct hostent *hostent)
- {
- struct connectdata *conn = (struct connectdata *)arg;
- struct ResolverResults *res;
- #ifdef HAVE_CARES_CALLBACK_TIMEOUTS
- (void)timeouts;
- #endif
- if(ARES_EDESTRUCTION == status)
-
- return;
- res = (struct ResolverResults *)conn->async.os_specific;
- if(res) {
- res->num_pending--;
- if(CURL_ASYNC_SUCCESS == status) {
- Curl_addrinfo *ai = Curl_he2ai(hostent, conn->async.port);
- if(ai) {
- compound_results(res, ai);
- }
- }
-
- if(res->last_status != ARES_SUCCESS)
- res->last_status = status;
- }
- }
- Curl_addrinfo *Curl_resolver_getaddrinfo(struct connectdata *conn,
- const char *hostname,
- int port,
- int *waitp)
- {
- char *bufp;
- struct Curl_easy *data = conn->data;
- struct in_addr in;
- int family = PF_INET;
- #ifdef ENABLE_IPV6
- struct in6_addr in6;
- #endif
- *waitp = 0;
-
- if(Curl_inet_pton(AF_INET, hostname, &in) > 0) {
-
- return Curl_ip2addr(AF_INET, &in, hostname, port);
- }
- #ifdef ENABLE_IPV6
-
- if(Curl_inet_pton (AF_INET6, hostname, &in6) > 0)
-
- return Curl_ip2addr(AF_INET6, &in6, hostname, port);
- switch(conn->ip_version) {
- default:
- #if ARES_VERSION >= 0x010601
- family = PF_UNSPEC;
- break;
- #endif
- case CURL_IPRESOLVE_V4:
- family = PF_INET;
- break;
- case CURL_IPRESOLVE_V6:
- family = PF_INET6;
- break;
- }
- #endif
- bufp = strdup(hostname);
- if(bufp) {
- struct ResolverResults *res = NULL;
- free(conn->async.hostname);
- conn->async.hostname = bufp;
- conn->async.port = port;
- conn->async.done = FALSE;
- conn->async.status = 0;
- conn->async.dns = NULL;
- res = calloc(sizeof(struct ResolverResults), 1);
- if(!res) {
- free(conn->async.hostname);
- conn->async.hostname = NULL;
- return NULL;
- }
- conn->async.os_specific = res;
-
- res->last_status = ARES_ENOTFOUND;
- #ifdef ENABLE_IPV6
- if(family == PF_UNSPEC) {
- if(Curl_ipv6works()) {
- res->num_pending = 2;
-
- ares_gethostbyname((ares_channel)data->state.resolver, hostname,
- PF_INET, query_completed_cb, conn);
- ares_gethostbyname((ares_channel)data->state.resolver, hostname,
- PF_INET6, query_completed_cb, conn);
- }
- else {
- res->num_pending = 1;
-
- ares_gethostbyname((ares_channel)data->state.resolver, hostname,
- PF_INET, query_completed_cb, conn);
- }
- }
- else
- #endif
- {
- res->num_pending = 1;
-
- ares_gethostbyname((ares_channel)data->state.resolver, hostname, family,
- query_completed_cb, conn);
- }
- *waitp = 1;
- }
- return NULL;
- }
- CURLcode Curl_set_dns_servers(struct Curl_easy *data,
- char *servers)
- {
- CURLcode result = CURLE_NOT_BUILT_IN;
- int ares_result;
-
- if(!(servers && servers[0]))
- return CURLE_OK;
- #if (ARES_VERSION >= 0x010704)
- ares_result = ares_set_servers_csv(data->state.resolver, servers);
- switch(ares_result) {
- case ARES_SUCCESS:
- result = CURLE_OK;
- break;
- case ARES_ENOMEM:
- result = CURLE_OUT_OF_MEMORY;
- break;
- case ARES_ENOTINITIALIZED:
- case ARES_ENODATA:
- case ARES_EBADSTR:
- default:
- result = CURLE_BAD_FUNCTION_ARGUMENT;
- break;
- }
- #else
- (void)data;
- (void)(ares_result);
- #endif
- return result;
- }
- CURLcode Curl_set_dns_interface(struct Curl_easy *data,
- const char *interf)
- {
- #if (ARES_VERSION >= 0x010704)
- if(!interf)
- interf = "";
- ares_set_local_dev((ares_channel)data->state.resolver, interf);
- return CURLE_OK;
- #else
- (void)data;
- (void)interf;
- return CURLE_NOT_BUILT_IN;
- #endif
- }
- CURLcode Curl_set_dns_local_ip4(struct Curl_easy *data,
- const char *local_ip4)
- {
- #if (ARES_VERSION >= 0x010704)
- struct in_addr a4;
- if((!local_ip4) || (local_ip4[0] == 0)) {
- a4.s_addr = 0;
- }
- else {
- if(Curl_inet_pton(AF_INET, local_ip4, &a4) != 1) {
- return CURLE_BAD_FUNCTION_ARGUMENT;
- }
- }
- ares_set_local_ip4((ares_channel)data->state.resolver, ntohl(a4.s_addr));
- return CURLE_OK;
- #else
- (void)data;
- (void)local_ip4;
- return CURLE_NOT_BUILT_IN;
- #endif
- }
- CURLcode Curl_set_dns_local_ip6(struct Curl_easy *data,
- const char *local_ip6)
- {
- #if (ARES_VERSION >= 0x010704) && defined(ENABLE_IPV6)
- unsigned char a6[INET6_ADDRSTRLEN];
- if((!local_ip6) || (local_ip6[0] == 0)) {
-
- memset(a6, 0, sizeof(a6));
- }
- else {
- if(Curl_inet_pton(AF_INET6, local_ip6, a6) != 1) {
- return CURLE_BAD_FUNCTION_ARGUMENT;
- }
- }
- ares_set_local_ip6((ares_channel)data->state.resolver, a6);
- return CURLE_OK;
- #else
- (void)data;
- (void)local_ip6;
- return CURLE_NOT_BUILT_IN;
- #endif
- }
- #endif
|