Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

raw sockets

by Anonymous Monk
on May 06, 2014 at 09:48 UTC ( #1085154=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

sorry to bother you, here is my question:
how to receive packets using perl's SOCK_RAW
without anythying but only 'use Socket;'
So far I've got this.
socket RAW, AF_INET, SOCK_RAW, 255;
How do I tell which interface to use? (eth0 for example)
Is setsockopt for this?
I couldn't google it out. I really really tried.
But I've only seen that first you 'socket' then you 'recv', and that obviously can't work.
Thank you for your time and consideration.

Replies are listed 'Best First'.
Re: raw sockets
by Anonymous Monk on May 06, 2014 at 11:17 UTC

    Perl's raw socket functions more or less mirror the underlying system calls. So when you're looking for documentation, you can Google something like "linux bind to interface", which would lead you to this Stack Overflow answer, which leads you to the SO_BINDTODEVICE option, which is documented in the socket(7) manpage.

    However, from what I can tell the SO_BINDTODEVICE is not available on all OSes, and it's also not available in the Socket module. See this thread for how you could hack it.

    On the other hand, why are you using the low-level functions, when there are higher-level modules such as IO::Socket and the Net:: family around? Also, I think it's much more common to bind your socket to a specific address instead of directly to an interface. Why do you need to bind to a specific interface?

      That was very helpful, thank you very much.
      I'm gonna carefully research all the links you've provided.
      "bind to interface" due to the bridging.
Re: raw sockets
by Anonymous Monk on May 06, 2014 at 10:07 UTC

    ... without anythying but only 'use Socket;' ...

    :) Why not do it without "use Socket"?

Re: raw sockets
by pprindeville (Novice) on Aug 15, 2014 at 21:12 UTC
    You can use Net::Interface to get the IP address associated with "eth0", such as:
    use Socket qw(AF_INET pack_sockaddr_in); use Net::Interface; my $intf = Net::Interface->new('eth0'); my $ipaddr = $intf->address(AF_INET);
    and then bind() the socket to the local address as:
    bind(SOCKET, pack_sockaddr_in(0, $ipaddr)) || die "can't bind socket t +o local address";
    and this should achieve what you're asking to be able to do.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1085154]
Front-paged by Arunbear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2018-04-22 13:30 GMT
Find Nodes?
    Voting Booth?