ssdotnet.inc 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 001.000.002 |
  3. |==============================================================================|
  4. | Content: Socket Independent Platform Layer - .NET definition include |
  5. |==============================================================================|
  6. | Copyright (c)2004, 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)2004. |
  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. {$IFDEF CIL}
  46. interface
  47. uses
  48. SyncObjs, SysUtils, Classes,
  49. System.Net,
  50. System.Net.Sockets;
  51. const
  52. DLLStackName = '';
  53. WinsockLevel = $0202;
  54. function InitSocketInterface(stack: string): Boolean;
  55. function DestroySocketInterface: Boolean;
  56. type
  57. u_char = Char;
  58. u_short = Word;
  59. u_int = Integer;
  60. u_long = Longint;
  61. pu_long = ^u_long;
  62. pu_short = ^u_short;
  63. PSockAddr = IPEndPoint;
  64. DWORD = integer;
  65. ULong = cardinal;
  66. TMemory = Array of byte;
  67. TLinger = LingerOption;
  68. TSocket = socket;
  69. TAddrFamily = AddressFamily;
  70. const
  71. WSADESCRIPTION_LEN = 256;
  72. WSASYS_STATUS_LEN = 128;
  73. type
  74. PWSAData = ^TWSAData;
  75. TWSAData = packed record
  76. wVersion: Word;
  77. wHighVersion: Word;
  78. szDescription: array[0..WSADESCRIPTION_LEN] of Char;
  79. szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;
  80. iMaxSockets: Word;
  81. iMaxUdpDg: Word;
  82. // lpVendorInfo: PChar;
  83. end;
  84. const
  85. MSG_NOSIGNAL = 0;
  86. INVALID_SOCKET = nil;
  87. AF_UNSPEC = AddressFamily.Unspecified;
  88. AF_INET = AddressFamily.InterNetwork;
  89. AF_INET6 = AddressFamily.InterNetworkV6;
  90. SOCKET_ERROR = integer(-1);
  91. FIONREAD = integer($4004667f);
  92. FIONBIO = integer($8004667e);
  93. FIOASYNC = integer($8004667d);
  94. SOMAXCONN = integer($7fffffff);
  95. IPPROTO_IP = ProtocolType.IP;
  96. IPPROTO_ICMP = ProtocolType.Icmp;
  97. IPPROTO_IGMP = ProtocolType.Igmp;
  98. IPPROTO_TCP = ProtocolType.Tcp;
  99. IPPROTO_UDP = ProtocolType.Udp;
  100. IPPROTO_RAW = ProtocolType.Raw;
  101. IPPROTO_IPV6 = ProtocolType.IPV6;
  102. //
  103. IPPROTO_ICMPV6 = ProtocolType.Icmp; //??
  104. SOCK_STREAM = SocketType.Stream;
  105. SOCK_DGRAM = SocketType.Dgram;
  106. SOCK_RAW = SocketType.Raw;
  107. SOCK_RDM = SocketType.Rdm;
  108. SOCK_SEQPACKET = SocketType.Seqpacket;
  109. SOL_SOCKET = SocketOptionLevel.Socket;
  110. SOL_IP = SocketOptionLevel.Ip;
  111. IP_OPTIONS = SocketOptionName.IPOptions;
  112. IP_HDRINCL = SocketOptionName.HeaderIncluded;
  113. IP_TOS = SocketOptionName.TypeOfService; { set/get IP Type Of Service }
  114. IP_TTL = SocketOptionName.IpTimeToLive; { set/get IP Time To Live }
  115. IP_MULTICAST_IF = SocketOptionName.MulticastInterface; { set/get IP multicast interface }
  116. IP_MULTICAST_TTL = SocketOptionName.MulticastTimeToLive; { set/get IP multicast timetolive }
  117. IP_MULTICAST_LOOP = SocketOptionName.MulticastLoopback; { set/get IP multicast loopback }
  118. IP_ADD_MEMBERSHIP = SocketOptionName.AddMembership; { add an IP group membership }
  119. IP_DROP_MEMBERSHIP = SocketOptionName.DropMembership; { drop an IP group membership }
  120. IP_DONTFRAGMENT = SocketOptionName.DontFragment; { set/get IP Don't Fragment flag }
  121. IPV6_UNICAST_HOPS = 8; // TTL
  122. IPV6_MULTICAST_IF = 9; // set/get IP multicast i/f
  123. IPV6_MULTICAST_HOPS = 10; // set/get IP multicast ttl
  124. IPV6_MULTICAST_LOOP = 11; // set/get IP multicast loopback
  125. IPV6_JOIN_GROUP = 12; // add an IP group membership
  126. IPV6_LEAVE_GROUP = 13; // drop an IP group membership
  127. SO_DEBUG = SocketOptionName.Debug; { turn on debugging info recording }
  128. SO_ACCEPTCONN = SocketOptionName.AcceptConnection; { socket has had listen() }
  129. SO_REUSEADDR = SocketOptionName.ReuseAddress; { allow local address reuse }
  130. SO_KEEPALIVE = SocketOptionName.KeepAlive; { keep connections alive }
  131. SO_DONTROUTE = SocketOptionName.DontRoute; { just use interface addresses }
  132. SO_BROADCAST = SocketOptionName.Broadcast; { permit sending of broadcast msgs }
  133. SO_USELOOPBACK = SocketOptionName.UseLoopback; { bypass hardware when possible }
  134. SO_LINGER = SocketOptionName.Linger; { linger on close if data present }
  135. SO_OOBINLINE = SocketOptionName.OutOfBandInline; { leave received OOB data in line }
  136. SO_DONTLINGER = SocketOptionName.DontLinger;
  137. { Additional options. }
  138. SO_SNDBUF = SocketOptionName.SendBuffer; { send buffer size }
  139. SO_RCVBUF = SocketOptionName.ReceiveBuffer; { receive buffer size }
  140. SO_SNDLOWAT = SocketOptionName.SendLowWater; { send low-water mark }
  141. SO_RCVLOWAT = SocketOptionName.ReceiveLowWater; { receive low-water mark }
  142. SO_SNDTIMEO = SocketOptionName.SendTimeout; { send timeout }
  143. SO_RCVTIMEO = SocketOptionName.ReceiveTimeout; { receive timeout }
  144. SO_ERROR = SocketOptionName.Error; { get error status and clear }
  145. SO_TYPE = SocketOptionName.Type; { get socket type }
  146. { WinSock 2 extension -- new options }
  147. // SO_GROUP_ID = $2001; { ID of a socket group}
  148. // SO_GROUP_PRIORITY = $2002; { the relative priority within a group}
  149. // SO_MAX_MSG_SIZE = $2003; { maximum message size }
  150. // SO_PROTOCOL_INFOA = $2004; { WSAPROTOCOL_INFOA structure }
  151. // SO_PROTOCOL_INFOW = $2005; { WSAPROTOCOL_INFOW structure }
  152. // SO_PROTOCOL_INFO = SO_PROTOCOL_INFOA;
  153. // PVD_CONFIG = $3001; {configuration info for service provider }
  154. { Option for opening sockets for synchronous access. }
  155. // SO_OPENTYPE = $7008;
  156. // SO_SYNCHRONOUS_ALERT = $10;
  157. // SO_SYNCHRONOUS_NONALERT = $20;
  158. { Other NT-specific options. }
  159. // SO_MAXDG = $7009;
  160. // SO_MAXPATHDG = $700A;
  161. // SO_UPDATE_ACCEPT_CONTEXT = $700B;
  162. // SO_CONNECT_TIME = $700C;
  163. { All Windows Sockets error constants are biased by WSABASEERR from the "normal" }
  164. WSABASEERR = 10000;
  165. { Windows Sockets definitions of regular Microsoft C error constants }
  166. WSAEINTR = (WSABASEERR+4);
  167. WSAEBADF = (WSABASEERR+9);
  168. WSAEACCES = (WSABASEERR+13);
  169. WSAEFAULT = (WSABASEERR+14);
  170. WSAEINVAL = (WSABASEERR+22);
  171. WSAEMFILE = (WSABASEERR+24);
  172. { Windows Sockets definitions of regular Berkeley error constants }
  173. WSAEWOULDBLOCK = (WSABASEERR+35);
  174. WSAEINPROGRESS = (WSABASEERR+36);
  175. WSAEALREADY = (WSABASEERR+37);
  176. WSAENOTSOCK = (WSABASEERR+38);
  177. WSAEDESTADDRREQ = (WSABASEERR+39);
  178. WSAEMSGSIZE = (WSABASEERR+40);
  179. WSAEPROTOTYPE = (WSABASEERR+41);
  180. WSAENOPROTOOPT = (WSABASEERR+42);
  181. WSAEPROTONOSUPPORT = (WSABASEERR+43);
  182. WSAESOCKTNOSUPPORT = (WSABASEERR+44);
  183. WSAEOPNOTSUPP = (WSABASEERR+45);
  184. WSAEPFNOSUPPORT = (WSABASEERR+46);
  185. WSAEAFNOSUPPORT = (WSABASEERR+47);
  186. WSAEADDRINUSE = (WSABASEERR+48);
  187. WSAEADDRNOTAVAIL = (WSABASEERR+49);
  188. WSAENETDOWN = (WSABASEERR+50);
  189. WSAENETUNREACH = (WSABASEERR+51);
  190. WSAENETRESET = (WSABASEERR+52);
  191. WSAECONNABORTED = (WSABASEERR+53);
  192. WSAECONNRESET = (WSABASEERR+54);
  193. WSAENOBUFS = (WSABASEERR+55);
  194. WSAEISCONN = (WSABASEERR+56);
  195. WSAENOTCONN = (WSABASEERR+57);
  196. WSAESHUTDOWN = (WSABASEERR+58);
  197. WSAETOOMANYREFS = (WSABASEERR+59);
  198. WSAETIMEDOUT = (WSABASEERR+60);
  199. WSAECONNREFUSED = (WSABASEERR+61);
  200. WSAELOOP = (WSABASEERR+62);
  201. WSAENAMETOOLONG = (WSABASEERR+63);
  202. WSAEHOSTDOWN = (WSABASEERR+64);
  203. WSAEHOSTUNREACH = (WSABASEERR+65);
  204. WSAENOTEMPTY = (WSABASEERR+66);
  205. WSAEPROCLIM = (WSABASEERR+67);
  206. WSAEUSERS = (WSABASEERR+68);
  207. WSAEDQUOT = (WSABASEERR+69);
  208. WSAESTALE = (WSABASEERR+70);
  209. WSAEREMOTE = (WSABASEERR+71);
  210. { Extended Windows Sockets error constant definitions }
  211. WSASYSNOTREADY = (WSABASEERR+91);
  212. WSAVERNOTSUPPORTED = (WSABASEERR+92);
  213. WSANOTINITIALISED = (WSABASEERR+93);
  214. WSAEDISCON = (WSABASEERR+101);
  215. WSAENOMORE = (WSABASEERR+102);
  216. WSAECANCELLED = (WSABASEERR+103);
  217. WSAEEINVALIDPROCTABLE = (WSABASEERR+104);
  218. WSAEINVALIDPROVIDER = (WSABASEERR+105);
  219. WSAEPROVIDERFAILEDINIT = (WSABASEERR+106);
  220. WSASYSCALLFAILURE = (WSABASEERR+107);
  221. WSASERVICE_NOT_FOUND = (WSABASEERR+108);
  222. WSATYPE_NOT_FOUND = (WSABASEERR+109);
  223. WSA_E_NO_MORE = (WSABASEERR+110);
  224. WSA_E_CANCELLED = (WSABASEERR+111);
  225. WSAEREFUSED = (WSABASEERR+112);
  226. { Error return codes from gethostbyname() and gethostbyaddr()
  227. (when using the resolver). Note that these errors are
  228. retrieved via WSAGetLastError() and must therefore follow
  229. the rules for avoiding clashes with error numbers from
  230. specific implementations or language run-time systems.
  231. For this reason the codes are based at WSABASEERR+1001.
  232. Note also that [WSA]NO_ADDRESS is defined only for
  233. compatibility purposes. }
  234. { Authoritative Answer: Host not found }
  235. WSAHOST_NOT_FOUND = (WSABASEERR+1001);
  236. HOST_NOT_FOUND = WSAHOST_NOT_FOUND;
  237. { Non-Authoritative: Host not found, or SERVERFAIL }
  238. WSATRY_AGAIN = (WSABASEERR+1002);
  239. TRY_AGAIN = WSATRY_AGAIN;
  240. { Non recoverable errors, FORMERR, REFUSED, NOTIMP }
  241. WSANO_RECOVERY = (WSABASEERR+1003);
  242. NO_RECOVERY = WSANO_RECOVERY;
  243. { Valid name, no data record of requested type }
  244. WSANO_DATA = (WSABASEERR+1004);
  245. NO_DATA = WSANO_DATA;
  246. { no address, look for MX record }
  247. WSANO_ADDRESS = WSANO_DATA;
  248. NO_ADDRESS = WSANO_ADDRESS;
  249. EWOULDBLOCK = WSAEWOULDBLOCK;
  250. EINPROGRESS = WSAEINPROGRESS;
  251. EALREADY = WSAEALREADY;
  252. ENOTSOCK = WSAENOTSOCK;
  253. EDESTADDRREQ = WSAEDESTADDRREQ;
  254. EMSGSIZE = WSAEMSGSIZE;
  255. EPROTOTYPE = WSAEPROTOTYPE;
  256. ENOPROTOOPT = WSAENOPROTOOPT;
  257. EPROTONOSUPPORT = WSAEPROTONOSUPPORT;
  258. ESOCKTNOSUPPORT = WSAESOCKTNOSUPPORT;
  259. EOPNOTSUPP = WSAEOPNOTSUPP;
  260. EPFNOSUPPORT = WSAEPFNOSUPPORT;
  261. EAFNOSUPPORT = WSAEAFNOSUPPORT;
  262. EADDRINUSE = WSAEADDRINUSE;
  263. EADDRNOTAVAIL = WSAEADDRNOTAVAIL;
  264. ENETDOWN = WSAENETDOWN;
  265. ENETUNREACH = WSAENETUNREACH;
  266. ENETRESET = WSAENETRESET;
  267. ECONNABORTED = WSAECONNABORTED;
  268. ECONNRESET = WSAECONNRESET;
  269. ENOBUFS = WSAENOBUFS;
  270. EISCONN = WSAEISCONN;
  271. ENOTCONN = WSAENOTCONN;
  272. ESHUTDOWN = WSAESHUTDOWN;
  273. ETOOMANYREFS = WSAETOOMANYREFS;
  274. ETIMEDOUT = WSAETIMEDOUT;
  275. ECONNREFUSED = WSAECONNREFUSED;
  276. ELOOP = WSAELOOP;
  277. ENAMETOOLONG = WSAENAMETOOLONG;
  278. EHOSTDOWN = WSAEHOSTDOWN;
  279. EHOSTUNREACH = WSAEHOSTUNREACH;
  280. ENOTEMPTY = WSAENOTEMPTY;
  281. EPROCLIM = WSAEPROCLIM;
  282. EUSERS = WSAEUSERS;
  283. EDQUOT = WSAEDQUOT;
  284. ESTALE = WSAESTALE;
  285. EREMOTE = WSAEREMOTE;
  286. type
  287. TVarSin = IPEndpoint;
  288. { function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean;
  289. function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean;
  290. function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean;
  291. function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean;
  292. function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean;
  293. function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6):boolean;
  294. procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6);
  295. procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6);
  296. var
  297. in6addr_any, in6addr_loopback : TInAddr6;
  298. }
  299. {procedure FD_CLR(Socket: TSocket; var FDSet: TFDSet);
  300. function FD_ISSET(Socket: TSocket; var FDSet: TFDSet): Boolean;
  301. procedure FD_SET(Socket: TSocket; var FDSet: TFDSet);
  302. procedure FD_ZERO(var FDSet: TFDSet);
  303. }
  304. {=============================================================================}
  305. function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer;
  306. function WSACleanup: Integer;
  307. function WSAGetLastError: Integer;
  308. function WSAGetLastErrorDesc: String;
  309. function GetHostName: string;
  310. function Shutdown(s: TSocket; how: Integer): Integer;
  311. // function SetSockOpt(s: TSocket; level, optname: Integer; optval: PChar;
  312. // optlen: Integer): Integer;
  313. function SetSockOpt(s: TSocket; level, optname: Integer; optval: TMemory;
  314. optlen: Integer): Integer;
  315. function SetSockOptObj(s: TSocket; level, optname: Integer; optval: TObject): Integer;
  316. function GetSockOpt(s: TSocket; level, optname: Integer; optval: TMemory;
  317. var optlen: Integer): Integer;
  318. // function SendTo(s: TSocket; const Buf; len, flags: Integer; addrto: PSockAddr;
  319. // tolen: Integer): Integer;
  320. /// function SendTo(s: TSocket; const Buf; len, flags: Integer; addrto: TVarSin): Integer;
  321. /// function Send(s: TSocket; const Buf; len, flags: Integer): Integer;
  322. /// function Recv(s: TSocket; var Buf; len, flags: Integer): Integer;
  323. // function RecvFrom(s: TSocket; var Buf; len, flags: Integer; from: PSockAddr;
  324. // var fromlen: Integer): Integer;
  325. /// function RecvFrom(s: TSocket; var Buf; len, flags: Integer; from: TVarSin): Integer;
  326. function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  327. function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  328. function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer;
  329. function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer;
  330. function ntohs(netshort: u_short): u_short;
  331. function ntohl(netlong: u_long): u_long;
  332. function Listen(s: TSocket; backlog: Integer): Integer;
  333. function IoctlSocket(s: TSocket; cmd: DWORD; var arg: integer): Integer;
  334. function htons(hostshort: u_short): u_short;
  335. function htonl(hostlong: u_long): u_long;
  336. // function GetSockName(s: TSocket; name: PSockAddr; var namelen: Integer): Integer;
  337. function GetSockName(s: TSocket; var name: TVarSin): Integer;
  338. // function GetPeerName(s: TSocket; name: PSockAddr; var namelen: Integer): Integer;
  339. function GetPeerName(s: TSocket; var name: TVarSin): Integer;
  340. // function Connect(s: TSocket; name: PSockAddr; namelen: Integer): Integer;
  341. function Connect(s: TSocket; const name: TVarSin): Integer;
  342. function CloseSocket(s: TSocket): Integer;
  343. // function Bind(s: TSocket; addr: PSockAddr; namelen: Integer): Integer;
  344. function Bind(s: TSocket; const addr: TVarSin): Integer;
  345. // function Accept(s: TSocket; addr: PSockAddr; var addrlen: Integer): TSocket;
  346. function Accept(s: TSocket; var addr: TVarSin): TSocket;
  347. function Socket(af, Struc, Protocol: Integer): TSocket;
  348. // Select = function(nfds: Integer; readfds, writefds, exceptfds: PFDSet;
  349. // timeout: PTimeVal): Longint;
  350. // {$IFDEF LINUX}cdecl{$ELSE}stdcall{$ENDIF};
  351. // TWSAIoctl = function (s: TSocket; dwIoControlCode: DWORD; lpvInBuffer: Pointer;
  352. // cbInBuffer: DWORD; lpvOutBuffer: Pointer; cbOutBuffer: DWORD;
  353. // lpcbBytesReturned: PDWORD; lpOverlapped: Pointer;
  354. // lpCompletionRoutine: pointer): u_int;
  355. // stdcall;
  356. function GetPortService(value: string): integer;
  357. function IsNewApi(Family: TAddrFamily): Boolean;
  358. function SetVarSin(var Sin: TVarSin; IP, Port: string; Family: TAddrFamily; SockProtocol, SockType: integer; PreferIP4: Boolean): integer;
  359. function GetSinIP(Sin: TVarSin): string;
  360. function GetSinPort(Sin: TVarSin): Integer;
  361. procedure ResolveNameToIP(Name: string; Family: TAddrFamily; SockProtocol, SockType: integer; const IPList: TStrings);
  362. function ResolveIPToName(IP: string; Family: TAddrFamily; SockProtocol, SockType: integer): string;
  363. function ResolvePort(Port: string; Family: TAddrFamily; SockProtocol, SockType: integer): Word;
  364. var
  365. SynSockCS: SyncObjs.TCriticalSection;
  366. SockEnhancedApi: Boolean;
  367. SockWship6Api: Boolean;
  368. {==============================================================================}
  369. implementation
  370. threadvar
  371. WSALastError: integer;
  372. WSALastErrorDesc: string;
  373. var
  374. services: Array [0..139, 0..1] of string =
  375. (
  376. ('echo', '7'),
  377. ('discard', '9'),
  378. ('sink', '9'),
  379. ('null', '9'),
  380. ('systat', '11'),
  381. ('users', '11'),
  382. ('daytime', '13'),
  383. ('qotd', '17'),
  384. ('quote', '17'),
  385. ('chargen', '19'),
  386. ('ttytst', '19'),
  387. ('source', '19'),
  388. ('ftp-data', '20'),
  389. ('ftp', '21'),
  390. ('telnet', '23'),
  391. ('smtp', '25'),
  392. ('mail', '25'),
  393. ('time', '37'),
  394. ('timeserver', '37'),
  395. ('rlp', '39'),
  396. ('nameserver', '42'),
  397. ('name', '42'),
  398. ('nickname', '43'),
  399. ('whois', '43'),
  400. ('domain', '53'),
  401. ('bootps', '67'),
  402. ('dhcps', '67'),
  403. ('bootpc', '68'),
  404. ('dhcpc', '68'),
  405. ('tftp', '69'),
  406. ('gopher', '70'),
  407. ('finger', '79'),
  408. ('http', '80'),
  409. ('www', '80'),
  410. ('www-http', '80'),
  411. ('kerberos', '88'),
  412. ('hostname', '101'),
  413. ('hostnames', '101'),
  414. ('iso-tsap', '102'),
  415. ('rtelnet', '107'),
  416. ('pop2', '109'),
  417. ('postoffice', '109'),
  418. ('pop3', '110'),
  419. ('sunrpc', '111'),
  420. ('rpcbind', '111'),
  421. ('portmap', '111'),
  422. ('auth', '113'),
  423. ('ident', '113'),
  424. ('tap', '113'),
  425. ('uucp-path', '117'),
  426. ('nntp', '119'),
  427. ('usenet', '119'),
  428. ('ntp', '123'),
  429. ('epmap', '135'),
  430. ('loc-srv', '135'),
  431. ('netbios-ns', '137'),
  432. ('nbname', '137'),
  433. ('netbios-dgm', '138'),
  434. ('nbdatagram', '138'),
  435. ('netbios-ssn', '139'),
  436. ('nbsession', '139'),
  437. ('imap', '143'),
  438. ('imap4', '143'),
  439. ('pcmail-srv', '158'),
  440. ('snmp', '161'),
  441. ('snmptrap', '162'),
  442. ('snmp-trap', '162'),
  443. ('print-srv', '170'),
  444. ('bgp', '179'),
  445. ('irc', '194'),
  446. ('ipx', '213'),
  447. ('ldap', '389'),
  448. ('https', '443'),
  449. ('mcom', '443'),
  450. ('microsoft-ds', '445'),
  451. ('kpasswd', '464'),
  452. ('isakmp', '500'),
  453. ('ike', '500'),
  454. ('exec', '512'),
  455. ('biff', '512'),
  456. ('comsat', '512'),
  457. ('login', '513'),
  458. ('who', '513'),
  459. ('whod', '513'),
  460. ('cmd', '514'),
  461. ('shell', '514'),
  462. ('syslog', '514'),
  463. ('printer', '515'),
  464. ('spooler', '515'),
  465. ('talk', '517'),
  466. ('ntalk', '517'),
  467. ('efs', '520'),
  468. ('router', '520'),
  469. ('route', '520'),
  470. ('routed', '520'),
  471. ('timed', '525'),
  472. ('timeserver', '525'),
  473. ('tempo', '526'),
  474. ('newdate', '526'),
  475. ('courier', '530'),
  476. ('rpc', '530'),
  477. ('conference', '531'),
  478. ('chat', '531'),
  479. ('netnews', '532'),
  480. ('readnews', '532'),
  481. ('netwall', '533'),
  482. ('uucp', '540'),
  483. ('uucpd', '540'),
  484. ('klogin', '543'),
  485. ('kshell', '544'),
  486. ('krcmd', '544'),
  487. ('new-rwho', '550'),
  488. ('new-who', '550'),
  489. ('remotefs', '556'),
  490. ('rfs', '556'),
  491. ('rfs_server', '556'),
  492. ('rmonitor', '560'),
  493. ('rmonitord', '560'),
  494. ('monitor', '561'),
  495. ('ldaps', '636'),
  496. ('sldap', '636'),
  497. ('doom', '666'),
  498. ('kerberos-adm', '749'),
  499. ('kerberos-iv', '750'),
  500. ('kpop', '1109'),
  501. ('phone', '1167'),
  502. ('ms-sql-s', '1433'),
  503. ('ms-sql-m', '1434'),
  504. ('wins', '1512'),
  505. ('ingreslock', '1524'),
  506. ('ingres', '1524'),
  507. ('l2tp', '1701'),
  508. ('pptp', '1723'),
  509. ('radius', '1812'),
  510. ('radacct', '1813'),
  511. ('nfsd', '2049'),
  512. ('nfs', '2049'),
  513. ('knetd', '2053'),
  514. ('gds_db', '3050'),
  515. ('man', '9535')
  516. );
  517. {function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean;
  518. begin
  519. Result := ((a^.s_un_dw.s_dw1 = 0) and (a^.s_un_dw.s_dw2 = 0) and
  520. (a^.s_un_dw.s_dw3 = 0) and (a^.s_un_dw.s_dw4 = 0));
  521. end;
  522. function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean;
  523. begin
  524. Result := ((a^.s_un_dw.s_dw1 = 0) and (a^.s_un_dw.s_dw2 = 0) and
  525. (a^.s_un_dw.s_dw3 = 0) and
  526. (a^.s_un_b.s_b13 = char(0)) and (a^.s_un_b.s_b14 = char(0)) and
  527. (a^.s_un_b.s_b15 = char(0)) and (a^.s_un_b.s_b16 = char(1)));
  528. end;
  529. function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean;
  530. begin
  531. Result := ((a^.s_un_b.s_b1 = u_char($FE)) and (a^.s_un_b.s_b2 = u_char($80)));
  532. end;
  533. function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean;
  534. begin
  535. Result := ((a^.s_un_b.s_b1 = u_char($FE)) and (a^.s_un_b.s_b2 = u_char($C0)));
  536. end;
  537. function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean;
  538. begin
  539. Result := (a^.s_un_b.s_b1 = char($FF));
  540. end;
  541. function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6): boolean;
  542. begin
  543. Result := (CompareMem( a, b, sizeof(TInAddr6)));
  544. end;
  545. procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6);
  546. begin
  547. FillChar(a^, sizeof(TInAddr6), 0);
  548. end;
  549. procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6);
  550. begin
  551. FillChar(a^, sizeof(TInAddr6), 0);
  552. a^.s_un_b.s_b16 := char(1);
  553. end;
  554. }
  555. {=============================================================================}
  556. procedure NullErr;
  557. begin
  558. WSALastError := 0;
  559. WSALastErrorDesc := '';
  560. end;
  561. procedure GetErrCode(E: System.Exception);
  562. var
  563. SE: System.Net.Sockets.SocketException;
  564. begin
  565. if E is System.Net.Sockets.SocketException then
  566. begin
  567. SE := E as System.Net.Sockets.SocketException;
  568. WSALastError := SE.ErrorCode;
  569. WSALastErrorDesc := SE.Message;
  570. end
  571. end;
  572. function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer;
  573. begin
  574. NullErr;
  575. with WSData do
  576. begin
  577. wVersion := wVersionRequired;
  578. wHighVersion := $202;
  579. szDescription := 'Synsock - Synapse Platform Independent Socket Layer';
  580. szSystemStatus := 'Running on .NET';
  581. iMaxSockets := 32768;
  582. iMaxUdpDg := 8192;
  583. end;
  584. Result := 0;
  585. end;
  586. function WSACleanup: Integer;
  587. begin
  588. NullErr;
  589. Result := 0;
  590. end;
  591. function WSAGetLastError: Integer;
  592. begin
  593. Result := WSALastError;
  594. end;
  595. function WSAGetLastErrorDesc: String;
  596. begin
  597. Result := WSALastErrorDesc;
  598. end;
  599. function GetHostName: string;
  600. begin
  601. Result := System.Net.DNS.GetHostName;
  602. end;
  603. function Shutdown(s: TSocket; how: Integer): Integer;
  604. begin
  605. Result := 0;
  606. NullErr;
  607. try
  608. s.ShutDown(SocketShutdown(how));
  609. except
  610. on e: System.Net.Sockets.SocketException do
  611. begin
  612. GetErrCode(e);
  613. Result := integer(SOCKET_ERROR);
  614. end;
  615. end;
  616. end;
  617. function SetSockOpt(s: TSocket; level, optname: Integer; optval: Tmemory;
  618. optlen: Integer): Integer;
  619. begin
  620. Result := 0;
  621. NullErr;
  622. try
  623. s.SetSocketOption(SocketOptionLevel(level), SocketOptionName(optname), optval);
  624. except
  625. on e: System.Net.Sockets.SocketException do
  626. begin
  627. GetErrCode(e);
  628. Result := integer(SOCKET_ERROR);
  629. end;
  630. end;
  631. end;
  632. function SetSockOptObj(s: TSocket; level, optname: Integer; optval: TObject): Integer;
  633. begin
  634. Result := 0;
  635. NullErr;
  636. try
  637. s.SetSocketOption(SocketOptionLevel(level), SocketOptionName(optname), optval);
  638. except
  639. on e: System.Net.Sockets.SocketException do
  640. begin
  641. GetErrCode(e);
  642. Result := integer(SOCKET_ERROR);
  643. end;
  644. end;
  645. end;
  646. function GetSockOpt(s: TSocket; level, optname: Integer; optval: Tmemory;
  647. var optlen: Integer): Integer;
  648. begin
  649. Result := 0;
  650. NullErr;
  651. try
  652. s.GetSocketOption(SocketOptionLevel(level), SocketOptionName(optname), optval);
  653. except
  654. on e: System.Net.Sockets.SocketException do
  655. begin
  656. GetErrCode(e);
  657. Result := integer(SOCKET_ERROR);
  658. end;
  659. end;
  660. end;
  661. function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer;
  662. //function SendTo(s: TSocket; const Buf; len, flags: Integer; addrto: TVarSin): Integer;
  663. begin
  664. NullErr;
  665. try
  666. result := s.SendTo(Buf, len, SocketFlags(flags), addrto);
  667. except
  668. on e: System.Net.Sockets.SocketException do
  669. begin
  670. GetErrCode(e);
  671. Result := integer(SOCKET_ERROR);
  672. end;
  673. end;
  674. end;
  675. function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  676. //function Send(s: TSocket; const Buf; len, flags: Integer): Integer;
  677. begin
  678. NullErr;
  679. try
  680. result := s.Send(Buf, len, SocketFlags(flags));
  681. except
  682. on e: System.Net.Sockets.SocketException do
  683. begin
  684. GetErrCode(e);
  685. Result := integer(SOCKET_ERROR);
  686. end;
  687. end;
  688. end;
  689. function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  690. //function Recv(s: TSocket; var Buf; len, flags: Integer): Integer;
  691. begin
  692. NullErr;
  693. try
  694. result := s.Receive(Buf, len, SocketFlags(flags));
  695. except
  696. on e: System.Net.Sockets.SocketException do
  697. begin
  698. GetErrCode(e);
  699. Result := integer(SOCKET_ERROR);
  700. end;
  701. end;
  702. end;
  703. //function RecvFrom(s: TSocket; var Buf; len, flags: Integer; from: PSockAddr;
  704. // var fromlen: Integer): Integer;
  705. function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer;
  706. //function RecvFrom(s: TSocket; var Buf; len, flags: Integer; from: TVarSin): Integer;
  707. var
  708. EP: EndPoint;
  709. begin
  710. NullErr;
  711. try
  712. EP := from;
  713. result := s.ReceiveFrom(Buf, len, SocketFlags(flags), EndPoint(EP));
  714. from := EP as IPEndPoint;
  715. except
  716. on e: System.Net.Sockets.SocketException do
  717. begin
  718. GetErrCode(e);
  719. Result := integer(SOCKET_ERROR);
  720. end;
  721. end;
  722. end;
  723. function ntohs(netshort: u_short): u_short;
  724. begin
  725. Result := IPAddress.NetworkToHostOrder(NetShort);
  726. end;
  727. function ntohl(netlong: u_long): u_long;
  728. begin
  729. Result := IPAddress.NetworkToHostOrder(NetLong);
  730. end;
  731. function Listen(s: TSocket; backlog: Integer): Integer;
  732. begin
  733. Result := 0;
  734. NullErr;
  735. try
  736. s.Listen(backlog);
  737. except
  738. on e: System.Net.Sockets.SocketException do
  739. begin
  740. GetErrCode(e);
  741. Result := integer(SOCKET_ERROR);
  742. end;
  743. end;
  744. end;
  745. function IoctlSocket(s: TSocket; cmd: DWORD; var arg: integer): Integer;
  746. var
  747. inv, outv: TMemory;
  748. begin
  749. Result := 0;
  750. NullErr;
  751. try
  752. if cmd = DWORD(FIONBIO) then
  753. s.Blocking := arg = 0
  754. else
  755. begin
  756. inv := BitConverter.GetBytes(arg);
  757. outv := BitConverter.GetBytes(integer(0));
  758. s.IOControl(cmd, inv, outv);
  759. arg := BitConverter.ToInt32(outv, 0);
  760. end;
  761. except
  762. on e: System.Net.Sockets.SocketException do
  763. begin
  764. GetErrCode(e);
  765. Result := integer(SOCKET_ERROR);
  766. end;
  767. end;
  768. end;
  769. function htons(hostshort: u_short): u_short;
  770. begin
  771. Result := IPAddress.HostToNetworkOrder(Hostshort);
  772. end;
  773. function htonl(hostlong: u_long): u_long;
  774. begin
  775. Result := IPAddress.HostToNetworkOrder(HostLong);
  776. end;
  777. //function GetSockName(s: TSocket; name: PSockAddr; var namelen: Integer): Integer;
  778. function GetSockName(s: TSocket; var name: TVarSin): Integer;
  779. begin
  780. Result := 0;
  781. NullErr;
  782. try
  783. Name := s.localEndPoint as IPEndpoint;
  784. except
  785. on e: System.Net.Sockets.SocketException do
  786. begin
  787. GetErrCode(e);
  788. Result := integer(SOCKET_ERROR);
  789. end;
  790. end;
  791. end;
  792. //function GetPeerName(s: TSocket; name: PSockAddr; var namelen: Integer): Integer;
  793. function GetPeerName(s: TSocket; var name: TVarSin): Integer;
  794. begin
  795. Result := 0;
  796. NullErr;
  797. try
  798. Name := s.RemoteEndPoint as IPEndpoint;
  799. except
  800. on e: System.Net.Sockets.SocketException do
  801. begin
  802. GetErrCode(e);
  803. Result := integer(SOCKET_ERROR);
  804. end;
  805. end;
  806. end;
  807. //function Connect(s: TSocket; name: PSockAddr; namelen: Integer): Integer;
  808. function Connect(s: TSocket; const name: TVarSin): Integer;
  809. begin
  810. Result := 0;
  811. NullErr;
  812. try
  813. s.Connect(name);
  814. except
  815. on e: System.Net.Sockets.SocketException do
  816. begin
  817. GetErrCode(e);
  818. Result := integer(SOCKET_ERROR);
  819. end;
  820. end;
  821. end;
  822. function CloseSocket(s: TSocket): Integer;
  823. begin
  824. Result := 0;
  825. NullErr;
  826. try
  827. s.Close;
  828. except
  829. on e: System.Net.Sockets.SocketException do
  830. begin
  831. Result := integer(SOCKET_ERROR);
  832. end;
  833. end;
  834. end;
  835. //function Bind(s: TSocket; addr: PSockAddr; namelen: Integer): Integer;
  836. function Bind(s: TSocket; const addr: TVarSin): Integer;
  837. begin
  838. Result := 0;
  839. NullErr;
  840. try
  841. s.Bind(addr);
  842. except
  843. on e: System.Net.Sockets.SocketException do
  844. begin
  845. GetErrCode(e);
  846. Result := integer(SOCKET_ERROR);
  847. end;
  848. end;
  849. end;
  850. //function Accept(s: TSocket; addr: PSockAddr; var addrlen: Integer): TSocket;
  851. function Accept(s: TSocket; var addr: TVarSin): TSocket;
  852. begin
  853. NullErr;
  854. try
  855. result := s.Accept();
  856. except
  857. on e: System.Net.Sockets.SocketException do
  858. begin
  859. GetErrCode(e);
  860. Result := nil;
  861. end;
  862. end;
  863. end;
  864. function Socket(af, Struc, Protocol: Integer): TSocket;
  865. begin
  866. NullErr;
  867. try
  868. result := TSocket.Create(AddressFamily(af), SocketType(Struc), ProtocolType(Protocol));
  869. except
  870. on e: System.Net.Sockets.SocketException do
  871. begin
  872. GetErrCode(e);
  873. Result := nil;
  874. end;
  875. end;
  876. end;
  877. {=============================================================================}
  878. function GetPortService(value: string): integer;
  879. var
  880. n: integer;
  881. begin
  882. Result := 0;
  883. value := Lowercase(value);
  884. for n := 0 to High(Services) do
  885. if services[n, 0] = value then
  886. begin
  887. Result := strtointdef(services[n, 1], 0);
  888. break;
  889. end;
  890. if Result = 0 then
  891. Result := StrToIntDef(value, 0);
  892. end;
  893. {=============================================================================}
  894. function IsNewApi(Family: TAddrFamily): Boolean;
  895. begin
  896. Result := true;
  897. end;
  898. function SetVarSin(var Sin: TVarSin; IP, Port: string; Family: TAddrFamily; SockProtocol, SockType: integer; PreferIP4: Boolean): integer;
  899. var
  900. IPs: array of IPAddress;
  901. n: integer;
  902. ip4, ip6: string;
  903. sip: string;
  904. begin
  905. sip := '';
  906. ip4 := '';
  907. ip6 := '';
  908. IPs := Dns.Resolve(IP).AddressList;
  909. for n :=low(IPs) to high(IPs) do begin
  910. if (ip4 = '') and (IPs[n].AddressFamily = AF_INET) then
  911. ip4 := IPs[n].toString;
  912. if (ip6 = '') and (IPs[n].AddressFamily = AF_INET6) then
  913. ip6 := IPs[n].toString;
  914. if (ip4 <> '') and (ip6 <> '') then
  915. break;
  916. end;
  917. case Family of
  918. AF_UNSPEC:
  919. begin
  920. if (ip4 <> '') and (ip6 <> '') then
  921. begin
  922. if PreferIP4 then
  923. sip := ip4
  924. else
  925. Sip := ip6;
  926. end
  927. else
  928. begin
  929. sip := ip4;
  930. if (ip6 <> '') then
  931. sip := ip6;
  932. end;
  933. end;
  934. AF_INET:
  935. sip := ip4;
  936. AF_INET6:
  937. sip := ip6;
  938. end;
  939. sin := TVarSin.Create(IPAddress.Parse(sip), GetPortService(Port));
  940. end;
  941. function GetSinIP(Sin: TVarSin): string;
  942. begin
  943. Result := Sin.Address.ToString;
  944. end;
  945. function GetSinPort(Sin: TVarSin): Integer;
  946. begin
  947. Result := Sin.Port;
  948. end;
  949. procedure ResolveNameToIP(Name: string; Family: TAddrFamily; SockProtocol, SockType: integer; const IPList: TStrings);
  950. var
  951. IPs :array of IPAddress;
  952. n: integer;
  953. begin
  954. IPList.Clear;
  955. IPs := Dns.Resolve(Name).AddressList;
  956. for n := low(IPs) to high(IPs) do
  957. begin
  958. if not(((Family = AF_INET6) and (IPs[n].AddressFamily = AF_INET))
  959. or ((Family = AF_INET) and (IPs[n].AddressFamily = AF_INET6))) then
  960. begin
  961. IPList.Add(IPs[n].toString);
  962. end;
  963. end;
  964. end;
  965. function ResolvePort(Port: string; Family: TAddrFamily; SockProtocol, SockType: integer): Word;
  966. var
  967. n: integer;
  968. begin
  969. Result := StrToIntDef(port, 0);
  970. if Result = 0 then
  971. begin
  972. port := Lowercase(port);
  973. for n := 0 to High(Services) do
  974. if services[n, 0] = port then
  975. begin
  976. Result := strtointdef(services[n, 1], 0);
  977. break;
  978. end;
  979. end;
  980. end;
  981. function ResolveIPToName(IP: string; Family: TAddrFamily; SockProtocol, SockType: integer): string;
  982. begin
  983. Result := Dns.GetHostByAddress(IP).HostName;
  984. end;
  985. {=============================================================================}
  986. function InitSocketInterface(stack: string): Boolean;
  987. begin
  988. Result := True;
  989. end;
  990. function DestroySocketInterface: Boolean;
  991. begin
  992. NullErr;
  993. Result := True;
  994. end;
  995. initialization
  996. begin
  997. SynSockCS := SyncObjs.TCriticalSection.Create;
  998. // SET_IN6_IF_ADDR_ANY (@in6addr_any);
  999. // SET_LOOPBACK_ADDR6 (@in6addr_loopback);
  1000. end;
  1001. finalization
  1002. begin
  1003. NullErr;
  1004. SynSockCS.Free;
  1005. end;
  1006. {$ENDIF}