socket.c 28.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*
 * 16-bit socket functions
 *
 * Copyright (C) 1993,1994,1996,1997 John Brezak, Erik Bos, Alex Korobka
 * Copyright (C) 2003 Alexandre Julliard
 *
 * 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 24 25
 */

#include "config.h"

#include "winsock2.h"
#include "wine/winbase16.h"
26
#include "winsock16.h"
27
#include "wownt32.h"
28
#include "winuser.h"
29 30 31 32
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(winsock);

33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
struct async_query_header
{
    HWND     hWnd;
    UINT     uMsg;
    SEGPTR   sbuf;
    INT      sbuflen;
    HANDLE16 handle;
};

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
{
    struct async_query_header query;
    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;
};

82 83 84 85 86 87 88 89 90 91 92
static INT num_startup;  /* reference counter */
static void *he_buffer;
static SEGPTR he_buffer_seg;
static void *se_buffer;
static SEGPTR se_buffer_seg;
static void *pe_buffer;
static SEGPTR pe_buffer_seg;
static SEGPTR dbuffer_seg;

extern int WINAPI WS_gethostname(char *name, int namelen);

93
static fd_set *ws_fdset_16_to_32( const ws_fd_set16 *set16, fd_set *set32 )
94 95 96 97 98 99 100
{
    UINT i;
    set32->fd_count = set16->fd_count;
    for (i = 0; i < set32->fd_count; i++) set32->fd_array[i] = set16->fd_array[i];
    return set32;
}

101
static ws_fd_set16 *ws_fdset_32_to_16( const fd_set *set32, ws_fd_set16 *set16 )
102 103 104 105 106 107 108
{
    UINT i;
    set16->fd_count = set32->fd_count;
    for (i = 0; i < set16->fd_count; i++) set16->fd_array[i] = set32->fd_array[i];
    return set16;
}

109 110 111 112 113 114 115
static DWORD finish_query( struct async_query_header *query, LPARAM lparam )
{
    PostMessageW( query->hWnd, query->uMsg, (WPARAM)query->handle, lparam );
    HeapFree( GetProcessHeap(), 0, query );
    return 0;
}

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
static int list_size(char** l, int item_size)
{
    int i,j = 0;
    if(l)
    {
        for(i=0;l[i];i++)
            j += (item_size) ? item_size : strlen(l[i]) + 1;
        j += (i + 1) * sizeof(char*);
    }
    return j;
}

static int list_dup(char** l_src, SEGPTR base, int item_size)
{
    int i, offset;
    char *ref = MapSL(base);
    SEGPTR *l_to = (SEGPTR *)ref;

    for (i = 0; l_src[i]; i++) ;
    offset = (i + 1) * sizeof(char*);
    for (i = 0; l_src[i]; i++)
    {
        int count = item_size ? item_size : strlen(l_src[i]) + 1;
        memcpy( ref + offset, l_src[i], count );
        l_to[i] = base + offset;
        offset += count;
    }
    l_to[i] = 0;
    return offset;
}

static SEGPTR get_buffer_he(int size)
{
    static int he_len;
    if (he_buffer)
    {
        if (he_len >= size ) return he_buffer_seg;
        UnMapLS( he_buffer_seg );
        HeapFree( GetProcessHeap(), 0, he_buffer );
    }
    he_buffer = HeapAlloc( GetProcessHeap(), 0, (he_len = size) );
    he_buffer_seg = MapLS( he_buffer );
    return he_buffer_seg;
}

static SEGPTR get_buffer_se(int size)
{
    static int se_len;
    if (se_buffer)
    {
        if (se_len >= size ) return se_buffer_seg;
        UnMapLS( se_buffer_seg );
        HeapFree( GetProcessHeap(), 0, se_buffer );
    }
    se_buffer = HeapAlloc( GetProcessHeap(), 0, (se_len = size) );
    se_buffer_seg = MapLS( se_buffer );
    return se_buffer_seg;
}

static SEGPTR get_buffer_pe(int size)
{
    static int pe_len;
    if (pe_buffer)
    {
        if (pe_len >= size ) return pe_buffer_seg;
        UnMapLS( pe_buffer_seg );
        HeapFree( GetProcessHeap(), 0, pe_buffer );
    }
    pe_buffer = HeapAlloc( GetProcessHeap(), 0, (pe_len = size) );
185
    pe_buffer_seg = MapLS( pe_buffer );
186 187 188 189 190
    return pe_buffer_seg;
}

/* duplicate hostent entry
 * and handle all Win16/Win32 dependent things (struct size, ...) *correctly*.
Austin English's avatar
Austin English committed
191
 * Ditto for protoent and servent.
192
 */
193
static SEGPTR ws_hostent_32_to_16( const struct hostent* he, SEGPTR base, int *buff_size )
194 195 196 197
{
    char *p;
    struct ws_hostent16 *p_to;

198
    int size = (sizeof(*p_to) +
199 200 201 202
                strlen(he->h_name) + 1 +
                list_size(he->h_aliases, 0) +
                list_size(he->h_addr_list, he->h_length));

203 204 205 206 207 208 209 210 211 212
    if (buff_size)
    {
        if (*buff_size < size)
        {
            *buff_size = size;
            return 0;
        }
        *buff_size = size;
    }
    else base = get_buffer_he(size);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    p_to = MapSL(base);

    p_to->h_addrtype = he->h_addrtype;
    p_to->h_length = he->h_length;

    p = (char *)(p_to + 1);
    p_to->h_name = base + (p - (char *)p_to);
    strcpy(p, he->h_name);
    p += strlen(p) + 1;

    p_to->h_aliases = base + (p - (char *)p_to);
    p += list_dup(he->h_aliases, p_to->h_aliases, 0);

    p_to->h_addr_list = base + (p - (char *)p_to);
    list_dup(he->h_addr_list, p_to->h_addr_list, he->h_length);

    return base;
}

232
static SEGPTR ws_protoent_32_to_16( const struct protoent *pe, SEGPTR base, int *buff_size )
233 234 235 236
{
    char *p;
    struct ws_protoent16 *p_to;

237
    int size = (sizeof(*p_to) +
238 239 240
                strlen(pe->p_name) + 1 +
                list_size(pe->p_aliases, 0));

241 242 243 244 245 246 247 248 249 250
    if (buff_size)
    {
        if (*buff_size < size)
        {
            *buff_size = size;
            return 0;
        }
        *buff_size = size;
    }
    else base = get_buffer_pe(size);
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
    p_to = MapSL(base);

    p_to->p_proto = pe->p_proto;
    p = (char *)(p_to + 1);

    p_to->p_name = base + (p - (char *)p_to);
    strcpy(p, pe->p_name);
    p += strlen(p) + 1;

    p_to->p_aliases = base + (p - (char *)p_to);
    list_dup(pe->p_aliases, p_to->p_aliases, 0);

    return base;
}

266
static SEGPTR ws_servent_32_to_16( const struct servent *se, SEGPTR base, int *buff_size )
267 268 269 270
{
    char *p;
    struct ws_servent16 *p_to;

271
    int size = (sizeof(*p_to) +
272 273 274 275
                strlen(se->s_proto) + 1 +
                strlen(se->s_name) + 1 +
                list_size(se->s_aliases, 0));

276 277 278 279 280 281 282 283 284 285
    if (buff_size)
    {
        if (*buff_size < size)
        {
            *buff_size = size;
            return 0;
        }
        *buff_size = size;
    }
    else base = get_buffer_se(size);
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
    p_to = MapSL(base);

    p_to->s_port = se->s_port;
    p = (char *)(p_to + 1);

    p_to->s_name = base + (p - (char *)p_to);
    strcpy(p, se->s_name);
    p += strlen(p) + 1;

    p_to->s_proto = base + (p - (char *)p_to);
    strcpy(p, se->s_proto);
    p += strlen(p) + 1;

    p_to->s_aliases = base + (p - (char *)p_to);
    list_dup(se->s_aliases, p_to->s_aliases, 0);

    return base;
}

305 306 307 308 309
static DWORD WINAPI async_gethostbyname(LPVOID arg)
{
    struct async_query_gethostbyname *aq = arg;
    int size = 0;
    WORD fail = 0;
310
    struct hostent *he;
311

312
    if ((he = gethostbyname( aq->host_name )))
313 314 315 316 317 318 319 320 321 322 323 324 325 326
    {
        size = aq->query.sbuflen;
        if (!ws_hostent_32_to_16( he, aq->query.sbuf, &size )) fail = WSAENOBUFS;
    }
    else fail = GetLastError();

    return finish_query( &aq->query, MAKELPARAM( size, fail ));
}

static DWORD WINAPI async_gethostbyaddr(LPVOID arg)
{
    struct async_query_gethostbyaddr *aq = arg;
    int size = 0;
    WORD fail = 0;
327
    struct hostent *he;
328

329
    if ((he = gethostbyaddr(aq->host_addr,aq->host_len,aq->host_type)))
330 331 332 333 334 335 336 337 338 339 340 341 342 343
    {
        size = aq->query.sbuflen;
        if (!ws_hostent_32_to_16( he, aq->query.sbuf, &size )) fail = WSAENOBUFS;
    }
    else fail = GetLastError();

    return finish_query( &aq->query, MAKELPARAM( size, fail ));
}

static DWORD WINAPI async_getprotobyname(LPVOID arg)
{
    struct async_query_getprotobyname *aq = arg;
    int size = 0;
    WORD fail = 0;
344
    struct protoent *pe;
345

346
    if ((pe = getprotobyname(aq->proto_name)))
347 348 349 350 351 352 353 354 355 356 357 358 359 360
    {
        size = aq->query.sbuflen;
        if (!ws_protoent_32_to_16( pe, aq->query.sbuf, &size )) fail = WSAENOBUFS;
    }
    else fail = GetLastError();

    return finish_query( &aq->query, MAKELPARAM( size, fail ));
}

static DWORD WINAPI async_getprotobynumber(LPVOID arg)
{
    struct async_query_getprotobynumber *aq = arg;
    int size = 0;
    WORD fail = 0;
361
    struct protoent *pe;
362

363
    if ((pe = getprotobynumber(aq->proto_number)))
364 365 366 367 368 369 370 371 372 373 374 375 376 377
    {
        size = aq->query.sbuflen;
        if (!ws_protoent_32_to_16( pe, aq->query.sbuf, &size )) fail = WSAENOBUFS;
    }
    else fail = GetLastError();

    return finish_query( &aq->query, MAKELPARAM( size, fail ));
}

static DWORD WINAPI async_getservbyname(LPVOID arg)
{
    struct async_query_getservbyname *aq = arg;
    int size = 0;
    WORD fail = 0;
378
    struct servent *se;
379

380
    if ((se = getservbyname(aq->serv_name,aq->serv_proto)))
381 382 383 384 385 386 387 388 389 390 391 392 393 394
    {
        size = aq->query.sbuflen;
        if (!ws_servent_32_to_16( se, aq->query.sbuf, &size )) fail = WSAENOBUFS;
    }
    else fail = GetLastError();

    return finish_query( &aq->query, MAKELPARAM( size, fail ));
}

static DWORD WINAPI async_getservbyport(LPVOID arg)
{
    struct async_query_getservbyport *aq = arg;
    int size = 0;
    WORD fail = 0;
395
    struct servent *se;
396

397
    if ((se = getservbyport(aq->serv_port,aq->serv_proto)))
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    {
        size = aq->query.sbuflen;
        if (!ws_servent_32_to_16( se, aq->query.sbuf, &size )) fail = WSAENOBUFS;
    }
    else fail = GetLastError();

    return finish_query( &aq->query, MAKELPARAM( size, fail ));
}

/****************************************************************************
 * The main async help function.
 *
 * 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.
 */
static HANDLE16 run_query( HWND16 hWnd, UINT uMsg, LPTHREAD_START_ROUTINE func,
                           struct async_query_header *query, SEGPTR sbuf, INT sbuflen )
{
    static LONG next_handle = 0xdead;
    HANDLE thread;
    ULONG handle = LOWORD( InterlockedIncrement( &next_handle ));

    /* avoid handle 0 */
    while (!handle) handle = LOWORD( InterlockedIncrement( &next_handle ));

    query->hWnd    = HWND_32(hWnd);
    query->uMsg    = uMsg;
    query->handle  = handle;
    query->sbuf    = sbuf;
    query->sbuflen = sbuflen;

    thread = CreateThread( NULL, 0, func, query, 0, NULL );
    if (!thread)
    {
        SetLastError( WSAEWOULDBLOCK );
        return 0;
    }
    CloseHandle( thread );
    return handle;
}
439 440 441 442

/***********************************************************************
 *              accept		(WINSOCK.1)
 */
443
SOCKET16 WINAPI accept16(SOCKET16 s, struct sockaddr* addr, INT16* addrlen16 )
444 445
{
    INT addrlen32 = addrlen16 ? *addrlen16 : 0;
446
    SOCKET retSocket = accept( s, addr, &addrlen32 );
447 448 449 450 451 452 453
    if( addrlen16 ) *addrlen16 = addrlen32;
    return retSocket;
}

/***********************************************************************
 *              bind			(WINSOCK.2)
 */
454
INT16 WINAPI bind16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
455
{
456
    return bind( s, name, namelen );
457 458 459 460 461 462 463
}

/***********************************************************************
 *              closesocket           (WINSOCK.3)
 */
INT16 WINAPI closesocket16(SOCKET16 s)
{
464
    return closesocket(s);
465 466 467 468 469
}

/***********************************************************************
 *              connect               (WINSOCK.4)
 */
470
INT16 WINAPI connect16(SOCKET16 s, struct sockaddr *name, INT16 namelen)
471
{
472
    return connect( s, name, namelen );
473 474 475 476 477
}

/***********************************************************************
 *              getpeername		(WINSOCK.5)
 */
478
INT16 WINAPI getpeername16(SOCKET16 s, struct sockaddr *name, INT16 *namelen16)
479 480
{
    INT namelen32 = *namelen16;
481
    INT retVal = getpeername( s, name, &namelen32 );
482 483 484 485 486 487 488
   *namelen16 = namelen32;
    return retVal;
}

/***********************************************************************
 *              getsockname		(WINSOCK.6)
 */
489
INT16 WINAPI getsockname16(SOCKET16 s, struct sockaddr *name, INT16 *namelen16)
490 491 492 493 494 495
{
    INT retVal;

    if( namelen16 )
    {
        INT namelen32 = *namelen16;
496
        retVal = getsockname( s, name, &namelen32 );
497 498 499 500 501 502 503 504 505 506 507 508 509 510
       *namelen16 = namelen32;
    }
    else retVal = SOCKET_ERROR;
    return retVal;
}

/***********************************************************************
 *              getsockopt		(WINSOCK.7)
 */
INT16 WINAPI getsockopt16(SOCKET16 s, INT16 level, INT16 optname, char *optval, INT16 *optlen)
{
    INT optlen32;
    INT *p = &optlen32;
    INT retVal;
511

512
    if( optlen ) optlen32 = *optlen; else p = NULL;
513
    retVal = getsockopt( s, (WORD)level, optname, optval, p );
514 515 516 517
    if( optlen ) *optlen = optlen32;
    return retVal;
}

518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544
/***********************************************************************
 *		htonl			(WINSOCK.8)
 */
u_long WINAPI htonl16(u_long hostlong)
{
    return htonl(hostlong);
}


/***********************************************************************
 *		htons			(WINSOCK.9)
 */
u_short WINAPI htons16(u_short hostshort)
{
    return htons(hostshort);
}

/***********************************************************************
 *		inet_addr		(WINSOCK.10)
 */
u_long WINAPI inet_addr16(const char *cp)
{
    if (!cp) return INADDR_NONE;
    return inet_addr(cp);
}


545 546 547
/***********************************************************************
 *		inet_ntoa		(WINSOCK.11)
 */
548
SEGPTR WINAPI inet_ntoa16(struct in_addr in)
549 550
{
    char* retVal;
551
    if (!(retVal = inet_ntoa( in ))) return 0;
552 553 554 555 556 557 558
    if (!dbuffer_seg) dbuffer_seg = MapLS( retVal );
    return dbuffer_seg;
}

/***********************************************************************
 *              ioctlsocket           (WINSOCK.12)
 */
559
INT16 WINAPI ioctlsocket16(SOCKET16 s, LONG cmd, u_long *argp)
560
{
561
    return ioctlsocket( s, cmd, argp );
562 563 564 565 566 567 568
}

/***********************************************************************
 *              listen		(WINSOCK.13)
 */
INT16 WINAPI listen16(SOCKET16 s, INT16 backlog)
{
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
    return listen( s, backlog );
}

/***********************************************************************
 *		ntohl			(WINSOCK.14)
 */
u_long WINAPI ntohl16(u_long netlong)
{
    return ntohl(netlong);
}


/***********************************************************************
 *		ntohs			(WINSOCK.15)
 */
u_short WINAPI ntohs16(u_short netshort)
{
    return ntohs(netshort);
587 588 589 590 591 592 593
}

/***********************************************************************
 *              recv			(WINSOCK.16)
 */
INT16 WINAPI recv16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
{
594
    return recv( s, buf, len, flags );
595 596 597 598 599 600
}

/***********************************************************************
 *              recvfrom		(WINSOCK.17)
 */
INT16 WINAPI recvfrom16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
601
                        struct sockaddr *from, INT16 *fromlen16)
602 603 604 605
{
    if (fromlen16)
    {
        INT fromlen32 = *fromlen16;
606
        INT retVal = recvfrom( s, buf, len, flags, from, &fromlen32 );
607 608 609
        *fromlen16 = fromlen32;
        return retVal;
    }
610
    else return recvfrom( s, buf, len, flags, from, NULL );
611 612 613 614 615 616 617
}

/***********************************************************************
 *		select			(WINSOCK.18)
 */
INT16 WINAPI select16(INT16 nfds, ws_fd_set16 *ws_readfds,
                      ws_fd_set16 *ws_writefds, ws_fd_set16 *ws_exceptfds,
618
                      struct timeval* timeout)
619
{
620 621
    fd_set read_set, write_set, except_set;
    fd_set *pread_set = NULL, *pwrite_set = NULL, *pexcept_set = NULL;
622 623 624 625 626 627
    int ret;

    if (ws_readfds) pread_set = ws_fdset_16_to_32( ws_readfds, &read_set );
    if (ws_writefds) pwrite_set = ws_fdset_16_to_32( ws_writefds, &write_set );
    if (ws_exceptfds) pexcept_set = ws_fdset_16_to_32( ws_exceptfds, &except_set );
    /* struct timeval is the same for both 32- and 16-bit code */
628
    ret = select( nfds, pread_set, pwrite_set, pexcept_set, timeout );
629 630 631 632 633 634 635 636 637 638 639
    if (ws_readfds) ws_fdset_32_to_16( &read_set, ws_readfds );
    if (ws_writefds) ws_fdset_32_to_16( &write_set, ws_writefds );
    if (ws_exceptfds) ws_fdset_32_to_16( &except_set, ws_exceptfds );
    return ret;
}

/***********************************************************************
 *              send			(WINSOCK.19)
 */
INT16 WINAPI send16(SOCKET16 s, char *buf, INT16 len, INT16 flags)
{
640
    return send( s, buf, len, flags );
641 642 643 644 645 646
}

/***********************************************************************
 *              sendto		(WINSOCK.20)
 */
INT16 WINAPI sendto16(SOCKET16 s, char *buf, INT16 len, INT16 flags,
647
                      struct sockaddr *to, INT16 tolen)
648
{
649
    return sendto( s, buf, len, flags, to, tolen );
650 651 652 653 654 655 656 657 658
}

/***********************************************************************
 *              setsockopt		(WINSOCK.21)
 */
INT16 WINAPI setsockopt16(SOCKET16 s, INT16 level, INT16 optname,
                          char *optval, INT16 optlen)
{
    if( !optval ) return SOCKET_ERROR;
659
    return setsockopt( s, (WORD)level, optname, optval, optlen );
660 661 662 663 664 665 666
}

/***********************************************************************
 *              shutdown		(WINSOCK.22)
 */
INT16 WINAPI shutdown16(SOCKET16 s, INT16 how)
{
667
    return shutdown( s, how );
668 669 670 671 672 673 674
}

/***********************************************************************
 *              socket		(WINSOCK.23)
 */
SOCKET16 WINAPI socket16(INT16 af, INT16 type, INT16 protocol)
{
675
    return socket( af, type, protocol );
676 677 678 679 680 681 682
}

/***********************************************************************
 *		gethostbyaddr		(WINSOCK.51)
 */
SEGPTR WINAPI gethostbyaddr16(const char *addr, INT16 len, INT16 type)
{
683
    struct hostent *he;
684

685
    if (!(he = gethostbyaddr( addr, len, type ))) return 0;
686
    return ws_hostent_32_to_16( he, 0, NULL );
687 688 689 690 691 692 693
}

/***********************************************************************
 *		gethostbyname		(WINSOCK.52)
 */
SEGPTR WINAPI gethostbyname16(const char *name)
{
694
    struct hostent *he;
695

696
    if (!(he = gethostbyname( name ))) return 0;
697
    return ws_hostent_32_to_16( he, 0, NULL );
698 699 700 701 702 703 704
}

/***********************************************************************
 *		getprotobyname		(WINSOCK.53)
 */
SEGPTR WINAPI getprotobyname16(const char *name)
{
705
    struct protoent *pe;
706

707
    if (!(pe = getprotobyname( name ))) return 0;
708
    return ws_protoent_32_to_16( pe, 0, NULL );
709 710 711 712 713 714 715
}

/***********************************************************************
 *		getprotobynumber	(WINSOCK.54)
 */
SEGPTR WINAPI getprotobynumber16(INT16 number)
{
716
    struct protoent *pe;
717

718
    if (!(pe = getprotobynumber( number ))) return 0;
719
    return ws_protoent_32_to_16( pe, 0, NULL );
720 721 722 723 724 725 726
}

/***********************************************************************
 *		getservbyname		(WINSOCK.55)
 */
SEGPTR WINAPI getservbyname16(const char *name, const char *proto)
{
727
    struct servent *se;
728

729
    if (!(se = getservbyname( name, proto ))) return 0;
730
    return ws_servent_32_to_16( se, 0, NULL );
731 732 733 734 735 736 737
}

/***********************************************************************
 *		getservbyport		(WINSOCK.56)
 */
SEGPTR WINAPI getservbyport16(INT16 port, const char *proto)
{
738
    struct servent *se;
739

740
    if (!(se = getservbyport( port, proto ))) return 0;
741
    return ws_servent_32_to_16( se, 0, NULL );
742 743 744 745 746 747 748
}

/***********************************************************************
 *              gethostname           (WINSOCK.57)
 */
INT16 WINAPI gethostname16(char *name, INT16 namelen)
{
749 750
    extern int WINAPI gethostname(char *name, INT namelen);
    return gethostname(name, namelen);
751 752 753 754 755 756 757 758 759 760
}

/***********************************************************************
 *      WSAAsyncSelect		(WINSOCK.101)
 */
INT16 WINAPI WSAAsyncSelect16(SOCKET16 s, HWND16 hWnd, UINT16 wMsg, LONG lEvent)
{
    return WSAAsyncSelect( s, HWND_32(hWnd), wMsg, lEvent );
}

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
/***********************************************************************
 *       WSAAsyncGetHostByAddr	(WINSOCK.102)
 */
HANDLE16 WINAPI WSAAsyncGetHostByAddr16(HWND16 hWnd, UINT16 uMsg, LPCSTR addr,
                               INT16 len, INT16 type, SEGPTR sbuf, INT16 buflen)
{
    struct async_query_gethostbyaddr *aq;

    TRACE("hwnd %04x, 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 );
    return run_query( hWnd, uMsg, async_gethostbyaddr, &aq->query, sbuf, buflen );
}

/***********************************************************************
 *       WSAAsyncGetHostByName	(WINSOCK.103)
 */
HANDLE16 WINAPI WSAAsyncGetHostByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
                                      SEGPTR sbuf, INT16 buflen)
{
    struct async_query_gethostbyname *aq;
    unsigned int len = strlen(name) + 1;

    TRACE("hwnd %04x, 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 );
    return run_query( hWnd, uMsg, async_gethostbyname, &aq->query, sbuf, buflen );
}

/***********************************************************************
 *       WSAAsyncGetProtoByNumber	(WINSOCK.104)
 */
HANDLE16 WINAPI WSAAsyncGetProtoByNumber16(HWND16 hWnd,UINT16 uMsg,INT16 number,
                                           SEGPTR sbuf, INT16 buflen)
{
    struct async_query_getprotobynumber *aq;

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

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

/***********************************************************************
 *       WSAAsyncGetProtoByName	(WINSOCK.105)
 */
HANDLE16 WINAPI WSAAsyncGetProtoByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
                                         SEGPTR sbuf, INT16 buflen)
{
    struct async_query_getprotobyname *aq;
    unsigned int len = strlen(name) + 1;

    TRACE("hwnd %04x, 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 );
    return run_query( hWnd, uMsg, async_getprotobyname, &aq->query, sbuf, buflen );
}

/***********************************************************************
 *       WSAAsyncGetServByPort	(WINSOCK.106)
 */
HANDLE16 WINAPI WSAAsyncGetServByPort16(HWND16 hWnd, UINT16 uMsg, INT16 port,
                                        LPCSTR proto, SEGPTR sbuf, INT16 buflen)
{
    struct async_query_getservbyport *aq;
    unsigned int len = strlen(proto) + 1;

    TRACE("hwnd %04x, 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;
    }
    aq->serv_proto = (char *)(aq + 1);
    aq->serv_port  = port;
    strcpy( aq->serv_proto, proto );
    return run_query( hWnd, uMsg, async_getservbyport, &aq->query, sbuf, buflen );
}

/***********************************************************************
 *       WSAAsyncGetServByName	(WINSOCK.107)
 */
HANDLE16 WINAPI WSAAsyncGetServByName16(HWND16 hWnd, UINT16 uMsg, LPCSTR name,
                                        LPCSTR proto, SEGPTR sbuf, INT16 buflen)
{
    struct async_query_getservbyname *aq;
    unsigned int len1 = strlen(name) + 1;
    unsigned int len2 = strlen(proto) + 1;

    TRACE("hwnd %04x, 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;
    }
    aq->serv_name  = (char *)(aq + 1);
    aq->serv_proto = aq->serv_name + len1;
    strcpy( aq->serv_name, name );
    strcpy( aq->serv_proto, proto );
    return run_query( hWnd, uMsg, async_getservbyname, &aq->query, sbuf, buflen );
}

/***********************************************************************
 *       WSACancelAsyncRequest	(WINSOCK.108)
 */
INT16 WINAPI WSACancelAsyncRequest16(HANDLE16 hAsyncTaskHandle)
{
    FIXME("(%04x),stub\n", hAsyncTaskHandle);
    return 0;
}

899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
/***********************************************************************
 *      WSASetBlockingHook		(WINSOCK.109)
 */
FARPROC16 WINAPI WSASetBlockingHook16(FARPROC16 lpBlockFunc)
{
    /* FIXME: should deal with 16-bit proc */
    return (FARPROC16)WSASetBlockingHook( (FARPROC)lpBlockFunc );
}

/***********************************************************************
 *      WSAUnhookBlockingHook	(WINSOCK.110)
 */
INT16 WINAPI WSAUnhookBlockingHook16(void)
{
    return WSAUnhookBlockingHook();
}

916 917 918 919 920 921 922 923
/***********************************************************************
 *      WSAGetLastError		(WINSOCK.111)
 */
INT WINAPI WSAGetLastError16(void)
{
    return WSAGetLastError();
}

924 925 926 927 928 929 930 931
/***********************************************************************
 *      WSASetLastError		(WINSOCK.112)
 */
void WINAPI WSASetLastError16(INT16 iError)
{
    WSASetLastError(iError);
}

932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947
/***********************************************************************
 *      WSACancelBlockingCall		(WINSOCK.113)
 */
INT WINAPI WSACancelBlockingCall16(void)
{
    return WSACancelBlockingCall();
}

/***********************************************************************
 *      WSAIsBlocking			(WINSOCK.114)
 */
BOOL WINAPI WSAIsBlocking16(void)
{
    return WSAIsBlocking();
}

948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990
/***********************************************************************
 *      WSAStartup			(WINSOCK.115)
 *
 * Create socket control struct, attach it to the global list and
 * update a pointer in the task struct.
 */
INT16 WINAPI WSAStartup16(UINT16 wVersionRequested, LPWSADATA16 lpWSAData)
{
    WSADATA data;
    INT ret = WSAStartup( wVersionRequested, &data );

    if (!ret)
    {
        lpWSAData->wVersion     = 0x0101;
        lpWSAData->wHighVersion = 0x0101;
        strcpy( lpWSAData->szDescription, data.szDescription );
        strcpy( lpWSAData->szSystemStatus, data.szSystemStatus );
        lpWSAData->iMaxSockets = data.iMaxSockets;
        lpWSAData->iMaxUdpDg = data.iMaxUdpDg;
        lpWSAData->lpVendorInfo = 0;
        num_startup++;
   }
    return ret;
}

/***********************************************************************
 *      WSACleanup			(WINSOCK.116)
 */
INT WINAPI WSACleanup16(void)
{
    if (num_startup)
    {
        if (!--num_startup)
        {
            /* delete scratch buffers */
            UnMapLS( he_buffer_seg );
            UnMapLS( se_buffer_seg );
            UnMapLS( pe_buffer_seg );
            UnMapLS( dbuffer_seg );
            he_buffer_seg = 0;
            se_buffer_seg = 0;
            pe_buffer_seg = 0;
            dbuffer_seg = 0;
991 992 993
            HeapFree( GetProcessHeap(), 0, he_buffer );
            HeapFree( GetProcessHeap(), 0, se_buffer );
            HeapFree( GetProcessHeap(), 0, pe_buffer );
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
            he_buffer = NULL;
            se_buffer = NULL;
            pe_buffer = NULL;
        }
    }
    return WSACleanup();
}


/***********************************************************************
 *	__WSAFDIsSet			(WINSOCK.151)
 */
INT16 WINAPI __WSAFDIsSet16(SOCKET16 s, ws_fd_set16 *set)
{
  int i = set->fd_count;

  TRACE("(%d,%p(%i))\n", s, set, i);

  while (i--)
      if (set->fd_array[i] == s) return 1;
  return 0;
}

/***********************************************************************
 *              WSARecvEx			(WINSOCK.1107)
 *
 * See description for WSARecvEx()
 */
INT16 WINAPI WSARecvEx16(SOCKET16 s, char *buf, INT16 len, INT16 *flags)
{
1024
    FIXME("(WSARecvEx16) partial packet return value not set\n");
1025 1026
    return recv16(s, buf, len, *flags);
}