hostip6.c (4148B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 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 12 * are also available at https://curl.se/docs/copyright.html. 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 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 25 #include "curl_setup.h" 26 27 /*********************************************************************** 28 * Only for IPv6-enabled builds 29 **********************************************************************/ 30 #ifdef CURLRES_IPV6 31 32 #ifdef HAVE_NETINET_IN_H 33 #include <netinet/in.h> 34 #endif 35 #ifdef HAVE_NETDB_H 36 #include <netdb.h> 37 #endif 38 #ifdef HAVE_ARPA_INET_H 39 #include <arpa/inet.h> 40 #endif 41 #ifdef __VMS 42 #include <in.h> 43 #include <inet.h> 44 #endif 45 46 #include "urldata.h" 47 #include "cfilters.h" 48 #include "sendf.h" 49 #include "hostip.h" 50 #include "hash.h" 51 #include "share.h" 52 #include "url.h" 53 #include "curlx/inet_pton.h" 54 #include "connect.h" 55 /* The last 3 #include files should be in this order */ 56 #include "curl_printf.h" 57 #include "curl_memory.h" 58 #include "memdebug.h" 59 60 #if defined(CURLRES_SYNCH) 61 62 #ifdef DEBUG_ADDRINFO 63 static void dump_addrinfo(const struct Curl_addrinfo *ai) 64 { 65 printf("dump_addrinfo:\n"); 66 for(; ai; ai = ai->ai_next) { 67 char buf[INET6_ADDRSTRLEN]; 68 printf(" fam %2d, CNAME %s, ", 69 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>"); 70 Curl_printable_address(ai, buf, sizeof(buf)); 71 printf("%s\n", buf); 72 } 73 } 74 #else 75 #define dump_addrinfo(x) Curl_nop_stmt 76 #endif 77 78 /* 79 * Curl_sync_getaddrinfo() when built IPv6-enabled (non-threading and 80 * non-ares version). 81 * 82 * Returns name information about the given hostname and port number. If 83 * successful, the 'addrinfo' is returned and the fourth argument will point 84 * to memory we need to free after use. That memory *MUST* be freed with 85 * Curl_freeaddrinfo(), nothing else. 86 */ 87 struct Curl_addrinfo *Curl_sync_getaddrinfo(struct Curl_easy *data, 88 const char *hostname, 89 int port, 90 int ip_version) 91 { 92 struct addrinfo hints; 93 struct Curl_addrinfo *res; 94 int error; 95 char sbuf[12]; 96 char *sbufptr = NULL; 97 #ifndef USE_RESOLVE_ON_IPS 98 char addrbuf[128]; 99 #endif 100 int pf = PF_INET; 101 102 if((ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) 103 /* The stack seems to be IPv6-enabled */ 104 pf = PF_UNSPEC; 105 106 memset(&hints, 0, sizeof(hints)); 107 hints.ai_family = pf; 108 hints.ai_socktype = 109 (Curl_conn_get_transport(data, data->conn) == TRNSPRT_TCP) ? 110 SOCK_STREAM : SOCK_DGRAM; 111 112 #ifndef USE_RESOLVE_ON_IPS 113 /* 114 * The AI_NUMERICHOST must not be set to get synthesized IPv6 address from 115 * an IPv4 address on iOS and macOS. 116 */ 117 if((1 == curlx_inet_pton(AF_INET, hostname, addrbuf)) || 118 (1 == curlx_inet_pton(AF_INET6, hostname, addrbuf))) { 119 /* the given address is numerical only, prevent a reverse lookup */ 120 hints.ai_flags = AI_NUMERICHOST; 121 } 122 #endif 123 124 if(port) { 125 msnprintf(sbuf, sizeof(sbuf), "%d", port); 126 sbufptr = sbuf; 127 } 128 129 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res); 130 if(error) { 131 infof(data, "getaddrinfo(3) failed for %s:%d", hostname, port); 132 return NULL; 133 } 134 135 if(port) { 136 Curl_addrinfo_set_port(res, port); 137 } 138 139 dump_addrinfo(res); 140 141 return res; 142 } 143 #endif /* CURLRES_SYNCH */ 144 145 #endif /* CURLRES_IPV6 */