home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 November / Chip_2002-11_cd1.bin / zkuste / delphi / kompon / d3456 / SYNAPSE.ZIP / source / lib / synsock.pas < prev   
Pascal/Delphi Source File  |  2002-07-07  |  23KB  |  640 lines

  1. {==============================================================================|
  2. | Project : Delphree - Synapse                                   | 002.001.000 |
  3. |==============================================================================|
  4. | Content: Socket Independent Platform Layer                                   |
  5. |==============================================================================|
  6. | Copyright (c)1999-2002, 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)2001.                     |
  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.  
  45. { Comment next line if you need dynamic loading of winsock under Windows
  46.   or any another DLL stack by CreateAlternate constructor of TBlockSocket Class.
  47.   if next line stay uncommented, is used static mapping. This is fater method.
  48.   Under Linx is always used static maping to Libc. }
  49. {$DEFINE STATICWINSOCK}
  50.  
  51. unit synsock;
  52.  
  53. interface
  54.  
  55. uses
  56. {$IFDEF LINUX}
  57.   Libc, KernelIoctl;
  58. {$ELSE}
  59.   Windows, WinSock;
  60. {$ENDIF}
  61.  
  62. {$IFDEF LINUX}
  63. const
  64.   WSAEINTR = EINTR;
  65.   WSAEBADF = EBADF;
  66.   WSAEACCES = EACCES;
  67.   WSAEFAULT = EFAULT;
  68.   WSAEINVAL = EINVAL;
  69.   WSAEMFILE = EMFILE;
  70.   WSAEWOULDBLOCK = EWOULDBLOCK;
  71.   WSAEINPROGRESS = EINPROGRESS;
  72.   WSAEALREADY = EALREADY;
  73.   WSAENOTSOCK = ENOTSOCK;
  74.   WSAEDESTADDRREQ = EDESTADDRREQ;
  75.   WSAEMSGSIZE = EMSGSIZE;
  76.   WSAEPROTOTYPE = EPROTOTYPE;
  77.   WSAENOPROTOOPT = ENOPROTOOPT;
  78.   WSAEPROTONOSUPPORT = EPROTONOSUPPORT;
  79.   WSAESOCKTNOSUPPORT = ESOCKTNOSUPPORT;
  80.   WSAEOPNOTSUPP = EOPNOTSUPP;
  81.   WSAEPFNOSUPPORT = EPFNOSUPPORT;
  82.   WSAEAFNOSUPPORT = EAFNOSUPPORT;
  83.   WSAEADDRINUSE = EADDRINUSE;
  84.   WSAEADDRNOTAVAIL = EADDRNOTAVAIL;
  85.   WSAENETDOWN = ENETDOWN;
  86.   WSAENETUNREACH = ENETUNREACH;
  87.   WSAENETRESET = ENETRESET;
  88.   WSAECONNABORTED = ECONNABORTED;
  89.   WSAECONNRESET = ECONNRESET;
  90.   WSAENOBUFS = ENOBUFS;
  91.   WSAEISCONN = EISCONN;
  92.   WSAENOTCONN = ENOTCONN;
  93.   WSAESHUTDOWN = ESHUTDOWN;
  94.   WSAETOOMANYREFS = ETOOMANYREFS;
  95.   WSAETIMEDOUT = ETIMEDOUT;
  96.   WSAECONNREFUSED = ECONNREFUSED;
  97.   WSAELOOP = ELOOP;
  98.   WSAENAMETOOLONG = ENAMETOOLONG;
  99.   WSAEHOSTDOWN = EHOSTDOWN;
  100.   WSAEHOSTUNREACH = EHOSTUNREACH;
  101.   WSAENOTEMPTY = ENOTEMPTY;
  102.   WSAEPROCLIM = -1;
  103.   WSAEUSERS = EUSERS;
  104.   WSAEDQUOT = EDQUOT;
  105.   WSAESTALE = ESTALE;
  106.   WSAEREMOTE = EREMOTE;
  107.   WSASYSNOTREADY = -2;
  108.   WSAVERNOTSUPPORTED = -3;
  109.   WSANOTINITIALISED = -4;
  110.   WSAEDISCON = -5;
  111.   WSAHOST_NOT_FOUND = HOST_NOT_FOUND;
  112.   WSATRY_AGAIN = TRY_AGAIN;
  113.   WSANO_RECOVERY = NO_RECOVERY;
  114. //  WSANO_DATA         = NO_DATA;
  115.   WSANO_DATA = -6;
  116.  
  117. {$ELSE}
  118.  
  119. const
  120.   DLLStackName = 'wsock32.dll';
  121. var
  122.   LibHandle: THandle = 0;
  123. {$ENDIF}
  124.  
  125. {$IFDEF LINUX}
  126. const
  127.   WSADESCRIPTION_LEN = 256;
  128.   WSASYS_STATUS_LEN = 128;
  129. type
  130.   PWSAData = ^TWSAData;
  131.   TWSAData = packed record
  132.     wVersion: Word;
  133.     wHighVersion: Word;
  134.     szDescription: array[0..WSADESCRIPTION_LEN] of Char;
  135.     szSystemStatus: array[0..WSASYS_STATUS_LEN] of Char;
  136.     iMaxSockets: Word;
  137.     iMaxUdpDg: Word;
  138.     lpVendorInfo: PChar;
  139.   end;
  140.   DWORD = Integer;
  141.   TLinger = Linger;
  142. {$ENDIF}
  143.  
  144. type
  145.   TWSAStartup = function(wVersionRequired: Word;
  146.     var WSData: TWSAData): Integer; stdcall;
  147.   TWSACleanup = function: Integer; stdcall;
  148.   TWSAGetLastError = function: Integer; stdcall;
  149.   TGetServByName = function(name, proto: PChar): PServEnt; stdcall;
  150.   TGetServByPort = function(port: Integer; proto: PChar): PServEnt; stdcall;
  151.   TGetProtoByName = function(name: PChar): PProtoEnt; stdcall;
  152.   TGetProtoByNumber = function(proto: Integer): PProtoEnt; stdcall;
  153.   TGetHostByName = function(name: PChar): PHostEnt; stdcall;
  154.   TGetHostByAddr = function(addr: Pointer; len, Struc: Integer): PHostEnt; stdcall;
  155.   TGetHostName = function(name: PChar; len: Integer): Integer; stdcall;
  156.   TShutdown = function(s: TSocket; how: Integer): Integer; stdcall;
  157.   TSetSockOpt = function(s: TSocket; level, optname: Integer;
  158.     optval: PChar; optlen: Integer): Integer; stdcall;
  159.   TGetSockOpt = function(s: TSocket; level, optname: Integer;
  160.     optval: PChar; var optlen: Integer): Integer; stdcall;
  161.   TSendTo = function(s: TSocket; var Buf;
  162.     len, flags: Integer; var addrto: TSockAddr;
  163.     tolen: Integer): Integer; stdcall;
  164.   TSend = function(s: TSocket; var Buf;
  165.     len, flags: Integer): Integer; stdcall;
  166.   TRecv = function(s: TSocket;
  167.     var Buf; len, flags: Integer): Integer; stdcall;
  168.   TRecvFrom = function(s: TSocket;
  169.     var Buf; len, flags: Integer; var from: TSockAddr;
  170.     var fromlen: Integer): Integer; stdcall;
  171.   Tntohs = function(netshort: u_short): u_short; stdcall;
  172.   Tntohl = function(netlong: u_long): u_long; stdcall;
  173.   TListen = function(s: TSocket; backlog: Integer): Integer; stdcall;
  174.   TIoctlSocket = function(s: TSocket; cmd: DWORD;
  175.     var arg: u_long): Integer; stdcall;
  176.   TInet_ntoa = function(inaddr: TInAddr): PChar; stdcall;
  177.   TInet_addr = function(cp: PChar): u_long; stdcall;
  178.   Thtons = function(hostshort: u_short): u_short; stdcall;
  179.   Thtonl = function(hostlong: u_long): u_long; stdcall;
  180.   TGetSockName = function(s: TSocket; var name: TSockAddr;
  181.     var namelen: Integer): Integer; stdcall;
  182.   TGetPeerName = function(s: TSocket; var name: TSockAddr;
  183.     var namelen: Integer): Integer; stdcall;
  184.   TConnect = function(s: TSocket; var name: TSockAddr;
  185.     namelen: Integer): Integer; stdcall;
  186.   TCloseSocket = function(s: TSocket): Integer; stdcall;
  187.   TBind = function(s: TSocket; var addr: TSockAddr;
  188.     namelen: Integer): Integer; stdcall;
  189.   TAccept = function(s: TSocket; addr: PSockAddr;
  190.     addrlen: PInteger): TSocket; stdcall;
  191.   TSocketProc = function(af, Struc, Protocol: Integer): TSocket; stdcall;
  192.   TSelect = function(nfds: Integer; readfds, writefds, exceptfds: PFDSet;
  193.     timeout: PTimeVal): Longint; stdcall;
  194.  
  195. var
  196.   WSAStartup: TWSAStartup = nil;
  197.   WSACleanup: TWSACleanup = nil;
  198.   WSAGetLastError: TWSAGetLastError = nil;
  199.   GetServByName: TGetServByName = nil;
  200.   GetServByPort: TGetServByPort = nil;
  201.   GetProtoByName: TGetProtoByName = nil;
  202.   GetProtoByNumber: TGetProtoByNumber = nil;
  203.   GetHostByName: TGetHostByName = nil;
  204.   GetHostByAddr: TGetHostByAddr = nil;
  205.   GetHostName: TGetHostName = nil;
  206.   Shutdown: TShutdown = nil;
  207.   SetSockOpt: TSetSockOpt = nil;
  208.   GetSockOpt: TGetSockOpt = nil;
  209.   SendTo: TSendTo = nil;
  210.   Send: TSend = nil;
  211.   Recv: TRecv = nil;
  212.   RecvFrom: TRecvFrom = nil;
  213.   ntohs: Tntohs = nil;
  214.   ntohl: Tntohl = nil;
  215.   Listen: TListen = nil;
  216.   IoctlSocket: TIoctlSocket = nil;
  217.   Inet_ntoa: TInet_ntoa = nil;
  218.   Inet_addr: TInet_addr = nil;
  219.   htons: Thtons = nil;
  220.   htonl: Thtonl = nil;
  221.   GetSockName: TGetSockName = nil;
  222.   GetPeerName: TGetPeerName = nil;
  223.   Connect: TConnect = nil;
  224.   CloseSocket: TCloseSocket = nil;
  225.   Bind: TBind = nil;
  226.   Accept: TAccept = nil;
  227.   Socket: TSocketProc = nil;
  228.   Select: TSelect = nil;
  229.  
  230. function InitSocketInterface(stack: string): Boolean;
  231. function DestroySocketInterface: Boolean;
  232.  
  233. {$IFDEF LINUX}
  234. function LSWSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer; stdcall;
  235. function LSWSACleanup: Integer; stdcall;
  236. function LSWSAGetLastError: Integer; stdcall;
  237. function LSGetServByName(name, proto: PChar): PServEnt; stdcall;
  238. function LSGetServByPort(port: Integer; proto: PChar): PServEnt; stdcall;
  239. function LSGetProtoByName(name: PChar): PProtoEnt; stdcall;
  240. function LSGetProtoByNumber(proto: Integer): PProtoEnt; stdcall;
  241. function LSGetHostByName(name: PChar): PHostEnt; stdcall;
  242. function LSGetHostByAddr(addr: Pointer; len, Struc: Integer): PHostEnt; stdcall;
  243. function LSGetHostName(name: PChar; len: Integer): Integer; stdcall;
  244. function LSShutdown(s: TSocket; how: Integer): Integer; stdcall;
  245. function LSSetSockOpt(s: TSocket; level, optname: Integer; optval: PChar;
  246.   optlen: Integer): Integer; stdcall;
  247. function LSGetSockOpt(s: TSocket; level, optname: Integer; optval: PChar;
  248.   var optlen: Integer): Integer; stdcall;
  249. function LSSendTo(s: TSocket; var Buf; len, flags: Integer;
  250.   var addrto: TSockAddr; tolen: Integer): Integer; stdcall;
  251. function LSSend(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
  252. function LSRecv(s: TSocket; var Buf; len, flags: Integer): Integer; stdcall;
  253. function LSRecvFrom(s: TSocket; var Buf; len, flags: Integer;
  254.   var from: TSockAddr; var fromlen: Integer): Integer; stdcall;
  255. function LSntohs(netshort: u_short): u_short; stdcall;
  256. function LSntohl(netlong: u_long): u_long; stdcall;
  257. function LSListen(s: TSocket; backlog: Integer): Integer; stdcall;
  258. function LSIoctlSocket(s: TSocket; cmd: DWORD; var arg: u_long): Integer; stdcall;
  259. function LSInet_ntoa(inaddr: TInAddr): PChar; stdcall;
  260. function LSInet_addr(cp: PChar): u_long; stdcall;
  261. function LShtons(hostshort: u_short): u_short; stdcall;
  262. function LShtonl(hostlong: u_long): u_long; stdcall;
  263. function LSGetSockName(s: TSocket; var name: TSockAddr;
  264.   var namelen: Integer): Integer; stdcall;
  265. function LSGetPeerName(s: TSocket; var name: TSockAddr;
  266.   var namelen: Integer): Integer; stdcall;
  267. function LSConnect(s: TSocket; var name: TSockAddr; namelen: Integer): Integer; stdcall;
  268. function LSCloseSocket(s: TSocket): Integer; stdcall;
  269. function LSBind(s: TSocket; var addr: TSockAddr; namelen: Integer): Integer; stdcall;
  270. function LSAccept(s: TSocket; addr: PSockAddr; addrlen: PInteger): TSocket; stdcall;
  271. function LSSocketProc(af, Struc, Protocol: Integer): TSocket; stdcall;
  272. function LSSelect(nfds: Integer; readfds, writefds, exceptfds: PFDSet;
  273.   timeout: PTimeVal): Longint; stdcall;
  274. {$ENDIF}
  275.  
  276. implementation
  277.  
  278. {$IFNDEF LINUX}
  279. {$IFNDEF STATICWINSOCK}
  280. uses syncobjs;
  281.  
  282. var
  283.   SynSockCS: TCriticalSection;
  284.   SynSockCount: Integer = 0;
  285. {$ENDIF}
  286. {$ENDIF}
  287.  
  288. {$IFDEF LINUX}
  289.  
  290. function LSWSAStartup(wVersionRequired: Word; var WSData: TWSAData): Integer;
  291. begin
  292.   with WSData do
  293.   begin
  294.     wVersion := wVersionRequired;
  295.     wHighVersion := $101;
  296.     szDescription := 'Synapse Platform Independent Socket Layer';
  297.     szSystemStatus := 'On Linux';
  298.     iMaxSockets := 32768;
  299.     iMaxUdpDg := 8192;
  300.   end;
  301.   Result := 0;
  302. end;
  303.  
  304. function LSWSACleanup: Integer;
  305. begin
  306.   Result := 0;
  307. end;
  308.  
  309. function LSWSAGetLastError: Integer;
  310. begin
  311.   Result := System.GetLastError;
  312. end;
  313.  
  314. function LSGetServByName(name, proto: PChar): PServEnt;
  315. begin
  316.   Result := libc.GetServByName(name, proto);
  317. end;
  318.  
  319. function LSGetServByPort(port: Integer; proto: PChar): PServEnt;
  320. begin
  321.   Result := libc.GetServByPort(port, proto);
  322. end;
  323.  
  324. function LSGetProtoByName(name: PChar): PProtoEnt;
  325. begin
  326.   Result := libc.GetProtoByName(Name);
  327. end;
  328.  
  329. function LSGetProtoByNumber(proto: Integer): PProtoEnt;
  330. begin
  331.   Result := libc.GetProtoByNumber(proto);
  332. end;
  333.  
  334. function LSGetHostByName(name: PChar): PHostEnt;
  335. begin
  336.   Result := libc.GetHostByName(Name);
  337. end;
  338.  
  339. function LSGetHostByAddr(addr: Pointer; len, Struc: Integer): PHostEnt;
  340. begin
  341.   Result := libc.GetHostByAddr(Addr, len, struc);
  342. end;
  343.  
  344. function LSGetHostName(name: PChar; len: Integer): Integer;
  345. begin
  346.   Result := libc.GetHostName(Name, Len);
  347. end;
  348.  
  349. function LSShutdown(s: TSocket; how: Integer): Integer;
  350. begin
  351.   Result := libc.Shutdown(S, How);
  352. end;
  353.  
  354. function LSSetSockOpt(s: TSocket; level, optname: Integer; optval: PChar;
  355.   optlen: Integer): Integer;
  356. begin
  357.   Result := libc.SetSockOpt(S, Level, OptName, OptVal, OptLen);
  358. end;
  359.  
  360. function LSGetSockOpt(s: TSocket; level, optname: Integer; optval: PChar;
  361.   var optlen: Integer): Integer;
  362. begin
  363.   Result := libc.getsockopt(s, level, optname, optval, cardinal(optlen));
  364. end;
  365.  
  366. function LSSendTo(s: TSocket; var Buf; len, flags: Integer;
  367.   var addrto: TSockAddr; tolen: Integer): Integer;
  368. begin
  369.   Result := libc.SendTo(S, Buf, Len, Flags, Addrto, Tolen);
  370. end;
  371.  
  372. function LSSend(s: TSocket; var Buf; len, flags: Integer): Integer;
  373. begin
  374.   Result := libc.Send(S, Buf, Len, Flags);
  375. end;
  376.  
  377. function LSRecv(s: TSocket; var Buf; len, flags: Integer): Integer;
  378. begin
  379.   Result := libc.Recv(S, Buf, Len, Flags);
  380. end;
  381.  
  382. function LSRecvFrom(s: TSocket; var Buf; len, flags: Integer;
  383.   var from: TSockAddr; var fromlen: Integer): Integer;
  384. begin
  385.   Result := libc.RecvFrom(S, Buf, Len, Flags, @from, @fromlen);
  386. end;
  387.  
  388. function LSntohs(netshort: u_short): u_short;
  389. begin
  390.   Result := libc.NToHS(netshort);
  391. end;
  392.  
  393. function LSntohl(netlong: u_long): u_long;
  394. begin
  395.   Result := libc.NToHL(netlong);
  396. end;
  397.  
  398. function LSListen(s: TSocket; backlog: Integer): Integer;
  399. begin
  400.   Result := libc.Listen(S, Backlog);
  401. end;
  402.  
  403. function LSIoctlSocket(s: TSocket; cmd: DWORD; var arg: u_long): Integer;
  404. begin
  405.   Result := libc.ioctl(s, cmd, @arg);
  406. end;
  407.  
  408. function LSInet_ntoa(inaddr: TInAddr): PChar;
  409. begin
  410.   Result := libc.inet_ntoa(inaddr);
  411. end;
  412.  
  413. function LSInet_addr(cp: PChar): u_long;
  414. begin
  415.   Result := libc.inet_addr(cp);
  416. end;
  417.  
  418. function LShtons(hostshort: u_short): u_short;
  419. begin
  420.   Result := libc.HToNs(HostShort);
  421. end;
  422.  
  423. function LShtonl(hostlong: u_long): u_long;
  424. begin
  425.   Result := libc.HToNL(HostLong);
  426. end;
  427.  
  428. function LSGetSockName(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer;
  429. begin
  430.   Result := libc.GetSockName(S, Name, cardinal(namelen));
  431. end;
  432.  
  433. function LSGetPeerName(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer;
  434. begin
  435.   Result := libc.GetPeerName(S, Name, cardinal(namelen));
  436. end;
  437.  
  438. function LSConnect(s: TSocket; var name: TSockAddr; namelen: Integer): Integer;
  439. begin
  440.   Result := libc.Connect(S, name, namelen);
  441. end;
  442.  
  443. function LSCloseSocket(s: TSocket): Integer;
  444. begin
  445.   Result := libc.__close(s);
  446. end;
  447.  
  448. function LSBind(s: TSocket; var addr: TSockAddr; namelen: Integer): Integer;
  449. begin
  450.   Result := libc.Bind(S, addr, namelen);
  451. end;
  452.  
  453. function LSAccept(s: TSocket; addr: PSockAddr; addrlen: PInteger): TSocket;
  454. begin
  455.   Result := libc.Accept(S, addr, psocketlength(addrlen));
  456. end;
  457.  
  458. function LSSocketProc(af, Struc, Protocol: Integer): TSocket;
  459. begin
  460.   Result := libc.Socket(Af, Struc, Protocol);
  461. end;
  462.  
  463. function LSSelect(nfds: Integer; readfds, writefds, exceptfds: PFDSet;
  464.   timeout: PTimeVal): Longint;
  465. begin
  466.   Result := libc.Select(nfds, readfds, writefds, exceptfds, timeout);
  467. end;
  468.  
  469. {$ENDIF}
  470.  
  471. function InitSocketInterface(stack: string): Boolean;
  472. begin
  473. {$IFDEF LINUX}
  474.   Accept := LSAccept;
  475.   Bind := LSBind;
  476.   CloseSocket := LSCloseSocket;
  477.   Connect := LSConnect;
  478.   GetPeerName := LSGetPeerName;
  479.   GetSockName := LSGetSockName;
  480.   GetSockOpt := LSGetSockOpt;
  481.   Htonl := LShtonl;
  482.   Htons := LShtons;
  483.   Inet_Addr := LSinet_addr;
  484.   Inet_Ntoa := LSinet_ntoa;
  485.   IoctlSocket := LSioctlsocket;
  486.   Listen := LSlisten;
  487.   Ntohl := LSntohl;
  488.   Ntohs := LSntohs;
  489.   Recv := LSrecv;
  490.   RecvFrom := LSrecvfrom;
  491.   Select := LSselect;
  492.   Send := LSsend;
  493.   SendTo := LSsendto;
  494.   SetSockOpt := LSsetsockopt;
  495.   ShutDown := LSshutdown;
  496.   Socket := LSsocketProc;
  497.   GetHostByAddr := LSGetHostByAddr;
  498.   GetHostByName := LSGetHostByName;
  499.   GetProtoByName := LSGetProtoByName;
  500.   GetProtoByNumber := LSGetProtoByNumber;
  501.   GetServByName := LSGetServByName;
  502.   GetServByPort := LSGetServByPort;
  503.   GetHostName := LSGetHostName;
  504.   WSAGetLastError := LSWSAGetLastError;
  505.   WSAStartup := LSWSAStartup;
  506.   WSACleanup := LSWSACleanup;
  507.   Result := True;
  508. {$ELSE}
  509. {$IFDEF STATICWINSOCK}
  510.   Accept := Winsock.Accept;
  511.   Bind := Winsock.Bind;
  512.   CloseSocket := Winsock.CloseSocket;
  513.   Connect := Winsock.Connect;
  514.   GetPeerName := Winsock.GetPeerName;
  515.   GetSockName := Winsock.GetSockName;
  516.   GetSockOpt := Winsock.GetSockOpt;
  517.   Htonl := Winsock.htonl;
  518.   Htons := Winsock.htons;
  519.   Inet_Addr := Winsock.inet_addr;
  520.   Inet_Ntoa := Winsock.inet_ntoa;
  521.   IoctlSocket := Winsock.ioctlsocket;
  522.   Listen := Winsock.listen;
  523.   Ntohl := Winsock.ntohl;
  524.   Ntohs := Winsock.ntohs;
  525.   Recv := Winsock.recv;
  526.   RecvFrom := Winsock.recvfrom;
  527.   Select := Winsock.select;
  528.   Send := Winsock.send;
  529.   SendTo := Winsock.sendto;
  530.   SetSockOpt := Winsock.setsockopt;
  531.   ShutDown := Winsock.shutdown;
  532.   Socket := Winsock.socket;
  533.   GetHostByAddr := Winsock.GetHostByAddr;
  534.   GetHostByName := Winsock.GetHostByName;
  535.   GetProtoByName := Winsock.GetProtoByName;
  536.   GetProtoByNumber := Winsock.GetProtoByNumber;
  537.   GetServByName := Winsock.GetServByName;
  538.   GetServByPort := Winsock.GetServByPort;
  539.   GetHostName := Winsock.GetHostName;
  540.   WSAGetLastError := Winsock.WSAGetLastError;
  541.   WSAStartup := Winsock.WSAStartup;
  542.   WSACleanup := Winsock.WSACleanup;
  543.   Result := True;
  544. {$ELSE}
  545.   Result := False;
  546.   if stack = '' then
  547.     stack := DLLStackName;
  548.   SynSockCS.Enter;
  549.   try
  550.     if SynSockCount = 0 then
  551.     begin
  552.       LibHandle := Windows.LoadLibrary(PChar(Stack));
  553.       if LibHandle <> 0 then
  554.       begin
  555.         Accept := Windows.GetProcAddress(LibHandle, PChar('accept'));
  556.         Bind := Windows.GetProcAddress(LibHandle, PChar('bind'));
  557.         CloseSocket := Windows.GetProcAddress(LibHandle, PChar('closesocket'));
  558.         Connect := Windows.GetProcAddress(LibHandle, PChar('connect'));
  559.         GetPeerName := Windows.GetProcAddress(LibHandle, PChar('getpeername'));
  560.         GetSockName := Windows.GetProcAddress(LibHandle, PChar('getsockname'));
  561.         GetSockOpt := Windows.GetProcAddress(LibHandle, PChar('getsockopt'));
  562.         Htonl := Windows.GetProcAddress(LibHandle, PChar('htonl'));
  563.         Htons := Windows.GetProcAddress(LibHandle, PChar('htons'));
  564.         Inet_Addr := Windows.GetProcAddress(LibHandle, PChar('inet_addr'));
  565.         Inet_Ntoa := Windows.GetProcAddress(LibHandle, PChar('inet_ntoa'));
  566.         IoctlSocket := Windows.GetProcAddress(LibHandle, PChar('ioctlsocket'));
  567.         Listen := Windows.GetProcAddress(LibHandle, PChar('listen'));
  568.         Ntohl := Windows.GetProcAddress(LibHandle, PChar('ntohl'));
  569.         Ntohs := Windows.GetProcAddress(LibHandle, PChar('ntohs'));
  570.         Recv := Windows.GetProcAddress(LibHandle, PChar('recv'));
  571.         RecvFrom := Windows.GetProcAddress(LibHandle, PChar('recvfrom'));
  572.         Select := Windows.GetProcAddress(LibHandle, PChar('select'));
  573.         Send := Windows.GetProcAddress(LibHandle, PChar('send'));
  574.         SendTo := Windows.GetProcAddress(LibHandle, PChar('sendto'));
  575.         SetSockOpt := Windows.GetProcAddress(LibHandle, PChar('setsockopt'));
  576.         ShutDown := Windows.GetProcAddress(LibHandle, PChar('shutdown'));
  577.         Socket := Windows.GetProcAddress(LibHandle, PChar('socket'));
  578.         GetHostByAddr := Windows.GetProcAddress(LibHandle, PChar('gethostbyaddr'));
  579.         GetHostByName := Windows.GetProcAddress(LibHandle, PChar('gethostbyname'));
  580.         GetProtoByName := Windows.GetProcAddress(LibHandle, PChar('getprotobyname'));
  581.         GetProtoByNumber := Windows.GetProcAddress(LibHandle, PChar('getprotobynumber'));
  582.         GetServByName := Windows.GetProcAddress(LibHandle, PChar('getservbyname'));
  583.         GetServByPort := Windows.GetProcAddress(LibHandle, PChar('getservbyport'));
  584.         GetHostName := Windows.GetProcAddress(LibHandle, PChar('gethostname'));
  585.         WSAGetLastError := Windows.GetProcAddress(LibHandle, PChar('WSAGetLastError'));
  586.         WSAStartup := Windows.GetProcAddress(LibHandle, PChar('WSAStartup'));
  587.         WSACleanup := Windows.GetProcAddress(LibHandle, PChar('WSACleanup'));
  588.         Result := True;
  589.       end;
  590.     end
  591.     else Result := True;
  592.     if Result then
  593.       Inc(SynSockCount);
  594.   finally
  595.     SynSockCS.Leave;
  596.   end;
  597. {$ENDIF}
  598. {$ENDIF}
  599. end;
  600.  
  601. function DestroySocketInterface: Boolean;
  602. begin
  603. {$IFDEF LINUX}
  604. {$ELSE}
  605. {$IFNDEF STATICWINSOCK}
  606.   SynSockCS.Enter;
  607.   try
  608.     Dec(SynSockCount);
  609.     if SynSockCount < 0 then
  610.       SynSockCount := 0;
  611.     if SynSockCount = 0 then
  612.       if LibHandle <> 0 then
  613.       begin
  614.         Windows.FreeLibrary(libHandle);
  615.         LibHandle := 0;
  616.       end;
  617.   finally
  618.     SynSockCS.Leave;
  619.   end;
  620. {$ENDIF}
  621. {$ENDIF}
  622.   Result := True;
  623. end;
  624.  
  625. {$IFNDEF LINUX}
  626. {$IFNDEF STATICWINSOCK}
  627. initialization
  628. begin
  629.   SynSockCS:= TCriticalSection.Create;
  630. end;
  631.  
  632. finalization
  633. begin
  634.   SynSockCS.Free;
  635. end;
  636. {$ENDIF}
  637. {$ENDIF}
  638.  
  639. end.
  640.