3 # based on http://www.perlmonks.org/index.pl?node_id=325248
10 use Net::DHCP::Constants;
11 use Data::Dump qw/dump/;
13 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
15 my $debug = shift @ARGV;
17 our ( $server_ip, $next_file );
20 my $sock = IO::Socket::INET->new(
22 # LocalAddr => 'localhost',
23 # LocalAddr => '10.0.0.100',
24 LocalAddr => '0.0.0.0',
27 # PeerPort => getservbyname('bootpc', 'udp'),
30 ) or die "Failed to bind to socket: $@";
38 my $ip = $_mac2ip->{$mac};
42 $_mac2ip->{$mac} = $ip;
46 warn "IP roll-over to 10\n";
55 print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
58 $sock->recv($buf, 1024);
59 print "<< peer:",$sock->peerhost,":",$sock->peerport,"\n";
65 eval { $dhcp = Net::DHCP::Packet->new($buf); };
66 die "can't use request", dump( $buf ) if $@;
69 warn "recv: ", $dhcp->toString, "\n\n";
72 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
73 my $ip = client_ip($mac);
75 my $file = $next_file;
76 $file = 'undionly.kpxe' if ! $file || $dhcp->getOptionValue(DHO_USER_CLASS()) ne 'gPXE';
78 my $packet = new Net::DHCP::Packet(
80 Hops => $dhcp->hops(),
82 Flags => $dhcp->flags(),
83 Ciaddr => $dhcp->ciaddr(),
86 Giaddr => $dhcp->giaddr(),
87 Chaddr => $dhcp->chaddr(),
89 # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
90 DHO_SUBNET_MASK() => '255.0.0.0',
93 warn ">> $mac == $ip server $server_ip\n";
95 warn "## ",$packet->toString(),"\n" if $debug;
97 my $reply = IO::Socket::INET->new(
98 LocalAddr => $server_ip,
102 PeerAddr => '255.255.255.255',
105 ) or die "socket: $@";
107 my $buff = $packet->serialize();
108 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
110 # system("arp -s $ip $mac"),
113 print "No bootp request.\n";