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");
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
DHO_DHCP_MESSAGE_TYPE() => DHCPDISCOVER(),
Flags => 0x8000,
DHO_VENDOR_CLASS_IDENTIFIER() => 'foo',
+ DHO_HOST_NAME() => $hostname,
);
logger("Sending DISCOVER to " . $send->peerhost . ":" . $send->peerport);
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
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();
Giaddr => $send->sockhost(),
DHO_DHCP_MESSAGE_TYPE() => DHCPREQUEST(),
DHO_VENDOR_CLASS_IDENTIFIER() => 'foo',
+ DHO_HOST_NAME() => $hostname,
DHO_DHCP_REQUESTED_ADDRESS() => $myip,
);
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
Ciaddr => $myip,
Giaddr => $send->sockhost(),
DHO_VENDOR_CLASS_IDENTIFIER() => 'foo',
+ DHO_HOST_NAME() => $hostname,
DHO_DHCP_MESSAGE_TYPE() => DHCPRELEASE()
);