3 # based on http://www.perlmonks.org/index.pl?node_id=325248
12 use Data::Dump qw/dump/;
15 use Net::DHCP::Packet;
16 use Net::DHCP::Constants 0.67;
17 die "need to run $0 as root like this\nsudo $0\n" unless $< == 0;
19 my $debug = shift @ARGV;
21 our ( $file, $gpxe_file );
22 our ( $ip_from, $ip_to ) = ( 10, 100 );
24 our $server_ip = readlink 'conf/server.ip' if -l 'conf/server.ip';
27 $server_ip = `/sbin/ifconfig`;
28 $server_ip =~ s/^.+?addr:([\d\.]+).*$/$1/gs;
29 warn "auto-configure server ip to $server_ip\n";
31 warn "server ip $server_ip\n";
34 my $sock = IO::Socket::INET->new(
36 # LocalAddr => 'localhost',
37 # LocalAddr => '10.0.0.100',
38 LocalAddr => '0.0.0.0',
41 # PeerPort => getservbyname('bootpc', 'udp'),
44 ) or die "Failed to bind to socket: $@";
52 my $conf = "conf/$server_ip";
53 mkdir $conf unless -e $conf;
55 if ( -e "$conf/mac/$mac" ) {
56 my $ip = read_file "$conf/mac/$mac";
57 print "$mac old $ip\n";
61 mkdir $_ foreach grep { ! -e $_ } map { "$conf/$_" } ( 'ip', 'mac' );
63 my $prefix = $server_ip;
64 $prefix =~ s{\.\d+$}{.};
65 my $ip = $prefix . $addr;
66 while ( -e "conf/ip/$ip" ) {
67 $ip = $prefix . $addr++;
68 die "all addresses allocated!" if $addr == $ip_to;
71 write_file "$conf/mac/$mac", $ip;
72 if ( -l "$conf/ip/$ip" && readlink "$conf/ip/$ip" ne "$conf/mac/$mac") {
73 unlink "$conf/ip/$ip";
74 symlink "$conf/mac/$mac", "$conf/ip/$ip";
75 warn "$mac IP changed to $ip";
78 print "$mac NEW $ip\n";
83 my $transaction = 0; # FIXME predictible transaction numbers
87 require "config.pl"; # refresh config
89 print "waiting for DHCP requests on ",$sock->sockhost,":",$sock->sockport,"\n";
92 $sock->recv($buf, 1024);
93 print "<< ",$sock->peerhost,":",$sock->peerport,"\n";
97 my $dhcp = Net::DHCP::Packet->new($buf);
98 $dhcp->comment( $transaction++ );
100 warn "recv: ", $dhcp->toString, "\n\n";
102 my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2);
103 my $ip = client_ip($mac);
104 my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS());
106 if ( $user_class eq 'gPXE' ) {
108 } elsif ( ! $file ) {
109 $file = 'undionly.kpxe';
114 Hops => $dhcp->hops(),
116 Flags => $dhcp->flags(),
117 Ciaddr => $dhcp->ciaddr(),
119 Siaddr => $server_ip,
120 Giaddr => $dhcp->giaddr(),
121 Chaddr => $dhcp->chaddr(),
123 # DHO_DHCP_MESSAGE_TYPE() => DHCPACK(),
124 # DHO_SUBNET_MASK() => '255.255.255.0',
127 my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE());
129 if ($messagetype eq DHCPDISCOVER()) {
130 warn "DHCP DISCOVER";
131 $packet->{Comment} = $dhcp->comment();
132 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER();
133 } elsif ($messagetype eq DHCPREQUEST()) {
134 my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS());
135 warn "DHCP REQUEST $requested_ip";
136 if ( $ip eq $requested_ip ) {
137 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK();
138 $packet->{DHO_DHCP_LEASE_TIME()} = 100;
140 $packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPNAK();
141 $packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip";
143 } elsif ($messagetype eq DHCPINFORM()) {
144 warn "DHCP INFORM ignored";
146 warn "$messagetype igored (bootp?)";
150 warn ">> $mac == $ip server: $server_ip", $file ? " file: $file\n" : "\n";
152 $packet = new Net::DHCP::Packet( %$packet );
153 warn "## ",$packet->toString(),"\n" if $debug;
155 my $reply = IO::Socket::INET->new(
156 LocalAddr => $server_ip,
160 PeerAddr => '255.255.255.255',
163 ) or die "socket: $@";
165 my $buff = $packet->serialize();
166 $reply->send( $buff, 0 ) or die "Error sending: $!\n";
168 # system("arp -s $ip $mac"),
171 print "No bootp request.\n";