ppp has asked for the wisdom of the Perl Monks concerning the following question:

I have written a script using perl and i have tested it using two different ways: (1)Using a wifi connection (I have router which is directly connected to server and i am connected to that router). When i run my script on wifi connection, I receive DHCPOffer with IP and when i send request than i receive DHCPNack on that Request. i don't know why ? (2) Using the same code when i connect using a dongle then i even don't receive DHCPDiscover it just kind of hang for about hours and there is no any kind of message from server.

Below is all my code :
use IO::Socket::INET; use Net::DHCP::Packet; use Net::DHCP::Constants; my $result='false'; my $nowtime = localtime(time); print "\nprint $nowtime\n"; SocketCreation(); if ( $result eq 'true') { print "\nTRUE\n"; } else { print "\nFALSE \n"; } #########Here is the finction definition to SocketCreation ########### +################ sub SocketCreation() { my $br_addr = sockaddr_in( '67', inet_aton('255.255.255.255') ); my $xid = int( rand(0xFFFFFFFF) ); #my $chaddr = '00189BF9C1Dp'; my $chaddr = '0016cbb7c882'; my $socket = IO::Socket::INET->new( Proto => 'udp', Broadcast => 1, LocalPort => '68', ) or die "Can't create socket: $@\n"; print "\n please wait and have patience......:\n"; my $discover_packet = Net::DHCP::Packet->new( Xid => $xid, Chaddr => $chaddr, Flags => 0x8000, DHO_DHCP_MESSAGE_TYPE() => DHCPDISCOVER(), DHO_HOST_NAME() => 'Perrr', DHO_VENDOR_CLASS_IDENTIFIER() => 'perlr', ); print "/n just a check\n"; $socket->send( $discover_packet->serialize(), 0, $br_addr) or die "Error sending:$!\n"; my $buf = ''; print "/n just a check2\n"; $socket->recv( $buf, 1024) or die "recvfrom() failed:$!"; #print STDERR $socket->toString(); my $resp = new Net::DHCP::Packet($buf); print "Details:\n" . $resp->toString(); $ip_offerto=$resp->yiaddr(); print "\n IP we got from google is : $ip_offerto\n"; $result = ($ip_offerto =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/) ? "t +rue":"false"; print $result; # create DHCP Packet REQUEST $request = Net::DHCP::Packet->new( Xid => 0x12345678, Ciaddr => $resp->yiaddr(), DHO_DHCP_MESSAGE_TYPE() => DHCPREQUEST(), DHO_VENDOR_CLASS_IDENTIFIER() => 'ShekharsIPRequ +est', DHO_DHCP_REQUESTED_ADDRESS() => $resp->yiaddr(), ); print"\nSending REQUEST to server\n"; print $request->toString(); $socket->send($request->serialize(),0,$br_addr) or die "Error sending:$!\n"; print "\nWaiting for response from server\n"; $socket->recv($buf, 4096) || die("recv:$!"); print "\nGot response\n"; $resp = new Net::DHCP::Packet($buf); print STDERR $resp->toString(); close($socket); }
Actually could some one please give me solution for : (1) Why it give DHPCNACK when i use WIfi (through another roter) ? (2) Why the same code hangs while run through dongle (USB Modem internet device)?
  • Comment on why i don't receive DHCPDiscover message from server using dongle and why same code give DHCPNac on wifi
  • Download Code

Replies are listed 'Best First'.
Re: why i don't receive DHCPDiscover message from server using dongle and why same code give DHCPNac on wifi
by Perlbotics (Archbishop) on Jan 11, 2015 at 18:16 UTC

    Things, I would try first:

    • example.com? - is the returned IP the correct one? Set manually to see if that helps?
    • Check if router filters on MAC... (update:) If the router does not forward IP packets from an unregistered source (the MAC of your PCs card/dongle), the request will not reach the server - this is a simple (but not very effective) security measure.
    • Check if local hosts interface(s) is reachable from server (routing). (update:) If the server is multi-homed and the routing is not configured right, the response might travel through the wrong (default-)network.
    • Snoop at server side, analyse traffic with wireshark. (updated)
    • Use i.e. perltidy to make code easier to parse for humans ;-)
      Example.com is extra part (just to print current IP at terminal , I removed it now). The real part of socket creation is done in SocketCreation() function. Please explain points 2 , 3 and 4 (specially 2, 3) in bit more details. Also please note that i am using windows (not linux) Thanks a lot.
        Please note that this is a Perl board, not a sys admin help desk.

        Cheers Rolf

        PS: Je suis Charlie!

Re: why i don't receive DHCPDiscover message from server using dongle and why same code give DHCPNac on wifi
by VinsWorldcom (Prior) on Jan 12, 2015 at 19:36 UTC

    I'm not sure what you're trying to do here. Windows already does DHCP for you. But nevertheless:

    1. use strict; use warnings;
    2. When I run this on Strawberry 5.18.1 MSWin32-x64-multi-thread on Windows 7 x64, I get:
      "Can't create socket: IO::Socket::INET: Only one usage of each socket address (protocol/network address/port) is normally permitted."

    This makes sense as my local UDP 68 port is in use by svchost.exe for DHCP services already. When I switch to a non-well-known-port (e.g., 6800) it runs but it doesn't send the packet out the interface I expected - which may be your problem when using the alternate NIC. I determined this with a packet capture (Wireshark) capturing on all interfaces with filter 'udp.srcport == 6800'.

    Windows has a preferred interface to send traffic out when there are several matches in the host route table. In this case, your destination of 255.255.255.255 could be for *any* network interface / card, so the default one is used, which by priority, may not be the one you "want".

A reply falls below the community's threshold of quality. You may see it by logging in.