[fpc-devel] StrToNetAddr6

Florian Klaempfl florian at freepascal.org
Wed Aug 24 22:35:04 CEST 2005


Johann Glaser wrote:

> Hi!
> 
> It seems that the StrToNetAddr6 function is not implemented yet
> (http://svn.freepascal.org/svn/fpc/trunk/rtl/inc/sockets.inc). I wrote
> an implementation (attached as file including some testcases). I hope
> you can use it for the Sockets unit.

Thx, added.

> 
> Bye
>   Hansi
> 
> 
> 
> ------------------------------------------------------------------------
> 
> Program IP6;
> 
> Function StrToNetAddr6(IP : String) : TIn6_addr;
> Var Part   : String;
>     IPv6   : TIn6_addr;
>     P,J    : Integer;
>     W      : Word;
>     Index  : Integer;
>     ZeroAt : Integer;
> Begin
>   FillChar(IPv6,SizeOf(IPv6),0);
>   { Every 16-bit block is converted at its own and stored into Result. When }
>   { the '::' zero-spacer is found, its location is stored. Afterwards the   }
>   { address is shifted and zero-filled.                                     }
>   Index := 0; ZeroAt := -1;
>   J := 0;
>   P := Pos(':',IP);
>   While (P > 0) and (Length(IP) > 0) and (Index < 8) do
>     Begin
>       Part := '$'+Copy(IP,1,P-1);
>       Delete(IP,1,P);
>       if Length(Part) > 1 then  { is there a digit after the '$'? }
>         Val(Part,W,J)
>       else W := 0;
>       IPv6.u6_addr16[Index] := HtoNS(W);
>       if J <> 0 then
>         Begin
>           FillChar(IPv6,SizeOf(IPv6),0);
>           Exit;
>         End;
>       if IP[1] = ':' then
>         Begin
>           ZeroAt := Index;
>           Delete(IP,1,1);
>         End;
>       Inc(Index);
>       P := Pos(':',IP); if P = 0 then P := Length(IP)+1;
>     End;
>   { address      a:b:c::f:g:h }
>   { Result now   a : b : c : f : g : h : 0 : 0, ZeroAt = 2, Index = 6 }
>   { Result after a : b : c : 0 : 0 : f : g : h }
>   if ZeroAt >= 0 then
>     Begin
>       Move(IPv6.u6_addr16[ZeroAt+1],IPv6.u6_addr16[(8-Index)+ZeroAt+1],2*(Index-ZeroAt-1));
>       FillChar(IPv6.u6_addr16[ZeroAt+1],2*(8-Index),0);
>     End;
> 
>   Result := IPv6;
> End;
> 
> Begin
>   StrToNetAddr6('1:2:3:4:5:6:7:8');
>   StrToNetAddr6('1:2:3::7:8');
>   StrToNetAddr6('1:2:3:4:5:6::');
>   StrToNetAddr6('::4:5:6:7:8');
>   StrToNetAddr6('::7:8');
>   StrToNetAddr6('1:2::');
>   StrToNetAddr6('::8');
>   StrToNetAddr6('::');
> End;
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-devel





More information about the fpc-devel mailing list