msvcrt: Added _resetstkoflw implementation.
[wine] / dlls / ws2_32 / async.c
1 /* Async WINSOCK DNS services
2  *
3  * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
4  * Copyright (C) 1999 Marcus Meissner
5  * Copyright (C) 2009 Alexandre Julliard
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  *
21  * NOTE: If you make any changes to fix a particular app, make sure
22  * they don't break something else like Netscape or telnet and ftp
23  * clients and servers (www.winsite.com got a lot of those).
24  *
25  * FIXME:
26  *      - Add WSACancel* and correct handle management. (works rather well for
27  *        now without it.)
28  *      - Verify & Check all calls for correctness
29  *        (currently only WSAGetHostByName*, WSAGetServByPort* calls)
30  *      - Check error returns.
31  *      - mirc/mirc32 Finger @linux.kernel.org sometimes fails in threaded mode.
32  *        (not sure why)
33  *      - This implementation did ignore the "NOTE:" section above (since the
34  *        whole stuff did not work anyway to other changes).
35  */
36
37 #include "config.h"
38 #include "wine/port.h"
39
40 #include <stdarg.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "wingdi.h"
44 #include "winuser.h"
45 #include "winsock2.h"
46 #include "ws2spi.h"
47
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
51
52
53 struct async_query_header
54 {
55     HWND   hWnd;
56     UINT   uMsg;
57     void  *sbuf;
58     INT    sbuflen;
59     HANDLE handle;
60 };
61
62 struct async_query_gethostbyname
63 {
64     struct async_query_header query;
65     char *host_name;
66 };
67
68 struct async_query_gethostbyaddr
69 {
70     struct async_query_header query;
71     char *host_addr;
72     int   host_len;
73     int   host_type;
74 };
75
76 struct async_query_getprotobyname
77 {
78     struct async_query_header query;
79     char *proto_name;
80 };
81
82 struct async_query_getprotobynumber
83 {
84     struct async_query_header query;
85     int   proto_number;
86 };
87
88 struct async_query_getservbyname
89 {
90     struct async_query_header query;
91     char *serv_name;
92     char *serv_proto;
93 };
94
95 struct async_query_getservbyport
96 {
97     struct async_query_header query;
98     char *serv_proto;
99     int   serv_port;
100 };
101
102
103 /* ----------------------------------- helper functions - */
104
105 static int list_size(char** l, int item_size)
106 {
107   int i,j = 0;
108   if(l)
109   { for(i=0;l[i];i++)
110         j += (item_size) ? item_size : strlen(l[i]) + 1;
111     j += (i + 1) * sizeof(char*); }
112   return j;
113 }
114
115 static int list_dup(char** l_src, char* ref, int item_size)
116 {
117    char*                p = ref;
118    char**               l_to = (char**)ref;
119    int                  i,j,k;
120
121    for(j=0;l_src[j];j++) ;
122    p += (j + 1) * sizeof(char*);
123    for(i=0;i<j;i++)
124    { l_to[i] = p;
125      k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
126      memcpy(p, l_src[i], k); p += k; }
127    l_to[i] = NULL;
128    return (p - ref);
129 }
130
131 static DWORD finish_query( struct async_query_header *query, LPARAM lparam )
132 {
133     PostMessageW( query->hWnd, query->uMsg, (WPARAM)query->handle, lparam );
134     HeapFree( GetProcessHeap(), 0, query );
135     return 0;
136 }
137
138 /* ----- hostent */
139
140 static LPARAM copy_he(void *base, int size, const struct WS_hostent *he)
141 {
142     char *p;
143     int needed;
144     struct WS_hostent *to = base;
145
146     if (!he) return MAKELPARAM( 0, GetLastError() );
147
148     needed = sizeof(struct WS_hostent) + strlen(he->h_name) + 1 +
149                  list_size(he->h_aliases, 0) +
150                  list_size(he->h_addr_list, he->h_length );
151     if (size < needed) return MAKELPARAM( needed, WSAENOBUFS );
152
153     to->h_addrtype = he->h_addrtype;
154     to->h_length = he->h_length;
155     p = (char *)(to + 1);
156     to->h_name = p;
157     strcpy(p, he->h_name); p += strlen(p) + 1;
158     to->h_aliases = (char **)p;
159     p += list_dup(he->h_aliases, p, 0);
160     to->h_addr_list = (char **)p;
161     list_dup(he->h_addr_list, p, he->h_length);
162     return MAKELPARAM( needed, 0 );
163 }
164
165 static DWORD WINAPI async_gethostbyname(LPVOID arg)
166 {
167     struct async_query_gethostbyname *aq = arg;
168     struct WS_hostent *he = WS_gethostbyname( aq->host_name );
169
170     return finish_query( &aq->query, copy_he( aq->query.sbuf, aq->query.sbuflen, he ));
171 }
172
173 static DWORD WINAPI async_gethostbyaddr(LPVOID arg)
174 {
175     struct async_query_gethostbyaddr *aq = arg;
176     struct WS_hostent *he = WS_gethostbyaddr( aq->host_addr, aq->host_len, aq->host_type );
177
178     return finish_query( &aq->query, copy_he( aq->query.sbuf, aq->query.sbuflen, he ));
179 }
180
181 /* ----- protoent */
182
183 static LPARAM copy_pe(void *base, int size, const struct WS_protoent* pe)
184 {
185     char *p;
186     int needed;
187     struct WS_protoent *to = base;
188
189     if (!pe) return MAKELPARAM( 0, GetLastError() );
190
191     needed = sizeof(struct WS_protoent) + strlen(pe->p_name) + 1 + list_size(pe->p_aliases, 0);
192     if (size < needed) return MAKELPARAM( needed, WSAENOBUFS );
193
194     to->p_proto = pe->p_proto;
195     p = (char *)(to + 1);
196     to->p_name = p;
197     strcpy(p, pe->p_name); p += strlen(p) + 1;
198     to->p_aliases = (char **)p;
199     list_dup(pe->p_aliases, p, 0);
200     return MAKELPARAM( needed, 0 );
201 }
202
203 static DWORD WINAPI async_getprotobyname(LPVOID arg)
204 {
205     struct async_query_getprotobyname *aq = arg;
206     struct WS_protoent *pe = WS_getprotobyname( aq->proto_name );
207
208     return finish_query( &aq->query, copy_pe( aq->query.sbuf, aq->query.sbuflen, pe ));
209 }
210
211 static DWORD WINAPI async_getprotobynumber(LPVOID arg)
212 {
213     struct async_query_getprotobynumber *aq = arg;
214     struct WS_protoent *pe = WS_getprotobynumber( aq->proto_number );
215
216     return finish_query( &aq->query, copy_pe( aq->query.sbuf, aq->query.sbuflen, pe ));
217 }
218
219 /* ----- servent */
220
221 static LPARAM copy_se(void *base, int size, const struct WS_servent* se)
222 {
223     char *p;
224     int needed;
225     struct WS_servent *to = base;
226
227     if (!se) return MAKELPARAM( 0, GetLastError() );
228
229     needed = sizeof(struct WS_servent) + strlen(se->s_proto) + strlen(se->s_name) + 2 + list_size(se->s_aliases, 0);
230     if (size < needed) return MAKELPARAM( needed, WSAENOBUFS );
231
232     to->s_port = se->s_port;
233     p = (char *)(to + 1);
234     to->s_name = p;
235     strcpy(p, se->s_name); p += strlen(p) + 1;
236     to->s_proto = p;
237     strcpy(p, se->s_proto); p += strlen(p) + 1;
238     to->s_aliases = (char **)p;
239     list_dup(se->s_aliases, p, 0);
240     return MAKELPARAM( needed, 0 );
241 }
242
243 static DWORD WINAPI async_getservbyname(LPVOID arg)
244 {
245     struct async_query_getservbyname *aq = arg;
246     struct WS_servent *se = WS_getservbyname( aq->serv_name, aq->serv_proto );
247
248     return finish_query( &aq->query, copy_se( aq->query.sbuf, aq->query.sbuflen, se ));
249 }
250
251 static DWORD WINAPI async_getservbyport(LPVOID arg)
252 {
253     struct async_query_getservbyport *aq = arg;
254     struct WS_servent *se = WS_getservbyport( aq->serv_port, aq->serv_proto );
255
256     return finish_query( &aq->query, copy_se( aq->query.sbuf, aq->query.sbuflen, se ));
257 }
258
259
260 /****************************************************************************
261  * The main async help function.
262  *
263  * It either starts a thread or just calls the function directly for platforms
264  * with no thread support. This relies on the fact that PostMessage() does
265  * not actually call the windowproc before the function returns.
266  */
267 static HANDLE run_query( HWND hWnd, UINT uMsg, LPTHREAD_START_ROUTINE func,
268                          struct async_query_header *query, void *sbuf, INT sbuflen )
269 {
270     static LONG next_handle = 0xdead;
271     HANDLE thread;
272     ULONG handle = LOWORD( InterlockedIncrement( &next_handle ));
273
274     /* avoid handle 0 */
275     while (!handle) handle = LOWORD( InterlockedIncrement( &next_handle ));
276
277     query->hWnd    = hWnd;
278     query->uMsg    = uMsg;
279     query->handle  = UlongToHandle( handle );
280     query->sbuf    = sbuf;
281     query->sbuflen = sbuflen;
282
283     thread = CreateThread( NULL, 0, func, query, 0, NULL );
284     if (!thread)
285     {
286         SetLastError( WSAEWOULDBLOCK );
287         return 0;
288     }
289     CloseHandle( thread );
290     return UlongToHandle( handle );
291 }
292
293
294 /***********************************************************************
295  *       WSAAsyncGetHostByAddr        (WS2_32.102)
296  */
297 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
298                                INT len, INT type, LPSTR sbuf, INT buflen)
299 {
300     struct async_query_gethostbyaddr *aq;
301
302     TRACE("hwnd %p, msg %04x, addr %p[%i]\n", hWnd, uMsg, addr, len );
303
304     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
305     {
306         SetLastError( WSAEWOULDBLOCK );
307         return 0;
308     }
309     aq->host_addr = (char *)(aq + 1);
310     aq->host_len  = len;
311     aq->host_type = type;
312     memcpy( aq->host_addr, addr, len );
313     return run_query( hWnd, uMsg, async_gethostbyaddr, &aq->query, sbuf, buflen );
314 }
315
316 /***********************************************************************
317  *       WSAAsyncGetHostByName  (WS2_32.103)
318  */
319 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
320                                         LPSTR sbuf, INT buflen)
321 {
322     struct async_query_gethostbyname *aq;
323     unsigned int len = strlen(name) + 1;
324
325     TRACE("hwnd %p, msg %04x, host %s, buffer %i\n", hWnd, uMsg, debugstr_a(name), buflen );
326
327     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
328     {
329         SetLastError( WSAEWOULDBLOCK );
330         return 0;
331     }
332     aq->host_name = (char *)(aq + 1);
333     strcpy( aq->host_name, name );
334     return run_query( hWnd, uMsg, async_gethostbyname, &aq->query, sbuf, buflen );
335 }
336
337 /***********************************************************************
338  *       WSAAsyncGetProtoByName       (WS2_32.105)
339  */
340 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
341                                          LPSTR sbuf, INT buflen)
342 {
343     struct async_query_getprotobyname *aq;
344     unsigned int len = strlen(name) + 1;
345
346     TRACE("hwnd %p, msg %04x, proto %s, buffer %i\n", hWnd, uMsg, debugstr_a(name), buflen );
347
348     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
349     {
350         SetLastError( WSAEWOULDBLOCK );
351         return 0;
352     }
353     aq->proto_name = (char *)(aq + 1);
354     strcpy( aq->proto_name, name );
355     return run_query( hWnd, uMsg, async_getprotobyname, &aq->query, sbuf, buflen );
356 }
357
358
359 /***********************************************************************
360  *       WSAAsyncGetProtoByNumber     (WS2_32.104)
361  */
362 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
363                                            LPSTR sbuf, INT buflen)
364 {
365     struct async_query_getprotobynumber *aq;
366
367     TRACE("hwnd %p, msg %04x, num %i\n", hWnd, uMsg, number );
368
369     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) )))
370     {
371         SetLastError( WSAEWOULDBLOCK );
372         return 0;
373     }
374     aq->proto_number = number;
375     return run_query( hWnd, uMsg, async_getprotobynumber, &aq->query, sbuf, buflen );
376 }
377
378 /***********************************************************************
379  *       WSAAsyncGetServByName        (WS2_32.107)
380  */
381 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
382                                         LPCSTR proto, LPSTR sbuf, INT buflen)
383 {
384     struct async_query_getservbyname *aq;
385     unsigned int len1 = strlen(name) + 1;
386     unsigned int len2 = strlen(proto) + 1;
387
388     TRACE("hwnd %p, msg %04x, name %s, proto %s\n", hWnd, uMsg, debugstr_a(name), debugstr_a(proto));
389
390     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len1 + len2 )))
391     {
392         SetLastError( WSAEWOULDBLOCK );
393         return 0;
394     }
395     aq->serv_name  = (char *)(aq + 1);
396     aq->serv_proto = aq->serv_name + len1;
397     strcpy( aq->serv_name, name );
398     strcpy( aq->serv_proto, proto );
399     return run_query( hWnd, uMsg, async_getservbyname, &aq->query, sbuf, buflen );
400 }
401
402 /***********************************************************************
403  *       WSAAsyncGetServByPort        (WS2_32.106)
404  */
405 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
406                                         LPCSTR proto, LPSTR sbuf, INT buflen)
407 {
408     struct async_query_getservbyport *aq;
409     unsigned int len = strlen(proto) + 1;
410
411     TRACE("hwnd %p, msg %04x, port %i, proto %s\n", hWnd, uMsg, port, debugstr_a(proto));
412
413     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
414     {
415         SetLastError( WSAEWOULDBLOCK );
416         return 0;
417     }
418     aq->serv_proto = (char *)(aq + 1);
419     aq->serv_port  = port;
420     strcpy( aq->serv_proto, proto );
421     return run_query( hWnd, uMsg, async_getservbyport, &aq->query, sbuf, buflen );
422 }
423
424 /***********************************************************************
425  *       WSACancelAsyncRequest  (WS2_32.108)
426  */
427 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
428 {
429     FIXME("(%p),stub\n", hAsyncTaskHandle);
430     return 0;
431 }
432
433 /***********************************************************************
434  *       WSApSetPostRoutine     (WS2_32.24)
435  */
436 INT WINAPI WSApSetPostRoutine(LPWPUPOSTMESSAGE lpPostRoutine)
437 {
438     FIXME("(%p), stub !\n", lpPostRoutine);
439     return 0;
440 }
441
442 /***********************************************************************
443  *        WPUCompleteOverlappedRequest   (WS2_32.25)
444  */
445 WSAEVENT WINAPI WPUCompleteOverlappedRequest(SOCKET s, LPWSAOVERLAPPED overlapped,
446                                              DWORD error, DWORD transferred, LPINT errcode)
447 {
448     FIXME("(0x%08lx,%p,0x%08x,0x%08x,%p), stub !\n", s, overlapped, error, transferred, errcode);
449
450     if (errcode)
451         *errcode = WSAEINVAL;
452
453     return NULL;
454 }