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; |
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 |
return $ip; |
return $ip; |
58 |
} else { |
} elsif ( in_our_range( $request_ip ) ) { |
59 |
$ip = client::next_ip( $mac ); |
$ip = client::next_ip( $mac ); |
60 |
print "NEW $mac $ip\n"; |
print "NEW $mac $ip\n"; |
61 |
|
} else { |
62 |
|
$ip = $request_ip; |
63 |
|
client::save_ip_mac( $ip, $mac ); |
64 |
|
warn "W: $ip our of server range $server::ip $server::netmask\n"; |
65 |
} |
} |
66 |
|
|
67 |
return $ip; |
return $ip; |
75 |
our $file; |
our $file; |
76 |
our $transaction = 0; # FIXME predictible transaction numbers |
our $transaction = 0; # FIXME predictible transaction numbers |
77 |
|
|
78 |
|
sub ip2bin { pack('C*', split(/\./, $_[0])) }; |
79 |
|
sub in_our_range { |
80 |
|
my $ip = shift; |
81 |
|
return 1 if $ip eq '0.0.0.0'; |
82 |
|
return 1 if ( |
83 |
|
( ip2bin($ip) & ip2bin($server::netmask) ) |
84 |
|
eq |
85 |
|
( ip2bin($server::ip) & ip2bin($server::netmask) ) |
86 |
|
); |
87 |
|
} |
88 |
|
|
89 |
sub process_packet { |
sub process_packet { |
90 |
my $sock = shift; |
my $sock = shift; |
91 |
|
|
100 |
return unless $buf; |
return unless $buf; |
101 |
|
|
102 |
my $dhcp = Net::DHCP::Packet->new($buf); |
my $dhcp = Net::DHCP::Packet->new($buf); |
|
$dhcp->comment( $transaction++ ); |
|
103 |
|
|
104 |
warn "recv: ", $dhcp->toString if $debug; |
warn "recv: ", $dhcp->toString if $debug; |
105 |
|
|
106 |
|
$dhcp->comment( $transaction++ ); |
107 |
|
|
108 |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
109 |
my $ip = client_ip($mac); |
my $ip = client_mac_ip($mac, $dhcp->ciaddr); |
110 |
|
|
111 |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
my $hostname = $dhcp->getOptionValue(DHO_HOST_NAME); |
112 |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; |
print "$ip ", client::conf( $ip => 'hostname', default => $hostname ), " >> /etc/hosts\n"; |
113 |
|
|
114 |
my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname }; |
my $audit = { mac => format::mac($mac), ip => $ip, hostname => $hostname, |
115 |
|
options => { |
116 |
|
map { |
117 |
|
( $_ => $dhcp->getOptionValue( $_ ) ) |
118 |
|
} @{ $dhcp->{options_order} } |
119 |
|
}, |
120 |
|
}; |
121 |
|
|
122 |
=for later |
=for later |
123 |
|
|
213 |
$packet = new Net::DHCP::Packet( %$packet ); |
$packet = new Net::DHCP::Packet( %$packet ); |
214 |
warn "send ",$packet->toString() if $debug; |
warn "send ",$packet->toString() if $debug; |
215 |
|
|
216 |
CouchDB::audit( $audit->{type}, $audit ); |
if ( in_our_range( $ip ) ) { |
217 |
|
my $buff = $packet->serialize(); |
218 |
|
|
219 |
my $reply = IO::Socket::INET->new( |
my $reply = IO::Socket::INET->new( |
220 |
LocalAddr => $server::ip, |
LocalAddr => $server::ip, |
221 |
LocalPort => 67, |
LocalPort => 67, |
222 |
Proto => "udp", |
Proto => "udp", |
223 |
Broadcast => 1, |
Broadcast => 1, |
224 |
PeerAddr => '255.255.255.255', |
PeerAddr => '255.255.255.255', |
225 |
PeerPort => 68, |
PeerPort => 68, |
226 |
Reuse => 1, |
Reuse => 1, |
227 |
) or die "socket: $@"; |
) or die "socket: $@"; |
228 |
|
|
229 |
my $buff = $packet->serialize(); |
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
230 |
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
} else { |
231 |
|
$audit->{error} = "$ip our of our range $server::ip $server::netmask"; |
232 |
|
} |
233 |
|
|
234 |
|
CouchDB::audit( $audit->{type}, $audit ); |
235 |
|
|
236 |
# system("arp -s $ip $mac"), |
# system("arp -s $ip $mac"), |
237 |
|
|