"Fossies" - the Fresh Open Source Software Archive 
Member "portfwd-0.29/tools/sample-director.pl" (8 May 2002, 1366 Bytes) of package /linux/privat/old/portfwd-0.29.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Perl source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 #! /usr/bin/perl -w
2 #
3 # $Id: sample-director.pl,v 1.5 2002/05/08 03:50:03 evertonm Exp $
4 #
5 # sample-director.pl remote_port_number
6
7 #
8 # As example, we've chosen to receive the remote
9 # port number as command line argument.
10 #
11 my $remote_port_number = $ARGV[0];
12 $remote_port_number = 2000 unless defined($remote_port_number);
13
14 #
15 # Enable flushing
16 #
17 $| = 1;
18
19 sub log {
20 my $line = $_[0];
21 system("echo `date` [$$] $line >> /tmp/sample-director-`date +%Y%m%d`.log");
22 }
23
24 sub response {
25 my $line = $_[0];
26
27 &log("OUT: $line");
28
29 print $line, "\n";
30 }
31
32 sub catch_term {
33 my $signame = shift;
34 &log('Received SIGTERM - exiting');
35 exit(0);
36 }
37 $SIG{TERM} = \&catch_term;
38
39 &log('Start');
40
41 #
42 # Infinite loop
43 #
44 while (<STDIN>) {
45 chomp;
46
47 &log(" IN: $_");
48
49 #
50 # Identify incoming connection
51 #
52 # Client from "source_address:source_port" is
53 # opening communication to "local_address:local_port"
54 # with "protocol".
55 #
56 my ($protocol, $source_address, $source_port, $local_address, $local_port) = split;
57
58 #
59 # Reject connections other than from localhost
60 #
61 if ($source_address ne '127.0.0.1') {
62 &response("reject");
63 next;
64 }
65
66 #
67 # Forward connections to localhost:$remote_port_number
68 #
69 &response("forward localhost $remote_port_number");
70 }
71
72 &log("Can't read from stdin - exiting");
73