Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 1 | /*************************************************************************** |
| 2 | * _ _ ____ _ |
| 3 | * Project ___| | | | _ \| | |
| 4 | * / __| | | | |_) | | |
| 5 | * | (__| |_| | _ <| |___ |
| 6 | * \___|\___/|_| \_\_____| |
| 7 | * |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 8 | * Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 9 | * |
| 10 | * This software is licensed as described in the file COPYING, which |
| 11 | * you should have received as part of this distribution. The terms |
Alex Deymo | d15eaac | 2016-06-28 14:49:26 -0700 | [diff] [blame] | 12 | * are also available at https://curl.haxx.se/docs/copyright.html. |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 13 | * |
| 14 | * You may opt to use, copy, modify, merge, publish, distribute and/or sell |
| 15 | * copies of the Software, and permit persons to whom the Software is |
| 16 | * furnished to do so, under the terms of the COPYING file. |
| 17 | * |
| 18 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY |
| 19 | * KIND, either express or implied. |
| 20 | * |
| 21 | ***************************************************************************/ |
| 22 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 23 | #include "curl_setup.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 24 | |
Alex Deymo | 486467e | 2017-12-19 19:04:07 +0100 | [diff] [blame] | 25 | /*********************************************************************** |
| 26 | * Only for builds using asynchronous name resolves |
| 27 | **********************************************************************/ |
| 28 | #ifdef CURLRES_ASYNCH |
| 29 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 30 | #ifdef HAVE_NETINET_IN_H |
| 31 | #include <netinet/in.h> |
| 32 | #endif |
| 33 | #ifdef HAVE_NETDB_H |
| 34 | #include <netdb.h> |
| 35 | #endif |
| 36 | #ifdef HAVE_ARPA_INET_H |
| 37 | #include <arpa/inet.h> |
| 38 | #endif |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 39 | #ifdef __VMS |
| 40 | #include <in.h> |
| 41 | #include <inet.h> |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 42 | #endif |
| 43 | |
| 44 | #ifdef HAVE_PROCESS_H |
| 45 | #include <process.h> |
| 46 | #endif |
| 47 | |
| 48 | #include "urldata.h" |
| 49 | #include "sendf.h" |
| 50 | #include "hostip.h" |
| 51 | #include "hash.h" |
| 52 | #include "share.h" |
| 53 | #include "strerror.h" |
| 54 | #include "url.h" |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 55 | #include "curl_memory.h" |
| 56 | /* The last #include file should be: */ |
| 57 | #include "memdebug.h" |
| 58 | |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 59 | /* |
| 60 | * Curl_addrinfo_callback() gets called by ares, gethostbyname_thread() |
| 61 | * or getaddrinfo_thread() when we got the name resolved (or not!). |
| 62 | * |
| 63 | * If the status argument is CURL_ASYNC_SUCCESS, this function takes |
| 64 | * ownership of the Curl_addrinfo passed, storing the resolved data |
| 65 | * in the DNS cache. |
| 66 | * |
| 67 | * The storage operation locks and unlocks the DNS cache. |
| 68 | */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 69 | CURLcode Curl_addrinfo_callback(struct connectdata *conn, |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 70 | int status, |
| 71 | struct Curl_addrinfo *ai) |
| 72 | { |
| 73 | struct Curl_dns_entry *dns = NULL; |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 74 | CURLcode result = CURLE_OK; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 75 | |
| 76 | conn->async.status = status; |
| 77 | |
| 78 | if(CURL_ASYNC_SUCCESS == status) { |
| 79 | if(ai) { |
Alex Deymo | e3149cc | 2016-10-05 11:18:42 -0700 | [diff] [blame] | 80 | struct Curl_easy *data = conn->data; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 81 | |
| 82 | if(data->share) |
| 83 | Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE); |
| 84 | |
| 85 | dns = Curl_cache_addr(data, ai, |
| 86 | conn->async.hostname, |
| 87 | conn->async.port); |
| 88 | if(!dns) { |
| 89 | /* failed to store, cleanup and return error */ |
| 90 | Curl_freeaddrinfo(ai); |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 91 | result = CURLE_OUT_OF_MEMORY; |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if(data->share) |
| 95 | Curl_share_unlock(data, CURL_LOCK_DATA_DNS); |
| 96 | } |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 97 | else { |
| 98 | result = CURLE_OUT_OF_MEMORY; |
| 99 | } |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | conn->async.dns = dns; |
| 103 | |
| 104 | /* Set async.done TRUE last in this function since it may be used multi- |
| 105 | threaded and once this is TRUE the other thread may read fields from the |
| 106 | async struct */ |
| 107 | conn->async.done = TRUE; |
| 108 | |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 109 | /* IPv4: The input hostent struct will be freed by ares when we return from |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 110 | this function */ |
Bertrand SIMONNET | e6cd738 | 2015-07-01 15:39:44 -0700 | [diff] [blame] | 111 | return result; |
| 112 | } |
| 113 | |
| 114 | /* Call this function after Curl_connect() has returned async=TRUE and |
| 115 | then a successful name resolve has been received. |
| 116 | |
| 117 | Note: this function disconnects and frees the conn data in case of |
| 118 | resolve failure */ |
| 119 | CURLcode Curl_async_resolved(struct connectdata *conn, |
| 120 | bool *protocol_done) |
| 121 | { |
| 122 | CURLcode result; |
| 123 | |
| 124 | if(conn->async.dns) { |
| 125 | conn->dns_entry = conn->async.dns; |
| 126 | conn->async.dns = NULL; |
| 127 | } |
| 128 | |
| 129 | result = Curl_setup_conn(conn, protocol_done); |
| 130 | |
| 131 | if(result) |
| 132 | /* We're not allowed to return failure with memory left allocated |
| 133 | in the connectdata struct, free those here */ |
| 134 | Curl_disconnect(conn, FALSE); /* close the connection */ |
| 135 | |
| 136 | return result; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Curl_getaddrinfo() is the generic low-level name resolve API within this |
| 141 | * source file. There are several versions of this function - for different |
| 142 | * name resolve layers (selected at build-time). They all take this same set |
| 143 | * of arguments |
| 144 | */ |
| 145 | Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn, |
| 146 | const char *hostname, |
| 147 | int port, |
| 148 | int *waitp) |
| 149 | { |
| 150 | return Curl_resolver_getaddrinfo(conn, hostname, port, waitp); |
Kristian Monsen | 5ab5018 | 2010-05-14 18:53:44 +0100 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | #endif /* CURLRES_ASYNCH */ |