--- bin/bootpd.pl 2007/11/18 01:29:04 67 +++ bin/bootpd.pl 2007/11/18 01:29:04 68 @@ -12,7 +12,7 @@ die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; -my $debug = 1; +my $debug = shift @ARGV; my $sock = IO::Socket::INET->new( LocalPort => 67, @@ -28,7 +28,27 @@ my $device = 'eth1'; -my $ip = '10.0.0.10'; +my $_ip = 10; +my $_mac2ip; + +sub client_ip { + my ( $mac ) = @_; + + my $ip = $_mac2ip->{$mac}; + return $ip if $ip; + + $ip = "10.0.0.$_ip"; + $_mac2ip->{$mac} = $ip; + + $_ip++; + if ( $_ip == 100 ) { + warn "IP roll-over to 10\n"; + $_ip = 10; + } + + return $ip; +} + my $ip_server = '10.0.0.100'; while (1) { @@ -50,6 +70,9 @@ warn "recv: ", $dhcp->toString, "\n\n"; } + my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); + my $ip = client_ip($mac); + my $packet = new Net::DHCP::Packet( Op => BOOTREPLY(), Hops => $dhcp->hops(), @@ -65,7 +88,9 @@ DHO_SUBNET_MASK() => '255.0.0.0', ); - warn ">> ",$sock->peeraddr,":",$sock->peerport," send $ip with server $ip_server\n",$packet->toString(),"\n"; + warn ">> $mac == $ip server $ip_server\n"; + + warn "## ",$packet->toString(),"\n" if $debug; my $reply = IO::Socket::INET->new( LocalAddr => $ip_server, @@ -77,11 +102,10 @@ Reuse => 1, ) or die "socket: $@"; - my $buff = $packet->serialize(); $reply->send( $buff, 0 ) or die "Error sending: $!\n"; -# system("arp -s $ip ",$dhcp->chaddr()); +# system("arp -s $ip $mac"), } else { print "No bootp request.\n";