-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththrou.pl
executable file
·199 lines (188 loc) · 4.48 KB
/
throu.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#!/usr/local/bin/perl
#
# throu.pl
# all purpose delegate for TCP & UDP
#
use Socket;
############server functions#######################
sub setup_server($$$)
{
my($S,$port,$protostr)=@_;
my $pack=caller;
my($proto,$ent);
$S=$pack."::".$S;
$proto=getprotobyname($protostr);
$port=getservbyname($port,$protostr) unless $port =~ /^\d+$/;
if(!socket($S,PF_INET,SOCK_STREAM,$proto)){
$server_errmsg="socket:$!";
return 0;
}
$ent=sockaddr_in($port,INADDR_ANY);
if($protostr eq "tcp" && !bind($S,$ent)){$server_errmsg= "$!:bind";return 0;}
if(!listen($S,5)){$server_errmsg="$!:listen";return 0;}
my $oldfh=select($S);$|=1;select($oldfh);
#binmode($S);
return 1;
}
#sub setup_udp_server
#{
# local($S,$port)=@_;
#
#
# $server_errmsg="not implemented udp\n";
# return 0;
#}
#sub setup_server
#{
# local($S,$port,$sock_proto)=@_;
# if($sock_proto eq "tcp"){
# &setup_tcp_server($S,$port) || die "$server_errmsg:can't setupt tcp";
# }elsif($sock_proto eq "udp"){
# &setup_udp_server($S,$port)|| die "$server_errmsg:can't setup udp";
# }else{
# die "sock_proto must 'tcp' or 'udp'";
# }
#}
sub connect_to_tcp_client
{
my($S,$SOCK)=@_;
accept($S,$SOCK)||die "$!:can't accept";
my $oldfh=select($S);$|=1;select($oldfh);
#binmode($S);
return 1;
}
sub connect_to_udp_client
{
my($S,$SOCK)=@_;
}
sub connect_to_client($$$)
{
my($S,$SOCK,$proto)=@_;
if($proto eq "tcp"){
&connect_to_tcp_client($S,$SOCK);
}elsif($proto eq "udp"){
&connect_to_udp_client($S,$SOCK);
}else{
die "sock_proto must 'tcp' or 'udp'";
}
}
#################client functions#######################
sub setup_client($$)
{
my($S,$protostr)=@_;
my $pack=caller;
$S=$pack."::".$S;
my $proto=getprotobyname($protostr);
socket($S,PF_INET,SOCK_STREAM,$proto);
#binmode($S);
}
sub connect_to_server
{
my($S,$host,$port,$proto)=@_;
my $pack=caller;
$S=$pack."::".$S;
$port=getservbyname($port,$proto) unless $port =~ /^\d+$/;
$ent=sockaddr_in($port,inet_aton($host));
connect($S,$ent)||die "$!:connect";
# stop buffering
my $oldfh=select($S);$|=1;select($oldfh);
#binmode($S);
}
sub print_client_data($)
{
if(!$Quiet){
my($str)=@_;
print "FROM CLIENT:[$str][0x". unpack("H*",$str) ."]\n";
}
}
sub print_server_data($)
{
if(!$Quiet){
my($str)=@_;
#print "FROM SERVER:[$str]\n";
print "FROM SERVER:[$str][0x". unpack("H*",$str) ."]\n";
}
}
sub transfer_data
{
my($CLIENT,$SERVER,$proto)=@_;
my $datalen;
if($proto='tcp'){$datalen=1;}else{$datalen=10000;}
while(1){
my $read_bits='';
vec($read_bits,fileno($CLIENT),1)=1;
vec($read_bits,fileno($SERVER),1)=1;
select($read_bits,undef,undef,undef);
if(vec($read_bits,fileno($CLIENT),1)){
sysread($CLIENT,$str,$datalen) || die "$!:read client";
print $SERVER $str;
&print_client_data($str);
}elsif(vec($read_bits,fileno($SERVER),1)){
sysread($SERVER,$str,$datalen)||die "$!:can't read server";
print $CLIENT $str;
&print_server_data($str);
}
}
}
sub usage
{
print "AAAAAA\n";
print STDERR <<_EOT_
usage: $0 -FromHost=<hostname> -FromPort=<port num> -ToHost=<hostname> -ToPort=<port number> [-q]
_EOT_
}
#########################
#####main###############
if(__FILE__ eq $0){
while($_=$ARGV[0]){
if(/^-FromHost=(\S+)$/){
$FromHost=$1;
}elsif(/^-ToHost=(\S+)$/){
$ToHost=$1;
}elsif(/^-FromPort=(\d+)$/){
$FromPort=$1;
}elsif(/^-ToPort=(\d+)$/){
$ToPort=$1;
}elsif(/^-Proto=(\S+)$/){
$Proto=$1;
}elsif(/^-q$/){
$Quiet=1;
}else{
&usage();
exit(1);
}
shift;
}
$FromHost= $FromHost || "localhost";
$ToHost=$ToHost || "localhost";
$FromPort= $FromPort || 4400;
$ToPort=$ToPort || 4500;
$Proto = $Proto || 'tcp';
print "From [$FromHost:$FromPort] To [$ToHost:$ToPort] proto=$Proto\n";
$SIG{CHLD}=sub{wait};
$sockcount=0;
&setup_server('SOCK',$FromPort,$Proto);
print "server setupped\n";
$CLIENT='CLIENT0001';
$SERVER='SOCKET0001';
while(1){
print "connecting...\n";
&connect_to_client($CLIENT,'SOCK',$Proto);
print "conneted!\n";
if(($pid=fork())<0){
die "$!:can't fork";
}elsif($pid==0){
&setup_client($SERVER,'tcp');
&connect_to_server($SERVER,$ToHost,$ToPort,$Proto);
&transfer_data($CLIENT,$SERVER,$Proto);
close($CLIENT);
if($Proto eq 'tcp'){close($SERVER);}
close('SOCK');
exit(0);
}else{
close($CLIENT);
}
$SERVER++;
$CLIENT++;
}
}