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

Hello,

I have already posted it to comp.lang.perl.misc. Reposting here as it is, probably for a wider audience.

-----------------------------------------

I am searching for a port forwarding package in perl, some thing like plugproxy in java(http://www.bbzzdd.com/plugproxy). My basic requirement is to have a platform independent snooping tool between two servers. The servers may be on the same m/c. So we need to replace currently used ipthreal/iptrace.

I was wondering if there exists any package with functionality similar to plugproxy. IPChains::PortFW seemed promising. But I couldn't install it on Solaris. The documentation also clearly mentions it's for linux.

In perl cook-book first edition chapter 17.18 there is a program which probably would serve our purpose with some modification. Another option is to make a perl wrapper over plugproxy and use it.

Could somebody point me to a module already exists or being writen which fits into this before we reinvent the wheel?

Thanks and regards,

Debashish.

Replies are listed 'Best First'.
Re: Port forwarding utility in perl
by Corion (Patriarch) on Jun 16, 2004 at 12:55 UTC
Re: Port forwarding utility in perl
by borisz (Canon) on Jun 16, 2004 at 12:47 UTC
Re: Port forwarding utility in perl
by Thelonius (Priest) on Jun 16, 2004 at 14:39 UTC
    The plugproxy web page isn't working right now, so I'm not sure what functionality it offers. Here is my own very simple utility that does TCP port forwarding, but it doesn't have any logging or tracing features. But it should be easy enough to add.
    #!perl -w # usage: portforward configfile # configfile has lines like this: #18025 mail.messagingengine.com:25 #18110 mail.messagingengine.com:110 #where 18025 is the TCP port on the local machine and # mail.messagingengine.com:25 #is where that port is forwarded to. use Socket; use IO::Socket; use IO::Select; use strict; $| = 1; my %ports; my $listen_set = IO::Select->new(); $SIG{CHLD} = 'IGNORE'; while (<>) { chomp; my ($localport, $remotehost) = split; $ports{$localport} = $remotehost; print "config $localport -> $remotehost\n"; my $socklisten = IO::Socket::INET->new(LocalPort => $localport, Listen => 2, Reuse => 1, Proto => 'tcp') or die "Cannot open sock on $localport: $!\n" +; $listen_set->add($socklisten); } my @ready; print "Parent ready to accept\n"; while (1) { @ready = $listen_set->can_read; for my $socklisten (@ready) { my $socklocal = $socklisten->accept; if (defined $socklocal) { my ($port, $myaddr) = sockaddr_in(getsockname($socklisten)); print "accepted on $port\n"; my $remotehost = $ports{$port}; if (! defined($remotehost)) { print "Internal error on port $port\n"; die; } if (fork()) { close($socklocal); } else { close($socklisten); my $sockremote = IO::Socket::INET->new( Proto => "tcp", PeerAddr => "$remotehost", Timeout => 30, ) or die "cannot create socketremote($remotehost): $!\n"; my $buf= ' 'x4096; if (fork()) { my $sent = 0; while (sysread($socklocal, $buf, 4096)) { print $sockremote $buf; $sent += length($buf); } # print "Total bytes sent $sent\n"; } else { my $rcvd = 0; while(sysread($sockremote, $buf, 4096)) { print $socklocal $buf; $rcvd += length($buf); } # print "Total bytes rcvd: $rcvd\n"; } exit(0); } } } } print "End of parent: $!\n"; __END__
Re: Port forwarding utility in perl (socat)
by grinder (Bishop) on Jun 16, 2004 at 13:24 UTC
    I am searching for a port forwarding package in perl

    Why Perl? (When all you have is a hammer, all your problems start to look like nails).

    My basic requirement is to have a platform independent snooping tool between two servers

    It sounds to me you want something like Socat. (The site seems to be down at the moment, although it does rate a mention on Freshmeat). Otherwise search around on the web.

    socat will do all you want and more. Take the time to learn how to use it, and it will reward you greatly.

    - another intruder with the mooring of the heat of the Perl

Re: Port forwarding utility in perl
by iburrell (Chaplain) on Jun 16, 2004 at 17:34 UTC
    Do you want a TCP proxy or a network sniffer? They are different tools. A TCP proxy in Perl is easy; Net::ProxyMod looks like one. There also seems to be Net::Diver for UDP packets. A firewall can turn an explicit proxy into a transparent one by forwarding connections to the proxy.

    I don't know of any network sniffers written in Perl. It should be possible to use libpcap through Net::Pcap. But ethereal or tcpdump would work better.

      Hi,

      I'm looking for something similar -- I need to see the queries going out from an LDAP client on Windows, to an LDAP server (Oracle OID), and want to use port-fowarding in Perl.

      I tried the various examples, but none worked -- in every case, the Perl script just blocked -- it never saw the end-of-transmission and somehow just blocked on the send or receive.

      Will this simply not work on Windows? Is there some alternative?

      Cheers,

      dehansen