ssfpc.pas 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  1. {==============================================================================|
  2. | Project : Ararat Synapse | 001.001.000 |
  3. |==============================================================================|
  4. | Content: Socket Independent Platform Layer - FreePascal definition include |
  5. |==============================================================================|
  6. | Copyright (c)2006-2009, 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)2006-2009. |
  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 FPC}
  46. {For FreePascal 2.x.x}
  47. //{$DEFINE FORCEOLDAPI}
  48. {Note about define FORCEOLDAPI:
  49. If you activate this compiler directive, then is allways used old socket API
  50. for name resolution. If you leave this directive inactive, then the new API
  51. is used, when running system allows it.
  52. For IPv6 support you must have new API!
  53. }
  54. {$IFDEF FPC}
  55. {$MODE DELPHI}
  56. {$ENDIF}
  57. {$H+}
  58. {$ifdef FreeBSD}
  59. {$DEFINE SOCK_HAS_SINLEN} // BSD definition of scoketaddr
  60. {$endif}
  61. {$ifdef darwin}
  62. {$DEFINE SOCK_HAS_SINLEN} // BSD definition of scoketaddr
  63. {$endif}
  64. interface
  65. uses
  66. SyncObjs, SysUtils, Classes,
  67. synafpc, BaseUnix, Unix, termio, sockets, netdb;
  68. function InitSocketInterface(stack: string): Boolean;
  69. function DestroySocketInterface: Boolean;
  70. const
  71. DLLStackName = '';
  72. WinsockLevel = $0202;
  73. cLocalHost = '127.0.0.1';
  74. cAnyHost = '0.0.0.0';
  75. c6AnyHost = '::0';
  76. c6Localhost = '::1';
  77. cLocalHostStr = 'localhost';
  78. type
  79. TSocket = longint;
  80. TAddrFamily = integer;
  81. TMemory = pointer;
  82. type
  83. TFDSet = Baseunix.TFDSet;
  84. PFDSet = ^TFDSet;
  85. Ptimeval = Baseunix.ptimeval;
  86. Ttimeval = Baseunix.ttimeval;
  87. const
  88. FIONREAD = termio.FIONREAD;
  89. FIONBIO = termio.FIONBIO;
  90. FIOASYNC = termio.FIOASYNC;
  91. const
  92. IPPROTO_IP = 0; { Dummy }
  93. IPPROTO_ICMP = 1; { Internet Control Message Protocol }
  94. IPPROTO_IGMP = 2; { Internet Group Management Protocol}
  95. IPPROTO_TCP = 6; { TCP }
  96. IPPROTO_UDP = 17; { User Datagram Protocol }
  97. IPPROTO_IPV6 = 41;
  98. IPPROTO_ICMPV6 = 58;
  99. IPPROTO_RAW = 255;
  100. IPPROTO_MAX = 256;
  101. type
  102. PInAddr = ^TInAddr;
  103. TInAddr = sockets.in_addr;
  104. PSockAddrIn = ^TSockAddrIn;
  105. TSockAddrIn = sockets.TInetSockAddr;
  106. TIP_mreq = record
  107. imr_multiaddr: TInAddr; // IP multicast address of group
  108. imr_interface: TInAddr; // local IP address of interface
  109. end;
  110. PInAddr6 = ^TInAddr6;
  111. TInAddr6 = sockets.Tin6_addr;
  112. PSockAddrIn6 = ^TSockAddrIn6;
  113. TSockAddrIn6 = sockets.TInetSockAddr6;
  114. TIPv6_mreq = record
  115. ipv6mr_multiaddr: TInAddr6; // IPv6 multicast address.
  116. ipv6mr_interface: integer; // Interface index.
  117. end;
  118. const
  119. INADDR_ANY = $00000000;
  120. INADDR_LOOPBACK = $7F000001;
  121. INADDR_BROADCAST = $FFFFFFFF;
  122. INADDR_NONE = $FFFFFFFF;
  123. ADDR_ANY = INADDR_ANY;
  124. INVALID_SOCKET = TSocket(NOT(0));
  125. SOCKET_ERROR = -1;
  126. Const
  127. IP_TOS = sockets.IP_TOS; { int; IP type of service and precedence. }
  128. IP_TTL = sockets.IP_TTL; { int; IP time to live. }
  129. IP_HDRINCL = sockets.IP_HDRINCL; { int; Header is included with data. }
  130. IP_OPTIONS = sockets.IP_OPTIONS; { ip_opts; IP per-packet options. }
  131. // IP_ROUTER_ALERT = sockets.IP_ROUTER_ALERT; { bool }
  132. IP_RECVOPTS = sockets.IP_RECVOPTS; { bool }
  133. IP_RETOPTS = sockets.IP_RETOPTS; { bool }
  134. // IP_PKTINFO = sockets.IP_PKTINFO; { bool }
  135. // IP_PKTOPTIONS = sockets.IP_PKTOPTIONS;
  136. // IP_PMTUDISC = sockets.IP_PMTUDISC; { obsolete name? }
  137. // IP_MTU_DISCOVER = sockets.IP_MTU_DISCOVER; { int; see below }
  138. // IP_RECVERR = sockets.IP_RECVERR; { bool }
  139. // IP_RECVTTL = sockets.IP_RECVTTL; { bool }
  140. // IP_RECVTOS = sockets.IP_RECVTOS; { bool }
  141. IP_MULTICAST_IF = sockets.IP_MULTICAST_IF; { in_addr; set/get IP multicast i/f }
  142. IP_MULTICAST_TTL = sockets.IP_MULTICAST_TTL; { u_char; set/get IP multicast ttl }
  143. IP_MULTICAST_LOOP = sockets.IP_MULTICAST_LOOP; { i_char; set/get IP multicast loopback }
  144. IP_ADD_MEMBERSHIP = sockets.IP_ADD_MEMBERSHIP; { ip_mreq; add an IP group membership }
  145. IP_DROP_MEMBERSHIP = sockets.IP_DROP_MEMBERSHIP; { ip_mreq; drop an IP group membership }
  146. SOL_SOCKET = sockets.SOL_SOCKET;
  147. SO_DEBUG = sockets.SO_DEBUG;
  148. SO_REUSEADDR = sockets.SO_REUSEADDR;
  149. SO_TYPE = sockets.SO_TYPE;
  150. SO_ERROR = sockets.SO_ERROR;
  151. SO_DONTROUTE = sockets.SO_DONTROUTE;
  152. SO_BROADCAST = sockets.SO_BROADCAST;
  153. SO_SNDBUF = sockets.SO_SNDBUF;
  154. SO_RCVBUF = sockets.SO_RCVBUF;
  155. SO_KEEPALIVE = sockets.SO_KEEPALIVE;
  156. SO_OOBINLINE = sockets.SO_OOBINLINE;
  157. // SO_NO_CHECK = sockets.SO_NO_CHECK;
  158. // SO_PRIORITY = sockets.SO_PRIORITY;
  159. SO_LINGER = sockets.SO_LINGER;
  160. // SO_BSDCOMPAT = sockets.SO_BSDCOMPAT;
  161. // SO_REUSEPORT = sockets.SO_REUSEPORT;
  162. // SO_PASSCRED = sockets.SO_PASSCRED;
  163. // SO_PEERCRED = sockets.SO_PEERCRED;
  164. SO_RCVLOWAT = sockets.SO_RCVLOWAT;
  165. SO_SNDLOWAT = sockets.SO_SNDLOWAT;
  166. SO_RCVTIMEO = sockets.SO_RCVTIMEO;
  167. SO_SNDTIMEO = sockets.SO_SNDTIMEO;
  168. { Security levels - as per NRL IPv6 - don't actually do anything }
  169. // SO_SECURITY_AUTHENTICATION = sockets.SO_SECURITY_AUTHENTICATION;
  170. // SO_SECURITY_ENCRYPTION_TRANSPORT = sockets.SO_SECURITY_ENCRYPTION_TRANSPORT;
  171. // SO_SECURITY_ENCRYPTION_NETWORK = sockets.SO_SECURITY_ENCRYPTION_NETWORK;
  172. // SO_BINDTODEVICE = sockets.SO_BINDTODEVICE;
  173. { Socket filtering }
  174. // SO_ATTACH_FILTER = sockets.SO_ATTACH_FILTER;
  175. // SO_DETACH_FILTER = sockets.SO_DETACH_FILTER;
  176. SOMAXCONN = 1024;
  177. IPV6_UNICAST_HOPS = sockets.IPV6_UNICAST_HOPS;
  178. IPV6_MULTICAST_IF = sockets.IPV6_MULTICAST_IF;
  179. IPV6_MULTICAST_HOPS = sockets.IPV6_MULTICAST_HOPS;
  180. IPV6_MULTICAST_LOOP = sockets.IPV6_MULTICAST_LOOP;
  181. IPV6_JOIN_GROUP = sockets.IPV6_JOIN_GROUP;
  182. IPV6_LEAVE_GROUP = sockets.IPV6_LEAVE_GROUP;
  183. const
  184. SOCK_STREAM = 1; { stream socket }
  185. SOCK_DGRAM = 2; { datagram socket }
  186. SOCK_RAW = 3; { raw-protocol interface }
  187. SOCK_RDM = 4; { reliably-delivered message }
  188. SOCK_SEQPACKET = 5; { sequenced packet stream }
  189. { TCP options. }
  190. TCP_NODELAY = $0001;
  191. { Address families. }
  192. AF_UNSPEC = 0; { unspecified }
  193. AF_INET = 2; { internetwork: UDP, TCP, etc. }
  194. AF_INET6 = 10; { Internetwork Version 6 }
  195. AF_MAX = 24;
  196. { Protocol families, same as address families for now. }
  197. PF_UNSPEC = AF_UNSPEC;
  198. PF_INET = AF_INET;
  199. PF_INET6 = AF_INET6;
  200. PF_MAX = AF_MAX;
  201. type
  202. { Structure used for manipulating linger option. }
  203. PLinger = ^TLinger;
  204. TLinger = packed record
  205. l_onoff: integer;
  206. l_linger: integer;
  207. end;
  208. const
  209. MSG_OOB = sockets.MSG_OOB; // Process out-of-band data.
  210. MSG_PEEK = sockets.MSG_PEEK; // Peek at incoming messages.
  211. MSG_NOSIGNAL = sockets.MSG_NOSIGNAL; // Do not generate SIGPIPE.
  212. const
  213. WSAEINTR = ESysEINTR;
  214. WSAEBADF = ESysEBADF;
  215. WSAEACCES = ESysEACCES;
  216. WSAEFAULT = ESysEFAULT;
  217. WSAEINVAL = ESysEINVAL;
  218. WSAEMFILE = ESysEMFILE;
  219. WSAEWOULDBLOCK = ESysEWOULDBLOCK;
  220. WSAEINPROGRESS = ESysEINPROGRESS;
  221. WSAEALREADY = ESysEALREADY;
  222. WSAENOTSOCK = ESysENOTSOCK;
  223. WSAEDESTADDRREQ = ESysEDESTADDRREQ;
  224. WSAEMSGSIZE = ESysEMSGSIZE;
  225. WSAEPROTOTYPE = ESysEPROTOTYPE;
  226. WSAENOPROTOOPT = ESysENOPROTOOPT;
  227. WSAEPROTONOSUPPORT = ESysEPROTONOSUPPORT;
  228. WSAESOCKTNOSUPPORT = ESysESOCKTNOSUPPORT;
  229. WSAEOPNOTSUPP = ESysEOPNOTSUPP;
  230. WSAEPFNOSUPPORT = ESysEPFNOSUPPORT;
  231. WSAEAFNOSUPPORT = ESysEAFNOSUPPORT;
  232. WSAEADDRINUSE = ESysEADDRINUSE;
  233. WSAEADDRNOTAVAIL = ESysEADDRNOTAVAIL;
  234. WSAENETDOWN = ESysENETDOWN;
  235. WSAENETUNREACH = ESysENETUNREACH;
  236. WSAENETRESET = ESysENETRESET;
  237. WSAECONNABORTED = ESysECONNABORTED;
  238. WSAECONNRESET = ESysECONNRESET;
  239. WSAENOBUFS = ESysENOBUFS;
  240. WSAEISCONN = ESysEISCONN;
  241. WSAENOTCONN = ESysENOTCONN;
  242. WSAESHUTDOWN = ESysESHUTDOWN;
  243. WSAETOOMANYREFS = ESysETOOMANYREFS;
  244. WSAETIMEDOUT = ESysETIMEDOUT;
  245. WSAECONNREFUSED = ESysECONNREFUSED;
  246. WSAELOOP = ESysELOOP;
  247. WSAENAMETOOLONG = ESysENAMETOOLONG;
  248. WSAEHOSTDOWN = ESysEHOSTDOWN;
  249. WSAEHOSTUNREACH = ESysEHOSTUNREACH;
  250. WSAENOTEMPTY = ESysENOTEMPTY;
  251. WSAEPROCLIM = -1;
  252. WSAEUSERS = ESysEUSERS;
  253. WSAEDQUOT = ESysEDQUOT;
  254. WSAESTALE = ESysESTALE;
  255. WSAEREMOTE = ESysEREMOTE;
  256. WSASYSNOTREADY = -2;
  257. WSAVERNOTSUPPORTED = -3;
  258. WSANOTINITIALISED = -4;
  259. WSAEDISCON = -5;
  260. WSAHOST_NOT_FOUND = 1;
  261. WSATRY_AGAIN = 2;
  262. WSANO_RECOVERY = 3;
  263. WSANO_DATA = -6;
  264. const
  265. WSADESCRIPTION_LEN = 256;
  266. WSASYS_STATUS_LEN = 128;
  267. type
  268. PWSAData = ^TWSAData;
  269. TWSAData = packed record
  270. wVersion: Word;
  271. wHighVersion: Word;
  272. szDescription: array[0..WSADESCRIPTION_LEN] of Char;
  273. szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;
  274. iMaxSockets: Word;
  275. iMaxUdpDg: Word;
  276. lpVendorInfo: PChar;
  277. end;
  278. function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean;
  279. function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean;
  280. function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean;
  281. function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean;
  282. function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean;
  283. function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6):boolean;
  284. procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6);
  285. procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6);
  286. var
  287. in6addr_any, in6addr_loopback : TInAddr6;
  288. procedure FD_CLR(Socket: TSocket; var FDSet: TFDSet);
  289. function FD_ISSET(Socket: TSocket; var FDSet: TFDSet): Boolean;
  290. procedure FD_SET(Socket: TSocket; var FDSet: TFDSet);
  291. procedure FD_ZERO(var FDSet: TFDSet);
  292. {=============================================================================}
  293. var
  294. SynSockCS: SyncObjs.TCriticalSection;
  295. SockEnhancedApi: Boolean;
  296. SockWship6Api: Boolean;
  297. type
  298. TVarSin = packed record
  299. {$ifdef SOCK_HAS_SINLEN}
  300. sin_len : cuchar;
  301. {$endif}
  302. case integer of
  303. 0: (AddressFamily: sa_family_t);
  304. 1: (
  305. case sin_family: sa_family_t of
  306. AF_INET: (sin_port: word;
  307. sin_addr: TInAddr;
  308. sin_zero: array[0..7] of Char);
  309. AF_INET6: (sin6_port: word;
  310. sin6_flowinfo: longword;
  311. sin6_addr: TInAddr6;
  312. sin6_scope_id: longword);
  313. );
  314. end;
  315. function SizeOfVarSin(sin: TVarSin): integer;
  316. function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer;
  317. function WSACleanup: Integer;
  318. function WSAGetLastError: Integer;
  319. function GetHostName: string;
  320. function Shutdown(s: TSocket; how: Integer): Integer;
  321. function SetSockOpt(s: TSocket; level, optname: Integer; optval: TMemory;
  322. optlen: Integer): Integer;
  323. function GetSockOpt(s: TSocket; level, optname: Integer; optval: TMemory;
  324. var optlen: Integer): Integer;
  325. function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  326. function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  327. function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer;
  328. function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer;
  329. function ntohs(netshort: word): word;
  330. function ntohl(netlong: longword): longword;
  331. function Listen(s: TSocket; backlog: Integer): Integer;
  332. function IoctlSocket(s: TSocket; cmd: DWORD; var arg: integer): Integer;
  333. function htons(hostshort: word): word;
  334. function htonl(hostlong: longword): longword;
  335. function GetSockName(s: TSocket; var name: TVarSin): Integer;
  336. function GetPeerName(s: TSocket; var name: TVarSin): Integer;
  337. function Connect(s: TSocket; const name: TVarSin): Integer;
  338. function CloseSocket(s: TSocket): Integer;
  339. function Bind(s: TSocket; const addr: TVarSin): Integer;
  340. function Accept(s: TSocket; var addr: TVarSin): TSocket;
  341. function Socket(af, Struc, Protocol: Integer): TSocket;
  342. function Select(nfds: Integer; readfds, writefds, exceptfds: PFDSet;
  343. timeout: PTimeVal): Longint;
  344. function IsNewApi(Family: integer): Boolean;
  345. function SetVarSin(var Sin: TVarSin; IP, Port: string; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer;
  346. function GetSinIP(Sin: TVarSin): string;
  347. function GetSinPort(Sin: TVarSin): Integer;
  348. procedure ResolveNameToIP(Name: string; Family, SockProtocol, SockType: integer; const IPList: TStrings);
  349. function ResolveIPToName(IP: string; Family, SockProtocol, SockType: integer): string;
  350. function ResolvePort(Port: string; Family, SockProtocol, SockType: integer): Word;
  351. {==============================================================================}
  352. implementation
  353. function IN6_IS_ADDR_UNSPECIFIED(const a: PInAddr6): boolean;
  354. begin
  355. Result := ((a^.u6_addr32[0] = 0) and (a^.u6_addr32[1] = 0) and
  356. (a^.u6_addr32[2] = 0) and (a^.u6_addr32[3] = 0));
  357. end;
  358. function IN6_IS_ADDR_LOOPBACK(const a: PInAddr6): boolean;
  359. begin
  360. Result := ((a^.u6_addr32[0] = 0) and (a^.u6_addr32[1] = 0) and
  361. (a^.u6_addr32[2] = 0) and
  362. (a^.u6_addr8[12] = 0) and (a^.u6_addr8[13] = 0) and
  363. (a^.u6_addr8[14] = 0) and (a^.u6_addr8[15] = 1));
  364. end;
  365. function IN6_IS_ADDR_LINKLOCAL(const a: PInAddr6): boolean;
  366. begin
  367. Result := ((a^.u6_addr8[0] = $FE) and (a^.u6_addr8[1] = $80));
  368. end;
  369. function IN6_IS_ADDR_SITELOCAL(const a: PInAddr6): boolean;
  370. begin
  371. Result := ((a^.u6_addr8[0] = $FE) and (a^.u6_addr8[1] = $C0));
  372. end;
  373. function IN6_IS_ADDR_MULTICAST(const a: PInAddr6): boolean;
  374. begin
  375. Result := (a^.u6_addr8[0] = $FF);
  376. end;
  377. function IN6_ADDR_EQUAL(const a: PInAddr6; const b: PInAddr6): boolean;
  378. begin
  379. Result := (CompareMem( a, b, sizeof(TInAddr6)));
  380. end;
  381. procedure SET_IN6_IF_ADDR_ANY (const a: PInAddr6);
  382. begin
  383. FillChar(a^, sizeof(TInAddr6), 0);
  384. end;
  385. procedure SET_LOOPBACK_ADDR6 (const a: PInAddr6);
  386. begin
  387. FillChar(a^, sizeof(TInAddr6), 0);
  388. a^.u6_addr8[15] := 1;
  389. end;
  390. {=============================================================================}
  391. function WSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer;
  392. begin
  393. with WSData do
  394. begin
  395. wVersion := wVersionRequired;
  396. wHighVersion := $202;
  397. szDescription := 'Synsock - Synapse Platform Independent Socket Layer';
  398. szSystemStatus := 'Running on Unix/Linux by FreePascal';
  399. iMaxSockets := 32768;
  400. iMaxUdpDg := 8192;
  401. end;
  402. Result := 0;
  403. end;
  404. function WSACleanup: Integer;
  405. begin
  406. Result := 0;
  407. end;
  408. function WSAGetLastError: Integer;
  409. begin
  410. Result := fpGetErrno;
  411. end;
  412. function FD_ISSET(Socket: TSocket; var fdset: TFDSet): Boolean;
  413. begin
  414. Result := fpFD_ISSET(socket, fdset) <> 0;
  415. end;
  416. procedure FD_SET(Socket: TSocket; var fdset: TFDSet);
  417. begin
  418. fpFD_SET(Socket, fdset);
  419. end;
  420. procedure FD_CLR(Socket: TSocket; var fdset: TFDSet);
  421. begin
  422. fpFD_CLR(Socket, fdset);
  423. end;
  424. procedure FD_ZERO(var fdset: TFDSet);
  425. begin
  426. fpFD_ZERO(fdset);
  427. end;
  428. {=============================================================================}
  429. function SizeOfVarSin(sin: TVarSin): integer;
  430. begin
  431. case sin.sin_family of
  432. AF_INET:
  433. Result := SizeOf(TSockAddrIn);
  434. AF_INET6:
  435. Result := SizeOf(TSockAddrIn6);
  436. else
  437. Result := 0;
  438. end;
  439. end;
  440. {=============================================================================}
  441. function Bind(s: TSocket; const addr: TVarSin): Integer;
  442. begin
  443. if fpBind(s, @addr, SizeOfVarSin(addr)) = 0 then
  444. Result := 0
  445. else
  446. Result := SOCKET_ERROR;
  447. end;
  448. function Connect(s: TSocket; const name: TVarSin): Integer;
  449. begin
  450. if fpConnect(s, @name, SizeOfVarSin(name)) = 0 then
  451. Result := 0
  452. else
  453. Result := SOCKET_ERROR;
  454. end;
  455. function GetSockName(s: TSocket; var name: TVarSin): Integer;
  456. var
  457. len: integer;
  458. begin
  459. len := SizeOf(name);
  460. FillChar(name, len, 0);
  461. Result := fpGetSockName(s, @name, @Len);
  462. end;
  463. function GetPeerName(s: TSocket; var name: TVarSin): Integer;
  464. var
  465. len: integer;
  466. begin
  467. len := SizeOf(name);
  468. FillChar(name, len, 0);
  469. Result := fpGetPeerName(s, @name, @Len);
  470. end;
  471. function GetHostName: string;
  472. begin
  473. Result := unix.GetHostName;
  474. end;
  475. function Send(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  476. begin
  477. Result := fpSend(s, pointer(Buf), len, flags);
  478. end;
  479. function Recv(s: TSocket; Buf: TMemory; len, flags: Integer): Integer;
  480. begin
  481. Result := fpRecv(s, pointer(Buf), len, flags);
  482. end;
  483. function SendTo(s: TSocket; Buf: TMemory; len, flags: Integer; addrto: TVarSin): Integer;
  484. begin
  485. Result := fpSendTo(s, pointer(Buf), len, flags, @addrto, SizeOfVarSin(addrto));
  486. end;
  487. function RecvFrom(s: TSocket; Buf: TMemory; len, flags: Integer; var from: TVarSin): Integer;
  488. var
  489. x: integer;
  490. begin
  491. x := SizeOf(from);
  492. Result := fpRecvFrom(s, pointer(Buf), len, flags, @from, @x);
  493. end;
  494. function Accept(s: TSocket; var addr: TVarSin): TSocket;
  495. var
  496. x: integer;
  497. begin
  498. x := SizeOf(addr);
  499. Result := fpAccept(s, @addr, @x);
  500. end;
  501. function Shutdown(s: TSocket; how: Integer): Integer;
  502. begin
  503. Result := fpShutdown(s, how);
  504. end;
  505. function SetSockOpt(s: TSocket; level, optname: Integer; optval: Tmemory;
  506. optlen: Integer): Integer;
  507. begin
  508. Result := fpsetsockopt(s, level, optname, pointer(optval), optlen);
  509. end;
  510. function GetSockOpt(s: TSocket; level, optname: Integer; optval: Tmemory;
  511. var optlen: Integer): Integer;
  512. begin
  513. Result := fpgetsockopt(s, level, optname, pointer(optval), @optlen);
  514. end;
  515. function ntohs(netshort: word): word;
  516. begin
  517. Result := sockets.ntohs(NetShort);
  518. end;
  519. function ntohl(netlong: longword): longword;
  520. begin
  521. Result := sockets.ntohl(NetLong);
  522. end;
  523. function Listen(s: TSocket; backlog: Integer): Integer;
  524. begin
  525. if fpListen(s, backlog) = 0 then
  526. Result := 0
  527. else
  528. Result := SOCKET_ERROR;
  529. end;
  530. function IoctlSocket(s: TSocket; cmd: DWORD; var arg: integer): Integer;
  531. begin
  532. Result := fpIoctl(s, cmd, @arg);
  533. end;
  534. function htons(hostshort: word): word;
  535. begin
  536. Result := sockets.htons(Hostshort);
  537. end;
  538. function htonl(hostlong: longword): longword;
  539. begin
  540. Result := sockets.htonl(HostLong);
  541. end;
  542. function CloseSocket(s: TSocket): Integer;
  543. begin
  544. Result := sockets.CloseSocket(s);
  545. end;
  546. function Socket(af, Struc, Protocol: Integer): TSocket;
  547. begin
  548. Result := fpSocket(af, struc, protocol);
  549. end;
  550. function Select(nfds: Integer; readfds, writefds, exceptfds: PFDSet;
  551. timeout: PTimeVal): Longint;
  552. begin
  553. Result := fpSelect(nfds, readfds, writefds, exceptfds, timeout);
  554. end;
  555. {=============================================================================}
  556. function IsNewApi(Family: integer): Boolean;
  557. begin
  558. Result := SockEnhancedApi;
  559. if not Result then
  560. Result := (Family = AF_INET6) and SockWship6Api;
  561. end;
  562. function SetVarSin(var Sin: TVarSin; IP, Port: string; Family, SockProtocol, SockType: integer; PreferIP4: Boolean): integer;
  563. var
  564. TwoPass: boolean;
  565. f1, f2: integer;
  566. function GetAddr(f:integer): integer;
  567. var
  568. a4: array [1..1] of in_addr;
  569. a6: array [1..1] of Tin6_addr;
  570. begin
  571. Result := WSAEPROTONOSUPPORT;
  572. case f of
  573. AF_INET:
  574. begin
  575. if IP = cAnyHost then
  576. begin
  577. Sin.sin_family := AF_INET;
  578. Result := 0;
  579. end
  580. else
  581. begin
  582. if lowercase(IP) = cLocalHostStr then
  583. a4[1].s_addr := htonl(INADDR_LOOPBACK)
  584. else
  585. begin
  586. a4[1].s_addr := 0;
  587. Result := WSAHOST_NOT_FOUND;
  588. a4[1] := StrTonetAddr(IP);
  589. if a4[1].s_addr = INADDR_ANY then
  590. Resolvename(ip, a4);
  591. end;
  592. if a4[1].s_addr <> INADDR_ANY then
  593. begin
  594. Sin.sin_family := AF_INET;
  595. sin.sin_addr := a4[1];
  596. Result := 0;
  597. end;
  598. end;
  599. end;
  600. AF_INET6:
  601. begin
  602. if IP = c6AnyHost then
  603. begin
  604. Sin.sin_family := AF_INET6;
  605. Result := 0;
  606. end
  607. else
  608. begin
  609. if lowercase(IP) = cLocalHostStr then
  610. SET_LOOPBACK_ADDR6(@a6[1])
  611. else
  612. begin
  613. Result := WSAHOST_NOT_FOUND;
  614. SET_IN6_IF_ADDR_ANY(@a6[1]);
  615. a6[1] := StrTonetAddr6(IP);
  616. if IN6_IS_ADDR_UNSPECIFIED(@a6[1]) then
  617. Resolvename6(ip, a6);
  618. end;
  619. if IN6_IS_ADDR_UNSPECIFIED(@a6[1]) then
  620. begin
  621. Sin.sin_family := AF_INET6;
  622. sin.sin6_addr := a6[1];
  623. Result := 0;
  624. end;
  625. end;
  626. end;
  627. end;
  628. end;
  629. begin
  630. Result := 0;
  631. FillChar(Sin, Sizeof(Sin), 0);
  632. Sin.sin_port := Resolveport(port, family, SockProtocol, SockType);
  633. TwoPass := False;
  634. if Family = AF_UNSPEC then
  635. begin
  636. if PreferIP4 then
  637. begin
  638. f1 := AF_INET;
  639. f2 := AF_INET6;
  640. TwoPass := True;
  641. end
  642. else
  643. begin
  644. f2 := AF_INET;
  645. f1 := AF_INET6;
  646. TwoPass := True;
  647. end;
  648. end
  649. else
  650. f1 := Family;
  651. Result := GetAddr(f1);
  652. if Result <> 0 then
  653. if TwoPass then
  654. Result := GetAddr(f2);
  655. end;
  656. function GetSinIP(Sin: TVarSin): string;
  657. begin
  658. Result := '';
  659. case sin.AddressFamily of
  660. AF_INET:
  661. begin
  662. result := NetAddrToStr(sin.sin_addr);
  663. end;
  664. AF_INET6:
  665. begin
  666. result := NetAddrToStr6(sin.sin6_addr);
  667. end;
  668. end;
  669. end;
  670. function GetSinPort(Sin: TVarSin): Integer;
  671. begin
  672. if (Sin.sin_family = AF_INET6) then
  673. Result := synsock.ntohs(Sin.sin6_port)
  674. else
  675. Result := synsock.ntohs(Sin.sin_port);
  676. end;
  677. procedure ResolveNameToIP(Name: string; Family, SockProtocol, SockType: integer; const IPList: TStrings);
  678. var
  679. x, n: integer;
  680. a4: array [1..255] of in_addr;
  681. a6: array [1..255] of Tin6_addr;
  682. begin
  683. IPList.Clear;
  684. if (family = AF_INET) or (family = AF_UNSPEC) then
  685. begin
  686. if lowercase(name) = cLocalHostStr then
  687. IpList.Add(cLocalHost)
  688. else
  689. begin
  690. a4[1] := StrTonetAddr(name);
  691. if a4[1].s_addr = INADDR_ANY then
  692. x := Resolvename(name, a4)
  693. else
  694. x := 1;
  695. for n := 1 to x do
  696. IpList.Add(netaddrToStr(a4[n]));
  697. end;
  698. end;
  699. if (family = AF_INET6) or (family = AF_UNSPEC) then
  700. begin
  701. if lowercase(name) = cLocalHostStr then
  702. IpList.Add(c6LocalHost)
  703. else
  704. begin
  705. a6[1] := StrTonetAddr6(name);
  706. if IN6_IS_ADDR_UNSPECIFIED(@a6[1]) then
  707. x := Resolvename6(name, a6)
  708. else
  709. x := 1;
  710. for n := 1 to x do
  711. IpList.Add(netaddrToStr6(a6[n]));
  712. end;
  713. end;
  714. if IPList.Count = 0 then
  715. IPList.Add(cLocalHost);
  716. end;
  717. function ResolvePort(Port: string; Family, SockProtocol, SockType: integer): Word;
  718. var
  719. ProtoEnt: TProtocolEntry;
  720. ServEnt: TServiceEntry;
  721. begin
  722. Result := synsock.htons(StrToIntDef(Port, 0));
  723. if Result = 0 then
  724. begin
  725. ProtoEnt.Name := '';
  726. GetProtocolByNumber(SockProtocol, ProtoEnt);
  727. ServEnt.port := 0;
  728. GetServiceByName(Port, ProtoEnt.Name, ServEnt);
  729. Result := ServEnt.port;
  730. end;
  731. end;
  732. function ResolveIPToName(IP: string; Family, SockProtocol, SockType: integer): string;
  733. var
  734. n: integer;
  735. a4: array [1..1] of in_addr;
  736. a6: array [1..1] of Tin6_addr;
  737. a: array [1..1] of string;
  738. begin
  739. Result := IP;
  740. a4[1] := StrToNetAddr(IP);
  741. if a4[1].s_addr <> INADDR_ANY then
  742. begin
  743. //why ResolveAddress need address in HOST order? :-O
  744. n := ResolveAddress(nettohost(a4[1]), a);
  745. if n > 0 then
  746. Result := a[1];
  747. end
  748. else
  749. begin
  750. a6[1] := StrToNetAddr6(IP);
  751. n := ResolveAddress6(a6[1], a);
  752. if n > 0 then
  753. Result := a[1];
  754. end;
  755. end;
  756. {=============================================================================}
  757. function InitSocketInterface(stack: string): Boolean;
  758. begin
  759. SockEnhancedApi := False;
  760. SockWship6Api := False;
  761. // Libc.Signal(Libc.SIGPIPE, TSignalHandler(Libc.SIG_IGN));
  762. Result := True;
  763. end;
  764. function DestroySocketInterface: Boolean;
  765. begin
  766. Result := True;
  767. end;
  768. initialization
  769. begin
  770. SynSockCS := SyncObjs.TCriticalSection.Create;
  771. SET_IN6_IF_ADDR_ANY (@in6addr_any);
  772. SET_LOOPBACK_ADDR6 (@in6addr_loopback);
  773. end;
  774. finalization
  775. begin
  776. SynSockCS.Free;
  777. end;
  778. {$ENDIF}