----------------------------------------------------------------------------- }\r
unit binipstuff;\r
\r
+{$ifdef fpc}\r
+{$mode delphi}\r
+{$endif}\r
+\r
interface\r
\r
+{$include lcoreconfig.inc}\r
+\r
+uses\r
{$ifndef win32}\r
-{$ifdef ipv6}\r
-uses sockets;\r
-{$endif}\r
+ sockets,\r
{$endif}\r
+ pgtypes;\r
\r
-{$ifdef fpc}\r
- {$mode delphi}\r
-{$endif}\r
{$ifdef cpu386}{$define i386}{$endif}\r
{$ifdef i386}{$define ENDIAN_LITTLE}{$endif}\r
\r
{$include uint32.inc}\r
\r
const\r
- hexchars:array[0..15] of char='0123456789abcdef';\r
- AF_INET=2;\r
+ hexchars:array[0..15] of ansichar='0123456789abcdef';\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
{$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
+ 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
+ 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=tbufferstring;\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):thostname;\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 ipbintostr(const binip:tbinip):string;\r
+function ipstrtobin(const s:thostname;var binip:tbinip):boolean;\r
+function ipstrtobinf(const s:thostname):tbinip;\r
+function ipbintostr(const binip:tbinip):thostname;\r
{$ifdef ipv6}\r
-function ip6bintostr(const bin:tin6_addr):string;\r
-function ip6strtobin(const s:string;var bin:tin6_addr):boolean;\r
+function ip6bintostr(const bin:tin6_addr):thostname;\r
+function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;\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
+function longip(s:thostname):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:ansistring;var inaddr:tinetsockaddrv):integer;\r
+function inaddrsize(inaddr:tinetsockaddrv):integer;\r
\r
implementation\r
\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:ansistring;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
+function longip(s:thostname):longint;\r
var\r
l:longint;\r
a,b:integer;\r
-function convertbyte(const s:string):integer;\r
+function convertbyte(const s:ansistring):integer;\r
begin\r
result := strtointdef(s,-1);\r
if result < 0 then begin\r
end;\r
\r
\r
-function ipstrtobin(const s:string;var binip:tbinip):boolean;\r
+function ipstrtobinf;\r
+begin\r
+ ipstrtobin(s,result);\r
+end;\r
+\r
+function ipstrtobin(const s:thostname;var binip:tbinip):boolean;\r
begin\r
binip.family := 0;\r
result := false;\r
end;\r
end;\r
\r
-function ipbintostr(const binip:tbinip):string;\r
+function ipbintostr(const binip:tbinip):thostname;\r
var\r
a:integer;\r
begin\r
\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
{fpc has hostaddrtostr6 and strtohostaddr6 but the later isnt implemented yet}\r
\r
\r
-function ip6bintostr(const bin:tin6_addr):string;\r
+function ip6bintostr(const bin:tin6_addr):thostname;\r
{base16 with lowercase output}\r
-function makehex(w:word):string;\r
+function makehex(w:word):ansistring;\r
begin\r
result := '';\r
if w >= 4096 then result := result + hexchars[w shr 12];\r
end;\r
end;\r
end;\r
+\r
+ {run length atleast 2 0 words}\r
+ if (runlength = 1) then begin\r
+ runlength := 0;\r
+ runbegin := 0;\r
+ end;\r
+\r
result := '';\r
for a := 0 to runbegin-1 do begin\r
if (a <> 0) then result := result + ':';\r
end;\r
end;\r
\r
-function ip6strtobin(const s:string;var bin:tin6_addr):boolean;\r
+function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;\r
var\r
a,b:integer;\r
- fields:array[0..7] of string;\r
+ fields:array[0..7] of ansistring;\r
fieldcount:integer;\r
emptyfield:integer;\r
wordcount:integer;\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
+\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: ansistring = 'bipl'#0;\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
+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):thostname;\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