add some missing ifdefs
[lcore.git] / binipstuff.pas
old mode 100755 (executable)
new mode 100644 (file)
index a1433fc..8f28a27
@@ -9,10 +9,8 @@ interface
 {$include lcoreconfig.inc}\r
 \r
 {$ifndef win32}\r
-{$ifdef ipv6}\r
 uses sockets;\r
 {$endif}\r
-{$endif}\r
 \r
 {$ifdef fpc}\r
   {$mode delphi}\r
@@ -24,11 +22,15 @@ uses sockets;
 \r
 const\r
   hexchars:array[0..15] of char='0123456789abcdef';\r
-  AF_INET=2;\r
   {$ifdef win32}\r
+    AF_INET=2;\r
     AF_INET6=23;\r
   {$else}\r
-    AF_INET6=10;\r
+    //redeclare these constants so units that use us can use them\r
+    //without using sockets directly\r
+    AF_INET=AF_INET;\r
+    AF_INET6=AF_INET6;\r
+    //AF_INET6=10;\r
   {$endif}\r
 \r
 type\r
@@ -86,12 +88,19 @@ type
 \r
 \r
 \r
-    {$ifdef ipv6}\r
+  {$ifdef ipv6}\r
     {$ifdef ver1_0}\r
       cuint16=word;\r
       cuint32=dword;\r
       sa_family_t=word;\r
 \r
+      TInetSockAddr6 = packed record\r
+        sin6_family: word;\r
+        sin6_port: word;\r
+        sin6_flowinfo: uint32;\r
+        sin6_addr: tin6_addr;\r
+        sin6_scope_id: uint32;\r
+      end;\r
     {$endif}\r
   {$endif}\r
   TinetSockAddrv = packed record\r
@@ -146,7 +155,9 @@ procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);
 {deprecated}\r
 function longip(s:string):longint;\r
 \r
+function needconverttov4(const ip:tbinip):boolean;\r
 procedure converttov4(var ip:tbinip);\r
+procedure converttov6(var ip:tbinip);\r
 \r
 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;\r
 function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;\r
@@ -316,7 +327,7 @@ end;
 \r
 {\r
 IPv6 address binary to/from string conversion routines\r
-written by beware (steendijk at xs4all dot nl)\r
+written by beware\r
 \r
 - implementation does not depend on other ipv6 code such as the tin6_addr type,\r
   the parameter can also be untyped.\r
@@ -501,26 +512,59 @@ begin
   result := comparebinip(ip1,ip2);\r
 end;\r
 \r
-{converts a binary IP to v4 if it is a v6 IP in the v4 range}\r
-procedure converttov4(var ip:tbinip);\r
+function needconverttov4(const ip:tbinip):boolean;\r
 begin\r
   {$ifdef ipv6}\r
   if ip.family = AF_INET6 then begin\r
     if (ip.ip6.u6_addr32[0] = 0) and (ip.ip6.u6_addr32[1] = 0) and\r
     (ip.ip6.u6_addr16[4] = 0) and (ip.ip6.u6_addr16[5] = $ffff) then begin\r
-      ip.family := AF_INET;\r
-      ip.ip := ip.ip6.s6_addr32[3];\r
+      result := true;\r
+      exit;\r
     end;\r
   end;\r
   {$endif}\r
+\r
+  result := false;\r
+end;\r
+\r
+{converts a binary IP to v4 if it is a v6 IP in the v4 range}\r
+procedure converttov4(var ip:tbinip);\r
+begin\r
+  {$ifdef ipv6}\r
+  if needconverttov4(ip) then begin\r
+    ip.family := AF_INET;\r
+    ip.ip := ip.ip6.s6_addr32[3];\r
+  end;\r
+  {$endif}\r
 end;\r
 \r
+\r
+{converts a binary IP to v6 if it is a v4 IP}\r
+procedure converttov6(var ip:tbinip);\r
+begin\r
+  {$ifdef ipv6}\r
+    if ip.family = AF_INET then begin\r
+      ip.family := AF_INET6;\r
+      ip.ip6.s6_addr32[3] := ip.ip; \r
+      ip.ip6.u6_addr32[0] := 0; \r
+      ip.ip6.u6_addr32[1] := 0;\r
+      ip.ip6.u6_addr16[4] := 0;\r
+      ip.ip6.u6_addr16[5] := $ffff;\r
+    end;\r
+  {$endif}\r
+end;\r
+\r
+\r
 {-----------biniplist stuff--------------------------------------------------}\r
 \r
 const\r
   biniplist_prefix='bipl'#0;\r
-  biniplist_prefixlen=length(biniplist_prefix);\r
+  //fpc 1.0.x doesn't seem to like use of length function in a constant \r
+  //definition\r
+  //biniplist_prefixlen=length(biniplist_prefix);\r
 \r
+  biniplist_prefixlen=5;\r
+  \r
 function biniplist_new:tbiniplist;\r
 begin\r
   result := biniplist_prefix;\r