FreeRDP
winsock.h
1 
20 #ifndef WINPR_WINSOCK_H
21 #define WINPR_WINSOCK_H
22 
23 #include <winpr/platform.h>
24 #include <winpr/winpr.h>
25 #include <winpr/wtypes.h>
26 #include <winpr/windows.h>
27 
28 WINPR_PRAGMA_DIAG_PUSH
29 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_IDENTIFIER
30 
31 #ifdef _WIN32
32 
33 #define _accept accept
34 #define _bind bind
35 #define _connect connect
36 #define _ioctlsocket ioctlsocket
37 #define _getpeername getpeername
38 #define _getsockname getsockname
39 #define _getsockopt getsockopt
40 #define _htonl htonl
41 #define _htons htons
42 #define _inet_addr inet_addr
43 #define _inet_ntoa inet_ntoa
44 #define _listen listen
45 #define _ntohl ntohl
46 #define _ntohs ntohs
47 #define _recv recv
48 #define _recvfrom recvfrom
49 #define _select select
50 #define _send send
51 #define _sendto sendto
52 #define _setsockopt setsockopt
53 #define _shutdown shutdown
54 #define _socket socket
55 #define _gethostbyaddr gethostbyaddr
56 #define _gethostbyname gethostbyname
57 #define _gethostname gethostname
58 #define _getservbyport getservbyport
59 #define _getservbyname getservbyname
60 #define _getprotobynumber getprotobynumber
61 #define _getprotobyname getprotobyname
62 
63 #define _IFF_UP IFF_UP
64 #define _IFF_BROADCAST IFF_BROADCAST
65 #define _IFF_LOOPBACK IFF_LOOPBACK
66 #define _IFF_POINTTOPOINT IFF_POINTTOPOINT
67 #define _IFF_MULTICAST IFF_MULTICAST
68 
69 #if (_WIN32_WINNT < 0x0600)
70 
71 WINPR_API PCSTR winpr_inet_ntop(INT Family, PVOID pAddr, PSTR pStringBuf, size_t StringBufSize);
72 WINPR_API INT winpr_inet_pton(INT Family, PCSTR pszAddrString, PVOID pAddrBuf);
73 
74 #define inet_ntop winpr_inet_ntop
75 #define inet_pton winpr_inet_pton
76 
77 #endif /* (_WIN32_WINNT < 0x0600) */
78 
79 #else /* _WIN32 */
80 
81 #include <netdb.h>
82 #include <unistd.h>
83 #include <sys/un.h>
84 #include <sys/ioctl.h>
85 #include <sys/socket.h>
86 #include <arpa/inet.h>
87 #include <netinet/in.h>
88 #include <netinet/tcp.h>
89 #include <net/if.h>
90 
91 #include <winpr/io.h>
92 #include <winpr/error.h>
93 #include <winpr/platform.h>
94 
95 #define WSAEVENT HANDLE
96 #define LPWSAEVENT LPHANDLE
97 #define WSAOVERLAPPED OVERLAPPED
99 
100 typedef UINT_PTR SOCKET;
101 typedef struct sockaddr_storage SOCKADDR_STORAGE;
102 
103 #ifndef INVALID_SOCKET
104 #define INVALID_SOCKET (SOCKET)(~0)
105 #endif
106 
107 #define WSADESCRIPTION_LEN 256
108 #define WSASYS_STATUS_LEN 128
109 
110 #define FD_READ_BIT 0
111 #define FD_READ (1 << FD_READ_BIT)
112 
113 #define FD_WRITE_BIT 1
114 #define FD_WRITE (1 << FD_WRITE_BIT)
115 
116 #define FD_OOB_BIT 2
117 #define FD_OOB (1 << FD_OOB_BIT)
118 
119 #define FD_ACCEPT_BIT 3
120 #define FD_ACCEPT (1 << FD_ACCEPT_BIT)
121 
122 #define FD_CONNECT_BIT 4
123 #define FD_CONNECT (1 << FD_CONNECT_BIT)
124 
125 #define FD_CLOSE_BIT 5
126 #define FD_CLOSE (1 << FD_CLOSE_BIT)
127 
128 #define FD_QOS_BIT 6
129 #define FD_QOS (1 << FD_QOS_BIT)
130 
131 #define FD_GROUP_QOS_BIT 7
132 #define FD_GROUP_QOS (1 << FD_GROUP_QOS_BIT)
133 
134 #define FD_ROUTING_INTERFACE_CHANGE_BIT 8
135 #define FD_ROUTING_INTERFACE_CHANGE (1 << FD_ROUTING_INTERFACE_CHANGE_BIT)
136 
137 #define FD_ADDRESS_LIST_CHANGE_BIT 9
138 #define FD_ADDRESS_LIST_CHANGE (1 << FD_ADDRESS_LIST_CHANGE_BIT)
139 
140 #define FD_MAX_EVENTS 10
141 #define FD_ALL_EVENTS ((1 << FD_MAX_EVENTS) - 1)
142 
143 #define SD_RECEIVE 0
144 #define SD_SEND 1
145 #define SD_BOTH 2
146 
147 #define SOCKET_ERROR (-1)
148 
149 typedef struct WSAData
150 {
151  WORD wVersion;
152  WORD wHighVersion;
153 #ifdef _M_AMD64
154  unsigned short iMaxSockets;
155  unsigned short iMaxUdpDg;
156  char* lpVendorInfo;
157  char szDescription[WSADESCRIPTION_LEN + 1];
158  char szSystemStatus[WSASYS_STATUS_LEN + 1];
159 #else
160  char szDescription[WSADESCRIPTION_LEN + 1];
161  char szSystemStatus[WSASYS_STATUS_LEN + 1];
162  unsigned short iMaxSockets;
163  unsigned short iMaxUdpDg;
164  char* lpVendorInfo;
165 #endif
166 } WSADATA, *LPWSADATA;
167 
168 #ifndef MAKEWORD
169 #define MAKEWORD(a, b) \
170  ((WORD)(((BYTE)((DWORD_PTR)(a)&0xFF)) | (((WORD)((BYTE)((DWORD_PTR)(b)&0xFF))) << 8)))
171 #endif
172 
173 typedef struct in6_addr IN6_ADDR;
174 typedef struct in6_addr* PIN6_ADDR;
175 typedef struct in6_addr* LPIN6_ADDR;
176 
178 {
179  SHORT sin6_family;
180  USHORT sin6_port;
181  ULONG sin6_flowinfo;
182  IN6_ADDR sin6_addr;
183 };
184 
185 typedef union sockaddr_gen
186 {
187  struct sockaddr Address;
188  struct sockaddr_in AddressIn; /* codespell:ignore addressin */
189  struct sockaddr_in6_old AddressIn6;
190 
191 } sockaddr_gen;
192 
193 WINPR_PRAGMA_DIAG_PUSH
194 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
195 
196 #define _IFF_UP 0x00000001
197 #define _IFF_BROADCAST 0x00000002
198 #define _IFF_LOOPBACK 0x00000004
199 #define _IFF_POINTTOPOINT 0x00000008
200 #define _IFF_MULTICAST 0x00000010
201 
202 WINPR_PRAGMA_DIAG_POP
203 
204 typedef struct
205 {
206  ULONG iiFlags;
207  sockaddr_gen iiAddress;
208  sockaddr_gen iiBroadcastAddress;
209  sockaddr_gen iiNetmask;
212 
213 #define MAX_PROTOCOL_CHAIN 7
214 #define WSAPROTOCOL_LEN 255
215 
216 typedef struct
217 {
218  int ChainLen;
219  DWORD ChainEntries[MAX_PROTOCOL_CHAIN];
221 
222 typedef struct
223 {
224  DWORD dwServiceFlags1;
225  DWORD dwServiceFlags2;
226  DWORD dwServiceFlags3;
227  DWORD dwServiceFlags4;
228  DWORD dwProviderFlags;
229  GUID ProviderId;
230  DWORD dwCatalogEntryId;
231  WSAPROTOCOLCHAIN ProtocolChain;
232  int iVersion;
233  int iAddressFamily;
234  int iMaxSockAddr;
235  int iMinSockAddr;
236  int iSocketType;
237  int iProtocol;
238  int iProtocolMaxOffset;
239  int iNetworkByteOrder;
240  int iSecurityScheme;
241  DWORD dwMessageSize;
242  DWORD dwProviderReserved;
243  CHAR szProtocol[WSAPROTOCOL_LEN + 1];
245 
246 typedef struct
247 {
248  DWORD dwServiceFlags1;
249  DWORD dwServiceFlags2;
250  DWORD dwServiceFlags3;
251  DWORD dwServiceFlags4;
252  DWORD dwProviderFlags;
253  GUID ProviderId;
254  DWORD dwCatalogEntryId;
255  WSAPROTOCOLCHAIN ProtocolChain;
256  int iVersion;
257  int iAddressFamily;
258  int iMaxSockAddr;
259  int iMinSockAddr;
260  int iSocketType;
261  int iProtocol;
262  int iProtocolMaxOffset;
263  int iNetworkByteOrder;
264  int iSecurityScheme;
265  DWORD dwMessageSize;
266  DWORD dwProviderReserved;
267  WCHAR szProtocol[WSAPROTOCOL_LEN + 1];
269 
270 typedef void(CALLBACK* LPWSAOVERLAPPED_COMPLETION_ROUTINE)(DWORD dwError, DWORD cbTransferred,
271  LPWSAOVERLAPPED lpOverlapped,
272  DWORD dwFlags);
273 
274 typedef UINT32 GROUP;
275 #define SG_UNCONSTRAINED_GROUP 0x01
276 #define SG_CONSTRAINED_GROUP 0x02
277 
278 #define SIO_GET_INTERFACE_LIST _IOR('t', 127, ULONG)
279 #define SIO_GET_INTERFACE_LIST_EX _IOR('t', 126, ULONG)
280 #define SIO_SET_MULTICAST_FILTER _IOW('t', 125, ULONG)
281 #define SIO_GET_MULTICAST_FILTER _IOW('t', 124 | IOC_IN, ULONG)
282 #define SIOCSIPMSFILTER SIO_SET_MULTICAST_FILTER
283 #define SIOCGIPMSFILTER SIO_GET_MULTICAST_FILTER
284 
285 #ifdef UNICODE
286 #define WSAPROTOCOL_INFO WSAPROTOCOL_INFOW
287 #define LPWSAPROTOCOL_INFO LPWSAPROTOCOL_INFOW
288 #else
289 #define WSAPROTOCOL_INFO WSAPROTOCOL_INFOA
290 #define LPWSAPROTOCOL_INFO LPWSAPROTOCOL_INFOA
291 #endif
292 
293 #ifdef __cplusplus
294 extern "C"
295 {
296 #endif
297 
298  WINPR_API int WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData);
299  WINPR_API int WSACleanup(void);
300 
301  WINPR_API void WSASetLastError(int iError);
302  WINPR_API int WSAGetLastError(void);
303 
304  WINPR_API BOOL WSACloseEvent(HANDLE hEvent);
305 
306  WINPR_ATTR_MALLOC(WSACloseEvent, 1)
307  WINPR_API HANDLE WSACreateEvent(void);
308  WINPR_API BOOL WSASetEvent(HANDLE hEvent);
309  WINPR_API BOOL WSAResetEvent(HANDLE hEvent);
310 
311  WINPR_API int WSAEventSelect(SOCKET s, WSAEVENT hEventObject, LONG lNetworkEvents);
312 
313  WINPR_API DWORD WSAWaitForMultipleEvents(DWORD cEvents, const HANDLE* lphEvents, BOOL fWaitAll,
314  DWORD dwTimeout, BOOL fAlertable);
315 
316  WINPR_API SOCKET WSASocketA(int af, int type, int protocol, LPWSAPROTOCOL_INFOA lpProtocolInfo,
317  GROUP g, DWORD dwFlags);
318  WINPR_API SOCKET WSASocketW(int af, int type, int protocol, LPWSAPROTOCOL_INFOW lpProtocolInfo,
319  GROUP g, DWORD dwFlags);
320 
321  WINPR_API int WSAIoctl(SOCKET s, DWORD dwIoControlCode, LPVOID lpvInBuffer, DWORD cbInBuffer,
322  LPVOID lpvOutBuffer, DWORD cbOutBuffer, LPDWORD lpcbBytesReturned,
323  LPWSAOVERLAPPED lpOverlapped,
324  LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
325 
326  WINPR_API SOCKET _accept(SOCKET s, struct sockaddr* addr, int* addrlen);
327  WINPR_API int _bind(SOCKET s, const struct sockaddr* addr, int namelen);
328  WINPR_API int closesocket(SOCKET s);
329  WINPR_API int _connect(SOCKET s, const struct sockaddr* name, int namelen);
330  WINPR_API int _ioctlsocket(SOCKET s, long cmd, u_long* argp);
331  WINPR_API int _getpeername(SOCKET s, struct sockaddr* name, int* namelen);
332  WINPR_API int _getsockname(SOCKET s, struct sockaddr* name, int* namelen);
333  WINPR_API int _getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen);
334  WINPR_API u_long _htonl(u_long hostlong);
335  WINPR_API u_short _htons(u_short hostshort);
336  WINPR_API unsigned long _inet_addr(const char* cp);
337  WINPR_API char* _inet_ntoa(struct in_addr in);
338  WINPR_API int _listen(SOCKET s, int backlog);
339  WINPR_API u_long _ntohl(u_long netlong);
340  WINPR_API u_short _ntohs(u_short netshort);
341  WINPR_API int _recv(SOCKET s, char* buf, int len, int flags);
342  WINPR_API int _recvfrom(SOCKET s, char* buf, int len, int flags, struct sockaddr* from,
343  int* fromlen);
344  WINPR_API int _select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
345  const struct timeval* timeout);
346  WINPR_API int _send(SOCKET s, const char* buf, int len, int flags);
347  WINPR_API int _sendto(SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to,
348  int tolen);
349  WINPR_API int _setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen);
350  WINPR_API int _shutdown(SOCKET s, int how);
351  WINPR_API SOCKET _socket(int af, int type, int protocol);
352  WINPR_API struct hostent* _gethostbyaddr(const char* addr, int len, int type);
353  WINPR_API struct hostent* _gethostbyname(const char* name);
354  WINPR_API int _gethostname(char* name, int namelen);
355  WINPR_API struct servent* /* codespell:ignore servent */ _getservbyport(int port,
356  const char* proto);
357  WINPR_API struct servent* /* codespell:ignore servent */ _getservbyname(const char* name,
358  const char* proto);
359  WINPR_API struct protoent* _getprotobynumber(int number);
360  WINPR_API struct protoent* _getprotobyname(const char* name);
361 
362 #ifdef __cplusplus
363 }
364 #endif
365 
366 #ifdef UNICODE
367 #define WSASocket WSASocketW
368 #else
369 #define WSASocket WSASocketA
370 #endif
371 
372 #endif /* _WIN32 */
373 
374 WINPR_PRAGMA_DIAG_POP
375 
376 #endif /* WINPR_WINSOCK_H */