5
5
* | (__| |_| | _ <| |___
6
6
* \___|\___/|_| \_\_____|
8
* Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
8
* Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
10
10
* This software is licensed as described in the file COPYING, which
11
11
* you should have received as part of this distribution. The terms
18
18
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
* KIND, either express or implied.
21
* $Id: hostip4.c,v 1.42 2007-11-07 09:21:35 bagder Exp $
21
* $Id: hostip4.c,v 1.47 2008-11-06 17:19:57 yangtse Exp $
22
22
***************************************************************************/
127
119
Curl_addrinfo *ai = NULL;
128
120
struct hostent *h = NULL;
130
122
struct hostent *buf = NULL;
132
124
#ifdef CURL_DISABLE_VERBOSE_STRINGS
136
(void)port; /* unused in IPv4 code */
138
128
*waitp = 0; /* don't wait, we act synchronously */
140
if(1 == Curl_inet_pton(AF_INET, hostname, &in))
130
if(Curl_inet_pton(AF_INET, hostname, &in) > 0)
141
131
/* This is a dotted IP address 123.123.123.123-style */
142
return Curl_ip2addr(in, hostname, port);
132
return Curl_ip2addr(AF_INET, &in, hostname, port);
144
134
#if defined(HAVE_GETHOSTBYNAME_R)
307
297
#endif /* CURLRES_SYNCH */
308
298
#endif /* CURLRES_IPV4 */
311
* Curl_he2ai() translates from a hostent struct to a Curl_addrinfo struct.
312
* The Curl_addrinfo is meant to work like the addrinfo struct does for IPv6
313
* stacks, but for all hosts and environments.
315
* Curl_addrinfo defined in "lib/hostip.h"
317
* struct Curl_addrinfo {
322
* socklen_t ai_addrlen; * Follow rfc3493 struct addrinfo *
323
* char *ai_canonname;
324
* struct sockaddr *ai_addr;
325
* struct Curl_addrinfo *ai_next;
328
* hostent defined in <netdb.h>
335
* char **h_addr_list;
338
* for backward compatibility:
340
* #define h_addr h_addr_list[0]
343
Curl_addrinfo *Curl_he2ai(const struct hostent *he, int port)
346
Curl_addrinfo *prevai = NULL;
347
Curl_addrinfo *firstai = NULL;
348
struct sockaddr_in *addr;
350
struct in_addr *curr;
353
/* no input == no output! */
356
for(i=0; (curr = (struct in_addr *)he->h_addr_list[i]) != NULL; i++) {
358
ai = calloc(1, sizeof(Curl_addrinfo) + sizeof(struct sockaddr_in));
364
/* store the pointer we want to return from this function */
368
/* make the previous entry point to this */
369
prevai->ai_next = ai;
371
ai->ai_family = AF_INET; /* we only support this */
373
/* we return all names as STREAM, so when using this address for TFTP
374
the type must be ignored and conn->socktype be used instead! */
375
ai->ai_socktype = SOCK_STREAM;
377
ai->ai_addrlen = sizeof(struct sockaddr_in);
378
/* make the ai_addr point to the address immediately following this struct
379
and use that area to store the address */
380
ai->ai_addr = (struct sockaddr *) ((char*)ai + sizeof(Curl_addrinfo));
382
/* FIXME: need to free this eventually */
383
ai->ai_canonname = strdup(he->h_name);
385
/* leave the rest of the struct filled with zero */
387
addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
389
memcpy((char *)&(addr->sin_addr), curr, sizeof(struct in_addr));
390
addr->sin_family = (unsigned short)(he->h_addrtype);
391
addr->sin_port = htons((unsigned short)port);