in reply to Figuring out which network interface a broadcast packet came in on?

You might try Net::DHCP::Packet and Net::DHCP::Constants.
-- gam3
A picture is worth a thousand words, but takes 200K.
  • Comment on Re: Figuring out which network interface a broadcast packet came in on?

Replies are listed 'Best First'.
Re^2: Figuring out which network interface a broadcast packet came in on?
by lyeoh (Acolyte) on Sep 02, 2005 at 17:51 UTC
    I believe those modules are for constructing DHCP packets, not for the actual sending or receiving.

    Something like Net-Packet (which I checked out months back) would be good. However I would like to bind to 0.0.0.0:$someport (and listen on all interfaces - not specific interfaces), and then figure out which interface a packet is coming in on (and then reply using the relevant interface). This would be much simpler and more efficient.

    With perl one can set socket options (I've done it for other socket options like SO_ORIGINAL_DST), however I don't know how to set the particular Linux options I need for this case and retrieve the ancillary messages, in order to figure out the packet's interface.

    Any ideas on how to do this in perl?

      I think you need to set rules in your routing table in order to route packets based on incoming/outgoing interfaces (ip is your friend here).

      As far as I know, a socket at the application layer can give you only information on ip address/port number and not about interface on which it arrived. You might have to dig into writing your own code at the kernel layer, or use one of the utilities like ip to get the information you want.

      Just my 2 cents..

        Routing tables aren't very useful for DHCP packets, because a significant proportion of DHCP packets don't have unique IP addresses even at a local network level. After all DHCP is typically used by hosts without valid IP addresses to get valid IP addresses.

        getsockopt and setsockopt allow you to get more info about a socket. e.g.

        # Get the original destination of the connection # that was redirected to our socket my $p= getsockopt $client, $SOL_IP, $SO_ORIGINAL_DST; $orig_destaddr=inet_ntoa(substr($p,4,4)); $orig_destport=unpack('n',substr($p,2,2));
        I just don't know how to read or write the ancillary messages once I set the IP_PKTINFO socket option. In C it's stuff like sendmsg, recvmsg or cmsg.

        DHCP doesn't really require high performance - legitimate client machines should not be sending DHCP packets at a high rate, so perl isn't such a bad fit other than this problem I'm having. A perl DHCP server won't be as prone to buffer overflows and will be flexible and more easily extensible.