X-Git-Url: http://git.datanom.net/pve-dhcp-server.git/blobdiff_plain/45adf7883055f085d650a986f5bc68e3df870f5e..HEAD:/dhcp.pl diff --git a/dhcp.pl b/dhcp.pl index 56f9502..cd1616e 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"); @@ -32,18 +32,21 @@ logger("Opening socket"); $send = IO::Socket::IP->new( Proto => 'udp', Broadcast => 1, PeerPort => 'bootps(67)', - PeerAddr => inet_ntoa(INADDR_LOOPBACK),#(INADDR_BROADCAST), + #PeerAddr => inet_ntoa(INADDR_LOOPBACK),#(INADDR_BROADCAST), + PeerAddr => inet_ntoa(INADDR_BROADCAST), ) || die "Socket (send) creation error: $@\n"; # yes, it uses $@ here # create DHCP Packet DISCOVER +my $xid = int(rand(0xFFFFFFFF)); $discover = Net::DHCP::Packet->new( Chaddr => $mymac, Giaddr => $send->sockhost(), - Xid => int(rand(0xFFFFFFFF)), # random xid + Xid => $xid, # random xid 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 +58,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 +68,11 @@ $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(); @@ -71,11 +80,12 @@ logger($response->toString()); # create DHCP Packet REQUEST $request = Net::DHCP::Packet->new( - Xid => int(rand(0xFFFFFFFF)), # random xid + Xid => $xid, # random xid Chaddr => $mymac, Giaddr => $send->sockhost(), DHO_DHCP_MESSAGE_TYPE() => DHCPREQUEST(), DHO_VENDOR_CLASS_IDENTIFIER() => 'foo', + DHO_HOST_NAME() => $hostname, DHO_DHCP_REQUESTED_ADDRESS() => $myip, ); @@ -90,6 +100,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 +127,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() );