2 * Wininet - Utility functions
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "wine/port.h"
28 #if defined(__MINGW32__) || defined (_MSC_VER)
42 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(wininet
);
47 #ifndef HAVE_GETADDRINFO
49 /* critical section to protect non-reentrant gethostbyname() */
50 static CRITICAL_SECTION cs_gethostbyname
;
51 static CRITICAL_SECTION_DEBUG critsect_debug
=
53 0, 0, &cs_gethostbyname
,
54 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
55 0, 0, { (DWORD_PTR
)(__FILE__
": cs_gethostbyname") }
57 static CRITICAL_SECTION cs_gethostbyname
= { &critsect_debug
, -1, 0, 0, 0, 0 };
61 #define TIME_STRING_LEN 30
63 time_t ConvertTimeString(LPCWSTR asctime
)
65 WCHAR tmpChar
[TIME_STRING_LEN
];
68 int timelen
= strlenW(asctime
);
73 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
74 memset( tmpChar
, 0, sizeof(tmpChar
) );
75 lstrcpynW(tmpChar
, asctime
, TIME_STRING_LEN
);
77 /* Assert that the string is the expected length */
78 if (strlenW(asctime
) >= TIME_STRING_LEN
) FIXME("\n");
80 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
81 * We assume the time is in this format
82 * and divide it into easy to swallow chunks
92 memset( &t
, 0, sizeof(t
) );
93 t
.tm_year
= atoiW(tmpChar
+12) - 1900;
94 t
.tm_mday
= atoiW(tmpChar
+5);
95 t
.tm_hour
= atoiW(tmpChar
+17);
96 t
.tm_min
= atoiW(tmpChar
+20);
97 t
.tm_sec
= atoiW(tmpChar
+23);
100 tmpChar2
= tmpChar
+ 8;
147 BOOL
GetAddress(LPCWSTR lpszServerName
, INTERNET_PORT nServerPort
,
148 struct sockaddr
*psa
, socklen_t
*sa_len
)
153 #ifdef HAVE_GETADDRINFO
154 struct addrinfo
*res
, hints
;
158 struct sockaddr_in
*sin
= (struct sockaddr_in
*)psa
;
161 TRACE("%s\n", debugstr_w(lpszServerName
));
163 /* Validate server name first
164 * Check if there is sth. like
165 * pinger.macromedia.com:80
166 * if yes, eliminate the :80....
168 found
= strchrW(lpszServerName
, ':');
170 len
= found
- lpszServerName
;
172 len
= strlenW(lpszServerName
);
174 sz
= WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, NULL
, 0, NULL
, NULL
);
175 if (!(name
= HeapAlloc( GetProcessHeap(), 0, sz
+ 1 ))) return FALSE
;
176 WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, name
, sz
, NULL
, NULL
);
179 #ifdef HAVE_GETADDRINFO
180 memset( &hints
, 0, sizeof(struct addrinfo
) );
181 /* Prefer IPv4 to IPv6 addresses, since some servers do not listen on
182 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
184 hints
.ai_family
= AF_INET
;
186 ret
= getaddrinfo( name
, NULL
, &hints
, &res
);
187 HeapFree( GetProcessHeap(), 0, name
);
190 TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(lpszServerName
), gai_strerror(ret
));
191 hints
.ai_family
= AF_INET6
;
192 ret
= getaddrinfo( name
, NULL
, &hints
, &res
);
195 TRACE("failed to get address of %s (%s)\n", debugstr_w(lpszServerName
), gai_strerror(ret
));
199 if (*sa_len
< res
->ai_addrlen
)
201 WARN("address too small\n");
205 *sa_len
= res
->ai_addrlen
;
206 memcpy( psa
, res
->ai_addr
, res
->ai_addrlen
);
208 switch (res
->ai_family
)
211 ((struct sockaddr_in
*)psa
)->sin_port
= htons(nServerPort
);
214 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(nServerPort
);
220 EnterCriticalSection( &cs_gethostbyname
);
221 phe
= gethostbyname(name
);
222 HeapFree( GetProcessHeap(), 0, name
);
226 TRACE("failed to get address of %s (%d)\n", debugstr_w(lpszServerName
), h_errno
);
227 LeaveCriticalSection( &cs_gethostbyname
);
230 if (*sa_len
< sizeof(struct sockaddr_in
))
232 WARN("address too small\n");
233 LeaveCriticalSection( &cs_gethostbyname
);
236 *sa_len
= sizeof(struct sockaddr_in
);
237 memset(sin
,0,sizeof(struct sockaddr_in
));
238 memcpy((char *)&sin
->sin_addr
, phe
->h_addr
, phe
->h_length
);
239 sin
->sin_family
= phe
->h_addrtype
;
240 sin
->sin_port
= htons(nServerPort
);
242 LeaveCriticalSection( &cs_gethostbyname
);
248 * Helper function for sending async Callbacks
251 static const char *get_callback_name(DWORD dwInternetStatus
) {
252 static const wininet_flag_info internet_status
[] = {
253 #define FE(x) { x, #x }
254 FE(INTERNET_STATUS_RESOLVING_NAME
),
255 FE(INTERNET_STATUS_NAME_RESOLVED
),
256 FE(INTERNET_STATUS_CONNECTING_TO_SERVER
),
257 FE(INTERNET_STATUS_CONNECTED_TO_SERVER
),
258 FE(INTERNET_STATUS_SENDING_REQUEST
),
259 FE(INTERNET_STATUS_REQUEST_SENT
),
260 FE(INTERNET_STATUS_RECEIVING_RESPONSE
),
261 FE(INTERNET_STATUS_RESPONSE_RECEIVED
),
262 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED
),
263 FE(INTERNET_STATUS_PREFETCH
),
264 FE(INTERNET_STATUS_CLOSING_CONNECTION
),
265 FE(INTERNET_STATUS_CONNECTION_CLOSED
),
266 FE(INTERNET_STATUS_HANDLE_CREATED
),
267 FE(INTERNET_STATUS_HANDLE_CLOSING
),
268 FE(INTERNET_STATUS_REQUEST_COMPLETE
),
269 FE(INTERNET_STATUS_REDIRECT
),
270 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE
),
271 FE(INTERNET_STATUS_USER_INPUT_REQUIRED
),
272 FE(INTERNET_STATUS_STATE_CHANGE
),
273 FE(INTERNET_STATUS_COOKIE_SENT
),
274 FE(INTERNET_STATUS_COOKIE_RECEIVED
),
275 FE(INTERNET_STATUS_PRIVACY_IMPACTED
),
276 FE(INTERNET_STATUS_P3P_HEADER
),
277 FE(INTERNET_STATUS_P3P_POLICYREF
),
278 FE(INTERNET_STATUS_COOKIE_HISTORY
)
283 for (i
= 0; i
< (sizeof(internet_status
) / sizeof(internet_status
[0])); i
++) {
284 if (internet_status
[i
].val
== dwInternetStatus
) return internet_status
[i
].name
;
289 VOID
INTERNET_SendCallback(object_header_t
*hdr
, DWORD_PTR dwContext
,
290 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
291 DWORD dwStatusInfoLength
)
293 LPVOID lpvNewInfo
= NULL
;
295 if( !hdr
->lpfnStatusCB
)
298 /* the IE5 version of wininet does not
299 send callbacks if dwContext is zero */
303 lpvNewInfo
= lpvStatusInfo
;
304 if(hdr
->dwInternalFlags
& INET_CALLBACKW
) {
305 switch(dwInternetStatus
) {
306 case INTERNET_STATUS_NAME_RESOLVED
:
307 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
308 case INTERNET_STATUS_CONNECTED_TO_SERVER
:
309 lpvNewInfo
= heap_strdupAtoW(lpvStatusInfo
);
311 case INTERNET_STATUS_RESOLVING_NAME
:
312 case INTERNET_STATUS_REDIRECT
:
313 lpvNewInfo
= heap_strdupW(lpvStatusInfo
);
317 switch(dwInternetStatus
)
319 case INTERNET_STATUS_NAME_RESOLVED
:
320 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
321 case INTERNET_STATUS_CONNECTED_TO_SERVER
:
322 lpvNewInfo
= HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo
) + 1);
323 if (lpvNewInfo
) strcpy(lpvNewInfo
, lpvStatusInfo
);
325 case INTERNET_STATUS_RESOLVING_NAME
:
326 case INTERNET_STATUS_REDIRECT
:
327 lpvNewInfo
= heap_strdupWtoA(lpvStatusInfo
);
332 TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %p, %d)\n",
333 hdr
->lpfnStatusCB
, hdr
->hInternet
, hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
334 lpvNewInfo
, dwStatusInfoLength
);
336 hdr
->lpfnStatusCB(hdr
->hInternet
, dwContext
, dwInternetStatus
,
337 lpvNewInfo
, dwStatusInfoLength
);
339 TRACE(" end callback().\n");
341 if(lpvNewInfo
!= lpvStatusInfo
)
342 HeapFree(GetProcessHeap(), 0, lpvNewInfo
);
345 static void SendAsyncCallbackProc(WORKREQUEST
*workRequest
)
347 struct WORKREQ_SENDCALLBACK
const *req
= &workRequest
->u
.SendCallback
;
349 TRACE("%p\n", workRequest
->hdr
);
351 INTERNET_SendCallback(workRequest
->hdr
,
352 req
->dwContext
, req
->dwInternetStatus
, req
->lpvStatusInfo
,
353 req
->dwStatusInfoLength
);
355 /* And frees the copy of the status info */
356 HeapFree(GetProcessHeap(), 0, req
->lpvStatusInfo
);
359 void SendAsyncCallback(object_header_t
*hdr
, DWORD_PTR dwContext
,
360 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
361 DWORD dwStatusInfoLength
)
363 TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
364 hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
365 lpvStatusInfo
, dwStatusInfoLength
,
366 hdr
->dwFlags
& INTERNET_FLAG_ASYNC
? "" : "non ",
369 if (!(hdr
->lpfnStatusCB
))
372 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
374 WORKREQUEST workRequest
;
375 struct WORKREQ_SENDCALLBACK
*req
;
376 void *lpvStatusInfo_copy
= lpvStatusInfo
;
380 lpvStatusInfo_copy
= HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength
);
381 memcpy(lpvStatusInfo_copy
, lpvStatusInfo
, dwStatusInfoLength
);
384 workRequest
.asyncproc
= SendAsyncCallbackProc
;
385 workRequest
.hdr
= WININET_AddRef( hdr
);
386 req
= &workRequest
.u
.SendCallback
;
387 req
->dwContext
= dwContext
;
388 req
->dwInternetStatus
= dwInternetStatus
;
389 req
->lpvStatusInfo
= lpvStatusInfo_copy
;
390 req
->dwStatusInfoLength
= dwStatusInfoLength
;
392 INTERNET_AsyncCall(&workRequest
);
395 INTERNET_SendCallback(hdr
, dwContext
, dwInternetStatus
,
396 lpvStatusInfo
, dwStatusInfoLength
);