1 { Copyright (C) 2005 Bas Steendijk and Peter Green
\r
2 For conditions of distribution and use, see copyright notice in zlib_license.txt
\r
3 which is included in the package
\r
4 ----------------------------------------------------------------------------- }
\r
9 {$include lcoreconfig.inc}
\r
20 {$ifdef cpu386}{$define i386}{$endif}
\r
21 {$ifdef i386}{$define ENDIAN_LITTLE}{$endif}
\r
23 {$include uint32.inc}
\r
26 hexchars:array[0..15] of char='0123456789abcdef';
\r
38 {$define want_Tin6_addr}
\r
41 {$define want_Tin6_addr}
\r
43 {$ifdef want_Tin6_addr}
\r
44 Tin6_addr = packed record
\r
46 0: (u6_addr8 : array[0..15] of byte);
\r
47 1: (u6_addr16 : array[0..7] of Word);
\r
48 2: (u6_addr32 : array[0..3] of uint32);
\r
49 3: (s6_addr8 : array[0..15] of shortint);
\r
50 4: (s6_addr : array[0..15] of shortint);
\r
51 5: (s6_addr16 : array[0..7] of smallint);
\r
52 6: (s6_addr32 : array[0..3] of LongInt);
\r
69 TInetSockAddr = packed Record
\r
73 pad :array [1..8] of byte;
\r
77 TInetSockAddr6 = packed record
\r
80 sin6_flowinfo: uint32;
\r
81 sin6_addr: tin6_addr;
\r
82 sin6_scope_id: uint32;
\r
97 TinetSockAddrv = packed record
\r
99 0: (InAddr:TInetSockAddr);
\r
101 1: (InAddr6:TInetSockAddr6);
\r
104 Pinetsockaddrv = ^Tinetsockaddrv;
\r
107 tsockaddrin=TInetSockAddr;
\r
112 bin IP list code, by beware
\r
113 while this is really just a string, on the interface side it must be treated
\r
114 as an opaque var which is passed as "var" when it needs to be modified}
\r
118 function biniplist_new:tbiniplist;
\r
119 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
\r
120 function biniplist_getcount(const l:tbiniplist):integer;
\r
121 function biniplist_get(const l:tbiniplist;index:integer):tbinip;
\r
122 procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
\r
123 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
\r
124 procedure biniplist_free(var l:tbiniplist);
\r
125 procedure biniplist_addlist(var l:tbiniplist;const l2:tbiniplist);
\r
126 function biniplist_tostr(const l:tbiniplist):string;
\r
128 function htons(w:word):word;
\r
129 function htonl(i:uint32):uint32;
\r
131 function ipstrtobin(const s:string;var binip:tbinip):boolean;
\r
132 function ipstrtobinf(const s:string):tbinip;
\r
133 function ipbintostr(const binip:tbinip):string;
\r
135 function ip6bintostr(const bin:tin6_addr):string;
\r
136 function ip6strtobin(const s:string;var bin:tin6_addr):boolean;
\r
139 function comparebinip(const ip1,ip2:tbinip):boolean;
\r
140 procedure maskbits(var binip:tbinip;bits:integer);
\r
141 function comparebinipmask(ip1,ip2:tbinip;bits:integer):boolean;
\r
144 function longip(s:string):longint;
\r
146 procedure converttov4(var ip:tbinip);
\r
148 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;
\r
149 function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;
\r
150 function inaddrsize(inaddr:tinetsockaddrv):integer;
\r
156 function htons(w:word):word;
\r
158 {$ifdef ENDIAN_LITTLE}
\r
159 result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);
\r
165 function htonl(i:uint32):uint32;
\r
167 {$ifdef ENDIAN_LITTLE}
\r
168 result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);
\r
175 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;
\r
177 result.family := inaddrv.inaddr.family;
\r
178 if result.family = AF_INET then result.ip := inaddrv.inaddr.addr;
\r
180 if result.family = AF_INET6 then result.ip6 := inaddrv.inaddr6.sin6_addr;
\r
184 function makeinaddrv(addr:tbinip;port:string;var inaddr:tinetsockaddrv):integer;
\r
187 { biniptemp := forwardlookup(addr,10);}
\r
188 fillchar(inaddr,sizeof(inaddr),0);
\r
189 //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp));
\r
190 if addr.family = AF_INET then begin
\r
191 inAddr.InAddr.family:=AF_INET;
\r
192 inAddr.InAddr.port:=htons(strtointdef(port,0));
\r
193 inAddr.InAddr.addr:=addr.ip;
\r
194 result := sizeof(tinetsockaddr);
\r
197 if addr.family = AF_INET6 then begin
\r
198 inAddr.InAddr6.sin6_family:=AF_INET6;
\r
199 inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0));
\r
200 inAddr.InAddr6.sin6_addr:=addr.ip6;
\r
201 result := sizeof(tinetsockaddr6);
\r
206 function inaddrsize(inaddr:tinetsockaddrv):integer;
\r
209 if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else
\r
211 result := sizeof(tinetsockaddr);
\r
215 {converts dotted v4 IP to longint. returns host endian order}
\r
216 function longip(s:string):longint;
\r
220 function convertbyte(const s:string):integer;
\r
222 result := strtointdef(s,-1);
\r
223 if result < 0 then begin
\r
227 if result > 255 then begin
\r
232 if (result <> 0) and (s[1] = '0') then begin
\r
237 if not (s[1] in ['0'..'9']) then begin
\r
246 if a = 0 then exit;
\r
247 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
249 s := copy(s,a+1,256);
\r
251 if a = 0 then exit;
\r
252 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
253 l := l or b shl 16;
\r
254 s := copy(s,a+1,256);
\r
256 if a = 0 then exit;
\r
257 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
259 s := copy(s,a+1,256);
\r
260 b := convertbyte(copy(s,1,256));if (b < 0) then exit;
\r
266 function ipstrtobinf;
\r
268 ipstrtobin(s,result);
\r
271 function ipstrtobin(const s:string;var binip:tbinip):boolean;
\r
276 if pos(':',s) <> 0 then begin
\r
277 {try ipv6. use builtin routine}
\r
278 result := ip6strtobin(s,binip.ip6);
\r
279 if result then binip.family := AF_INET6;
\r
285 binip.ip := htonl(longip(s));
\r
286 if (binip.ip <> 0) or (s = '0.0.0.0') then begin
\r
288 binip.family := AF_INET;
\r
293 function ipbintostr(const binip:tbinip):string;
\r
299 if binip.family = AF_INET6 then begin
\r
300 result := ip6bintostr(binip.ip6);
\r
303 if binip.family = AF_INET then begin
\r
304 a := htonl(binip.ip);
\r
305 result := inttostr(a shr 24)+'.'+inttostr((a shr 16) and $ff)+'.'+inttostr((a shr 8) and $ff)+'.'+inttostr(a and $ff);
\r
310 {------------------------------------------------------------------------------}
\r
315 IPv6 address binary to/from string conversion routines
\r
316 written by beware (steendijk at xs4all dot nl)
\r
318 - implementation does not depend on other ipv6 code such as the tin6_addr type,
\r
319 the parameter can also be untyped.
\r
320 - it is host endian neutral - binary format is aways network order
\r
321 - it supports compression of zeroes
\r
322 - it supports ::ffff:192.168.12.34 style addresses
\r
323 - they are made to do the Right Thing, more efficient implementations are possible
\r
326 {fpc has hostaddrtostr6 and strtohostaddr6 but the later isnt implemented yet}
\r
329 function ip6bintostr(const bin:tin6_addr):string;
\r
330 {base16 with lowercase output}
\r
331 function makehex(w:word):string;
\r
334 if w >= 4096 then result := result + hexchars[w shr 12];
\r
335 if w >= 256 then result := result + hexchars[w shr 8 and $f];
\r
336 if w >= 16 then result := result + hexchars[w shr 4 and $f];
\r
337 result := result + hexchars[w and $f];
\r
341 a,b,c,addrlen:integer;
\r
342 runbegin,runlength:integer;
\r
343 bytes:array[0..15] of byte absolute bin;
\r
344 words:array[0..7] of word;
\r
345 dwords:array[0..3] of integer absolute words;
\r
347 for a := 0 to 7 do begin
\r
348 words[a] := bytes[a shl 1] shl 8 or bytes[a shl 1 or 1];
\r
350 if (dwords[0] = 0) and (dwords[1] = 0) and (words[4] = 0) and (words[5] = $ffff) then begin
\r
351 {::ffff:/96 exception: v4 IP}
\r
356 {find longest run of zeroes}
\r
359 for a := 0 to addrlen-1 do begin
\r
360 if words[a] = 0 then begin
\r
362 for b := a to addrlen-1 do if words[b] = 0 then begin
\r
365 if (c > runlength) then begin
\r
372 for a := 0 to runbegin-1 do begin
\r
373 if (a <> 0) then result := result + ':';
\r
374 result := result + makehex(words[a]);
\r
376 if runlength > 0 then result := result + '::';
\r
377 c := runbegin+runlength;
\r
378 for a := c to addrlen-1 do begin
\r
379 if (a > c) then result := result + ':';
\r
380 result := result + makehex(words[a]);
\r
382 if addrlen = 6 then begin
\r
383 result := result + ':'+inttostr(bytes[12])+'.'+inttostr(bytes[13])+'.'+inttostr(bytes[14])+'.'+inttostr(bytes[15]);
\r
387 function ip6strtobin(const s:string;var bin:tin6_addr):boolean;
\r
390 fields:array[0..7] of string;
\r
391 fieldcount:integer;
\r
392 emptyfield:integer;
\r
394 words:array[0..7] of word;
\r
395 bytes:array[0..15] of byte absolute bin;
\r
398 for a := 0 to 7 do fields[a] := '';
\r
400 for a := 1 to length(s) do begin
\r
401 if s[a] = ':' then inc(fieldcount) else fields[fieldcount] := fields[fieldcount] + s[a];
\r
402 if fieldcount > 7 then exit;
\r
404 if fieldcount < 2 then exit;
\r
406 {find the empty field (compressed zeroes), not counting the first and last there may be at most 1}
\r
408 for a := 1 to fieldcount-1 do begin
\r
409 if fields[a] = '' then begin
\r
410 if emptyfield = -1 then emptyfield := a else exit;
\r
414 {check if last field is a valid v4 IP}
\r
415 a := longip(fields[fieldcount]);
\r
416 if (a <> 0) or (fields[fieldcount] = '0.0.0.0') then wordcount := 6 else wordcount := 8;
\r
417 {0:1:2:3:4:5:6.6.6.6
\r
419 fillchar(words,sizeof(words),0);
\r
420 if wordcount = 6 then begin
\r
421 if fieldcount > 6 then exit;
\r
422 words[6] := a shr 16;
\r
423 words[7] := a and $ffff;
\r
425 if emptyfield = -1 then begin
\r
426 {no run length: must be an exact number of fields}
\r
427 if wordcount = 6 then begin
\r
428 if fieldcount <> 6 then exit;
\r
430 end else if wordcount = 8 then begin
\r
431 if fieldcount <> 7 then exit;
\r
435 for a := 0 to emptyfield do begin
\r
436 if fields[a] = '' then b := 0 else b := strtointdef('$'+fields[a],-1);
\r
437 if (b < 0) or (b > $ffff) then exit;
\r
440 if wordcount = 6 then dec(fieldcount);
\r
441 for a := wordcount-1 downto wordcount-(fieldcount-emptyfield) do begin
\r
442 b := a+fieldcount-wordcount+1;
\r
443 if fields[b] = '' then b := 0 else b := strtointdef('$'+fields[b],-1);
\r
444 if (b < 0) or (b > $ffff) then exit;
\r
447 for a := 0 to 7 do begin
\r
448 bytes[a shl 1] := words[a] shr 8;
\r
449 bytes[a shl 1 or 1] := words[a] and $ff;
\r
455 function comparebinip(const ip1,ip2:tbinip):boolean;
\r
457 if (ip1.ip <> ip2.ip) then begin
\r
463 if ip1.family = AF_INET6 then begin
\r
464 if (ip1.ip6.s6_addr32[1] <> ip2.ip6.s6_addr32[1])
\r
465 or (ip1.ip6.s6_addr32[2] <> ip2.ip6.s6_addr32[2])
\r
466 or (ip1.ip6.s6_addr32[3] <> ip2.ip6.s6_addr32[3]) then begin
\r
473 result := (ip1.family = ip2.family);
\r
476 procedure maskbits(var binip:tbinip;bits:integer);
\r
478 ipmax={$ifdef ipv6}15{$else}3{$endif};
\r
479 type tarr=array[0..ipmax] of byte;
\r
485 if bits = 0 then b := 0 else b := ((bits-1) div 8)+1;
\r
486 for a := b to ipmax do begin
\r
489 if (bits and 7 <> 0) then begin
\r
490 arr[bits shr 3] := arr[bits div 8] and not ($ff shr (bits and 7))
\r
494 function comparebinipmask;
\r
496 maskbits(ip1,bits);
\r
497 maskbits(ip2,bits);
\r
498 result := comparebinip(ip1,ip2);
\r
501 {converts a binary IP to v4 if it is a v6 IP in the v4 range}
\r
502 procedure converttov4(var ip:tbinip);
\r
505 if ip.family = AF_INET6 then begin
\r
506 if (ip.ip6.u6_addr32[0] = 0) and (ip.ip6.u6_addr32[1] = 0) and
\r
507 (ip.ip6.u6_addr16[4] = 0) and (ip.ip6.u6_addr16[5] = $ffff) then begin
\r
508 ip.family := AF_INET;
\r
509 ip.ip := ip.ip6.s6_addr32[3];
\r
515 {------------------------------------------------------------------------------}
\r
517 function biniplist_new:tbiniplist;
\r
522 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
\r
526 a := biniplist_getcount(l);
\r
527 biniplist_setcount(l,a+1);
\r
528 biniplist_set(l,a,ip);
\r
531 function biniplist_getcount(const l:tbiniplist):integer;
\r
533 result := length(l) div sizeof(tbinip);
\r
536 function biniplist_get(const l:tbiniplist;index:integer):tbinip;
\r
538 if (index >= biniplist_getcount(l)) then begin
\r
539 fillchar(result,sizeof(result),0);
\r
542 move(l[index*sizeof(tbinip)+1],result,sizeof(result));
\r
545 procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
\r
548 move(ip,l[index*sizeof(tbinip)+1],sizeof(ip));
\r
551 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
\r
553 setlength(l,sizeof(tbinip)*newlen);
\r
556 procedure biniplist_free(var l:tbiniplist);
\r
561 procedure biniplist_addlist;
\r
566 function biniplist_tostr(const l:tbiniplist):string;
\r
571 for a := 0 to biniplist_getcount(l)-1 do begin
\r
572 if result <> '(' then result := result + ', ';
\r
573 result := result + ipbintostr(biniplist_get(l,a));
\r
575 result := result + ')';
\r