OSDN Git Service

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