ReactOS 0.4.15-dev-7842-g558ab78
utility.c
Go to the documentation of this file.
1#ifdef __REACTOS__
2#include "precomp.h"
3#include "inet_ntop.c"
4#else
5/*
6 * Wininet - Utility functions
7 *
8 * Copyright 1999 Corel Corporation
9 * Copyright 2002 CodeWeavers Inc.
10 *
11 * Ulrich Czekalla
12 * Aric Stewart
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 */
28
29#include "ws2tcpip.h"
30
31#include <stdarg.h>
32#include <stdlib.h>
33#include <string.h>
34#include <time.h>
35
36#include "windef.h"
37#include "winbase.h"
38#include "wininet.h"
39#include "winnls.h"
40
41#include "wine/debug.h"
42#include "internet.h"
43#endif /* defined(__REACTOS__) */
44
46
47#define TIME_STRING_LEN 30
48
50{
51 WCHAR tmpChar[TIME_STRING_LEN];
52 WCHAR *tmpChar2;
53 struct tm t;
54 int timelen = lstrlenW(asctime);
55
56 if(!timelen)
57 return 0;
58
59 /* FIXME: the atoiWs below rely on that tmpChar is \0 padded */
60 memset( tmpChar, 0, sizeof(tmpChar) );
62
63 /* Assert that the string is the expected length */
65
66 /* Convert a time such as 'Mon, 15 Nov 1999 16:09:35 GMT' into a SYSTEMTIME structure
67 * We assume the time is in this format
68 * and divide it into easy to swallow chunks
69 */
70 tmpChar[3]='\0';
71 tmpChar[7]='\0';
72 tmpChar[11]='\0';
73 tmpChar[16]='\0';
74 tmpChar[19]='\0';
75 tmpChar[22]='\0';
76 tmpChar[25]='\0';
77
78 memset( &t, 0, sizeof(t) );
79 t.tm_year = wcstol(tmpChar+12, NULL, 10) - 1900;
80 t.tm_mday = wcstol(tmpChar+5, NULL, 10);
81 t.tm_hour = wcstol(tmpChar+17, NULL, 10);
82 t.tm_min = wcstol(tmpChar+20, NULL, 10);
83 t.tm_sec = wcstol(tmpChar+23, NULL, 10);
84
85 /* and month */
86 tmpChar2 = tmpChar + 8;
87 switch(tmpChar2[2])
88 {
89 case 'n':
90 if(tmpChar2[1]=='a')
91 t.tm_mon = 0;
92 else
93 t.tm_mon = 5;
94 break;
95 case 'b':
96 t.tm_mon = 1;
97 break;
98 case 'r':
99 if(tmpChar2[1]=='a')
100 t.tm_mon = 2;
101 else
102 t.tm_mon = 3;
103 break;
104 case 'y':
105 t.tm_mon = 4;
106 break;
107 case 'l':
108 t.tm_mon = 6;
109 break;
110 case 'g':
111 t.tm_mon = 7;
112 break;
113 case 'p':
114 t.tm_mon = 8;
115 break;
116 case 't':
117 t.tm_mon = 9;
118 break;
119 case 'v':
120 t.tm_mon = 10;
121 break;
122 case 'c':
123 t.tm_mon = 11;
124 break;
125 default:
126 FIXME("\n");
127 }
128
129 return mktime(&t);
130}
131
132
133BOOL GetAddress(const WCHAR *name, INTERNET_PORT port, struct sockaddr *psa, int *sa_len, char *addr_str)
134{
136 void *addr = NULL;
137 int ret;
138
139 TRACE("%s\n", debugstr_w(name));
140
141 memset( &hints, 0, sizeof(hints) );
142 /* Prefer IPv4 to IPv6 addresses, since some servers do not listen on
143 * their IPv6 addresses even though they have IPv6 addresses in the DNS.
144 */
145 hints.ai_family = AF_INET;
146
148 if (ret != 0)
149 {
150 TRACE("failed to get IPv4 address of %s, retrying with IPv6\n", debugstr_w(name));
151 hints.ai_family = AF_INET6;
153 }
154 if (ret != 0)
155 {
156 TRACE("failed to get address of %s\n", debugstr_w(name));
157 return FALSE;
158 }
159 if (*sa_len < res->ai_addrlen)
160 {
161 WARN("address too small\n");
163 return FALSE;
164 }
165 *sa_len = res->ai_addrlen;
166 memcpy( psa, res->ai_addr, res->ai_addrlen );
167 /* Copy port */
168 switch (res->ai_family)
169 {
170 case AF_INET:
171 addr = &((struct sockaddr_in *)psa)->sin_addr;
172 ((struct sockaddr_in *)psa)->sin_port = htons(port);
173 break;
174 case AF_INET6:
175 addr = &((struct sockaddr_in6 *)psa)->sin6_addr;
176 ((struct sockaddr_in6 *)psa)->sin6_port = htons(port);
177 break;
178 }
179
180 if(addr_str)
181 inet_ntop(res->ai_family, addr, addr_str, INET6_ADDRSTRLEN);
183 return TRUE;
184}
185
186/*
187 * Helper function for sending async Callbacks
188 */
189
190static const char *get_callback_name(DWORD dwInternetStatus) {
191 static const wininet_flag_info internet_status[] = {
192#define FE(x) { x, #x }
218#undef FE
219 };
220 DWORD i;
221
222 for (i = 0; i < ARRAY_SIZE(internet_status); i++) {
223 if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
224 }
225 return "Unknown";
226}
227
228static const char *debugstr_status_info(DWORD status, void *info)
229{
230 switch(status) {
233 return wine_dbg_sprintf("{%s, %d}", wine_dbgstr_longlong(iar->dwResult), iar->dwError);
234 }
235 default:
236 return wine_dbg_sprintf("%p", info);
237 }
238}
239
241{
242 void *new_info = info;
243
244 if( !hdr->lpfnStatusCB )
245 return;
246
247 /* the IE5 version of wininet does not
248 send callbacks if dwContext is zero */
249 if(!context)
250 return;
251
252 switch(status) {
256 new_info = heap_alloc(info_len);
257 if(new_info)
258 memcpy(new_info, info, info_len);
259 break;
262 if(hdr->dwInternalFlags & INET_CALLBACKW) {
263 new_info = heap_strdupW(info);
264 break;
265 }else {
266 new_info = heap_strdupWtoA(info);
267 info_len = strlen(new_info)+1;
268 break;
269 }
270 }
271
272 TRACE(" callback(%p) (%p (%p), %08lx, %d (%s), %s, %d)\n",
273 hdr->lpfnStatusCB, hdr->hInternet, hdr, context, status, get_callback_name(status),
274 debugstr_status_info(status, new_info), info_len);
275
276 hdr->lpfnStatusCB(hdr->hInternet, context, status, new_info, info_len);
277
278 TRACE(" end callback().\n");
279
280 if(new_info != info)
281 heap_free(new_info);
282}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define FreeAddrInfoW(a)
Definition: addrinfo.c:21
INT WSAAPI GetAddrInfoW(IN PCWSTR pszNodeName, IN PCWSTR pszServiceName, IN const ADDRINFOW *ptHints, OUT PADDRINFOW *pptResult)
Definition: addrinfo.c:509
static char * heap_strdupWtoA(const WCHAR *str)
static void * heap_alloc(size_t len)
Definition: appwiz.h:66
static BOOL heap_free(void *mem)
Definition: appwiz.h:76
#define WINE_DEFAULT_DEBUG_CHANNEL(t)
Definition: precomp.h:23
#define ARRAY_SIZE(A)
Definition: main.h:33
#define FIXME(fmt,...)
Definition: debug.h:111
#define WARN(fmt,...)
Definition: debug.h:112
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
static WCHAR * heap_strdupW(const WCHAR *str)
Definition: propsheet.c:178
const char * wine_dbg_sprintf(const char *format,...)
Definition: compat.c:296
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:49
#define lstrcpynW
Definition: compat.h:738
#define lstrlenW
Definition: compat.h:750
USHORT port
Definition: uri.c:228
void INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR context, DWORD status, void *info, DWORD info_len)
Definition: utility.c:240
static const char * debugstr_status_info(DWORD status, void *info)
Definition: utility.c:228
time_t ConvertTimeString(LPCWSTR asctime)
Definition: utility.c:49
static const char * get_callback_name(DWORD dwInternetStatus)
Definition: utility.c:190
#define FE(x)
#define TIME_STRING_LEN
Definition: utility.c:47
BOOL GetAddress(const WCHAR *name, INTERNET_PORT port, struct sockaddr *psa, int *sa_len, char *addr_str)
Definition: utility.c:133
__kernel_time_t time_t
Definition: linux.h:252
#define AF_INET
Definition: tcpip.h:117
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLdouble GLdouble t
Definition: gl.h:2047
GLuint res
Definition: glext.h:9613
GLenum const GLvoid * addr
Definition: glext.h:9621
GLuint GLfloat * val
Definition: glext.h:7180
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_ long __cdecl wcstol(_In_z_ const wchar_t *_Str, _Out_opt_ _Deref_post_z_ wchar_t **_EndPtr, _In_ int _Radix)
#define INET_CALLBACKW
Definition: internet.h:244
char hdr[14]
Definition: iptest.cpp:33
#define debugstr_w
Definition: kernel32.h:32
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define htons(x)
Definition: module.h:215
static SCRIPT_CACHE SCRIPT_ANALYSIS * psa
Definition: usp10.c:64
_Check_return_ _CRTIMP char *__cdecl asctime(_In_ const struct tm *_Tm)
_CRTIMP time_t __cdecl mktime(struct tm *_Tm)
Definition: time.h:418
#define memset(x, y, z)
Definition: compat.h:39
namespace GUID const ADDRINFOEXW * hints
Definition: sock.c:80
#define TRACE(s)
Definition: solgame.cpp:4
DWORD_PTR dwResult
Definition: wininet.h:155
Definition: http.c:7252
Definition: name.c:39
Definition: ps.c:97
Definition: time.h:68
const char * name
Definition: internet.h:468
uint32_t DWORD_PTR
Definition: typedefs.h:65
const char *WSAAPI inet_ntop(int af, const void *src, char *dst, size_t cnt)
Definition: unix_func.c:8
int ret
WORD INTERNET_PORT
Definition: winhttp.h:38
#define INTERNET_STATUS_RECEIVING_RESPONSE
Definition: wininet.h:889
#define INTERNET_STATUS_CONNECTION_CLOSED
Definition: wininet.h:894
#define INTERNET_STATUS_STATE_CHANGE
Definition: wininet.h:902
#define INTERNET_STATUS_REQUEST_SENT
Definition: wininet.h:888
#define INTERNET_STATUS_COOKIE_RECEIVED
Definition: wininet.h:904
#define INTERNET_STATUS_USER_INPUT_REQUIRED
Definition: wininet.h:901
#define INTERNET_STATUS_SENDING_REQUEST
Definition: wininet.h:887
#define INTERNET_STATUS_PREFETCH
Definition: wininet.h:892
#define INTERNET_STATUS_HANDLE_CLOSING
Definition: wininet.h:896
#define INTERNET_STATUS_RESOLVING_NAME
Definition: wininet.h:883
#define INTERNET_STATUS_INTERMEDIATE_RESPONSE
Definition: wininet.h:900
#define INTERNET_STATUS_RESPONSE_RECEIVED
Definition: wininet.h:890
#define INTERNET_STATUS_REDIRECT
Definition: wininet.h:899
#define INTERNET_STATUS_REQUEST_COMPLETE
Definition: wininet.h:898
#define INTERNET_STATUS_CONNECTING_TO_SERVER
Definition: wininet.h:885
#define INTERNET_STATUS_HANDLE_CREATED
Definition: wininet.h:895
#define INTERNET_STATUS_COOKIE_HISTORY
Definition: wininet.h:908
#define INTERNET_STATUS_COOKIE_SENT
Definition: wininet.h:903
#define INTERNET_STATUS_CONNECTED_TO_SERVER
Definition: wininet.h:886
#define INTERNET_STATUS_NAME_RESOLVED
Definition: wininet.h:884
#define INTERNET_STATUS_CTL_RESPONSE_RECEIVED
Definition: wininet.h:891
#define INTERNET_STATUS_P3P_HEADER
Definition: wininet.h:906
#define INTERNET_STATUS_CLOSING_CONNECTION
Definition: wininet.h:893
#define INTERNET_STATUS_PRIVACY_IMPACTED
Definition: wininet.h:905
#define INTERNET_STATUS_P3P_POLICYREF
Definition: wininet.h:907
#define AF_INET6
Definition: winsock.h:369
#define INET6_ADDRSTRLEN
Definition: ws2ipdef.h:132
__wchar_t WCHAR
Definition: xmlstorage.h:180
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185