new
[libcurl.git] / lib / hostip.c
blob644818fea373f2cfb7ff07b1e0515a4e8c3c51ab
1 /*****************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
8 * The contents of this file are subject to the Mozilla Public License
9 * Version 1.0 (the "License"); you may not use this file except in
10 * compliance with the License. You may obtain a copy of the License at
11 * http://www.mozilla.org/MPL/
13 * Software distributed under the License is distributed on an "AS IS"
14 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
15 * License for the specific language governing rights and limitations
16 * under the License.
18 * The Original Code is Curl.
20 * The Initial Developer of the Original Code is Daniel Stenberg.
22 * Portions created by the Initial Developer are Copyright (C) 1998.
23 * All Rights Reserved.
25 * ------------------------------------------------------------
26 * Main author:
27 * - Daniel Stenberg <Daniel.Stenberg@haxx.nu>
29 * http://curl.haxx.nu
31 * $Source: /cvsroot/curl/curl/lib/hostip.c,v $
32 * $Revision: 1.1.1.1 $
33 * $Date: 1999-12-29 14:21:31 $
34 * $Author: bagder $
35 * $State: Exp $
36 * $Locker: $
38 * ------------------------------------------------------------
39 ****************************************************************************/
41 #include <string.h>
43 #include "setup.h"
45 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
46 #include <winsock.h>
47 #else
48 #ifdef HAVE_SYS_TYPES_H
49 #include <sys/types.h>
50 #endif
51 #ifdef HAVE_SYS_SOCKET_H
52 #include <sys/socket.h>
53 #endif
54 #include <netinet/in.h>
55 #include <netdb.h>
56 #ifdef HAVE_ARPA_INET_H
57 #include <arpa/inet.h>
58 #endif
59 #endif
61 #include "urldata.h"
62 #include "sendf.h"
64 /* --- resolve name or IP-number --- */
66 char *MakeIP(unsigned long num)
68 #ifdef HAVE_INET_NTOA
69 struct in_addr in;
71 in.s_addr = htonl(num);
72 return (inet_ntoa(in));
73 #else
74 static char addr[128];
75 unsigned char *paddr;
77 num = htonl(num); /* htonl() added to avoid endian probs */
78 paddr = (unsigned char *)&num;
79 sprintf(addr, "%u.%u.%u.%u", paddr[0], paddr[1], paddr[2], paddr[3]);
80 return (addr);
81 #endif
84 /* Stolen from Dancer source code, written by
85 Bjorn Reese <breese@imada.ou.dk> */
86 #ifndef INADDR_NONE
87 #define INADDR_NONE (unsigned long) ~0
88 #endif
89 struct hostent *GetHost(struct UrlData *data, char *hostname)
91 struct hostent *h = NULL;
92 unsigned long in;
93 static struct hostent he;
94 static char name[MAXHOSTNAMELEN];
95 static char *addrlist[2];
96 static struct in_addr addrentry;
98 if ( (in=inet_addr(hostname)) != INADDR_NONE ) {
99 addrentry.s_addr = in;
100 addrlist[0] = (char *)&addrentry;
101 addrlist[1] = NULL;
102 he.h_name = strncpy(name, MakeIP(ntohl(in)), MAXHOSTNAMELEN);
103 he.h_addrtype = AF_INET;
104 he.h_length = sizeof(struct in_addr);
105 he.h_addr_list = addrlist;
106 h = &he;
107 } else if ( (h=gethostbyname(hostname)) == NULL ) {
108 infof(data, "gethostbyname(2) failed for %s\n", hostname);
110 return (h);