help with IO::Socket:INET

Discussion in 'Perl Misc' started by me, Mar 26, 2009.

  1. me

    me Guest

    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?
     
    me, Mar 26, 2009
    #1
    1. Advertising

  2. me

    smallpond Guest

    On Mar 26, 9:58 am, me <> 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.
     
    smallpond, Mar 26, 2009
    #2
    1. Advertising

  3. On Mar 26, 10:28 am, Ben Morrow <> wrote:
    > Quoth 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).

    >
    > 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?
     
    deja_vu_was_better, Mar 26, 2009
    #3
  4. deja_vu_was_better <> wrote in news:3c0bed90-52d8-478a-
    :

    > $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/
     
    A. Sinan Unur, Mar 26, 2009
    #4
  5. 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/"
     
    Tad J McClellan, Mar 27, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rad
    Replies:
    0
    Views:
    559
  2. Aggro

    Re: INET socket programming questions

    Aggro, Jun 28, 2003, in forum: C Programming
    Replies:
    1
    Views:
    1,220
    Artemio
    Jun 29, 2003
  3. Sébastien Cottalorda

    Pb with IO::Socket::INET and recv

    Sébastien Cottalorda, Sep 5, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    269
    Sébastien Cottalorda
    Sep 16, 2003
  4. Kai Bleek
    Replies:
    7
    Views:
    366
    Kai Bleek
    Oct 31, 2003
  5. Joe
    Replies:
    10
    Views:
    289
Loading...

Share This Page