12 |
|
|
13 |
die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; |
die "need to run $0 as root like this\nsudo $0\n" unless $< == 0; |
14 |
|
|
15 |
my $debug = 1; |
my $debug = shift @ARGV; |
16 |
|
|
17 |
my $sock = IO::Socket::INET->new( |
my $sock = IO::Socket::INET->new( |
18 |
LocalPort => 67, |
LocalPort => 67, |
28 |
|
|
29 |
my $device = 'eth1'; |
my $device = 'eth1'; |
30 |
|
|
31 |
my $ip = '10.0.0.10'; |
my $_ip = 10; |
32 |
|
my $_mac2ip; |
33 |
|
|
34 |
|
sub client_ip { |
35 |
|
my ( $mac ) = @_; |
36 |
|
|
37 |
|
my $ip = $_mac2ip->{$mac}; |
38 |
|
return $ip if $ip; |
39 |
|
|
40 |
|
$ip = "10.0.0.$_ip"; |
41 |
|
$_mac2ip->{$mac} = $ip; |
42 |
|
|
43 |
|
$_ip++; |
44 |
|
if ( $_ip == 100 ) { |
45 |
|
warn "IP roll-over to 10\n"; |
46 |
|
$_ip = 10; |
47 |
|
} |
48 |
|
|
49 |
|
return $ip; |
50 |
|
} |
51 |
|
|
52 |
my $ip_server = '10.0.0.100'; |
my $ip_server = '10.0.0.100'; |
53 |
|
|
54 |
while (1) { |
while (1) { |
70 |
warn "recv: ", $dhcp->toString, "\n\n"; |
warn "recv: ", $dhcp->toString, "\n\n"; |
71 |
} |
} |
72 |
|
|
73 |
|
my $mac = substr($dhcp->chaddr(),0,$dhcp->hlen()*2); |
74 |
|
my $ip = client_ip($mac); |
75 |
|
|
76 |
my $packet = new Net::DHCP::Packet( |
my $packet = new Net::DHCP::Packet( |
77 |
Op => BOOTREPLY(), |
Op => BOOTREPLY(), |
78 |
Hops => $dhcp->hops(), |
Hops => $dhcp->hops(), |
88 |
DHO_SUBNET_MASK() => '255.0.0.0', |
DHO_SUBNET_MASK() => '255.0.0.0', |
89 |
); |
); |
90 |
|
|
91 |
warn ">> ",$sock->peeraddr,":",$sock->peerport," send $ip with server $ip_server\n",$packet->toString(),"\n"; |
warn ">> $mac == $ip server $ip_server\n"; |
92 |
|
|
93 |
|
warn "## ",$packet->toString(),"\n" if $debug; |
94 |
|
|
95 |
my $reply = IO::Socket::INET->new( |
my $reply = IO::Socket::INET->new( |
96 |
LocalAddr => $ip_server, |
LocalAddr => $ip_server, |
102 |
Reuse => 1, |
Reuse => 1, |
103 |
) or die "socket: $@"; |
) or die "socket: $@"; |
104 |
|
|
|
|
|
105 |
my $buff = $packet->serialize(); |
my $buff = $packet->serialize(); |
106 |
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
$reply->send( $buff, 0 ) or die "Error sending: $!\n"; |
107 |
|
|
108 |
# system("arp -s $ip ",$dhcp->chaddr()); |
# system("arp -s $ip $mac"), |
109 |
|
|
110 |
} else { |
} else { |
111 |
print "No bootp request.\n"; |
print "No bootp request.\n"; |