Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > help with IO::Socket:INET

Reply
Thread Tools

help with IO::Socket:INET

 
 
me
Guest
Posts: n/a
 
      03-26-2009
Hi, I'm using IO::Socket:INET in a client/server connect and all is
working well but I want to restrict connections on the server side by
only allowing certain IP addresses to connect (or in the case I was
working to drop the connection if the IP is not contained in a list).

my $sock = new IO::Socket::INET(
LocalPort => $port,
Proto => 'tcp',
Listen => SOMAXCONN,
Reuse => 1);
$sock or die "no socket : $!";

if ($pid = fork) {
log_msg("Parent spawning child process $pid", $logfile);
close $client;
} elsif (defined ($pid = fork)) {
if ($sock->peerhost() ne $accepted_ip) {
printf "$timestamp Connection from $accepted_ip rejected\n";
close $client;


the $sock->PeerHost() is not evaluating as expected. Is this used on
the server side? how to accomplish what I want?
 
Reply With Quote
 
 
 
 
smallpond
Guest
Posts: n/a
 
      03-26-2009
On Mar 26, 9:58 am, me <eba...@yahoo.com> wrote:
> Hi, I'm using IO::Socket:INET in a client/server connect and all is
> working well but I want to restrict connections on the server side by
> only allowing certain IP addresses to connect (or in the case I was
> working to drop the connection if the IP is not contained in a list).
>
> my $sock = new IO::Socket::INET(
> LocalPort => $port,
> Proto => 'tcp',
> Listen => SOMAXCONN,
> Reuse => 1);
> $sock or die "no socket : $!";
>
> if ($pid = fork) {
> log_msg("Parent spawning child process $pid", $logfile);
> close $client;} elsif (defined ($pid = fork)) {
>


print "Remote:",$sock->peerhost(),": Accept:",$accepted_ip,":\n";

> if ($sock->peerhost() ne $accepted_ip) {
> printf "$timestamp Connection from $accepted_ip rejected\n";


Note that you are printing the wrong address here. ^^

> close $client;
>
> the $sock->PeerHost() is not evaluating as expected. Is this used on
> the server side? how to accomplish what I want?


String comparisons of numeric data could have a problem if the
data is not in the same exact form. IPv4 addresses can always
be converted to a 32-bit integer.

 
Reply With Quote
 
 
 
 
deja_vu_was_better
Guest
Posts: n/a
 
      03-26-2009
On Mar 26, 10:28*am, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth me <eba...@yahoo.com>:
>
> > Hi, I'm using IO::Socket:INET in a client/server connect and all is
> > working well but I want to restrict connections on the server side by
> > only allowing certain IP addresses to connect (or in the case I was
> > working to drop the connection if the IP is not contained in a list).

>
> This is often something better handled at the firewall. You may want to
> talk to your firewall admin to see if they can help you.
>
> > my $sock = new IO::Socket::INET(
> > * * * LocalPort => $port,
> > * * * Proto => 'tcp',
> > * * * Listen => SOMAXCONN,
> > * * * Reuse => 1);
> > $sock or die "no socket : $!";

>
> > if ($pid = fork) *{
> > * *log_msg("Parent spawning child process $pid", $logfile);
> > * *close $client;
> > } elsif (defined ($pid = fork)) *{

>
> This is not correct. You will end up forking twice, which is not what
> you meant. You want something more
>
> * * my $pid = fork;
>
> * * unless (defined $pid) {
> * * * * die "fork failed: $!";
> * * }
>
> * * if ($pid) {
> * * * * # parent
> * * }
> * * else {
> * * * * # child
> * * }
>
> > * *if ($sock->peerhost() ne $accepted_ip) *{
> > * * *printf "$timestamp * Connection from $accepted_ip rejected\n";
> > * * *close $client;

>
> > the $sock->PeerHost() is not evaluating as expected. *Is this used on
> > the server side? *how to accomplish what I want?

>
> You haven't called ->accept, so you haven't actually made a connection
> yet. ->accept returns a new socket for each connection, and it is that
> socket which has a valid ->peerhost. For example,
>
> * * my $S = IO::Socket::INET->new(
> * * * * LocalPort => $port,
> * * * * Listen * *=> 1,
> * * );
>
> * * my $C = $S->accept;
>
> * * warn "Got connection from " . $C->peerhost;
>
> Ben


Forgive me I actually did have the accept in before the fork as in
$client = $sock->accept() but when I did a

print "got a connection from $client->peerhost\n";

I get:

got a connection from IO::Socket::INET=GLOB(0x1c24284)->peerhost


It is not evaluating to a string, I guess, so how do I do that?
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      03-26-2009
deja_vu_was_better <> wrote in news:3c0bed90-52d8-478a-
b771-:

> $client = $sock->accept() but when I did a
>
> print "got a connection from $client->peerhost\n";
>
> I get:
>
> got a connection from IO::Socket::INET=GLOB(0x1c24284)->peerhost
>
> It is not evaluating to a string, I guess, so how do I do that?


printf "got a connection from %s\n", $client->peerhost;

Sinan

--
A. Sinan Unur <>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      03-27-2009
deja_vu_was_better <> wrote:


> print "got a connection from $client->peerhost\n";
>
> I get:
>
> got a connection from IO::Socket::INET=GLOB(0x1c24284)->peerhost
>
>
> It is not evaluating to a string,



Sure it is.

$client is evaluating to the string "IO::Socket::INET=GLOB(0x1c24284)".

function (method) calls are not interpolated.


> I guess, so how do I do that?



print "got a connection from ", $client->peerhost, "\n";


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
HELP - HELP - HELP =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?= ASP .Net 4 03-09-2006 12:46 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57