To make life easier, here is a Net::Server based approach:
#!/usr/bin/env perl
use strict;
use warnings;
use base qw(Net::Server::Fork);
use Net::DHCP::Packet;
use Net::DHCP::Constants;
my $server_ip = "10.10.10.1";
my $client_ip = "10.10.10.10";
my $subnet_mask = "255.255.255.0";
__PACKAGE__->run(
proto => 'udp',
port => 67,
host => "255.255.255.255",
);
sub process_request {
my ($self, $client) = @_;
my $buf = $self->{'server'}->{'udp_data'};
my $packet = Net::DHCP::Packet->new($buf);
my $messagetype = $packet->getOptionValue(DHO_DHCP_MESSAGE_TYPE())
+;
if ($messagetype eq DHCPDISCOVER()) {
send_offer($client, $packet);
} elsif ($messagetype eq DHCPREQUEST()) {
send_ack($client, $packet);
}
}
sub send_offer {
my ($client, $request) = @_;
my $offer = Net::DHCP::Packet->new(
Op => BOOTREPLY(),
Xid => $request->xid(),
Flags => $request->flags(),
Ciaddr => $request->ciaddr(),
Yiaddr => $client_ip,
Siaddr => $server_ip,
Giaddr => $request->giaddr(),
Chaddr => $request->chaddr(),
DHO_DHCP_MESSAGE_TYPE() => DHCPOFFER(),
);
$offer->addOptionValue(DHO_SUBNET_MASK(), $subnet_mask);
$offer->addOptionValue(DHO_NAME_SERVERS, $server_ip);
$client->send($offer->serialize()) or die $!;
print STDERR "sent offer\n";
}
sub send_ack {
my ($client, $request) = @_;
print STDERR "send ack\n";
}
my @a=qw(random brilliant braindead); print $a[rand(@a)];
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.