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

Discussion in 'Perl Misc' started by Lincoln Yeoh, Sep 3, 2005.

  1. Lincoln Yeoh

    Lincoln Yeoh Guest

    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.
    Lincoln Yeoh, Sep 3, 2005
    #1
    1. Advertising

  2. Lincoln Yeoh

    Thomas Kratz Guest

    Lincoln Yeoh wrote:
    > 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^.-
    Thomas Kratz, Sep 5, 2005
    #2
    1. Advertising

  3. Lincoln Yeoh

    Lincoln Yeoh Guest

    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.

    On Mon, 05 Sep 2005 13:26:16 +0200, Thomas Kratz
    <> wrote:

    >
    >Here is a way using IO::Socket::INET:
    >my $c = $s->accept();
    >print inet_ntoa($c->sockaddr());
    >
    >Thomas
    Lincoln Yeoh, Sep 6, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lincoln Yeoh
    Replies:
    1
    Views:
    545
    Jorgen Grahn
    Sep 3, 2005
  2. Li Han
    Replies:
    2
    Views:
    505
    bobicanprogram
    Feb 9, 2009
  3. Gelonida N
    Replies:
    4
    Views:
    880
    Gelonida N
    Sep 11, 2011
  4. Martin Farnik

    UDP Broadcast Packet (offtopic)

    Martin Farnik, Jul 4, 2003, in forum: Ruby
    Replies:
    3
    Views:
    158
  5. Lincoln Yeoh
    Replies:
    0
    Views:
    101
    Lincoln Yeoh
    Sep 3, 2005
Loading...

Share This Page