From: Michael Rasmussen Date: Wed, 23 Jul 2014 22:06:06 +0000 (+0200) Subject: Adapt to latest code X-Git-Url: http://git.datanom.net/pve-dhcp-server.git/commitdiff_plain/094c3a58621bdbaa5aab15c958a7d91afe3c8748 Adapt to latest code --- diff --git a/dhcp.pl b/dhcp.pl index 56f9502..7890766 100644 --- a/dhcp.pl +++ b/dhcp.pl @@ -10,11 +10,11 @@ use Net::DHCP::Constants; use POSIX qw(setsid strftime); -my ($request, $send, $receive, $discover, $response, $buf, $serverip, $myip, $mymac); +my ($request, $send, $receive, $discover, $response, $buf, $serverip, $myip, $mymac, $hostname); $mymac = qx/ \/sbin\/ifconfig | grep -P '^eth0.+HWaddr\\s*' | awk '{print \$5}' /; $mymac =~ tr/://d; - +chomp($hostname = `hostname -s`); # Overrule when testing #$mymac = '001cc0c33317'; logger("mac: $mymac"); @@ -33,6 +33,7 @@ $send = IO::Socket::IP->new( Proto => 'udp', Broadcast => 1, PeerPort => 'bootps(67)', PeerAddr => inet_ntoa(INADDR_LOOPBACK),#(INADDR_BROADCAST), + #PeerAddr => inet_ntoa(INADDR_BROADCAST), ) || die "Socket (send) creation error: $@\n"; # yes, it uses $@ here @@ -44,6 +45,7 @@ $discover = Net::DHCP::Packet->new( DHO_DHCP_MESSAGE_TYPE() => DHCPDISCOVER(), Flags => 0x8000, DHO_VENDOR_CLASS_IDENTIFIER() => 'foo', + DHO_HOST_NAME() => $hostname, ); logger("Sending DISCOVER to " . $send->peerhost . ":" . $send->peerport); @@ -55,6 +57,7 @@ $receive = IO::Socket::IP->new( Proto => 'udp', Broadcast => 1, LocalPort => 'bootpc(68)', #LocalAddr => inet_ntoa(INADDR_LOOPBACK),#(INADDR_ANY), + LocalAddr => inet_ntoa(INADDR_ANY), ) || die "Socket (receive) creation error: $@\n"; # yes, it uses $@ here @@ -64,6 +67,10 @@ $receive->recv($buf, 1024, 0); logger("Got response from " . $receive->peerhost . ":" . $receive->peerport); $receive->close; $response = new Net::DHCP::Packet($buf); +if ($response->getOptionValue(DHO_DHCP_MESSAGE_TYPE()) == DHCPNAK()) { + logger("Request for ip was denied"); + exit; +} $serverip = $response->getOptionValue(DHO_DHCP_SERVER_IDENTIFIER()); $myip = $response->yiaddr(); @@ -76,6 +83,7 @@ $request = Net::DHCP::Packet->new( Giaddr => $send->sockhost(), DHO_DHCP_MESSAGE_TYPE() => DHCPREQUEST(), DHO_VENDOR_CLASS_IDENTIFIER() => 'foo', + DHO_HOST_NAME() => $hostname, DHO_DHCP_REQUESTED_ADDRESS() => $myip, ); @@ -90,6 +98,7 @@ $receive = IO::Socket::IP->new( Proto => 'udp', Broadcast => 1, LocalPort => 'bootpc(68)', #LocalAddr => inet_ntoa(INADDR_LOOPBACK),#inet_ntoa(INADDR_ANY), + LocalAddr => inet_ntoa(INADDR_ANY), ) || die "Socket creation error: $@\n"; # yes, it uses $@ here @@ -116,6 +125,7 @@ if ($response->getOptionValue(DHO_DHCP_MESSAGE_TYPE()) == DHCPACK()) { Ciaddr => $myip, Giaddr => $send->sockhost(), DHO_VENDOR_CLASS_IDENTIFIER() => 'foo', + DHO_HOST_NAME() => $hostname, DHO_DHCP_MESSAGE_TYPE() => DHCPRELEASE() ); diff --git a/dhcpd.pl b/dhcpd.pl index 2afdbcd..d2f3444 100644 --- a/dhcpd.pl +++ b/dhcpd.pl @@ -2,13 +2,13 @@ use strict; use warnings; -use PVE::DHCPServer qw(:constants); +use PVE::DHCPServer; use Data::Dumper; my $server = PVE::DHCPServer->new( NODAEMON => 1, DEBUG => 1, - LOG_LEVEL => DEBUG); + LOG_LEVEL => INFO); #print Dumper($server); $server->run;