source: UsbWattMeter/trunk/curl-7.47.1/lib/hostip6.c@ 164

Last change on this file since 164 was 164, checked in by coas-nagasima, 6 years ago

TOPPERS/ECNLサンプルアプリ「USB充電器電力計」を追加

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
  • Property svn:mime-type set to text/x-csrc
File size: 6.2 KB
Line 
1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2015, 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.haxx.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 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#ifdef HAVE_NETINET_IN_H
26#include <netinet/in.h>
27#endif
28#ifdef HAVE_NETDB_H
29#include <netdb.h>
30#endif
31#ifdef HAVE_ARPA_INET_H
32#include <arpa/inet.h>
33#endif
34#ifdef __VMS
35#include <in.h>
36#include <inet.h>
37#endif
38
39#ifdef HAVE_PROCESS_H
40#include <process.h>
41#endif
42
43#include "urldata.h"
44#include "sendf.h"
45#include "hostip.h"
46#include "hash.h"
47#include "share.h"
48#include "strerror.h"
49#include "url.h"
50#include "inet_pton.h"
51#include "connect.h"
52#include "curl_printf.h"
53#include "curl_memory.h"
54/* The last #include file should be: */
55#include "memdebug.h"
56
57/***********************************************************************
58 * Only for IPv6-enabled builds
59 **********************************************************************/
60#ifdef CURLRES_IPV6
61
62
63#if defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO)
64/* These are strictly for memory tracing and are using the same style as the
65 * family otherwise present in memdebug.c. I put these ones here since they
66 * require a bunch of structs I didn't want to include in memdebug.c
67 */
68
69/*
70 * For CURLRES_ARS, this should be written using ares_gethostbyaddr()
71 * (ignoring the fact c-ares doesn't return 'serv').
72 */
73
74int curl_dogetnameinfo(GETNAMEINFO_QUAL_ARG1 GETNAMEINFO_TYPE_ARG1 sa,
75 GETNAMEINFO_TYPE_ARG2 salen,
76 char *host, GETNAMEINFO_TYPE_ARG46 hostlen,
77 char *serv, GETNAMEINFO_TYPE_ARG46 servlen,
78 GETNAMEINFO_TYPE_ARG7 flags,
79 int line, const char *source)
80{
81 int res = (getnameinfo)(sa, salen,
82 host, hostlen,
83 serv, servlen,
84 flags);
85 if(0 == res)
86 /* success */
87 curl_memlog("GETNAME %s:%d getnameinfo()\n",
88 source, line);
89 else
90 curl_memlog("GETNAME %s:%d getnameinfo() failed = %d\n",
91 source, line, res);
92 return res;
93}
94#endif /* defined(CURLDEBUG) && defined(HAVE_GETNAMEINFO) */
95
96/*
97 * Curl_ipv6works() returns TRUE if IPv6 seems to work.
98 */
99bool Curl_ipv6works(void)
100{
101 /* the nature of most system is that IPv6 status doesn't come and go
102 during a program's lifetime so we only probe the first time and then we
103 have the info kept for fast re-use */
104 static int ipv6_works = -1;
105 if(-1 == ipv6_works) {
106 /* probe to see if we have a working IPv6 stack */
107 curl_socket_t s = socket(PF_INET6, SOCK_DGRAM, 0);
108 if(s == CURL_SOCKET_BAD)
109 /* an IPv6 address was requested but we can't get/use one */
110 ipv6_works = 0;
111 else {
112 ipv6_works = 1;
113 Curl_closesocket(NULL, s);
114 }
115 }
116 return (ipv6_works>0)?TRUE:FALSE;
117}
118
119/*
120 * Curl_ipvalid() checks what CURL_IPRESOLVE_* requirements that might've
121 * been set and returns TRUE if they are OK.
122 */
123bool Curl_ipvalid(struct connectdata *conn)
124{
125 if(conn->ip_version == CURL_IPRESOLVE_V6)
126 return Curl_ipv6works();
127 return TRUE;
128}
129
130#if defined(CURLRES_SYNCH)
131
132#ifdef DEBUG_ADDRINFO
133static void dump_addrinfo(struct connectdata *conn, const Curl_addrinfo *ai)
134{
135 printf("dump_addrinfo:\n");
136 for(; ai; ai = ai->ai_next) {
137 char buf[INET6_ADDRSTRLEN];
138
139 printf(" fam %2d, CNAME %s, ",
140 ai->ai_family, ai->ai_canonname ? ai->ai_canonname : "<none>");
141 if(Curl_printable_address(ai, buf, sizeof(buf)))
142 printf("%s\n", buf);
143 else
144 printf("failed; %s\n", Curl_strerror(conn, SOCKERRNO));
145 }
146}
147#else
148#define dump_addrinfo(x,y) Curl_nop_stmt
149#endif
150
151/*
152 * Curl_getaddrinfo() when built IPv6-enabled (non-threading and
153 * non-ares version).
154 *
155 * Returns name information about the given hostname and port number. If
156 * successful, the 'addrinfo' is returned and the forth argument will point to
157 * memory we need to free after use. That memory *MUST* be freed with
158 * Curl_freeaddrinfo(), nothing else.
159 */
160Curl_addrinfo *Curl_getaddrinfo(struct connectdata *conn,
161 const char *hostname,
162 int port,
163 int *waitp)
164{
165 struct addrinfo hints;
166 Curl_addrinfo *res;
167 int error;
168 char sbuf[12];
169 char *sbufptr = NULL;
170 char addrbuf[128];
171 int pf;
172 struct SessionHandle *data = conn->data;
173
174 *waitp = 0; /* synchronous response only */
175
176 /*
177 * Check if a limited name resolve has been requested.
178 */
179 switch(conn->ip_version) {
180 case CURL_IPRESOLVE_V4:
181 pf = PF_INET;
182 break;
183 case CURL_IPRESOLVE_V6:
184 pf = PF_INET6;
185 break;
186 default:
187 pf = PF_UNSPEC;
188 break;
189 }
190
191 if((pf != PF_INET) && !Curl_ipv6works())
192 /* The stack seems to be a non-IPv6 one */
193 pf = PF_INET;
194
195 memset(&hints, 0, sizeof(hints));
196 hints.ai_family = pf;
197 hints.ai_socktype = conn->socktype;
198
199 if((1 == Curl_inet_pton(AF_INET, hostname, addrbuf)) ||
200 (1 == Curl_inet_pton(AF_INET6, hostname, addrbuf))) {
201 /* the given address is numerical only, prevent a reverse lookup */
202 hints.ai_flags = AI_NUMERICHOST;
203 }
204
205 if(port) {
206 snprintf(sbuf, sizeof(sbuf), "%d", port);
207 sbufptr=sbuf;
208 }
209 error = Curl_getaddrinfo_ex(hostname, sbufptr, &hints, &res);
210 if(error) {
211 infof(data, "getaddrinfo(3) failed for %s:%d\n", hostname, port);
212 return NULL;
213 }
214
215 dump_addrinfo(conn, res);
216
217 return res;
218}
219#endif /* CURLRES_SYNCH */
220#endif /* CURLRES_IPV6 */
221
Note: See TracBrowser for help on using the repository browser.