39 |
|
|
40 |
use client; |
use client; |
41 |
|
|
42 |
sub client_ip { |
sub client_mac_ip { |
43 |
my ( $mac ) = @_; |
my ( $mac, $request_ip ) = @_; |
44 |
|
|
45 |
my $conf = $server::conf; |
my $conf = $server::conf; |
46 |
mkdir $conf unless -e $conf; |
mkdir $conf unless -e $conf; |
50 |
if ( $ip = client::ip_from_mac( $mac ) ) { |
if ( $ip = client::ip_from_mac( $mac ) ) { |
51 |
print "RENEW $mac $ip\n"; |
print "RENEW $mac $ip\n"; |
52 |
return $ip; |
return $ip; |
53 |
} else { |
} elsif ( in_our_range( $request_ip ) ) { |
54 |
$ip = client::next_ip( $mac ); |
$ip = client::next_ip( $mac ); |
55 |
print "NEW $mac $ip\n"; |
print "NEW $mac $ip\n"; |
56 |
|
} else { |
57 |
|
$ip = $request_ip; |
58 |
|
client::save_ip_mac( $ip, $mac ); |
59 |
|
warn "W: $ip our of server range $server::ip $server::netmask\n"; |
60 |
} |
} |
61 |
|
|
62 |
return $ip; |
return $ip; |
70 |
our $file; |
our $file; |
71 |
our $transaction = 0; # FIXME predictible transaction numbers |
our $transaction = 0; # FIXME predictible transaction numbers |
72 |
|
|
73 |
|
sub ip2bin { pack('C*', split(/\./, $_[0])) }; |
74 |
|
sub in_our_range { |
75 |
|
my $ip = shift; |
76 |
|
return 1 if $ip eq '0.0.0.0'; |
77 |
|
return 1 if ( |
78 |
|
( ip2bin($ip) & ip2bin($server::netmask) ) |
79 |
|
eq |
80 |
|
( ip2bin($server::ip) & ip2bin($server::netmask) ) |
81 |
|
); |
82 |
|
} |
83 |
|
|
84 |
sub process_packet { |
sub process_packet { |
85 |
my $sock = shift; |
my $sock = shift; |
86 |
|
|
100 |
warn "recv: ", $dhcp->toString if $debug; |
warn "recv: ", $dhcp->toString if $debug; |
101 |
|
|
102 |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
103 |
my $ip = client_ip($mac); |
my $ip = client_mac_ip($mac, $dhcp->ciaddr); |
104 |
|
|
105 |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
106 |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; |
201 |
$packet = new Net::DHCP::Packet( %$packet ); |
$packet = new Net::DHCP::Packet( %$packet ); |
202 |
warn "send ",$packet->toString() if $debug; |
warn "send ",$packet->toString() if $debug; |
203 |
|
|
204 |
CouchDB::audit( $audit->{type}, $audit ); |
if ( in_our_range( $ip ) ) { |
205 |
|
my $buff = $packet->serialize(); |
206 |
|
|
207 |
my $reply = IO::Socket::INET->new( |
my $reply = IO::Socket::INET->new( |
208 |
LocalAddr => $server::ip, |
LocalAddr => $server::ip, |
209 |
LocalPort => 67, |
LocalPort => 67, |
210 |
Proto => "udp", |
Proto => "udp", |
211 |
Broadcast => 1, |
Broadcast => 1, |
212 |
PeerAddr => '255.255.255.255', |
PeerAddr => '255.255.255.255', |
213 |
PeerPort => 68, |
PeerPort => 68, |
214 |
Reuse => 1, |
Reuse => 1, |
215 |
) or die "socket: $@"; |
) or die "socket: $@"; |
216 |
|
|
217 |
my $buff = $packet->serialize(); |
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
218 |
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
} else { |
219 |
|
$audit->{error} = "$ip our of our range $server::ip $server::netmask"; |
220 |
|
} |
221 |
|
|
222 |
|
CouchDB::audit( $audit->{type}, $audit ); |
223 |
|
|
224 |
# system("arp -s $ip $mac"), |
# system("arp -s $ip $mac"), |
225 |
|
|