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
13 {$include lcoreconfig.inc}
\r
22 {$include pgtypes.inc}
\r
24 {$include uint32.inc}
\r
27 hexchars:array[0..15] of ansichar='0123456789abcdef';
\r
32 //redeclare these constants so units that use us can use them
\r
33 //without using sockets directly
\r
43 {$define want_Tin6_addr}
\r
46 {$define want_Tin6_addr}
\r
48 {$ifdef want_Tin6_addr}
\r
49 Tin6_addr = packed record
\r
51 0: (u6_addr8 : array[0..15] of byte);
\r
52 1: (u6_addr16 : array[0..7] of Word);
\r
53 2: (u6_addr32 : array[0..3] of uint32);
\r
54 3: (s6_addr8 : array[0..15] of shortint);
\r
55 4: (s6_addr : array[0..15] of shortint);
\r
56 5: (s6_addr16 : array[0..7] of smallint);
\r
57 6: (s6_addr32 : array[0..3] of LongInt);
\r
73 {zipplet 20170204: FPC 3.0.0 changed the definition of TInetSockAddr:
\r
74 - http://www.freepascal.org/docs-html/rtl/sockets/tinetsockaddr.html
\r
75 - http://www.freepascal.org/docs-html/rtl/sockets/sockaddr_in.html
\r
76 Due to this, TInetSockAddr -> TLInetSockAddr4 / TLInetSockAddr6
\r
77 Using our own types no matter what OS or compiler version will prevent future problems.
\r
78 Adding "4" to non IPv6 record names improves code clarity }
\r
81 //zipplet 20170204: Do we still need to support ver1_0? Perhaps a cleanup is in order.
\r
82 //For now keep supporting it for compatibility.
\r
90 TLInetSockAddr4 = packed Record
\r
99 pad :array [0..7] of byte; //zipplet 20170204 - originally this was 1..8 for some reason
\r
103 TLInetSockAddr6 = packed record
\r
111 sin6_flowinfo: uint32;
\r
112 sin6_addr: tin6_addr;
\r
113 sin6_scope_id: uint32;
\r
117 //zipplet 20170204: I did not rename the unioned record. We might want to rename this to TLinetSockAddrv
\r
118 TinetSockAddrv = packed record
\r
120 0: (InAddr:TLInetSockAddr4);
\r
122 1: (InAddr6:TLInetSockAddr6);
\r
125 Pinetsockaddrv = ^Tinetsockaddrv;
\r
128 tsockaddrin=TLInetSockAddr4;
\r
131 bin IP list code, by beware
\r
132 while this is really just a string, on the interface side it must be treated
\r
133 as an opaque var which is passed as "var" when it needs to be modified}
\r
135 tbiniplist=tbufferstring;
\r
137 function biniplist_new:tbiniplist;
\r
138 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
\r
139 function biniplist_getcount(const l:tbiniplist):integer;
\r
140 function biniplist_get(const l:tbiniplist;index:integer):tbinip;
\r
141 procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
\r
142 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
\r
143 procedure biniplist_free(var l:tbiniplist);
\r
144 procedure biniplist_addlist(var l:tbiniplist;const l2:tbiniplist);
\r
145 function biniplist_tostr(const l:tbiniplist):thostname;
\r
146 function isbiniplist(const l:tbiniplist):boolean;
\r
148 function htons(w:word):word;
\r
149 function htonl(i:uint32):uint32;
\r
151 function ipstrtobin(const s:thostname;var binip:tbinip):boolean;
\r
152 function ipstrtobinf(const s:thostname):tbinip;
\r
153 function ipbintostr(const binip:tbinip):thostname;
\r
155 function ip6bintostr(const bin:tin6_addr):thostname;
\r
156 function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;
\r
159 function comparebinip(const ip1,ip2:tbinip):boolean;
\r
160 procedure maskbits(var binip:tbinip;bits:integer);
\r
161 function comparebinipmask(ip1,ip2:tbinip;bits:integer):boolean;
\r
163 procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);
\r
166 function longip(s:thostname):longint;
\r
168 function needconverttov4(const ip:tbinip):boolean;
\r
169 procedure converttov4(var ip:tbinip);
\r
170 procedure converttov6(var ip:tbinip);
\r
172 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;
\r
173 function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;
\r
174 function inaddrsize(inaddr:tinetsockaddrv):integer;
\r
176 function getbinipbitlength(const ip:tbinip):integer;
\r
177 function getipstrbitlength(const ip:thostname):integer;
\r
178 function getfamilybitlength(family:integer):integer;
\r
184 function htons(w:word):word;
\r
186 {$ifdef ENDIAN_LITTLE}
\r
187 result := ((w and $ff00) shr 8) or ((w and $ff) shl 8);
\r
193 function htonl(i:uint32):uint32;
\r
195 {$ifdef ENDIAN_LITTLE}
\r
196 result := (i shr 24) or (i shr 8 and $ff00) or (i shl 8 and $ff0000) or (i shl 24 and $ff000000);
\r
203 function inaddrvtobinip(inaddrv:tinetsockaddrv):tbinip;
\r
205 result.family := inaddrv.inaddr.family;
\r
206 if result.family = AF_INET then result.ip := inaddrv.inaddr.addr;
\r
208 if result.family = AF_INET6 then result.ip6 := inaddrv.inaddr6.sin6_addr;
\r
212 function makeinaddrv(addr:tbinip;port:ansistring;var inaddr:tinetsockaddrv):integer;
\r
215 { biniptemp := forwardlookup(addr,10);}
\r
216 fillchar(inaddr,sizeof(inaddr),0);
\r
217 //writeln('converted address '+addr+' to binip '+ipbintostr(biniptemp));
\r
218 if addr.family = AF_INET then begin
\r
220 inAddr.InAddr.Len := sizeof(tlinetsockaddr4);
\r
222 inAddr.InAddr.family:=AF_INET;
\r
223 inAddr.InAddr.port:=htons(strtointdef(port,0));
\r
224 inAddr.InAddr.addr:=addr.ip;
\r
225 result := sizeof(tlinetsockaddr4);
\r
228 if addr.family = AF_INET6 then begin
\r
230 inAddr.InAddr6.sin6_len := sizeof(tlinetsockaddr6);
\r
232 inAddr.InAddr6.sin6_family:=AF_INET6;
\r
233 inAddr.InAddr6.sin6_port:=htons(strtointdef(port,0));
\r
234 inAddr.InAddr6.sin6_addr:=addr.ip6;
\r
235 result := sizeof(tlinetsockaddr6);
\r
240 function inaddrsize(inaddr:tinetsockaddrv):integer;
\r
243 if inaddr.inaddr.family = AF_INET6 then result := sizeof(tlinetsockaddr6) else
\r
245 result := sizeof(tlinetsockaddr4);
\r
249 {converts dotted v4 IP to longint. returns host endian order}
\r
250 function longip(s:thostname):longint;
\r
254 function convertbyte(const s:ansistring):integer;
\r
256 result := strtointdef(s,-1);
\r
257 if result < 0 then begin
\r
261 if result > 255 then begin
\r
266 if (result <> 0) and (s[1] = '0') then begin
\r
271 if not (s[1] in ['0'..'9']) then begin
\r
280 if a = 0 then exit;
\r
281 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
283 s := copy(s,a+1,256);
\r
285 if a = 0 then exit;
\r
286 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
287 l := l or b shl 16;
\r
288 s := copy(s,a+1,256);
\r
290 if a = 0 then exit;
\r
291 b := convertbyte(copy(s,1,a-1));if (b < 0) then exit;
\r
293 s := copy(s,a+1,256);
\r
294 b := convertbyte(copy(s,1,256));if (b < 0) then exit;
\r
300 function ipstrtobinf;
\r
302 ipstrtobin(s,result);
\r
305 function ipstrtobin(const s:thostname;var binip:tbinip):boolean;
\r
310 if pos(':',s) <> 0 then begin
\r
311 {try ipv6. use builtin routine}
\r
312 result := ip6strtobin(s,binip.ip6);
\r
313 if result then binip.family := AF_INET6;
\r
319 // zipplet: htonl() expects a uint32 but longip() spits out longint.
\r
320 // Because longip() is deprecated, we do not fix it but typecast.
\r
321 //binip.ip := htonl(longip(s));
\r
322 binip.ip := htonl(uint32(longip(s)));
\r
323 if (binip.ip <> 0) or (s = '0.0.0.0') then begin
\r
325 binip.family := AF_INET;
\r
330 function ipbintostr(const binip:tbinip):thostname;
\r
336 if binip.family = AF_INET6 then begin
\r
337 result := ip6bintostr(binip.ip6);
\r
340 if binip.family = AF_INET then begin
\r
341 a := htonl(binip.ip);
\r
342 result := inttostr(a shr 24)+'.'+inttostr((a shr 16) and $ff)+'.'+inttostr((a shr 8) and $ff)+'.'+inttostr(a and $ff);
\r
347 {------------------------------------------------------------------------------}
\r
352 IPv6 address binary to/from string conversion routines
\r
355 - implementation does not depend on other ipv6 code such as the tin6_addr type,
\r
356 the parameter can also be untyped.
\r
357 - it is host endian neutral - binary format is always network order
\r
358 - it supports compression of zeroes
\r
359 - it supports ::ffff:192.168.12.34 style addresses
\r
360 - they are made to do the Right Thing, more efficient implementations are possible
\r
363 {fpc has hostaddrtostr6 and strtohostaddr6 but the later isnt implemented yet}
\r
366 function ip6bintostr(const bin:tin6_addr):thostname;
\r
367 {base16 with lowercase output}
\r
368 function makehex(w:word):ansistring;
\r
371 if w >= 4096 then result := result + hexchars[w shr 12];
\r
372 if w >= 256 then result := result + hexchars[w shr 8 and $f];
\r
373 if w >= 16 then result := result + hexchars[w shr 4 and $f];
\r
374 result := result + hexchars[w and $f];
\r
378 a,b,c,addrlen:integer;
\r
379 runbegin,runlength:integer;
\r
380 bytes:array[0..15] of byte absolute bin;
\r
381 words:array[0..7] of word;
\r
382 dwords:array[0..3] of integer absolute words;
\r
384 for a := 0 to 7 do begin
\r
385 words[a] := bytes[a shl 1] shl 8 or bytes[a shl 1 or 1];
\r
387 if (dwords[0] = 0) and (dwords[1] = 0) and (words[4] = 0) and (words[5] = $ffff) then begin
\r
388 {::ffff:/96 exception: v4 IP}
\r
393 {find longest run of zeroes}
\r
396 for a := 0 to addrlen-1 do begin
\r
397 if words[a] = 0 then begin
\r
399 for b := a to addrlen-1 do if words[b] = 0 then begin
\r
402 if (c > runlength) then begin
\r
409 {run length at least 2 0 words}
\r
410 if (runlength = 1) then begin
\r
416 for a := 0 to runbegin-1 do begin
\r
417 if (a <> 0) then result := result + ':';
\r
418 result := result + makehex(words[a]);
\r
420 if runlength > 0 then result := result + '::';
\r
421 c := runbegin+runlength;
\r
422 for a := c to addrlen-1 do begin
\r
423 if (a > c) then result := result + ':';
\r
424 result := result + makehex(words[a]);
\r
426 if addrlen = 6 then begin
\r
427 result := result + ':'+inttostr(bytes[12])+'.'+inttostr(bytes[13])+'.'+inttostr(bytes[14])+'.'+inttostr(bytes[15]);
\r
431 function ip6strtobin(const s:thostname;var bin:tin6_addr):boolean;
\r
434 fields:array[0..7] of ansistring;
\r
435 fieldcount:integer;
\r
436 emptyfield:integer;
\r
438 words:array[0..7] of word;
\r
439 bytes:array[0..15] of byte absolute bin;
\r
442 for a := 0 to 7 do fields[a] := '';
\r
444 for a := 1 to length(s) do begin
\r
445 if s[a] = ':' then inc(fieldcount) else fields[fieldcount] := fields[fieldcount] + s[a];
\r
446 if fieldcount > 7 then exit;
\r
448 if fieldcount < 2 then exit;
\r
450 {find the empty field (compressed zeroes), not counting the first and last there may be at most 1}
\r
452 for a := 1 to fieldcount-1 do begin
\r
453 if fields[a] = '' then begin
\r
454 if emptyfield = -1 then emptyfield := a else exit;
\r
458 {check if last field is a valid v4 IP}
\r
459 a := longip(fields[fieldcount]);
\r
460 if (a <> 0) or (fields[fieldcount] = '0.0.0.0') then wordcount := 6 else wordcount := 8;
\r
461 {0:1:2:3:4:5:6.6.6.6
\r
463 fillchar(words,sizeof(words),0);
\r
464 if wordcount = 6 then begin
\r
465 if fieldcount > 6 then exit;
\r
466 words[6] := a shr 16;
\r
467 words[7] := a and $ffff;
\r
469 if emptyfield = -1 then begin
\r
470 {no run length: must be an exact number of fields}
\r
471 if wordcount = 6 then begin
\r
472 if fieldcount <> 6 then exit;
\r
474 end else if wordcount = 8 then begin
\r
475 if fieldcount <> 7 then exit;
\r
479 for a := 0 to emptyfield do begin
\r
480 if fields[a] = '' then b := 0 else b := strtointdef('$'+fields[a],-1);
\r
481 if (b < 0) or (b > $ffff) then exit;
\r
484 if wordcount = 6 then dec(fieldcount);
\r
485 for a := wordcount-1 downto wordcount-(fieldcount-emptyfield) do begin
\r
486 b := a+fieldcount-wordcount+1;
\r
487 if fields[b] = '' then b := 0 else b := strtointdef('$'+fields[b],-1);
\r
488 if (b < 0) or (b > $ffff) then exit;
\r
491 for a := 0 to 7 do begin
\r
492 bytes[a shl 1] := words[a] shr 8;
\r
493 bytes[a shl 1 or 1] := words[a] and $ff;
\r
499 function comparebinip(const ip1,ip2:tbinip):boolean;
\r
501 if (ip1.ip <> ip2.ip) then begin
\r
507 if ip1.family = AF_INET6 then begin
\r
508 if (ip1.ip6.s6_addr32[1] <> ip2.ip6.s6_addr32[1])
\r
509 or (ip1.ip6.s6_addr32[2] <> ip2.ip6.s6_addr32[2])
\r
510 or (ip1.ip6.s6_addr32[3] <> ip2.ip6.s6_addr32[3]) then begin
\r
517 result := (ip1.family = ip2.family);
\r
520 procedure maskbits(var binip:tbinip;bits:integer);
\r
522 ipmax={$ifdef ipv6}15{$else}3{$endif};
\r
523 type tarr=array[0..ipmax] of byte;
\r
529 if bits = 0 then b := 0 else b := ((bits-1) div 8)+1;
\r
530 for a := b to ipmax do begin
\r
533 if (bits and 7 <> 0) then begin
\r
534 arr[bits shr 3] := arr[bits div 8] and not ($ff shr (bits and 7))
\r
538 function comparebinipmask;
\r
540 maskbits(ip1,bits);
\r
541 maskbits(ip2,bits);
\r
542 result := comparebinip(ip1,ip2);
\r
545 function needconverttov4(const ip:tbinip):boolean;
\r
548 if ip.family = AF_INET6 then begin
\r
549 if (ip.ip6.u6_addr32[0] = 0) and (ip.ip6.u6_addr32[1] = 0) and
\r
550 (ip.ip6.u6_addr16[4] = 0) and (ip.ip6.u6_addr16[5] = $ffff) then begin
\r
560 {converts a binary IP to v4 if it is a v6 IP in the v4 range}
\r
561 procedure converttov4(var ip:tbinip);
\r
564 if needconverttov4(ip) then begin
\r
565 ip.family := AF_INET;
\r
566 ip.ip := ip.ip6.s6_addr32[3];
\r
572 {converts a binary IP to v6 if it is a v4 IP}
\r
573 procedure converttov6(var ip:tbinip);
\r
576 if ip.family = AF_INET then begin
\r
577 ip.family := AF_INET6;
\r
578 ip.ip6.s6_addr32[3] := ip.ip;
\r
579 ip.ip6.u6_addr32[0] := 0;
\r
580 ip.ip6.u6_addr32[1] := 0;
\r
581 ip.ip6.u6_addr16[4] := 0;
\r
582 ip.ip6.u6_addr16[5] := $ffff;
\r
588 {-----------biniplist stuff--------------------------------------------------}
\r
591 biniplist_prefix: ansistring = 'bipl'#0;
\r
592 //fpc 1.0.x doesn't seem to like use of length function in a constant
\r
594 //biniplist_prefixlen=length(biniplist_prefix);
\r
596 biniplist_prefixlen=5;
\r
598 function biniplist_new:tbiniplist;
\r
600 result := biniplist_prefix;
\r
603 procedure biniplist_add(var l:tbiniplist;ip:tbinip);
\r
607 a := biniplist_getcount(l);
\r
608 biniplist_setcount(l,a+1);
\r
609 biniplist_set(l,a,ip);
\r
612 function biniplist_getcount(const l:tbiniplist):integer;
\r
614 result := (length(l)-biniplist_prefixlen) div sizeof(tbinip);
\r
617 function biniplist_get(const l:tbiniplist;index:integer):tbinip;
\r
619 if (index >= biniplist_getcount(l)) then begin
\r
620 fillchar(result,sizeof(result),0);
\r
623 move(l[index*sizeof(tbinip)+1+biniplist_prefixlen],result,sizeof(result));
\r
626 procedure biniplist_set(var l:tbiniplist;index:integer;ip:tbinip);
\r
629 move(ip,l[index*sizeof(tbinip)+1+biniplist_prefixlen],sizeof(ip));
\r
632 procedure biniplist_setcount(var l:tbiniplist;newlen:integer);
\r
634 setlength(l,(sizeof(tbinip)*newlen)+biniplist_prefixlen);
\r
637 procedure biniplist_free(var l:tbiniplist);
\r
642 procedure biniplist_addlist;
\r
644 l := l + copy(l2,biniplist_prefixlen+1,maxlongint);
\r
647 function biniplist_tostr(const l:tbiniplist):thostname;
\r
652 for a := 0 to biniplist_getcount(l)-1 do begin
\r
653 if result <> '(' then result := result + ', ';
\r
654 result := result + ipbintostr(biniplist_get(l,a));
\r
656 result := result + ')';
\r
659 function isbiniplist(const l:tbiniplist):boolean;
\r
663 for i := 1 to biniplist_prefixlen do begin
\r
664 if biniplist_prefix[i] <> l[i] then begin
\r
672 procedure addipsoffamily(var l:tbiniplist;const l2:tbiniplist;family:integer);
\r
677 for a := biniplist_getcount(l2)-1 downto 0 do begin
\r
678 biniptemp := biniplist_get(l2,a);
\r
679 if (biniptemp.family = family) then biniplist_add(l,biniptemp);
\r
683 function getfamilybitlength(family:integer):integer;
\r
686 if family = AF_INET6 then result := 128 else
\r
688 if family = AF_INET then result := 32
\r
692 function getbinipbitlength(const ip:tbinip):integer;
\r
694 result := getfamilybitlength(ip.family);
\r
697 function getipstrbitlength(const ip:thostname):integer;
\r
701 ipstrtobin(ip,biniptemp);
\r
702 result := getbinipbitlength(biniptemp);
\r