#!d:\perli\bin\perl -w # # UDP example of a listening socket # Listens to socket and connetcs a TCP socket, # TCP connects # Allows two Telenet sessions to connect. # Copies one TCP/IP connection to the other. # # #!/usr/bin/perl -w use strict; use Socket; use Sys::Hostname; # require "ioctl.ph"; my ( $count, $hisiaddr, $hispaddr, $histime, $host, $iaddr, $paddr, $port, $proto, $rin, $rout, $rtime, $SECS_of_70_YEARS); my ( $paddr2, $proto_tcp ); open CLIENT,"null"; close CLIENT; $SECS_of_70_YEARS = 2208988800; # $iaddr = gethostbyname(hostname()); $iaddr = gethostbyname('127.0.0.1'); $proto = getprotobyname('udp'); $proto_tcp = getprotobyname('tcp'); $port = getservbyname('time', 'udp'); $port = shift || 2346; $paddr = sockaddr_in($port,inet_aton("127.0.0.1")); socket(SOCKET, PF_INET, SOCK_DGRAM, $proto) || die "socket: $!"; bind(SOCKET, $paddr) || die "bind: $!"; #Set up a socket to listen for a Telnet session. $paddr2 = sockaddr_in(80,inet_aton("127.0.0.1")); socket(TELNET2, PF_INET, SOCK_STREAM, $proto_tcp) || die "socket: $!"; setsockopt(TELNET2, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!"; bind(TELNET2, $paddr2) || die "bind: $!"; listen(TELNET2,SOMAXCONN); $| = 1; printf "%-12s %8s %s\n", "localhost", 0, scalar localtime time; $count = 0; while( 1 ) { $rin = ''; vec($rin, fileno(SOCKET), 1) = 1; vec($rin, fileno(TELNET2), 1) = 1; #vec($rin, fileno(STDIN), 1) = 1; # timeout after 10.0 seconds my $finished = 0; my $rout = $rin; my $icMessage; my ( $junk, $ts, $protocol, $OrigID, $DestID, $Operation, $p1, $p2, $p3, $p4, $p5 ); my $mask; while ( !$finished ) { $ts ++; $finished = 0; # $count = select( undef, undef, undef, 1 ) ; # Block until input or 1 second $count = select($rout = $rin, undef, undef, 5 ) ; #$count = select( undef, undef, undef, 5 ) ; print "\r\nWait For: ".unpack("b*",$rin)."\r\n"; print "\r\n ---------------------------------------------\r\n"; print "$ts $count ".unpack("b*",$rout)."\r\n"; my $icMessage = ' '; # event - is it TIMEOUT if( $count == 0 ){ # print " ---------------------------------------------\r\n"; print " Timeout\r\n\r\n\r\n"; } # event - is it TELNET2 if ( vec($rout, fileno(TELNET2), 1) == 1 ) { print " TELNET2\r\n\r\n\r\n"; my ( $paddr_telnet, $iaddr ); my($port,$iaddr) = sockaddr_in($paddr2); my $name = gethostbyaddr($iaddr,AF_INET); print "connection from $name [", inet_ntoa($iaddr), "] at port $port"; if( vec($rin, fileno(CLIENT), 1) != 1 ){ $paddr_telnet = accept(CLIENT,TELNET2); vec($rin, fileno(CLIENT), 1) = 1; } else { $paddr_telnet = accept(CLIENT2,TELNET2); vec($rin, fileno(CLIENT2), 1) = 1; } syswrite CLIENT, "You connected from $name [". inet_ntoa($iaddr). "] at port $port"; } ##print "+++ CLIENT closed\r\n\r\n\r\n" if ! defined fileno(CLIENT) ; if ( vec($rout, fileno(CLIENT), 1) == 1 ) { ##ioctl (CLIENT,FIONREAD,$count); print " CLIENT Activity there $count bytes to read \r\n\r\n\r\n"; my ( $ip ) ; $count = sysread CLIENT,$ip,10; if ($count > 0 ) { print "CLIENT: $ip"; my $EOL = "\015\012"; print CLIENT "You typed: $ip\r\n$EOL"; syswrite CLIENT, "syswrite: You typed: $ip\r\n$EOL"; ## may need to check if file is still open syswrite CLIENT2, "syswrite: You typed: $ip\r\n$EOL"; } else { # stop responding to events vec($rin, fileno(CLIENT), 1) = 0; print "CLIENT: No data- closing file"; close( CLIENT ); } } if ( vec($rout, fileno(CLIENT2), 1) == 1 ) { ##ioctl (CLIENT2,FIONREAD,$count); print " CLIENT2 Activity there $count bytes to read \r\n\r\n\r\n"; my ( $ip ) ; $count = sysread CLIENT2,$ip,10; if ($count > 0 ) { print "CLIENT2: $ip"; my $EOL = "\015\012"; print CLIENT2 "You typed: $ip\r\n$EOL"; syswrite CLIENT2, "syswrite: You typed: $ip\r\n$EOL"; ## may need to check if file is still open syswrite CLIENT, "syswrite: You typed: $ip\r\n$EOL"; } else { # stop responding to events vec($rin, fileno(CLIENT2), 1) = 0; print "CLIENT2: No data- closing file"; close( CLIENT2 ); } } # event - is it SOCKET if ( vec($rout, fileno(SOCKET), 1) == 1 ) { my $message = recv(SOCKET, $icMessage, 255, 0); #TS,Protocol,OrigID,DestID,Operation,P1,,,CRLF ( $junk,$ts,$protocol,$OrigID,$DestID,$Operation,$p1,$p2,$p3,$p4,$p5)=split(/,/,$icMessage); print "$icMessage\r\n"; print "TS: $ts,"; print "PROT: $protocol, TO: $OrigID, FROM: $DestID OPERATION: $Operation\n\r"; } } }