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

L

Lincoln Yeoh

Hi,

If I have a program listening on 0.0.0.0:(someport) 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 Perl?

This would be useful for writing something like a DHCP server - such a
server could receive packets with source IP addresses of 0.0.0.0
destined to 255.255.255.255, 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.

Thanks!
Link.
 
T

Thomas Kratz

Lincoln said:
Hi,

If I have a program listening on 0.0.0.0:(someport) 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 Perl?

This would be useful for writing something like a DHCP server - such a
server could receive packets with source IP addresses of 0.0.0.0
destined to 255.255.255.255, 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.

Here is a way using IO::Socket::INET:

use strict;
use warnings;

use IO::Socket;

my $s = IO::Socket::INET->new(
Listen => 5,
LocalPort => 7777,
) or die $@;

my $c = $s->accept();
print inet_ntoa($c->sockaddr());

Thomas

--
$/=$,,$_=<DATA>,s,(.*),$1,see;__END__
s,^(.*\043),,mg,@_=map{[split'']}split;{#>J~.>_an~>>e~......>r~
$_=$_[$%][$"];y,<~>^,-++-,?{$/=--$|?'"':#..u.t.^.o.P.r.>ha~.e..
'%',s,(.),\$$/$1=1,,$;=$_}:/\w/?{y,_, ,,#..>s^~ht<._..._..c....
print}:y,.,,||last,,,,,,$_=$;;eval,redo}#.....>.e.r^.>l^..>k^.-
 
L

Lincoln Yeoh

Thanks, that would only work if I had different IP addresses on all
the interfaces though. I guess my problem is kinda unique :).

I think I'll go check out:
http://search.cpan.org/~mjp/Socket-MsgHdr-0.01/MsgHdr.pm

Hope it works for my scenario or I find something similar that does.

Otherwise I might have to learn Python and PyXAPI (and hope that works
:) ).

Link.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,733
Messages
2,569,440
Members
44,832
Latest member
GlennSmall

Latest Threads

Top