| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
#include "curl_setup.h" |
| 26 |
|
| 27 |
#include <curl/curl.h> |
| 28 |
|
| 29 |
#ifdef HAVE_NETINET_IN_H |
| 30 |
# include <netinet/in.h> |
| 31 |
#endif |
| 32 |
#ifdef HAVE_NETINET_IN6_H |
| 33 |
# include <netinet/in6.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 HAVE_SYS_UN_H |
| 42 |
# include <sys/un.h> |
| 43 |
#endif |
| 44 |
|
| 45 |
#ifdef __VMS |
| 46 |
# include <in.h> |
| 47 |
# include <inet.h> |
| 48 |
#endif |
| 49 |
|
| 50 |
#include <stddef.h> |
| 51 |
|
| 52 |
#include "curl_addrinfo.h" |
| 53 |
#include "fake_addrinfo.h" |
| 54 |
#include "curlx/inet_pton.h" |
| 55 |
#include "curlx/warnless.h" |
| 56 |
|
| 57 |
#include "curl_printf.h" |
| 58 |
#include "curl_memory.h" |
| 59 |
#include "memdebug.h" |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
#if defined(__INTEL_COMPILER) && (__INTEL_COMPILER == 910) && \ |
| 71 |
defined(__OPTIMIZE__) && defined(__unix__) && defined(__i386__) |
| 72 |
|
| 73 |
# define vqualifier volatile |
| 74 |
#else |
| 75 |
# define vqualifier |
| 76 |
#endif |
| 77 |
|
| 78 |
void |
| 79 |
Curl_freeaddrinfo(struct Curl_addrinfo *cahead) |
| 80 |
{ |
| 81 |
struct Curl_addrinfo *vqualifier canext; |
| 82 |
struct Curl_addrinfo *ca; |
| 83 |
|
| 84 |
for(ca = cahead; ca; ca = canext) { |
| 85 |
canext = ca->ai_next; |
| 86 |
free(ca); |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
|
| 91 |
#ifdef HAVE_GETADDRINFO |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
int |
| 107 |
Curl_getaddrinfo_ex(const char *nodename, |
| 108 |
const char *servname, |
| 109 |
const struct addrinfo *hints, |
| 110 |
struct Curl_addrinfo **result) |
| 111 |
{ |
| 112 |
const struct addrinfo *ai; |
| 113 |
struct addrinfo *aihead; |
| 114 |
struct Curl_addrinfo *cafirst = NULL; |
| 115 |
struct Curl_addrinfo *calast = NULL; |
| 116 |
struct Curl_addrinfo *ca; |
| 117 |
size_t ss_size; |
| 118 |
int error; |
| 119 |
|
| 120 |
*result = NULL; |
| 121 |
|
| 122 |
error = CURL_GETADDRINFO(nodename, servname, hints, &aihead); |
| 123 |
if(error) |
| 124 |
return error; |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
for(ai = aihead; ai != NULL; ai = ai->ai_next) { |
| 129 |
size_t namelen = ai->ai_canonname ? strlen(ai->ai_canonname) + 1 : 0; |
| 130 |
|
| 131 |
|
| 132 |
if(ai->ai_family == AF_INET) |
| 133 |
ss_size = sizeof(struct sockaddr_in); |
| 134 |
#ifdef USE_IPV6 |
| 135 |
else if(ai->ai_family == AF_INET6) |
| 136 |
ss_size = sizeof(struct sockaddr_in6); |
| 137 |
#endif |
| 138 |
else |
| 139 |
continue; |
| 140 |
|
| 141 |
|
| 142 |
if(!ai->ai_addr || !(ai->ai_addrlen > 0)) |
| 143 |
continue; |
| 144 |
|
| 145 |
|
| 146 |
if((size_t)ai->ai_addrlen < ss_size) |
| 147 |
continue; |
| 148 |
|
| 149 |
ca = malloc(sizeof(struct Curl_addrinfo) + ss_size + namelen); |
| 150 |
if(!ca) { |
| 151 |
error = EAI_MEMORY; |
| 152 |
break; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
ca->ai_flags = ai->ai_flags; |
| 159 |
ca->ai_family = ai->ai_family; |
| 160 |
ca->ai_socktype = ai->ai_socktype; |
| 161 |
ca->ai_protocol = ai->ai_protocol; |
| 162 |
ca->ai_addrlen = (curl_socklen_t)ss_size; |
| 163 |
ca->ai_addr = NULL; |
| 164 |
ca->ai_canonname = NULL; |
| 165 |
ca->ai_next = NULL; |
| 166 |
|
| 167 |
ca->ai_addr = (void *)((char *)ca + sizeof(struct Curl_addrinfo)); |
| 168 |
memcpy(ca->ai_addr, ai->ai_addr, ss_size); |
| 169 |
|
| 170 |
if(namelen) { |
| 171 |
ca->ai_canonname = (void *)((char *)ca->ai_addr + ss_size); |
| 172 |
memcpy(ca->ai_canonname, ai->ai_canonname, namelen); |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if(!cafirst) |
| 177 |
cafirst = ca; |
| 178 |
|
| 179 |
|
| 180 |
if(calast) |
| 181 |
calast->ai_next = ca; |
| 182 |
calast = ca; |
| 183 |
|
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
if(aihead) |
| 188 |
CURL_FREEADDRINFO(aihead); |
| 189 |
|
| 190 |
|
| 191 |
if(error) { |
| 192 |
Curl_freeaddrinfo(cafirst); |
| 193 |
cafirst = NULL; |
| 194 |
} |
| 195 |
else if(!cafirst) { |
| 196 |
#ifdef EAI_NONAME |
| 197 |
|
| 198 |
error = EAI_NONAME; |
| 199 |
#else |
| 200 |
|
| 201 |
error = EAI_NODATA; |
| 202 |
#endif |
| 203 |
#ifdef USE_WINSOCK |
| 204 |
SET_SOCKERRNO(error); |
| 205 |
#endif |
| 206 |
} |
| 207 |
|
| 208 |
*result = cafirst; |
| 209 |
|
| 210 |
|
| 211 |
return error; |
| 212 |
} |
| 213 |
#endif |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
#if !(defined(HAVE_GETADDRINFO) && defined(HAVE_GETADDRINFO_THREADSAFE)) |
| 257 |
struct Curl_addrinfo * |
| 258 |
Curl_he2ai(const struct hostent *he, int port) |
| 259 |
{ |
| 260 |
struct Curl_addrinfo *ai; |
| 261 |
struct Curl_addrinfo *prevai = NULL; |
| 262 |
struct Curl_addrinfo *firstai = NULL; |
| 263 |
struct sockaddr_in *addr; |
| 264 |
#ifdef USE_IPV6 |
| 265 |
struct sockaddr_in6 *addr6; |
| 266 |
#endif |
| 267 |
CURLcode result = CURLE_OK; |
| 268 |
int i; |
| 269 |
char *curr; |
| 270 |
|
| 271 |
if(!he) |
| 272 |
|
| 273 |
return NULL; |
| 274 |
|
| 275 |
DEBUGASSERT((he->h_name != NULL) && (he->h_addr_list != NULL)); |
| 276 |
|
| 277 |
for(i = 0; (curr = he->h_addr_list[i]) != NULL; i++) { |
| 278 |
size_t ss_size; |
| 279 |
size_t namelen = strlen(he->h_name) + 1; |
| 280 |
#ifdef USE_IPV6 |
| 281 |
if(he->h_addrtype == AF_INET6) |
| 282 |
ss_size = sizeof(struct sockaddr_in6); |
| 283 |
else |
| 284 |
#endif |
| 285 |
ss_size = sizeof(struct sockaddr_in); |
| 286 |
|
| 287 |
|
| 288 |
ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + namelen); |
| 289 |
if(!ai) { |
| 290 |
result = CURLE_OUT_OF_MEMORY; |
| 291 |
break; |
| 292 |
} |
| 293 |
|
| 294 |
ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo)); |
| 295 |
|
| 296 |
ai->ai_canonname = (char *)ai->ai_addr + ss_size; |
| 297 |
memcpy(ai->ai_canonname, he->h_name, namelen); |
| 298 |
|
| 299 |
if(!firstai) |
| 300 |
|
| 301 |
firstai = ai; |
| 302 |
|
| 303 |
if(prevai) |
| 304 |
|
| 305 |
prevai->ai_next = ai; |
| 306 |
|
| 307 |
ai->ai_family = he->h_addrtype; |
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
ai->ai_socktype = SOCK_STREAM; |
| 312 |
|
| 313 |
ai->ai_addrlen = (curl_socklen_t)ss_size; |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
switch(ai->ai_family) { |
| 318 |
case AF_INET: |
| 319 |
addr = (void *)ai->ai_addr; |
| 320 |
|
| 321 |
memcpy(&addr->sin_addr, curr, sizeof(struct in_addr)); |
| 322 |
addr->sin_family = (CURL_SA_FAMILY_T)(he->h_addrtype); |
| 323 |
addr->sin_port = htons((unsigned short)port); |
| 324 |
break; |
| 325 |
|
| 326 |
#ifdef USE_IPV6 |
| 327 |
case AF_INET6: |
| 328 |
addr6 = (void *)ai->ai_addr; |
| 329 |
|
| 330 |
memcpy(&addr6->sin6_addr, curr, sizeof(struct in6_addr)); |
| 331 |
addr6->sin6_family = (CURL_SA_FAMILY_T)(he->h_addrtype); |
| 332 |
addr6->sin6_port = htons((unsigned short)port); |
| 333 |
break; |
| 334 |
#endif |
| 335 |
} |
| 336 |
|
| 337 |
prevai = ai; |
| 338 |
} |
| 339 |
|
| 340 |
if(result) { |
| 341 |
Curl_freeaddrinfo(firstai); |
| 342 |
firstai = NULL; |
| 343 |
} |
| 344 |
|
| 345 |
return firstai; |
| 346 |
} |
| 347 |
#endif |
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
struct Curl_addrinfo * |
| 359 |
Curl_ip2addr(int af, const void *inaddr, const char *hostname, int port) |
| 360 |
{ |
| 361 |
struct Curl_addrinfo *ai; |
| 362 |
size_t addrsize; |
| 363 |
size_t namelen; |
| 364 |
struct sockaddr_in *addr; |
| 365 |
#ifdef USE_IPV6 |
| 366 |
struct sockaddr_in6 *addr6; |
| 367 |
#endif |
| 368 |
|
| 369 |
DEBUGASSERT(inaddr && hostname); |
| 370 |
|
| 371 |
namelen = strlen(hostname) + 1; |
| 372 |
|
| 373 |
if(af == AF_INET) |
| 374 |
addrsize = sizeof(struct sockaddr_in); |
| 375 |
#ifdef USE_IPV6 |
| 376 |
else if(af == AF_INET6) |
| 377 |
addrsize = sizeof(struct sockaddr_in6); |
| 378 |
#endif |
| 379 |
else |
| 380 |
return NULL; |
| 381 |
|
| 382 |
|
| 383 |
ai = calloc(1, sizeof(struct Curl_addrinfo) + addrsize + namelen); |
| 384 |
if(!ai) |
| 385 |
return NULL; |
| 386 |
|
| 387 |
ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo)); |
| 388 |
|
| 389 |
ai->ai_canonname = (char *)ai->ai_addr + addrsize; |
| 390 |
memcpy(ai->ai_canonname, hostname, namelen); |
| 391 |
ai->ai_family = af; |
| 392 |
ai->ai_socktype = SOCK_STREAM; |
| 393 |
ai->ai_addrlen = (curl_socklen_t)addrsize; |
| 394 |
|
| 395 |
|
| 396 |
switch(af) { |
| 397 |
case AF_INET: |
| 398 |
addr = (void *)ai->ai_addr; |
| 399 |
|
| 400 |
memcpy(&addr->sin_addr, inaddr, sizeof(struct in_addr)); |
| 401 |
#ifdef __MINGW32__ |
| 402 |
addr->sin_family = (short)af; |
| 403 |
#else |
| 404 |
addr->sin_family = (CURL_SA_FAMILY_T)af; |
| 405 |
#endif |
| 406 |
addr->sin_port = htons((unsigned short)port); |
| 407 |
break; |
| 408 |
|
| 409 |
#ifdef USE_IPV6 |
| 410 |
case AF_INET6: |
| 411 |
addr6 = (void *)ai->ai_addr; |
| 412 |
|
| 413 |
memcpy(&addr6->sin6_addr, inaddr, sizeof(struct in6_addr)); |
| 414 |
#ifdef __MINGW32__ |
| 415 |
addr6->sin6_family = (short)af; |
| 416 |
#else |
| 417 |
addr6->sin6_family = (CURL_SA_FAMILY_T)af; |
| 418 |
#endif |
| 419 |
addr6->sin6_port = htons((unsigned short)port); |
| 420 |
break; |
| 421 |
#endif |
| 422 |
} |
| 423 |
|
| 424 |
return ai; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
struct Curl_addrinfo *Curl_str2addr(char *address, int port) |
| 432 |
{ |
| 433 |
struct in_addr in; |
| 434 |
if(curlx_inet_pton(AF_INET, address, &in) > 0) |
| 435 |
|
| 436 |
return Curl_ip2addr(AF_INET, &in, address, port); |
| 437 |
#ifdef USE_IPV6 |
| 438 |
{ |
| 439 |
struct in6_addr in6; |
| 440 |
if(curlx_inet_pton(AF_INET6, address, &in6) > 0) |
| 441 |
|
| 442 |
return Curl_ip2addr(AF_INET6, &in6, address, port); |
| 443 |
} |
| 444 |
#endif |
| 445 |
return NULL; |
| 446 |
} |
| 447 |
|
| 448 |
#ifdef USE_UNIX_SOCKETS |
| 449 |
|
| 450 |
|
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
struct Curl_addrinfo *Curl_unix2addr(const char *path, bool *longpath, |
| 455 |
bool abstract) |
| 456 |
{ |
| 457 |
struct Curl_addrinfo *ai; |
| 458 |
struct sockaddr_un *sa_un; |
| 459 |
size_t path_len; |
| 460 |
|
| 461 |
*longpath = FALSE; |
| 462 |
|
| 463 |
ai = calloc(1, sizeof(struct Curl_addrinfo) + sizeof(struct sockaddr_un)); |
| 464 |
if(!ai) |
| 465 |
return NULL; |
| 466 |
ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo)); |
| 467 |
|
| 468 |
sa_un = (void *) ai->ai_addr; |
| 469 |
sa_un->sun_family = AF_UNIX; |
| 470 |
|
| 471 |
|
| 472 |
path_len = strlen(path) + 1; |
| 473 |
if(path_len > sizeof(sa_un->sun_path)) { |
| 474 |
free(ai); |
| 475 |
*longpath = TRUE; |
| 476 |
return NULL; |
| 477 |
} |
| 478 |
|
| 479 |
ai->ai_family = AF_UNIX; |
| 480 |
ai->ai_socktype = SOCK_STREAM; |
| 481 |
ai->ai_addrlen = (curl_socklen_t) |
| 482 |
((offsetof(struct sockaddr_un, sun_path) + path_len) & 0x7FFFFFFF); |
| 483 |
|
| 484 |
|
| 485 |
if(abstract) |
| 486 |
memcpy(sa_un->sun_path + 1, path, path_len - 1); |
| 487 |
else |
| 488 |
memcpy(sa_un->sun_path, path, path_len); |
| 489 |
|
| 490 |
return ai; |
| 491 |
} |
| 492 |
#endif |
| 493 |
|
| 494 |
#if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) && \ |
| 495 |
defined(HAVE_FREEADDRINFO) |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
void |
| 505 |
curl_dbg_freeaddrinfo(struct addrinfo *freethis, |
| 506 |
int line, const char *source) |
| 507 |
{ |
| 508 |
curl_dbg_log("ADDR %s:%d freeaddrinfo(%p)\n", |
| 509 |
source, line, (void *)freethis); |
| 510 |
#ifdef USE_LWIPSOCK |
| 511 |
lwip_freeaddrinfo(freethis); |
| 512 |
#elif defined(USE_FAKE_GETADDRINFO) |
| 513 |
{ |
| 514 |
const char *env = getenv("CURL_DNS_SERVER"); |
| 515 |
if(env) |
| 516 |
r_freeaddrinfo(freethis); |
| 517 |
else |
| 518 |
freeaddrinfo(freethis); |
| 519 |
} |
| 520 |
#else |
| 521 |
freeaddrinfo(freethis); |
| 522 |
#endif |
| 523 |
} |
| 524 |
#endif |
| 525 |
|
| 526 |
|
| 527 |
#if defined(CURLDEBUG) && defined(HAVE_GETADDRINFO) |
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
int |
| 537 |
curl_dbg_getaddrinfo(const char *hostname, |
| 538 |
const char *service, |
| 539 |
const struct addrinfo *hints, |
| 540 |
struct addrinfo **result, |
| 541 |
int line, const char *source) |
| 542 |
{ |
| 543 |
#ifdef USE_LWIPSOCK |
| 544 |
int res = lwip_getaddrinfo(hostname, service, hints, result); |
| 545 |
#elif defined(USE_FAKE_GETADDRINFO) |
| 546 |
int res; |
| 547 |
const char *env = getenv("CURL_DNS_SERVER"); |
| 548 |
if(env) |
| 549 |
res = r_getaddrinfo(hostname, service, hints, result); |
| 550 |
else |
| 551 |
res = getaddrinfo(hostname, service, hints, result); |
| 552 |
#else |
| 553 |
int res = getaddrinfo(hostname, service, hints, result); |
| 554 |
#endif |
| 555 |
if(0 == res) |
| 556 |
|
| 557 |
curl_dbg_log("ADDR %s:%d getaddrinfo() = %p\n", |
| 558 |
source, line, (void *)*result); |
| 559 |
else |
| 560 |
curl_dbg_log("ADDR %s:%d getaddrinfo() failed\n", |
| 561 |
source, line); |
| 562 |
return res; |
| 563 |
} |
| 564 |
#endif |
| 565 |
|
| 566 |
#if defined(HAVE_GETADDRINFO) && defined(USE_RESOLVE_ON_IPS) |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
void Curl_addrinfo_set_port(struct Curl_addrinfo *addrinfo, int port) |
| 572 |
{ |
| 573 |
struct Curl_addrinfo *ca; |
| 574 |
struct sockaddr_in *addr; |
| 575 |
#ifdef USE_IPV6 |
| 576 |
struct sockaddr_in6 *addr6; |
| 577 |
#endif |
| 578 |
for(ca = addrinfo; ca != NULL; ca = ca->ai_next) { |
| 579 |
switch(ca->ai_family) { |
| 580 |
case AF_INET: |
| 581 |
addr = (void *)ca->ai_addr; |
| 582 |
addr->sin_port = htons((unsigned short)port); |
| 583 |
break; |
| 584 |
|
| 585 |
#ifdef USE_IPV6 |
| 586 |
case AF_INET6: |
| 587 |
addr6 = (void *)ca->ai_addr; |
| 588 |
addr6->sin6_port = htons((unsigned short)port); |
| 589 |
break; |
| 590 |
#endif |
| 591 |
} |
| 592 |
} |
| 593 |
} |
| 594 |
#endif |