utility.c 7.56 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 "ws2tcpip.h"
26

27
#include <stdarg.h>
28 29 30 31
#include <stdlib.h>
#include <string.h>
#include <time.h>

32 33
#include "windef.h"
#include "winbase.h"
34
#include "wininet.h"
35
#include "winnls.h"
36

37
#include "wine/debug.h"
38 39
#include "internet.h"

40
WINE_DEFAULT_DEBUG_CHANNEL(wininet);
41 42 43

#define TIME_STRING_LEN  30

44
time_t ConvertTimeString(LPCWSTR asctime)
45
{
46 47
    WCHAR tmpChar[TIME_STRING_LEN];
    WCHAR *tmpChar2;
48
    struct tm t;
49
    int timelen = lstrlenW(asctime);
50

51
    if(!timelen)
52 53
        return 0;

54 55 56
    /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
    memset( tmpChar, 0, sizeof(tmpChar) );
    lstrcpynW(tmpChar, asctime, TIME_STRING_LEN);
57 58

    /* Assert that the string is the expected length */
59
    if (lstrlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
60

61 62 63 64 65 66 67 68 69 70 71 72
    /* 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';

73
    memset( &t, 0, sizeof(t) );
74 75 76 77 78
    t.tm_year = wcstol(tmpChar+12, NULL, 10) - 1900;
    t.tm_mday = wcstol(tmpChar+5, NULL, 10);
    t.tm_hour = wcstol(tmpChar+17, NULL, 10);
    t.tm_min = wcstol(tmpChar+20, NULL, 10);
    t.tm_sec = wcstol(tmpChar+23, NULL, 10);
79

80 81 82 83 84 85
    /* and month */
    tmpChar2 = tmpChar + 8;
    switch(tmpChar2[2])
    {
        case 'n':
            if(tmpChar2[1]=='a')
86
                t.tm_mon = 0;
87
            else
88
                t.tm_mon = 5;
89 90
            break;
        case 'b':
91
            t.tm_mon = 1;
92 93 94
            break;
        case 'r':
            if(tmpChar2[1]=='a')
95
                t.tm_mon = 2;
96
            else
97
                t.tm_mon = 3;
98 99
            break;
        case 'y':
100
            t.tm_mon = 4;
101 102
            break;
        case 'l':
103
            t.tm_mon = 6;
104 105
            break;
        case 'g':
106
            t.tm_mon = 7;
107 108
            break;
        case 'p':
109
            t.tm_mon = 8;
110 111
            break;
        case 't':
112
            t.tm_mon = 9;
113 114
            break;
        case 'v':
115
            t.tm_mon = 10;
116 117
            break;
        case 'c':
118
            t.tm_mon = 11;
119 120 121 122 123
            break;
        default:
            FIXME("\n");
    }

124
    return mktime(&t);
125 126 127
}


128
BOOL GetAddress(const WCHAR *name, INTERNET_PORT port, struct sockaddr *psa, int *sa_len, char *addr_str)
129
{
130
    ADDRINFOW *res, hints;
131
    void *addr = NULL;
132
    int ret;
133

134
    TRACE("%s\n", debugstr_w(name));
135

136
    memset( &hints, 0, sizeof(hints) );
137 138 139
    /* Prefer IPv4 to IPv6 addresses, since some servers do not listen on
     * their IPv6 addresses even though they have IPv6 addresses in the DNS.
     */
140 141
    hints.ai_family = AF_INET;

142
    ret = GetAddrInfoW(name, NULL, &hints, &res);
143 144
    if (ret != 0)
    {
145
        TRACE("failed to get IPv4 address of %s, retrying with IPv6\n", debugstr_w(name));
146
        hints.ai_family = AF_INET6;
147
        ret = GetAddrInfoW(name, NULL, &hints, &res);
148 149 150
    }
    if (ret != 0)
    {
151
        TRACE("failed to get address of %s\n", debugstr_w(name));
152
        return FALSE;
153
    }
154
    if (*sa_len < res->ai_addrlen)
155 156
    {
        WARN("address too small\n");
157
        FreeAddrInfoW(res);
158 159
        return FALSE;
    }
160 161 162 163 164 165
    *sa_len = res->ai_addrlen;
    memcpy( psa, res->ai_addr, res->ai_addrlen );
    /* Copy port */
    switch (res->ai_family)
    {
    case AF_INET:
166
        addr = &((struct sockaddr_in *)psa)->sin_addr;
167
        ((struct sockaddr_in *)psa)->sin_port = htons(port);
168
        break;
169
    case AF_INET6:
170
        addr = &((struct sockaddr_in6 *)psa)->sin6_addr;
171
        ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
172
        break;
173
    }
174

175 176
    if(addr_str)
        inet_ntop(res->ai_family, addr, addr_str, INET6_ADDRSTRLEN);
177
    FreeAddrInfoW(res);
178 179
    return TRUE;
}
180 181 182 183 184

/*
 * Helper function for sending async Callbacks
 */

185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
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
212
	FE(INTERNET_STATUS_COOKIE_HISTORY)
213 214
#undef FE
    };
215
    DWORD i;
216

217
    for (i = 0; i < ARRAY_SIZE(internet_status); i++) {
218 219 220 221 222
	if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
    }
    return "Unknown";
}

223 224 225 226 227 228 229 230 231 232 233 234
static const char *debugstr_status_info(DWORD status, void *info)
{
    switch(status) {
    case INTERNET_STATUS_REQUEST_COMPLETE: {
        INTERNET_ASYNC_RESULT *iar = info;
        return wine_dbg_sprintf("{%s, %d}", wine_dbgstr_longlong(iar->dwResult), iar->dwError);
    }
    default:
        return wine_dbg_sprintf("%p", info);
    }
}

235
void INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR context, DWORD status, void *info, DWORD info_len)
236
{
237
    void *new_info = info;
238

239
    if( !hdr->lpfnStatusCB )
240
        return;
241

242 243
    /* the IE5 version of wininet does not
       send callbacks if dwContext is zero */
244
    if(!context)
245
        return;
246

247 248 249 250 251 252 253 254 255 256 257 258
    switch(status) {
    case INTERNET_STATUS_NAME_RESOLVED:
    case INTERNET_STATUS_CONNECTING_TO_SERVER:
    case INTERNET_STATUS_CONNECTED_TO_SERVER:
        new_info = heap_alloc(info_len);
        if(new_info)
            memcpy(new_info, info, info_len);
        break;
    case INTERNET_STATUS_RESOLVING_NAME:
    case INTERNET_STATUS_REDIRECT:
        if(hdr->dwInternalFlags & INET_CALLBACKW) {
            new_info = heap_strdupW(info);
259
            break;
260 261 262
        }else {
            new_info = heap_strdupWtoA(info);
            info_len = strlen(new_info)+1;
263
            break;
264
        }
265
    }
Lionel Ulmer's avatar
Lionel Ulmer committed
266
    
267
    TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %s, %d)\n",
268 269
	  hdr->lpfnStatusCB, hdr->hInternet, hdr, context, status, get_callback_name(status),
	  debugstr_status_info(status, new_info), info_len);
Lionel Ulmer's avatar
Lionel Ulmer committed
270
    
271
    hdr->lpfnStatusCB(hdr->hInternet, context, status, new_info, info_len);
Lionel Ulmer's avatar
Lionel Ulmer committed
272 273 274

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

275 276
    if(new_info != info)
        heap_free(new_info);
277
}