X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/055fa6bf18e0733d1bf2f97075d6bb33c76e72b5..9d2bca1f897628eaed87c733670f82ddb96b8c01:/binipstuff.pas?ds=sidebyside

diff --git a/binipstuff.pas b/binipstuff.pas
index 675b03e..1f5fed4 100644
--- a/binipstuff.pas
+++ b/binipstuff.pas
@@ -8,11 +8,11 @@ interface
 
 {$include lcoreconfig.inc}
 
+uses
 {$ifndef win32}
-{$ifdef ipv6}
-uses sockets;
-{$endif}
+  sockets,
 {$endif}
+  pgtypes;
 
 {$ifdef fpc}
   {$mode delphi}
@@ -23,12 +23,16 @@ uses sockets;
 {$include uint32.inc}
 
 const
-  hexchars:array[0..15] of char='0123456789abcdef';
-  AF_INET=2;
+  hexchars:array[0..15] of ansichar='0123456789abcdef';
   {$ifdef win32}
+    AF_INET=2;
     AF_INET6=23;
   {$else}
-    AF_INET6=10;
+    //redeclare these constants so units that use us can use them
+    //without using sockets directly
+    AF_INET=AF_INET;
+    AF_INET6=AF_INET6;
+    //AF_INET6=10;
   {$endif}
 
 type
@@ -120,7 +124,7 @@ bin IP list code, by beware
 while this is really just a string, on the interface side it must be treated
 as an opaque var which is passed as "var" when it needs to be modified}
 
-  tbiniplist=string;
+  tbiniplist=tbufferstring;
 
 function biniplist_new:tbiniplist;
 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
@@ -130,18 +134,18 @@ procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
 procedure biniplist_free(var l:tbiniplist);
 procedure biniplist_addlist(var l:tbiniplist;const l2:tbiniplist);
-function biniplist_tostr(const l:tbiniplist):string;
+function biniplist_tostr(const l:tbiniplist):thostname;
 function isbiniplist(const l:tbiniplist):boolean;
 
 function htons(w:word):word;
 function htonl(i:uint32):uint32;
 
-function ipstrtobin(const s:string;var binip:tbinip):boolean;
-function ipstrtobinf(const s:string):tbinip;
-function ipbintostr(const binip:tbinip):string;
+function ipstrtobin(const s:thostname;var binip:tbinip):boolean;
+function ipstrtobinf(const s:thostname):tbinip;
+function ipbintostr(const binip:tbinip):thostname;
 {$ifdef ipv6}
-function ip6bintostr(const bin:tin6_addr):string;
-function ip6strtobin(const s:string;var bin:tin6_addr):boolean;
+function ip6bintostr(const bin:tin6_addr):thostname;
+function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;
 {$endif}
 
 function comparebinip(const ip1,ip2:tbinip):boolean;
@@ -151,13 +155,14 @@ function comparebinipmask(ip1,ip2:tbinip;bits:integer):boolean;
 procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);
 
 {deprecated}
-function longip(s:string):longint;
+function longip(s:thostname):longint;
 
 function needconverttov4(const ip:tbinip):boolean;
 procedure converttov4(var ip:tbinip);
+procedure converttov6(var ip:tbinip);
 
 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;
-function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;
+function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;
 function inaddrsize(inaddr:tinetsockaddrv):integer;
 
 implementation
@@ -192,7 +197,7 @@ begin
   {$endif}
 end;
 
-function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;
+function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;
 begin
   result := 0;
 {  biniptemp := forwardlookup(addr,10);}
@@ -224,11 +229,11 @@ end;
 
 {internal}
 {converts dotted v4 IP to longint. returns host endian order}
-function longip(s:string):longint;
+function longip(s:thostname):longint;
 var
   l:longint;
   a,b:integer;
-function convertbyte(const s:string):integer;
+function convertbyte(const s:ansistring):integer;
 begin
   result := strtointdef(s,-1);
   if result < 0 then begin
@@ -279,7 +284,7 @@ begin
   ipstrtobin(s,result);
 end;
 
-function ipstrtobin(const s:string;var binip:tbinip):boolean;
+function ipstrtobin(const s:thostname;var binip:tbinip):boolean;
 begin
   binip.family := 0;
   result := false;
@@ -301,7 +306,7 @@ begin
   end;
 end;
 
-function ipbintostr(const binip:tbinip):string;
+function ipbintostr(const binip:tbinip):thostname;
 var
   a:integer;
 begin
@@ -337,9 +342,9 @@ written by beware
 {fpc has hostaddrtostr6 and strtohostaddr6 but the later isnt implemented yet}
 
 
-function ip6bintostr(const bin:tin6_addr):string;
+function ip6bintostr(const bin:tin6_addr):thostname;
 {base16 with lowercase output}
-function makehex(w:word):string;
+function makehex(w:word):ansistring;
 begin
   result := '';
   if w >= 4096 then result := result + hexchars[w shr 12];
@@ -379,6 +384,13 @@ begin
       end;
     end;
   end;
+
+  {run length atleast 2 0 words}
+  if (runlength = 1) then begin
+    runlength := 0;
+    runbegin := 0;
+  end;
+
   result := '';
   for a := 0 to runbegin-1 do begin
     if (a <> 0) then result := result + ':';
@@ -395,10 +407,10 @@ begin
   end;
 end;
 
-function ip6strtobin(const s:string;var bin:tin6_addr):boolean;
+function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;
 var
   a,b:integer;
-  fields:array[0..7] of string;
+  fields:array[0..7] of ansistring;
   fieldcount:integer;
   emptyfield:integer;
   wordcount:integer;
@@ -535,10 +547,27 @@ begin
   {$endif}
 end;
 
+
+{converts a binary IP to v6 if it is a v4 IP}
+procedure converttov6(var ip:tbinip);
+begin
+  {$ifdef ipv6}
+    if ip.family = AF_INET then begin
+      ip.family := AF_INET6;
+      ip.ip6.s6_addr32[3] := ip.ip; 
+      ip.ip6.u6_addr32[0] := 0; 
+      ip.ip6.u6_addr32[1] := 0;
+      ip.ip6.u6_addr16[4] := 0;
+      ip.ip6.u6_addr16[5] := $ffff;
+    end;
+  {$endif}
+end;
+
+
 {-----------biniplist stuff--------------------------------------------------}
 
 const
-  biniplist_prefix='bipl'#0;
+  biniplist_prefix: ansistring = 'bipl'#0;
   //fpc 1.0.x doesn't seem to like use of length function in a constant 
   //definition
   //biniplist_prefixlen=length(biniplist_prefix);
@@ -594,7 +623,7 @@ begin
   l := l + copy(l2,biniplist_prefixlen+1,maxlongint);
 end;
 
-function biniplist_tostr(const l:tbiniplist):string;
+function biniplist_tostr(const l:tbiniplist):thostname;
 var
   a:integer;
 begin