atl80: Added AtlComModuleRegisterServer implementation (based on AtlModuleRegisterSer...
[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;
273     do
274         handle = LOWORD( InterlockedIncrement( &next_handle ));
275     while (!handle); /* avoid handle 0 */
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         HeapFree( GetProcessHeap(), 0, query );
288         return 0;
289     }
290     CloseHandle( thread );
291     return UlongToHandle( handle );
292 }
293
294
295 /***********************************************************************
296  *       WSAAsyncGetHostByAddr        (WS2_32.102)
297  */
298 HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
299                                INT len, INT type, LPSTR sbuf, INT buflen)
300 {
301     struct async_query_gethostbyaddr *aq;
302
303     TRACE("hwnd %p, msg %04x, addr %p[%i]\n", hWnd, uMsg, addr, len );
304
305     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
306     {
307         SetLastError( WSAEWOULDBLOCK );
308         return 0;
309     }
310     aq->host_addr = (char *)(aq + 1);
311     aq->host_len  = len;
312     aq->host_type = type;
313     memcpy( aq->host_addr, addr, len );
314     return run_query( hWnd, uMsg, async_gethostbyaddr, &aq->query, sbuf, buflen );
315 }
316
317 /***********************************************************************
318  *       WSAAsyncGetHostByName  (WS2_32.103)
319  */
320 HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
321                                         LPSTR sbuf, INT buflen)
322 {
323     struct async_query_gethostbyname *aq;
324     unsigned int len = strlen(name) + 1;
325
326     TRACE("hwnd %p, msg %04x, host %s, buffer %i\n", hWnd, uMsg, debugstr_a(name), buflen );
327
328     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
329     {
330         SetLastError( WSAEWOULDBLOCK );
331         return 0;
332     }
333     aq->host_name = (char *)(aq + 1);
334     strcpy( aq->host_name, name );
335     return run_query( hWnd, uMsg, async_gethostbyname, &aq->query, sbuf, buflen );
336 }
337
338 /***********************************************************************
339  *       WSAAsyncGetProtoByName       (WS2_32.105)
340  */
341 HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
342                                          LPSTR sbuf, INT buflen)
343 {
344     struct async_query_getprotobyname *aq;
345     unsigned int len = strlen(name) + 1;
346
347     TRACE("hwnd %p, msg %04x, proto %s, buffer %i\n", hWnd, uMsg, debugstr_a(name), buflen );
348
349     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
350     {
351         SetLastError( WSAEWOULDBLOCK );
352         return 0;
353     }
354     aq->proto_name = (char *)(aq + 1);
355     strcpy( aq->proto_name, name );
356     return run_query( hWnd, uMsg, async_getprotobyname, &aq->query, sbuf, buflen );
357 }
358
359
360 /***********************************************************************
361  *       WSAAsyncGetProtoByNumber     (WS2_32.104)
362  */
363 HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
364                                            LPSTR sbuf, INT buflen)
365 {
366     struct async_query_getprotobynumber *aq;
367
368     TRACE("hwnd %p, msg %04x, num %i\n", hWnd, uMsg, number );
369
370     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) )))
371     {
372         SetLastError( WSAEWOULDBLOCK );
373         return 0;
374     }
375     aq->proto_number = number;
376     return run_query( hWnd, uMsg, async_getprotobynumber, &aq->query, sbuf, buflen );
377 }
378
379 /***********************************************************************
380  *       WSAAsyncGetServByName        (WS2_32.107)
381  */
382 HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
383                                         LPCSTR proto, LPSTR sbuf, INT buflen)
384 {
385     struct async_query_getservbyname *aq;
386     unsigned int len1 = strlen(name) + 1;
387     unsigned int len2 = proto ? strlen(proto) + 1 : 0;
388
389     TRACE("hwnd %p, msg %04x, name %s, proto %s\n", hWnd, uMsg, debugstr_a(name), debugstr_a(proto));
390
391     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len1 + len2 )))
392     {
393         SetLastError( WSAEWOULDBLOCK );
394         return 0;
395     }
396
397     aq->serv_name  = (char *)(aq + 1);
398     strcpy( aq->serv_name, name );
399
400     if (proto)
401     {
402         aq->serv_proto = aq->serv_name + len1;
403         strcpy( aq->serv_proto, proto );
404     }
405     else
406         aq->serv_proto = NULL;
407
408     return run_query( hWnd, uMsg, async_getservbyname, &aq->query, sbuf, buflen );
409 }
410
411 /***********************************************************************
412  *       WSAAsyncGetServByPort        (WS2_32.106)
413  */
414 HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
415                                         LPCSTR proto, LPSTR sbuf, INT buflen)
416 {
417     struct async_query_getservbyport *aq;
418     unsigned int len = proto ? strlen(proto) + 1 : 0;
419
420     TRACE("hwnd %p, msg %04x, port %i, proto %s\n", hWnd, uMsg, port, debugstr_a(proto));
421
422     if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
423     {
424         SetLastError( WSAEWOULDBLOCK );
425         return 0;
426     }
427
428     if (proto)
429     {
430         aq->serv_proto = (char *)(aq + 1);
431         strcpy( aq->serv_proto, proto );
432     }
433     else
434         aq->serv_proto = NULL;
435
436     aq->serv_port = port;
437
438     return run_query( hWnd, uMsg, async_getservbyport, &aq->query, sbuf, buflen );
439 }
440
441 /***********************************************************************
442  *       WSACancelAsyncRequest  (WS2_32.108)
443  */
444 INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
445 {
446     FIXME("(%p),stub\n", hAsyncTaskHandle);
447     return 0;
448 }
449
450 /***********************************************************************
451  *       WSApSetPostRoutine     (WS2_32.24)
452  */
453 INT WINAPI WSApSetPostRoutine(LPWPUPOSTMESSAGE lpPostRoutine)
454 {
455     FIXME("(%p), stub !\n", lpPostRoutine);
456     return 0;
457 }
458
459 /***********************************************************************
460  *        WPUCompleteOverlappedRequest   (WS2_32.25)
461  */
462 WSAEVENT WINAPI WPUCompleteOverlappedRequest(SOCKET s, LPWSAOVERLAPPED overlapped,
463                                              DWORD error, DWORD transferred, LPINT errcode)
464 {
465     FIXME("(0x%08lx,%p,0x%08x,0x%08x,%p), stub !\n", s, overlapped, error, transferred, errcode);
466
467     if (errcode)
468         *errcode = WSAEINVAL;
469
470     return NULL;
471 }