23 |
use Net::DHCP::Packet; |
use Net::DHCP::Packet; |
24 |
use Net::DHCP::Constants 0.67; |
use Net::DHCP::Constants 0.67; |
25 |
|
|
26 |
|
use CouchDB; |
27 |
|
use format; |
28 |
|
|
29 |
use server; |
use server; |
30 |
my $debug = server::debug; |
my $debug = server::debug; |
31 |
|
|
90 |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
91 |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; |
92 |
|
|
93 |
|
my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname }; |
94 |
|
|
95 |
=for later |
=for later |
96 |
|
|
97 |
my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS()); |
my $user_class = $dhcp->getOptionValue(DHO_USER_CLASS()); |
144 |
} |
} |
145 |
|
|
146 |
warn "W: options requested but missing: ",dump( @missing ),$/; |
warn "W: options requested but missing: ",dump( @missing ),$/; |
147 |
|
$audit->{requested} = [ @requested ]; |
148 |
|
$audit->{missing} = [ @missing ]; |
149 |
|
|
150 |
foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) { |
foreach my $opt ( 'magic', 'config_file', 'path_prefix', 'reboot_time' ) { |
151 |
my $DH0 = eval 'DHO_PXELINUX_' . uc $opt; |
my $DH0 = eval 'DHO_PXELINUX_' . uc $opt; |
160 |
my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE()); |
my $messagetype = $dhcp->getOptionValue(DHO_DHCP_MESSAGE_TYPE()); |
161 |
|
|
162 |
if ($messagetype eq DHCPDISCOVER()) { |
if ($messagetype eq DHCPDISCOVER()) { |
163 |
log::mac $mac, "DHCP DISCOVER"; |
$audit->{type} = 'discover'; |
164 |
$packet->{Comment} = $dhcp->comment(); |
$packet->{Comment} = $dhcp->comment(); |
165 |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER(); |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPOFFER(); |
166 |
} elsif ($messagetype eq DHCPREQUEST()) { |
} elsif ($messagetype eq DHCPREQUEST()) { |
167 |
my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()); |
my $requested_ip = $dhcp->getOptionValue(DHO_DHCP_REQUESTED_ADDRESS()); |
168 |
log::mac $mac, "DHCP REQUEST $requested_ip $ip $file"; |
$audit->{type} = 'request'; |
169 |
if ( $ip eq $requested_ip ) { |
if ( $ip eq $requested_ip ) { |
170 |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK(); |
$packet->{DHO_DHCP_MESSAGE_TYPE()} = DHCPACK(); |
171 |
$packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min |
$packet->{DHO_DHCP_LEASE_TIME()} = 5 * 60; # 5 min |
175 |
$packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip"; |
$packet->{DHO_DHCP_MESSAGE()} = "Bad request, expected $ip"; |
176 |
} |
} |
177 |
} elsif ($messagetype eq DHCPINFORM()) { |
} elsif ($messagetype eq DHCPINFORM()) { |
178 |
log::mac $mac, "DHCP INFORM ignored"; |
$audit->{type} = 'inform'; |
179 |
} else { |
} else { |
180 |
log::mac $mac, "$messagetype igored (bootp?)"; |
$audit->{type} = sprintf('ignored %x', $messagetype); |
181 |
} |
} |
182 |
|
|
183 |
warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug; |
warn ">> $mac == $ip server: $server::ip", $file ? " file: $file\n" : "\n" if $debug; |
184 |
|
$audit->{response} = $packet; |
185 |
|
|
186 |
$packet = new Net::DHCP::Packet( %$packet ); |
$packet = new Net::DHCP::Packet( %$packet ); |
187 |
warn "send ",$packet->toString() if $debug; |
warn "send ",$packet->toString() if $debug; |
188 |
|
|
189 |
|
CouchDB::audit( $audit->{type}, $audit ); |
190 |
|
|
191 |
my $reply = IO::Socket::INET->new( |
my $reply = IO::Socket::INET->new( |
192 |
LocalAddr => $server::ip, |
LocalAddr => $server::ip, |
193 |
LocalPort => 67, |
LocalPort => 67, |
221 |
|
|
222 |
print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; |
print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; |
223 |
|
|
224 |
|
CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } ); |
225 |
|
|
226 |
while (1) { |
while (1) { |
227 |
process_packet $sock; |
process_packet $sock; |
228 |
} |
} |