+++ /dev/null
-{ Copyright (C) 2005 Bas Steendijk and Peter Green\r
- For conditions of distribution and use, see copyright notice in zlib_license.txt\r
- which is included in the package\r
- ----------------------------------------------------------------------------- }\r
-unit binipstuff;\r
-\r
-interface\r
-\r
-{$ifndef win32}\r
-{$ifdef ipv6}\r
-uses sockets;\r
-{$endif}\r
-{$endif}\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
- {$ifdef win32}\r
- AF_INET6=23;\r
- {$else}\r
- AF_INET6=10;\r
- {$endif}\r
-\r
-type\r
- {$ifdef ipv6}\r
- \r
- {$ifdef win32}\r
- {$define want_Tin6_addr}\r
- {$endif}\r
- {$ifdef ver1_0}\r
- {$define want_Tin6_addr}\r
- {$endif}\r
- {$ifdef want_Tin6_addr}\r
- Tin6_addr = packed record\r
- case byte of\r
- 0: (u6_addr8 : array[0..15] of byte);\r
- 1: (u6_addr16 : array[0..7] of Word);\r
- 2: (u6_addr32 : array[0..3] of uint32);\r
- 3: (s6_addr8 : array[0..15] of shortint);\r
- 4: (s6_addr : array[0..15] of shortint);\r
- 5: (s6_addr16 : array[0..7] of smallint);\r
- 6: (s6_addr32 : array[0..3] of LongInt);\r
- end;\r
- {$endif}\r
- {$endif}\r
-\r
- tbinip=record\r
- family:integer;\r
- {$ifdef ipv6}\r
- case integer of\r
- 0: (ip:longint);\r
- 1: (ip6:tin6_addr);\r
- {$else}\r
- ip:longint;\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
-\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
-\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
-{$ifdef ipv6}\r
-function ip6bintostr(const bin:tin6_addr):string;\r
-function ip6strtobin(const s:string;var bin:tin6_addr):boolean;\r
-{$endif}\r
-\r
-function comparebinip(const ip1,ip2:tbinip):boolean;\r
-\r
-{deprecated}\r
-function longip(s:string):longint;\r
-\r
-procedure converttov4(var ip:tbinip);\r
-\r
-implementation\r
-\r
-uses sysutils;\r
-\r
-function htons(w:word):word;\r
-begin\r
- {$ifdef ENDIAN_LITTLE}\r
- result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);\r
- {$else}\r
- result := w;\r
- {$endif}\r
-end;\r
-\r
-function htonl(i:uint32):uint32;\r
-begin\r
- {$ifdef ENDIAN_LITTLE}\r
- result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);\r
- {$else}\r
- result := i;\r
- {$endif}\r
-end;\r
-\r
-{internal}\r
-{converts dotted v4 IP to longint. returns host endian order}\r
-function longip(s:string):longint;\r
-var\r
- l:longint;\r
- a,b:integer;\r
-function convertbyte(const s:string):integer;\r
-begin\r
- result := strtointdef(s,-1);\r
- if result < 0 then begin\r
- result := -1;\r
- exit;\r
- end;\r
- if result > 255 then begin\r
- result := -1;\r
- exit;\r
- end;\r
- {01 exception}\r
- if (result <> 0) and (s[1] = '0') then begin\r
- result := -1;\r
- exit;\r
- end;\r
- {+1 exception}\r
- if not (s[1] in ['0'..'9']) then begin\r
- result := -1;\r
- exit\r
- end;\r
-end;\r
-\r
-begin\r
- result := 0;\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := b shl 24;\r
- s := copy(s,a+1,256);\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := l or b shl 16;\r
- s := copy(s,a+1,256);\r
- a := pos('.',s);\r
- if a = 0 then exit;\r
- b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;\r
- l := l or b shl 8;\r
- s := copy(s,a+1,256);\r
- b := convertbyte(copy(s,1,256));if (b < 0) then exit;\r
- l := l or b;\r
- result := l;\r
-end;\r
-\r
-\r
-function ipstrtobin(const s:string;var binip:tbinip):boolean;\r
-begin\r
- binip.family := 0;\r
- result := false;\r
- {$ifdef ipv6}\r
- if pos(':',s) <> 0 then begin\r
- {try ipv6. use builtin routine}\r
- result := ip6strtobin(s,binip.ip6);\r
- if result then binip.family := AF_INET6;\r
- exit;\r
- end;\r
- {$endif}\r
-\r
- {try v4}\r
- binip.ip := htonl(longip(s));\r
- if (binip.ip <> 0) or (s = '0.0.0.0') then begin\r
- result := true;\r
- binip.family := AF_INET;\r
- exit;\r
- end;\r
-end;\r
-\r
-function ipbintostr(const binip:tbinip):string;\r
-var\r
- a:integer;\r
-begin\r
- result := '';\r
- {$ifdef ipv6}\r
- if binip.family = AF_INET6 then begin\r
- result := ip6bintostr(binip.ip6);\r
- end else\r
- {$endif}\r
- if binip.family = AF_INET then begin\r
- a := htonl(binip.ip);\r
- result := inttostr(a shr 24)+'.'+inttostr((a shr 16) and $ff)+'.'+inttostr((a shr 8) and $ff)+'.'+inttostr(a and $ff);\r
- end;\r
-end;\r
-\r
-\r
-{------------------------------------------------------------------------------}\r
-\r
-{$ifdef ipv6}\r
-\r
-{\r
-IPv6 address binary to/from string conversion routines\r
-written by beware (steendijk at xs4all dot nl)\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 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
-}\r
-\r
-{fpc has hostaddrtostr6 and strtohostaddr6 but the later isnt implemented yet}\r
-\r
-\r
-function ip6bintostr(const bin:tin6_addr):string;\r
-{base16 with lowercase output}\r
-function makehex(w:word):string;\r
-begin\r
- result := '';\r
- if w >= 4096 then result := result + hexchars[w shr 12];\r
- if w >= 256 then result := result + hexchars[w shr 8 and $f];\r
- if w >= 16 then result := result + hexchars[w shr 4 and $f];\r
- result := result + hexchars[w and $f];\r
-end;\r
-\r
-var\r
- a,b,c,addrlen:integer;\r
- runbegin,runlength:integer;\r
- bytes:array[0..15] of byte absolute bin;\r
- words:array[0..7] of word;\r
- dwords:array[0..3] of integer absolute words;\r
-begin\r
- for a := 0 to 7 do begin\r
- words[a] := bytes[a shl 1] shl 8 or bytes[a shl 1 or 1];\r
- end;\r
- if (dwords[0] = 0) and (dwords[1] = 0) and (words[4] = 0) and (words[5] = $ffff) then begin\r
- {::ffff:/96 exception: v4 IP}\r
- addrlen := 6;\r
- end else begin\r
- addrlen := 8;\r
- end;\r
- {find longest run of zeroes}\r
- runbegin := 0;\r
- runlength := 0;\r
- for a := 0 to addrlen-1 do begin\r
- if words[a] = 0 then begin\r
- c := 0;\r
- for b := a to addrlen-1 do if words[b] = 0 then begin\r
- inc(c);\r
- end else break;\r
- if (c > runlength) then begin\r
- runlength := c;\r
- runbegin := a;\r
- end;\r
- end;\r
- end;\r
- result := '';\r
- for a := 0 to runbegin-1 do begin\r
- if (a <> 0) then result := result + ':';\r
- result := result + makehex(words[a]);\r
- end;\r
- if runlength > 0 then result := result + '::';\r
- c := runbegin+runlength;\r
- for a := c to addrlen-1 do begin\r
- if (a > c) then result := result + ':';\r
- result := result + makehex(words[a]);\r
- end;\r
- if addrlen = 6 then begin\r
- result := result + ':'+inttostr(bytes[12])+'.'+inttostr(bytes[13])+'.'+inttostr(bytes[14])+'.'+inttostr(bytes[15]);\r
- end;\r
-end;\r
-\r
-function ip6strtobin(const s:string;var bin:tin6_addr):boolean;\r
-var\r
- a,b:integer;\r
- fields:array[0..7] of string;\r
- fieldcount:integer;\r
- emptyfield:integer;\r
- wordcount:integer;\r
- words:array[0..7] of word;\r
- bytes:array[0..15] of byte absolute bin;\r
-begin\r
- result := false;\r
- for a := 0 to 7 do fields[a] := '';\r
- fieldcount := 0;\r
- for a := 1 to length(s) do begin\r
- if s[a] = ':' then inc(fieldcount) else fields[fieldcount] := fields[fieldcount] + s[a];\r
- if fieldcount > 7 then exit;\r
- end;\r
- if fieldcount < 2 then exit;\r
-\r
- {find the empty field (compressed zeroes), not counting the first and last there may be at most 1}\r
- emptyfield := -1;\r
- for a := 1 to fieldcount-1 do begin\r
- if fields[a] = '' then begin\r
- if emptyfield = -1 then emptyfield := a else exit;\r
- end;\r
- end;\r
-\r
- {check if last field is a valid v4 IP}\r
- a := longip(fields[fieldcount]);\r
- if (a <> 0) or (fields[fieldcount] = '0.0.0.0') then wordcount := 6 else wordcount := 8;\r
- {0:1:2:3:4:5:6.6.6.6\r
- 0:1:2:3:4:5:6:7}\r
- fillchar(words,sizeof(words),0);\r
- if wordcount = 6 then begin\r
- if fieldcount > 6 then exit;\r
- words[6] := a shr 16;\r
- words[7] := a and $ffff;\r
- end;\r
- if emptyfield = -1 then begin\r
- {no run length: must be an exact number of fields}\r
- if wordcount = 6 then begin\r
- if fieldcount <> 6 then exit;\r
- emptyfield := 5;\r
- end else if wordcount = 8 then begin\r
- if fieldcount <> 7 then exit;\r
- emptyfield := 7;\r
- end else exit;\r
- end;\r
- for a := 0 to emptyfield do begin\r
- if fields[a] = '' then b := 0 else b := strtointdef('$'+fields[a],-1);\r
- if (b < 0) or (b > $ffff) then exit;\r
- words[a] := b;\r
- end;\r
- if wordcount = 6 then dec(fieldcount);\r
- for a := wordcount-1 downto wordcount-(fieldcount-emptyfield) do begin\r
- b := a+fieldcount-wordcount+1;\r
- if fields[b] = '' then b := 0 else b := strtointdef('$'+fields[b],-1);\r
- if (b < 0) or (b > $ffff) then exit;\r
- words[a] := b;\r
- end;\r
- for a := 0 to 7 do begin\r
- bytes[a shl 1] := words[a] shr 8;\r
- bytes[a shl 1 or 1] := words[a] and $ff;\r
- end;\r
- result := true;\r
-end;\r
-{$endif}\r
-\r
-function comparebinip(const ip1,ip2:tbinip):boolean;\r
-begin\r
- if (ip1.ip <> ip2.ip) then begin\r
- result := false;\r
- exit;\r
- end;\r
-\r
- {$ifdef ipv6}\r
- if ip1.family = AF_INET6 then begin\r
- if (ip1.ip6.s6_addr32[1] <> ip2.ip6.s6_addr32[1])\r
- or (ip1.ip6.s6_addr32[2] <> ip2.ip6.s6_addr32[2])\r
- or (ip1.ip6.s6_addr32[3] <> ip2.ip6.s6_addr32[3]) then begin\r
- result := false;\r
- exit;\r
- end;\r
- end;\r
- {$endif}\r
-\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
-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
- end;\r
- end;\r
- {$endif}\r
-end;\r
-\r
-end.\r