~ubuntu-branches/ubuntu/vivid/curl/vivid

« back to all changes in this revision

Viewing changes to lib/hostip4.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-04-02 23:35:45 UTC
  • mto: (1.2.1 upstream) (3.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20090402233545-geixkwhe3izccjt7
Tags: upstream-7.19.4
ImportĀ upstreamĀ versionĀ 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
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.
20
20
 *
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
 ***************************************************************************/
23
23
 
24
24
#include "setup.h"
53
53
#include <stdlib.h>
54
54
#endif
55
55
 
56
 
#ifdef HAVE_SETJMP_H
57
 
#include <setjmp.h>
58
 
#endif
59
 
 
60
56
#ifdef HAVE_PROCESS_H
61
57
#include <process.h>
62
58
#endif
73
69
#define _MPRINTF_REPLACE /* use our functions only */
74
70
#include <curl/mprintf.h>
75
71
 
76
 
#if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
77
 
#include "inet_ntoa_r.h"
78
 
#endif
79
 
 
80
72
#include "memory.h"
81
73
/* The last #include file should be: */
82
74
#include "memdebug.h"
126
118
#endif
127
119
  Curl_addrinfo *ai = NULL;
128
120
  struct hostent *h = NULL;
129
 
  in_addr_t in;
 
121
  struct in_addr in;
130
122
  struct hostent *buf = NULL;
131
123
 
132
124
#ifdef CURL_DISABLE_VERBOSE_STRINGS
133
125
  (void)conn;
134
126
#endif
135
127
 
136
 
  (void)port; /* unused in IPv4 code */
137
 
 
138
128
  *waitp = 0; /* don't wait, we act synchronously */
139
129
 
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);
143
133
 
144
134
#if defined(HAVE_GETHOSTBYNAME_R)
145
135
  /*
150
140
  else {
151
141
    int h_errnop;
152
142
 
153
 
    buf = (struct hostent *)calloc(CURL_HOSTENT_SIZE, 1);
 
143
    buf = calloc(CURL_HOSTENT_SIZE, 1);
154
144
    if(!buf)
155
145
      return NULL; /* major failure */
156
146
    /*
307
297
#endif /* CURLRES_SYNCH */
308
298
#endif /* CURLRES_IPV4 */
309
299
 
310
 
/*
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.
314
 
 *
315
 
 *   Curl_addrinfo defined in "lib/hostip.h"
316
 
 *
317
 
 *     struct Curl_addrinfo {
318
 
 *       int                   ai_flags;
319
 
 *       int                   ai_family;
320
 
 *       int                   ai_socktype;
321
 
 *       int                   ai_protocol;
322
 
 *       socklen_t             ai_addrlen;   * Follow rfc3493 struct addrinfo *
323
 
 *       char                 *ai_canonname;
324
 
 *       struct sockaddr      *ai_addr;
325
 
 *       struct Curl_addrinfo *ai_next;
326
 
 *     };
327
 
 *
328
 
 *   hostent defined in <netdb.h>
329
 
 *
330
 
 *     struct hostent {
331
 
 *       char    *h_name;
332
 
 *       char    **h_aliases;
333
 
 *       int     h_addrtype;
334
 
 *       int     h_length;
335
 
 *       char    **h_addr_list;
336
 
 *     };
337
 
 *
338
 
 *   for backward compatibility:
339
 
 *
340
 
 *     #define h_addr  h_addr_list[0]
341
 
 */
342
 
 
343
 
Curl_addrinfo *Curl_he2ai(const struct hostent *he, int port)
344
 
{
345
 
  Curl_addrinfo *ai;
346
 
  Curl_addrinfo *prevai = NULL;
347
 
  Curl_addrinfo *firstai = NULL;
348
 
  struct sockaddr_in *addr;
349
 
  int i;
350
 
  struct in_addr *curr;
351
 
 
352
 
  if(!he)
353
 
    /* no input == no output! */
354
 
    return NULL;
355
 
 
356
 
  for(i=0; (curr = (struct in_addr *)he->h_addr_list[i]) != NULL; i++) {
357
 
 
358
 
    ai = calloc(1, sizeof(Curl_addrinfo) + sizeof(struct sockaddr_in));
359
 
 
360
 
    if(!ai)
361
 
      break;
362
 
 
363
 
    if(!firstai)
364
 
      /* store the pointer we want to return from this function */
365
 
      firstai = ai;
366
 
 
367
 
    if(prevai)
368
 
      /* make the previous entry point to this */
369
 
      prevai->ai_next = ai;
370
 
 
371
 
    ai->ai_family = AF_INET;              /* we only support this */
372
 
 
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;
376
 
 
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));
381
 
 
382
 
    /* FIXME: need to free this eventually */
383
 
    ai->ai_canonname = strdup(he->h_name);
384
 
 
385
 
    /* leave the rest of the struct filled with zero */
386
 
 
387
 
    addr = (struct sockaddr_in *)ai->ai_addr; /* storage area for this info */
388
 
 
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);
392
 
 
393
 
    prevai = ai;
394
 
  }
395
 
  return firstai;
396
 
}
397