24 |
use Net::DHCP::Constants 0.67; |
use Net::DHCP::Constants 0.67; |
25 |
|
|
26 |
use CouchDB; |
use CouchDB; |
27 |
|
use format; |
28 |
|
|
29 |
use server; |
use server; |
30 |
my $debug = server::debug; |
my $debug = server::debug; |
39 |
|
|
40 |
use client; |
use client; |
41 |
|
|
42 |
sub client_ip { |
sub client_mac_ip { |
43 |
my ( $mac ) = @_; |
my ( $mac, $request_ip ) = @_; |
44 |
|
|
45 |
|
if ( ! $mac ) { |
46 |
|
warn "W: no mac in requiest\n"; |
47 |
|
return; |
48 |
|
} |
49 |
|
|
50 |
my $conf = $server::conf; |
my $conf = $server::conf; |
51 |
mkdir $conf unless -e $conf; |
mkdir $conf unless -e $conf; |
54 |
|
|
55 |
if ( $ip = client::ip_from_mac( $mac ) ) { |
if ( $ip = client::ip_from_mac( $mac ) ) { |
56 |
print "RENEW $mac $ip\n"; |
print "RENEW $mac $ip\n"; |
57 |
|
client::save_ip_mac( $ip, $mac ); |
58 |
return $ip; |
return $ip; |
59 |
} else { |
} elsif ( in_our_range( $request_ip ) ) { |
60 |
$ip = client::next_ip( $mac ); |
$ip = client::next_ip( $mac ); |
61 |
print "NEW $mac $ip\n"; |
print "NEW $mac $ip\n"; |
62 |
|
} else { |
63 |
|
$ip = $request_ip; |
64 |
|
client::save_ip_mac( $ip, $mac ); |
65 |
|
warn "W: $ip our of server range $server::ip $server::netmask\n"; |
66 |
} |
} |
67 |
|
|
68 |
return $ip; |
return $ip; |
76 |
our $file; |
our $file; |
77 |
our $transaction = 0; # FIXME predictible transaction numbers |
our $transaction = 0; # FIXME predictible transaction numbers |
78 |
|
|
79 |
|
sub ip2bin { pack('C*', split(/\./, $_[0])) }; |
80 |
|
sub in_our_range { |
81 |
|
my $ip = shift; |
82 |
|
return 1 if $ip eq '0.0.0.0'; |
83 |
|
return 1 if ( |
84 |
|
( ip2bin($ip) & ip2bin($server::netmask) ) |
85 |
|
eq |
86 |
|
( ip2bin($server::ip) & ip2bin($server::netmask) ) |
87 |
|
); |
88 |
|
} |
89 |
|
|
90 |
sub process_packet { |
sub process_packet { |
91 |
my $sock = shift; |
my $sock = shift; |
92 |
|
|
101 |
return unless $buf; |
return unless $buf; |
102 |
|
|
103 |
my $dhcp = Net::DHCP::Packet->new($buf); |
my $dhcp = Net::DHCP::Packet->new($buf); |
|
$dhcp->comment( $transaction++ ); |
|
104 |
|
|
105 |
warn "recv: ", $dhcp->toString if $debug; |
warn "recv: ", $dhcp->toString if $debug; |
106 |
|
|
107 |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
$dhcp->comment( $transaction++ ); |
108 |
my $ip = client_ip($mac); |
|
109 |
|
my $mac = format::mac( substr($dhcp->chaddr(),0,$dhcp->hlen()*2) ); |
110 |
|
my $ip = client_mac_ip($mac, $dhcp->ciaddr); |
111 |
|
|
112 |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
113 |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n" if $hostname; |
114 |
|
|
115 |
my $audit = { mac => $mac, ip => $ip, hostname => $hostname }; |
my $audit = { mac => $mac, ip => $ip, hostname => $hostname, |
116 |
CouchDB::audit( 'request', $audit ); |
options => { |
117 |
|
map { |
118 |
|
( $_ => $dhcp->getOptionValue( $_ ) ) |
119 |
|
} @{ $dhcp->{options_order} } |
120 |
|
}, |
121 |
|
}; |
122 |
|
|
123 |
=for later |
=for later |
124 |
|
|
214 |
$packet = new Net::DHCP::Packet( %$packet ); |
$packet = new Net::DHCP::Packet( %$packet ); |
215 |
warn "send ",$packet->toString() if $debug; |
warn "send ",$packet->toString() if $debug; |
216 |
|
|
217 |
CouchDB::audit( 'response', $audit ); |
if ( in_our_range( $ip ) ) { |
218 |
|
my $buff = $packet->serialize(); |
219 |
|
|
220 |
my $reply = IO::Socket::INET->new( |
my $reply = IO::Socket::INET->new( |
221 |
LocalAddr => $server::ip, |
LocalAddr => $server::ip, |
222 |
LocalPort => 67, |
LocalPort => 67, |
223 |
Proto => "udp", |
Proto => "udp", |
224 |
Broadcast => 1, |
Broadcast => 1, |
225 |
PeerAddr => '255.255.255.255', |
PeerAddr => '255.255.255.255', |
226 |
PeerPort => 68, |
PeerPort => 68, |
227 |
Reuse => 1, |
Reuse => 1, |
228 |
) or die "socket: $@"; |
) or die "socket: $@"; |
229 |
|
|
230 |
|
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
231 |
|
} else { |
232 |
|
$audit->{error} = "$ip our of our range $server::ip $server::netmask"; |
233 |
|
} |
234 |
|
|
235 |
my $buff = $packet->serialize(); |
CouchDB::audit( $audit->{type}, $audit ); |
|
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
|
236 |
|
|
237 |
# system("arp -s $ip $mac"), |
# system("arp -s $ip $mac"), |
238 |
|
|
254 |
|
|
255 |
print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; |
print "DHCP listen on ",$sock->sockhost,":",$sock->sockport,"\n"; |
256 |
|
|
257 |
CouchDB::audit( 'start', { 'listen' => { addr => $sock->sockhost, port => $sock->sockport } } ); |
CouchDB::audit( 'start', { addr => $sock->sockhost, port => $sock->sockport } ); |
258 |
|
|
259 |
while (1) { |
while (1) { |
260 |
process_packet $sock; |
process_packet $sock; |