Программа для прослушивания журнала АРК Томск http://blindage.org/?p=6627
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.

sswin32.inc 55KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 002.003.000 |
  3. |==============================================================================|
  4. | Content: Socket Independent Platform Layer - Win32/64 definition include |
  5. |==============================================================================|
  6. | Copyright (c)1999-2011, Lukas Gebauer |
  7. | All rights reserved. |
  8. | |
  9. | Redistribution and use in source and binary forms, with or without |
  10. | modification, are permitted provided that the following conditions are met: |
  11. | |
  12. | Redistributions of source code must retain the above copyright notice, this |
  13. | list of conditions and the following disclaimer. |
  14. | |
  15. | Redistributions in binary form must reproduce the above copyright notice, |
  16. | this list of conditions and the following disclaimer in the documentation |
  17. | and/or other materials provided with the distribution. |
  18. | |
  19. | Neither the name of Lukas Gebauer nor the names of its contributors may |
  20. | be used to endorse or promote products derived from this software without |
  21. | specific prior written permission. |
  22. | |
  23. | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
  24. | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
  25. | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
  26. | ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR |
  27. | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
  28. | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
  29. | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
  30. | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
  31. | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
  32. | OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
  33. | DAMAGE. |
  34. |==============================================================================|
  35. | The Initial Developer of the Original Code is Lukas Gebauer (Czech Republic).|
  36. | Portions created by Lukas Gebauer are Copyright (c)2003-2011. |
  37. | All Rights Reserved. |
  38. |==============================================================================|
  39. | Contributor(s): |
  40. |==============================================================================|
  41. | History: see HISTORY.HTM from distribution package |
  42. | (Found at URL: http://www.ararat.cz/synapse/) |
  43. |==============================================================================}
  44. {:@exclude}
  45. //{$DEFINE WINSOCK1}
  46. {Note about define WINSOCK1:
  47. If you activate this compiler directive, then socket interface level 1.1 is
  48. used instead default level 2.2. Level 2.2 is not available on old W95, however
  49. you can install update.
  50. }
  51. //{$DEFINE FORCEOLDAPI}
  52. {Note about define FORCEOLDAPI:
  53. If you activate this compiler directive, then is allways used old socket API
  54. for name resolution. If you leave this directive inactive, then the new API
  55. is used, when running system allows it.
  56. For IPv6 support you must have new API!
  57. }
  58. {$IFDEF FPC}
  59. {$MODE DELPHI}
  60. {$ENDIF}
  61. {$H+}
  62. {$IFDEF VER125}
  63. {$DEFINE BCB}
  64. {$ENDIF}
  65. {$IFDEF BCB}
  66. {$ObjExportAll On}
  67. (*$HPPEMIT '/* EDE 2003-02-19 */' *)
  68. (*$HPPEMIT 'namespace Synsock { using System::Shortint; }' *)
  69. (*$HPPEMIT '#undef h_addr' *)
  70. (*$HPPEMIT '#undef IOCPARM_MASK' *)
  71. (*$HPPEMIT '#undef FD_SETSIZE' *)
  72. (*$HPPEMIT '#undef IOC_VOID' *)
  73. (*$HPPEMIT '#undef IOC_OUT' *)
  74. (*$HPPEMIT '#undef IOC_IN' *)
  75. (*$HPPEMIT '#undef IOC_INOUT' *)
  76. (*$HPPEMIT '#undef FIONREAD' *)
  77. (*$HPPEMIT '#undef FIONBIO' *)
  78. (*$HPPEMIT '#undef FIOASYNC' *)
  79. (*$HPPEMIT '#undef IPPROTO_IP' *)
  80. (*$HPPEMIT '#undef IPPROTO_ICMP' *)
  81. (*$HPPEMIT '#undef IPPROTO_IGMP' *)
  82. (*$HPPEMIT '#undef IPPROTO_TCP' *)
  83. (*$HPPEMIT '#undef IPPROTO_UDP' *)
  84. (*$HPPEMIT '#undef IPPROTO_RAW' *)
  85. (*$HPPEMIT '#undef IPPROTO_MAX' *)
  86. (*$HPPEMIT '#undef INADDR_ANY' *)
  87. (*$HPPEMIT '#undef INADDR_LOOPBACK' *)
  88. (*$HPPEMIT '#undef INADDR_BROADCAST' *)
  89. (*$HPPEMIT '#undef INADDR_NONE' *)
  90. (*$HPPEMIT '#undef INVALID_SOCKET' *)
  91. (*$HPPEMIT '#undef SOCKET_ERROR' *)
  92. (*$HPPEMIT '#undef WSADESCRIPTION_LEN' *)
  93. (*$HPPEMIT '#undef WSASYS_STATUS_LEN' *)
  94. (*$HPPEMIT '#undef IP_OPTIONS' *)
  95. (*$HPPEMIT '#undef IP_TOS' *)
  96. (*$HPPEMIT '#undef IP_TTL' *)
  97. (*$HPPEMIT '#undef IP_MULTICAST_IF' *)
  98. (*$HPPEMIT '#undef IP_MULTICAST_TTL' *)
  99. (*$HPPEMIT '#undef IP_MULTICAST_LOOP' *)
  100. (*$HPPEMIT '#undef IP_ADD_MEMBERSHIP' *)
  101. (*$HPPEMIT '#undef IP_DROP_MEMBERSHIP' *)
  102. (*$HPPEMIT '#undef IP_DONTFRAGMENT' *)
  103. (*$HPPEMIT '#undef IP_DEFAULT_MULTICAST_TTL' *)
  104. (*$HPPEMIT '#undef IP_DEFAULT_MULTICAST_LOOP' *)
  105. (*$HPPEMIT '#undef IP_MAX_MEMBERSHIPS' *)
  106. (*$HPPEMIT '#undef SOL_SOCKET' *)
  107. (*$HPPEMIT '#undef SO_DEBUG' *)
  108. (*$HPPEMIT '#undef SO_ACCEPTCONN' *)
  109. (*$HPPEMIT '#undef SO_REUSEADDR' *)
  110. (*$HPPEMIT '#undef SO_KEEPALIVE' *)
  111. (*$HPPEMIT '#undef SO_DONTROUTE' *)
  112. (*$HPPEMIT '#undef SO_BROADCAST' *)
  113. (*$HPPEMIT '#undef SO_USELOOPBACK' *)
  114. (*$HPPEMIT '#undef SO_LINGER' *)
  115. (*$HPPEMIT '#undef SO_OOBINLINE' *)
  116. (*$HPPEMIT '#undef SO_DONTLINGER' *)
  117. (*$HPPEMIT '#undef SO_SNDBUF' *)
  118. (*$HPPEMIT '#undef SO_RCVBUF' *)
  119. (*$HPPEMIT '#undef SO_SNDLOWAT' *)
  120. (*$HPPEMIT '#undef SO_RCVLOWAT' *)
  121. (*$HPPEMIT '#undef SO_SNDTIMEO' *)
  122. (*$HPPEMIT '#undef SO_RCVTIMEO' *)
  123. (*$HPPEMIT '#undef SO_ERROR' *)
  124. (*$HPPEMIT '#undef SO_OPENTYPE' *)
  125. (*$HPPEMIT '#undef SO_SYNCHRONOUS_ALERT' *)
  126. (*$HPPEMIT '#undef SO_SYNCHRONOUS_NONALERT' *)
  127. (*$HPPEMIT '#undef SO_MAXDG' *)
  128. (*$HPPEMIT '#undef SO_MAXPATHDG' *)
  129. (*$HPPEMIT '#undef SO_UPDATE_ACCEPT_CONTEXT' *)
  130. (*$HPPEMIT '#undef SO_CONNECT_TIME' *)
  131. (*$HPPEMIT '#undef SO_TYPE' *)
  132. (*$HPPEMIT '#undef SOCK_STREAM' *)
  133. (*$HPPEMIT '#undef SOCK_DGRAM' *)
  134. (*$HPPEMIT '#undef SOCK_RAW' *)
  135. (*$HPPEMIT '#undef SOCK_RDM' *)
  136. (*$HPPEMIT '#undef SOCK_SEQPACKET' *)
  137. (*$HPPEMIT '#undef TCP_NODELAY' *)
  138. (*$HPPEMIT '#undef AF_UNSPEC' *)
  139. (*$HPPEMIT '#undef SOMAXCONN' *)
  140. (*$HPPEMIT '#undef AF_INET' *)
  141. (*$HPPEMIT '#undef AF_MAX' *)
  142. (*$HPPEMIT '#undef PF_UNSPEC' *)
  143. (*$HPPEMIT '#undef PF_INET' *)
  144. (*$HPPEMIT '#undef PF_MAX' *)
  145. (*$HPPEMIT '#undef MSG_OOB' *)
  146. (*$HPPEMIT '#undef MSG_PEEK' *)
  147. (*$HPPEMIT '#undef WSABASEERR' *)
  148. (*$HPPEMIT '#undef WSAEINTR' *)
  149. (*$HPPEMIT '#undef WSAEBADF' *)
  150. (*$HPPEMIT '#undef WSAEACCES' *)
  151. (*$HPPEMIT '#undef WSAEFAULT' *)
  152. (*$HPPEMIT '#undef WSAEINVAL' *)
  153. (*$HPPEMIT '#undef WSAEMFILE' *)
  154. (*$HPPEMIT '#undef WSAEWOULDBLOCK' *)
  155. (*$HPPEMIT '#undef WSAEINPROGRESS' *)
  156. (*$HPPEMIT '#undef WSAEALREADY' *)
  157. (*$HPPEMIT '#undef WSAENOTSOCK' *)
  158. (*$HPPEMIT '#undef WSAEDESTADDRREQ' *)
  159. (*$HPPEMIT '#undef WSAEMSGSIZE' *)
  160. (*$HPPEMIT '#undef WSAEPROTOTYPE' *)
  161. (*$HPPEMIT '#undef WSAENOPROTOOPT' *)
  162. (*$HPPEMIT '#undef WSAEPROTONOSUPPORT' *)
  163. (*$HPPEMIT '#undef WSAESOCKTNOSUPPORT' *)
  164. (*$HPPEMIT '#undef WSAEOPNOTSUPP' *)
  165. (*$HPPEMIT '#undef WSAEPFNOSUPPORT' *)
  166. (*$HPPEMIT '#undef WSAEAFNOSUPPORT' *)
  167. (*$HPPEMIT '#undef WSAEADDRINUSE' *)
  168. (*$HPPEMIT '#undef WSAEADDRNOTAVAIL' *)
  169. (*$HPPEMIT '#undef WSAENETDOWN' *)
  170. (*$HPPEMIT '#undef WSAENETUNREACH' *)
  171. (*$HPPEMIT '#undef WSAENETRESET' *)
  172. (*$HPPEMIT '#undef WSAECONNABORTED' *)
  173. (*$HPPEMIT '#undef WSAECONNRESET' *)
  174. (*$HPPEMIT '#undef WSAENOBUFS' *)
  175. (*$HPPEMIT '#undef WSAEISCONN' *)
  176. (*$HPPEMIT '#undef WSAENOTCONN' *)
  177. (*$HPPEMIT '#undef WSAESHUTDOWN' *)
  178. (*$HPPEMIT '#undef WSAETOOMANYREFS' *)
  179. (*$HPPEMIT '#undef WSAETIMEDOUT' *)
  180. (*$HPPEMIT '#undef WSAECONNREFUSED' *)
  181. (*$HPPEMIT '#undef WSAELOOP' *)
  182. (*$HPPEMIT '#undef WSAENAMETOOLONG' *)
  183. (*$HPPEMIT '#undef WSAEHOSTDOWN' *)
  184. (*$HPPEMIT '#undef WSAEHOSTUNREACH' *)
  185. (*$HPPEMIT '#undef WSAENOTEMPTY' *)
  186. (*$HPPEMIT '#undef WSAEPROCLIM' *)
  187. (*$HPPEMIT '#undef WSAEUSERS' *)
  188. (*$HPPEMIT '#undef WSAEDQUOT' *)
  189. (*$HPPEMIT '#undef WSAESTALE' *)
  190. (*$HPPEMIT '#undef WSAEREMOTE' *)
  191. (*$HPPEMIT '#undef WSASYSNOTREADY' *)
  192. (*$HPPEMIT '#undef WSAVERNOTSUPPORTED' *)
  193. (*$HPPEMIT '#undef WSANOTINITIALISED' *)
  194. (*$HPPEMIT '#undef WSAEDISCON' *)
  195. (*$HPPEMIT '#undef WSAENOMORE' *)
  196. (*$HPPEMIT '#undef WSAECANCELLED' *)
  197. (*$HPPEMIT '#undef WSAEEINVALIDPROCTABLE' *)
  198. (*$HPPEMIT '#undef WSAEINVALIDPROVIDER' *)
  199. (*$HPPEMIT '#undef WSAEPROVIDERFAILEDINIT' *)
  200. (*$HPPEMIT '#undef WSASYSCALLFAILURE' *)
  201. (*$HPPEMIT '#undef WSASERVICE_NOT_FOUND' *)
  202. (*$HPPEMIT '#undef WSATYPE_NOT_FOUND' *)
  203. (*$HPPEMIT '#undef WSA_E_NO_MORE' *)
  204. (*$HPPEMIT '#undef WSA_E_CANCELLED' *)
  205. (*$HPPEMIT '#undef WSAEREFUSED' *)
  206. (*$HPPEMIT '#undef WSAHOST_NOT_FOUND' *)
  207. (*$HPPEMIT '#undef HOST_NOT_FOUND' *)
  208. (*$HPPEMIT '#undef WSATRY_AGAIN' *)
  209. (*$HPPEMIT '#undef TRY_AGAIN' *)
  210. (*$HPPEMIT '#undef WSANO_RECOVERY' *)
  211. (*$HPPEMIT '#undef NO_RECOVERY' *)
  212. (*$HPPEMIT '#undef WSANO_DATA' *)
  213. (*$HPPEMIT '#undef NO_DATA' *)
  214. (*$HPPEMIT '#undef WSANO_ADDRESS' *)
  215. (*$HPPEMIT '#undef ENAMETOOLONG' *)
  216. (*$HPPEMIT '#undef ENOTEMPTY' *)
  217. (*$HPPEMIT '#undef FD_CLR' *)
  218. (*$HPPEMIT '#undef FD_ISSET' *)
  219. (*$HPPEMIT '#undef FD_SET' *)
  220. (*$HPPEMIT '#undef FD_ZERO' *)
  221. (*$HPPEMIT '#undef NO_ADDRESS' *)
  222. (*$HPPEMIT '#undef ADDR_ANY' *)
  223. (*$HPPEMIT '#undef SO_GROUP_ID' *)
  224. (*$HPPEMIT '#undef SO_GROUP_PRIORITY' *)
  225. (*$HPPEMIT '#undef SO_MAX_MSG_SIZE' *)
  226. (*$HPPEMIT '#undef SO_PROTOCOL_INFOA' *)
  227. (*$HPPEMIT '#undef SO_PROTOCOL_INFOW' *)
  228. (*$HPPEMIT '#undef SO_PROTOCOL_INFO' *)
  229. (*$HPPEMIT '#undef PVD_CONFIG' *)
  230. (*$HPPEMIT '#undef AF_INET6' *)
  231. (*$HPPEMIT '#undef PF_INET6' *)
  232. {$ENDIF}
  233. {$IFDEF FPC}
  234. {$IFDEF WIN32}
  235. {$ALIGN OFF}
  236. {$ELSE}
  237. {$PACKRECORDS C}
  238. {$ENDIF}
  239. {$ENDIF}
  240. interface
  241. uses
  242. SyncObjs, SysUtils, Classes,
  243. Windows;
  244. function InitSocketInterface(stack: String): Boolean;
  245. function DestroySocketInterface: Boolean;
  246. const
  247. {$IFDEF WINSOCK1}
  248. WinsockLevel = $0101;
  249. {$ELSE}
  250. WinsockLevel = $0202;
  251. {$ENDIF}
  252. type
  253. u_short = Word;
  254. u_int = Integer;
  255. u_long = Longint;
  256. pu_long = ^u_long;
  257. pu_short = ^u_short;
  258. {$IFDEF FPC}
  259. TSocket = ptruint;
  260. {$ELSE}
  261. {$IFDEF WIN64}
  262. TSocket = UINT_PTR;
  263. {$ELSE}
  264. TSocket = u_int;
  265. {$ENDIF}
  266. {$ENDIF}
  267. TAddrFamily = integer;
  268. TMemory = pointer;
  269. const
  270. {$IFDEF WINCE}
  271. DLLStackName = 'ws2.dll';
  272. {$ELSE}
  273. {$IFDEF WINSOCK1}
  274. DLLStackName = 'wsock32.dll';
  275. {$ELSE}
  276. DLLStackName = 'ws2_32.dll';
  277. {$ENDIF}
  278. {$ENDIF}
  279. DLLwship6 = 'wship6.dll';
  280. cLocalhost = '127.0.0.1';
  281. cAnyHost = '0.0.0.0';
  282. cBroadcast = '255.255.255.255';
  283. c6Localhost = '::1';
  284. c6AnyHost = '::0';
  285. c6Broadcast = 'ffff::1';
  286. cAnyPort = '0';
  287. const
  288. FD_SETSIZE = 64;
  289. type
  290. PFDSet = ^TFDSet;
  291. TFDSet = record
  292. fd_count: u_int;
  293. fd_array: array[0..FD_SETSIZE-1] of TSocket;
  294. end;
  295. const
  296. FIONREAD = $4004667f;
  297. FIONBIO = $8004667e;
  298. FIOASYNC = $8004667d;
  299. type
  300. PTimeVal = ^TTimeVal;
  301. TTimeVal = record
  302. tv_sec: Longint;
  303. tv_usec: Longint;
  304. end;
  305. const
  306. IPPROTO_IP = 0; { Dummy }
  307. IPPROTO_ICMP = 1; { Internet Control Message Protocol }
  308. IPPROTO_IGMP = 2; { Internet Group Management Protocol}
  309. IPPROTO_TCP = 6; { TCP }
  310. IPPROTO_UDP = 17; { User Datagram Protocol }
  311. IPPROTO_IPV6 = 41;
  312. IPPROTO_ICMPV6 = 58;
  313. IPPROTO_RM = 113;
  314. IPPROTO_RAW = 255;
  315. IPPROTO_MAX = 256;
  316. type
  317. PInAddr = ^TInAddr;
  318. TInAddr = record
  319. case integer of
  320. 0: (S_bytes: packed array [0..3] of byte);
  321. 1: (S_addr: u_long);
  322. end;
  323. PSockAddrIn = ^TSockAddrIn;
  324. TSockAddrIn = record
  325. case Integer of
  326. 0: (sin_family: u_short;
  327. sin_port: u_short;
  328. sin_addr: TInAddr;
  329. sin_zero: array[0..7] of byte);
  330. 1: (sa_family: u_short;
  331. sa_data: array[0..13] of byte)
  332. end;
  333. TIP_mreq = record
  334. imr_multiaddr: TInAddr; { IP multicast address of group }
  335. imr_interface: TInAddr; { local IP address of interface }
  336. end;
  337. PInAddr6 = ^TInAddr6;
  338. TInAddr6 = record
  339. case integer of
  340. 0: (S6_addr: packed array [0..15] of byte);
  341. 1: (u6_addr8: packed array [0..15] of byte);
  342. 2: (u6_addr16: packed array [0..7] of word);
  343. 3: (u6_addr32: packed array [0..3] of integer);
  344. end;
  345. PSockAddrIn6 = ^TSockAddrIn6;
  346. TSockAddrIn6 = record
  347. sin6_family: u_short; // AF_INET6
  348. sin6_port: u_short; // Transport level port number
  349. sin6_flowinfo: u_long; // IPv6 flow information
  350. sin6_addr: TInAddr6; // IPv6 address
  351. sin6_scope_id: u_long; // Scope Id: IF number for link-local
  352. // SITE id for site-local
  353. end;
  354. TIPv6_mreq = record
  355. ipv6mr_multiaddr: TInAddr6; // IPv6 multicast address.
  356. ipv6mr_interface: integer; // Interface index.
  357. padding: integer;
  358. end;
  359. PHostEnt = ^THostEnt;
  360. THostEnt = record
  361. h_name: PAnsiChar;
  362. h_aliases: ^PAnsiChar;
  363. h_addrtype: Smallint;
  364. h_length: Smallint;
  365. case integer of
  366. 0: (h_addr_list: ^PAnsiChar);
  367. 1: (h_addr: ^PInAddr);
  368. end;
  369. PNetEnt = ^TNetEnt;
  370. TNetEnt = record
  371. n_name: PAnsiChar;
  372. n_aliases: ^PAnsiChar;
  373. n_addrtype: Smallint;
  374. n_net: u_long;
  375. end;
  376. PServEnt = ^TServEnt;
  377. TServEnt = record
  378. s_name: PAnsiChar;
  379. s_aliases: ^PAnsiChar;
  380. {$ifdef WIN64}
  381. s_proto: PAnsiChar;
  382. s_port: Smallint;
  383. {$else}
  384. s_port: Smallint;
  385. s_proto: PAnsiChar;
  386. {$endif}
  387. end;
  388. PProtoEnt = ^TProtoEnt;
  389. TProtoEnt = record
  390. p_name: PAnsiChar;
  391. p_aliases: ^PAnsichar;
  392. p_proto: Smallint;
  393. end;
  394. const
  395. INADDR_ANY = $00000000;
  396. INADDR_LOOPBACK = $7F000001;
  397. INADDR_BROADCAST = $FFFFFFFF;
  398. INADDR_NONE = $FFFFFFFF;
  399. ADDR_ANY = INADDR_ANY;
  400. INVALID_SOCKET = TSocket(NOT(0));
  401. SOCKET_ERROR = -1;
  402. Const
  403. {$IFDEF WINSOCK1}
  404. IP_OPTIONS = 1;
  405. IP_MULTICAST_IF = 2; { set/get IP multicast interface }
  406. IP_MULTICAST_TTL = 3; { set/get IP multicast timetolive }
  407. IP_MULTICAST_LOOP = 4; { set/get IP multicast loopback }
  408. IP_ADD_MEMBERSHIP = 5; { add an IP group membership }
  409. IP_DROP_MEMBERSHIP = 6; { drop an IP group membership }
  410. IP_TTL = 7; { set/get IP Time To Live }
  411. IP_TOS = 8; { set/get IP Type Of Service }
  412. IP_DONTFRAGMENT = 9; { set/get IP Don't Fragment flag }
  413. {$ELSE}
  414. IP_OPTIONS = 1;
  415. IP_HDRINCL = 2;
  416. IP_TOS = 3; { set/get IP Type Of Service }
  417. IP_TTL = 4; { set/get IP Time To Live }
  418. IP_MULTICAST_IF = 9; { set/get IP multicast interface }
  419. IP_MULTICAST_TTL = 10; { set/get IP multicast timetolive }
  420. IP_MULTICAST_LOOP = 11; { set/get IP multicast loopback }
  421. IP_ADD_MEMBERSHIP = 12; { add an IP group membership }
  422. IP_DROP_MEMBERSHIP = 13; { drop an IP group membership }
  423. IP_DONTFRAGMENT = 14; { set/get IP Don't Fragment flag }
  424. {$ENDIF}
  425. IP_DEFAULT_MULTICAST_TTL = 1; { normally limit m'casts to 1 hop }
  426. IP_DEFAULT_MULTICAST_LOOP = 1; { normally hear sends if a member }
  427. IP_MAX_MEMBERSHIPS = 20; { per socket; must fit in one mbuf }
  428. SOL_SOCKET = $ffff; {options for socket level }
  429. { Option flags per-socket. }
  430. SO_DEBUG = $0001; { turn on debugging info recording }
  431. SO_ACCEPTCONN = $0002; { socket has had listen() }
  432. SO_REUSEADDR = $0004; { allow local address reuse }
  433. SO_KEEPALIVE = $0008; { keep connections alive }
  434. SO_DONTROUTE = $0010; { just use interface addresses }
  435. SO_BROADCAST = $0020; { permit sending of broadcast msgs }
  436. SO_USELOOPBACK = $0040; { bypass hardware when possible }
  437. SO_LINGER = $0080; { linger on close if data present }
  438. SO_OOBINLINE = $0100; { leave received OOB data in line }
  439. SO_DONTLINGER = $ff7f;
  440. { Additional options. }
  441. SO_SNDBUF = $1001; { send buffer size }
  442. SO_RCVBUF = $1002; { receive buffer size }
  443. SO_SNDLOWAT = $1003; { send low-water mark }
  444. SO_RCVLOWAT = $1004; { receive low-water mark }
  445. SO_SNDTIMEO = $1005; { send timeout }
  446. SO_RCVTIMEO = $1006; { receive timeout }
  447. SO_ERROR = $1007; { get error status and clear }
  448. SO_TYPE = $1008; { get socket type }
  449. { WinSock 2 extension -- new options }
  450. SO_GROUP_ID = $2001; { ID of a socket group}
  451. SO_GROUP_PRIORITY = $2002; { the relative priority within a group}
  452. SO_MAX_MSG_SIZE = $2003; { maximum message size }
  453. SO_PROTOCOL_INFOA = $2004; { WSAPROTOCOL_INFOA structure }
  454. SO_PROTOCOL_INFOW = $2005; { WSAPROTOCOL_INFOW structure }
  455. SO_PROTOCOL_INFO = SO_PROTOCOL_INFOA;
  456. PVD_CONFIG = $3001; {configuration info for service provider }
  457. { Option for opening sockets for synchronous access. }
  458. SO_OPENTYPE = $7008;
  459. SO_SYNCHRONOUS_ALERT = $10;
  460. SO_SYNCHRONOUS_NONALERT = $20;
  461. { Other NT-specific options. }
  462. SO_MAXDG = $7009;
  463. SO_MAXPATHDG = $700A;
  464. SO_UPDATE_ACCEPT_CONTEXT = $700B;
  465. SO_CONNECT_TIME = $700C;
  466. SOMAXCONN = $7fffffff;
  467. IPV6_UNICAST_HOPS = 8; // ???
  468. IPV6_MULTICAST_IF = 9; // set/get IP multicast i/f
  469. IPV6_MULTICAST_HOPS = 10; // set/get IP multicast ttl
  470. IPV6_MULTICAST_LOOP = 11; // set/get IP multicast loopback
  471. IPV6_JOIN_GROUP = 12; // add an IP group membership
  472. IPV6_LEAVE_GROUP = 13; // drop an IP group membership
  473. MSG_NOSIGNAL = 0;
  474. // getnameinfo constants
  475. NI_MAXHOST = 1025;
  476. NI_MAXSERV = 32;
  477. NI_NOFQDN = $1;
  478. NI_NUMERICHOST = $2;
  479. NI_NAMEREQD = $4;
  480. NI_NUMERICSERV = $8;
  481. NI_DGRAM = $10;
  482. const
  483. SOCK_STREAM = 1; { stream socket }
  484. SOCK_DGRAM = 2; { datagram socket }
  485. SOCK_RAW = 3; { raw-protocol interface }
  486. SOCK_RDM = 4; { reliably-delivered message }
  487. SOCK_SEQPACKET = 5; { sequenced packet stream }
  488. { TCP options. }
  489. TCP_NODELAY = $0001;
  490. { Address families. }
  491. AF_UNSPEC = 0; { unspecified }
  492. AF_INET = 2; { internetwork: UDP, TCP, etc. }
  493. AF_INET6 = 23; { Internetwork Version 6 }
  494. AF_MAX = 24;
  495. { Protocol families, same as address families for now. }
  496. PF_UNSPEC = AF_UNSPEC;
  497. PF_INET = AF_INET;
  498. PF_INET6 = AF_INET6;
  499. PF_MAX = AF_MAX;
  500. type
  501. { Structure used by kernel to store most addresses. }
  502. PSockAddr = ^TSockAddr;
  503. TSockAddr = TSockAddrIn;
  504. { Structure used by kernel to pass protocol information in raw sockets. }
  505. PSockProto = ^TSockProto;
  506. TSockProto = record
  507. sp_family: u_short;
  508. sp_protocol: u_short;
  509. end;
  510. type
  511. PAddrInfo = ^TAddrInfo;
  512. TAddrInfo = record
  513. ai_flags: integer; // AI_PASSIVE, AI_CANONNAME, AI_NUMERICHOST.
  514. ai_family: integer; // PF_xxx.
  515. ai_socktype: integer; // SOCK_xxx.
  516. ai_protocol: integer; // 0 or IPPROTO_xxx for IPv4 and IPv6.
  517. ai_addrlen: u_int; // Length of ai_addr.
  518. ai_canonname: PAnsiChar; // Canonical name for nodename.
  519. ai_addr: PSockAddr; // Binary address.
  520. ai_next: PAddrInfo; // Next structure in linked list.
  521. end;
  522. const
  523. // Flags used in "hints" argument to getaddrinfo().
  524. AI_PASSIVE = $1; // Socket address will be used in bind() call.
  525. AI_CANONNAME = $2; // Return canonical name in first ai_canonname.
  526. AI_NUMERICHOST = $4; // Nodename must be a numeric address string.
  527. type
  528. { Structure used for manipulating linger option. }
  529. PLinger = ^TLinger;
  530. TLinger = record
  531. l_onoff: u_short;
  532. l_linger: u_short;
  533. end;
  534. const
  535. MSG_OOB = $01; // Process out-of-band data.
  536. MSG_PEEK = $02; // Peek at incoming messages.
  537. const
  538. { All Windows Sockets error constants are biased by WSABASEERR from the "normal" }
  539. WSABASEERR = 10000;
  540. { Windows Sockets definitions of regular Microsoft C error constants }
  541. WSAEINTR = (WSABASEERR+4);
  542. WSAEBADF = (WSABASEERR+9);
  543. WSAEACCES = (WSABASEERR+13);
  544. WSAEFAULT = (WSABASEERR+14);
  545. WSAEINVAL = (WSABASEERR+22);
  546. WSAEMFILE = (WSABASEERR+24);
  547. { Windows Sockets definitions of regular Berkeley error constants }
  548. WSAEWOULDBLOCK = (WSABASEERR+35);
  549. WSAEINPROGRESS = (WSABASEERR+36);
  550. WSAEALREADY = (WSABASEERR+37);
  551. WSAENOTSOCK = (WSABASEERR+38);
  552. WSAEDESTADDRREQ = (WSABASEERR+39);
  553. WSAEMSGSIZE = (WSABASEERR+40);
  554. WSAEPROTOTYPE = (WSABASEERR+41);
  555. WSAENOPROTOOPT = (WSABASEERR+42);
  556. WSAEPROTONOSUPPORT = (WSABASEERR+43);
  557. WSAESOCKTNOSUPPORT = (WSABASEERR+44);
  558. WSAEOPNOTSUPP = (WSABASEERR+45);
  559. WSAEPFNOSUPPORT = (WSABASEERR+46);
  560. WSAEAFNOSUPPORT = (WSABASEERR+47);
  561. WSAEADDRINUSE = (WSABASEERR+48);
  562. WSAEADDRNOTAVAIL = (WSABASEERR+49);
  563. WSAENETDOWN = (WSABASEERR+50);
  564. WSAENETUNREACH = (WSABASEERR+51);
  565. WSAENETRESET = (WSABASEERR+52);
  566. WSAECONNABORTED = (WSABASEERR+53);
  567. WSAECONNRESET = (WSABASEERR+54);
  568. WSAENOBUFS = (WSABASEERR+55);
  569. WSAEISCONN = (WSABASEERR+56);
  570. WSAENOTCONN = (WSABASEERR+57);
  571. WSAESHUTDOWN = (WSABASEERR+58);
  572. WSAETOOMANYREFS = (WSABASEERR+59);
  573. WSAETIMEDOUT = (WSABASEERR+60);
  574. WSAECONNREFUSED = (WSABASEERR+61);
  575. WSAELOOP = (WSABASEERR+62);
  576. WSAENAMETOOLONG = (WSABASEERR+63);
  577. WSAEHOSTDOWN = (WSABASEERR+64);
  578. WSAEHOSTUNREACH = (WSABASEERR+65);
  579. WSAENOTEMPTY = (WSABASEERR+66);
  580. WSAEPROCLIM = (WSABASEERR+67);
  581. WSAEUSERS = (WSABASEERR+68);
  582. WSAEDQUOT = (WSABASEERR+69);
  583. WSAESTALE = (WSABASEERR+70);
  584. WSAEREMOTE = (WSABASEERR+71);
  585. { Extended Windows Sockets error constant definitions }
  586. WSASYSNOTREADY = (WSABASEERR+91);
  587. WSAVERNOTSUPPORTED = (WSABASEERR+92);
  588. WSANOTINITIALISED = (WSABASEERR+93);
  589. WSAEDISCON = (WSABASEERR+101);
  590. WSAENOMORE = (WSABASEERR+102);
  591. WSAECANCELLED = (WSABASEERR+103);
  592. WSAEEINVALIDPROCTABLE = (WSABASEERR+104);
  593. WSAEINVALIDPROVIDER = (WSABASEERR+105);
  594. WSAEPROVIDERFAILEDINIT = (WSABASEERR+106);
  595. WSASYSCALLFAILURE = (WSABASEERR+107);
  596. WSASERVICE_NOT_FOUND = (WSABASEERR+108);
  597. WSATYPE_NOT_FOUND = (WSABASEERR+109);
  598. WSA_E_NO_MORE = (WSABASEERR+110);
  599. WSA_E_CANCELLED = (WSABASEERR+111);
  600. WSAEREFUSED = (WSABASEERR+112);
  601. { Error return codes from gethostbyname() and gethostbyaddr()
  602. (when using the resolver). Note that these errors are
  603. retrieved via WSAGetLastError() and must therefore follow
  604. the rules for avoiding clashes with error numbers from
  605. specific implementations or language run-time systems.
  606. For this reason the codes are based at WSABASEERR+1001.
  607. Note also that [WSA]NO_ADDRESS is defined only for
  608. compatibility purposes. }
  609. { Authoritative Answer: Host not found }
  610. WSAHOST_NOT_FOUND = (WSABASEERR+1001);
  611. HOST_NOT_FOUND = WSAHOST_NOT_FOUND;
  612. { Non-Authoritative: Host not found, or SERVERFAIL }
  613. WSATRY_AGAIN = (WSABASEERR+1002);
  614. TRY_AGAIN = WSATRY_AGAIN;
  615. { Non recoverable errors, FORMERR, REFUSED, NOTIMP }
  616. WSANO_RECOVERY = (WSABASEERR+1003);
  617. NO_RECOVERY = WSANO_RECOVERY;
  618. { Valid name, no data record of requested type }
  619. WSANO_DATA = (WSABASEERR+1004);
  620. NO_DATA = WSANO_DATA;
  621. { no address, look for MX record }
  622. WSANO_ADDRESS = WSANO_DATA;
  623. NO_ADDRESS = WSANO_ADDRESS;
  624. EWOULDBLOCK = WSAEWOULDBLOCK;
  625. EINPROGRESS = WSAEINPROGRESS;
  626. EALREADY = WSAEALREADY;
  627. ENOTSOCK = WSAENOTSOCK;
  628. EDESTADDRREQ = WSAEDESTADDRREQ;
  629. EMSGSIZE = WSAEMSGSIZE;
  630. EPROTOTYPE = WSAEPROTOTYPE;
  631. ENOPROTOOPT = WSAENOPROTOOPT;
  632. EPROTONOSUPPORT = WSAEPROTONOSUPPORT;
  633. ESOCKTNOSUPPORT = WSAESOCKTNOSUPPORT;
  634. EOPNOTSUPP = WSAEOPNOTSUPP;
  635. EPFNOSUPPORT = WSAEPFNOSUPPORT;
  636. EAFNOSUPPORT = WSAEAFNOSUPPORT;
  637. EADDRINUSE = WSAEADDRINUSE;
  638. EADDRNOTAVAIL = WSAEADDRNOTAVAIL;
  639. ENETDOWN = WSAENETDOWN;
  640. ENETUNREACH = WSAENETUNREACH;
  641. ENETRESET = WSAENETRESET;
  642. ECONNABORTED = WSAECONNABORTED;
  643. ECONNRESET = WSAECONNRESET;
  644. ENOBUFS = WSAENOBUFS;
  645. EISCONN = WSAEISCONN;
  646. ENOTCONN = WSAENOTCONN;
  647. ESHUTDOWN = WSAESHUTDOWN;
  648. ETOOMANYREFS = WSAETOOMANYREFS;
  649. ETIMEDOUT = WSAETIMEDOUT;
  650. ECONNREFUSED = WSAECONNREFUSED;
  651. ELOOP = WSAELOOP;
  652. ENAMETOOLONG = WSAENAMETOOLONG;
  653. EHOSTDOWN = WSAEHOSTDOWN;
  654. EHOSTUNREACH = WSAEHOSTUNREACH;
  655. ENOTEMPTY = WSAENOTEMPTY;
  656. EPROCLIM = WSAEPROCLIM;
  657. EUSERS = WSAEUSERS;
  658. EDQUOT = WSAEDQUOT;
  659. ESTALE = WSAESTALE;
  660. EREMOTE = WSAEREMOTE;
  661. EAI_ADDRFAMILY = 1; // Address family for nodename not supported.
  662. EAI_AGAIN = 2; // Temporary failure in name resolution.
  663. EAI_BADFLAGS = 3; // Invalid value for ai_flags.
  664. EAI_FAIL = 4; // Non-recoverable failure in name resolution.
  665. EAI_FAMILY = 5; // Address family ai_family not supported.
  666. EAI_MEMORY = 6; // Memory allocation failure.
  667. EAI_NODATA = 7; // No address associated with nodename.
  668. EAI_NONAME = 8; // Nodename nor servname provided, or not known.
  669. EAI_SERVICE = 9; // Servname not supported for ai_socktype.
  670. EAI_SOCKTYPE = 10; // Socket type ai_socktype not supported.
  671. EAI_SYSTEM = 11; // System error returned in errno.
  672. const
  673. WSADESCRIPTION_LEN = 256;
  674. WSASYS_STATUS_LEN = 128;
  675. type
  676. PWSAData = ^TWSAData;
  677. TWSAData = record
  678. wVersion: Word;
  679. wHighVersion: Word;
  680. {$ifdef win64}
  681. iMaxSockets : Word;
  682. iMaxUdpDg : Word;
  683. lpVendorInfo : PAnsiChar;
  684. szDescription : array[0..WSADESCRIPTION_LEN] of AnsiChar;
  685. szSystemStatus : array[0..WSASYS_STATUS_LEN] of AnsiChar;
  686. {$else}
  687. szDescription: array[0..WSADESCRIPTION_LEN] of AnsiChar;
  688. szSystemStatus: array[0..WSASYS_STATUS_LEN] of AnsiChar;
  689. iMaxSockets: Word;
  690. iMaxUdpDg: Word;
  691. lpVendorInfo: PAnsiChar;
  692. {$endif}
  693. end;
  694. function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean;
  695. function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean;
  696. function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean;
  697. function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean;
  698. function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean;
  699. function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6):boolean;
  700. procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6);
  701. procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6);
  702. var
  703. in6addr_any, in6addr_loopback : TInAddr6;
  704. procedure FD_CLR(Socket: TSocket; var FDSet: TFDSet);
  705. function FD_ISSET(Socket: TSocket; var FDSet: TFDSet): Boolean;
  706. procedure FD_SET(Socket: TSocket; var FDSet: TFDSet);
  707. procedure FD_ZERO(var FDSet: TFDSet);
  708. {=============================================================================}
  709. type
  710. TWSAStartup = function(wVersionRequired: Word; var WSData: TWSAData): Integer;
  711. stdcall;
  712. TWSACleanup = function: Integer;
  713. stdcall;
  714. TWSAGetLastError = function: Integer;
  715. stdcall;
  716. TGetServByName = function(name, proto: PAnsiChar): PServEnt;
  717. stdcall;
  718. TGetServByPort = function(port: Integer; proto: PAnsiChar): PServEnt;
  719. stdcall;
  720. TGetProtoByName = function(name: PAnsiChar): PProtoEnt;
  721. stdcall;
  722. TGetProtoByNumber = function(proto: Integer): PProtoEnt;
  723. stdcall;
  724. TGetHostByName = function(name: PAnsiChar): PHostEnt;
  725. stdcall;
  726. TGetHostByAddr = function(addr: Pointer; len, Struc: Integer): PHostEnt;
  727. stdcall;
  728. TGetHostName = function(name: PAnsiChar; len: Integer): Integer;
  729. stdcall;
  730. TShutdown = function(s: TSocket; how: Integer): Integer;
  731. stdcall;
  732. TSetSockOpt = function(s: TSocket; level, optname: Integer; optval: PAnsiChar;
  733. optlen: Integer): Integer;
  734. stdcall;
  735. TGetSockOpt = function(s: TSocket; level, optname: Integer; optval: PAnsiChar;
  736. var optlen: Integer): Integer;
  737. stdcall;
  738. TSendTo = function(s: TSocket; const Buf; len, flags: Integer; addrto: PSockAddr;
  739. tolen: Integer): Integer;
  740. stdcall;
  741. TSend = function(s: TSocket; const Buf; len, flags: Integer): Integer;
  742. stdcall;
  743. TRecv = function(s: TSocket; var Buf; len, flags: Integer): Integer;
  744. stdcall;
  745. TRecvFrom = function(s: TSocket; var Buf; len, flags: Integer; from: PSockAddr;
  746. var fromlen: Integer): Integer;
  747. stdcall;
  748. Tntohs = function(netshort: u_short): u_short;
  749. stdcall;
  750. Tntohl = function(netlong: u_long): u_long;
  751. stdcall;
  752. TListen = function(s: TSocket; backlog: Integer): Integer;
  753. stdcall;
  754. TIoctlSocket = function(s: TSocket; cmd: DWORD; var arg: Integer): Integer;
  755. stdcall;
  756. TInet_ntoa = function(inaddr: TInAddr): PAnsiChar;
  757. stdcall;
  758. TInet_addr = function(cp: PAnsiChar): u_long;
  759. stdcall;
  760. Thtons = function(hostshort: u_short): u_short;
  761. stdcall;
  762. Thtonl = function(hostlong: u_long): u_long;
  763. stdcall;
  764. TGetSockName = function(s: TSocket; name: PSockAddr; var namelen: Integer): Integer;
  765. stdcall;
  766. TGetPeerName = function(s: TSocket; name: PSockAddr; var namelen: Integer): Integer;
  767. stdcall;
  768. TConnect = function(s: TSocket; name: PSockAddr; namelen: Integer): Integer;
  769. stdcall;
  770. TCloseSocket = function(s: TSocket): Integer;
  771. stdcall;
  772. TBind = function(s: TSocket; addr: PSockAddr; namelen: Integer): Integer;
  773. stdcall;
  774. TAccept = function(s: TSocket; addr: PSockAddr; var addrlen: Integer): TSocket;
  775. stdcall;
  776. TTSocket = function(af, Struc, Protocol: Integer): TSocket;
  777. stdcall;
  778. TSelect = function(nfds: Integer; readfds, writefds, exceptfds: PFDSet;
  779. timeout: PTimeVal): Longint;
  780. stdcall;
  781. TGetAddrInfo = function(NodeName: PAnsiChar; ServName: PAnsiChar; Hints: PAddrInfo;
  782. var Addrinfo: PAddrInfo): integer;
  783. stdcall;
  784. TFreeAddrInfo = procedure(ai: PAddrInfo);
  785. stdcall;
  786. TGetNameInfo = function( addr: PSockAddr; namelen: Integer; host: PAnsiChar;
  787. hostlen: DWORD; serv: PAnsiChar; servlen: DWORD; flags: integer): integer;
  788. stdcall;
  789. T__WSAFDIsSet = function (s: TSocket; var FDSet: TFDSet): Bool;
  790. stdcall;
  791. TWSAIoctl = function (s: TSocket; dwIoControlCode: DWORD; lpvInBuffer: Pointer;
  792. cbInBuffer: DWORD; lpvOutBuffer: Pointer; cbOutBuffer: DWORD;
  793. lpcbBytesReturned: PDWORD; lpOverlapped: Pointer;
  794. lpCompletionRoutine: pointer): u_int;
  795. stdcall;
  796. var
  797. WSAStartup: TWSAStartup = nil;
  798. WSACleanup: TWSACleanup = nil;
  799. WSAGetLastError: TWSAGetLastError = nil;
  800. GetServByName: TGetServByName = nil;
  801. GetServByPort: TGetServByPort = nil;
  802. GetProtoByName: TGetProtoByName = nil;
  803. GetProtoByNumber: TGetProtoByNumber = nil;
  804. GetHostByName: TGetHostByName = nil;
  805. GetHostByAddr: TGetHostByAddr = nil;
  806. ssGetHostName: TGetHostName = nil;
  807. Shutdown: TShutdown = nil;
  808. SetSockOpt: TSetSockOpt = nil;
  809. GetSockOpt: TGetSockOpt = nil;
  810. ssSendTo: TSendTo = nil;
  811. ssSend: TSend = nil;
  812. ssRecv: TRecv = nil;
  813. ssRecvFrom: TRecvFrom = nil;
  814. ntohs: Tntohs = nil;
  815. ntohl: Tntohl = nil;
  816. Listen: TListen = nil;
  817. IoctlSocket: TIoctlSocket = nil;
  818. Inet_ntoa: TInet_ntoa = nil;
  819. Inet_addr: TInet_addr = nil;
  820. htons: Thtons = nil;
  821. htonl: Thtonl = nil;
  822. ssGetSockName: TGetSockName = nil;
  823. ssGetPeerName: TGetPeerName = nil;
  824. ssConnect: TConnect = nil;
  825. CloseSocket: TCloseSocket = nil;
  826. ssBind: TBind = nil;
  827. ssAccept: TAccept = nil;
  828. Socket: TTSocket = nil;
  829. Select: TSelect = nil;
  830. GetAddrInfo: TGetAddrInfo = nil;
  831. FreeAddrInfo: TFreeAddrInfo = nil;
  832. GetNameInfo: TGetNameInfo = nil;
  833. __WSAFDIsSet: T__WSAFDIsSet = nil;
  834. WSAIoctl: TWSAIoctl = nil;
  835. var
  836. SynSockCS: SyncObjs.TCriticalSection;
  837. SockEnhancedApi: Boolean;
  838. SockWship6Api: Boolean;
  839. type
  840. TVarSin = packed record
  841. case integer of
  842. 0: (AddressFamily: u_short);
  843. 1: (
  844. case sin_family: u_short of
  845. AF_INET: (sin_port: u_short;
  846. sin_addr: TInAddr;
  847. sin_zero: array[0..7] of byte);
  848. AF_INET6: (sin6_port: u_short;
  849. sin6_flowinfo: u_long;
  850. sin6_addr: TInAddr6;
  851. sin6_scope_id: u_long);
  852. );
  853. end;
  854. function SizeOfVarSin(sin: TVarSin): integer;
  855. function Bind(s: TSocket; const addr: TVarSin): Integer;
  856. function Connect(s: TSocket; const name: TVarSin): Integer;
  857. function GetSockName(s: TSocket; var name: TVarSin): Integer;
  858. function GetPeerName(s: TSocket; var name: TVarSin): Integer;
  859. function GetHostName: AnsiString;
  860. function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  861. function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  862. function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer;
  863. function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer;
  864. function Accept(s: TSocket; var addr: TVarSin): TSocket;
  865. function IsNewApi(Family: integer): Boolean;
  866. function SetVarSin(var Sin: TVarSin; IP, Port: AnsiString; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer;
  867. function GetSinIP(Sin: TVarSin): AnsiString;
  868. function GetSinPort(Sin: TVarSin): Integer;
  869. procedure ResolveNameToIP(Name: AnsiString; Family, SockProtocol, SockType: integer; const IPList: TStrings);
  870. function ResolveIPToName(IP: AnsiString; Family, SockProtocol, SockType: integer): AnsiString;
  871. function ResolvePort(Port: AnsiString; Family, SockProtocol, SockType: integer): Word;
  872. {==============================================================================}
  873. implementation
  874. var
  875. SynSockCount: Integer = 0;
  876. LibHandle: THandle = 0;
  877. Libwship6Handle: THandle = 0;
  878. function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean;
  879. begin
  880. Result := ((a^.u6_addr32[0] = 0) and (a^.u6_addr32[1] = 0) and
  881. (a^.u6_addr32[2] = 0) and (a^.u6_addr32[3] = 0));
  882. end;
  883. function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean;
  884. begin
  885. Result := ((a^.u6_addr32[0] = 0) and (a^.u6_addr32[1] = 0) and
  886. (a^.u6_addr32[2] = 0) and
  887. (a^.u6_addr8[12] = 0) and (a^.u6_addr8[13] = 0) and
  888. (a^.u6_addr8[14] = 0) and (a^.u6_addr8[15] = 1));
  889. end;
  890. function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean;
  891. begin
  892. Result := ((a^.u6_addr8[0] = $FE) and (a^.u6_addr8[1] = $80));
  893. end;
  894. function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean;
  895. begin
  896. Result := ((a^.u6_addr8[0] = $FE) and (a^.u6_addr8[1] = $C0));
  897. end;
  898. function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean;
  899. begin
  900. Result := (a^.u6_addr8[0] = $FF);
  901. end;
  902. function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6): boolean;
  903. begin
  904. Result := (CompareMem( a, b, sizeof(TInAddr6)));
  905. end;
  906. procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6);
  907. begin
  908. FillChar(a^, sizeof(TInAddr6), 0);
  909. end;
  910. procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6);
  911. begin
  912. FillChar(a^, sizeof(TInAddr6), 0);
  913. a^.u6_addr8[15] := 1;
  914. end;
  915. {=============================================================================}
  916. procedure FD_CLR(Socket: TSocket; var FDSet: TFDSet);
  917. var
  918. I: Integer;
  919. begin
  920. I := 0;
  921. while I < FDSet.fd_count do
  922. begin
  923. if FDSet.fd_array[I] = Socket then
  924. begin
  925. while I < FDSet.fd_count - 1 do
  926. begin
  927. FDSet.fd_array[I] := FDSet.fd_array[I + 1];
  928. Inc(I);
  929. end;
  930. Dec(FDSet.fd_count);
  931. Break;
  932. end;
  933. Inc(I);
  934. end;
  935. end;
  936. function FD_ISSET(Socket: TSocket; var FDSet: TFDSet): Boolean;
  937. begin
  938. Result := __WSAFDIsSet(Socket, FDSet);
  939. end;
  940. procedure FD_SET(Socket: TSocket; var FDSet: TFDSet);
  941. begin
  942. if FDSet.fd_count < FD_SETSIZE then
  943. begin
  944. FDSet.fd_array[FDSet.fd_count] := Socket;
  945. Inc(FDSet.fd_count);
  946. end;
  947. end;
  948. procedure FD_ZERO(var FDSet: TFDSet);
  949. begin
  950. FDSet.fd_count := 0;
  951. end;
  952. {=============================================================================}
  953. function SizeOfVarSin(sin: TVarSin): integer;
  954. begin
  955. case sin.sin_family of
  956. AF_INET:
  957. Result := SizeOf(TSockAddrIn);
  958. AF_INET6:
  959. Result := SizeOf(TSockAddrIn6);
  960. else
  961. Result := 0;
  962. end;
  963. end;
  964. {=============================================================================}
  965. function Bind(s: TSocket; const addr: TVarSin): Integer;
  966. begin
  967. Result := ssBind(s, @addr, SizeOfVarSin(addr));
  968. end;
  969. function Connect(s: TSocket; const name: TVarSin): Integer;
  970. begin
  971. Result := ssConnect(s, @name, SizeOfVarSin(name));
  972. end;
  973. function GetSockName(s: TSocket; var name: TVarSin): Integer;
  974. var
  975. len: integer;
  976. begin
  977. len := SizeOf(name);
  978. FillChar(name, len, 0);
  979. Result := ssGetSockName(s, @name, Len);
  980. end;
  981. function GetPeerName(s: TSocket; var name: TVarSin): Integer;
  982. var
  983. len: integer;
  984. begin
  985. len := SizeOf(name);
  986. FillChar(name, len, 0);
  987. Result := ssGetPeerName(s, @name, Len);
  988. end;
  989. function GetHostName: AnsiString;
  990. var
  991. s: AnsiString;
  992. begin
  993. Result := '';
  994. setlength(s, 255);
  995. ssGetHostName(pAnsichar(s), Length(s) - 1);
  996. Result := PAnsichar(s);
  997. end;
  998. function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  999. begin
  1000. Result := ssSend(s, Buf^, len, flags);
  1001. end;
  1002. function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  1003. begin
  1004. Result := ssRecv(s, Buf^, len, flags);
  1005. end;
  1006. function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer;
  1007. begin
  1008. Result := ssSendTo(s, Buf^, len, flags, @addrto, SizeOfVarSin(addrto));
  1009. end;
  1010. function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer;
  1011. var
  1012. x: integer;
  1013. begin
  1014. x := SizeOf(from);
  1015. Result := ssRecvFrom(s, Buf^, len, flags, @from, x);
  1016. end;
  1017. function Accept(s: TSocket; var addr: TVarSin): TSocket;
  1018. var
  1019. x: integer;
  1020. begin
  1021. x := SizeOf(addr);
  1022. Result := ssAccept(s, @addr, x);
  1023. end;
  1024. {=============================================================================}
  1025. function IsNewApi(Family: integer): Boolean;
  1026. begin
  1027. Result := SockEnhancedApi;
  1028. if not Result then
  1029. Result := (Family = AF_INET6) and SockWship6Api;
  1030. end;
  1031. function SetVarSin(var Sin: TVarSin; IP, Port: AnsiString; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer;
  1032. type
  1033. pu_long = ^u_long;
  1034. var
  1035. ProtoEnt: PProtoEnt;
  1036. ServEnt: PServEnt;
  1037. HostEnt: PHostEnt;
  1038. r: integer;
  1039. Hints1, Hints2: TAddrInfo;
  1040. Sin1, Sin2: TVarSin;
  1041. TwoPass: boolean;
  1042. function GetAddr(const IP, port: AnsiString; Hints: TAddrInfo; var Sin: TVarSin): integer;
  1043. var
  1044. Addr: PAddrInfo;
  1045. begin
  1046. Addr := nil;
  1047. try
  1048. FillChar(Sin, Sizeof(Sin), 0);
  1049. if Hints.ai_socktype = SOCK_RAW then
  1050. begin
  1051. Hints.ai_socktype := 0;
  1052. Hints.ai_protocol := 0;
  1053. Result := synsock.GetAddrInfo(PAnsiChar(IP), nil, @Hints, Addr);
  1054. end
  1055. else
  1056. begin
  1057. if (IP = cAnyHost) or (IP = c6AnyHost) then
  1058. begin
  1059. Hints.ai_flags := AI_PASSIVE;
  1060. Result := synsock.GetAddrInfo(nil, PAnsiChar(Port), @Hints, Addr);
  1061. end
  1062. else
  1063. if (IP = cLocalhost) or (IP = c6Localhost) then
  1064. begin
  1065. Result := synsock.GetAddrInfo(nil, PAnsiChar(Port), @Hints, Addr);
  1066. end
  1067. else
  1068. begin
  1069. Result := synsock.GetAddrInfo(PAnsiChar(IP), PAnsiChar(Port), @Hints, Addr);
  1070. end;
  1071. end;
  1072. if Result = 0 then
  1073. if (Addr <> nil) then
  1074. Move(Addr^.ai_addr^, Sin, Addr^.ai_addrlen);
  1075. finally
  1076. if Assigned(Addr) then
  1077. synsock.FreeAddrInfo(Addr);
  1078. end;
  1079. end;
  1080. begin
  1081. Result := 0;
  1082. FillChar(Sin, Sizeof(Sin), 0);
  1083. if not IsNewApi(family) then
  1084. begin
  1085. SynSockCS.Enter;
  1086. try
  1087. Sin.sin_family := AF_INET;
  1088. ProtoEnt := synsock.GetProtoByNumber(SockProtocol);
  1089. ServEnt := nil;
  1090. if (ProtoEnt <> nil) and (StrToIntDef(string(Port),-1) =-1) then
  1091. ServEnt := synsock.GetServByName(PAnsiChar(Port), ProtoEnt^.p_name);
  1092. if ServEnt = nil then
  1093. Sin.sin_port := synsock.htons(StrToIntDef(string(Port), 0))
  1094. else
  1095. Sin.sin_port := ServEnt^.s_port;
  1096. if IP = cBroadcast then
  1097. Sin.sin_addr.s_addr := u_long(INADDR_BROADCAST)
  1098. else
  1099. begin
  1100. Sin.sin_addr.s_addr := synsock.inet_addr(PAnsiChar(IP));
  1101. if Sin.sin_addr.s_addr = u_long(INADDR_NONE) then
  1102. begin
  1103. HostEnt := synsock.GetHostByName(PAnsiChar(IP));
  1104. Result := synsock.WSAGetLastError;
  1105. if HostEnt <> nil then
  1106. Sin.sin_addr.S_addr := u_long(Pu_long(HostEnt^.h_addr_list^)^);
  1107. end;
  1108. end;
  1109. finally
  1110. SynSockCS.Leave;
  1111. end;
  1112. end
  1113. else
  1114. begin
  1115. FillChar(Hints1, Sizeof(Hints1), 0);
  1116. FillChar(Hints2, Sizeof(Hints2), 0);
  1117. TwoPass := False;
  1118. if Family = AF_UNSPEC then
  1119. begin
  1120. if PreferIP4 then
  1121. begin
  1122. Hints1.ai_family := AF_INET;
  1123. Hints2.ai_family := AF_INET6;
  1124. TwoPass := True;
  1125. end
  1126. else
  1127. begin
  1128. Hints2.ai_family := AF_INET;
  1129. Hints1.ai_family := AF_INET6;
  1130. TwoPass := True;
  1131. end;
  1132. end
  1133. else
  1134. Hints1.ai_family := Family;
  1135. Hints1.ai_socktype := SockType;
  1136. Hints1.ai_protocol := SockProtocol;
  1137. Hints2.ai_socktype := Hints1.ai_socktype;
  1138. Hints2.ai_protocol := Hints1.ai_protocol;
  1139. r := GetAddr(IP, Port, Hints1, Sin1);
  1140. Result := r;
  1141. sin := sin1;
  1142. if r <> 0 then
  1143. if TwoPass then
  1144. begin
  1145. r := GetAddr(IP, Port, Hints2, Sin2);
  1146. Result := r;
  1147. if r = 0 then
  1148. sin := sin2;
  1149. end;
  1150. end;
  1151. end;
  1152. function GetSinIP(Sin: TVarSin): AnsiString;
  1153. var
  1154. p: PAnsiChar;
  1155. host, serv: AnsiString;
  1156. hostlen, servlen: integer;
  1157. r: integer;
  1158. begin
  1159. Result := '';
  1160. if not IsNewApi(Sin.AddressFamily) then
  1161. begin
  1162. p := synsock.inet_ntoa(Sin.sin_addr);
  1163. if p <> nil then
  1164. Result := p;
  1165. end
  1166. else
  1167. begin
  1168. hostlen := NI_MAXHOST;
  1169. servlen := NI_MAXSERV;
  1170. setlength(host, hostlen);
  1171. setlength(serv, servlen);
  1172. r := getnameinfo(@sin, SizeOfVarSin(sin), PAnsiChar(host), hostlen,
  1173. PAnsiChar(serv), servlen, NI_NUMERICHOST + NI_NUMERICSERV);
  1174. if r = 0 then
  1175. Result := PAnsiChar(host);
  1176. end;
  1177. end;
  1178. function GetSinPort(Sin: TVarSin): Integer;
  1179. begin
  1180. if (Sin.sin_family = AF_INET6) then
  1181. Result := synsock.ntohs(Sin.sin6_port)
  1182. else
  1183. Result := synsock.ntohs(Sin.sin_port);
  1184. end;
  1185. procedure ResolveNameToIP(Name: AnsiString; Family, SockProtocol, SockType: integer; const IPList: TStrings);
  1186. type
  1187. TaPInAddr = array[0..250] of PInAddr;
  1188. PaPInAddr = ^TaPInAddr;
  1189. var
  1190. Hints: TAddrInfo;
  1191. Addr: PAddrInfo;
  1192. AddrNext: PAddrInfo;
  1193. r: integer;
  1194. host, serv: AnsiString;
  1195. hostlen, servlen: integer;
  1196. RemoteHost: PHostEnt;
  1197. IP: u_long;
  1198. PAdrPtr: PaPInAddr;
  1199. i: Integer;
  1200. s: String;
  1201. InAddr: TInAddr;
  1202. begin
  1203. IPList.Clear;
  1204. if not IsNewApi(Family) then
  1205. begin
  1206. IP := synsock.inet_addr(PAnsiChar(Name));
  1207. if IP = u_long(INADDR_NONE) then
  1208. begin
  1209. SynSockCS.Enter;
  1210. try
  1211. RemoteHost := synsock.GetHostByName(PAnsiChar(Name));
  1212. if RemoteHost <> nil then
  1213. begin
  1214. PAdrPtr := PAPInAddr(RemoteHost^.h_addr_list);
  1215. i := 0;
  1216. while PAdrPtr^[i] <> nil do
  1217. begin
  1218. InAddr := PAdrPtr^[i]^;
  1219. s := Format('%d.%d.%d.%d', [InAddr.S_bytes[0], InAddr.S_bytes[1],
  1220. InAddr.S_bytes[2], InAddr.S_bytes[3]]);
  1221. IPList.Add(s);
  1222. Inc(i);
  1223. end;
  1224. end;
  1225. finally
  1226. SynSockCS.Leave;
  1227. end;
  1228. end
  1229. else
  1230. IPList.Add(string(Name));
  1231. end
  1232. else
  1233. begin
  1234. Addr := nil;
  1235. try
  1236. FillChar(Hints, Sizeof(Hints), 0);
  1237. Hints.ai_family := AF_UNSPEC;
  1238. Hints.ai_socktype := SockType;
  1239. Hints.ai_protocol := SockProtocol;
  1240. Hints.ai_flags := 0;
  1241. r := synsock.GetAddrInfo(PAnsiChar(Name), nil, @Hints, Addr);
  1242. if r = 0 then
  1243. begin
  1244. AddrNext := Addr;
  1245. while not(AddrNext = nil) do
  1246. begin
  1247. if not(((Family = AF_INET6) and (AddrNext^.ai_family = AF_INET))
  1248. or ((Family = AF_INET) and (AddrNext^.ai_family = AF_INET6))) then
  1249. begin
  1250. hostlen := NI_MAXHOST;
  1251. servlen := NI_MAXSERV;
  1252. setlength(host, hostlen);
  1253. setlength(serv, servlen);
  1254. r := getnameinfo(AddrNext^.ai_addr, AddrNext^.ai_addrlen,
  1255. PAnsiChar(host), hostlen, PAnsiChar(serv), servlen,
  1256. NI_NUMERICHOST + NI_NUMERICSERV);
  1257. if r = 0 then
  1258. begin
  1259. host := PAnsiChar(host);
  1260. IPList.Add(string(host));
  1261. end;
  1262. end;
  1263. AddrNext := AddrNext^.ai_next;
  1264. end;
  1265. end;
  1266. finally
  1267. if Assigned(Addr) then
  1268. synsock.FreeAddrInfo(Addr);
  1269. end;
  1270. end;
  1271. if IPList.Count = 0 then
  1272. IPList.Add(cAnyHost);
  1273. end;
  1274. function ResolvePort(Port: AnsiString; Family, SockProtocol, SockType: integer): Word;
  1275. var
  1276. ProtoEnt: PProtoEnt;
  1277. ServEnt: PServEnt;
  1278. Hints: TAddrInfo;
  1279. Addr: PAddrInfo;
  1280. r: integer;
  1281. begin
  1282. Result := 0;
  1283. if not IsNewApi(Family) then
  1284. begin
  1285. SynSockCS.Enter;
  1286. try
  1287. ProtoEnt := synsock.GetProtoByNumber(SockProtocol);
  1288. ServEnt := nil;
  1289. if ProtoEnt <> nil then
  1290. ServEnt := synsock.GetServByName(PAnsiChar(Port), ProtoEnt^.p_name);
  1291. if ServEnt = nil then
  1292. Result := StrToIntDef(string(Port), 0)
  1293. else
  1294. Result := synsock.htons(ServEnt^.s_port);
  1295. finally
  1296. SynSockCS.Leave;
  1297. end;
  1298. end
  1299. else
  1300. begin
  1301. Addr := nil;
  1302. try
  1303. FillChar(Hints, Sizeof(Hints), 0);
  1304. Hints.ai_family := AF_UNSPEC;
  1305. Hints.ai_socktype := SockType;
  1306. Hints.ai_protocol := Sockprotocol;
  1307. Hints.ai_flags := AI_PASSIVE;
  1308. r := synsock.GetAddrInfo(nil, PAnsiChar(Port), @Hints, Addr);
  1309. if (r = 0) and Assigned(Addr) then
  1310. begin
  1311. if Addr^.ai_family = AF_INET then
  1312. Result := synsock.htons(Addr^.ai_addr^.sin_port);
  1313. if Addr^.ai_family = AF_INET6 then
  1314. Result := synsock.htons(PSockAddrIn6(Addr^.ai_addr)^.sin6_port);
  1315. end;
  1316. finally
  1317. if Assigned(Addr) then
  1318. synsock.FreeAddrInfo(Addr);
  1319. end;
  1320. end;
  1321. end;
  1322. function ResolveIPToName(IP: AnsiString; Family, SockProtocol, SockType: integer): AnsiString;
  1323. var
  1324. Hints: TAddrInfo;
  1325. Addr: PAddrInfo;
  1326. r: integer;
  1327. host, serv: AnsiString;
  1328. hostlen, servlen: integer;
  1329. RemoteHost: PHostEnt;
  1330. IPn: u_long;
  1331. begin
  1332. Result := IP;
  1333. if not IsNewApi(Family) then
  1334. begin
  1335. IPn := synsock.inet_addr(PAnsiChar(IP));
  1336. if IPn <> u_long(INADDR_NONE) then
  1337. begin
  1338. SynSockCS.Enter;
  1339. try
  1340. RemoteHost := GetHostByAddr(@IPn, SizeOf(IPn), AF_INET);
  1341. if RemoteHost <> nil then
  1342. Result := RemoteHost^.h_name;
  1343. finally
  1344. SynSockCS.Leave;
  1345. end;
  1346. end;
  1347. end
  1348. else
  1349. begin
  1350. Addr := nil;
  1351. try
  1352. FillChar(Hints, Sizeof(Hints), 0);
  1353. Hints.ai_family := AF_UNSPEC;
  1354. Hints.ai_socktype := SockType;
  1355. Hints.ai_protocol := SockProtocol;
  1356. Hints.ai_flags := 0;
  1357. r := synsock.GetAddrInfo(PAnsiChar(IP), nil, @Hints, Addr);
  1358. if (r = 0) and Assigned(Addr)then
  1359. begin
  1360. hostlen := NI_MAXHOST;
  1361. servlen := NI_MAXSERV;
  1362. setlength(host, hostlen);
  1363. setlength(serv, servlen);
  1364. r := getnameinfo(Addr^.ai_addr, Addr^.ai_addrlen,
  1365. PAnsiChar(host), hostlen, PAnsiChar(serv), servlen,
  1366. NI_NUMERICSERV);
  1367. if r = 0 then
  1368. Result := PAnsiChar(host);
  1369. end;
  1370. finally
  1371. if Assigned(Addr) then
  1372. synsock.FreeAddrInfo(Addr);
  1373. end;
  1374. end;
  1375. end;
  1376. {=============================================================================}
  1377. function InitSocketInterface(stack: String): Boolean;
  1378. begin
  1379. Result := False;
  1380. SockEnhancedApi := False;
  1381. if stack = '' then
  1382. stack := DLLStackName;
  1383. SynSockCS.Enter;
  1384. try
  1385. if SynSockCount = 0 then
  1386. begin
  1387. SockEnhancedApi := False;
  1388. SockWship6Api := False;
  1389. LibHandle := LoadLibrary(PChar(Stack));
  1390. if LibHandle <> 0 then
  1391. begin
  1392. WSAIoctl := GetProcAddress(LibHandle, PAnsiChar(AnsiString('WSAIoctl')));
  1393. __WSAFDIsSet := GetProcAddress(LibHandle, PAnsiChar(AnsiString('__WSAFDIsSet')));
  1394. CloseSocket := GetProcAddress(LibHandle, PAnsiChar(AnsiString('closesocket')));
  1395. IoctlSocket := GetProcAddress(LibHandle, PAnsiChar(AnsiString('ioctlsocket')));
  1396. WSAGetLastError := GetProcAddress(LibHandle, PAnsiChar(AnsiString('WSAGetLastError')));
  1397. WSAStartup := GetProcAddress(LibHandle, PAnsiChar(AnsiString('WSAStartup')));
  1398. WSACleanup := GetProcAddress(LibHandle, PAnsiChar(AnsiString('WSACleanup')));
  1399. ssAccept := GetProcAddress(LibHandle, PAnsiChar(AnsiString('accept')));
  1400. ssBind := GetProcAddress(LibHandle, PAnsiChar(AnsiString('bind')));
  1401. ssConnect := GetProcAddress(LibHandle, PAnsiChar(AnsiString('connect')));
  1402. ssGetPeerName := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getpeername')));
  1403. ssGetSockName := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getsockname')));
  1404. GetSockOpt := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getsockopt')));
  1405. Htonl := GetProcAddress(LibHandle, PAnsiChar(AnsiString('htonl')));
  1406. Htons := GetProcAddress(LibHandle, PAnsiChar(AnsiString('htons')));
  1407. Inet_Addr := GetProcAddress(LibHandle, PAnsiChar(AnsiString('inet_addr')));
  1408. Inet_Ntoa := GetProcAddress(LibHandle, PAnsiChar(AnsiString('inet_ntoa')));
  1409. Listen := GetProcAddress(LibHandle, PAnsiChar(AnsiString('listen')));
  1410. Ntohl := GetProcAddress(LibHandle, PAnsiChar(AnsiString('ntohl')));
  1411. Ntohs := GetProcAddress(LibHandle, PAnsiChar(AnsiString('ntohs')));
  1412. ssRecv := GetProcAddress(LibHandle, PAnsiChar(AnsiString('recv')));
  1413. ssRecvFrom := GetProcAddress(LibHandle, PAnsiChar(AnsiString('recvfrom')));
  1414. Select := GetProcAddress(LibHandle, PAnsiChar(AnsiString('select')));
  1415. ssSend := GetProcAddress(LibHandle, PAnsiChar(AnsiString('send')));
  1416. ssSendTo := GetProcAddress(LibHandle, PAnsiChar(AnsiString('sendto')));
  1417. SetSockOpt := GetProcAddress(LibHandle, PAnsiChar(AnsiString('setsockopt')));
  1418. ShutDown := GetProcAddress(LibHandle, PAnsiChar(AnsiString('shutdown')));
  1419. Socket := GetProcAddress(LibHandle, PAnsiChar(AnsiString('socket')));
  1420. GetHostByAddr := GetProcAddress(LibHandle, PAnsiChar(AnsiString('gethostbyaddr')));
  1421. GetHostByName := GetProcAddress(LibHandle, PAnsiChar(AnsiString('gethostbyname')));
  1422. GetProtoByName := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getprotobyname')));
  1423. GetProtoByNumber := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getprotobynumber')));
  1424. GetServByName := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getservbyname')));
  1425. GetServByPort := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getservbyport')));
  1426. ssGetHostName := GetProcAddress(LibHandle, PAnsiChar(AnsiString('gethostname')));
  1427. {$IFNDEF FORCEOLDAPI}
  1428. GetAddrInfo := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getaddrinfo')));
  1429. FreeAddrInfo := GetProcAddress(LibHandle, PAnsiChar(AnsiString('freeaddrinfo')));
  1430. GetNameInfo := GetProcAddress(LibHandle, PAnsiChar(AnsiString('getnameinfo')));
  1431. SockEnhancedApi := Assigned(GetAddrInfo) and Assigned(FreeAddrInfo)
  1432. and Assigned(GetNameInfo);
  1433. if not SockEnhancedApi then
  1434. begin
  1435. LibWship6Handle := LoadLibrary(PChar(DLLWship6));
  1436. if LibWship6Handle <> 0 then
  1437. begin
  1438. GetAddrInfo := GetProcAddress(LibWship6Handle, PAnsiChar(AnsiString('getaddrinfo')));
  1439. FreeAddrInfo := GetProcAddress(LibWship6Handle, PAnsiChar(AnsiString('freeaddrinfo')));
  1440. GetNameInfo := GetProcAddress(LibWship6Handle, PAnsiChar(AnsiString('getnameinfo')));
  1441. SockWship6Api := Assigned(GetAddrInfo) and Assigned(FreeAddrInfo)
  1442. and Assigned(GetNameInfo);
  1443. end;
  1444. end;
  1445. {$ENDIF}
  1446. Result := True;
  1447. end;
  1448. end
  1449. else Result := True;
  1450. if Result then
  1451. Inc(SynSockCount);
  1452. finally
  1453. SynSockCS.Leave;
  1454. end;
  1455. end;
  1456. function DestroySocketInterface: Boolean;
  1457. begin
  1458. SynSockCS.Enter;
  1459. try
  1460. Dec(SynSockCount);
  1461. if SynSockCount < 0 then
  1462. SynSockCount := 0;
  1463. if SynSockCount = 0 then
  1464. begin
  1465. if LibHandle <> 0 then
  1466. begin
  1467. FreeLibrary(libHandle);
  1468. LibHandle := 0;
  1469. end;
  1470. if LibWship6Handle <> 0 then
  1471. begin
  1472. FreeLibrary(LibWship6Handle);
  1473. LibWship6Handle := 0;
  1474. end;
  1475. end;
  1476. finally
  1477. SynSockCS.Leave;
  1478. end;
  1479. Result := True;
  1480. end;
  1481. initialization
  1482. begin
  1483. SynSockCS := SyncObjs.TCriticalSection.Create;
  1484. SET_IN6_IF_ADDR_ANY (@in6addr_any);
  1485. SET_LOOPBACK_ADDR6 (@in6addr_loopback);
  1486. end;
  1487. finalization
  1488. begin
  1489. SynSockCS.Free;
  1490. end;