utility.c 11.5 KB
Newer Older
1
/*
2 3 4
 * Wininet - Utility functions
 *
 * Copyright 1999 Corel Corporation
5
 * Copyright 2002 CodeWeavers Inc.
6 7
 *
 * Ulrich Czekalla
8
 * Aric Stewart
9
 *
10 11 12 13 14 15 16 17 18 19 20 21
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
23 24
 */

25
#include "config.h"
26
#include "wine/port.h"
27

28 29 30 31
#if defined(__MINGW32__) || defined (_MSC_VER)
#include <ws2tcpip.h>
#endif

32
#include <stdarg.h>
33 34 35 36
#include <stdlib.h>
#include <string.h>
#include <time.h>

37 38
#include "windef.h"
#include "winbase.h"
39
#include "wininet.h"
40
#include "winnls.h"
41

42
#include "wine/debug.h"
43 44
#include "internet.h"

45
WINE_DEFAULT_DEBUG_CHANNEL(wininet);
46

47 48
#ifndef HAVE_GETADDRINFO

49 50 51 52 53 54 55 56 57 58
/* critical section to protect non-reentrant gethostbyname() */
static CRITICAL_SECTION cs_gethostbyname;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
    0, 0, &cs_gethostbyname,
    { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": cs_gethostbyname") }
};
static CRITICAL_SECTION cs_gethostbyname = { &critsect_debug, -1, 0, 0, 0, 0 };

59 60
#endif

61 62
#define TIME_STRING_LEN  30

63
time_t ConvertTimeString(LPCWSTR asctime)
64
{
65 66
    WCHAR tmpChar[TIME_STRING_LEN];
    WCHAR *tmpChar2;
67
    struct tm t;
68
    int timelen = strlenW(asctime);
69

70
    if(!timelen)
71 72
        return 0;

73 74 75
    /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
    memset( tmpChar, 0, sizeof(tmpChar) );
    lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
76 77

    /* Assert that the string is the expected length */
78
    if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
79

80 81 82 83 84 85 86 87 88 89 90 91
    /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
     * We assume the time is in this format
     * and divide it into easy to swallow chunks
     */
    tmpChar[3]='\0';
    tmpChar[7]='\0';
    tmpChar[11]='\0';
    tmpChar[16]='\0';
    tmpChar[19]='\0';
    tmpChar[22]='\0';
    tmpChar[25]='\0';

92
    memset( &t, 0, sizeof(t) );
93 94 95 96 97
    t.tm_year = atoiW(tmpChar+12) - 1900;
    t.tm_mday = atoiW(tmpChar+5);
    t.tm_hour = atoiW(tmpChar+17);
    t.tm_min = atoiW(tmpChar+20);
    t.tm_sec = atoiW(tmpChar+23);
98

99 100 101 102 103 104
    /* and month */
    tmpChar2 = tmpChar + 8;
    switch(tmpChar2[2])
    {
        case 'n':
            if(tmpChar2[1]=='a')
105
                t.tm_mon = 0;
106
            else
107
                t.tm_mon = 5;
108 109
            break;
        case 'b':
110
            t.tm_mon = 1;
111 112 113
            break;
        case 'r':
            if(tmpChar2[1]=='a')
114
                t.tm_mon = 2;
115
            else
116
                t.tm_mon = 3;
117 118
            break;
        case 'y':
119
            t.tm_mon = 4;
120 121
            break;
        case 'l':
122
            t.tm_mon = 6;
123 124
            break;
        case 'g':
125
            t.tm_mon = 7;
126 127
            break;
        case 'p':
128
            t.tm_mon = 8;
129 130
            break;
        case 't':
131
            t.tm_mon = 9;
132 133
            break;
        case 'v':
134
            t.tm_mon = 10;
135 136
            break;
        case 'c':
137
            t.tm_mon = 11;
138 139 140 141 142
            break;
        default:
            FIXME("\n");
    }

143
    return mktime(&t);
144 145 146
}


147
BOOL GetAddress(LPCWSTR lpszServerName, INTERNET_PORT nServerPort,
148
	struct sockaddr *psa, socklen_t *sa_len)
149
{
150 151 152
    WCHAR *found;
    char *name;
    int len, sz;
153 154 155 156
#ifdef HAVE_GETADDRINFO
    struct addrinfo *res, hints;
    int ret;
#else
157
    struct hostent *phe;
158
    struct sockaddr_in *sin = (struct sockaddr_in *)psa;
159
#endif
160

161
    TRACE("%s\n", debugstr_w(lpszServerName));
162

163 164 165 166 167
    /* Validate server name first
     * Check if there is sth. like
     * pinger.macromedia.com:80
     * if yes, eliminate the :80....
     */
168
    found = strchrW(lpszServerName, ':');
169
    if (found)
170 171 172 173 174
        len = found - lpszServerName;
    else
        len = strlenW(lpszServerName);

    sz = WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, NULL, 0, NULL, NULL );
175
    if (!(name = HeapAlloc( GetProcessHeap(), 0, sz + 1 ))) return FALSE;
176 177
    WideCharToMultiByte( CP_UNIXCP, 0, lpszServerName, len, name, sz, NULL, NULL );
    name[sz] = 0;
178

179 180
#ifdef HAVE_GETADDRINFO
    memset( &hints, 0, sizeof(struct addrinfo) );
181 182 183
    /* Prefer IPv4 to IPv6 addresses, since some servers do not listen on
     * their IPv6 addresses even though they have IPv6 addresses in the DNS.
     */
184 185 186 187 188 189
    hints.ai_family = AF_INET;

    ret = getaddrinfo( name, NULL, &hints, &res );
    HeapFree( GetProcessHeap(), 0, name );
    if (ret != 0)
    {
190 191 192 193 194 195 196 197
        TRACE("failed to get IPv4 address of %s (%s), retrying with IPv6\n", debugstr_w(lpszServerName), gai_strerror(ret));
        hints.ai_family = AF_INET6;
        ret = getaddrinfo( name, NULL, &hints, &res );
        if (ret != 0)
        {
            TRACE("failed to get address of %s (%s)\n", debugstr_w(lpszServerName), gai_strerror(ret));
            return FALSE;
        }
198
    }
199
    if (*sa_len < res->ai_addrlen)
200 201 202 203 204
    {
        WARN("address too small\n");
        freeaddrinfo( res );
        return FALSE;
    }
205 206 207 208 209 210 211 212
    *sa_len = res->ai_addrlen;
    memcpy( psa, res->ai_addr, res->ai_addrlen );
    /* Copy port */
    switch (res->ai_family)
    {
    case AF_INET:
        ((struct sockaddr_in *)psa)->sin_port = htons(nServerPort);
        break;
213 214 215
    case AF_INET6:
        ((struct sockaddr_in6 *)psa)->sin6_port = htons(nServerPort);
        break;
216
    }
217 218 219

    freeaddrinfo( res );
#else
220
    EnterCriticalSection( &cs_gethostbyname );
221
    phe = gethostbyname(name);
222
    HeapFree( GetProcessHeap(), 0, name );
223

224
    if (NULL == phe)
225
    {
226
        TRACE("failed to get address of %s (%d)\n", debugstr_w(lpszServerName), h_errno);
227
        LeaveCriticalSection( &cs_gethostbyname );
228 229
        return FALSE;
    }
230 231 232 233 234 235 236 237 238 239 240
    if (*sa_len < sizeof(struct sockaddr_in))
    {
        WARN("address too small\n");
        LeaveCriticalSection( &cs_gethostbyname );
        return FALSE;
    }
    *sa_len = sizeof(struct sockaddr_in);
    memset(sin,0,sizeof(struct sockaddr_in));
    memcpy((char *)&sin->sin_addr, phe->h_addr, phe->h_length);
    sin->sin_family = phe->h_addrtype;
    sin->sin_port = htons(nServerPort);
241

242
    LeaveCriticalSection( &cs_gethostbyname );
243
#endif
244 245
    return TRUE;
}
246 247 248 249 250

/*
 * Helper function for sending async Callbacks
 */

251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
static const char *get_callback_name(DWORD dwInternetStatus) {
    static const wininet_flag_info internet_status[] = {
#define FE(x) { x, #x }
	FE(INTERNET_STATUS_RESOLVING_NAME),
	FE(INTERNET_STATUS_NAME_RESOLVED),
	FE(INTERNET_STATUS_CONNECTING_TO_SERVER),
	FE(INTERNET_STATUS_CONNECTED_TO_SERVER),
	FE(INTERNET_STATUS_SENDING_REQUEST),
	FE(INTERNET_STATUS_REQUEST_SENT),
	FE(INTERNET_STATUS_RECEIVING_RESPONSE),
	FE(INTERNET_STATUS_RESPONSE_RECEIVED),
	FE(INTERNET_STATUS_CTL_RESPONSE_RECEIVED),
	FE(INTERNET_STATUS_PREFETCH),
	FE(INTERNET_STATUS_CLOSING_CONNECTION),
	FE(INTERNET_STATUS_CONNECTION_CLOSED),
	FE(INTERNET_STATUS_HANDLE_CREATED),
	FE(INTERNET_STATUS_HANDLE_CLOSING),
	FE(INTERNET_STATUS_REQUEST_COMPLETE),
	FE(INTERNET_STATUS_REDIRECT),
	FE(INTERNET_STATUS_INTERMEDIATE_RESPONSE),
	FE(INTERNET_STATUS_USER_INPUT_REQUIRED),
	FE(INTERNET_STATUS_STATE_CHANGE),
	FE(INTERNET_STATUS_COOKIE_SENT),
	FE(INTERNET_STATUS_COOKIE_RECEIVED),
	FE(INTERNET_STATUS_PRIVACY_IMPACTED),
	FE(INTERNET_STATUS_P3P_HEADER),
	FE(INTERNET_STATUS_P3P_POLICYREF),
Lionel Ulmer's avatar
Lionel Ulmer committed
278
	FE(INTERNET_STATUS_COOKIE_HISTORY)
279 280
#undef FE
    };
281
    DWORD i;
282 283 284 285 286 287 288

    for (i = 0; i < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
	if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
    }
    return "Unknown";
}

289
VOID INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR dwContext,
290 291
                           DWORD dwInternetStatus, LPVOID lpvStatusInfo,
                           DWORD dwStatusInfoLength)
292
{
293
    LPVOID lpvNewInfo = NULL;
294

295
    if( !hdr->lpfnStatusCB )
296
        return;
297

298 299 300 301
    /* the IE5 version of wininet does not
       send callbacks if dwContext is zero */
    if( !dwContext )
        return;
302

303
    lpvNewInfo = lpvStatusInfo;
304 305 306 307 308
    if(hdr->dwInternalFlags & INET_CALLBACKW) {
        switch(dwInternetStatus) {
        case INTERNET_STATUS_NAME_RESOLVED:
        case INTERNET_STATUS_CONNECTING_TO_SERVER:
        case INTERNET_STATUS_CONNECTED_TO_SERVER:
309
            lpvNewInfo = heap_strdupAtoW(lpvStatusInfo);
310 311 312
            break;
        case INTERNET_STATUS_RESOLVING_NAME:
        case INTERNET_STATUS_REDIRECT:
313
            lpvNewInfo = heap_strdupW(lpvStatusInfo);
314
            break;
315 316
        }
    }else {
317 318
        switch(dwInternetStatus)
        {
319 320 321 322 323 324
        case INTERNET_STATUS_NAME_RESOLVED:
        case INTERNET_STATUS_CONNECTING_TO_SERVER:
        case INTERNET_STATUS_CONNECTED_TO_SERVER:
            lpvNewInfo = HeapAlloc(GetProcessHeap(), 0, strlen(lpvStatusInfo) + 1);
            if (lpvNewInfo) strcpy(lpvNewInfo, lpvStatusInfo);
            break;
325 326
        case INTERNET_STATUS_RESOLVING_NAME:
        case INTERNET_STATUS_REDIRECT:
327
            lpvNewInfo = heap_strdupWtoA(lpvStatusInfo);
328
            break;
329
        }
330
    }
Lionel Ulmer's avatar
Lionel Ulmer committed
331
    
332
    TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %p, %d)\n",
333
	  hdr->lpfnStatusCB, hdr->hInternet, hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
Lionel Ulmer's avatar
Lionel Ulmer committed
334 335
	  lpvNewInfo, dwStatusInfoLength);
    
336
    hdr->lpfnStatusCB(hdr->hInternet, dwContext, dwInternetStatus,
337
                      lpvNewInfo, dwStatusInfoLength);
Lionel Ulmer's avatar
Lionel Ulmer committed
338 339 340

    TRACE(" end callback().\n");

341
    if(lpvNewInfo != lpvStatusInfo)
342
        HeapFree(GetProcessHeap(), 0, lpvNewInfo);
343 344
}

345 346 347 348 349
static void SendAsyncCallbackProc(WORKREQUEST *workRequest)
{
    struct WORKREQ_SENDCALLBACK const *req = &workRequest->u.SendCallback;

    TRACE("%p\n", workRequest->hdr);
350

351 352 353 354 355 356 357
    INTERNET_SendCallback(workRequest->hdr,
                          req->dwContext, req->dwInternetStatus, req->lpvStatusInfo,
                          req->dwStatusInfoLength);

    /* And frees the copy of the status info */
    HeapFree(GetProcessHeap(), 0, req->lpvStatusInfo);
}
358

359
void SendAsyncCallback(object_header_t *hdr, DWORD_PTR dwContext,
360 361
                       DWORD dwInternetStatus, LPVOID lpvStatusInfo,
                       DWORD dwStatusInfoLength)
362
{
363
    TRACE("(%p, %08lx, %d (%s), %p, %d): %sasync call with callback %p\n",
Lionel Ulmer's avatar
Lionel Ulmer committed
364 365 366 367 368 369 370 371 372 373 374 375
	  hdr, dwContext, dwInternetStatus, get_callback_name(dwInternetStatus),
	  lpvStatusInfo, dwStatusInfoLength,
	  hdr->dwFlags & INTERNET_FLAG_ASYNC ? "" : "non ",
	  hdr->lpfnStatusCB);
    
    if (!(hdr->lpfnStatusCB))
	return;
    
    if (hdr->dwFlags & INTERNET_FLAG_ASYNC)
    {
	WORKREQUEST workRequest;
	struct WORKREQ_SENDCALLBACK *req;
376 377 378 379 380 381 382 383
	void *lpvStatusInfo_copy = lpvStatusInfo;

	if (lpvStatusInfo)
	{
	    lpvStatusInfo_copy = HeapAlloc(GetProcessHeap(), 0, dwStatusInfoLength);
	    memcpy(lpvStatusInfo_copy, lpvStatusInfo, dwStatusInfoLength);
	}

384
	workRequest.asyncproc = SendAsyncCallbackProc;
Lionel Ulmer's avatar
Lionel Ulmer committed
385 386 387 388
	workRequest.hdr = WININET_AddRef( hdr );
	req = &workRequest.u.SendCallback;
	req->dwContext = dwContext;
	req->dwInternetStatus = dwInternetStatus;
389
	req->lpvStatusInfo = lpvStatusInfo_copy;
Lionel Ulmer's avatar
Lionel Ulmer committed
390 391 392 393 394
	req->dwStatusInfoLength = dwStatusInfoLength;
	
	INTERNET_AsyncCall(&workRequest);
    }
    else
395 396
	INTERNET_SendCallback(hdr, dwContext, dwInternetStatus,
			      lpvStatusInfo, dwStatusInfoLength);
397
}