jwaws2tcpip.pas 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  1. {******************************************************************************}
  2. { }
  3. { Winsock2 TCP/IP Extensions API interface Unit for Object Pascal }
  4. { }
  5. { Portions created by Microsoft are Copyright (C) 1995-2001 Microsoft }
  6. { Corporation. All Rights Reserved. }
  7. { }
  8. { The original file is: ws2tcpip.h, released June 2000. The original Pascal }
  9. { code is: WS2tcpip.pas, released December 2000. The initial developer of the }
  10. { Pascal code is Marcel van Brakel (brakelm att chello dott nl). }
  11. { }
  12. { Portions created by Marcel van Brakel are Copyright (C) 1999-2001 }
  13. { Marcel van Brakel. All Rights Reserved. }
  14. { }
  15. { Obtained through: Joint Endeavour of Delphi Innovators (Project JEDI) }
  16. { }
  17. { You may retrieve the latest version of this file at the Project JEDI }
  18. { APILIB home page, located at http://jedi-apilib.sourceforge.net }
  19. { }
  20. { The contents of this file are used with permission, subject to the Mozilla }
  21. { Public License Version 1.1 (the "License"); you may not use this file except }
  22. { in compliance with the License. You may obtain a copy of the License at }
  23. { http://www.mozilla.org/MPL/MPL-1.1.html }
  24. { }
  25. { Software distributed under the License is distributed on an "AS IS" basis, }
  26. { WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for }
  27. { the specific language governing rights and limitations under the License. }
  28. { }
  29. { Alternatively, the contents of this file may be used under the terms of the }
  30. { GNU Lesser General Public License (the "LGPL License"), in which case the }
  31. { provisions of the LGPL License are applicable instead of those above. }
  32. { If you wish to allow use of your version of this file only under the terms }
  33. { of the LGPL License and not to allow others to use your version of this file }
  34. { under the MPL, indicate your decision by deleting the provisions above and }
  35. { replace them with the notice and other provisions required by the LGPL }
  36. { License. If you do not delete the provisions above, a recipient may use }
  37. { your version of this file under either the MPL or the LGPL License. }
  38. { }
  39. { For more information about the LGPL: http://www.gnu.org/copyleft/lesser.html }
  40. { }
  41. {******************************************************************************}
  42. unit JwaWS2tcpip;
  43. {$WEAKPACKAGEUNIT}
  44. {$HPPEMIT ''}
  45. {$HPPEMIT '#include "ws2tcpip.h"'}
  46. {$HPPEMIT ''}
  47. {$I jediapilib.inc}
  48. interface
  49. uses
  50. JwaWinSock2, JwaWinType;
  51. //
  52. // WS2TCPIP.H - WinSock2 Extension for TCP/IP protocols
  53. //
  54. // This file contains TCP/IP specific information for use
  55. // by WinSock2 compatible applications.
  56. //
  57. // Copyright (c) 1995-1999 Microsoft Corporation
  58. //
  59. // To provide the backward compatibility, all the TCP/IP
  60. // specific definitions that were included in the WINSOCK.H
  61. // file are now included in WINSOCK2.H file. WS2TCPIP.H
  62. // file includes only the definitions introduced in the
  63. // "WinSock 2 Protocol-Specific Annex" document.
  64. //
  65. // Rev 0.3 Nov 13, 1995
  66. // Rev 0.4 Dec 15, 1996
  67. //
  68. // Argument structure for IP_ADD_MEMBERSHIP and IP_DROP_MEMBERSHIP
  69. type
  70. ip_mreq = record
  71. imr_multiaddr: in_addr; // IP multicast address of group
  72. imr_interface: in_addr; // local IP address of interface
  73. end;
  74. {$EXTERNALSYM ip_mreq}
  75. TIPMReq = ip_mreq;
  76. PIPMReq = ^ip_mreq;
  77. // Argument structure for IP_ADD_SOURCE_MEMBERSHIP, IP_DROP_SOURCE_MEMBERSHIP,
  78. // IP_BLOCK_SOURCE, and IP_UNBLOCK_SOURCE
  79. //
  80. ip_mreq_source = record
  81. imr_multiaddr: in_addr; // IP multicast address of group
  82. imr_sourceaddr: in_addr; // IP address of source
  83. imr_interface: in_addr; // local IP address of interface
  84. end;
  85. {$EXTERNALSYM ip_mreq_source}
  86. TIpMreqSource = ip_mreq_source;
  87. PIpMreqSource = ^ip_mreq_source;
  88. // Argument structure for SIO_{GET,SET}_MULTICAST_FILTER
  89. ip_msfilter = record
  90. imsf_multiaddr: in_addr; // IP multicast address of group
  91. imsf_interface: in_addr; // local IP address of interface
  92. imsf_fmode: u_long; // filter mode - INCLUDE or EXCLUDE
  93. imsf_numsrc: u_long; // number of sources in src_list
  94. imsf_slist: array [0..0] of in_addr;
  95. end;
  96. {$EXTERNALSYM ip_msfilter}
  97. TIpMsFilter = ip_msfilter;
  98. PIpMsFilter = ^ip_msfilter;
  99. function IP_MSFILTER_SIZE(numsrc: Integer): Integer;
  100. {$EXTERNALSYM IP_MSFILTER_SIZE(numsrc)}
  101. const
  102. MCAST_INCLUDE = 0;
  103. {$EXTERNALSYM MCAST_INCLUDE}
  104. MCAST_EXCLUDE = 1;
  105. {$EXTERNALSYM MCAST_EXCLUDE}
  106. // TCP/IP specific Ioctl codes
  107. const
  108. SIO_GET_INTERFACE_LIST = IOC_OUT or ((4 and IOCPARM_MASK) shl 16) or ((Ord('t')) shl 8) or (127);
  109. {$EXTERNALSYM SIO_GET_INTERFACE_LIST}
  110. // New IOCTL with address size independent address array
  111. SIO_GET_INTERFACE_LIST_EX = IOC_OUT or ((4 and IOCPARM_MASK) shl 16) or ((Ord('t')) shl 8) or (126);
  112. {$EXTERNALSYM SIO_GET_INTERFACE_LIST_EX}
  113. SIO_SET_MULTICAST_FILTER = DWORD(IOC_IN or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('t') shl 8) or 125);
  114. {$EXTERNALSYM SIO_SET_MULTICAST_FILTER}
  115. SIO_GET_MULTICAST_FILTER = DWORD(IOC_IN or ((SizeOf(u_long) and IOCPARM_MASK) shl 16) or (Ord('t') shl 8) or 124 or IOC_IN);
  116. {$EXTERNALSYM SIO_GET_MULTICAST_FILTER}
  117. // Option to use with [gs]etsockopt at the IPPROTO_IP level
  118. const
  119. IP_OPTIONS = 1; // set/get IP options
  120. {$EXTERNALSYM IP_OPTIONS}
  121. IP_HDRINCL = 2; // header is included with data
  122. {$EXTERNALSYM IP_HDRINCL}
  123. IP_TOS = 3; // IP type of service and preced
  124. {$EXTERNALSYM IP_TOS}
  125. IP_TTL = 4; // IP time to live
  126. {$EXTERNALSYM IP_TTL}
  127. IP_MULTICAST_IF = 9; // set/get IP multicast i/f
  128. {$EXTERNALSYM IP_MULTICAST_IF}
  129. IP_MULTICAST_TTL = 10; // set/get IP multicast ttl
  130. {$EXTERNALSYM IP_MULTICAST_TTL}
  131. IP_MULTICAST_LOOP = 11; // set/get IP multicast loopback
  132. {$EXTERNALSYM IP_MULTICAST_LOOP}
  133. IP_ADD_MEMBERSHIP = 12; // add an IP group membership
  134. {$EXTERNALSYM IP_ADD_MEMBERSHIP}
  135. IP_DROP_MEMBERSHIP = 13; // drop an IP group membership
  136. {$EXTERNALSYM IP_DROP_MEMBERSHIP}
  137. IP_DONTFRAGMENT = 14; // don't fragment IP datagrams
  138. {$EXTERNALSYM IP_DONTFRAGMENT}
  139. IP_ADD_SOURCE_MEMBERSHIP = 15; // join IP group/source
  140. {$EXTERNALSYM IP_ADD_SOURCE_MEMBERSHIP}
  141. IP_DROP_SOURCE_MEMBERSHIP = 16; // leave IP group/source
  142. {$EXTERNALSYM IP_DROP_SOURCE_MEMBERSHIP}
  143. IP_BLOCK_SOURCE = 17; // block IP group/source
  144. {$EXTERNALSYM IP_BLOCK_SOURCE}
  145. IP_UNBLOCK_SOURCE = 18; // unblock IP group/source
  146. {$EXTERNALSYM IP_UNBLOCK_SOURCE}
  147. IP_PKTINFO = 19; // receive packet information for ipv4
  148. {$EXTERNALSYM IP_PKTINFO}
  149. // Option to use with [gs]etsockopt at the IPPROTO_IPV6 level
  150. IPV6_HDRINCL = 2; // Header is included with data
  151. {$EXTERNALSYM IPV6_HDRINCL}
  152. IPV6_UNICAST_HOPS = 4; // Set/get IP unicast hop limit
  153. {$EXTERNALSYM IPV6_UNICAST_HOPS}
  154. IPV6_MULTICAST_IF = 9; // Set/get IP multicast interface
  155. {$EXTERNALSYM IPV6_MULTICAST_IF}
  156. IPV6_MULTICAST_HOPS = 10; // Set/get IP multicast ttl
  157. {$EXTERNALSYM IPV6_MULTICAST_HOPS}
  158. IPV6_MULTICAST_LOOP = 11; // Set/get IP multicast loopback
  159. {$EXTERNALSYM IPV6_MULTICAST_LOOP}
  160. IPV6_ADD_MEMBERSHIP = 12; // Add an IP group membership
  161. {$EXTERNALSYM IPV6_ADD_MEMBERSHIP}
  162. IPV6_DROP_MEMBERSHIP = 13; // Drop an IP group membership
  163. {$EXTERNALSYM IPV6_DROP_MEMBERSHIP}
  164. IPV6_JOIN_GROUP = IPV6_ADD_MEMBERSHIP;
  165. {$EXTERNALSYM IPV6_JOIN_GROUP}
  166. IPV6_LEAVE_GROUP = IPV6_DROP_MEMBERSHIP;
  167. {$EXTERNALSYM IPV6_LEAVE_GROUP}
  168. IPV6_PKTINFO = 19; // Receive packet information for ipv6
  169. {$EXTERNALSYM IPV6_PKTINFO}
  170. // Option to use with [gs]etsockopt at the IPPROTO_UDP level
  171. UDP_NOCHECKSUM = 1;
  172. {$EXTERNALSYM UDP_NOCHECKSUM}
  173. UDP_CHECKSUM_COVERAGE = 20; // Set/get UDP-Lite checksum coverage
  174. {$EXTERNALSYM UDP_CHECKSUM_COVERAGE}
  175. // Option to use with [gs]etsockopt at the IPPROTO_TCP level
  176. TCP_EXPEDITED_1122 = $0002;
  177. {$EXTERNALSYM TCP_EXPEDITED_1122}
  178. // IPv6 definitions
  179. type
  180. in6_addr = record
  181. case Integer of
  182. 0: (Byte: array [0..15] of u_char);
  183. 1: (Word: array[0..7] of u_short);
  184. 2: (s6_bytes: array [0..15] of u_char);
  185. 3: (s6_addr: array [0..15] of u_char);
  186. 4: (s6_words: array[0..7] of u_short);
  187. end;
  188. {$EXTERNALSYM in6_addr}
  189. TIn6Addr = in6_addr;
  190. PIn6Addr = ^in6_addr;
  191. //
  192. // Defines to match RFC 2553.
  193. //
  194. //#define _S6_un u
  195. //#define _S6_u8 Byte
  196. // Argument structure for IPV6_JOIN_GROUP and IPV6_LEAVE_GROUP
  197. type
  198. ipv6_mreq = record
  199. ipv6mr_multiaddr: in6_addr; // IPv6 multicast address
  200. ipv6mr_interface: Cardinal; // Interface index
  201. end;
  202. {$EXTERNALSYM ipv6_mreq}
  203. TIpV6MReq = ipv6_mreq;
  204. PIpV6MReq = ^ipv6_mreq;
  205. type
  206. in_addr6 = record
  207. s6_addr: array [0..15] of u_char; // IPv6 address
  208. end;
  209. {$EXTERNALSYM in_addr6}
  210. TInAddr6 = in_addr6;
  211. PInAddr6 = ^in_addr6;
  212. // Old IPv6 socket address structure (retained for sockaddr_gen definition below)
  213. type
  214. sockaddr_in6_old = record
  215. sin6_family: short; // AF_INET6
  216. sin6_port: u_short; // Transport level port number
  217. sin6_flowinfo: u_long; // IPv6 flow information
  218. sin6_addr: in6_addr; // IPv6 address
  219. end;
  220. {$EXTERNALSYM sockaddr_in6_old}
  221. TSockAddrIn6Old = sockaddr_in6_old;
  222. PSockAddrIn6Old = ^sockaddr_in6_old;
  223. // IPv6 socket address structure, RFC 2553
  224. SOCKADDR_IN6 = record
  225. sin6_family: short; // AF_INET6
  226. sin6_port: u_short; // Transport level port number
  227. sin6_flowinfo: u_long; // IPv6 flow information
  228. sin6_addr: in6_addr; // IPv6 address
  229. sin6_scope_id: u_long; // set of interfaces for a scope
  230. end;
  231. {$EXTERNALSYM SOCKADDR_IN6}
  232. PSOCKADDR_IN6 = ^SOCKADDR_IN6;
  233. {$EXTERNALSYM PSOCKADDR_IN6}
  234. LPSOCKADDR_IN6 = ^SOCKADDR_IN6;
  235. {$EXTERNALSYM LPSOCKADDR_IN6}
  236. TSockAddrIn6 = SOCKADDR_IN6;
  237. PSockAddrIn6 = LPSOCKADDR_IN6;
  238. // Macro that works for both IPv4 and IPv6
  239. function SS_PORT(ssp: Pointer): u_short;
  240. {$EXTERNALSYM SS_PORT}
  241. const
  242. IN6ADDR_ANY_INIT: in6_addr = (Word: (0, 0, 0, 0, 0, 0, 0, 0));
  243. {$EXTERNALSYM IN6ADDR_ANY_INIT}
  244. IN6ADDR_LOOPBACK_INIT: in6_addr = (Byte: (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1));
  245. {$EXTERNALSYM IN6ADDR_LOOPBACK_INIT}
  246. in6addr_any: in6_addr = (Word: (0, 0, 0, 0, 0, 0, 0, 0));
  247. {$EXTERNALSYM in6addr_any}
  248. in6addr_loopback: in6_addr = (Byte: (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1));
  249. {$EXTERNALSYM in6addr_loopback}
  250. procedure IN6ADDR_SETANY(var x: TSockAddrIn6);
  251. {$EXTERNALSYM IN6ADDR_SETANY}
  252. procedure IN6ADDR_SETLOOPBACK(var x: TSockAddrIn6);
  253. {$EXTERNALSYM IN6ADDR_SETLOOPBACK}
  254. function IN6ADDR_ISANY(const x: TSockAddrIn6): Boolean;
  255. {$EXTERNALSYM IN6ADDR_ISANY}
  256. function IN6ADDR_ISLOOPBACK(const x: TSockAddrIn6): Boolean;
  257. {$EXTERNALSYM IN6ADDR_ISLOOPBACK}
  258. function IN6_ADDR_EQUAL(const a, b: in6_addr): Boolean;
  259. {$EXTERNALSYM IN6_ADDR_EQUAL}
  260. function IN6_IS_ADDR_UNSPECIFIED(const a: in6_addr): boolean;
  261. {$EXTERNALSYM IN6_IS_ADDR_UNSPECIFIED}
  262. function IN6_IS_ADDR_LOOPBACK(const a: in6_addr): Boolean;
  263. {$EXTERNALSYM IN6_IS_ADDR_LOOPBACK}
  264. function IN6_IS_ADDR_MULTICAST(const a: in6_addr): Boolean;
  265. {$EXTERNALSYM IN6_IS_ADDR_MULTICAST}
  266. function IN6_IS_ADDR_LINKLOCAL(const a: in6_addr): Boolean;
  267. {$EXTERNALSYM IN6_IS_ADDR_LINKLOCAL}
  268. function IN6_IS_ADDR_SITELOCAL(const a: in6_addr): Boolean;
  269. {$EXTERNALSYM IN6_IS_ADDR_SITELOCAL}
  270. function IN6_IS_ADDR_V4MAPPED(const a: in6_addr): Boolean;
  271. {$EXTERNALSYM IN6_IS_ADDR_V4MAPPED}
  272. function IN6_IS_ADDR_V4COMPAT(const a: in6_addr): Boolean;
  273. {$EXTERNALSYM IN6_IS_ADDR_V4COMPAT}
  274. function IN6_IS_ADDR_MC_NODELOCAL(const a: in6_addr): Boolean;
  275. {$EXTERNALSYM IN6_IS_ADDR_MC_NODELOCAL}
  276. function IN6_IS_ADDR_MC_LINKLOCAL(const a: in6_addr): Boolean;
  277. {$EXTERNALSYM IN6_IS_ADDR_MC_LINKLOCAL}
  278. function IN6_IS_ADDR_MC_SITELOCAL(const a: in6_addr): Boolean;
  279. {$EXTERNALSYM IN6_IS_ADDR_MC_SITELOCAL}
  280. function IN6_IS_ADDR_MC_ORGLOCAL(const a: in6_addr): Boolean;
  281. {$EXTERNALSYM IN6_IS_ADDR_MC_ORGLOCAL}
  282. function IN6_IS_ADDR_MC_GLOBAL(const a: in6_addr): Boolean;
  283. {$EXTERNALSYM IN6_IS_ADDR_MC_GLOBAL}
  284. type
  285. sockaddr_gen = record
  286. case Integer of
  287. 0: (Address: sockaddr);
  288. 1: (AddressIn: sockaddr_in);
  289. 2: (AddressIn6: sockaddr_in6_old);
  290. end;
  291. {$EXTERNALSYM sockaddr_gen}
  292. TSockAddrGen = sockaddr_gen;
  293. PSockAddrGen = ^sockaddr_gen;
  294. // Structure to keep interface specific information
  295. _INTERFACE_INFO = record
  296. iiFlags: u_long; // Interface flags
  297. iiAddress: sockaddr_gen; // Interface address
  298. iiBroadcastAddress: sockaddr_gen; // Broadcast address
  299. iiNetmask: sockaddr_gen; // Network mask
  300. end;
  301. {$EXTERNALSYM _INTERFACE_INFO}
  302. INTERFACE_INFO = _INTERFACE_INFO;
  303. {$EXTERNALSYM INTERFACE_INFO}
  304. LPINTERFACE_INFO = ^INTERFACE_INFO;
  305. {$EXTERNALSYM LPINTERFACE_INFO}
  306. TInterfaceInfo = INTERFACE_INFO;
  307. PInterfaceInfo = LPINTERFACE_INFO;
  308. // New structure that does not have dependency on the address size
  309. _INTERFACE_INFO_EX = record
  310. iiFlags: u_long; // Interface flags
  311. iiAddress: SOCKET_ADDRESS; // Interface address
  312. iiBroadcastAddress: SOCKET_ADDRESS; // Broadcast address
  313. iiNetmask: SOCKET_ADDRESS; // Network mask
  314. end;
  315. {$EXTERNALSYM _INTERFACE_INFO_EX}
  316. INTERFACE_INFO_EX = _INTERFACE_INFO_EX;
  317. {$EXTERNALSYM INTERFACE_INFO_EX}
  318. LPINTERFACE_INFO_EX = ^INTERFACE_INFO_EX;
  319. {$EXTERNALSYM LPINTERFACE_INFO_EX}
  320. TInterfaceInfoEx = INTERFACE_INFO_EX;
  321. PInterfaceInfoEx = LPINTERFACE_INFO_EX;
  322. // Possible flags for the iiFlags - bitmask
  323. const
  324. IFF_UP = $00000001; // Interface is up
  325. {$EXTERNALSYM IFF_UP}
  326. IFF_BROADCAST = $00000002; // Broadcast is supported
  327. {$EXTERNALSYM IFF_BROADCAST}
  328. IFF_LOOPBACK = $00000004; // this is loopback interface
  329. {$EXTERNALSYM IFF_LOOPBACK}
  330. IFF_POINTTOPOINT = $00000008; //this is point-to-point interface
  331. {$EXTERNALSYM IFF_POINTTOPOINT}
  332. IFF_MULTICAST = $00000010; // multicast is supported
  333. {$EXTERNALSYM IFF_MULTICAST}
  334. // structure for IP_PKTINFO option
  335. //
  336. type
  337. in_pktinfo = record
  338. ipi_addr: IN_ADDR; // destination IPv4 address
  339. ipi_ifindex: UINT; // received interface index
  340. end;
  341. {$EXTERNALSYM in_pktinfo}
  342. TInPktInfo = in_pktinfo;
  343. PInPktInfo = ^in_pktinfo;
  344. // C_ASSERT(sizeof(IN_PKTINFO) == 8);
  345. // structure for IPV6_PKTINFO option
  346. //
  347. in6_pktinfo = record
  348. ipi6_addr: IN6_ADDR; // destination IPv6 address
  349. ipi6_ifindex: UINT; // received interface index
  350. end;
  351. {$EXTERNALSYM in6_pktinfo}
  352. TIn6PktInfo = in6_pktinfo;
  353. PIn6PktInfo = ^in6_pktinfo;
  354. // C_ASSERT(sizeof(IN6_PKTINFO) == 20);
  355. // Error codes from getaddrinfo()
  356. const
  357. EAI_AGAIN = WSATRY_AGAIN;
  358. {$EXTERNALSYM EAI_AGAIN}
  359. EAI_BADFLAGS = WSAEINVAL;
  360. {$EXTERNALSYM EAI_BADFLAGS}
  361. EAI_FAIL = WSANO_RECOVERY;
  362. {$EXTERNALSYM EAI_FAIL}
  363. EAI_FAMILY = WSAEAFNOSUPPORT;
  364. {$EXTERNALSYM EAI_FAMILY}
  365. EAI_MEMORY = WSA_NOT_ENOUGH_MEMORY;
  366. {$EXTERNALSYM EAI_MEMORY}
  367. //#define EAI_NODATA WSANO_DATA
  368. EAI_NONAME = WSAHOST_NOT_FOUND;
  369. {$EXTERNALSYM EAI_NONAME}
  370. EAI_SERVICE = WSATYPE_NOT_FOUND;
  371. {$EXTERNALSYM EAI_SERVICE}
  372. EAI_SOCKTYPE = WSAESOCKTNOSUPPORT;
  373. {$EXTERNALSYM EAI_SOCKTYPE}
  374. //
  375. // DCR_FIX: EAI_NODATA remove or fix
  376. //
  377. // EAI_NODATA was removed from rfc2553bis
  378. // need to find out from the authors why and
  379. // determine the error for "no records of this type"
  380. // temporarily, we'll keep #define to avoid changing
  381. // code that could change back; use NONAME
  382. //
  383. EAI_NODATA = EAI_NONAME;
  384. {$EXTERNALSYM EAI_NODATA}
  385. // Structure used in getaddrinfo() call
  386. type
  387. LPADDRINFO = ^addrinfo;
  388. {$EXTERNALSYM LPADDRINFO}
  389. addrinfo = record
  390. ai_flags: Integer; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST
  391. ai_family: Integer; // PF_xxx
  392. ai_socktype: Integer; // SOCK_xxx
  393. ai_protocol: Integer; // 0 or IPPROTO_xxx for IPv4 and IPv6
  394. ai_addrlen: size_t; // Length of ai_addr
  395. ai_canonname: PChar; // Canonical name for nodename
  396. ai_addr: PSockAddr; // Binary address
  397. ai_next: LPADDRINFO; // Next structure in linked list
  398. end;
  399. {$EXTERNALSYM addrinfo}
  400. TAddrInfo = addrinfo;
  401. PAddrInfo = LPADDRINFO;
  402. // Flags used in "hints" argument to getaddrinfo()
  403. const
  404. AI_PASSIVE = $1; // Socket address will be used in bind() call
  405. {$EXTERNALSYM AI_PASSIVE}
  406. AI_CANONNAME = $2; // Return canonical name in first ai_canonname
  407. {$EXTERNALSYM AI_CANONNAME}
  408. AI_NUMERICHOST = $4; // Nodename must be a numeric address string
  409. {$EXTERNALSYM AI_NUMERICHOST}
  410. function getaddrinfo(nodename, servname: PChar; hints: PAddrInfo; var res: PAddrInfo): Integer; stdcall;
  411. {$EXTERNALSYM getaddrinfo}
  412. procedure freeaddrinfo(ai: PAddrInfo); stdcall;
  413. {$EXTERNALSYM freeaddrinfo}
  414. // WARNING: The gai_strerror inline functions below use static buffers,
  415. // and hence are not thread-safe. We'll use buffers long enough to hold
  416. // 1k characters. Any system error messages longer than this will be
  417. // returned as empty strings. However 1k should work for the error codes
  418. // used by getaddrinfo().
  419. const
  420. GAI_STRERROR_BUFFER_SIZE = 1024;
  421. {$EXTERNALSYM GAI_STRERROR_BUFFER_SIZE}
  422. function gai_strerrorA(ecode: Integer): PChar;
  423. {$EXTERNALSYM gai_strerrorA}
  424. function gai_strerrorW(ecode: Integer): PWideChar;
  425. {$EXTERNALSYM gai_strerrorW}
  426. function gai_strerror(ecode: Integer): PTCHAR;
  427. {$EXTERNALSYM gai_strerror}
  428. type
  429. socklen_t = Integer;
  430. {$EXTERNALSYM socklen_t}
  431. function getnameinfo(sa: PSockAddr; salen: socklen_t; host: PChar; hostlen: DWORD; serv: PChar; servlen: DWORD; flags: Integer): Integer; stdcall;
  432. {$EXTERNALSYM getnameinfo}
  433. const
  434. NI_MAXHOST = 1025; // Max size of a fully-qualified domain name
  435. {$EXTERNALSYM NI_MAXHOST}
  436. NI_MAXSERV = 32; // Max size of a service name
  437. {$EXTERNALSYM NI_MAXSERV}
  438. INET_ADDRSTRLEN = 16; // Max size of numeric form of IPv4 address
  439. {$EXTERNALSYM INET_ADDRSTRLEN}
  440. INET6_ADDRSTRLEN = 46; // Max size of numeric form of IPv6 address
  441. {$EXTERNALSYM INET6_ADDRSTRLEN}
  442. // Flags for getnameinfo()
  443. NI_NOFQDN = $01; // Only return nodename portion for local hosts
  444. {$EXTERNALSYM NI_NOFQDN}
  445. NI_NUMERICHOST = $02; // Return numeric form of the host's address
  446. {$EXTERNALSYM NI_NUMERICHOST}
  447. NI_NAMEREQD = $04; // Error if the host's name not in DNS
  448. {$EXTERNALSYM NI_NAMEREQD}
  449. NI_NUMERICSERV = $08; // Return numeric form of the service (port #)
  450. {$EXTERNALSYM NI_NUMERICSERV}
  451. NI_DGRAM = $10; // Service is a datagram service
  452. {$EXTERNALSYM NI_DGRAM}
  453. implementation
  454. uses
  455. SysUtils, JwaWinBase, JwaWinNT;
  456. function IP_MSFILTER_SIZE(numsrc: Integer): Integer;
  457. begin
  458. Result := SizeOf(ip_msfilter) - SizeOf(in_addr) + (numsrc * SizeOf(in_addr));
  459. end;
  460. function SS_PORT(ssp: Pointer): u_short;
  461. begin
  462. Result := PSockAddrIn(ssp)^.sin_port;
  463. end;
  464. procedure IN6ADDR_SETANY(var x: TSockAddrIn6);
  465. var
  466. I: Integer;
  467. begin
  468. x.sin6_family := AF_INET6;
  469. x.sin6_port := 0;
  470. x.sin6_flowinfo := 0;
  471. for I := 0 to 15 do
  472. x.sin6_addr.s6_addr[I] := 0;
  473. end;
  474. procedure IN6ADDR_SETLOOPBACK(var x: TSockAddrIn6);
  475. var
  476. I: Integer;
  477. begin
  478. x.sin6_family := AF_INET6;
  479. x.sin6_port := 0;
  480. x.sin6_flowinfo := 0;
  481. for I := 0 to 14 do
  482. x.sin6_addr.s6_addr[I] := 0;
  483. x.sin6_addr.s6_addr[15] := 1;
  484. end;
  485. function IN6ADDR_ISANY(const x: TSockAddrIn6): Boolean;
  486. var
  487. I: Integer;
  488. begin
  489. Result := x.sin6_family = AF_INET6;
  490. for I := 0 to 15 do
  491. Result := Result and (x.sin6_addr.s6_addr[I] = 0);
  492. end;
  493. function IN6ADDR_ISLOOPBACK(const x: TSockAddrIn6): Boolean;
  494. var
  495. I: Integer;
  496. begin
  497. Result := x.sin6_family = AF_INET6;
  498. for I := 0 to 14 do
  499. Result := Result and (x.sin6_addr.s6_addr[I] = 0);
  500. Result := Result and (x.sin6_addr.s6_addr[15] = 1);
  501. end;
  502. function IN6_ADDR_EQUAL(const a, b: in6_addr): Boolean;
  503. begin
  504. Result := CompareMem(@a, @b, SizeOf(in6_addr));
  505. end;
  506. function IN6_IS_ADDR_UNSPECIFIED(const a: in6_addr): boolean;
  507. begin
  508. Result := IN6_ADDR_EQUAL(a, in6addr_any);
  509. end;
  510. function IN6_IS_ADDR_LOOPBACK(const a: in6_addr): Boolean;
  511. begin
  512. Result := IN6_ADDR_EQUAL(a, in6addr_loopback);
  513. end;
  514. function IN6_IS_ADDR_MULTICAST(const a: in6_addr): Boolean;
  515. begin
  516. Result := (a.s6_bytes[0] = $ff);
  517. end;
  518. function IN6_IS_ADDR_LINKLOCAL(const a: in6_addr): Boolean;
  519. begin
  520. Result := ((a.s6_bytes[0] = $fe) and ((a.s6_bytes[1] and $c0) = $80));
  521. end;
  522. function IN6_IS_ADDR_SITELOCAL(const a: in6_addr): Boolean;
  523. begin
  524. Result := ((a.s6_bytes[0] = $fe) and ((a.s6_bytes[1] and $c0) = $c0));
  525. end;
  526. function IN6_IS_ADDR_V4MAPPED(const a: in6_addr): Boolean;
  527. begin
  528. Result := ((a.s6_words[0] = 0) and (a.s6_words[1] = 0) and (a.s6_words[2] = 0) and
  529. (a.s6_words[3] = 0) and (a.s6_words[4] = 0) and (a.s6_words[5] = $ffff));
  530. end;
  531. function IN6_IS_ADDR_V4COMPAT(const a: in6_addr): Boolean;
  532. begin
  533. Result :=
  534. ((a.s6_words[0] = 0) and
  535. (a.s6_words[1] = 0) and
  536. (a.s6_words[2] = 0) and
  537. (a.s6_words[3] = 0) and
  538. (a.s6_words[4] = 0) and
  539. (a.s6_words[5] = 0) and
  540. not ((a.s6_words[6] = 0) and
  541. (a.s6_addr[14] = 0) and
  542. ((a.s6_addr[15] = 0) or (a.s6_addr[15] = 1))));
  543. end;
  544. function IN6_IS_ADDR_MC_NODELOCAL(const a: in6_addr): Boolean;
  545. begin
  546. Result := IN6_IS_ADDR_MULTICAST(a) and ((a.s6_bytes[1] and $f) = 1);
  547. end;
  548. function IN6_IS_ADDR_MC_LINKLOCAL(const a: in6_addr): Boolean;
  549. begin
  550. Result := IN6_IS_ADDR_MULTICAST(a) and ((a.s6_bytes[1] and $f) = 2);
  551. end;
  552. function IN6_IS_ADDR_MC_SITELOCAL(const a: in6_addr): Boolean;
  553. begin
  554. Result := IN6_IS_ADDR_MULTICAST(a) and ((a.s6_bytes[1] and $f) = 5);
  555. end;
  556. function IN6_IS_ADDR_MC_ORGLOCAL(const a: in6_addr): Boolean;
  557. begin
  558. Result := IN6_IS_ADDR_MULTICAST(a) and ((a.s6_bytes[1] and $f) = 8);
  559. end;
  560. function IN6_IS_ADDR_MC_GLOBAL(const a: in6_addr): Boolean;
  561. begin
  562. Result := IN6_IS_ADDR_MULTICAST(a) and ((a.s6_bytes[1] and $f) = $e);
  563. end;
  564. var
  565. gai_strerror_buffA: array [0..GAI_STRERROR_BUFFER_SIZE-1] of Char;
  566. gai_strerror_buffW: array [0..GAI_STRERROR_BUFFER_SIZE-1] of WideChar;
  567. function gai_strerrorA(ecode: Integer): PChar;
  568. var
  569. dwMsgLen: DWORD;
  570. begin
  571. dwMsgLen := FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_MAX_WIDTH_MASK,
  572. nil, ecode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), PChar(@gai_strerror_buffA[0]), GAI_STRERROR_BUFFER_SIZE, nil);
  573. if dwMsgLen = 0 then
  574. Result := nil
  575. else
  576. Result := PChar(@gai_strerror_buffA[0]);
  577. end;
  578. function gai_strerrorW(ecode: Integer): PWideChar;
  579. var
  580. dwMsgLen: DWORD;
  581. begin
  582. dwMsgLen := FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM or FORMAT_MESSAGE_IGNORE_INSERTS or FORMAT_MESSAGE_MAX_WIDTH_MASK,
  583. nil, ecode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), PWideChar(@gai_strerror_buffW[0]), GAI_STRERROR_BUFFER_SIZE, nil);
  584. if dwMsgLen = 0 then
  585. Result := nil
  586. else
  587. Result := PWideChar(@gai_strerror_buffW[0]);
  588. end;
  589. function gai_strerror(ecode: Integer): PTCHAR;
  590. begin
  591. {$IFDEF UNICODE}
  592. Result := PTCHAR(gai_strerrorW(ecode));
  593. {$ELSE}
  594. Result := PTCHAR(gai_strerrorA(ecode));
  595. {$ENDIF UNICODE}
  596. end;
  597. const
  598. ws2tcpip = 'ws2_32.dll';
  599. {$IFDEF DYNAMIC_LINK}
  600. var
  601. _getaddrinfo: Pointer;
  602. function getaddrinfo;
  603. begin
  604. GetProcedureAddress(_getaddrinfo, ws2tcpip, 'getaddrinfo');
  605. asm
  606. MOV ESP, EBP
  607. POP EBP
  608. JMP [_getaddrinfo]
  609. end;
  610. end;
  611. var
  612. _freeaddrinfo: Pointer;
  613. procedure freeaddrinfo;
  614. begin
  615. GetProcedureAddress(_freeaddrinfo, ws2tcpip, 'freeaddrinfo');
  616. asm
  617. MOV ESP, EBP
  618. POP EBP
  619. JMP [_freeaddrinfo]
  620. end;
  621. end;
  622. var
  623. _getnameinfo: Pointer;
  624. function getnameinfo;
  625. begin
  626. GetProcedureAddress(_getnameinfo, ws2tcpip, 'getnameinfo');
  627. asm
  628. MOV ESP, EBP
  629. POP EBP
  630. JMP [_getnameinfo]
  631. end;
  632. end;
  633. {$ELSE}
  634. function getaddrinfo; external ws2tcpip name 'getaddrinfo';
  635. procedure freeaddrinfo; external ws2tcpip name 'freeaddrinfo';
  636. function getnameinfo; external ws2tcpip name 'getnameinfo';
  637. {$ENDIF DYNAMIC_LINK}
  638. end.