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;
189  struct sockaddr_in6_old AddressIn6;
190 } sockaddr_gen;
191 
192 WINPR_PRAGMA_DIAG_PUSH
193 WINPR_PRAGMA_DIAG_IGNORED_RESERVED_ID_MACRO
194 
195 #define _IFF_UP 0x00000001
196 #define _IFF_BROADCAST 0x00000002
197 #define _IFF_LOOPBACK 0x00000004
198 #define _IFF_POINTTOPOINT 0x00000008
199 #define _IFF_MULTICAST 0x00000010
200 
201 WINPR_PRAGMA_DIAG_POP
202 
203 typedef struct
204 {
205  ULONG iiFlags;
206  sockaddr_gen iiAddress;
207  sockaddr_gen iiBroadcastAddress;
208  sockaddr_gen iiNetmask;
211 
212 #define MAX_PROTOCOL_CHAIN 7
213 #define WSAPROTOCOL_LEN 255
214 
215 typedef struct
216 {
217  int ChainLen;
218  DWORD ChainEntries[MAX_PROTOCOL_CHAIN];
220 
221 typedef struct
222 {
223  DWORD dwServiceFlags1;
224  DWORD dwServiceFlags2;
225  DWORD dwServiceFlags3;
226  DWORD dwServiceFlags4;
227  DWORD dwProviderFlags;
228  GUID ProviderId;
229  DWORD dwCatalogEntryId;
230  WSAPROTOCOLCHAIN ProtocolChain;
231  int iVersion;
232  int iAddressFamily;
233  int iMaxSockAddr;
234  int iMinSockAddr;
235  int iSocketType;
236  int iProtocol;
237  int iProtocolMaxOffset;
238  int iNetworkByteOrder;
239  int iSecurityScheme;
240  DWORD dwMessageSize;
241  DWORD dwProviderReserved;
242  CHAR szProtocol[WSAPROTOCOL_LEN + 1];
244 
245 typedef struct
246 {
247  DWORD dwServiceFlags1;
248  DWORD dwServiceFlags2;
249  DWORD dwServiceFlags3;
250  DWORD dwServiceFlags4;
251  DWORD dwProviderFlags;
252  GUID ProviderId;
253  DWORD dwCatalogEntryId;
254  WSAPROTOCOLCHAIN ProtocolChain;
255  int iVersion;
256  int iAddressFamily;
257  int iMaxSockAddr;
258  int iMinSockAddr;
259  int iSocketType;
260  int iProtocol;
261  int iProtocolMaxOffset;
262  int iNetworkByteOrder;
263  int iSecurityScheme;
264  DWORD dwMessageSize;
265  DWORD dwProviderReserved;
266  WCHAR szProtocol[WSAPROTOCOL_LEN + 1];
268 
269 typedef void(CALLBACK* LPWSAOVERLAPPED_COMPLETION_ROUTINE)(DWORD dwError, DWORD cbTransferred,
270  LPWSAOVERLAPPED lpOverlapped,
271  DWORD dwFlags);
272 
273 typedef UINT32 GROUP;
274 #define SG_UNCONSTRAINED_GROUP 0x01
275 #define SG_CONSTRAINED_GROUP 0x02
276 
277 #define SIO_GET_INTERFACE_LIST _IOR('t', 127, ULONG)
278 #define SIO_GET_INTERFACE_LIST_EX _IOR('t', 126, ULONG)
279 #define SIO_SET_MULTICAST_FILTER _IOW('t', 125, ULONG)
280 #define SIO_GET_MULTICAST_FILTER _IOW('t', 124 | IOC_IN, ULONG)
281 #define SIOCSIPMSFILTER SIO_SET_MULTICAST_FILTER
282 #define SIOCGIPMSFILTER SIO_GET_MULTICAST_FILTER
283 
284 #ifdef UNICODE
285 #define WSAPROTOCOL_INFO WSAPROTOCOL_INFOW
286 #define LPWSAPROTOCOL_INFO LPWSAPROTOCOL_INFOW
287 #else
288 #define WSAPROTOCOL_INFO WSAPROTOCOL_INFOA
289 #define LPWSAPROTOCOL_INFO LPWSAPROTOCOL_INFOA
290 #endif
291 
292 #ifdef __cplusplus
293 extern "C"
294 {
295 #endif
296 
297  WINPR_API int WSAStartup(WORD wVersionRequired, LPWSADATA lpWSAData);
298  WINPR_API int WSACleanup(void);
299 
300  WINPR_API void WSASetLastError(int iError);
301  WINPR_API int WSAGetLastError(void);
302 
303  WINPR_API BOOL WSACloseEvent(HANDLE hEvent);
304 
305  WINPR_ATTR_MALLOC(WSACloseEvent, 1)
306  WINPR_API HANDLE WSACreateEvent(void);
307  WINPR_API BOOL WSASetEvent(HANDLE hEvent);
308  WINPR_API BOOL WSAResetEvent(HANDLE hEvent);
309 
310  WINPR_API int WSAEventSelect(SOCKET s, WSAEVENT hEventObject, LONG lNetworkEvents);
311 
312  WINPR_API DWORD WSAWaitForMultipleEvents(DWORD cEvents, const HANDLE* lphEvents, BOOL fWaitAll,
313  DWORD dwTimeout, BOOL fAlertable);
314 
315  WINPR_API SOCKET WSASocketA(int af, int type, int protocol, LPWSAPROTOCOL_INFOA lpProtocolInfo,
316  GROUP g, DWORD dwFlags);
317  WINPR_API SOCKET WSASocketW(int af, int type, int protocol, LPWSAPROTOCOL_INFOW lpProtocolInfo,
318  GROUP g, DWORD dwFlags);
319 
320  WINPR_API int WSAIoctl(SOCKET s, DWORD dwIoControlCode, LPVOID lpvInBuffer, DWORD cbInBuffer,
321  LPVOID lpvOutBuffer, DWORD cbOutBuffer, LPDWORD lpcbBytesReturned,
322  LPWSAOVERLAPPED lpOverlapped,
323  LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine);
324 
325  WINPR_API SOCKET _accept(SOCKET s, struct sockaddr* addr, int* addrlen);
326  WINPR_API int _bind(SOCKET s, const struct sockaddr* addr, int namelen);
327  WINPR_API int closesocket(SOCKET s);
328  WINPR_API int _connect(SOCKET s, const struct sockaddr* name, int namelen);
329  WINPR_API int _ioctlsocket(SOCKET s, long cmd, u_long* argp);
330  WINPR_API int _getpeername(SOCKET s, struct sockaddr* name, int* namelen);
331  WINPR_API int _getsockname(SOCKET s, struct sockaddr* name, int* namelen);
332  WINPR_API int _getsockopt(SOCKET s, int level, int optname, char* optval, int* optlen);
333  WINPR_API u_long _htonl(u_long hostlong);
334  WINPR_API u_short _htons(u_short hostshort);
335  WINPR_API unsigned long _inet_addr(const char* cp);
336  WINPR_API char* _inet_ntoa(struct in_addr in);
337  WINPR_API int _listen(SOCKET s, int backlog);
338  WINPR_API u_long _ntohl(u_long netlong);
339  WINPR_API u_short _ntohs(u_short netshort);
340  WINPR_API int _recv(SOCKET s, char* buf, int len, int flags);
341  WINPR_API int _recvfrom(SOCKET s, char* buf, int len, int flags, struct sockaddr* from,
342  int* fromlen);
343  WINPR_API int _select(int nfds, fd_set* readfds, fd_set* writefds, fd_set* exceptfds,
344  const struct timeval* timeout);
345  WINPR_API int _send(SOCKET s, const char* buf, int len, int flags);
346  WINPR_API int _sendto(SOCKET s, const char* buf, int len, int flags, const struct sockaddr* to,
347  int tolen);
348  WINPR_API int _setsockopt(SOCKET s, int level, int optname, const char* optval, int optlen);
349  WINPR_API int _shutdown(SOCKET s, int how);
350  WINPR_API SOCKET _socket(int af, int type, int protocol);
351  WINPR_API struct hostent* _gethostbyaddr(const char* addr, int len, int type);
352  WINPR_API struct hostent* _gethostbyname(const char* name);
353  WINPR_API int _gethostname(char* name, int namelen);
354  WINPR_API struct servent* _getservbyport(int port, const char* proto);
355  WINPR_API struct servent* _getservbyname(const char* name, const char* proto);
356  WINPR_API struct protoent* _getprotobynumber(int number);
357  WINPR_API struct protoent* _getprotobyname(const char* name);
358 
359 #ifdef __cplusplus
360 }
361 #endif
362 
363 #ifdef UNICODE
364 #define WSASocket WSASocketW
365 #else
366 #define WSASocket WSASocketA
367 #endif
368 
369 #endif /* _WIN32 */
370 
371 WINPR_PRAGMA_DIAG_POP
372 
373 #endif /* WINPR_WINSOCK_H */