ReactOS  0.4.14-dev-115-g4576127
utility.c
Go to the documentation of this file.
1 /*
2  * Wininet - Utility functions
3  *
4  * Copyright 1999 Corel Corporation
5  * Copyright 2002 CodeWeavers Inc.
6  *
7  * Ulrich Czekalla
8  * Aric Stewart
9  *
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.
14  *
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.
19  *
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
23  */
24 
25 #include "ws2tcpip.h"
26 
27 #include <stdarg.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 
32 #include "windef.h"
33 #include "winbase.h"
34 #include "wininet.h"
35 #include "winnls.h"
36 
37 #include "wine/debug.h"
38 #include "internet.h"
39 
40 #ifdef __REACTOS__
41 #include <stdio.h>
42 #include "inet_ntop.c"
43 #endif
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 = strlenW(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 */
64  if (strlenW(asctime) >= TIME_STRING_LEN) FIXME("\n");
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 = atoiW(tmpChar+12) - 1900;
80  t.tm_mday = atoiW(tmpChar+5);
81  t.tm_hour = atoiW(tmpChar+17);
82  t.tm_min = atoiW(tmpChar+20);
83  t.tm_sec = atoiW(tmpChar+23);
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 
133 BOOL GetAddress(const WCHAR *name, INTERNET_PORT port, struct sockaddr *psa, int *sa_len, char *addr_str)
134 {
135  ADDRINFOW *res, hints;
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 
147  ret = GetAddrInfoW(name, NULL, &hints, &res);
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;
152  ret = GetAddrInfoW(name, NULL, &hints, &res);
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 
190 static 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 < (sizeof(internet_status) / sizeof(internet_status[0])); i++) {
223  if (internet_status[i].val == dwInternetStatus) return internet_status[i].name;
224  }
225  return "Unknown";
226 }
227 
228 static 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 }
namespace GUID const ADDRINFOEXW * hints
Definition: sock.c:80
#define TRUE
Definition: types.h:120
char hdr[14]
Definition: iptest.cpp:33
WINE_UNICODE_INLINE unsigned int strlenW(const WCHAR *str)
Definition: unicode.h:212
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define INTERNET_STATUS_HANDLE_CLOSING
Definition: wininet.h:896
Definition: http.c:6587
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269
#define INTERNET_STATUS_REQUEST_SENT
Definition: wininet.h:888
#define WARN(fmt,...)
Definition: debug.h:111
#define INTERNET_STATUS_CONNECTING_TO_SERVER
Definition: wininet.h:885
GLdouble GLdouble t
Definition: gl.h:2047
#define INET6_ADDRSTRLEN
Definition: request.c:1491
#define INTERNET_STATUS_PREFETCH
Definition: wininet.h:892
_Check_return_ _CRTIMP char *__cdecl asctime(_In_ const struct tm *_Tm)
static void * heap_alloc(size_t len)
Definition: appwiz.h:65
#define INTERNET_STATUS_CONNECTION_CLOSED
Definition: wininet.h:894
#define lstrcpynW
Definition: compat.h:397
struct _test_info info[]
Definition: SetCursorPos.c:19
const char * wine_dbg_sprintf(const char *format,...)
Definition: compat.c:271
WORD INTERNET_PORT
Definition: winhttp.h:38
#define INET_CALLBACKW
Definition: internet.h:245
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
INT WSAAPI GetAddrInfoW(IN PCWSTR pszNodeName, IN PCWSTR pszServiceName, IN const ADDRINFOW *ptHints, OUT PADDRINFOW *pptResult)
Definition: addrinfo.c:509
unsigned int BOOL
Definition: ntddk_ex.h:94
#define INTERNET_STATUS_CLOSING_CONNECTION
Definition: wininet.h:893
static WCHAR * heap_strdupW(const WCHAR *str)
Definition: propsheet.c:178
#define debugstr_w
Definition: kernel32.h:32
#define FIXME(fmt,...)
Definition: debug.h:110
#define TIME_STRING_LEN
Definition: utility.c:47
smooth NULL
Definition: ftsmooth.c:416
#define AF_INET6
Definition: winsock.h:369
#define INTERNET_STATUS_RECEIVING_RESPONSE
Definition: wininet.h:889
WINE_DEFAULT_DEBUG_CHANNEL(wininet)
GLuint GLfloat * val
Definition: glext.h:7180
#define INTERNET_STATUS_NAME_RESOLVED
Definition: wininet.h:884
#define INTERNET_STATUS_CTL_RESPONSE_RECEIVED
Definition: wininet.h:891
#define TRACE(s)
Definition: solgame.cpp:4
#define INTERNET_STATUS_P3P_POLICYREF
Definition: wininet.h:907
#define INTERNET_STATUS_COOKIE_RECEIVED
Definition: wininet.h:904
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define INTERNET_STATUS_P3P_HEADER
Definition: wininet.h:906
static const char * debugstr_status_info(DWORD status, void *info)
Definition: utility.c:228
static char * heap_strdupWtoA(const WCHAR *str)
unsigned long DWORD
Definition: ntddk_ex.h:95
#define INTERNET_STATUS_REDIRECT
Definition: wininet.h:899
DWORD_PTR dwResult
Definition: wininet.h:155
int ret
GLenum const GLvoid * addr
Definition: glext.h:9621
#define INTERNET_STATUS_SENDING_REQUEST
Definition: wininet.h:887
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
#define INTERNET_STATUS_RESOLVING_NAME
Definition: wininet.h:883
Definition: time.h:76
uint32_t DWORD_PTR
Definition: typedefs.h:63
#define INTERNET_STATUS_COOKIE_HISTORY
Definition: wininet.h:908
const char * name
Definition: internet.h:468
__kernel_time_t time_t
Definition: linux.h:252
#define INTERNET_STATUS_REQUEST_COMPLETE
Definition: wininet.h:898
#define FE(x)
#define INTERNET_STATUS_STATE_CHANGE
Definition: wininet.h:902
_CRTIMP time_t __cdecl mktime(struct tm *_Tm)
Definition: time.h:426
#define INTERNET_STATUS_CONNECTED_TO_SERVER
Definition: wininet.h:886
void INTERNET_SendCallback(object_header_t *hdr, DWORD_PTR context, DWORD status, void *info, DWORD info_len)
Definition: utility.c:240
static SCRIPT_CACHE SCRIPT_ANALYSIS * psa
Definition: usp10.c:64
WINSOCK_API_LINKAGE VOID WSAAPI FreeAddrInfoW(_In_opt_ PADDRINFOW pAddrInfo)
#define INTERNET_STATUS_COOKIE_SENT
Definition: wininet.h:903
Definition: name.c:36
time_t ConvertTimeString(LPCWSTR asctime)
Definition: utility.c:49
GLuint res
Definition: glext.h:9613
#define AF_INET
Definition: tcpip.h:117
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:41
const char *WSAAPI inet_ntop(int af, const void *src, char *dst, size_t cnt)
Definition: unix_func.c:8
#define INTERNET_STATUS_HANDLE_CREATED
Definition: wininet.h:895
#define INTERNET_STATUS_USER_INPUT_REQUIRED
Definition: wininet.h:901
#define htons(x)
Definition: module.h:213
USHORT port
Definition: uri.c:227
#define memset(x, y, z)
Definition: compat.h:39
static const char * get_callback_name(DWORD dwInternetStatus)
Definition: utility.c:190
#define INTERNET_STATUS_INTERMEDIATE_RESPONSE
Definition: wininet.h:900
#define INTERNET_STATUS_RESPONSE_RECEIVED
Definition: wininet.h:890
#define INTERNET_STATUS_PRIVACY_IMPACTED
Definition: wininet.h:905
WINE_UNICODE_INLINE int atoiW(const WCHAR *str)
Definition: unicode.h:315
BOOL GetAddress(const WCHAR *name, INTERNET_PORT port, struct sockaddr *psa, int *sa_len, char *addr_str)
Definition: utility.c:133
static BOOL heap_free(void *mem)
Definition: appwiz.h:75
Definition: ps.c:97