X-Git-Url: http://www.lcore.org/git/lcore.git/blobdiff_plain/6cb6b7ede2d178e03fa817bc28474c175f5a93b9..refs/heads/delphi2010:/binipstuff.pas

diff --git a/binipstuff.pas b/binipstuff.pas
old mode 100755
new mode 100644
index 0c23533..cfc7c1b
--- a/binipstuff.pas
+++ b/binipstuff.pas
@@ -8,11 +8,9 @@ interface
 
 {$include lcoreconfig.inc}
 
-{$ifndef win32}
-{$ifdef ipv6}
-uses sockets;
-{$endif}
-{$endif}
+uses
+  {$ifndef win32}{$ifdef ipv6}sockets,{$endif}{$endif}
+  pgtypes;
 
 {$ifdef fpc}
   {$mode delphi}
@@ -23,7 +21,7 @@ uses sockets;
 {$include uint32.inc}
 
 const
-  hexchars:array[0..15] of char='0123456789abcdef';
+  hexchars:array[0..15] of ansichar='0123456789abcdef';
   AF_INET=2;
   {$ifdef win32}
     AF_INET6=23;
@@ -86,12 +84,19 @@ type
 
 
 
-    {$ifdef ipv6}
+  {$ifdef ipv6}
     {$ifdef ver1_0}
       cuint16=word;
       cuint32=dword;
       sa_family_t=word;
 
+      TInetSockAddr6 = packed record
+        sin6_family: word;
+        sin6_port: word;
+        sin6_flowinfo: uint32;
+        sin6_addr: tin6_addr;
+        sin6_scope_id: uint32;
+      end;
     {$endif}
   {$endif}
   TinetSockAddrv = packed record
@@ -113,7 +118,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);
@@ -123,30 +128,34 @@ 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;
 procedure maskbits(var binip:tbinip;bits:integer);
 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);
 
 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
@@ -181,7 +190,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);}
@@ -213,11 +222,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
@@ -268,7 +277,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;
@@ -290,7 +299,7 @@ begin
   end;
 end;
 
-function ipbintostr(const binip:tbinip):string;
+function ipbintostr(const binip:tbinip):thostname;
 var
   a:integer;
 begin
@@ -313,7 +322,7 @@ end;
 
 {
 IPv6 address binary to/from string conversion routines
-written by beware (steendijk at xs4all dot nl)
+written by beware
 
 - implementation does not depend on other ipv6 code such as the tin6_addr type,
   the parameter can also be untyped.
@@ -326,9 +335,9 @@ written by beware (steendijk at xs4all dot nl)
 {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];
@@ -384,10 +393,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;
@@ -498,25 +507,45 @@ begin
   result := comparebinip(ip1,ip2);
 end;
 
-{converts a binary IP to v4 if it is a v6 IP in the v4 range}
-procedure converttov4(var ip:tbinip);
+function needconverttov4(const ip:tbinip):boolean;
 begin
   {$ifdef ipv6}
   if ip.family = AF_INET6 then begin
     if (ip.ip6.u6_addr32[0] = 0) and (ip.ip6.u6_addr32[1] = 0) and
     (ip.ip6.u6_addr16[4] = 0) and (ip.ip6.u6_addr16[5] = $ffff) then begin
-      ip.family := AF_INET;
-      ip.ip := ip.ip6.s6_addr32[3];
+      result := true;
+      exit;
     end;
   end;
   {$endif}
+
+  result := false;
 end;
 
-{------------------------------------------------------------------------------}
+{converts a binary IP to v4 if it is a v6 IP in the v4 range}
+procedure converttov4(var ip:tbinip);
+begin
+  {$ifdef ipv6}
+  if needconverttov4(ip) then begin
+    ip.family := AF_INET;
+    ip.ip := ip.ip6.s6_addr32[3];
+  end;
+  {$endif}
+end;
+
+{-----------biniplist stuff--------------------------------------------------}
+
+const
+  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);
 
+  biniplist_prefixlen=5;
+  
 function biniplist_new:tbiniplist;
 begin
-  result := '';
+  result := biniplist_prefix;
 end;
 
 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
@@ -530,7 +559,7 @@ end;
 
 function biniplist_getcount(const l:tbiniplist):integer;
 begin
-  result := length(l) div sizeof(tbinip);
+  result := (length(l)-biniplist_prefixlen) div sizeof(tbinip);
 end;
 
 function biniplist_get(const l:tbiniplist;index:integer):tbinip;
@@ -539,18 +568,18 @@ begin
     fillchar(result,sizeof(result),0);
     exit;
   end;
-  move(l[index*sizeof(tbinip)+1],result,sizeof(result));
+  move(l[index*sizeof(tbinip)+1+biniplist_prefixlen],result,sizeof(result));
 end;
 
 procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
 begin
   uniquestring(l);
-  move(ip,l[index*sizeof(tbinip)+1],sizeof(ip));
+  move(ip,l[index*sizeof(tbinip)+1+biniplist_prefixlen],sizeof(ip));
 end;
 
 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
 begin
-  setlength(l,sizeof(tbinip)*newlen);
+  setlength(l,(sizeof(tbinip)*newlen)+biniplist_prefixlen);
 end;
 
 procedure biniplist_free(var l:tbiniplist);
@@ -560,10 +589,10 @@ end;
 
 procedure biniplist_addlist;
 begin
-  l := l + l2;
+  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
@@ -575,4 +604,29 @@ begin
   result := result + ')';
 end;
 
+function isbiniplist(const l:tbiniplist):boolean;
+var
+  i : integer;
+begin
+  for i := 1 to biniplist_prefixlen do begin
+    if biniplist_prefix[i] <> l[i] then begin
+      result := false;
+      exit;
+    end;
+  end;
+  result := true;
+end;
+
+procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);
+var
+  a:integer;
+  biniptemp:tbinip;
+begin
+  for a := biniplist_getcount(l2)-1 downto 0 do begin
+    biniptemp := biniplist_get(l2,a);
+    if (biniptemp.family = family) then biniplist_add(l,biniptemp);
+  end;
+end;
+
+
 end.