3 * @copy 2012 MinGW.org project
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:
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
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.
26 #pragma GCC system_header
29 * TCP/IP specific extensions in Windows Sockets 2
31 * Portions Copyright (c) 1980, 1983, 1988, 1993
32 * The Regents of the University of California. All rights reserved.
36 #if (defined _WINSOCK_H && !defined _WINSOCK2_H)
37 #error "ws2tcpip.h is not compatible with winsock.h. Include winsock2.h instead."
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.
54 * See also: msdn kb article Q257460
55 * http://support.microsoft.com/support/kb/articles/Q257/4/60.asp
58 /* This is also defined in winsock.h; value hasn't changed */
63 * These are also be defined in winsock.h,
64 * but values have changed for WinSock2 interface
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
81 * As with BSD implementation, IPPROTO_IPV6 level socket options have
82 * same values as IPv4 counterparts.
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
94 #define IP_DEFAULT_MULTICAST_TTL 1
95 #define IP_DEFAULT_MULTICAST_LOOP 1
96 #define IP_MAX_MEMBERSHIPS 20
98 #define TCP_EXPEDITED_1122 2
100 #define UDP_NOCHECKSUM 1
102 /* INTERFACE_INFO iiFlags */
104 #define IFF_BROADCAST 2
105 #define IFF_LOOPBACK 4
106 #define IFF_POINTTOPOINT 8
107 #define IFF_MULTICAST 16
109 #define SIO_GET_INTERFACE_LIST _IOR('t', 127, u_long)
111 #define INET_ADDRSTRLEN 16
112 #define INET6_ADDRSTRLEN 46
114 /* getnameinfo constants */
115 #define NI_MAXHOST 1025
116 #define NI_MAXSERV 32
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
124 /* getaddrinfo constants */
126 #define AI_CANONNAME 2
127 #define AI_NUMERICHOST 4
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
141 * ip_mreq also in winsock.h for WinSock1.1,
142 * but online msdn docs say it is defined here for WinSock2.
146 struct in_addr imr_multiaddr;
147 struct in_addr imr_interface;
150 struct ip_mreq_source {
151 struct in_addr imr_multiaddr;
152 struct in_addr imr_sourceaddr;
153 struct in_addr imr_interface;
157 struct in_addr imsf_multiaddr;
158 struct in_addr imsf_interface;
161 struct in_addr imsf_slist[1];
164 #define IP_MSFILTER_SIZE(numsrc) \
165 (sizeof(struct ip_msfilter) - sizeof(struct in_addr) \
166 + (numsrc) * sizeof(struct in_addr))
172 typedef struct in_pktinfo IN_PKTINFO;
176 /* These require XP or .NET Server or use of add-on IPv6 stacks on NT 4
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 */
192 /* s6_addr is the standard name */
193 #define s6_addr _S6_un._S6_u8
195 /* These are GLIBC names */
196 #define s6_addr16 _S6_un._S6_u16
197 #define s6_addr32 _S6_un._S6_u32
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
204 typedef struct in6_addr IN6_ADDR, *PIN6_ADDR, *LPIN6_ADDR;
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 */
213 typedef struct sockaddr_in6 SOCKADDR_IN6, *PSOCKADDR_IN6, *LPSOCKADDR_IN6;
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 }
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)
227 /* Address Testing Macros
229 These macro functions all take const struct in6_addr* as arg.
230 Static inlines would allow type checking, but RFC 2553 says they
232 NB: These are written specifically for little endian host */
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))
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)) */
247 #define IN6_IS_ADDR_MULTICAST(_addr) (((const u_char *) (_addr))[0] == 0xff)
249 #define IN6_IS_ADDR_LINKLOCAL(_addr) \
250 ( (((const u_char *)(_addr))[0] == 0xfe) \
251 && ((((const u_char *)(_addr))[1] & 0xc0) == 0x80))
253 #define IN6_IS_ADDR_SITELOCAL(_addr) \
254 ( (((const u_char *)(_addr))[0] == 0xfe) \
255 && ((((const u_char *)(_addr))[1] & 0xc0) == 0xc0))
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))) */
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 ) */
272 #define IN6_IS_ADDR_MC_NODELOCAL(_addr) \
273 ( IN6_IS_ADDR_MULTICAST(_addr) \
274 && ((((const u_char *)(_addr))[1] & 0xf) == 0x1))
276 #define IN6_IS_ADDR_MC_LINKLOCAL(_addr) \
277 ( IN6_IS_ADDR_MULTICAST (_addr) \
278 && ((((const u_char *)(_addr))[1] & 0xf) == 0x2))
280 #define IN6_IS_ADDR_MC_SITELOCAL(_addr) \
281 ( IN6_IS_ADDR_MULTICAST(_addr) \
282 && ((((const u_char *)(_addr))[1] & 0xf) == 0x5))
284 #define IN6_IS_ADDR_MC_ORGLOCAL(_addr) \
285 ( IN6_IS_ADDR_MULTICAST(_addr) \
286 && ((((const u_char *)(_addr))[1] & 0xf) == 0x8))
288 #define IN6_IS_ADDR_MC_GLOBAL(_addr) \
289 ( IN6_IS_ADDR_MULTICAST(_addr) \
290 && ((((const u_char *)(_addr))[1] & 0xf) == 0xe))
293 typedef int socklen_t;
296 struct in6_addr ipv6mr_multiaddr;
297 unsigned int ipv6mr_interface;
299 typedef struct ipv6_mreq IPV6_MREQ;
305 typedef struct in6_pktinfo IN6_PKTINFO;
314 struct sockaddr *ai_addr;
315 struct addrinfo *ai_next;
318 #if (_WIN32_WINNT >= 0x0501)
319 void WSAAPI freeaddrinfo (struct addrinfo*);
320 int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
322 int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
325 /* FIXME: Need WS protocol-independent API helpers. */
328 static __inline char*
329 gai_strerrorA(int ecode)
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);
339 static __inline WCHAR*
340 gai_strerrorW(int ecode)
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);
351 #define gai_strerror gai_strerrorW
353 #define gai_strerror gai_strerrorA
356 /* Some older IPv4/IPv6 compatibility stuff */
358 /* This struct lacks sin6_scope_id; retained for use in sockaddr_gen */
359 struct sockaddr_in6_old {
362 u_long sin6_flowinfo;
363 struct in6_addr sin6_addr;
366 typedef union sockaddr_gen{
367 struct sockaddr Address;
368 struct sockaddr_in AddressIn;
369 struct sockaddr_in6_old AddressIn6;
373 typedef struct _INTERFACE_INFO {
375 sockaddr_gen iiAddress;
376 sockaddr_gen iiBroadcastAddress;
377 sockaddr_gen iiNetmask;
378 } INTERFACE_INFO, *LPINTERFACE_INFO;
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.
387 The old definition causes problems on newer NT and on XP.
389 typedef struct _OLD_INTERFACE_INFO {
391 struct sockaddr iiAddress;
392 struct sockaddr iiBroadcastAddress;
393 struct sockaddr iiNetmask;
394 } OLD_INTERFACE_INFO;