[fpc-pascal] ipv6 address convertion

Koenraad Lelong fpascal1 at de-brouwerij.be
Thu Aug 1 11:25:17 CEST 2013


Hi,
I need to manipulate an ipv6 address. I need to add some value to the 
netaddress and replace the hostaddress with another value. E.g. add 2 to 
the netaddress and replace the hostaddress by ::1
2001:db8:8500:11f8:6631:50ff:fe9a:640b/62 -> 2001:db8:8500:11fa::1/62

Is there a function in fpc that converts a 'short' ipv6 address to its 
full length ? I.e. convert :: to the required number of 0's to get a 
'full' address ? E.g.
2001:DB8:8500:11f8::1 -> 2001:0DB8:8500:11f8:0000:0000:0000:0001

And what are the NetAddrToStr6 and HostAddrToStr6 and their counterparts 
supposed to do ? Same for the ipv4 functions.

If I try following program (compiled with fpc 2.6.0) with this :
./project1 6 2001:DB8:8500:11f8::1/62
I get strange results :
netadr  : ::0033:3DBB:1000:0000:0034:5296:1000
hostadr : ::
netadr  : 2001:0DB8:8500:11F8::0001
hostadr : 2001:0DB8:8500:11F8::0001

Note the first netaddress.

program project1;

{$mode objfpc}{$H+}

uses
   {$IFDEF UNIX}{$IFDEF UseCThreads}
   cthreads,
   {$ENDIF}{$ENDIF}
   Classes, sockets;

var
   inparam : string;
   address : string;

begin

if not ((paramstr(1)='6') or (paramstr(1)='4')) then
  writeln('only 4 or 6 allowed as first parameter')
else
  if paramstr(1)='6' then
   begin
    inparam:=paramstr(2);
    writeln(inparam);
    address:=copy(inparam,1,pos('/',inparam)-1);
    writeln('netadr  : ',NetAddrToStr6(StrToNetAddr6(inparam)));
    writeln('hostadr : ',HostAddrToStr6(StrToHostAddr6(inparam)));
    writeln('netadr  : ',NetAddrToStr6(StrToNetAddr6(address)));
    writeln('hostadr : ',HostAddrToStr6(StrToHostAddr6(address)));
   end
else
  begin
    inparam:=paramstr(2);
    writeln(inparam);
    address:=copy(inparam,1,pos('/',inparam)-1);
    writeln('netadr  : ',NetAddrToStr(StrToNetAddr(inparam)));
    writeln('hostadr : ',HostAddrToStr(StrToHostAddr(inparam)));
    writeln('netadr  : ',NetAddrToStr(StrToNetAddr(address)));
    writeln('hostadr : ',HostAddrToStr(StrToHostAddr(address)));
  end;
end.


Thanks,

Koenraad Lelong



More information about the fpc-pascal mailing list