# prüfe, ob es in der DB zu dieser Adresse Netz- bzw. zu diesem Netz
# Einzeladressen gibt. Wenn ja: Warne.

use utf8;
use strict;
use warnings;
no warnings 'redefine';
use Dbase::Help qw(DoFn);
use Fehler qw(warnung);

sub check_ipnr_net($) {
	my($adr) = @_;
	if($adr->db_bits) { ## Netz: suche Einzeladressen
		my($id,$ip,$bits);

		($id,$ip,$bits) = DoFn("select id,ip6,bits from ipkunde where ${\ $adr->networkaddr->dbs } and (ende IS NULL OR ende > UNIX_TIMESTAMP(NOW()))");
		if($id) {
			$ip = Dbase::IP->new_db($ip,$bits);
			warnung "Für dieses Netz gibt es eine Netzadresse: #$id: $ip\n";
		}

		($id,$ip,$bits) = DoFn("select id,ip6,bits from ipkunde where ${\ $adr->broadcastaddr->dbs } and (ende IS NULL OR ende > UNIX_TIMESTAMP(NOW()))");
		if($id) {
			$ip = Dbase::IP->new_db($ip,$bits);
			warnung "Für dieses Netz gibt es eine Broadcastadresse: #$id: $ip\n";
		}
	} else { # Einzeladresse: Suche Netz- und Broadcastadresse
		my($id,$ip,$bits) = DoFn("select id,ip6,bits from ipkunde where ip6='${\ $adr->db_ip6 }' and bits>0 and (ende IS NULL OR ende > UNIX_TIMESTAMP(NOW())) order by bits limit 1");
		if(defined $id) {
			$ip = Dbase::IP->new_db($ip,$bits);
			warnung "Dies ist eine Netzadresse zu #$id: $ip\n";
		}
		my $bcadr = $adr;
		while(1) {
			$bcadr = $bcadr->bitmask($bcadr->db_bits+1,1);
			last if $bcadr->broadcastaddr->numeric ne $adr->numeric;
			my($id,$ip,$bits) = DoFn("select id,ip6,bits from ipkunde where ${\ $bcadr->dbs } and (ende IS NULL OR ende > UNIX_TIMESTAMP(NOW()))");
			if($id) {
				$ip = Dbase::IP->new_db($ip,$bits);
				warnung "Dies ist eine Broadcastadresse zu #$id: $ip\n";
				last;
			}

		}

	}
}

1;
