Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / wsock32 / protocol.c
blob9482887ff72df4e345ad2670ceeee087c197a03c
1 /*
2 * WSOCK32 specific functions
4 * Copyright (C) 2001 Stefan Leichter
5 * Copyright (C) 2008 Hans Leidekker
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.
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.
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
22 #define USE_WS_PREFIX
24 #include "config.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <string.h>
30 #ifdef HAVE_ARPA_INET_H
31 #include <arpa/inet.h>
32 #endif
33 #ifdef HAVE_NETDB_H
34 #include <netdb.h>
35 #endif
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winsock2.h"
40 #include "nspapi.h"
42 #include "wine/debug.h"
43 #include "wine/unicode.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(winsock);
47 /*****************************************************************************
48 * inet_network [WSOCK32.1100]
50 UINT WINAPI WSOCK32_inet_network(const char *cp)
52 #ifdef HAVE_INET_NETWORK
53 return inet_network(cp);
54 #else
55 return 0;
56 #endif
59 /*****************************************************************************
60 * getnetbyname [WSOCK32.1101]
62 struct netent * WINAPI WSOCK32_getnetbyname(const char *name)
64 #ifdef HAVE_GETNETBYNAME
65 return getnetbyname(name);
66 #else
67 return NULL;
68 #endif
71 static DWORD map_service(DWORD wsaflags)
73 DWORD flags = 0;
75 if (wsaflags & XP1_CONNECTIONLESS) flags |= XP_CONNECTIONLESS;
76 if (wsaflags & XP1_GUARANTEED_DELIVERY) flags |= XP_GUARANTEED_DELIVERY;
77 if (wsaflags & XP1_GUARANTEED_ORDER) flags |= XP_GUARANTEED_ORDER;
78 if (wsaflags & XP1_MESSAGE_ORIENTED) flags |= XP_MESSAGE_ORIENTED;
79 if (wsaflags & XP1_PSEUDO_STREAM) flags |= XP_PSEUDO_STREAM;
80 if (wsaflags & XP1_GRACEFUL_CLOSE) flags |= XP_GRACEFUL_CLOSE;
81 if (wsaflags & XP1_EXPEDITED_DATA) flags |= XP_EXPEDITED_DATA;
82 if (wsaflags & XP1_CONNECT_DATA) flags |= XP_CONNECT_DATA;
83 if (wsaflags & XP1_DISCONNECT_DATA) flags |= XP_DISCONNECT_DATA;
84 if (wsaflags & XP1_SUPPORT_BROADCAST) flags |= XP_SUPPORTS_BROADCAST;
85 if (wsaflags & XP1_SUPPORT_MULTIPOINT) flags |= XP_SUPPORTS_MULTICAST;
86 if (wsaflags & XP1_QOS_SUPPORTED) flags |= XP_BANDWIDTH_ALLOCATION;
87 if (wsaflags & XP1_PARTIAL_MESSAGE) flags |= XP_FRAGMENTATION;
88 return flags;
91 /*****************************************************************************
92 * EnumProtocolsA [WSOCK32.1111]
94 INT WINAPI EnumProtocolsA(LPINT protocols, LPVOID buffer, LPDWORD buflen)
96 INT ret;
97 DWORD size, string_size = WSAPROTOCOL_LEN + 1;
99 TRACE("%p, %p, %p\n", protocols, buffer, buflen);
101 if (!buflen) return SOCKET_ERROR;
103 size = 0;
104 ret = WSAEnumProtocolsA(protocols, NULL, &size);
106 if (ret == SOCKET_ERROR && WSAGetLastError() == WSAENOBUFS)
108 DWORD num_protocols = size / sizeof(WSAPROTOCOL_INFOA);
109 if (*buflen < num_protocols * (sizeof(PROTOCOL_INFOA) + string_size))
111 *buflen = num_protocols * (sizeof(PROTOCOL_INFOA) + string_size);
112 return SOCKET_ERROR;
114 if (buffer)
116 WSAPROTOCOL_INFOA *wsabuf;
117 PROTOCOL_INFOA *pi = buffer;
118 unsigned int i, string_offset;
120 if (!(wsabuf = HeapAlloc(GetProcessHeap(), 0, size))) return SOCKET_ERROR;
122 ret = WSAEnumProtocolsA(protocols, wsabuf, &size);
123 string_offset = ret * sizeof(PROTOCOL_INFOA);
125 for (i = 0; i < ret; i++)
127 pi[i].dwServiceFlags = map_service(wsabuf[i].dwServiceFlags1);
128 pi[i].iAddressFamily = wsabuf[i].iAddressFamily;
129 pi[i].iMaxSockAddr = wsabuf[i].iMaxSockAddr;
130 pi[i].iMinSockAddr = wsabuf[i].iMinSockAddr;
131 pi[i].iSocketType = wsabuf[i].iSocketType;
132 pi[i].iProtocol = wsabuf[i].iProtocol;
133 pi[i].dwMessageSize = wsabuf[i].dwMessageSize;
135 memcpy((char *)buffer + string_offset, wsabuf[i].szProtocol, string_size);
136 pi[i].lpProtocol = (char *)buffer + string_offset;
137 string_offset += string_size;
139 HeapFree(GetProcessHeap(), 0, wsabuf);
142 return ret;
145 /*****************************************************************************
146 * EnumProtocolsW [WSOCK32.1112]
148 INT WINAPI EnumProtocolsW(LPINT protocols, LPVOID buffer, LPDWORD buflen)
150 INT ret;
151 DWORD size, string_size = (WSAPROTOCOL_LEN + 1) * sizeof(WCHAR);
153 TRACE("%p, %p, %p\n", protocols, buffer, buflen);
155 if (!buflen) return SOCKET_ERROR;
157 size = 0;
158 ret = WSAEnumProtocolsW(protocols, NULL, &size);
160 if (ret == SOCKET_ERROR && WSAGetLastError() == WSAENOBUFS)
162 DWORD num_protocols = size / sizeof(WSAPROTOCOL_INFOW);
163 if (*buflen < num_protocols * (sizeof(PROTOCOL_INFOW) + string_size))
165 *buflen = num_protocols * (sizeof(PROTOCOL_INFOW) + string_size);
166 return SOCKET_ERROR;
168 if (buffer)
170 WSAPROTOCOL_INFOW *wsabuf;
171 PROTOCOL_INFOW *pi = buffer;
172 unsigned int i, string_offset;
174 if (!(wsabuf = HeapAlloc(GetProcessHeap(), 0, size))) return SOCKET_ERROR;
176 ret = WSAEnumProtocolsW(protocols, wsabuf, &size);
177 string_offset = ret * sizeof(PROTOCOL_INFOW);
179 for (i = 0; i < ret; i++)
181 pi[i].dwServiceFlags = map_service(wsabuf[i].dwServiceFlags1);
182 pi[i].iAddressFamily = wsabuf[i].iAddressFamily;
183 pi[i].iMaxSockAddr = wsabuf[i].iMaxSockAddr;
184 pi[i].iMinSockAddr = wsabuf[i].iMinSockAddr;
185 pi[i].iSocketType = wsabuf[i].iSocketType;
186 pi[i].iProtocol = wsabuf[i].iProtocol;
187 pi[i].dwMessageSize = wsabuf[i].dwMessageSize;
189 memcpy((char *)buffer + string_offset, wsabuf[i].szProtocol, string_size);
190 pi[i].lpProtocol = (WCHAR *)(char *)buffer + string_offset;
191 string_offset += string_size;
193 HeapFree(GetProcessHeap(), 0, wsabuf);
196 return ret;