* More htons like functionality. IPV6 string support still missing

This commit is contained in:
marco 2005-02-13 19:59:57 +00:00
parent e26ab8447c
commit 7e9faf970a
2 changed files with 213 additions and 4 deletions

View File

@ -219,9 +219,193 @@ begin
{$endif}
end;
Type array4int = array[1..4] of byte;
function NetAddrToStr (Entry : in_addr) : AnsiString;
Var Dummy : Ansistring;
i,j : Longint;
begin
NetAddrToStr:='';
j:=entry.s_addr;
For I:=1 to 4 do
begin
Str(array4int(j)[i],Dummy);
NetAddrToStr:=NetAddrToStr+Dummy;
If I>1 Then
NetAddrToStr:=NetAddrToStr+'.';
end;
end;
function HostAddrToStr (Entry : in_addr) : AnsiString;
Var x: in_addr;
begin
x.s_addr:=htonl(entry.s_addr);
HostAddrToStr:=NetAddrToStr(x);
end;
function StrToHostAddr(IP : AnsiString) : in_addr ;
Var
Dummy : AnsiString;
I,j,k : Longint;
Temp : in_addr;
begin
strtohostaddr.s_addr:=0; //:=NoAddress;
For I:=1 to 4 do
begin
If I<4 Then
begin
J:=Pos('.',IP);
If J=0 then
exit;
Dummy:=Copy(IP,1,J-1);
Delete (IP,1,J);
end
else
Dummy:=IP;
Val (Dummy,k,J);
array4int(temp.s_addr)[i]:=k;
If J<>0 then Exit;
end;
strtohostaddr.s_addr:=ntohl(Temp.s_addr);
end;
function StrToNetAddr(IP : AnsiString) : in_addr;
begin
StrToNetAddr.s_addr:=htonl(StrToHostAddr(IP).s_addr);
end;
Function HostToNet (Host : in_addr):in_addr;
begin
HostToNet.s_addr:=htonl(host.s_addr);
end;
Function NetToHost (Net : in_addr) : in_addr;
begin
NetToHost.s_addr:=ntohl(net.s_addr);
end;
Function HostToNet (Host : Longint) : Longint;
begin
HostToNet:=htonl(host);
end;
Function NetToHost (Net : Longint) : Longint;
begin
NetToHost:=ntohl(net);
end;
Function ShortHostToNet (Host : Word) : Word;
begin
ShortHostToNet:=htons(host);
end;
Function ShortNetToHost (Net : Word) : Word;
begin
ShortNEtToHost:=ntohs(net);
end;
const digittab : shortstring = ('0123456789ABCDEF');
function lclinttohex (i:integer;digits:longint): ansistring;
var j,k : integer;
begin
SetLength(lclinttohex,4);
lclinttohex[4]:=digittab[1+(i and 15)];
lclinttohex[3]:=digittab[1+((i shr 4) and 15)];
lclinttohex[2]:=digittab[1+((i shr 8) and 15)];
lclinttohex[1]:=digittab[1+((i shr 12) and 15)];;
end;
function HostAddrToStr6 (Entry : TIn6_Addr) :ansiString;
var
i: byte;
zr1,zr2: set of byte;
zc1,zc2: byte;
have_skipped: boolean;
begin
zr1 := [];
zr2 := [];
zc1 := 0;
zc2 := 0;
for i := 0 to 7 do begin
if Entry.u6_addr16[i] = 0 then begin
include(zr2, i);
inc(zc2);
end else begin
if zc1 < zc2 then begin
zc1 := zc2;
zr1 := zr2;
zc2 := 0; zr2 := [];
end;
end;
end;
if zc1 < zc2 then begin
zc1 := zc2;
zr1 := zr2;
end;
SetLength(HostAddrToStr6, 8*5-1);
SetLength(HostAddrToStr6, 0);
have_skipped := false;
for i := 0 to 7 do begin
if not (i in zr1) then begin
if have_skipped then begin
if HostAddrToStr6 = ''
then HostAddrToStr6 := '::'
else HostAddrToStr6 := HostAddrToStr6 + ':';
have_skipped := false;
end;
// FIXME: is that shortnettohost really proper there? I wouldn't be too sure...
HostAddrToStr6 := HostAddrToStr6 +lclIntToHex(ShortNetToHost(Entry.u6_addr16[i]), 1) + ':';
end else begin
have_skipped := true;
end;
end;
if have_skipped then
if HostAddrToStr6 = ''
then HostAddrToStr6 := '::'
else HostAddrToStr6 := HostAddrToStr6 + ':';
if HostAddrToStr6 = '' then HostAddrToStr6 := '::';
if not (7 in zr1) then
SetLength(HostAddrToStr6, Length(HostAddrToStr6)-1);
end;
function StrToHostAddr6(IP : String) : TIn6_addr;
begin
end;
function NetAddrToStr6 (Entry : TIn6_Addr) : ansiString;
begin
netaddrtostr6 := HostAddrToStr6((Entry));
end;
function StrToNetAddr6(IP : ansiString) : TIn6_Addr;
begin
StrToNetAddr6 := StrToHostAddr6(IP);
end;
{
$Log$
Revision 1.14 2004-11-01 19:39:19 peter
Revision 1.15 2005-02-13 19:59:57 marco
* More htons like functionality. IPV6 string support still missing
Revision 1.14 2004/11/01 19:39:19 peter
* disable inline for 1.9.4
Revision 1.13 2004/11/01 17:29:47 marco

View File

@ -140,6 +140,7 @@ Type
TSockPairArray = Array[0..1] of Longint;
TSockArray = Array[1..2] of Longint; //legacy
Var
SocketError:cint;
@ -189,14 +190,38 @@ Function Accept(Sock:longint;var addr:TInetSockAddr;var SockIn,SockOut:text):Boo
Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:text):Boolean;
Function Connect(Sock:longint;const addr:TInetSockAddr;var SockIn,SockOut:file):Boolean;
{ Utility routines}
function htonl( host : longint):longint; {$ifdef HASINLINE} inline; {$endif}
Function NToHl (Net : Longint) : Longint; {$ifdef HASINLINE} inline; {$endif}
Function NToHl( Net : Longint) : Longint; {$ifdef HASINLINE} inline; {$endif}
function htons( host : word):word; {$ifdef HASINLINE} inline; {$endif}
Function NToHs (Net : word):word; {$ifdef HASINLINE} inline; {$endif}
Function NToHs( Net : word):word; {$ifdef HASINLINE} inline; {$endif}
function NetAddrToStr (Entry : in_addr) : AnsiString;
function HostAddrToStr(Entry : in_addr) : AnsiString;
function StrToHostAddr(IP : AnsiString) : in_addr ;
function StrToNetAddr (IP : AnsiString) : in_addr;
{ these for are for netdb legacy compat}
Function HostToNet (Host : in_addr) : in_addr;
Function NetToHost (Net : in_addr) : in_addr;
Function HostToNet (Host : Longint) : Longint;
Function NetToHost (Net : Longint) : Longint;
Function ShortHostToNet(Host : Word) : Word;
Function ShortNetToHost(Net : Word) : Word;
// ipv6
function HostAddrToStr6(Entry : Tin6_addr) : AnsiString;
function StrToHostAddr6(IP : String) : Tin6_addr; // not implemented?!?
function NetAddrToStr6 (Entry: Tin6_addr) : AnsiString;
function StrToNetAddr6 (IP : AnsiString) : TIn6_Addr;
{
$Log$
Revision 1.22 2005-02-12 17:34:56 marco
Revision 1.23 2005-02-13 19:59:57 marco
* More htons like functionality. IPV6 string support still missing
Revision 1.22 2005/02/12 17:34:56 marco
* some kylix stuf
Revision 1.21 2004/12/23 18:32:26 marco