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
30 * TCP/IP specific extensions in Windows Sockets 2
32 * Portions Copyright (c) 1980, 1983, 1988, 1993
33 * The Regents of the University of California. All rights reserved.
37 #if (defined _WINSOCK_H && !defined _WINSOCK2_H)
38 #error "ws2tcpip.h is not compatible with winsock.h. Include winsock2.h instead."
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.
56 * See also: msdn kb article Q257460
57 * http://support.microsoft.com/support/kb/articles/Q257/4/60.asp
60 /* This is also defined in winsock.h; value hasn't changed */
65 * These are also be defined in winsock.h,
66 * but values have changed for WinSock2 interface
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
83 * As with BSD implementation, IPPROTO_IPV6 level socket options have
84 * same values as IPv4 counterparts.
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
96 #define IP_DEFAULT_MULTICAST_TTL 1
97 #define IP_DEFAULT_MULTICAST_LOOP 1
98 #define IP_MAX_MEMBERSHIPS 20
100 #define TCP_EXPEDITED_1122 2
102 #define UDP_NOCHECKSUM 1
104 /* INTERFACE_INFO iiFlags */
106 #define IFF_BROADCAST 2
107 #define IFF_LOOPBACK 4
108 #define IFF_POINTTOPOINT 8
109 #define IFF_MULTICAST 16
111 #define SIO_GET_INTERFACE_LIST _IOR('t', 127, u_long)
113 #define INET_ADDRSTRLEN 16
114 #define INET6_ADDRSTRLEN 46
116 /* getnameinfo constants */
117 #define NI_MAXHOST 1025
118 #define NI_MAXSERV 32
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
126 /* getaddrinfo constants */
128 #define AI_CANONNAME 2
129 #define AI_NUMERICHOST 4
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
143 * ip_mreq also in winsock.h for WinSock1.1,
144 * but online msdn docs say it is defined here for WinSock2.
148 struct in_addr imr_multiaddr;
149 struct in_addr imr_interface;
152 struct ip_mreq_source {
153 struct in_addr imr_multiaddr;
154 struct in_addr imr_sourceaddr;
155 struct in_addr imr_interface;
159 struct in_addr imsf_multiaddr;
160 struct in_addr imsf_interface;
163 struct in_addr imsf_slist[1];
166 #define IP_MSFILTER_SIZE(numsrc) \
167 (sizeof(struct ip_msfilter) - sizeof(struct in_addr) \
168 + (numsrc) * sizeof(struct in_addr))
174 typedef struct in_pktinfo IN_PKTINFO;
178 /* These require XP or .NET Server or use of add-on IPv6 stacks on NT 4
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 */
194 /* s6_addr is the standard name */
195 #define s6_addr _S6_un._S6_u8
197 /* These are GLIBC names */
198 #define s6_addr16 _S6_un._S6_u16
199 #define s6_addr32 _S6_un._S6_u32
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
206 typedef struct in6_addr IN6_ADDR, *PIN6_ADDR, *LPIN6_ADDR;
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 */
215 typedef struct sockaddr_in6 SOCKADDR_IN6, *PSOCKADDR_IN6, *LPSOCKADDR_IN6;
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 }
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)
229 /* Address Testing Macros
231 These macro functions all take const struct in6_addr* as arg.
232 Static inlines would allow type checking, but RFC 2553 says they
234 NB: These are written specifically for little endian host */
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))
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)) */
249 #define IN6_IS_ADDR_MULTICAST(_addr) (((const u_char *) (_addr))[0] == 0xff)
251 #define IN6_IS_ADDR_LINKLOCAL(_addr) \
252 ( (((const u_char *)(_addr))[0] == 0xfe) \
253 && ((((const u_char *)(_addr))[1] & 0xc0) == 0x80))
255 #define IN6_IS_ADDR_SITELOCAL(_addr) \
256 ( (((const u_char *)(_addr))[0] == 0xfe) \
257 && ((((const u_char *)(_addr))[1] & 0xc0) == 0xc0))
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))) */
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 ) */
274 #define IN6_IS_ADDR_MC_NODELOCAL(_addr) \
275 ( IN6_IS_ADDR_MULTICAST(_addr) \
276 && ((((const u_char *)(_addr))[1] & 0xf) == 0x1))
278 #define IN6_IS_ADDR_MC_LINKLOCAL(_addr) \
279 ( IN6_IS_ADDR_MULTICAST (_addr) \
280 && ((((const u_char *)(_addr))[1] & 0xf) == 0x2))
282 #define IN6_IS_ADDR_MC_SITELOCAL(_addr) \
283 ( IN6_IS_ADDR_MULTICAST(_addr) \
284 && ((((const u_char *)(_addr))[1] & 0xf) == 0x5))
286 #define IN6_IS_ADDR_MC_ORGLOCAL(_addr) \
287 ( IN6_IS_ADDR_MULTICAST(_addr) \
288 && ((((const u_char *)(_addr))[1] & 0xf) == 0x8))
290 #define IN6_IS_ADDR_MC_GLOBAL(_addr) \
291 ( IN6_IS_ADDR_MULTICAST(_addr) \
292 && ((((const u_char *)(_addr))[1] & 0xf) == 0xe))
295 typedef int socklen_t;
298 struct in6_addr ipv6mr_multiaddr;
299 unsigned int ipv6mr_interface;
301 typedef struct ipv6_mreq IPV6_MREQ;
307 typedef struct in6_pktinfo IN6_PKTINFO;
316 struct sockaddr *ai_addr;
317 struct addrinfo *ai_next;
320 static __inline char*
321 gai_strerrorA(int ecode)
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);
331 static __inline WCHAR*
332 gai_strerrorW(int ecode)
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);
343 #define gai_strerror __AW(gai_strerror)
345 /* Some older IPv4/IPv6 compatibility stuff */
347 /* This struct lacks sin6_scope_id; retained for use in sockaddr_gen */
348 struct sockaddr_in6_old {
351 u_long sin6_flowinfo;
352 struct in6_addr sin6_addr;
355 typedef union sockaddr_gen{
356 struct sockaddr Address;
357 struct sockaddr_in AddressIn;
358 struct sockaddr_in6_old AddressIn6;
362 typedef struct _INTERFACE_INFO {
364 sockaddr_gen iiAddress;
365 sockaddr_gen iiBroadcastAddress;
366 sockaddr_gen iiNetmask;
367 } INTERFACE_INFO, *LPINTERFACE_INFO;
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.
376 The old definition causes problems on newer NT and on XP.
378 typedef struct _OLD_INTERFACE_INFO {
380 struct sockaddr iiAddress;
381 struct sockaddr iiBroadcastAddress;
382 struct sockaddr iiNetmask;
383 } OLD_INTERFACE_INFO;
386 #if (_WIN32_WINNT >= _WIN32_WINNT_WINXP)
388 * For WIN2K the user includes wspiapi.h for these functions.
390 void WSAAPI freeaddrinfo (struct addrinfo*);
391 int WSAAPI getaddrinfo (const char*,const char*,const struct addrinfo*,
393 int WSAAPI getnameinfo(const struct sockaddr*,socklen_t,char*,DWORD,
395 #endif /* (_WIN32_WINNT >= _WIN32_WINNT_WINXP) */