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

Hi Monks, I am now studying Net::DHCP::Packet by runing an example script copied from the module's document. but I find that all the DHCP packets sent by this script with the source IP address of my machine, but this address is supposed to be 0.0.0.0. I added a line which specifies the LocalAddr into it but the result was same. I looked into the source, and find out the problem may be is in IO::Socket::INET. But even after I explicitly assigned 0.0.0.0 to LocalAddr, the result doesn't change at all. Could any one help me on this? Thanks a lot Below is the code:
#!/usr/bin/perl # Simple DHCP client - sending a broadcasted DHCP Discover request use IO::Socket::INET; use Net::DHCP::Packet; use Net::DHCP::Constants; # creat DHCP Packet $discover = Net::DHCP::Packet->new( xid => int(rand(0xFFFFFFFF)), # random xid Flags => 0x8000, # ask for broadcast answer DHO_DHCP_MESSAGE_TYPE() => DHCPDISCOVER() ); # send packet $handle = IO::Socket::INET->new(Proto => 'udp', Broadcast => 1, PeerPort => '67', LocalPort => '68', LocalAddr => '0.0.0.0', # I added this line PeerAddr => '255.255.255.255') or die "socket: $@"; # yes, it uses $@ here $handle->send($discover->serialize()) or die "Error sending broadcast inform:$!\n";

Replies are listed 'Best First'.
Re: how can I set LocalAddr to INADDR_ANY in IO::Socket::INET
by ikegami (Patriarch) on Mar 19, 2008 at 07:32 UTC

    '0.0.0.0' is the same as INADDR_ANY (once packed), so you're telling the system to let it pick the interface. That's the default behaviour, so that's why LocalHost => '0.0.0.0', has no effect.

    I don't know how you can work around that, but I suspect you'll have to work at a much lower level. Raw packet?

    By the way, it makes no sense to specify LocalAddr and LocalPort, since LocalAddr is a shortcut for LocalHost and LocalPort.

Re: how can I set LocalAddr to INADDR_ANY in IO::Socket::INET
by Corion (Patriarch) on Mar 19, 2008 at 07:45 UTC

    Have you looked at how the DHCP modules on CPAN do their stuff? For example, Net::DHCPClient even seems to have a ->discover method.

    If you look at the source of Net::DHCPClient, you'll find that it uses Net::RawIP to send the packet it constructs, so I guess you will have to do the same, or at least something similar.

Re: how can I set LocalAddr to INADDR_ANY in IO::Socket::INET
by ikegami (Patriarch) on Mar 19, 2008 at 07:51 UTC

    Your whole problem rest on the claim that the source address must be '0.0.0.0'. Are you sure about that? It would normally be that since the machine has no other address, but I don't think it's a requirement.

    For example, wouldn't the source address be something other than '0.0.0.0' in the case where the request is forwarded by a router? It's been a while since I've read the spec and it's not jumping out at me.

      Thanks ikegami, you are right! I tried the script and the discovery packet with a non-zero SA packet can be recognized by the DHCP server, which means my assumption is wrong. Thank you all :-)
        which means my assumption is wrong. Thank you all :-)
        Humans, still making assumptions :)
      Yes, I'm trying to simulate a DHCP client to communicate with the server. And normally, when I issue a DHCP discovery request, the Source IP Address should be 0.0.0.0, I haven't seen any discovery packet with a non-0.0.0.0 source address. So if I want to send out a DHCP discovery packet, the SA must be 0.0.0.0.