\r
interface\r
\r
+{$include lcoreconfig.inc}\r
+\r
{$ifndef win32}\r
{$ifdef ipv6}\r
uses sockets;\r
{$endif}\r
{$endif}\r
\r
+\r
+\r
+ {$ifdef ipv6}\r
+ {$ifdef ver1_0}\r
+ cuint16=word;\r
+ cuint32=dword;\r
+ sa_family_t=word;\r
+\r
+ {$endif}\r
+ {$endif}\r
+ TinetSockAddrv = packed record\r
+ case integer of\r
+ 0: (InAddr:TInetSockAddr);\r
+ {$ifdef ipv6}\r
+ 1: (InAddr6:TInetSockAddr6);\r
+ {$endif}\r
+ end;\r
+ Pinetsockaddrv = ^Tinetsockaddrv;\r
+\r
+ type\r
+ tsockaddrin=TInetSockAddr;\r
+\r
+\r
+\r
+{\r
+bin IP list code, by beware\r
+while this is really just a string, on the interface side it must be treated\r
+as an opaque var which is passed as "var" when it needs to be modified}\r
+\r
+ tbiniplist=string;\r
+\r
+function biniplist_new:tbiniplist;\r
+procedure biniplist_add(var l:tbiniplist;ip:tbinip);\r
+function biniplist_getcount(const l:tbiniplist):integer;\r
+function biniplist_get(const l:tbiniplist;index:integer):tbinip;\r
+procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);\r
+procedure biniplist_setcount(var l:tbiniplist;newlen:integer);\r
+procedure biniplist_free(var l:tbiniplist);\r
+procedure biniplist_addlist(var l:tbiniplist;const l2:tbiniplist);\r
+function biniplist_tostr(const l:tbiniplist):string;\r
+function isbiniplist(const l:tbiniplist):boolean;\r
+\r
function htons(w:word):word;\r
function htonl(i:uint32):uint32;\r
\r
function ipstrtobin(const s:string;var binip:tbinip):boolean;\r
+function ipstrtobinf(const s:string):tbinip;\r
function ipbintostr(const binip:tbinip):string;\r
{$ifdef ipv6}\r
function ip6bintostr(const bin:tin6_addr):string;\r
{$endif}\r
\r
function comparebinip(const ip1,ip2:tbinip):boolean;\r
+procedure maskbits(var binip:tbinip;bits:integer);\r
+function comparebinipmask(ip1,ip2:tbinip;bits:integer):boolean;\r
+\r
+procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);\r
\r
{deprecated}\r
function longip(s:string):longint;\r
\r
+function needconverttov4(const ip:tbinip):boolean;\r
procedure converttov4(var ip:tbinip);\r
\r
+function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;\r
+function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;\r
+function inaddrsize(inaddr:tinetsockaddrv):integer;\r
+\r
implementation\r
\r
uses sysutils;\r
{$endif}\r
end;\r
\r
+\r
+function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;\r
+begin\r
+ result.family := inaddrv.inaddr.family;\r
+ if result.family = AF_INET then result.ip := inaddrv.inaddr.addr;\r
+ {$ifdef ipv6}\r
+ if result.family = AF_INET6 then result.ip6 := inaddrv.inaddr6.sin6_addr;\r
+ {$endif}\r
+end;\r
+\r
+function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;\r
+begin\r
+ result := 0;\r
+{ biniptemp := forwardlookup(addr,10);}\r
+ fillchar(inaddr,sizeof(inaddr),0);\r
+ //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp));\r
+ if addr.family = AF_INET then begin\r
+ inAddr.InAddr.family:=AF_INET;\r
+ inAddr.InAddr.port:=htons(strtointdef(port,0));\r
+ inAddr.InAddr.addr:=addr.ip;\r
+ result := sizeof(tinetsockaddr);\r
+ end else\r
+ {$ifdef ipv6}\r
+ if addr.family = AF_INET6 then begin\r
+ inAddr.InAddr6.sin6_family:=AF_INET6;\r
+ inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0));\r
+ inAddr.InAddr6.sin6_addr:=addr.ip6;\r
+ result := sizeof(tinetsockaddr6);\r
+ end;\r
+ {$endif}\r
+end;\r
+\r
+function inaddrsize(inaddr:tinetsockaddrv):integer;\r
+begin\r
+ {$ifdef ipv6}\r
+ if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else\r
+ {$endif}\r
+ result := sizeof(tinetsockaddr);\r
+end;\r
+\r
{internal}\r
{converts dotted v4 IP to longint. returns host endian order}\r
function longip(s:string):longint;\r
end;\r
\r
\r
+function ipstrtobinf;\r
+begin\r
+ ipstrtobin(s,result);\r
+end;\r
+\r
function ipstrtobin(const s:string;var binip:tbinip):boolean;\r
begin\r
binip.family := 0;\r
result := (ip1.family = ip2.family);\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
+procedure maskbits(var binip:tbinip;bits:integer);\r
+const\r
+ ipmax={$ifdef ipv6}15{$else}3{$endif};\r
+type tarr=array[0..ipmax] of byte;\r
+var\r
+ arr:^tarr;\r
+ a,b:integer;\r
+begin\r
+ arr := @binip.ip;\r
+ if bits = 0 then b := 0 else b := ((bits-1) div 8)+1;\r
+ for a := b to ipmax do begin\r
+ arr[a] := 0;\r
+ end;\r
+ if (bits and 7 <> 0) then begin\r
+ arr[bits shr 3] := arr[bits div 8] and not ($ff shr (bits and 7))\r
+ end;\r
+end;\r
+\r
+function comparebinipmask;\r
+begin\r
+ maskbits(ip1,bits);\r
+ maskbits(ip2,bits);\r
+ result := comparebinip(ip1,ip2);\r
+end;\r
+\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
+{-----------biniplist stuff--------------------------------------------------}\r
+\r
+const\r
+ biniplist_prefix='bipl'#0;\r
+ biniplist_prefixlen=length(biniplist_prefix);\r
+\r
+function biniplist_new:tbiniplist;\r
+begin\r
+ result := biniplist_prefix;\r
+end;\r
+\r
+procedure biniplist_add(var l:tbiniplist;ip:tbinip);\r
+var\r
+ a:integer;\r
+begin\r
+ a := biniplist_getcount(l);\r
+ biniplist_setcount(l,a+1);\r
+ biniplist_set(l,a,ip);\r
+end;\r
+\r
+function biniplist_getcount(const l:tbiniplist):integer;\r
+begin\r
+ result := (length(l)-biniplist_prefixlen) div sizeof(tbinip);\r
+end;\r
+\r
+function biniplist_get(const l:tbiniplist;index:integer):tbinip;\r
+begin\r
+ if (index >= biniplist_getcount(l)) then begin\r
+ fillchar(result,sizeof(result),0);\r
+ exit;\r
+ end;\r
+ move(l[index*sizeof(tbinip)+1+biniplist_prefixlen],result,sizeof(result));\r
+end;\r
+\r
+procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);\r
+begin\r
+ uniquestring(l);\r
+ move(ip,l[index*sizeof(tbinip)+1+biniplist_prefixlen],sizeof(ip));\r
+end;\r
+\r
+procedure biniplist_setcount(var l:tbiniplist;newlen:integer);\r
+begin\r
+ setlength(l,(sizeof(tbinip)*newlen)+biniplist_prefixlen);\r
+end;\r
+\r
+procedure biniplist_free(var l:tbiniplist);\r
+begin\r
+ l := '';\r
end;\r
\r
+procedure biniplist_addlist;\r
+begin\r
+ l := l + copy(l2,biniplist_prefixlen+1,maxlongint);\r
+end;\r
+\r
+function biniplist_tostr(const l:tbiniplist):string;\r
+var\r
+ a:integer;\r
+begin\r
+ result := '(';\r
+ for a := 0 to biniplist_getcount(l)-1 do begin\r
+ if result <> '(' then result := result + ', ';\r
+ result := result + ipbintostr(biniplist_get(l,a));\r
+ end;\r
+ result := result + ')';\r
+end;\r
+\r
+function isbiniplist(const l:tbiniplist):boolean;\r
+var\r
+ i : integer;\r
+begin\r
+ for i := 1 to biniplist_prefixlen do begin\r
+ if biniplist_prefix[i] <> l[i] then begin\r
+ result := false;\r
+ exit;\r
+ end;\r
+ end;\r
+ result := true;\r
+end;\r
+\r
+procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);\r
+var\r
+ a:integer;\r
+ biniptemp:tbinip;\r
+begin\r
+ for a := biniplist_getcount(l2)-1 downto 0 do begin\r
+ biniptemp := biniplist_get(l2,a);\r
+ if (biniptemp.family = family) then biniplist_add(l,biniptemp);\r
+ end;\r
+end;\r
+\r
+\r
end.\r