Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

770 lines
22 KiB

  1. /*
  2. ** WS2TCPIP.H - WinSock2 Extension for TCP/IP protocols
  3. **
  4. ** This file contains TCP/IP specific information for use
  5. ** by WinSock2 compatible applications.
  6. **
  7. ** Copyright (c) Microsoft Corporation. All rights reserved.
  8. **
  9. ** To provide the backward compatibility, all the TCP/IP
  10. ** specific definitions that were included in the WINSOCK.H
  11. ** file are now included in WINSOCK2.H file. WS2TCPIP.H
  12. ** file includes only the definitions introduced in the
  13. ** "WinSock 2 Protocol-Specific Annex" document.
  14. **
  15. ** Rev 0.3 Nov 13, 1995
  16. ** Rev 0.4 Dec 15, 1996
  17. */
  18. #ifndef _WS2TCPIP_H_
  19. #define _WS2TCPIP_H_
  20. #if _MSC_VER > 1000
  21. #pragma once
  22. #endif
  23. /* Argument structure for IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP */
  24. struct ip_mreq {
  25. struct in_addr imr_multiaddr; /* IP multicast address of group */
  26. struct in_addr imr_interface; /* local IP address of interface */
  27. };
  28. /* Argument structure for IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
  29. * IP_BLOCK_SOURCE, and IP_UNBLOCK_SOURCE
  30. */
  31. struct ip_mreq_source {
  32. struct in_addr imr_multiaddr; /* IP multicast address of group */
  33. struct in_addr imr_sourceaddr; /* IP address of source */
  34. struct in_addr imr_interface; /* local IP address of interface */
  35. };
  36. /* Argument structure for SIO_{GET,SET}_MULTICAST_FILTER */
  37. struct ip_msfilter {
  38. struct in_addr imsf_multiaddr; /* IP multicast address of group */
  39. struct in_addr imsf_interface; /* local IP address of interface */
  40. u_long imsf_fmode; /* filter mode - INCLUDE or EXCLUDE */
  41. u_long imsf_numsrc; /* number of sources in src_list */
  42. struct in_addr imsf_slist[1];
  43. };
  44. #define IP_MSFILTER_SIZE(numsrc) \
  45. (sizeof(struct ip_msfilter)-sizeof(struct in_addr) + (numsrc)*sizeof(struct in_addr))
  46. #define MCAST_INCLUDE 0
  47. #define MCAST_EXCLUDE 1
  48. /* TCP/IP specific Ioctl codes */
  49. #define SIO_GET_INTERFACE_LIST _IOR('t', 127, u_long)
  50. /* New IOCTL with address size independent address array */
  51. #define SIO_GET_INTERFACE_LIST_EX _IOR('t', 126, u_long)
  52. #define SIO_SET_MULTICAST_FILTER _IOW('t', 125, u_long)
  53. #define SIO_GET_MULTICAST_FILTER _IOW('t', 124 | IOC_IN, u_long)
  54. /* Option to use with [gs]etsockopt at the IPPROTO_IP level */
  55. #define IP_OPTIONS 1 /* set/get IP options */
  56. #define IP_HDRINCL 2 /* header is included with data */
  57. #define IP_TOS 3 /* IP type of service and preced*/
  58. #define IP_TTL 4 /* IP time to live */
  59. #define IP_MULTICAST_IF 9 /* set/get IP multicast i/f */
  60. #define IP_MULTICAST_TTL 10 /* set/get IP multicast ttl */
  61. #define IP_MULTICAST_LOOP 11 /*set/get IP multicast loopback */
  62. #define IP_ADD_MEMBERSHIP 12 /* add an IP group membership */
  63. #define IP_DROP_MEMBERSHIP 13/* drop an IP group membership */
  64. #define IP_DONTFRAGMENT 14 /* don't fragment IP datagrams */
  65. #define IP_ADD_SOURCE_MEMBERSHIP 15 /* join IP group/source */
  66. #define IP_DROP_SOURCE_MEMBERSHIP 16 /* leave IP group/source */
  67. #define IP_BLOCK_SOURCE 17 /* block IP group/source */
  68. #define IP_UNBLOCK_SOURCE 18 /* unblock IP group/source */
  69. #define IP_PKTINFO 19 /* receive packet information for ipv4*/
  70. #define IP_RECEIVE_BROADCAST 22 /* allow/block broadcast reception */
  71. /* Option to use with [gs]etsockopt at the IPPROTO_IPV6 level */
  72. #define IPV6_HDRINCL 2 /* Header is included with data */
  73. #define IPV6_UNICAST_HOPS 4 /* Set/get IP unicast hop limit */
  74. #define IPV6_MULTICAST_IF 9 /* Set/get IP multicast interface */
  75. #define IPV6_MULTICAST_HOPS 10 /* Set/get IP multicast ttl */
  76. #define IPV6_MULTICAST_LOOP 11 /* Set/get IP multicast loopback */
  77. #define IPV6_ADD_MEMBERSHIP 12 /* Add an IP group membership */
  78. #define IPV6_DROP_MEMBERSHIP 13 /* Drop an IP group membership */
  79. #define IPV6_JOIN_GROUP IPV6_ADD_MEMBERSHIP
  80. #define IPV6_LEAVE_GROUP IPV6_DROP_MEMBERSHIP
  81. #define IPV6_PKTINFO 19 /* Receive packet information for ipv6 */
  82. #define IPV6_HOPLIMIT 21 /* Receive packet hop limit */
  83. #define IPV6_PROTECTION_LEVEL 23 /* Set/get IPv6 protection level */
  84. /* Values of IPV6_PROTECTION_LEVEL */
  85. #define PROTECTION_LEVEL_UNRESTRICTED 10 /* For peer-to-peer apps */
  86. #define PROTECTION_LEVEL_DEFAULT 20 /* Default level */
  87. #define PROTECTION_LEVEL_RESTRICTED 30 /* For Intranet apps */
  88. /* Option to use with [gs]etsockopt at the IPPROTO_UDP level */
  89. #define UDP_NOCHECKSUM 1
  90. #define UDP_CHECKSUM_COVERAGE 20 /* Set/get UDP-Lite checksum coverage */
  91. /* Option to use with [gs]etsockopt at the IPPROTO_TCP level */
  92. #define TCP_EXPEDITED_1122 0x0002
  93. /* IPv6 definitions */
  94. #ifndef s6_addr
  95. struct in6_addr {
  96. union {
  97. u_char Byte[16];
  98. u_short Word[8];
  99. } u;
  100. };
  101. #define in_addr6 in6_addr
  102. /*
  103. ** Defines to match RFC 2553.
  104. */
  105. #define _S6_un u
  106. #define _S6_u8 Byte
  107. #define s6_addr _S6_un._S6_u8
  108. /*
  109. ** Defines for our implementation.
  110. */
  111. #define s6_bytes u.Byte
  112. #define s6_words u.Word
  113. #endif
  114. /* Argument structure for IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP */
  115. typedef struct ipv6_mreq {
  116. struct in6_addr ipv6mr_multiaddr; /* IPv6 multicast address */
  117. unsigned int ipv6mr_interface; /* Interface index */
  118. } IPV6_MREQ;
  119. /* Old IPv6 socket address structure (retained for sockaddr_gen definition below) */
  120. struct sockaddr_in6_old {
  121. short sin6_family; /* AF_INET6 */
  122. u_short sin6_port; /* Transport level port number */
  123. u_long sin6_flowinfo; /* IPv6 flow information */
  124. struct in6_addr sin6_addr; /* IPv6 address */
  125. };
  126. /* IPv6 socket address structure, RFC 2553 */
  127. struct sockaddr_in6 {
  128. short sin6_family; /* AF_INET6 */
  129. u_short sin6_port; /* Transport level port number */
  130. u_long sin6_flowinfo; /* IPv6 flow information */
  131. struct in6_addr sin6_addr; /* IPv6 address */
  132. u_long sin6_scope_id; /* set of interfaces for a scope */
  133. };
  134. typedef struct in6_addr IN6_ADDR;
  135. typedef struct in6_addr *PIN6_ADDR;
  136. typedef struct in6_addr FAR *LPIN6_ADDR;
  137. typedef struct sockaddr_in6 SOCKADDR_IN6;
  138. typedef struct sockaddr_in6 *PSOCKADDR_IN6;
  139. typedef struct sockaddr_in6 FAR *LPSOCKADDR_IN6;
  140. /* Macro that works for both IPv4 and IPv6 */
  141. #define SS_PORT(ssp) (((struct sockaddr_in*)(ssp))->sin_port)
  142. #define IN6ADDR_ANY_INIT { 0 }
  143. #define IN6ADDR_LOOPBACK_INIT { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 }
  144. #ifdef __cplusplus
  145. extern "C" {
  146. #endif
  147. extern const struct in6_addr in6addr_any;
  148. extern const struct in6_addr in6addr_loopback;
  149. #ifdef __cplusplus
  150. }
  151. #endif
  152. #ifdef _MSC_VER
  153. #define WS2TCPIP_INLINE __inline
  154. #else
  155. #define WS2TCPIP_INLINE extern inline /* GNU style */
  156. #endif
  157. WS2TCPIP_INLINE int
  158. IN6_ADDR_EQUAL(const struct in6_addr *a, const struct in6_addr *b)
  159. {
  160. return (memcmp(a, b, sizeof(struct in6_addr)) == 0);
  161. }
  162. WS2TCPIP_INLINE int
  163. IN6_IS_ADDR_UNSPECIFIED(const struct in6_addr *a)
  164. {
  165. return ((a->s6_words[0] == 0) &&
  166. (a->s6_words[1] == 0) &&
  167. (a->s6_words[2] == 0) &&
  168. (a->s6_words[3] == 0) &&
  169. (a->s6_words[4] == 0) &&
  170. (a->s6_words[5] == 0) &&
  171. (a->s6_words[6] == 0) &&
  172. (a->s6_words[7] == 0));
  173. }
  174. WS2TCPIP_INLINE int
  175. IN6_IS_ADDR_LOOPBACK(const struct in6_addr *a)
  176. {
  177. return ((a->s6_words[0] == 0) &&
  178. (a->s6_words[1] == 0) &&
  179. (a->s6_words[2] == 0) &&
  180. (a->s6_words[3] == 0) &&
  181. (a->s6_words[4] == 0) &&
  182. (a->s6_words[5] == 0) &&
  183. (a->s6_words[6] == 0) &&
  184. (a->s6_words[7] == 0x0100));
  185. }
  186. WS2TCPIP_INLINE int
  187. IN6_IS_ADDR_MULTICAST(const struct in6_addr *a)
  188. {
  189. return (a->s6_bytes[0] == 0xff);
  190. }
  191. WS2TCPIP_INLINE int
  192. IN6_IS_ADDR_LINKLOCAL(const struct in6_addr *a)
  193. {
  194. return ((a->s6_bytes[0] == 0xfe) &&
  195. ((a->s6_bytes[1] & 0xc0) == 0x80));
  196. }
  197. WS2TCPIP_INLINE int
  198. IN6_IS_ADDR_SITELOCAL(const struct in6_addr *a)
  199. {
  200. return ((a->s6_bytes[0] == 0xfe) &&
  201. ((a->s6_bytes[1] & 0xc0) == 0xc0));
  202. }
  203. WS2TCPIP_INLINE int
  204. IN6_IS_ADDR_V4MAPPED(const struct in6_addr *a)
  205. {
  206. return ((a->s6_words[0] == 0) &&
  207. (a->s6_words[1] == 0) &&
  208. (a->s6_words[2] == 0) &&
  209. (a->s6_words[3] == 0) &&
  210. (a->s6_words[4] == 0) &&
  211. (a->s6_words[5] == 0xffff));
  212. }
  213. WS2TCPIP_INLINE int
  214. IN6_IS_ADDR_V4COMPAT(const struct in6_addr *a)
  215. {
  216. return ((a->s6_words[0] == 0) &&
  217. (a->s6_words[1] == 0) &&
  218. (a->s6_words[2] == 0) &&
  219. (a->s6_words[3] == 0) &&
  220. (a->s6_words[4] == 0) &&
  221. (a->s6_words[5] == 0) &&
  222. !((a->s6_words[6] == 0) &&
  223. (a->s6_addr[14] == 0) &&
  224. ((a->s6_addr[15] == 0) || (a->s6_addr[15] == 1))));
  225. }
  226. WS2TCPIP_INLINE int
  227. IN6_IS_ADDR_MC_NODELOCAL(const struct in6_addr *a)
  228. {
  229. return IN6_IS_ADDR_MULTICAST(a) && ((a->s6_bytes[1] & 0xf) == 1);
  230. }
  231. WS2TCPIP_INLINE int
  232. IN6_IS_ADDR_MC_LINKLOCAL(const struct in6_addr *a)
  233. {
  234. return IN6_IS_ADDR_MULTICAST(a) && ((a->s6_bytes[1] & 0xf) == 2);
  235. }
  236. WS2TCPIP_INLINE int
  237. IN6_IS_ADDR_MC_SITELOCAL(const struct in6_addr *a)
  238. {
  239. return IN6_IS_ADDR_MULTICAST(a) && ((a->s6_bytes[1] & 0xf) == 5);
  240. }
  241. WS2TCPIP_INLINE int
  242. IN6_IS_ADDR_MC_ORGLOCAL(const struct in6_addr *a)
  243. {
  244. return IN6_IS_ADDR_MULTICAST(a) && ((a->s6_bytes[1] & 0xf) == 8);
  245. }
  246. WS2TCPIP_INLINE int
  247. IN6_IS_ADDR_MC_GLOBAL(const struct in6_addr *a)
  248. {
  249. return IN6_IS_ADDR_MULTICAST(a) && ((a->s6_bytes[1] & 0xf) == 0xe);
  250. }
  251. WS2TCPIP_INLINE int
  252. IN6ADDR_ISANY(const struct sockaddr_in6 *a)
  253. {
  254. return ((a->sin6_family == AF_INET6) &&
  255. IN6_IS_ADDR_UNSPECIFIED(&a->sin6_addr));
  256. }
  257. WS2TCPIP_INLINE int
  258. IN6ADDR_ISLOOPBACK(const struct sockaddr_in6 *a)
  259. {
  260. return ((a->sin6_family == AF_INET6) &&
  261. IN6_IS_ADDR_LOOPBACK(&a->sin6_addr));
  262. }
  263. WS2TCPIP_INLINE void
  264. IN6_SET_ADDR_UNSPECIFIED(struct in6_addr *a)
  265. {
  266. memset(a->s6_bytes, 0, sizeof(struct in6_addr));
  267. }
  268. WS2TCPIP_INLINE void
  269. IN6_SET_ADDR_LOOPBACK(struct in6_addr *a)
  270. {
  271. memset(a->s6_bytes, 0, sizeof(struct in6_addr));
  272. a->s6_bytes[15] = 1;
  273. }
  274. WS2TCPIP_INLINE void
  275. IN6ADDR_SETANY(struct sockaddr_in6 *a)
  276. {
  277. a->sin6_family = AF_INET6;
  278. a->sin6_port = 0;
  279. a->sin6_flowinfo = 0;
  280. IN6_SET_ADDR_UNSPECIFIED(&a->sin6_addr);
  281. a->sin6_scope_id = 0;
  282. }
  283. WS2TCPIP_INLINE void
  284. IN6ADDR_SETLOOPBACK(struct sockaddr_in6 *a)
  285. {
  286. a->sin6_family = AF_INET6;
  287. a->sin6_port = 0;
  288. a->sin6_flowinfo = 0;
  289. IN6_SET_ADDR_LOOPBACK(&a->sin6_addr);
  290. a->sin6_scope_id = 0;
  291. }
  292. typedef union sockaddr_gen{
  293. struct sockaddr Address;
  294. struct sockaddr_in AddressIn;
  295. struct sockaddr_in6_old AddressIn6;
  296. } sockaddr_gen;
  297. /* Structure to keep interface specific information */
  298. typedef struct _INTERFACE_INFO
  299. {
  300. u_long iiFlags; /* Interface flags */
  301. sockaddr_gen iiAddress; /* Interface address */
  302. sockaddr_gen iiBroadcastAddress; /* Broadcast address */
  303. sockaddr_gen iiNetmask; /* Network mask */
  304. } INTERFACE_INFO, FAR * LPINTERFACE_INFO;
  305. /* New structure that does not have dependency on the address size */
  306. typedef struct _INTERFACE_INFO_EX
  307. {
  308. u_long iiFlags; /* Interface flags */
  309. SOCKET_ADDRESS iiAddress; /* Interface address */
  310. SOCKET_ADDRESS iiBroadcastAddress; /* Broadcast address */
  311. SOCKET_ADDRESS iiNetmask; /* Network mask */
  312. } INTERFACE_INFO_EX, FAR * LPINTERFACE_INFO_EX;
  313. /* Possible flags for the iiFlags - bitmask */
  314. #define IFF_UP 0x00000001 /* Interface is up */
  315. #define IFF_BROADCAST 0x00000002 /* Broadcast is supported */
  316. #define IFF_LOOPBACK 0x00000004 /* this is loopback interface */
  317. #define IFF_POINTTOPOINT 0x00000008 /*this is point-to-point interface*/
  318. #define IFF_MULTICAST 0x00000010 /* multicast is supported */
  319. // structure for IP_PKTINFO option
  320. //
  321. typedef struct in_pktinfo {
  322. IN_ADDR ipi_addr; // destination IPv4 address
  323. UINT ipi_ifindex; // received interface index
  324. } IN_PKTINFO;
  325. C_ASSERT(sizeof(IN_PKTINFO) == 8);
  326. // structure for IPV6_PKTINFO option
  327. //
  328. typedef struct in6_pktinfo {
  329. IN6_ADDR ipi6_addr; // destination IPv6 address
  330. UINT ipi6_ifindex; // received interface index
  331. } IN6_PKTINFO;
  332. C_ASSERT(sizeof(IN6_PKTINFO) == 20);
  333. /* Error codes from getaddrinfo() */
  334. #define EAI_AGAIN WSATRY_AGAIN
  335. #define EAI_BADFLAGS WSAEINVAL
  336. #define EAI_FAIL WSANO_RECOVERY
  337. #define EAI_FAMILY WSAEAFNOSUPPORT
  338. #define EAI_MEMORY WSA_NOT_ENOUGH_MEMORY
  339. //#define EAI_NODATA WSANO_DATA
  340. #define EAI_NONAME WSAHOST_NOT_FOUND
  341. #define EAI_SERVICE WSATYPE_NOT_FOUND
  342. #define EAI_SOCKTYPE WSAESOCKTNOSUPPORT
  343. //
  344. // DCR_FIX: EAI_NODATA remove or fix
  345. //
  346. // EAI_NODATA was removed from rfc2553bis
  347. // need to find out from the authors why and
  348. // determine the error for "no records of this type"
  349. // temporarily, we'll keep #define to avoid changing
  350. // code that could change back; use NONAME
  351. //
  352. #define EAI_NODATA EAI_NONAME
  353. /* Structure used in getaddrinfo() call */
  354. typedef struct addrinfo
  355. {
  356. int ai_flags; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
  357. int ai_family; // PF_xxx
  358. int ai_socktype; // SOCK_xxx
  359. int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6
  360. size_t ai_addrlen; // Length of ai_addr
  361. char * ai_canonname; // Canonical name for nodename
  362. struct sockaddr * ai_addr; // Binary address
  363. struct addrinfo * ai_next; // Next structure in linked list
  364. }
  365. ADDRINFOA, *PADDRINFOA;
  366. typedef struct addrinfoW
  367. {
  368. int ai_flags; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
  369. int ai_family; // PF_xxx
  370. int ai_socktype; // SOCK_xxx
  371. int ai_protocol; // 0 or IPPROTO_xxx for IPv4 and IPv6
  372. size_t ai_addrlen; // Length of ai_addr
  373. PWSTR ai_canonname; // Canonical name for nodename
  374. struct sockaddr * ai_addr; // Binary address
  375. struct addrinfoW * ai_next; // Next structure in linked list
  376. }
  377. ADDRINFOW, *PADDRINFOW;
  378. // Switchable definition for GetAddrInfo()
  379. #ifdef UNICODE
  380. typedef ADDRINFOW ADDRINFOT, *PADDRINFOT;
  381. #else
  382. typedef ADDRINFOA ADDRINFOT, *PADDRINFOT;
  383. #endif
  384. // RFC standard definition for getaddrinfo()
  385. typedef ADDRINFOA ADDRINFO, FAR * LPADDRINFO;
  386. /* Flags used in "hints" argument to getaddrinfo() */
  387. #define AI_PASSIVE 0x1 /* Socket address will be used in bind() call */
  388. #define AI_CANONNAME 0x2 /* Return canonical name in first ai_canonname */
  389. #define AI_NUMERICHOST 0x4 /* Nodename must be a numeric address string */
  390. #ifdef __cplusplus
  391. extern "C" {
  392. #endif
  393. WINSOCK_API_LINKAGE
  394. int
  395. WSAAPI
  396. getaddrinfo(
  397. IN const char FAR * nodename,
  398. IN const char FAR * servname,
  399. IN const struct addrinfo FAR * hints,
  400. OUT struct addrinfo FAR * FAR * res
  401. );
  402. #if (_WIN32_WINNT >= 0x0502)
  403. WINSOCK_API_LINKAGE
  404. int
  405. WSAAPI
  406. GetAddrInfoW(
  407. IN PCWSTR pNodeName,
  408. IN PCWSTR pServiceName,
  409. IN const ADDRINFOW * pHints,
  410. OUT PADDRINFOW * ppResult
  411. );
  412. #define GetAddrInfoA getaddrinfo
  413. #ifdef UNICODE
  414. #define GetAddrInfo GetAddrInfoW
  415. #else
  416. #define GetAddrInfo GetAddrInfoA
  417. #endif
  418. #endif
  419. #if INCL_WINSOCK_API_TYPEDEFS
  420. typedef
  421. int
  422. (WSAAPI * LPFN_GETADDRINFO)(
  423. IN const char FAR * nodename,
  424. IN const char FAR * servname,
  425. IN const struct addrinfo FAR * hints,
  426. OUT struct addrinfo FAR * FAR * res
  427. );
  428. typedef
  429. int
  430. (WSAAPI * LPFN_GETADDRINFOW)(
  431. IN PCWSTR pNodeName,
  432. IN PCWSTR pServiceName,
  433. IN const ADDRINFOW * pHints,
  434. OUT PADDRINFOW * ppResult
  435. );
  436. #define LPFN_GETADDRINFOA LPFN_GETADDRINFO
  437. #ifdef UNICODE
  438. #define LPFN_GETADDRINFOT LPFN_GETADDRINFOW
  439. #else
  440. #define LPFN_GETADDRINFOT LPFN_GETADDRINFOA
  441. #endif
  442. #endif
  443. WINSOCK_API_LINKAGE
  444. void
  445. WSAAPI
  446. freeaddrinfo(
  447. IN LPADDRINFO pAddrInfo
  448. );
  449. #if (_WIN32_WINNT >= 0x0502)
  450. WINSOCK_API_LINKAGE
  451. void
  452. WSAAPI
  453. FreeAddrInfoW(
  454. IN PADDRINFOW pAddrInfo
  455. );
  456. #define FreeAddrInfoA freeaddrinfo
  457. #ifdef UNICODE
  458. #define FreeAddrInfo FreeAddrInfoW
  459. #else
  460. #define FreeAddrInfo FreeAddrInfoA
  461. #endif
  462. #endif
  463. #if INCL_WINSOCK_API_TYPEDEFS
  464. typedef
  465. void
  466. (WSAAPI * LPFN_FREEADDRINFO)(
  467. IN struct addrinfo FAR * ai
  468. );
  469. typedef
  470. void
  471. (WSAAPI * LPFN_FREEADDRINFOW)(
  472. IN PADDRINFOW * pAddrInfo
  473. );
  474. #define LPFN_FREEADDRINFOA LPFN_FREEADDRINFO
  475. #ifdef UNICODE
  476. #define LPFN_FREEADDRINFOT LPFN_FREEADDRINFOW
  477. #else
  478. #define LPFN_FREEADDRINFOT LPFN_FREEADDRINFOA
  479. #endif
  480. #endif
  481. typedef int socklen_t;
  482. WINSOCK_API_LINKAGE
  483. int
  484. WSAAPI
  485. getnameinfo(
  486. IN const struct sockaddr FAR * sa,
  487. IN socklen_t salen,
  488. OUT char FAR * host,
  489. IN DWORD hostlen,
  490. OUT char FAR * serv,
  491. IN DWORD servlen,
  492. IN int flags
  493. );
  494. #if (_WIN32_WINNT >= 0x0502)
  495. WINSOCK_API_LINKAGE
  496. INT
  497. WSAAPI
  498. GetNameInfoW(
  499. IN const SOCKADDR * pSockaddr,
  500. IN socklen_t SockaddrLength,
  501. OUT PWCHAR pNodeBuffer,
  502. IN DWORD NodeBufferSize,
  503. OUT PWCHAR pServiceBuffer,
  504. IN DWORD ServiceBufferSize,
  505. IN INT Flags
  506. );
  507. #define GetNameInfoA getnameinfo
  508. #ifdef UNICODE
  509. #define GetNameInfo GetNameInfoW
  510. #else
  511. #define GetNameInfo GetNameInfoA
  512. #endif
  513. #endif
  514. #if INCL_WINSOCK_API_TYPEDEFS
  515. typedef
  516. int
  517. (WSAAPI * LPFN_GETNAMEINFO)(
  518. IN const struct sockaddr FAR * sa,
  519. IN socklen_t salen,
  520. OUT char FAR * host,
  521. IN DWORD hostlen,
  522. OUT char FAR * serv,
  523. IN DWORD servlen,
  524. IN int flags
  525. );
  526. typedef
  527. INT
  528. (WSAAPI * LPFN_GETNAMEINFOW)(
  529. IN const SOCKADDR * pSockaddr,
  530. IN socklen_t SockaddrLength,
  531. OUT PWCHAR pNodeBuffer,
  532. IN DWORD NodeBufferSize,
  533. OUT PWCHAR pServiceBuffer,
  534. IN DWORD ServiceBufferSize,
  535. IN INT Flags
  536. );
  537. #define LPFN_GETNAMEINFOA LPFN_GETNAMEINFO
  538. #ifdef UNICODE
  539. #define LPFN_GETNAMEINFOT LPFN_GETNAMEINFOW
  540. #else
  541. #define LPFN_GETNAMEINFOT LPFN_GETNAMEINFOA
  542. #endif
  543. #endif
  544. #if INCL_WINSOCK_API_PROTOTYPES
  545. #ifdef UNICODE
  546. #define gai_strerror gai_strerrorW
  547. #else
  548. #define gai_strerror gai_strerrorA
  549. #endif /* UNICODE */
  550. // WARNING: The gai_strerror inline functions below use static buffers,
  551. // and hence are not thread-safe. We'll use buffers long enough to hold
  552. // 1k characters. Any system error messages longer than this will be
  553. // returned as empty strings. However 1k should work for the error codes
  554. // used by getaddrinfo().
  555. #define GAI_STRERROR_BUFFER_SIZE 1024
  556. WS2TCPIP_INLINE
  557. char *
  558. gai_strerrorA(
  559. IN int ecode)
  560. {
  561. DWORD dwMsgLen;
  562. static char buff[GAI_STRERROR_BUFFER_SIZE + 1];
  563. dwMsgLen = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
  564. |FORMAT_MESSAGE_IGNORE_INSERTS
  565. |FORMAT_MESSAGE_MAX_WIDTH_MASK,
  566. NULL,
  567. ecode,
  568. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  569. (LPSTR)buff,
  570. GAI_STRERROR_BUFFER_SIZE,
  571. NULL);
  572. return buff;
  573. }
  574. WS2TCPIP_INLINE
  575. WCHAR *
  576. gai_strerrorW(
  577. IN int ecode
  578. )
  579. {
  580. DWORD dwMsgLen;
  581. static WCHAR buff[GAI_STRERROR_BUFFER_SIZE + 1];
  582. dwMsgLen = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM
  583. |FORMAT_MESSAGE_IGNORE_INSERTS
  584. |FORMAT_MESSAGE_MAX_WIDTH_MASK,
  585. NULL,
  586. ecode,
  587. MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  588. (LPWSTR)buff,
  589. GAI_STRERROR_BUFFER_SIZE,
  590. NULL);
  591. return buff;
  592. }
  593. #endif /* INCL_WINSOCK_API_PROTOTYPES */
  594. #define NI_MAXHOST 1025 /* Max size of a fully-qualified domain name */
  595. #define NI_MAXSERV 32 /* Max size of a service name */
  596. //
  597. // Maximum length of address literals (potentially including a port number)
  598. // generated by any address-to-string conversion routine. This length can
  599. // be used when declaring buffers used with getnameinfo, WSAAddressToString,
  600. // inet_ntoa, etc. We just provide one define, rather than one per api,
  601. // to avoid confusion.
  602. //
  603. // The totals are derived from the following data:
  604. // 15: IPv4 address
  605. // 45: IPv6 address including embedded IPv4 address
  606. // 11: Scope Id
  607. // 2: Brackets around IPv6 address when port is present
  608. // 6: Port (including colon)
  609. // 1: Terminating null byte
  610. //
  611. #define INET_ADDRSTRLEN 22
  612. #define INET6_ADDRSTRLEN 65
  613. /* Flags for getnameinfo() */
  614. #define NI_NOFQDN 0x01 /* Only return nodename portion for local hosts */
  615. #define NI_NUMERICHOST 0x02 /* Return numeric form of the host's address */
  616. #define NI_NAMEREQD 0x04 /* Error if the host's name not in DNS */
  617. #define NI_NUMERICSERV 0x08 /* Return numeric form of the service (port #) */
  618. #define NI_DGRAM 0x10 /* Service is a datagram service */
  619. #ifdef __cplusplus
  620. }
  621. #endif
  622. //
  623. // Unless the build environment is explicitly targeting only
  624. // platforms that include built-in getaddrinfo() support, include
  625. // the backwards-compatibility version of the relevant APIs.
  626. //
  627. #if !defined(_WIN32_WINNT) || (_WIN32_WINNT <= 0x0500)
  628. #include <wspiapi.h>
  629. #endif
  630. #endif /* _WS2TCPIP_H_ */