help with IO::Socket:INET

M

me

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?
 
S

smallpond

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.
 
D

deja_vu_was_better

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.



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
    }



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?
 
A

A. Sinan Unur

$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 <[email protected]>
(remove .invalid and reverse each component for email address)

comp.lang.perl.misc guidelines on the WWW:
http://www.rehabitation.com/clpmisc/
 
T

Tad J McClellan

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";
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top