How to know which interface a broadcast packet comes in on?

(Lincoln Yeoh) #1


If I have a program listening on on all interfaces,
how do I know which network interface a broadcast packet is coming in
on - assuming Linux and _many_ interfaces. And how do I set which
interface a frame will leave on, assuming I'm sending a raw frame (no
IP address, just the ethernet address).

If I use C, it seems I'm to use the IP_PKTINFO socket options,
send/recv the ancillary messages and set/check the ipi_ifindex value.

struct in_pktinfo {
  unsigned int ipi_ifindex; /* Interface index */
  struct in_addr ipi_spec_dst; /* Local address */
  struct in_addr ipi_addr; /* Header Destination address

How would I achieve the same thing in Ruby?

This would be useful for writing something like a DHCP server - such a
server could receive packets with source IP addresses of
destined to, and will need to know which interface the
packet is coming in from AND the source ethernet (or other link layer)
address so that it can send the replies out the right interface.