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
12 {$ifndef win32}{$ifdef ipv6}sockets,{$endif}{$endif}
\r
18 {$ifdef cpu386}{$define i386}{$endif}
\r
19 {$ifdef i386}{$define ENDIAN_LITTLE}{$endif}
\r
21 {$include uint32.inc}
\r
24 hexchars:array[0..15] of ansichar='0123456789abcdef';
\r
29 //redeclare these constants so units that use us can use them
\r
30 //without using sockets directly
\r
40 {$define want_Tin6_addr}
\r
43 {$define want_Tin6_addr}
\r
45 {$ifdef want_Tin6_addr}
\r
46 Tin6_addr = packed record
\r
48 0: (u6_addr8 : array[0..15] of byte);
\r
49 1: (u6_addr16 : array[0..7] of Word);
\r
50 2: (u6_addr32 : array[0..3] of uint32);
\r
51 3: (s6_addr8 : array[0..15] of shortint);
\r
52 4: (s6_addr : array[0..15] of shortint);
\r
53 5: (s6_addr16 : array[0..7] of smallint);
\r
54 6: (s6_addr32 : array[0..3] of LongInt);
\r
71 TInetSockAddr = packed Record
\r
75 pad :array [1..8] of byte;
\r
79 TInetSockAddr6 = packed record
\r
82 sin6_flowinfo: uint32;
\r
83 sin6_addr: tin6_addr;
\r
84 sin6_scope_id: uint32;
\r
97 TInetSockAddr6 = packed record
\r
100 sin6_flowinfo: uint32;
\r
101 sin6_addr: tin6_addr;
\r
102 sin6_scope_id: uint32;
\r
106 TinetSockAddrv = packed record
\r
108 0: (InAddr:TInetSockAddr);
\r
110 1: (InAddr6:TInetSockAddr6);
\r
113 Pinetsockaddrv = ^Tinetsockaddrv;
\r
116 tsockaddrin=TInetSockAddr;
\r
121 bin IP list code, by beware
\r
122 while this is really just a string, on the interface side it must be treated
\r
123 as an opaque var which is passed as "var" when it needs to be modified}
\r
125 tbiniplist=tbufferstring;
\r
127 function biniplist_new:tbiniplist;
\r
128 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
\r
129 function biniplist_getcount(const l:tbiniplist):integer;
\r
130 function biniplist_get(const l:tbiniplist;index:integer):tbinip;
\r
131 procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
\r
132 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
\r
133 procedure biniplist_free(var l:tbiniplist);
\r
134 procedure biniplist_addlist(var l:tbiniplist;const l2:tbiniplist);
\r
135 function biniplist_tostr(const l:tbiniplist):thostname;
\r
136 function isbiniplist(const l:tbiniplist):boolean;
\r
138 function htons(w:word):word;
\r
139 function htonl(i:uint32):uint32;
\r
141 function ipstrtobin(const s:thostname;var binip:tbinip):boolean;
\r
142 function ipstrtobinf(const s:thostname):tbinip;
\r
143 function ipbintostr(const binip:tbinip):thostname;
\r
145 function ip6bintostr(const bin:tin6_addr):thostname;
\r
146 function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;
\r
149 function comparebinip(const ip1,ip2:tbinip):boolean;
\r
150 procedure maskbits(var binip:tbinip;bits:integer);
\r
151 function comparebinipmask(ip1,ip2:tbinip;bits:integer):boolean;
\r
153 procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);
\r
156 function longip(s:thostname):longint;
\r
158 function needconverttov4(const ip:tbinip):boolean;
\r
159 procedure converttov4(var ip:tbinip);
\r
160 procedure converttov6(var ip:tbinip);
\r
162 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;
\r
163 function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;
\r
164 function inaddrsize(inaddr:tinetsockaddrv):integer;
\r
170 function htons(w:word):word;
\r
172 {$ifdef ENDIAN_LITTLE}
\r
173 result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);
\r
179 function htonl(i:uint32):uint32;
\r
181 {$ifdef ENDIAN_LITTLE}
\r
182 result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);
\r
189 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;
\r
191 result.family := inaddrv.inaddr.family;
\r
192 if result.family = AF_INET then result.ip := inaddrv.inaddr.addr;
\r
194 if result.family = AF_INET6 then result.ip6 := inaddrv.inaddr6.sin6_addr;
\r
198 function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;
\r
201 { biniptemp := forwardlookup(addr,10);}
\r
202 fillchar(inaddr,sizeof(inaddr),0);
\r
203 //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp));
\r
204 if addr.family = AF_INET then begin
\r
205 inAddr.InAddr.family:=AF_INET;
\r
206 inAddr.InAddr.port:=htons(strtointdef(port,0));
\r
207 inAddr.InAddr.addr:=addr.ip;
\r
208 result := sizeof(tinetsockaddr);
\r
211 if addr.family = AF_INET6 then begin
\r
212 inAddr.InAddr6.sin6_family:=AF_INET6;
\r
213 inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0));
\r
214 inAddr.InAddr6.sin6_addr:=addr.ip6;
\r
215 result := sizeof(tinetsockaddr6);
\r
220 function inaddrsize(inaddr:tinetsockaddrv):integer;
\r
223 if inaddr.inaddr.family = AF_INET6 then result := sizeof(tinetsockaddr6) else
\r
225 result := sizeof(tinetsockaddr);
\r
229 {converts dotted v4 IP to longint. returns host endian order}
\r
230 function longip(s:thostname):longint;
\r
234 function convertbyte(const s:ansistring):integer;
\r
236 result := strtointdef(s,-1);
\r
237 if result < 0 then begin
\r
241 if result > 255 then begin
\r
246 if (result <> 0) and (s[1] = '0') then begin
\r
251 if not (s[1] in ['0'..'9']) then begin
\r
260 if a = 0 then exit;
\r
261 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
263 s := copy(s,a+1,256);
\r
265 if a = 0 then exit;
\r
266 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
267 l := l or b shl 16;
\r
268 s := copy(s,a+1,256);
\r
270 if a = 0 then exit;
\r
271 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
273 s := copy(s,a+1,256);
\r
274 b := convertbyte(copy(s,1,256));if (b < 0) then exit;
\r
280 function ipstrtobinf;
\r
282 ipstrtobin(s,result);
\r
285 function ipstrtobin(const s:thostname;var binip:tbinip):boolean;
\r
290 if pos(':',s) <> 0 then begin
\r
291 {try ipv6. use builtin routine}
\r
292 result := ip6strtobin(s,binip.ip6);
\r
293 if result then binip.family := AF_INET6;
\r
299 binip.ip := htonl(longip(s));
\r
300 if (binip.ip <> 0) or (s = '0.0.0.0') then begin
\r
302 binip.family := AF_INET;
\r
307 function ipbintostr(const binip:tbinip):thostname;
\r
313 if binip.family = AF_INET6 then begin
\r
314 result := ip6bintostr(binip.ip6);
\r
317 if binip.family = AF_INET then begin
\r
318 a := htonl(binip.ip);
\r
319 result := inttostr(a shr 24)+'.'+inttostr((a shr 16) and $ff)+'.'+inttostr((a shr 8) and $ff)+'.'+inttostr(a and $ff);
\r
324 {------------------------------------------------------------------------------}
\r
329 IPv6 address binary to/from string conversion routines
\r
332 - implementation does not depend on other ipv6 code such as the tin6_addr type,
\r
333 the parameter can also be untyped.
\r
334 - it is host endian neutral - binary format is aways network order
\r
335 - it supports compression of zeroes
\r
336 - it supports ::ffff:192.168.12.34 style addresses
\r
337 - they are made to do the Right Thing, more efficient implementations are possible
\r
340 {fpc has hostaddrtostr6 and strtohostaddr6 but the later isnt implemented yet}
\r
343 function ip6bintostr(const bin:tin6_addr):thostname;
\r
344 {base16 with lowercase output}
\r
345 function makehex(w:word):ansistring;
\r
348 if w >= 4096 then result := result + hexchars[w shr 12];
\r
349 if w >= 256 then result := result + hexchars[w shr 8 and $f];
\r
350 if w >= 16 then result := result + hexchars[w shr 4 and $f];
\r
351 result := result + hexchars[w and $f];
\r
355 a,b,c,addrlen:integer;
\r
356 runbegin,runlength:integer;
\r
357 bytes:array[0..15] of byte absolute bin;
\r
358 words:array[0..7] of word;
\r
359 dwords:array[0..3] of integer absolute words;
\r
361 for a := 0 to 7 do begin
\r
362 words[a] := bytes[a shl 1] shl 8 or bytes[a shl 1 or 1];
\r
364 if (dwords[0] = 0) and (dwords[1] = 0) and (words[4] = 0) and (words[5] = $ffff) then begin
\r
365 {::ffff:/96 exception: v4 IP}
\r
370 {find longest run of zeroes}
\r
373 for a := 0 to addrlen-1 do begin
\r
374 if words[a] = 0 then begin
\r
376 for b := a to addrlen-1 do if words[b] = 0 then begin
\r
379 if (c > runlength) then begin
\r
386 for a := 0 to runbegin-1 do begin
\r
387 if (a <> 0) then result := result + ':';
\r
388 result := result + makehex(words[a]);
\r
390 if runlength > 0 then result := result + '::';
\r
391 c := runbegin+runlength;
\r
392 for a := c to addrlen-1 do begin
\r
393 if (a > c) then result := result + ':';
\r
394 result := result + makehex(words[a]);
\r
396 if addrlen = 6 then begin
\r
397 result := result + ':'+inttostr(bytes[12])+'.'+inttostr(bytes[13])+'.'+inttostr(bytes[14])+'.'+inttostr(bytes[15]);
\r
401 function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;
\r
404 fields:array[0..7] of ansistring;
\r
405 fieldcount:integer;
\r
406 emptyfield:integer;
\r
408 words:array[0..7] of word;
\r
409 bytes:array[0..15] of byte absolute bin;
\r
412 for a := 0 to 7 do fields[a] := '';
\r
414 for a := 1 to length(s) do begin
\r
415 if s[a] = ':' then inc(fieldcount) else fields[fieldcount] := fields[fieldcount] + s[a];
\r
416 if fieldcount > 7 then exit;
\r
418 if fieldcount < 2 then exit;
\r
420 {find the empty field (compressed zeroes), not counting the first and last there may be at most 1}
\r
422 for a := 1 to fieldcount-1 do begin
\r
423 if fields[a] = '' then begin
\r
424 if emptyfield = -1 then emptyfield := a else exit;
\r
428 {check if last field is a valid v4 IP}
\r
429 a := longip(fields[fieldcount]);
\r
430 if (a <> 0) or (fields[fieldcount] = '0.0.0.0') then wordcount := 6 else wordcount := 8;
\r
431 {0:1:2:3:4:5:6.6.6.6
\r
433 fillchar(words,sizeof(words),0);
\r
434 if wordcount = 6 then begin
\r
435 if fieldcount > 6 then exit;
\r
436 words[6] := a shr 16;
\r
437 words[7] := a and $ffff;
\r
439 if emptyfield = -1 then begin
\r
440 {no run length: must be an exact number of fields}
\r
441 if wordcount = 6 then begin
\r
442 if fieldcount <> 6 then exit;
\r
444 end else if wordcount = 8 then begin
\r
445 if fieldcount <> 7 then exit;
\r
449 for a := 0 to emptyfield do begin
\r
450 if fields[a] = '' then b := 0 else b := strtointdef('$'+fields[a],-1);
\r
451 if (b < 0) or (b > $ffff) then exit;
\r
454 if wordcount = 6 then dec(fieldcount);
\r
455 for a := wordcount-1 downto wordcount-(fieldcount-emptyfield) do begin
\r
456 b := a+fieldcount-wordcount+1;
\r
457 if fields[b] = '' then b := 0 else b := strtointdef('$'+fields[b],-1);
\r
458 if (b < 0) or (b > $ffff) then exit;
\r
461 for a := 0 to 7 do begin
\r
462 bytes[a shl 1] := words[a] shr 8;
\r
463 bytes[a shl 1 or 1] := words[a] and $ff;
\r
469 function comparebinip(const ip1,ip2:tbinip):boolean;
\r
471 if (ip1.ip <> ip2.ip) then begin
\r
477 if ip1.family = AF_INET6 then begin
\r
478 if (ip1.ip6.s6_addr32[1] <> ip2.ip6.s6_addr32[1])
\r
479 or (ip1.ip6.s6_addr32[2] <> ip2.ip6.s6_addr32[2])
\r
480 or (ip1.ip6.s6_addr32[3] <> ip2.ip6.s6_addr32[3]) then begin
\r
487 result := (ip1.family = ip2.family);
\r
490 procedure maskbits(var binip:tbinip;bits:integer);
\r
492 ipmax={$ifdef ipv6}15{$else}3{$endif};
\r
493 type tarr=array[0..ipmax] of byte;
\r
499 if bits = 0 then b := 0 else b := ((bits-1) div 8)+1;
\r
500 for a := b to ipmax do begin
\r
503 if (bits and 7 <> 0) then begin
\r
504 arr[bits shr 3] := arr[bits div 8] and not ($ff shr (bits and 7))
\r
508 function comparebinipmask;
\r
510 maskbits(ip1,bits);
\r
511 maskbits(ip2,bits);
\r
512 result := comparebinip(ip1,ip2);
\r
515 function needconverttov4(const ip:tbinip):boolean;
\r
518 if ip.family = AF_INET6 then begin
\r
519 if (ip.ip6.u6_addr32[0] = 0) and (ip.ip6.u6_addr32[1] = 0) and
\r
520 (ip.ip6.u6_addr16[4] = 0) and (ip.ip6.u6_addr16[5] = $ffff) then begin
\r
530 {converts a binary IP to v4 if it is a v6 IP in the v4 range}
\r
531 procedure converttov4(var ip:tbinip);
\r
534 if needconverttov4(ip) then begin
\r
535 ip.family := AF_INET;
\r
536 ip.ip := ip.ip6.s6_addr32[3];
\r
542 {converts a binary IP to v6 if it is a v4 IP}
\r
543 procedure converttov6(var ip:tbinip);
\r
546 if ip.family = AF_INET then begin
\r
547 ip.family := AF_INET6;
\r
548 ip.ip6.s6_addr32[3] := ip.ip;
\r
549 ip.ip6.u6_addr32[0] := 0;
\r
550 ip.ip6.u6_addr32[1] := 0;
\r
551 ip.ip6.u6_addr16[4] := 0;
\r
552 ip.ip6.u6_addr16[5] := $ffff;
\r
558 {-----------biniplist stuff--------------------------------------------------}
\r
561 biniplist_prefix: ansistring = 'bipl'#0;
\r
562 //fpc 1.0.x doesn't seem to like use of length function in a constant
\r
564 //biniplist_prefixlen=length(biniplist_prefix);
\r
566 biniplist_prefixlen=5;
\r
568 function biniplist_new:tbiniplist;
\r
570 result := biniplist_prefix;
\r
573 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
\r
577 a := biniplist_getcount(l);
\r
578 biniplist_setcount(l,a+1);
\r
579 biniplist_set(l,a,ip);
\r
582 function biniplist_getcount(const l:tbiniplist):integer;
\r
584 result := (length(l)-biniplist_prefixlen) div sizeof(tbinip);
\r
587 function biniplist_get(const l:tbiniplist;index:integer):tbinip;
\r
589 if (index >= biniplist_getcount(l)) then begin
\r
590 fillchar(result,sizeof(result),0);
\r
593 move(l[index*sizeof(tbinip)+1+biniplist_prefixlen],result,sizeof(result));
\r
596 procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
\r
599 move(ip,l[index*sizeof(tbinip)+1+biniplist_prefixlen],sizeof(ip));
\r
602 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
\r
604 setlength(l,(sizeof(tbinip)*newlen)+biniplist_prefixlen);
\r
607 procedure biniplist_free(var l:tbiniplist);
\r
612 procedure biniplist_addlist;
\r
614 l := l + copy(l2,biniplist_prefixlen+1,maxlongint);
\r
617 function biniplist_tostr(const l:tbiniplist):thostname;
\r
622 for a := 0 to biniplist_getcount(l)-1 do begin
\r
623 if result <> '(' then result := result + ', ';
\r
624 result := result + ipbintostr(biniplist_get(l,a));
\r
626 result := result + ')';
\r
629 function isbiniplist(const l:tbiniplist):boolean;
\r
633 for i := 1 to biniplist_prefixlen do begin
\r
634 if biniplist_prefix[i] <> l[i] then begin
\r
642 procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);
\r
647 for a := biniplist_getcount(l2)-1 downto 0 do begin
\r
648 biniptemp := biniplist_get(l2,a);
\r
649 if (biniptemp.family = family) then biniplist_add(l,biniptemp);
\r