OSDN Git Service

Redo the filters based on assumptions discussed in mingw-dvlpr list.
[mingw/mingw-org-wsl.git] / include / ws2tcpip.h
1 /**
2  * @file ws2tcpip.h
3  * @copy 2012 MinGW.org project
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 #ifndef _WS2TCPIP_H
25 #define _WS2TCPIP_H
26 #pragma GCC system_header
27 #include <_mingw.h>
28
29 /*
30  * TCP/IP specific extensions in Windows Sockets 2
31  *
32  * Portions Copyright (c) 1980, 1983, 1988, 1993
33  * The Regents of the University of California.  All rights reserved.
34  *
35  */
36
37 #if (defined _WINSOCK_H && !defined _WINSOCK2_H)
38 #error "ws2tcpip.h is not compatible with winsock.h. Include winsock2.h instead."
39 #endif
40
41 #include <winsock2.h>
42
43 #ifdef  __cplusplus
44 extern "C" {
45 #endif
46
47 /* 
48  * The IP_* macros are also defined in winsock.h, but some values are different there.
49  * The values defined in winsock.h for 1.1 and used in wsock32.dll are consistent
50  * with the original values Steve Deering defined in his document "IP Multicast Extensions
51  * for 4.3BSD UNIX related systems (MULTICAST 1.2 Release)." However, these conflicted with
52  * the definitions for some IPPROTO_IP level socket options already assigned by BSD,
53  * so Berkeley changed all the values by adding 7.  WinSock2 (ws2_32.dll)  uses
54  * the BSD 4.4 compatible values defined here.
55  *
56  * See also: msdn kb article Q257460
57  * http://support.microsoft.com/support/kb/articles/Q257/4/60.asp
58  */
59
60 /* This is also defined in winsock.h; value hasn't changed */
61 #define IP_OPTIONS  1
62
63 #define IP_HDRINCL  2
64 /*
65  * These are also be defined in winsock.h,
66  * but values have changed for WinSock2 interface
67  */
68 #define IP_TOS                  3   /* old (winsock 1.1) value 8 */
69 #define IP_TTL                  4   /* old value 7 */
70 #define IP_MULTICAST_IF         9   /* old value 2 */
71 #define IP_MULTICAST_TTL        10  /* old value 3 */
72 #define IP_MULTICAST_LOOP       11  /* old value 4 */
73 #define IP_ADD_MEMBERSHIP       12  /* old value 5 */
74 #define IP_DROP_MEMBERSHIP      13  /* old value 6 */
75 #define IP_DONTFRAGMENT         14  /* old value 9 */
76 #define IP_ADD_SOURCE_MEMBERSHIP        15
77 #define IP_DROP_SOURCE_MEMBERSHIP       16
78 #define IP_BLOCK_SOURCE                 17
79 #define IP_UNBLOCK_SOURCE               18
80 #define IP_PKTINFO                      19
81
82 /*
83  * As with BSD implementation, IPPROTO_IPV6 level socket options have
84  * same values as IPv4 counterparts.
85  */
86 #define IPV6_UNICAST_HOPS       4
87 #define IPV6_MULTICAST_IF       9
88 #define IPV6_MULTICAST_HOPS     10
89 #define IPV6_MULTICAST_LOOP     11
90 #define IPV6_ADD_MEMBERSHIP     12
91 #define IPV6_DROP_MEMBERSHIP    13
92 #define IPV6_JOIN_GROUP         IPV6_ADD_MEMBERSHIP
93 #define IPV6_LEAVE_GROUP        IPV6_DROP_MEMBERSHIP
94 #define IPV6_PKTINFO            19
95
96 #define IP_DEFAULT_MULTICAST_TTL 1 
97 #define IP_DEFAULT_MULTICAST_LOOP 1 
98 #define IP_MAX_MEMBERSHIPS 20 
99
100 #define TCP_EXPEDITED_1122  2
101
102 #define UDP_NOCHECKSUM 1
103
104 /* INTERFACE_INFO iiFlags */
105 #define IFF_UP  1
106 #define IFF_BROADCAST   2
107 #define IFF_LOOPBACK    4
108 #define IFF_POINTTOPOINT    8
109 #define IFF_MULTICAST   16
110
111 #define SIO_GET_INTERFACE_LIST  _IOR('t', 127, u_long)
112
113 #define INET_ADDRSTRLEN  16
114 #define INET6_ADDRSTRLEN 46
115
116 /* getnameinfo constants */ 
117 #define NI_MAXHOST      1025
118 #define NI_MAXSERV      32
119
120 #define NI_NOFQDN       0x01
121 #define NI_NUMERICHOST  0x02
122 #define NI_NAMEREQD     0x04
123 #define NI_NUMERICSERV  0x08
124 #define NI_DGRAM        0x10
125
126 /* getaddrinfo constants */
127 #define AI_PASSIVE      1
128 #define AI_CANONNAME    2
129 #define AI_NUMERICHOST  4
130
131 /* getaddrinfo error codes */
132 #define EAI_AGAIN       WSATRY_AGAIN
133 #define EAI_BADFLAGS    WSAEINVAL
134 #define EAI_FAIL        WSANO_RECOVERY
135 #define EAI_FAMILY      WSAEAFNOSUPPORT
136 #define EAI_MEMORY      WSA_NOT_ENOUGH_MEMORY
137 #define EAI_NODATA      WSANO_DATA
138 #define EAI_NONAME      WSAHOST_NOT_FOUND
139 #define EAI_SERVICE     WSATYPE_NOT_FOUND
140 #define EAI_SOCKTYPE    WSAESOCKTNOSUPPORT
141
142 /*
143  *   ip_mreq also in winsock.h for WinSock1.1,
144  *   but online msdn docs say it is defined here for WinSock2.
145  */ 
146
147 struct ip_mreq {
148         struct in_addr  imr_multiaddr;
149         struct in_addr  imr_interface;
150 };
151
152 struct ip_mreq_source {
153         struct in_addr  imr_multiaddr;
154         struct in_addr  imr_sourceaddr;
155         struct in_addr  imr_interface;
156 };
157
158 struct ip_msfilter {
159         struct in_addr  imsf_multiaddr;
160         struct in_addr  imsf_interface;
161         u_long          imsf_fmode;
162         u_long          imsf_numsrc;
163         struct in_addr  imsf_slist[1];
164 };
165
166 #define IP_MSFILTER_SIZE(numsrc) \
167    (sizeof(struct ip_msfilter) - sizeof(struct in_addr) \
168    + (numsrc) * sizeof(struct in_addr))
169
170 struct in_pktinfo {
171         IN_ADDR ipi_addr;
172         UINT    ipi_ifindex;
173 };
174 typedef struct in_pktinfo IN_PKTINFO;
175
176
177 /* ipv6 */ 
178 /* These require XP or .NET Server or use of add-on IPv6 stacks on NT 4
179   or higher */
180
181 /* This is based on the example given in RFC 2553 with stdint types
182    changed to BSD types.  For now, use these  field names until there
183    is some consistency in MS docs. In this file, we only use the
184    in6_addr structure start address, with casts to get the right offsets
185    when testing addresses */
186   
187 struct in6_addr {
188     union {
189         u_char  _S6_u8[16];
190         u_short _S6_u16[8];
191         u_long  _S6_u32[4];
192         } _S6_un;
193 };
194 /* s6_addr is the standard name */
195 #define s6_addr         _S6_un._S6_u8
196
197 /* These are GLIBC names */ 
198 #define s6_addr16       _S6_un._S6_u16
199 #define s6_addr32       _S6_un._S6_u32
200
201 /* These are used in some MS code */
202 #define in_addr6        in6_addr
203 #define _s6_bytes       _S6_un._S6_u8
204 #define _s6_words       _S6_un._S6_u16
205
206 typedef struct in6_addr IN6_ADDR,  *PIN6_ADDR, *LPIN6_ADDR;
207
208 struct sockaddr_in6 {
209         short sin6_family;      /* AF_INET6 */
210         u_short sin6_port;      /* transport layer port # */
211         u_long sin6_flowinfo;   /* IPv6 traffic class & flow info */
212         struct in6_addr sin6_addr;  /* IPv6 address */
213         u_long sin6_scope_id;   /* set of interfaces for a scope */
214 };
215 typedef struct sockaddr_in6 SOCKADDR_IN6, *PSOCKADDR_IN6, *LPSOCKADDR_IN6;
216
217 extern const struct in6_addr in6addr_any;
218 extern const struct in6_addr in6addr_loopback;
219 /* the above can get initialised using: */ 
220 #define IN6ADDR_ANY_INIT        { 0 }
221 #define IN6ADDR_LOOPBACK_INIT   { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }
222
223 /* Described in RFC 2292, but not in 2553 */
224 /* int IN6_ARE_ADDR_EQUAL(const struct in6_addr * a, const struct in6_addr * b) */
225 #define IN6_ARE_ADDR_EQUAL(a, b)        \
226     (memcmp ((void*)(a), (void*)(b), sizeof (struct in6_addr)) == 0)
227
228
229 /* Address Testing Macros 
230
231  These macro functions all take const struct in6_addr* as arg.
232  Static inlines would allow type checking, but RFC 2553 says they
233  macros.         
234  NB: These are written specifically for little endian host */
235
236 #define IN6_IS_ADDR_UNSPECIFIED(_addr) \
237         (   (((const u_long *)(_addr))[0] == 0) \
238          && (((const u_long *)(_addr))[1] == 0) \
239          && (((const u_long *)(_addr))[2] == 0) \
240          && (((const u_long *)(_addr))[3] == 0))
241
242 #define IN6_IS_ADDR_LOOPBACK(_addr) \
243         (   (((const u_long *)(_addr))[0] == 0) \
244          && (((const u_long *)(_addr))[1] == 0) \
245          && (((const u_long *)(_addr))[2] == 0) \
246          && (((const u_long *)(_addr))[3] == 0x01000000)) /* Note byte order reversed */
247 /*          (((const u_long *)(_addr))[3] == ntohl(1))  */
248
249 #define IN6_IS_ADDR_MULTICAST(_addr) (((const u_char *) (_addr))[0] == 0xff)
250
251 #define IN6_IS_ADDR_LINKLOCAL(_addr) \
252         (   (((const u_char *)(_addr))[0] == 0xfe)      \
253          && ((((const u_char *)(_addr))[1] & 0xc0) == 0x80))
254
255 #define IN6_IS_ADDR_SITELOCAL(_addr) \
256         (   (((const u_char *)(_addr))[0] == 0xfe)      \
257          && ((((const u_char *)(_addr))[1] & 0xc0) == 0xc0))
258
259 #define IN6_IS_ADDR_V4MAPPED(_addr) \
260         (   (((const u_long *)(_addr))[0] == 0)         \
261          && (((const u_long *)(_addr))[1] == 0)         \
262          && (((const u_long *)(_addr))[2] == 0xffff0000)) /* Note byte order reversed */
263 /*          (((const u_long *)(_addr))[2] == ntohl(0x0000ffff))) */
264
265 #define IN6_IS_ADDR_V4COMPAT(_addr) \
266         (   (((const u_long *)(_addr))[0] == 0)         \
267          && (((const u_long *)(_addr))[1] == 0)         \
268          && (((const u_long *)(_addr))[2] == 0)         \
269          && (((const u_long *)(_addr))[3] != 0)         \
270          && (((const u_long *)(_addr))[3] != 0x01000000)) /* Note byte order reversed */
271 /*           (ntohl (((const u_long *)(_addr))[3]) > 1 ) */
272
273
274 #define IN6_IS_ADDR_MC_NODELOCAL(_addr) \
275         (   IN6_IS_ADDR_MULTICAST(_addr)                \
276          && ((((const u_char *)(_addr))[1] & 0xf) == 0x1)) 
277
278 #define IN6_IS_ADDR_MC_LINKLOCAL(_addr) \
279         (   IN6_IS_ADDR_MULTICAST (_addr)               \
280          && ((((const u_char *)(_addr))[1] & 0xf) == 0x2))
281
282 #define IN6_IS_ADDR_MC_SITELOCAL(_addr) \
283         (   IN6_IS_ADDR_MULTICAST(_addr)                \
284          && ((((const u_char *)(_addr))[1] & 0xf) == 0x5))
285
286 #define IN6_IS_ADDR_MC_ORGLOCAL(_addr)  \
287         (   IN6_IS_ADDR_MULTICAST(_addr)                \
288          && ((((const u_char *)(_addr))[1] & 0xf) == 0x8))
289
290 #define IN6_IS_ADDR_MC_GLOBAL(_addr)    \
291         (   IN6_IS_ADDR_MULTICAST(_addr)        \
292          && ((((const u_char *)(_addr))[1] & 0xf) == 0xe))
293
294
295 typedef int socklen_t;
296
297 struct ipv6_mreq {
298         struct in6_addr ipv6mr_multiaddr;
299         unsigned int    ipv6mr_interface;
300 };
301 typedef struct ipv6_mreq IPV6_MREQ;
302
303 struct in6_pktinfo {
304         IN6_ADDR ipi6_addr;
305         UINT     ipi6_ifindex;
306 };
307 typedef struct  in6_pktinfo IN6_PKTINFO;
308
309 struct addrinfo {
310         int     ai_flags;
311         int     ai_family;
312         int     ai_socktype;
313         int     ai_protocol;
314         size_t  ai_addrlen;
315         char   *ai_canonname;
316         struct sockaddr  *ai_addr;
317         struct addrinfo  *ai_next;
318 };
319
320 static __inline char*
321 gai_strerrorA(int ecode)
322 {
323         static char message[1024+1];
324         DWORD dwFlags = FORMAT_MESSAGE_FROM_SYSTEM
325                       | FORMAT_MESSAGE_IGNORE_INSERTS
326                       | FORMAT_MESSAGE_MAX_WIDTH_MASK;
327         DWORD dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
328         FormatMessageA(dwFlags, NULL, ecode, dwLanguageId, (LPSTR)message, 1024, NULL);
329         return message;
330 }
331 static __inline WCHAR*
332 gai_strerrorW(int ecode)
333 {
334         static WCHAR message[1024+1];
335         DWORD dwFlags = FORMAT_MESSAGE_FROM_SYSTEM
336                       | FORMAT_MESSAGE_IGNORE_INSERTS
337                       | FORMAT_MESSAGE_MAX_WIDTH_MASK;
338         DWORD dwLanguageId = MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT);
339         FormatMessageW(dwFlags, NULL, ecode, dwLanguageId, (LPWSTR)message, 1024, NULL);
340         return message;
341 }
342
343 #define gai_strerror __AW(gai_strerror)
344
345 /* Some older IPv4/IPv6 compatibility stuff */
346
347 /* This struct lacks sin6_scope_id; retained for use in sockaddr_gen */
348 struct sockaddr_in6_old {
349         short   sin6_family;
350         u_short sin6_port;
351         u_long  sin6_flowinfo;
352         struct in6_addr sin6_addr;
353 };
354
355 typedef union sockaddr_gen{
356         struct sockaddr         Address;
357         struct sockaddr_in      AddressIn;
358         struct sockaddr_in6_old AddressIn6;
359 } sockaddr_gen;
360
361
362 typedef struct _INTERFACE_INFO {
363         u_long          iiFlags;
364         sockaddr_gen    iiAddress;
365         sockaddr_gen    iiBroadcastAddress;
366         sockaddr_gen    iiNetmask;
367 } INTERFACE_INFO, *LPINTERFACE_INFO;
368
369 /*
370    The definition above can cause problems on NT4,prior to sp4.
371    To workaround, include the following struct and typedef and
372    #define INTERFACE_INFO OLD_INTERFACE_INFO
373    See: FIX: WSAIoctl SIO_GET_INTERFACE_LIST Option Problem
374    (Q181520) in MSDN KB.
375
376    The old definition causes problems on newer NT and on XP.
377
378 typedef struct _OLD_INTERFACE_INFO {
379         u_long          iiFlags;
380         struct sockaddr iiAddress;
381         struct sockaddr iiBroadcastAddress;
382         struct sockaddr iiNetmask;
383 } OLD_INTERFACE_INFO;
384 */
385
386 #if (_WIN32_WINNT >= _WIN32_WINNT_WINXP)
387 /**
388  * For WIN2K the user includes wspiapi.h for these functions.
389  */
390 void WSAAPI freeaddrinfo (struct addrinfo*);
391 int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
392                         struct addrinfo**);
393 int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
394                        char*,DWORD,int);
395 #endif /* (_WIN32_WINNT >= _WIN32_WINNT_WINXP) */
396
397 #ifdef  __cplusplus
398 }
399 #endif
400
401 #endif