home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 December / Chip_2001-12_cd1.bin / zkuste / delphi / unity / d23456 / SYNAPSE.ZIP / source / lib / synsock.pas < prev   
Encoding:
Pascal/Delphi Source File  |  2001-08-09  |  20.9 KB  |  621 lines

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