Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Printing data from a raw socket to the screen and/or echoing it back via event driven means

by Feynman (Novice)
on Sep 18, 2012 at 19:15 UTC ( #994334=perlquestion: print w/ replies, xml ) Need Help??
Feynman has asked for the wisdom of the Perl Monks concerning the following question:

#What I am ultimately trying to accomplish:
My ultimate goal is to forward everything that a raw socket picks up at 0.0.0.0 over an XMPP chat line get another computer to receive the data and pass it on to its 0.0.0.0 address via another raw socket. I almost had it in python using an IRC, but found that IRCs do not like long messages or many short ones. XMPP servers seem to be less restrictive so I was hoping the system would work with one of those. I decided to switch from Python to Perl after evaluating all the socket management and XMPP client modules for a number of scripting languages. After attempting this in a number of scripting languages, I have learned to come pretty close to my final goal by starting with an XMPP (or IRC) echo bot and a socket server that prints data to the screen and/or sends it back to the source. In the past I used TCP, but I am switching to raw sockets for a number of reasons that are not important for this particular question.

#What I would like to be able to do first:
For the time being, I have been trying to learn to set up a raw socket, bind it to address 0.0.0.0, and have it print everything it receives in an event driven manner. I assumed this would mean using IO::Async or AnyEvent. I found that AnyEvent does not like raw sockets because they are "not streams" or something, so I was trying to use IO:Async. However, I am not very familiar with Perl, and I honestly have just been trying to hash together example codes so I can study something. I cannot imagine this is very hard to do, but for some reason I cannot figure out how to do it.

#What am asking of anyone who would be willing to help:
I would really appreciate some example codes that use raw sockets. Something that prints the raw data to the screen and/or echos it back to the sender would be great. From there, I need to learn how to set up an event driven echo bot for XMPP in Perl. Once I have those, I already know how I want to combine them into something that transmits data over a chatline because I have done similar things in other scripting languages.

Thanks

Comment on Printing data from a raw socket to the screen and/or echoing it back via event driven means
Re: Printing data from a raw socket to the screen and/or echoing it back via event driven means
by Rudolf (Monk) on Sep 18, 2012 at 19:54 UTC

    Hello, instead of making a socket from scratch you can use the alternative is IO::Socket::INET which gives you a nice scalar reference that you can call some methods on to make reading and things easier. good luck!

    use IO::Socket::INET; my($ip,$port)=('0.0.0.0','9999'); my $server = new IO::Socket::INET ( PeerAddr => $ip, PeerPort => $port, Proto => 'tcp', ) or die "Couldn't connect to Server: $!\n";
      Thanks for your repy! So, you showed me how to make a TCP socket at 0.0.0.0 port 9999. That helps, but I was wondering how I would use IO::Async or AnyEvent or some other event driven module to automagically have data echoed back or printed to the screen when something gets sent to the socket. In particular, I wanted to use a RAW socket, and I have not seen a lot of documentation on using raw sockets. Here is some pseudocode:
      use IO::Socket::INET; my $server = new IO::Socket::INET(LocalAddr => '0.0.0.0', Proto => 'ra +w'); use My::MagicEventBasedModule; my $my_event_loop_handler_thing = new My::MagicEventBasedModule ( file_handler => $server, when_there_is_data_to_be_read => sub{ my ($data, $source_address, $source_port) = @_; print $data; $server->write("You've got data!"); } ); #maybe you have to "run" it like a thread in Python: $my_event_loop_handler_thing->run();
      Something like that would be ideal. I thought IO::Async or AnyEvent would make for a nice substitute for the fictitious My::MagicEventBasedModule. As you can see in the pseudocode, I associated a subroutine with a point at which $my_event_loop_handler_thing detects the filehandler to have data that could be read. In my pseudocode, the subroutine is passed the data along with the source address and port that sent it. That would be nice, but all I really need is the data. The raw IP packet would contain all the information I would need. Actually I would just encode it in hex and pass it over XMPP to the other computer, which would decode it and feed it to its raw socket at 0.0.0.0. Then the process would start again as the other computer replies. For now, I just need to know how to use an event based module to print raw data from a raw socket to the screen every time data is sent to the socket. The reason I want it to be event based is so I am not explicitly assigning threads and/or processes to constantly check the socket and the XMPP client for new data and messages respectively.
        A truly raw socket listens at the ethernet level, which is not something IO::Socket::INET does (which works at the IP-level, which gives you the choices for protocols TCP, UDP or ICMP).
        For raw sockets, you will have to use Socket. Edit: Raw sockets cannot be bound to an IP.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://994334]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (14)
As of 2014-09-17 19:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (97 votes), past polls