Updated the copyright year since changes have been this year.
[platform/upstream/curl.git] / lib / hostasyn.c
1 /***************************************************************************
2  *                                  _   _ ____  _
3  *  Project                     ___| | | |  _ \| |
4  *                             / __| | | | |_) | |
5  *                            | (__| |_| |  _ <| |___
6  *                             \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 1998 - 2005, 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 http://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  * $Id$
22  ***************************************************************************/
23
24 #include "setup.h"
25
26 #include <string.h>
27 #include <errno.h>
28
29 #define _REENTRANT
30
31 #if defined(WIN32) && !defined(__GNUC__) || defined(__MINGW32__)
32 #include <malloc.h>
33 #else
34 #ifdef HAVE_SYS_TYPES_H
35 #include <sys/types.h>
36 #endif
37 #ifdef HAVE_SYS_SOCKET_H
38 #include <sys/socket.h>
39 #endif
40 #ifdef HAVE_NETINET_IN_H
41 #include <netinet/in.h>
42 #endif
43 #ifdef HAVE_NETDB_H
44 #include <netdb.h>
45 #endif
46 #ifdef HAVE_ARPA_INET_H
47 #include <arpa/inet.h>
48 #endif
49 #ifdef HAVE_STDLIB_H
50 #include <stdlib.h>     /* required for free() prototypes */
51 #endif
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>     /* for the close() proto */
54 #endif
55 #ifdef  VMS
56 #include <in.h>
57 #include <inet.h>
58 #include <stdlib.h>
59 #endif
60 #endif
61
62 #ifdef HAVE_SETJMP_H
63 #include <setjmp.h>
64 #endif
65
66 #ifdef WIN32
67 #include <process.h>
68 #endif
69
70 #include "urldata.h"
71 #include "sendf.h"
72 #include "hostip.h"
73 #include "hash.h"
74 #include "share.h"
75 #include "strerror.h"
76 #include "url.h"
77
78 #define _MPRINTF_REPLACE /* use our functions only */
79 #include <curl/mprintf.h>
80
81 #if defined(HAVE_INET_NTOA_R) && !defined(HAVE_INET_NTOA_R_DECL)
82 #include "inet_ntoa_r.h"
83 #endif
84
85 #include "memory.h"
86 /* The last #include file should be: */
87 #include "memdebug.h"
88
89 /***********************************************************************
90  * Only for builds using asynchronous name resolves
91  **********************************************************************/
92 #ifdef CURLRES_ASYNCH
93 /*
94  * addrinfo_callback() gets called by ares, gethostbyname_thread() or
95  * getaddrinfo_thread() when we got the name resolved (or not!).
96  *
97  * If the status argument is CURL_ASYNC_SUCCESS, we might need to copy the
98  * address field since it might be freed when this function returns. This
99  * operation stores the resolved data in the DNS cache.
100  *
101  * NOTE: for IPv6 operations, Curl_addrinfo_copy() returns the same
102  * pointer it is given as argument!
103  *
104  * The storage operation locks and unlocks the DNS cache.
105  */
106 static CURLcode addrinfo_callback(void *arg, /* "struct connectdata *" */
107                                   int status,
108                                   void *addr)
109 {
110   struct connectdata *conn = (struct connectdata *)arg;
111   struct Curl_dns_entry *dns = NULL;
112   CURLcode rc = CURLE_OK;
113
114   conn->async.status = status;
115
116   if(CURL_ASYNC_SUCCESS == status) {
117
118     /*
119      * IPv4: Curl_addrinfo_copy() copies the address and returns an allocated
120      * version.
121      *
122      * IPv6: Curl_addrinfo_copy() returns the input pointer!
123      */
124     Curl_addrinfo *ai = Curl_addrinfo_copy(addr, conn->async.port);
125     if(ai) {
126       struct SessionHandle *data = conn->data;
127
128       if(data->share)
129         Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
130
131       dns = Curl_cache_addr(data, ai,
132                             conn->async.hostname,
133                             conn->async.port);
134       if(!dns) {
135         /* failed to store, cleanup and return error */
136         Curl_freeaddrinfo(ai);
137         rc = CURLE_OUT_OF_MEMORY;
138       }
139
140       if(data->share)
141         Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
142     }
143     else
144       rc = CURLE_OUT_OF_MEMORY;
145   }
146
147   conn->async.dns = dns;
148
149  /* Set async.done TRUE last in this function since it may be used multi-
150     threaded and once this is TRUE the other thread may read fields from the
151     async struct */
152   conn->async.done = TRUE;
153
154   /* ipv4: The input hostent struct will be freed by ares when we return from
155      this function */
156   return rc;
157 }
158
159 CURLcode Curl_addrinfo4_callback(void *arg, /* "struct connectdata *" */
160                                  int status,
161                                  struct hostent *hostent)
162 {
163   return addrinfo_callback(arg, status, hostent);
164 }
165
166 #ifdef CURLRES_IPV6
167 CURLcode Curl_addrinfo6_callback(void *arg, /* "struct connectdata *" */
168                                  int status,
169                                  struct addrinfo *ai)
170 {
171   return addrinfo_callback(arg, status, ai);
172 }
173 #endif
174
175 #endif /* CURLRES_ASYNC */