advapi32: Implement LsaSetSecret stub.
[wine/wine-kai.git] / dlls / wininet / utility.c
blobff914f419875248c521912c6257e973939d44db9
1 /*
2 * Wininet - Utility functions
4 * Copyright 1999 Corel Corporation
5 * Copyright 2002 CodeWeavers Inc.
7 * Ulrich Czekalla
8 * Aric Stewart
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
25 #include "config.h"
26 #include "wine/port.h"
28 #include <stdarg.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <time.h>
33 #include "windef.h"
34 #include "winbase.h"
35 #include "wininet.h"
36 #include "winnls.h"
38 #include "wine/debug.h"
39 #include "internet.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(wininet);
43 #ifndef HAVE_GETADDRINFO
45 /* critical section to protect non-reentrant gethostbyname() */
46 static CRITICAL_SECTION cs_gethostbyname;
47 static CRITICAL_SECTION_DEBUG critsect_debug =
49 0, 0, &cs_gethostbyname,
50 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
51 0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
53 static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };
55 #endif
57 #define TIME_STRING_LEN 30
59 time_t ConvertTimeString(LPCWSTR asctime)
61 WCHAR tmpChar[TIME_STRING_LEN];
62 WCHAR *tmpChar2;
63 struct tm t;
64 int timelen = strlenW(asctime);
66 if(!timelen)
67 return 0;
69 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
70 memset( tmpChar, 0, sizeof(tmpChar) );
71 lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
73 /* Assert that the string is the expected length */
74 if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
76 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
77 * We assume the time is in this format
78 * and divide it into easy to swallow chunks
80 tmpChar[3]='\0';
81 tmpChar[7]='\0';
82 tmpChar[11]='\0';
83 tmpChar[16]='\0';
84 tmpChar[19]='\0';
85 tmpChar[22]='\0';
86 tmpChar[25]='\0';
88 memset( &t, 0, sizeof(t) );
89 t.tm_year = atoiW(tmpChar+12) - 1900;
90 t.tm_mday = atoiW(tmpChar+5);
91 t.tm_hour = atoiW(tmpChar+17);
92 t.tm_min = atoiW(tmpChar+20);
93 t.tm_sec = atoiW(tmpChar+23);
95 /* and month */
96 tmpChar2 = tmpChar + 8;
97 switch(tmpChar2[2])
99 case 'n':
100 if(tmpChar2[1]=='a')
101 t.tm_mon = 0;
102 else
103 t.tm_mon = 5;
104 break;
105 case 'b':
106 t.tm_mon = 1;
107 break;
108 case 'r':
109 if(tmpChar2[1]=='a')
110 t.tm_mon = 2;
111 else
112 t.tm_mon = 3;
113 break;
114 case 'y':
115 t.tm_mon = 4;
116 break;
117 case 'l':
118 t.tm_mon = 6;
119 break;
120 case 'g':
121 t.tm_mon = 7;
122 break;
123 case 'p':
124 t.tm_mon = 8;
125 break;
126 case 't':
127 t.tm_mon = 9;
128 break;
129 case 'v':
130 t.tm_mon = 10;
131 break;
132 case 'c':
133 t.tm_mon = 11;
134 break;
135 default:
136 FIXME("\n");
139 return mktime(&t);
143 BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
144 struct sockaddr_in *psa)
146 WCHAR *found;
147 char *name;
148 int len, sz;
149 #ifdef HAVE_GETADDRINFO
150 struct addrinfo *res, hints;
151 int ret;
152 #else
153 struct hostent *phe;
154 #endif
156 TRACE("%s\n", debugstr_w(lpszServerName));
158 /* Validate server name first
159 * Check if there is sth. like
160 * pinger.macromedia.com:80
161 * if yes, eliminate the :80....
163 found = strchrW(lpszServerName, ':');
164 if (found)
165 len = found - lpszServerName;
166 else
167 len = strlenW(lpszServerName);
169 sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
170 if (!(name = HeapAlloc( GetProcessHeap(), 0, sz + 1 ))) return FALSE;
171 WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
172 name[sz] = 0;
174 #ifdef HAVE_GETADDRINFO
175 memset( &hints, 0, sizeof(struct addrinfo) );
176 hints.ai_family = AF_INET;
178 ret = getaddrinfo( name, NULL, &hints, &res );
179 HeapFree( GetProcessHeap(), 0, name );
180 if (ret != 0)
182 TRACE("failed to get address of %s (%s)\n", debugstr_w(lpszServerName), gai_strerror(ret));
183 return FALSE;
185 memset( psa, 0, sizeof(struct sockaddr_in) );
186 memcpy( &psa->sin_addr, &((struct sockaddr_in *)res->ai_addr)->sin_addr, sizeof(struct in_addr) );
187 psa->sin_family = res->ai_family;
188 psa->sin_port = htons(nServerPort);
190 freeaddrinfo( res );
191 #else
192 EnterCriticalSection( &cs_gethostbyname );
193 phe = gethostbyname(name);
194 HeapFree( GetProcessHeap(), 0, name );
196 if (NULL == phe)
198 TRACE("failed to get address of %s (%d)\n", debugstr_w(lpszServerName), h_errno);
199 LeaveCriticalSection( &cs_gethostbyname );
200 return FALSE;
202 memset(psa,0,sizeof(struct sockaddr_in));
203 memcpy((char *)&psa->sin_addr, phe->h_addr, phe->h_length);
204 psa->sin_family = phe->h_addrtype;
205 psa->sin_port = htons(nServerPort);
207 LeaveCriticalSection( &cs_gethostbyname );
208 #endif
209 return TRUE;
213 * Helper function for sending async Callbacks
216 static const char *get_callback_name(DWORD dwInternetStatus) {
217 static const wininet_flag_info internet_status[] = {
218 #define FE(x) { x, #x }
219 FE(INTERNET_STATUS_RESOLVING_NAME),
220 FE(INTERNET_STATUS_NAME_RESOLVED),
221 FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
222 FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
223 FE(INTERNET_STATUS_SENDING_REQUEST),
224 FE(INTERNET_STATUS_REQUEST_SENT),
225 FE(INTERNET_STATUS_RECEIVING_RESPONSE),
226 FE(INTERNET_STATUS_RESPONSE_RECEIVED),
227 FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
228 FE(INTERNET_STATUS_PREFETCH),
229 FE(INTERNET_STATUS_CLOSING_CONNECTION),
230 FE(INTERNET_STATUS_CONNECTION_CLOSED),
231 FE(INTERNET_STATUS_HANDLE_CREATED),
232 FE(INTERNET_STATUS_HANDLE_CLOSING),
233 FE(INTERNET_STATUS_REQUEST_COMPLETE),
234 FE(INTERNET_STATUS_REDIRECT),
235 FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
236 FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
237 FE(INTERNET_STATUS_STATE_CHANGE),
238 FE(INTERNET_STATUS_COOKIE_SENT),
239 FE(INTERNET_STATUS_COOKIE_RECEIVED),
240 FE(INTERNET_STATUS_PRIVACY_IMPACTED),
241 FE(INTERNET_STATUS_P3P_HEADER),
242 FE(INTERNET_STATUS_P3P_POLICYREF),
243 FE(INTERNET_STATUS_COOKIE_HISTORY)
244 #undef FE
246 DWORD i;
248 for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
249 if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
251 return "Unknown";
254 VOID INTERNET_SendCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
255 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
256 DWORD dwStatusInfoLength)
258 LPVOID lpvNewInfo = NULL;
260 if( !hdr->lpfnStatusCB )
261 return;
263 /* the IE5 version of wininet does not
264 send callbacks if dwContext is zero */
265 if( !dwContext )
266 return;
268 lpvNewInfo = lpvStatusInfo;
269 if(hdr->dwInternalFlags & INET_CALLBACKW) {
270 switch(dwInternetStatus) {
271 case INTERNET_STATUS_NAME_RESOLVED:
272 case INTERNET_STATUS_CONNECTING_TO_SERVER:
273 case INTERNET_STATUS_CONNECTED_TO_SERVER:
274 lpvNewInfo = WININET_strdup_AtoW(lpvStatusInfo);
275 break;
276 case INTERNET_STATUS_RESOLVING_NAME:
277 case INTERNET_STATUS_REDIRECT:
278 lpvNewInfo = WININET_strdupW(lpvStatusInfo);
279 break;
281 }else {
282 switch(dwInternetStatus)
284 case INTERNET_STATUS_NAME_RESOLVED:
285 case INTERNET_STATUS_CONNECTING_TO_SERVER:
286 case INTERNET_STATUS_CONNECTED_TO_SERVER:
287 lpvNewInfo = HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo) + 1);
288 if (lpvNewInfo) strcpy(lpvNewInfo, lpvStatusInfo);
289 break;
290 case INTERNET_STATUS_RESOLVING_NAME:
291 case INTERNET_STATUS_REDIRECT:
292 lpvNewInfo = WININET_strdup_WtoA(lpvStatusInfo);
293 break;
297 TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %p, %d)\n",
298 hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
299 lpvNewInfo, dwStatusInfoLength);
301 hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
302 lpvNewInfo, dwStatusInfoLength);
304 TRACE(" end callback().\n");
306 if(lpvNewInfo != lpvStatusInfo)
307 HeapFree(GetProcessHeap(), 0, lpvNewInfo);
310 static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
312 struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;
314 TRACE("%p\n", workRequest->hdr);
316 INTERNET_SendCallback(workRequest->hdr,
317 req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
318 req->dwStatusInfoLength);
320 /* And frees the copy of the status info */
321 HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
324 VOID SendAsyncCallback(LPWININETHANDLEHEADER hdr, DWORD_PTR dwContext,
325 DWORD dwInternetStatus, LPVOID lpvStatusInfo,
326 DWORD dwStatusInfoLength)
328 TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
329 hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
330 lpvStatusInfo, dwStatusInfoLength,
331 hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
332 hdr->lpfnStatusCB);
334 if (!(hdr->lpfnStatusCB))
335 return;
337 if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
339 WORKREQUEST workRequest;
340 struct WORKREQ_SENDCALLBACK *req;
341 void *lpvStatusInfo_copy = lpvStatusInfo;
343 if (lpvStatusInfo)
345 lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
346 memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
349 workRequest.asyncproc = SendAsyncCallbackProc;
350 workRequest.hdr = WININET_AddRef( hdr );
351 req = &workRequest.u.SendCallback;
352 req->dwContext = dwContext;
353 req->dwInternetStatus = dwInternetStatus;
354 req->lpvStatusInfo = lpvStatusInfo_copy;
355 req->dwStatusInfoLength = dwStatusInfoLength;
357 INTERNET_AsyncCall(&workRequest);
359 else
360 INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
361 lpvStatusInfo, dwStatusInfoLength);