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
27 #ifndef HAVE_GETADDRINFO
29 /* critical section to protect non-reentrant gethostbyname() */
30 static CRITICAL_SECTION cs_gethostbyname
;
31 static CRITICAL_SECTION_DEBUG critsect_debug
=
33 0, 0, &cs_gethostbyname
,
34 { &critsect_debug
.ProcessLocksList
, &critsect_debug
.ProcessLocksList
},
35 0, 0, { (DWORD_PTR
)(__FILE__
": cs_gethostbyname") }
37 static CRITICAL_SECTION cs_gethostbyname
= { &critsect_debug
, -1, 0, 0, 0, 0 };
41 #define TIME_STRING_LEN 30
43 time_t ConvertTimeString(LPCWSTR asctime
)
45 WCHAR tmpChar
[TIME_STRING_LEN
];
48 int timelen
= strlenW(asctime
);
53 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
54 memset( tmpChar
, 0, sizeof(tmpChar
) );
55 lstrcpynW(tmpChar
, asctime
, TIME_STRING_LEN
);
57 /* Assert that the string is the expected length */
58 if (strlenW(asctime
) >= TIME_STRING_LEN
) FIXME("\n");
60 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
61 * We assume the time is in this format
62 * and divide it into easy to swallow chunks
72 memset( &t
, 0, sizeof(t
) );
73 t
.tm_year
= atoiW(tmpChar
+12) - 1900;
74 t
.tm_mday
= atoiW(tmpChar
+5);
75 t
.tm_hour
= atoiW(tmpChar
+17);
76 t
.tm_min
= atoiW(tmpChar
+20);
77 t
.tm_sec
= atoiW(tmpChar
+23);
80 tmpChar2
= tmpChar
+ 8;
127 BOOL
GetAddress(LPCWSTR lpszServerName
, INTERNET_PORT nServerPort
,
128 struct sockaddr
*psa
, socklen_t
*sa_len
)
133 #ifdef HAVE_GETADDRINFO
134 struct addrinfo
*res
, hints
;
138 struct sockaddr_in
*sin
= (struct sockaddr_in
*)psa
;
141 TRACE("%s\n", debugstr_w(lpszServerName
));
143 /* Validate server name first
144 * Check if there is something like
145 * pinger.macromedia.com:80
146 * if yes, eliminate the :80....
148 found
= strchrW(lpszServerName
, ':');
150 len
= found
- lpszServerName
;
152 len
= strlenW(lpszServerName
);
154 sz
= WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, NULL
, 0, NULL
, NULL
);
155 if (!(name
= heap_alloc(sz
+ 1))) return FALSE
;
156 WideCharToMultiByte( CP_UNIXCP
, 0, lpszServerName
, len
, name
, sz
, NULL
, NULL
);
159 #ifdef HAVE_GETADDRINFO
160 memset( &hints
, 0, sizeof(struct addrinfo
) );
161 /* Prefer IPv4 to IPv6 addresses, since some servers do not listen on
162 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
164 hints
.ai_family
= AF_INET
;
166 ret
= getaddrinfo( name
, NULL
, &hints
, &res
);
169 TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(lpszServerName
), gai_strerror(ret
));
170 hints
.ai_family
= AF_INET6
;
171 ret
= getaddrinfo( name
, NULL
, &hints
, &res
);
176 TRACE("failed to get address of %s (%s)\n", debugstr_w(lpszServerName
), gai_strerror(ret
));
179 if (*sa_len
< res
->ai_addrlen
)
181 WARN("address too small\n");
185 *sa_len
= res
->ai_addrlen
;
186 memcpy( psa
, res
->ai_addr
, res
->ai_addrlen
);
188 switch (res
->ai_family
)
191 ((struct sockaddr_in
*)psa
)->sin_port
= htons(nServerPort
);
194 ((struct sockaddr_in6
*)psa
)->sin6_port
= htons(nServerPort
);
200 EnterCriticalSection( &cs_gethostbyname
);
201 phe
= gethostbyname(name
);
206 TRACE("failed to get address of %s (%d)\n", debugstr_w(lpszServerName
), h_errno
);
207 LeaveCriticalSection( &cs_gethostbyname
);
210 if (*sa_len
< sizeof(struct sockaddr_in
))
212 WARN("address too small\n");
213 LeaveCriticalSection( &cs_gethostbyname
);
216 *sa_len
= sizeof(struct sockaddr_in
);
217 memset(sin
,0,sizeof(struct sockaddr_in
));
218 memcpy((char *)&sin
->sin_addr
, phe
->h_addr
, phe
->h_length
);
219 sin
->sin_family
= phe
->h_addrtype
;
220 sin
->sin_port
= htons(nServerPort
);
222 LeaveCriticalSection( &cs_gethostbyname
);
228 * Helper function for sending async Callbacks
231 static const char *get_callback_name(DWORD dwInternetStatus
) {
232 static const wininet_flag_info internet_status
[] = {
233 #define FE(x) { x, #x }
234 FE(INTERNET_STATUS_RESOLVING_NAME
),
235 FE(INTERNET_STATUS_NAME_RESOLVED
),
236 FE(INTERNET_STATUS_CONNECTING_TO_SERVER
),
237 FE(INTERNET_STATUS_CONNECTED_TO_SERVER
),
238 FE(INTERNET_STATUS_SENDING_REQUEST
),
239 FE(INTERNET_STATUS_REQUEST_SENT
),
240 FE(INTERNET_STATUS_RECEIVING_RESPONSE
),
241 FE(INTERNET_STATUS_RESPONSE_RECEIVED
),
242 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED
),
243 FE(INTERNET_STATUS_PREFETCH
),
244 FE(INTERNET_STATUS_CLOSING_CONNECTION
),
245 FE(INTERNET_STATUS_CONNECTION_CLOSED
),
246 FE(INTERNET_STATUS_HANDLE_CREATED
),
247 FE(INTERNET_STATUS_HANDLE_CLOSING
),
248 FE(INTERNET_STATUS_REQUEST_COMPLETE
),
249 FE(INTERNET_STATUS_REDIRECT
),
250 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE
),
251 FE(INTERNET_STATUS_USER_INPUT_REQUIRED
),
252 FE(INTERNET_STATUS_STATE_CHANGE
),
253 FE(INTERNET_STATUS_COOKIE_SENT
),
254 FE(INTERNET_STATUS_COOKIE_RECEIVED
),
255 FE(INTERNET_STATUS_PRIVACY_IMPACTED
),
256 FE(INTERNET_STATUS_P3P_HEADER
),
257 FE(INTERNET_STATUS_P3P_POLICYREF
),
258 FE(INTERNET_STATUS_COOKIE_HISTORY
)
263 for (i
= 0; i
< (sizeof(internet_status
) / sizeof(internet_status
[0])); i
++) {
264 if (internet_status
[i
].val
== dwInternetStatus
) return internet_status
[i
].name
;
269 static const char *debugstr_status_info(DWORD status
, void *info
)
272 case INTERNET_STATUS_REQUEST_COMPLETE
: {
273 INTERNET_ASYNC_RESULT
*iar
= info
;
274 return wine_dbg_sprintf("{%s, %d}", wine_dbgstr_longlong(iar
->dwResult
), iar
->dwError
);
277 return wine_dbg_sprintf("%p", info
);
281 void INTERNET_SendCallback(object_header_t
*hdr
, DWORD_PTR context
, DWORD status
, void *info
, DWORD info_len
)
283 void *new_info
= info
;
285 if( !hdr
->lpfnStatusCB
)
288 /* the IE5 version of wininet does not
289 send callbacks if dwContext is zero */
294 case INTERNET_STATUS_NAME_RESOLVED
:
295 case INTERNET_STATUS_CONNECTING_TO_SERVER
:
296 case INTERNET_STATUS_CONNECTED_TO_SERVER
:
297 new_info
= heap_alloc(info_len
);
299 memcpy(new_info
, info
, info_len
);
301 case INTERNET_STATUS_RESOLVING_NAME
:
302 case INTERNET_STATUS_REDIRECT
:
303 if(hdr
->dwInternalFlags
& INET_CALLBACKW
) {
304 new_info
= heap_strdupW(info
);
307 new_info
= heap_strdupWtoA(info
);
308 info_len
= strlen(new_info
)+1;
313 TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %s, %d)\n",
314 hdr
->lpfnStatusCB
, hdr
->hInternet
, hdr
, context
, status
, get_callback_name(status
),
315 debugstr_status_info(status
, new_info
), info_len
);
317 hdr
->lpfnStatusCB(hdr
->hInternet
, context
, status
, new_info
, info_len
);
319 TRACE(" end callback().\n");
330 DWORD status_info_len
;
331 } send_callback_task_t
;
333 static void SendAsyncCallbackProc(task_header_t
*hdr
)
335 send_callback_task_t
*task
= (send_callback_task_t
*)hdr
;
337 TRACE("%p\n", task
->hdr
.hdr
);
339 INTERNET_SendCallback(task
->hdr
.hdr
, task
->context
, task
->status
, task
->status_info
, task
->status_info_len
);
341 /* And frees the copy of the status info */
342 heap_free(task
->status_info
);
345 void SendAsyncCallback(object_header_t
*hdr
, DWORD_PTR dwContext
,
346 DWORD dwInternetStatus
, LPVOID lpvStatusInfo
,
347 DWORD dwStatusInfoLength
)
349 TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
350 hdr
, dwContext
, dwInternetStatus
, get_callback_name(dwInternetStatus
),
351 lpvStatusInfo
, dwStatusInfoLength
,
352 hdr
->dwFlags
& INTERNET_FLAG_ASYNC
? "" : "non ",
355 if (!(hdr
->lpfnStatusCB
))
358 if (hdr
->dwFlags
& INTERNET_FLAG_ASYNC
)
360 send_callback_task_t
*task
;
361 void *lpvStatusInfo_copy
= lpvStatusInfo
;
365 lpvStatusInfo_copy
= heap_alloc(dwStatusInfoLength
);
366 memcpy(lpvStatusInfo_copy
, lpvStatusInfo
, dwStatusInfoLength
);
369 task
= alloc_async_task(hdr
, SendAsyncCallbackProc
, sizeof(*task
));
370 task
->context
= dwContext
;
371 task
->status
= dwInternetStatus
;
372 task
->status_info
= lpvStatusInfo_copy
;
373 task
->status_info_len
= dwStatusInfoLength
;
375 INTERNET_AsyncCall(&task
->hdr
);
378 INTERNET_SendCallback(hdr
, dwContext
, dwInternetStatus
,
379 lpvStatusInfo
, dwStatusInfoLength
);