Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Re: simple DHCP server in Perl

by Rhandom (Curate)
on Jul 17, 2014 at 14:21 UTC ( #1094057=note: print w/replies, xml ) Need Help??

in reply to simple DHCP server in Perl

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 = ""; my $client_ip = ""; my $subnet_mask = ""; __PACKAGE__->run( proto => 'udp', port => 67, host => "", ); 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)];

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1094057]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2020-09-29 11:41 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (146 votes). Check out past polls.