protocol.c 6.37 KB
Newer Older
1 2 3 4
/*
 * WSOCK32 specific functions
 *
 * Copyright (C) 2001 Stefan Leichter
5
 * Copyright (C) 2008 Hans Leidekker
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
#include <stdarg.h>
23

24
#include "windef.h"
25
#include "winbase.h"
26
#include "winsock2.h"
27
#include "nspapi.h"
28

29
#include "wine/debug.h"
30

31 32
WINE_DEFAULT_DEBUG_CHANNEL(winsock);

33 34 35
/*****************************************************************************
 *          inet_network       [WSOCK32.1100]
 */
Patrik Stridvall's avatar
Patrik Stridvall committed
36
UINT WINAPI WSOCK32_inet_network(const char *cp)
37
{
38
    return ntohl( inet_addr( cp ) );
39 40 41 42 43
}

/*****************************************************************************
 *          getnetbyname       [WSOCK32.1101]
 */
Patrik Stridvall's avatar
Patrik Stridvall committed
44
struct netent * WINAPI WSOCK32_getnetbyname(const char *name)
45
{
46
    ERR( "%s: not supported\n", debugstr_a(name) );
47
    return NULL;
48
}
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64

static DWORD map_service(DWORD wsaflags)
{
    DWORD flags = 0;

    if (wsaflags & XP1_CONNECTIONLESS)      flags |= XP_CONNECTIONLESS;
    if (wsaflags & XP1_GUARANTEED_DELIVERY) flags |= XP_GUARANTEED_DELIVERY;
    if (wsaflags & XP1_GUARANTEED_ORDER)    flags |= XP_GUARANTEED_ORDER;
    if (wsaflags & XP1_MESSAGE_ORIENTED)    flags |= XP_MESSAGE_ORIENTED;
    if (wsaflags & XP1_PSEUDO_STREAM)       flags |= XP_PSEUDO_STREAM;
    if (wsaflags & XP1_GRACEFUL_CLOSE)      flags |= XP_GRACEFUL_CLOSE;
    if (wsaflags & XP1_EXPEDITED_DATA)      flags |= XP_EXPEDITED_DATA;
    if (wsaflags & XP1_CONNECT_DATA)        flags |= XP_CONNECT_DATA;
    if (wsaflags & XP1_DISCONNECT_DATA)     flags |= XP_DISCONNECT_DATA;
    if (wsaflags & XP1_SUPPORT_BROADCAST)   flags |= XP_SUPPORTS_BROADCAST;
    if (wsaflags & XP1_SUPPORT_MULTIPOINT)  flags |= XP_SUPPORTS_MULTICAST;
65
    if (wsaflags & XP1_QOS_SUPPORTED)       flags |= XP_BANDWIDTH_ALLOCATION;
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    if (wsaflags & XP1_PARTIAL_MESSAGE)     flags |= XP_FRAGMENTATION;
    return flags;
}

/*****************************************************************************
 *          EnumProtocolsA       [WSOCK32.1111]
 */
INT WINAPI EnumProtocolsA(LPINT protocols, LPVOID buffer, LPDWORD buflen)
{
    INT ret;
    DWORD size, string_size = WSAPROTOCOL_LEN + 1;

    TRACE("%p, %p, %p\n", protocols, buffer, buflen);

    if (!buflen) return SOCKET_ERROR;

    size = 0;
    ret = WSAEnumProtocolsA(protocols, NULL, &size);

    if (ret == SOCKET_ERROR && WSAGetLastError() == WSAENOBUFS)
    {
        DWORD num_protocols = size / sizeof(WSAPROTOCOL_INFOA);
        if (*buflen < num_protocols * (sizeof(PROTOCOL_INFOA) + string_size))
        {
            *buflen = num_protocols * (sizeof(PROTOCOL_INFOA) + string_size);
            return SOCKET_ERROR;
        }
        if (buffer)
        {
            WSAPROTOCOL_INFOA *wsabuf;
            PROTOCOL_INFOA *pi = buffer;
97 98
            unsigned int string_offset;
            INT i;
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 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

            if (!(wsabuf = HeapAlloc(GetProcessHeap(), 0, size))) return SOCKET_ERROR;

            ret = WSAEnumProtocolsA(protocols, wsabuf, &size);
            string_offset = ret * sizeof(PROTOCOL_INFOA);

            for (i = 0; i < ret; i++)
            {
                pi[i].dwServiceFlags = map_service(wsabuf[i].dwServiceFlags1);
                pi[i].iAddressFamily = wsabuf[i].iAddressFamily;
                pi[i].iMaxSockAddr   = wsabuf[i].iMaxSockAddr;
                pi[i].iMinSockAddr   = wsabuf[i].iMinSockAddr;
                pi[i].iSocketType    = wsabuf[i].iSocketType;
                pi[i].iProtocol      = wsabuf[i].iProtocol;
                pi[i].dwMessageSize  = wsabuf[i].dwMessageSize;

                memcpy((char *)buffer + string_offset, wsabuf[i].szProtocol, string_size);
                pi[i].lpProtocol = (char *)buffer + string_offset;
                string_offset += string_size;
            }
            HeapFree(GetProcessHeap(), 0, wsabuf);
        }
    }
    return ret;
}

/*****************************************************************************
 *          EnumProtocolsW       [WSOCK32.1112]
 */
INT WINAPI EnumProtocolsW(LPINT protocols, LPVOID buffer, LPDWORD buflen)
{
    INT ret;
    DWORD size, string_size = (WSAPROTOCOL_LEN + 1) * sizeof(WCHAR);

    TRACE("%p, %p, %p\n", protocols, buffer, buflen);

    if (!buflen) return SOCKET_ERROR;

    size = 0;
    ret = WSAEnumProtocolsW(protocols, NULL, &size);

    if (ret == SOCKET_ERROR && WSAGetLastError() == WSAENOBUFS)
    {
        DWORD num_protocols = size / sizeof(WSAPROTOCOL_INFOW);
        if (*buflen < num_protocols * (sizeof(PROTOCOL_INFOW) + string_size))
        {
            *buflen = num_protocols * (sizeof(PROTOCOL_INFOW) + string_size);
            return SOCKET_ERROR;
        }
        if (buffer)
        {
            WSAPROTOCOL_INFOW *wsabuf;
            PROTOCOL_INFOW *pi = buffer;
152 153
            unsigned int string_offset;
            INT i;
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170

            if (!(wsabuf = HeapAlloc(GetProcessHeap(), 0, size))) return SOCKET_ERROR;

            ret = WSAEnumProtocolsW(protocols, wsabuf, &size);
            string_offset = ret * sizeof(PROTOCOL_INFOW);

            for (i = 0; i < ret; i++)
            {
                pi[i].dwServiceFlags = map_service(wsabuf[i].dwServiceFlags1);
                pi[i].iAddressFamily = wsabuf[i].iAddressFamily;
                pi[i].iMaxSockAddr   = wsabuf[i].iMaxSockAddr;
                pi[i].iMinSockAddr   = wsabuf[i].iMinSockAddr;
                pi[i].iSocketType    = wsabuf[i].iSocketType;
                pi[i].iProtocol      = wsabuf[i].iProtocol;
                pi[i].dwMessageSize  = wsabuf[i].dwMessageSize;

                memcpy((char *)buffer + string_offset, wsabuf[i].szProtocol, string_size);
171
                pi[i].lpProtocol = (WCHAR *)((char *)buffer + string_offset);
172 173 174 175 176 177 178
                string_offset += string_size;
            }
            HeapFree(GetProcessHeap(), 0, wsabuf);
        }
    }
    return ret;
}