Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Why socket not responding in webserver

by romy_mathew (Beadle)
on Oct 16, 2011 at 10:00 UTC ( [id://931750]=perlquestion: print w/replies, xml ) Need Help??

romy_mathew has asked for the wisdom of the Perl Monks concerning the following question:

Hi I wrote a socket server and client and tested on local host on my home system and I was able to communicate with multiple PC @ home. Now I moved the socket server to the web server and tried to test the client connection on my terminal. but every time i get a connection time out error message. I am posting the server code and client code here, can anyone help to find , what is going wrong....

Server

#!/usr/bin/perl use strict; use IO::Socket; use POSIX 'WNOHANG'; use constant PORT => 12000; my $quit = 0; $SIG{CHLD} = sub { while (waitpid(-1,WNOHANG)>0) { } }; my $lisen_socket = IO::Socket::INET->new( LocalPort => PORT, Proto => 'tcp', Listen => 1, Reuse => 1 ); die "cant create a soocket \n" unless $lisen_socket; warn "server ready.. waiting for connection in port : 12000......\n"; while (!$quit) { my $connection; next unless $connection = $lisen_socket->accept; defined(my $child = fork()) or die "cannot fork : $!\n"; if($child == 0) { print "Inside child\n"; $lisen_socket->close(); &interact($connection); exit 0; print "exiting from child\n"; } $connection->close(); } sub interact { my $data; my $count = 0; my $sock = shift; #STDIN->fdopen($sock,"<") or die "cannot reopen STDIN : $!\n"; #STDOUT->fdopen($sock,">") or die "cannot reopen STDOUT : $!\n"; #STDERR->fdopen($sock,">") or die "cannot reopen STDERR : $!\n"; $| =1; while(defined $sock) { print $count; $data = <$sock>; if($data ne "") { print "Server : $data\n"; print $sock "type next line\n"; $count++; } } }

Client

#!/usr/bin/perl use strict; use IO::Socket::INET; $| =1; my $socket = IO::Socket::INET->new( PeerHost =>'208.91.198.132', PeerPort =>'12000', Proto => 'tcp', ) or die "cannot create a socket $@\n"; print "TCP Connection Creatred Sucessfully\n";

Replies are listed 'Best First'.
Re: Why socket not responding in webserver
by Perlbotics (Archbishop) on Oct 16, 2011 at 10:19 UTC

    Probably a firewall issue: Client  --(SYN)--> Firewall( TCP 12000 blocked )  --X--> Webhost.
    Contact admin.

    Take care not to introduce security risks. Maybe you can realise your service by means of an existing webserver and e.g. CGI?

      Would like to know adding socket communication like the similar one I posted above is a real security issue...?, I had this feeling{socket communication is high risk} from many online posts and documents but still don't understand why this is highly prone for attack.

        Depends. You didn't gave much information about the environment your script is used in (exposition to threats) and what it is supposed to do (extend of damage in case of successful attack). The measures you have to take might be simple (e.g. when operating in a somewhat closed environment: intranet, VPN) or more complex (Internet).

        E.g., your current implementation is accessible for everybody who can connect to port 12000 of the webhost. If your're not talking about an intranet webhost, the script is in principle globally accessible! There is no password protection. If you had one, it could be snooped since there is no encryption in place. I guess, you will replace the interact() sub with something more powerful. It could be something innocent as

        # simple time service sub interact { ... print $sock "It's: " . localtime . "\n"; ... }
        or something like
        # poor man's telnet sub interact { ... while ( <$sock> ) { chomp; print $sock `$_ 2>&1`; } ... }

        Read perlsec as an introduction. Minimum security measures to take:

        • use taint-mode / sanitize all input
        • execute with minimum privileges
        • chroot if possible
        • provide password protection
        • restrict access to given IPs (ideally localhost, maybe you can use an SSH-tunnel?)
        • restrict local resource usage (e.g. kill script if interact() runs too long) / ulimit
        • use a daemon package
        • don't forget logging
        • keep your environment patched
        • update: add - SIGPIPE handler (see what I mean with next item?)
        • etc. ... it's the game to forget something that can be used to compromise your site.
        It is not impossible to create a reasonable safe socket based server, but it is hard work and little mistakes have potential for great harm (or I am just too paranoid ;-).

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-04-26 03:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found