----------------------------------------------------------------------------- }\r
unit binipstuff;\r
\r
+{$ifdef fpc}\r
+{$mode delphi}\r
+{$endif}\r
+\r
interface\r
\r
{$include lcoreconfig.inc}\r
\r
-{$ifndef win32}\r
-{$ifdef ipv6}\r
-uses sockets;\r
-{$endif}\r
+uses\r
+{$ifndef mswindows}\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 pgtypes.inc}\r
\r
{$include uint32.inc}\r
\r
const\r
- hexchars:array[0..15] of char='0123456789abcdef';\r
- AF_INET=2;\r
- {$ifdef win32}\r
+ hexchars:array[0..15] of ansichar='0123456789abcdef';\r
+ {$ifdef mswindows}\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
{$ifdef ipv6}\r
\r
- {$ifdef win32}\r
+ {$ifdef mswindows}\r
{$define want_Tin6_addr}\r
{$endif}\r
{$ifdef ver1_0}\r
{$endif}\r
end;\r
\r
- {$ifdef win32}\r
- TInetSockAddr = packed Record\r
- family:Word;\r
- port :Word;\r
- addr :uint32;\r
- pad :array [1..8] of byte;\r
- end;\r
- {$ifdef ipv6}\r
+ {zipplet 20170204: FPC 3.0.0 changed the definition of TInetSockAddr:\r
+ - http://www.freepascal.org/docs-html/rtl/sockets/tinetsockaddr.html\r
+ - http://www.freepascal.org/docs-html/rtl/sockets/sockaddr_in.html\r
+ Due to this, TInetSockAddr -> TLInetSockAddr4 / TLInetSockAddr6\r
+ Using our own types no matter what OS or compiler version will prevent future problems.\r
+ Adding "4" to non IPv6 record names improves code clarity }\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
+ {$ifndef mswindows}\r
+ //zipplet 20170204: Do we still need to support ver1_0? Perhaps a cleanup is in order.\r
+ //For now keep supporting it for compatibility.\r
+ {$ifdef ver1_0}\r
+ cuint16 = word;\r
+ cuint32 = dword;\r
+ sa_family_t = word;\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
+ TLInetSockAddr4 = packed Record\r
+ family:Word;\r
+ port :Word;\r
+ addr :uint32;\r
+ pad :array [0..7] of byte; //zipplet 20170204 - originally this was 1..8 for some reason\r
+ end;\r
+ \r
+ {$ifdef ipv6}\r
+ TLInetSockAddr6 = 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
+\r
+ //zipplet 20170204: I did not rename the unioned record. We might want to rename this to TLinetSockAddrv\r
TinetSockAddrv = packed record\r
case integer of\r
- 0: (InAddr:TInetSockAddr);\r
+ 0: (InAddr:TLInetSockAddr4);\r
{$ifdef ipv6}\r
- 1: (InAddr6:TInetSockAddr6);\r
+ 1: (InAddr6:TLInetSockAddr6);\r
{$endif}\r
end;\r
Pinetsockaddrv = ^Tinetsockaddrv;\r
\r
type\r
- tsockaddrin=TInetSockAddr;\r
-\r
-\r
+ tsockaddrin=TLInetSockAddr4;\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
+ tbiniplist=tbufferstring;\r
\r
function biniplist_new:tbiniplist;\r
procedure biniplist_add(var l:tbiniplist;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 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 ipstrtobinf(const s:string):tbinip;\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:string;var inaddr:tinetsockaddrv):integer;\r
+function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;\r
function inaddrsize(inaddr:tinetsockaddrv):integer;\r
\r
+function getbinipbitlength(const ip:tbinip):integer;\r
+function getipstrbitlength(const ip:thostname):integer;\r
+function getfamilybitlength(family:integer):integer;\r
+\r
implementation\r
\r
uses sysutils;\r
{$endif}\r
end;\r
\r
-function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;\r
+function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;\r
begin\r
result := 0;\r
{ biniptemp := forwardlookup(addr,10);}\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
+ result := sizeof(tlinetsockaddr4);\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
+ result := sizeof(tlinetsockaddr6);\r
end;\r
{$endif}\r
end;\r
function inaddrsize(inaddr:tinetsockaddrv):integer;\r
begin\r
{$ifdef ipv6}\r
- if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else\r
+ if inaddr.inaddr.family = AF_INET6 then result := sizeof(tlinetsockaddr6) else\r
{$endif}\r
- result := sizeof(tinetsockaddr);\r
+ result := sizeof(tlinetsockaddr4);\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
ipstrtobin(s,result);\r
end;\r
\r
-function ipstrtobin(const s:string;var binip:tbinip):boolean;\r
+function ipstrtobin(const s:thostname;var binip:tbinip):boolean;\r
begin\r
binip.family := 0;\r
result := false;\r
{$endif}\r
\r
{try v4}\r
- binip.ip := htonl(longip(s));\r
+ // zipplet: htonl() expects a uint32 but longip() spits out longint.\r
+ // Because longip() is deprecated, we do not fix it but typecast.\r
+ //binip.ip := htonl(longip(s));\r
+ binip.ip := htonl(uint32(longip(s)));\r
if (binip.ip <> 0) or (s = '0.0.0.0') then begin\r
result := true;\r
binip.family := AF_INET;\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
-- it is host endian neutral - binary format is aways network order\r
+- it is host endian neutral - binary format is always network order\r
- it supports compression of zeroes\r
- it supports ::ffff:192.168.12.34 style addresses\r
- they are made to do the Right Thing, more efficient implementations are possible\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 at least 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 := 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
-{------------------------------------------------------------------------------}\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 := '';\r
+ result := biniplist_prefix;\r
end;\r
\r
procedure biniplist_add(var l:tbiniplist;ip:tbinip);\r
\r
function biniplist_getcount(const l:tbiniplist):integer;\r
begin\r
- result := length(l) div sizeof(tbinip);\r
+ result := (length(l)-biniplist_prefixlen) div sizeof(tbinip);\r
end;\r
\r
function biniplist_get(const l:tbiniplist;index:integer):tbinip;\r
fillchar(result,sizeof(result),0);\r
exit;\r
end;\r
- move(l[index*sizeof(tbinip)+1],result,sizeof(result));\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],sizeof(ip));\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);\r
+ setlength(l,(sizeof(tbinip)*newlen)+biniplist_prefixlen);\r
end;\r
\r
procedure biniplist_free(var l:tbiniplist);\r
\r
procedure biniplist_addlist;\r
begin\r
- l := l + l2;\r
+ l := l + copy(l2,biniplist_prefixlen+1,maxlongint);\r
end;\r
\r
-function biniplist_tostr(const l:tbiniplist):string;\r
+function biniplist_tostr(const l:tbiniplist):thostname;\r
var\r
a:integer;\r
begin\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
+function getfamilybitlength(family:integer):integer;\r
+begin\r
+ {$ifdef ipv6}\r
+ if family = AF_INET6 then result := 128 else\r
+ {$endif}\r
+ if family = AF_INET then result := 32\r
+ else result := 0;\r
+end;\r
+\r
+function getbinipbitlength(const ip:tbinip):integer;\r
+begin\r
+ result := getfamilybitlength(ip.family);\r
+end;\r
+\r
+function getipstrbitlength(const ip:thostname):integer;\r
+var\r
+ biniptemp:tbinip;\r
+begin\r
+ ipstrtobin(ip,biniptemp);\r
+ result := getbinipbitlength(biniptemp);\r
+end;\r
+\r
end.\r