async.c 14.1 KB
Newer Older
1
/* Async WINSOCK DNS services
2
 *
3 4
 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka.
 * Copyright (C) 1999 Marcus Meissner
5
 * Copyright (C) 2009 Alexandre Julliard
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * 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
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23
 *
 * NOTE: If you make any changes to fix a particular app, make sure
 * they don't break something else like Netscape or telnet and ftp
 * clients and servers (www.winsite.com got a lot of those).
24
 *
25 26 27 28 29 30 31 32 33 34 35
 * FIXME:
 *	- Add WSACancel* and correct handle management. (works rather well for
 *	  now without it.)
 *	- Verify & Check all calls for correctness
 *	  (currently only WSAGetHostByName*, WSAGetServByPort* calls)
 *	- Check error returns.
 *	- mirc/mirc32 Finger @linux.kernel.org sometimes fails in threaded mode.
 *	  (not sure why)
 *	- This implementation did ignore the "NOTE:" section above (since the
 *	  whole stuff did not work anyway to other changes).
 */
36

37
#include "config.h"
38
#include "wine/port.h"
39

40
#include <stdarg.h>
41 42
#include "windef.h"
#include "winbase.h"
43 44
#include "wingdi.h"
#include "winuser.h"
45
#include "winsock2.h"
46 47
#include "ws2spi.h"

48
#include "wine/debug.h"
49

50
WINE_DEFAULT_DEBUG_CHANNEL(winsock);
51

52

53 54
struct async_query_header
{
55
    LPARAM (*func)( struct async_query_header *query );
56 57 58 59 60 61 62
    HWND   hWnd;
    UINT   uMsg;
    void  *sbuf;
    INT    sbuflen;
    HANDLE handle;
};

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
struct async_query_gethostbyname
{
    struct async_query_header query;
    char *host_name;
};

struct async_query_gethostbyaddr
{
    struct async_query_header query;
    char *host_addr;
    int   host_len;
    int   host_type;
};

struct async_query_getprotobyname
{
79
    struct async_query_header query;
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
    char *proto_name;
};

struct async_query_getprotobynumber
{
    struct async_query_header query;
    int   proto_number;
};

struct async_query_getservbyname
{
    struct async_query_header query;
    char *serv_name;
    char *serv_proto;
};

struct async_query_getservbyport
{
    struct async_query_header query;
    char *serv_proto;
    int   serv_port;
};

103

104 105 106 107 108 109
/* ----------------------------------- helper functions - */

static int list_size(char** l, int item_size)
{
  int i,j = 0;
  if(l)
110
  { for(i=0;l[i];i++)
111 112 113 114 115
	j += (item_size) ? item_size : strlen(l[i]) + 1;
    j += (i + 1) * sizeof(char*); }
  return j;
}

116
static int list_dup(char** l_src, char* ref, int item_size)
117
{
118 119 120 121 122 123 124
   char*		p = ref;
   char**		l_to = (char**)ref;
   int			i,j,k;

   for(j=0;l_src[j];j++) ;
   p += (j + 1) * sizeof(char*);
   for(i=0;i<j;i++)
125
   { l_to[i] = p;
126 127 128 129 130 131 132 133
     k = ( item_size ) ? item_size : strlen(l_src[i]) + 1;
     memcpy(p, l_src[i], k); p += k; }
   l_to[i] = NULL;
   return (p - ref);
}

/* ----- hostent */

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
static LPARAM copy_he(void *base, int size, const struct WS_hostent *he)
{
    char *p;
    int needed;
    struct WS_hostent *to = base;

    if (!he) return MAKELPARAM( 0, GetLastError() );

    needed = sizeof(struct WS_hostent) + strlen(he->h_name) + 1 +
                 list_size(he->h_aliases, 0) +
                 list_size(he->h_addr_list, he->h_length );
    if (size < needed) return MAKELPARAM( needed, WSAENOBUFS );

    to->h_addrtype = he->h_addrtype;
    to->h_length = he->h_length;
    p = (char *)(to + 1);
    to->h_name = p;
    strcpy(p, he->h_name); p += strlen(p) + 1;
    to->h_aliases = (char **)p;
    p += list_dup(he->h_aliases, p, 0);
    to->h_addr_list = (char **)p;
    list_dup(he->h_addr_list, p, he->h_length);
    return MAKELPARAM( needed, 0 );
157 158
}

159
static LPARAM async_gethostbyname( struct async_query_header *query )
160
{
161
    struct async_query_gethostbyname *aq = CONTAINING_RECORD( query, struct async_query_gethostbyname, query );
162
    struct WS_hostent *he = WS_gethostbyname( aq->host_name );
163

164
    return copy_he( query->sbuf, query->sbuflen, he );
165 166
}

167
static LPARAM async_gethostbyaddr( struct async_query_header *query )
168
{
169
    struct async_query_gethostbyaddr *aq = CONTAINING_RECORD( query, struct async_query_gethostbyaddr, query );
170 171
    struct WS_hostent *he = WS_gethostbyaddr( aq->host_addr, aq->host_len, aq->host_type );

172
    return copy_he( query->sbuf, query->sbuflen, he );
173 174
}

175 176
/* ----- protoent */

177
static LPARAM copy_pe(void *base, int size, const struct WS_protoent* pe)
178
{
179 180 181
    char *p;
    int needed;
    struct WS_protoent *to = base;
182

183 184 185 186 187 188 189 190 191 192 193 194
    if (!pe) return MAKELPARAM( 0, GetLastError() );

    needed = sizeof(struct WS_protoent) + strlen(pe->p_name) + 1 + list_size(pe->p_aliases, 0);
    if (size < needed) return MAKELPARAM( needed, WSAENOBUFS );

    to->p_proto = pe->p_proto;
    p = (char *)(to + 1);
    to->p_name = p;
    strcpy(p, pe->p_name); p += strlen(p) + 1;
    to->p_aliases = (char **)p;
    list_dup(pe->p_aliases, p, 0);
    return MAKELPARAM( needed, 0 );
195 196
}

197
static LPARAM async_getprotobyname( struct async_query_header *query )
198
{
199
    struct async_query_getprotobyname *aq = CONTAINING_RECORD( query, struct async_query_getprotobyname, query );
200
    struct WS_protoent *pe = WS_getprotobyname( aq->proto_name );
201

202
    return copy_pe( query->sbuf, query->sbuflen, pe );
203 204
}

205
static LPARAM async_getprotobynumber( struct async_query_header *query )
206
{
207
    struct async_query_getprotobynumber *aq = CONTAINING_RECORD( query, struct async_query_getprotobynumber, query );
208
    struct WS_protoent *pe = WS_getprotobynumber( aq->proto_number );
209

210
    return copy_pe( query->sbuf, query->sbuflen, pe );
211 212
}

213 214
/* ----- servent */

215
static LPARAM copy_se(void *base, int size, const struct WS_servent* se)
216
{
217 218 219
    char *p;
    int needed;
    struct WS_servent *to = base;
220

221 222 223 224 225 226 227 228 229 230 231 232 233 234
    if (!se) return MAKELPARAM( 0, GetLastError() );

    needed = sizeof(struct WS_servent) + strlen(se->s_proto) + strlen(se->s_name) + 2 + list_size(se->s_aliases, 0);
    if (size < needed) return MAKELPARAM( needed, WSAENOBUFS );

    to->s_port = se->s_port;
    p = (char *)(to + 1);
    to->s_name = p;
    strcpy(p, se->s_name); p += strlen(p) + 1;
    to->s_proto = p;
    strcpy(p, se->s_proto); p += strlen(p) + 1;
    to->s_aliases = (char **)p;
    list_dup(se->s_aliases, p, 0);
    return MAKELPARAM( needed, 0 );
235 236
}

237
static LPARAM async_getservbyname( struct async_query_header *query )
238
{
239
    struct async_query_getservbyname *aq = CONTAINING_RECORD( query, struct async_query_getservbyname, query );
240
    struct WS_servent *se = WS_getservbyname( aq->serv_name, aq->serv_proto );
241

242
    return copy_se( query->sbuf, query->sbuflen, se );
243
}
244

245
static LPARAM async_getservbyport( struct async_query_header *query )
246
{
247
    struct async_query_getservbyport *aq = CONTAINING_RECORD( query, struct async_query_getservbyport, query );
248
    struct WS_servent *se = WS_getservbyport( aq->serv_port, aq->serv_proto );
249

250 251 252 253 254 255 256 257 258 259
    return copy_se( query->sbuf, query->sbuflen, se );
}


static void WINAPI async_worker( TP_CALLBACK_INSTANCE *instance, void *context )
{
    struct async_query_header *query = context;
    LPARAM lparam = query->func( query );
    PostMessageW( query->hWnd, query->uMsg, (WPARAM)query->handle, lparam );
    HeapFree( GetProcessHeap(), 0, query );
260 261
}

262

263
/****************************************************************************
264 265
 * The main async help function.
 *
266 267 268 269
 * It either starts a thread or just calls the function directly for platforms
 * with no thread support. This relies on the fact that PostMessage() does
 * not actually call the windowproc before the function returns.
 */
270
static HANDLE run_query( HWND hWnd, UINT uMsg, LPARAM (*func)(struct async_query_header *),
271
                         struct async_query_header *query, void *sbuf, INT sbuflen )
272 273
{
    static LONG next_handle = 0xdead;
274 275 276 277
    ULONG handle;
    do
        handle = LOWORD( InterlockedIncrement( &next_handle ));
    while (!handle); /* avoid handle 0 */
278

279
    query->func    = func;
280 281 282 283 284 285
    query->hWnd    = hWnd;
    query->uMsg    = uMsg;
    query->handle  = UlongToHandle( handle );
    query->sbuf    = sbuf;
    query->sbuflen = sbuflen;

286
    if (!TrySubmitThreadpoolCallback( async_worker, query, NULL ))
287 288
    {
        SetLastError( WSAEWOULDBLOCK );
289
        HeapFree( GetProcessHeap(), 0, query );
290 291 292
        return 0;
    }
    return UlongToHandle( handle );
293 294 295 296
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
297
 *       WSAAsyncGetHostByAddr        (WS2_32.102)
298 299 300 301
 */
HANDLE WINAPI WSAAsyncGetHostByAddr(HWND hWnd, UINT uMsg, LPCSTR addr,
                               INT len, INT type, LPSTR sbuf, INT buflen)
{
302 303 304 305 306 307 308 309 310 311 312 313 314
    struct async_query_gethostbyaddr *aq;

    TRACE("hwnd %p, msg %04x, addr %p[%i]\n", hWnd, uMsg, addr, len );

    if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
    {
        SetLastError( WSAEWOULDBLOCK );
        return 0;
    }
    aq->host_addr = (char *)(aq + 1);
    aq->host_len  = len;
    aq->host_type = type;
    memcpy( aq->host_addr, addr, len );
315
    return run_query( hWnd, uMsg, async_gethostbyaddr, &aq->query, sbuf, buflen );
316 317 318
}

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
319
 *       WSAAsyncGetHostByName	(WS2_32.103)
320
 */
321
HANDLE WINAPI WSAAsyncGetHostByName(HWND hWnd, UINT uMsg, LPCSTR name,
322 323
					LPSTR sbuf, INT buflen)
{
324 325 326 327 328 329 330 331 332 333 334 335
    struct async_query_gethostbyname *aq;
    unsigned int len = strlen(name) + 1;

    TRACE("hwnd %p, msg %04x, host %s, buffer %i\n", hWnd, uMsg, debugstr_a(name), buflen );

    if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
    {
        SetLastError( WSAEWOULDBLOCK );
        return 0;
    }
    aq->host_name = (char *)(aq + 1);
    strcpy( aq->host_name, name );
336
    return run_query( hWnd, uMsg, async_gethostbyname, &aq->query, sbuf, buflen );
337 338 339
}

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
340
 *       WSAAsyncGetProtoByName       (WS2_32.105)
341 342 343 344
 */
HANDLE WINAPI WSAAsyncGetProtoByName(HWND hWnd, UINT uMsg, LPCSTR name,
                                         LPSTR sbuf, INT buflen)
{
345 346 347 348 349 350 351 352 353 354 355 356
    struct async_query_getprotobyname *aq;
    unsigned int len = strlen(name) + 1;

    TRACE("hwnd %p, msg %04x, proto %s, buffer %i\n", hWnd, uMsg, debugstr_a(name), buflen );

    if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
    {
        SetLastError( WSAEWOULDBLOCK );
        return 0;
    }
    aq->proto_name = (char *)(aq + 1);
    strcpy( aq->proto_name, name );
357
    return run_query( hWnd, uMsg, async_getprotobyname, &aq->query, sbuf, buflen );
358 359 360 361
}


/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
362
 *       WSAAsyncGetProtoByNumber     (WS2_32.104)
363 364 365 366
 */
HANDLE WINAPI WSAAsyncGetProtoByNumber(HWND hWnd, UINT uMsg, INT number,
                                           LPSTR sbuf, INT buflen)
{
367 368 369 370 371 372 373 374 375 376
    struct async_query_getprotobynumber *aq;

    TRACE("hwnd %p, msg %04x, num %i\n", hWnd, uMsg, number );

    if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) )))
    {
        SetLastError( WSAEWOULDBLOCK );
        return 0;
    }
    aq->proto_number = number;
377
    return run_query( hWnd, uMsg, async_getprotobynumber, &aq->query, sbuf, buflen );
378 379 380
}

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
381
 *       WSAAsyncGetServByName        (WS2_32.107)
382 383 384 385
 */
HANDLE WINAPI WSAAsyncGetServByName(HWND hWnd, UINT uMsg, LPCSTR name,
                                        LPCSTR proto, LPSTR sbuf, INT buflen)
{
386 387
    struct async_query_getservbyname *aq;
    unsigned int len1 = strlen(name) + 1;
388
    unsigned int len2 = proto ? strlen(proto) + 1 : 0;
389 390 391 392 393 394 395 396

    TRACE("hwnd %p, msg %04x, name %s, proto %s\n", hWnd, uMsg, debugstr_a(name), debugstr_a(proto));

    if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len1 + len2 )))
    {
        SetLastError( WSAEWOULDBLOCK );
        return 0;
    }
397

398 399
    aq->serv_name  = (char *)(aq + 1);
    strcpy( aq->serv_name, name );
400 401 402 403 404 405 406 407 408

    if (proto)
    {
        aq->serv_proto = aq->serv_name + len1;
        strcpy( aq->serv_proto, proto );
    }
    else
        aq->serv_proto = NULL;

409
    return run_query( hWnd, uMsg, async_getservbyname, &aq->query, sbuf, buflen );
410 411 412
}

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
413
 *       WSAAsyncGetServByPort        (WS2_32.106)
414 415 416 417
 */
HANDLE WINAPI WSAAsyncGetServByPort(HWND hWnd, UINT uMsg, INT port,
                                        LPCSTR proto, LPSTR sbuf, INT buflen)
{
418
    struct async_query_getservbyport *aq;
419
    unsigned int len = proto ? strlen(proto) + 1 : 0;
420 421 422 423 424 425 426 427

    TRACE("hwnd %p, msg %04x, port %i, proto %s\n", hWnd, uMsg, port, debugstr_a(proto));

    if (!(aq = HeapAlloc( GetProcessHeap(), 0, sizeof(*aq) + len )))
    {
        SetLastError( WSAEWOULDBLOCK );
        return 0;
    }
428 429 430 431 432 433 434 435 436 437 438

    if (proto)
    {
        aq->serv_proto = (char *)(aq + 1);
        strcpy( aq->serv_proto, proto );
    }
    else
        aq->serv_proto = NULL;

    aq->serv_port = port;

439
    return run_query( hWnd, uMsg, async_getservbyport, &aq->query, sbuf, buflen );
440 441 442
}

/***********************************************************************
Patrik Stridvall's avatar
Patrik Stridvall committed
443
 *       WSACancelAsyncRequest	(WS2_32.108)
444 445 446
 */
INT WINAPI WSACancelAsyncRequest(HANDLE hAsyncTaskHandle)
{
447
    FIXME("(%p),stub\n", hAsyncTaskHandle);
448 449 450
    return 0;
}

451 452 453
/***********************************************************************
 *       WSApSetPostRoutine	(WS2_32.24)
 */
454
INT WINAPI WSApSetPostRoutine(LPWPUPOSTMESSAGE lpPostRoutine)
455 456 457 458
{
    FIXME("(%p), stub !\n", lpPostRoutine);
    return 0;
}
459 460

/***********************************************************************
461
 *        WPUCompleteOverlappedRequest   (WS2_32.25)
462 463
 */
WSAEVENT WINAPI WPUCompleteOverlappedRequest(SOCKET s, LPWSAOVERLAPPED overlapped,
464
                                             DWORD error, DWORD transferred, LPINT errcode)
465
{
466
    FIXME("(0x%08lx,%p,0x%08x,0x%08x,%p), stub !\n", s, overlapped, error, transferred, errcode);
467

468 469
    if (errcode)
        *errcode = WSAEINVAL;
470 471 472

    return NULL;
}