{*_* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Author: Franois PIETTE
Description:
Creation: Jan 02, 2002 (Original code from TWSocket (c) F. Piette 1996)
Version: 1.00
EMail: http://www.overbyte.be francois.piette@overbyte.be
http://www.rtfm.be/fpiette francois.piette@rtfm.be
francois.piette@pophost.eunet.be
Support: Use the mailing list twsocket@elists.org
Follow "support" link at http://www.overbyte.be for subscription.
Legal issues: Copyright (C) 1999-2002 by Franois PIETTE
Rue de Grady 24, 4053 Embourg, Belgium. Fax: +32-4-365.74.56
<francois.piette@overbyte.be><francois.piette@swing.be>
This software is provided 'as-is', without any express or
implied warranty. In no event will the author be held liable
for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it
and redistribute it freely, subject to the following
restrictions:
1. The origin of this software must not be misrepresented,
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment
in the product documentation would be appreciated but is
not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
4. You must register this software by sending a picture postcard
to the author. Use a nice stamp and mention your name, street
address, EMail address and any comment you like to say.
History:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
unit IcsSocketIntf;
{$BOOLEVAL OFF}
{$WRITEABLECONST ON}
{$ObjExportAll On}
interface
uses
SysUtils, Libc, IcsLogMsg;
const
{$IFDEF LINUX}
FIONREAD = $541B;
FIONBIO = $5421;
{$ENDIF}
function Ics_socket(af, Struct, protocol: Integer): TSocket;
function Ics_bind(s: TSocket; var addr: TSockAddr; namelen: Integer): Integer;
function Ics_connect(s: TSocket; var name: TSockAddr; namelen: Integer): Integer;
function Ics_closesocket(s: TSocket): Integer;
function Ics_listen(s: TSocket; backlog: Integer): Integer;
function Ics_accept(s: TSocket; var addr: TSockAddr; var addrlen: Integer): TSocket;
function Ics_select(nfds: Integer; readfds, writefds, exceptfds: PFdSet; timeout: Ptimeval): Integer;
function Ics_send(s: TSocket; var Buf; len, flags: Integer): Integer;
function Ics_sendto(s: TSocket; var Buf; len, flags: Integer;
var addrto: TSockAddr; tolen: Integer): Integer;
function Ics_htons(hostshort: u_short): u_short;
function Ics_inet_addr(cp: PChar): u_long;
function Ics_gethostbyname(name: PChar): PHostEnt;
function Ics_gethostname(name: PChar; Len: socklen_t): Integer;
function Ics_getservbyname(name, proto: PChar): PServEnt;
function Ics_ntohs(netshort: u_short): u_short;
function Ics_getprotobyname(name: PChar): PProtoEnt;
function Ics_getsockname(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer;
function Ics_setsockopt(s: TSocket; level, optname: Integer; optval: PChar; optlen: Integer): Integer;
function Ics_ioctlsocket(s: TSocket; cmd: u_long; var arg: u_long): Integer;
function Ics_recv(s: TSocket; var Buf; len, flags: Integer): Integer;
function Ics_recvfrom(s: TSocket; var Buf; len, flags: Integer; Addr: TSockAddr; AddrSize: Integer): Integer;
function Ics_inet_ntoa(inaddr: TInAddr): PChar;
function Ics_getpeername(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer;
function Ics_shutdown(s: TSocket; how: Integer): Integer;
{$IFNDEF WIN32}
function ioctlsocket(__fd: Tsocket; cmd: u_long; var arg: u_long): Integer; cdecl;
function getpeername(__fd: TSocket; var __addr: sockaddr; var __len: socklen_t): Integer; cdecl;
{$ENDIF}
implementation
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_select(nfds: integer; readfds, writefds, exceptfds: PFDSet; timeout: Ptimeval): Integer;
begin
LogMsg('Ics_select(' + IntToStr(nfds) + ')');
Result := select(nfds, readfds, writefds, exceptfds, timeout);
LogMsg('Ics_select = ' + IntToStr(Result));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_inet_ntoa(inaddr: TInAddr): PChar;
begin
Result := inet_ntoa(inaddr);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_getpeername(
s: TSocket; var name: TSockAddr; var namelen: Integer): Integer;
begin
Result := getpeername(s, name, socklen_t(namelen));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_accept(s: TSocket; var addr: TSockAddr; var addrlen: Integer): TSocket;
begin
Result := {$IFDEF WIN32} winsock.accept(s, @addr, @addrlen);
{$ELSE} libc.accept(s, @addr, @addrlen); {$ENDIF}
LogMsg('Ics_accept = ' + IntToStr(Result));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_listen(s: TSocket; backlog: Integer): Integer;
begin
Result := listen(s, backlog);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_ioctlsocket(s: TSocket; cmd: u_long; var arg: u_long): Integer;
begin
{$IFDEF WIN32}
Result := ioctlsocket(s, DWORD(cmd), arg);
{$ELSE}
Result := ioctlsocket(s, cmd, arg);
{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_sendto(
s: TSocket; var Buf; len, flags: Integer;
var addrto: TSockAddr; tolen: Integer): Integer;
begin
Result := {$IFDEF WIN32} winsock.sendto {$ELSE} libc.sendto {$ENDIF}
(s, Buf, len, flags, addrto, tolen);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_send(s: TSocket; var Buf; len, flags: Integer): Integer;
begin
Result := {$IFDEF WIN32} winsock.send {$ELSE} libc.send {$ENDIF}
(s, Buf, len, flags);
LogMsg('Ics_send(' + IntToStr(s) + ') = ' + IntToStr(Result));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_recv(s: TSocket; var Buf; len, flags: Integer): Integer;
begin
Result := {$IFDEF WIN32} winsock.recv {$ELSE} libc.recv {$ENDIF}
(s, Buf, len, flags);
LogMsg('Ics_recv(' + IntToStr(s) + ') = ' + IntToStr(Result));
end;
function Ics_recvfrom(s: TSocket; var Buf; len, flags: Integer; Addr: TSockAddr; AddrSize: Integer): Integer;
begin
Result := {$IFDEF WIN32} winsock.recvfrom {$ELSE} libc.recvfrom {$ENDIF}
(s, Buf, len, flags, @Addr, @AddrSize);
LogMsg('Ics_recvfrom(' + IntToStr(s) + ') = ' + IntToStr(Result));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_setsockopt(s: TSocket; level, optname: Integer; optval: PChar; optlen: Integer): Integer;
begin
Result := setsockopt(s, level, optname, optval, optlen);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_getsockname(s: TSocket; var name: TSockAddr; var namelen: Integer): Integer;
begin
Result := {$IFDEF WIN32} winsock.getsockname(s, name, namelen);
{$ELSE} libc.getsockname(s, name, socklen_t(namelen));{$ENDIF}
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_getprotobyname(name: PChar): PProtoEnt;
begin
Result := getprotobyname(name);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_ntohs(netshort: u_short): u_short;
begin
Result := ntohs(netshort);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_getservbyname(name, proto: PChar): PServEnt;
begin
Result := getservbyname(name, proto);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_gethostbyname(name: PChar): PHostEnt;
begin
Result := gethostbyname(name);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_gethostname(name: PChar; Len: socklen_t): Integer;
begin
Result := gethostname(name, Len);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_inet_addr(cp: PChar): u_long;
begin
Result := inet_addr(cp);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_htons(hostshort: u_short): u_short;
begin
Result := htons(hostshort);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_socket(af, Struct, protocol: Integer): TSocket;
begin
Result := socket(af, Struct, protocol);
LogMsg('Ics_socket = ' + IntToStr(Result));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_closesocket(s: TSocket): Integer;
begin
Result := {$IFDEF WIN32} winsock.closesocket {$ELSE} libc.__close {$ENDIF} (s);
LogMsg('Ics_closesocket(' + IntToStr(s) + ') = ' + IntToStr(Result));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_shutdown(s: TSocket; how: Integer): Integer;
begin
Result := {$IFDEF WIN32} winsock.shutdwon
{$ELSE} libc.shutdown {$ENDIF} (s, how);
LogMsg('Ics_closesocket(' + IntToStr(s) + ') = ' + IntToStr(Result));
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_bind(s: TSocket; var addr: TSockAddr; namelen: Integer): Integer;
begin
Result := bind(s, addr, namelen);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
function Ics_connect(s: TSocket; var name: TSockAddr; namelen: Integer): Integer;
begin
Result := connect(s, name, namelen);
end;
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
{$IFNDEF WIN32}
const
libcmodulename = 'libc.so.6';
function ioctlsocket; external libcmodulename name 'ioctl';
function getpeername; external libcmodulename name 'getpeername';
{$ENDIF}
{* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *}
end.