blib/lib/NET/MitM.pm

filter: Statementstmt 31.8% 102 / 320 Branchbran 14.1% 24 / 170 Conditioncond 2.9% 1 / 34 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 51.1% 23 / 45 Podpod 100% 24 / 24 totaltotal 28.2% 174 / 615 CC 102 SCAR 50.4
CC
102
Cov
24%
_main_loop
756.0
_spawn_child
72.0
_cull_child
42.0
CRAP 154.1
↑ Summary
1 package NET::MitM;
2  
3 =head1 NAME
4  
5 NET::MitM - Man in the Middle - connects a client and a server, giving visibility of and control over messages passed.
6  
7 =head1 VERSION
8  
9 Version 0.02
10  
11 =cut
12  
13 4 our $VERSION = '0.02';
14  
15 =head1 SYNOPSIS
16  
17 NET::MitM is designed to be inserted between a client and a server. It proxies all traffic through verbatum, and also copies that same data to a log file and/or a callback function, allowing a data session to be monitored, recorded, even altered on the fly.
18  
19 MitM acts as a 'man in the middle', sitting between the client and server. To the client, MitM looks like the server. To the server, MitM looks like the client.
20  
21 MitM cannot be used to covertly operate on unsuspecting client/server sessions - it requires that you control either the client or the server. If you control the client, you can tell it to connect via your MitM. If you control the server, you can move it to a different port, and put a MitM in its place.
22  
23 When started, MitM opens a socket and listens for connections. When that socket is connected to, MitM opens another connection to the server. Messages from either client or server are passed to the other, and a copy of each message is, potentially, logged. Alternately, callback methods may be used to add business logic, including potentially altering the messages being passed.
24  
25 MitM can also be used as a proxy to allow two processes on machines that cannot 'see' each other to communicate via an intermediary machine that is visible to both.
26  
27 There is an (as yet unreleased) sister module L<NET::Replay> that allows a MitM session to be replayed.
28  
29 =head3 Usage
30  
31 Assume the following script is running on the local machine:
32  
33 use NET::MitM;
34 my $MitM = NET::MitM->new("cpan.org", 80, 10080);
35 $MitM->log_file("MitM.log");
36 $MitM->go();
37  
38 A browser connecting to L<http://localhost:10080> will now cause MitM to open a connection to cpan.org, and messages sent by either end will be passed to the other end, and logged to MitM.log.
39  
40 For another example, see samples/mitm.pl in the MitM distribution.
41  
42 =head3 Modifying messages on the fly.
43  
44 However you deploy MitM, it will be virtually identical to having the client and server talk directly. The difference will be that either the client and/or server will be at an address other than the one its counterpart believes it to be at. Most programs ignore this, but sometimes it matters.
45  
46 For example, HTTP browsers pass a number of parameters, one of which is "Host", the host to which the browser believes it is connecting. Often, this parameter is unused. But sometimes, a single HTTP server will be serving content for more than one website. Such a server generally relies on the Host parameter to know what it is to return. If the MitM is not on the same host as the HTTP server, the host parameter that the browser passes will cause the HTTP server to fail to serve the desired pages.
47  
48 Further, HTTP servers typically return URLs containing the host address. If the browser navigates to a returned URL, it will from that point onwards connect directly to the server in the URL instead of communicating via MitM.
49  
50 Both of these problems can be worked around by modifying the messages being passed.
51  
52 For example, assume the following script is running on the local machine:
53  
54 use NET::MitM;
55 sub send_($) {$_[0] =~ s/Host: .*:\d+/Host: cpan.org/;}
56 sub receive($) {$_[0] =~ s/cpan.org:\d+/localhost:10080/g;}
57 my $MitM = NET::MitM->new("cpan.org", 80, 10080);
58 $MitM->client_to_server_callback(\&send);
59 $MitM->server_to_client_callback(\&receive);
60 $MitM->log_file("http_MitM.log");
61 $MitM->go();
62  
63 The send callback tells the server that it is to serve cpan.org pages, instead of some other set of pages, while the receive callback tells the browser to access cpan.org URLs via the MitM process, not directly. The HTTP server will now respond properly, even though the browser sent the wrong hostname, and the browser will now behave as desired and direct future requests via the MitM.
64  
65 For another example, see samples/http_mitm.pl in the MitM distribution.
66  
67 A more difficult problem is security aware processes, such as those that use HTTPS based protocols. They are actively hostname aware. Precisely to defend against a man-in-the-middle attack, they check their counterpart's reported hostname (but not normally the port) against the actual hostname. Unless client, server and MitM are all on the same host, either the client or the server will notice that the remote hostname is not what it should be, and will abort the connection.
68 There is no good workaround for this, unless you can run an instance of MitM on the server, and another on the client - but even if you do, you still have to deal with the communication being encrypted.
69  
70 =head1 SUBROUTINES/METHODS
71  
72 =cut
73  
74 # #######
75 # Globals
76 # #######
77  
78 4 ▶use 5.002;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 4 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
80 4 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
81 4 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 4 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 4 ▶eval {use Time::HiRes qw(time)}; # only needed for high precision time_interval - will still work fine even if missing
Subroutine BEGIN: called CC=1 SCAR=0.0
86  
87 4 my $protocol = getprotobyname('tcp'); # TODO: make dynamic?
88  
89 =head2 new( remote_ip_address, local_port_num, remote_port_num )
90  
91 Creates a new MitM
92  
93 =head4 Parameters
94  
95 =over
96  
97 =item * remote_ip_address - the remote hostname/IP address of the server
98  
99 =item * remote_port_num - the remote port number of the server
100  
101 =item * local_port_num - the port number to listen on
102  
103 =item * Returns - a new MitM object
104  
105 =back
106  
107 =head4 Usage
108  
109 To keep a record of all messages sent:
110  
111 use NET::MitM;
112 my $MitM = NET::MitM->new("www.cpan.org", 80, 10080);
113 $MitM->log_file("MitM.log");
114 $MitM->go();
115  
116 =cut
117  
118 sub hhmmss();
119  
120 4 my $mitm_count=0;
121  
122 5 ▶sub _new(){
Subroutine _new: called CC=1 SCAR=0.0
123 5 my %this;
124 5 $this{verbose} = 1;
125 5 $this{parallel} = 0;
126 5 $this{mydate} = \&hhmmss;
127 5 $this{name} = "MitM".++$mitm_count;
128 5 return \%this;
129 }
130  
131 0 ▶sub new($$;$$$) {
Subroutine new: not called CC=4 SCAR=30.0
Pod documented
132 0 my $class=shift;
133 0 my $this=_new();
134 0 ▶ $this->{remote_ip_address} = shift or croak "remote hostname/ip address missing";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"remote_ip_address"} = shift() or croak("remote hostname/ip address missing") 0 0 0
135 0 ▶ $this->{remote_port_num} = shift or croak "remote port number missing";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"remote_port_num"} = shift() or croak("remote port number missing") 0 0 0
136 0 ▶ $this->{local_port_num} = shift || $this->{remote_port_num};
Condition0%0/3
shift() || $this->{'remote_port_num'}
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $this->{'remote_port_num'}
shift() $this->{'remote_port_num'}
Truth table0%0/3
A || B
A= shift()B= $this->{'remote_port_num'}
ABresult
1X1
011
000
137 0 return bless($this, $class);
138 }
139  
140 =head2 go( )
141  
142 Listen on local_port, accept incoming connections, and forwards messages back and forth.
143  
144 =head4 Parameters
145  
146 =over
147  
148 =item * --none--
149  
150 =item * Returns --none--
151  
152 =back
153  
154 =head4 Usage
155  
156 When a connection on local_port is received a connect to remote_ip_address:remote_port is created and messages from the client are passed to the server and vice-versa.
157  
158 If parallel() was set, which is not the default, there will be a new process created for each such session.
159  
160 If any callback functions have been set, they will be called before each message is passed.
161 If logging is on, messages will be logged.
162  
163 go() does not return. You may want to L<fork> before calling it. There is no way to stop it from outside except using a signal to interrupt it. This will probably change in a future release of MitM.
164  
165 If new_server() was used instead of new(), messages from client are instead passed to the server callback function.
166  
167 =cut
168  
169 # Convenience function - intentionally not exposed. If you really want to call it, you can of course. But if you are going to violate encapsulation, why not go directly to the variables?
170  
171 7 ▶sub _set($;$) {
Subroutine _set: called CC=3 SCAR=11.0
172 7 my $this=shift;
173 7 ▶ my $key=shift or confess "missing mandatory parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $key = shift() or confess("missing mandatory parameter") 7 0 7
174 7 my $value=shift;
175 7 ▶ if(defined $value){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 5 2 7
176 5 $this->{$key} = $value;
177 }
178 7 return $this->{$key};
179 }
180  
181 =head2 name( [name] )
182  
183 Names the object - will be reported back in logging/debug
184  
185 =head4 Parameters
186  
187 =over
188  
189 =item * name - the new name (default is MitM1, MitM2, ...)
190  
191 =item * Returns - the current or new setting
192  
193 =back
194  
195 =head4 Usage
196  
197 For a minimal MitM:
198  
199 use NET::MitM;
200 my $MitM = NET::MitM->new("www.cpan.org", 80, 10080);
201 $MitM->go();
202  
203 =cut
204  
205 7 ▶sub name(;$) {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
206 7 my $this=shift;
207 7 my $value=shift;
208 7 return $this->_set("name", $value);
209 }
210  
211 =head2 verbose( [level] )
212  
213 Turns on/off reporting to stdout.
214  
215 =head4 Parameters
216  
217 =over
218  
219 =item * level - how verbose to be. 0=nothing, 1=normal, 2=debug. The default is 1.
220  
221 =item * Returns - the current or new setting
222  
223 =back
224  
225 =head4 Usage
226  
227 Setting verbose changes the amount of information printed to stdout.
228  
229 =cut
230  
231 0 ▶sub verbose(;$) {
Subroutine verbose: not called CC=1 SCAR=6.9
Pod documented
232 0 my $this=shift;
233 0 my $verbose=shift;
234 #warn "verbose->(",$verbose||"--undef--",")\n";
235 0 return $this->_set("verbose", $verbose);
236 }
237  
238 =head2 client_to_server_callback( callback )
239  
240 Set a callback function to monitor/modify each message sent to server
241  
242 =head4 Parameters
243  
244 =over
245  
246 =item * callback - a reference to a function to be called for each message sent to server
247  
248 =item * Returns - the current or new setting
249  
250 =back
251  
252 =head4 Usage
253  
254 If client_to_server_callback is set, it will be called with a copy of each message to the server before it is sent. Whatever the callback returns will be sent.
255  
256 For example, to modify messages:
257  
258 use NET::MitM;
259 sub send_($) {$_[0] =~ s/Host: .*:\d+/Host: cpan.org/;}
260 sub receive($) {$_[0] =~ s/www.cpan.org(:\d+)?/localhost:10080/g;}
261 my $MitM = NET::MitM->new("www.cpan.org", 80, 10080);
262 $MitM->client_to_server_callback(\&send);
263 $MitM->server_to_client_callback(\&receive);
264 $MitM->go();
265  
266 If the callback is readonly, it should either return a copy of the original message, or undef. Be careful not to accidentally return something else - remember that perl methods implicitly returns the value of the last command executed.
267  
268 For example, to write messages to a log:
269  
270 sub peek($) {my $msg = shift; print LOG; return $msg;}
271 my $MitM = NET::MitM->new("www.cpan.org", 80, 10080);
272 $MitM->client_to_server_callback(\&peek);
273 $MitM->server_to_client_callback(\&peek);
274 $MitM->go();
275  
276 This would also work:
277 sub peek($) {my $msg = shift; print LOG; return undef;}
278 ...
279  
280 But this is unlikely to do what you would want:
281 sub peek($) {my $msg = shift; print LOG}
282 ...
283  
284 =cut
285  
286 0 ▶sub client_to_server_callback(;$) {
Subroutine client_to_server_callback: not called CC=1 SCAR=6.9
Pod documented
287 0 my $this=shift;
288 0 my $callback=shift;
289 0 return $this->_set("client_to_server_callback", $callback);
290 }
291  
292 =head2 server_to_client_callback( [callback] )
293  
294 Set a callback function to monitor/modify each message received from server
295  
296 =head4 Parameters
297  
298 =over
299  
300 =item * callback - a reference to a function to be called for each inward message
301  
302 =item * Returns - the current or new setting
303  
304 =back
305  
306 =head4 Usage
307  
308 If server_to_client_callback is set, it will be called with a copy of each message received from the server before it is sent to the client. Whatever the callback returns will be sent.
309  
310 If the callback is readonly, it should either return a copy of the original message, or undef. Be careful not to accidentally return something else - remember that perl methods implicitly returns the value of the last command executed.
311  
312 =cut
313  
314 0 ▶sub server_to_client_callback(;$) {
Subroutine server_to_client_callback: not called CC=1 SCAR=6.9
Pod documented
315 0 my $this=shift;
316 0 my $callback=shift;
317 0 return $this->_set("server_to_client_callback", $callback);
318 }
319  
320 =head2 timer_callback( [interval, callback] )
321  
322 Set a callback function to be called at regular intervals
323  
324 =head4 Parameters
325  
326 =over
327  
328 =item * interval - how often the callback function is to be called - must be > 0 seconds, may be fractional
329 =item * callback - a reference to a function to be called every interval seconds
330  
331 =item * Returns - the current or new setting, as an array
332  
333 =back
334  
335 =head4 Usage
336  
337 If the callback is set, it will be called every interval seconds. Interval must be > 0 seconds. It may be fractional. If interval is passed as 0 it will be reset to 1 second. This is to prevent accidental spin-wait. If you really want to spin-wait, pass an extremely small but non-zero interval.
338  
339 If the callback returns false, mainloop will exit and return control to the caller.
340  
341 The time spent in callbacks is not additional to the specified interval - the timer callback will be called every interval seconds, or as close as possible to every interval seconds.
342  
343 Please remember that if you have called fork before calling go() that the timer_callback method will be executed in a different process to the parent - the two processes will need to use some form of L<IPC> to communicate.
344  
345 =cut
346  
347 0 ▶sub timer_callback(;$) {
Subroutine timer_callback: not called CC=3 SCAR=24.8
Pod documented
348 0 my $this=shift;
349 0 my $interval=shift;
350 0 my $callback=shift;
351 0 ▶ if(defined $interval && $interval==0){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $interval and $interval == 0) 0 0 0
Condition0%0/3
defined $interval and $interval == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $interval and $interval == 0
defined $interval $interval == 0
Truth table0%0/3
A and B
A= defined $intervalB= $interval == 0
ABresult
0X0
100
111
352 0 $interval=1;
353 }
354 0 $interval=$this->_set("timer_interval", $interval);
355 0 $callback=$this->_set("timer_callback", $callback);
356 0 return ($interval, $callback);
357 }
358  
359 =head2 parallel( [level] )
360  
361 Turns on/off running in parallel.
362  
363 =head4 Parameters
364  
365 =over
366  
367 =item * level - 0=serial, 1=parallel. Default is 0 (run in serial).
368  
369 =item * Returns - the current or new setting
370  
371 =back
372  
373 =head4 Usage
374  
375 If running in parallel, MitM starts a new process for each new connection using L<fork>.
376  
377 Running in serial still allows multiple clients to run concurrently, as so long as none of them have long-running callbacks. If they do, they will block other clients from sending/recieving.
378  
379 =cut
380  
381 0 ▶sub parallel(;$) {
Subroutine parallel: not called CC=2 SCAR=17.9
Pod documented
382 0 my $this=shift;
383 0 my $parallel=shift;
384 0 ▶ if($parallel){
Branch: 1 branch
BranchTrueFalseTotal
if ($parallel) 0 0 0
385 0 $SIG{CLD} = "IGNORE";
386 }
387 0 return $this->_set("parallel", $parallel);
388 }
389  
390 =head2 serial( [level] )
391  
392 Turns on/off running in serial
393  
394 =head4 Parameters
395  
396 =over
397  
398 =item * level - 0=parallel, 1=serial. Default is 1, i.e. run in serial.
399  
400 =item * Returns - the current or new setting
401  
402 =back
403  
404 =head4 Usage
405  
406 Calling this function with level=$l is exactly equivalent to calling parallel with level=!$l.
407  
408 If running in parallel, MitM starts a new process for each new connection using L<fork>.
409  
410 Running in serial, which is the default, still allows multiple clients to run concurrently, as so long as none of them have long-running callbacks. If they do, they will block other clients from sending/recieving.
411  
412 =cut
413  
414 0 ▶sub serial(;$) {
Subroutine serial: not called CC=3 SCAR=24.8
Pod documented
415 0 my $this=shift;
416 0 my $level=shift;
417 0 ▶ my $parallel = $this->parallel(defined $level ? ! $level : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $level ? : 0 0 0
418 0 ▶ return $parallel ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$parallel ? : 0 0 0
419 }
420  
421 =head2 log_file( [log_file_name] ] )
422  
423 log_file() sets, or clears, a log file.
424  
425 =head4 Parameters
426  
427 =over
428  
429 =item * log_file_name - the name of the log file to be appended to. Passing "" disables logging. Passing nothing, or undef, returns the current log filename without change.
430  
431 =item * Returns - log file name
432  
433 =back
434  
435 =head4 Usage
436  
437 The log file contains a record of connects and disconnects and messages as sent back and forwards. Log entries are timestamped. If the log file already exists, it is appended to.
438  
439 The default timestamp is "hh:mm:ss", eg 19:49:43 - see mydate() and hhmmss().
440  
441 =cut
442  
443 1 ▶sub log_file(;$) {
Subroutine log_file: called CC=9 SCAR=29.5
Pod documented
444 1 my $this=shift;
445 1 my $new_log_file=shift;
446 1 ▶ if(defined $new_log_file){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_log_file) 1 0 1
447 1 ▶ if(!$new_log_file){
Branch: 1 branch
BranchTrueFalseTotal
if (not $new_log_file) { } 0 1 1
448 0 ▶ if($this->{LOGFILE}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LOGFILE"}) 0 0 0
449 0 close($this->{LOGFILE});
450 0 $this->{log_file}=$this->{LOGFILE}=undef;
451 0 ▶ print "Logging turned off\n" if $this->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} 0 0 0
452 }
453 }else{
454 1 my $LOGFILE;
455 1 ▶ if( open($LOGFILE, ">>$new_log_file") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $LOGFILE, ">>$new_log_file") { } 1 0 1
456 1 binmode($LOGFILE);
457 1 $LOGFILE->autoflush(1); # TODO make this configurable?
458 1 $this->{log_file}=$new_log_file;
459 1 $this->{LOGFILE}=$LOGFILE;
460 }
461 else {
462 0 ▶ print "Failed to open $new_log_file for logging: $!" if $this->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} 0 0 0
463 }
464 1 ▶ print "Logging to $this->{log_file}\n" if $this->{verbose} && $this->{log_file};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} and $this->{"log_file"} 1 0 1
Condition33%1/3
$this->{"verbose"} and $this->{"log_file"}
!ll&&!rl&&r
001
MC/DC0%0/2
$this->{"verbose"} and $this->{"log_file"}
$this->{"verbose"} $this->{"log_file"}
Truth table33%1/3
A and B
A= $this->{"verbose"}B= $this->{"log_file"}
ABresult
0X0
100
111
465 }
466 }
467 1 return $this->{log_file};
468 }
469  
470 =head2 defrag_delay( [delay] )
471  
472 Use a small delay to defragment messages
473  
474 =head4 Parameters
475  
476 =over
477  
478 =item * Delay - seconds to wait - fractions of a second are OK
479  
480 =item * Returns - the current setting.
481  
482 =back
483  
484 =head4 Usage
485  
486 Under TCPIP, there is always a risk that large messages will be fragmented in transit, and that messages sent close together may be concatenated.
487  
488 Client/Server programs have to know how to turn a stream of bytes into the messages they care about, either by repeatedly reading until they see an end-of-message (defragmenting), or by splitting the bytes read into multiple messages (deconcatenating).
489  
490 For our purposes, fragmentation and concatenation can make our logs harder to read.
491  
492 Without knowning the protocol, it's not possible to tell for sure if a message has been fragmented or concatenated.
493  
494 A small delay can be used as a way of defragmenting messages, although it increases the risk that separate messages may be concatenated.
495  
496 Eg:
497 $MitM->defrag_delay( 0.1 );
498  
499 =cut
500  
501 0 ▶sub defrag_delay(;$) {
Subroutine defrag_delay: not called CC=1 SCAR=6.9
Pod documented
502 0 my $this=shift;
503 0 my $defrag_delay=shift;
504 0 return $this->_set("defrag_delays",$defrag_delay);
505 }
506  
507 =head1 SUPPORTING SUBROUTINES/METHODS
508  
509 The remaining functions are supplimentary. new_server() and new_client() provide a simple client and a simple server that may be useful in some circumstances. The other methods are only likely to be useful if you choose to bypass go() in order to, for example, have more control over messages being received and sent.
510  
511 =head2 new_server( local_port_num, callback_function )
512  
513 Returns a very simple server, adequate for simple tasks.
514  
515 =head4 Parameters
516  
517 =over
518  
519 =item * local_port_num - the Port number to listen on
520  
521 =item * callback_function - a reference to a function to be called when a message arrives - must return a response which will be returned to the client
522  
523 =item * Returns - a new server
524  
525 =back
526  
527 =head4 Usage
528  
529 sub do_something($){
530 my $in = shift;
531 my $out = ...
532 return $out;
533 }
534  
535 my $server = NET::MitM::new_server(8080,\&do_something) || die;
536 $server->go();
537
538 The server returned by new_server has a method, go(), which tells it to start receiving messages (arbitrary strings). Each string is passed to the callback_function, which is expected to return a single string, being the response to be returned to caller. If the callback returns undef, the original message will be echoed back to the client.
539  
540 go() does not return. You may want to L<fork> before calling it.
541  
542 See, for another example, samples/echo_server.pl in the MitM distribution.
543  
544 =cut
545  
546 3 ▶sub new_server($%) {
Subroutine new_server: called CC=3 SCAR=11.3
Pod documented
547 3 my $class=shift;
548 3 my $this=_new();
549 3 ▶ $this->{local_port_num} = shift or croak "no port number passed";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"local_port_num"} = shift() or croak("no port number passed") 3 0 3
550 3 ▶ $this->{server_callback} = shift or croak "no callback passed";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"server_callback"} = shift() or croak("no callback passed") 3 0 3
551 3 return bless $this;
552 }
553  
554 =head2 new_client( remote_host, local_port_num )
555  
556 new client returns a very simple client, adequate for simple tasks
557  
558 The server returned has a method, send_and_receive(), which sends a message and receives a response.
559  
560 Alternately, send_to_server() may be used to send a message, and read_from_server() may be used to receive a message.
561  
562 Explicitly calling connect_to_server() is optional, but may be useful if you want to be sure the server is reachable. If you don't call it explicitly, it will be called the first time a message is sent.
563  
564 =head4 Parameters
565  
566 =over
567  
568 =item * remote_ip_address - the hostname/IP address of the server
569  
570 =item * remote_port_num - the Port number of the server
571  
572 =item * Returns - a new client object
573  
574 =back
575  
576 =head4 Usage
577  
578 my $client = NET::MitM::new_client("localhost", 8080) || die("failed to start test client: $!");
579 $client->connect_to_server();
580 my $resp = $client->send_and_receive("hello");
581 ...
582  
583 See, for example, samples/client.pl or samples/clients.pl in the MitM distribution.
584  
585 =cut
586  
587 1 ▶sub new_client($%) {
Subroutine new_client: called CC=3 SCAR=11.3
Pod documented
588 1 my $class=shift;
589 1 my $this=_new();
590 1 ▶ $this->{remote_ip_address} = shift or croak "remote hostname/ip address missing";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"remote_ip_address"} = shift() or croak("remote hostname/ip address missing") 1 0 1
591 1 ▶ $this->{remote_port_num} = shift or croak "remote port number missing";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"remote_port_num"} = shift() or croak("remote port number missing") 1 0 1
592 1 return bless $this;
593 }
594  
595 =head2 log( string )
596  
597 log is a convenience function that prefixes output with a timestamp and PID information then writes to log_file.
598  
599 =head4 Parameters
600  
601 =over
602  
603 =item * string(s) - one or more strings to be logged
604  
605 =item * Returns --none--
606  
607 =back
608  
609 =head4 Usage
610  
611 log is a convenience function that prefixes output with a timestamp and PID information then writes to log_file.
612  
613 log() does nothing unless log_file is set, which by default, it is not.
614  
615 =cut
616  
617 sub log($@)
618 1 ▶{
Subroutine log: called CC=2 SCAR=7.0
Pod documented
619 1 my $this=shift;
620 1 ▶ printf {$this->{LOGFILE}} "%u/%s %s\n", $$, $this->{mydate}(), "@_" if $this->{LOGFILE};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"LOGFILE"} 1 0 1
621 1 return undef;
622 }
623  
624 =head2 echo( string(s) )
625  
626 Prints to stdout and/or the logfile
627  
628 =head4 Parameters
629  
630 =over
631  
632 =item * string(s) - one or more strings to be echoed (printed)
633  
634 =item * Returns --none--
635  
636 =back
637  
638 =head4 Usage
639  
640 echo() is a convenience function that prefixes output with a timestamp and PID information and prints it to standard out if verbose is set and, if log_file() has been set, logs it to the log file.
641  
642 =cut
643  
644 sub echo($@)
645 1 ▶{
Subroutine echo: called CC=4 SCAR=15.8
Pod documented
646 1 my $this=shift;
647 1 $this->log("@_");
648 1 ▶ return if !$this->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"verbose"} 0 1 1
649 1 ▶ confess "Did not expect not to have a name" if !$this->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"name"} 0 1 1
650 1 ▶ if($_[0] =~ m/^[<>]{3}$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^[<>]{3}$/) { } 0 1 1
651 0 my $prefix=shift;
652 0 my $msg=join "", @_;
653 0 printf("%s: %u/%s %s %d bytes\n", $this->{name}, $$, $this->{mydate}(), $prefix, length($msg));
654 }else{
655 1 printf("%s: %u/%s\n", $this->{name}, $$, join(" ", $this->{mydate}(), @_));
656 }
657 1 return undef;
658 }
659  
660 =head2 send_to_server( string(s) )
661  
662 send_to_server() sends a message to the server
663  
664 =head4 Parameters
665  
666 =over
667  
668 =item * string(s) - one or more strings to be sent
669  
670 =item * Return: true if successful
671  
672 =back
673  
674 =head4 Usage
675  
676 If a callback is set, it will be called before the message is sent.
677  
678 send_to_server() may 'die' if it detects a failure to send.
679  
680 =cut
681  
682 sub _do_callback($$)
683 0 ▶{
Subroutine _do_callback: not called CC=3 SCAR=24.8
684 0 my $callback = shift;
685 0 my $msg = shift;
686 0 ▶ if($callback){
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) 0 0 0
687 0 my $new_msg = $callback->($msg);
688 0 ▶ $msg = $new_msg unless !defined $new_msg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_msg 0 0 0
689 }
690 0 return $msg;
691 }
692  
693 sub _logmsg
694 0 ▶{
Subroutine _logmsg: not called CC=2 SCAR=17.9
695 0 my $this = shift;
696 0 my $direction = shift;
697 0 my $msg = shift;
698 0 ▶ if($this->{verbose}>1){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'verbose'} > 1) { } 0 0 0
699 0 $this->echo($direction,"(".length($msg)." bytes) {$msg}\n");
700 }else{
701 # don't print the whole message by default, in case it is either binary &/or long
702 0 $this->echo($direction,"(".length($msg)." bytes)\n");
703 0 $this->log($direction," {{{$msg}}}\n");
704 }
705 }
706  
707 sub send_to_server($@)
708 1 ▶{
Subroutine send_to_server: called CC=5 SCAR=29.7
Pod documented
709 1 my $this = shift;
710 1 my $msg = shift;
711 1 $this->connect_to_server();
712 0 ▶ $this->log("calling server callback ($msg)\n") if $this->{client_to_server_callback} && $this->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"client_to_server_callback"} and $this->{"verbose"} > 1 0 0 0
Condition0%0/3
$this->{"client_to_server_callback"} and $this->{"verbose"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"client_to_server_callback"} and $this->{"verbose"} > 1
$this->{"client_to_server_callback"} $this->{"verbose"} > 1
Truth table0%0/3
A and B
A= $this->{"client_to_server_callback"}B= $this->{"verbose"} > 1
ABresult
0X0
100
111
713 0 $msg = _do_callback( $this->{client_to_server_callback}, $msg );
714 0 $this->_logmsg(">>>",$msg);
715 0 ▶ confess "SERVER being null was unexpected" if !$this->{SERVER};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"SERVER"} 0 0 0
716 0 ▶ return print({$this->{SERVER}} $msg) || die "Can't send to server: $?";
Condition0%0/2
print({$this->{'SERVER'};} $msg) || die("Can't send to server: $?")
l!l
00
MC/DC0%0/1
print({$this->{'SERVER'};} $msg) || die("Can't send to server: $?")
print({$this->{'SERVER'};} $msg)
Truth table0%0/2
A
A= print({$this->{'SERVER'};} $msg)
Aresult
11
00
717 }
718  
719 =head2 send_to_client( string(s) )
720  
721 Sends a message to the client
722  
723 =head4 Parameters
724  
725 =over
726  
727 =item * string(s) - one or more strings to be sent
728  
729 =item * Return: true if successful
730  
731 =back
732  
733 =head4 Usage
734  
735 If a callback is set, it will be called before the message is sent.
736  
737 =cut
738  
739 sub send_to_client($@)
740 0 ▶{
Subroutine send_to_client: not called CC=3 SCAR=24.8
Pod documented
741 0 my $this = shift;
742 0 my $msg = shift;
743 0 ▶ $this->echo("calling client callback ($msg)\n") if $this->{server_to_client_callback} && $this->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"server_to_client_callback"} and $this->{"verbose"} > 1 0 0 0
Condition0%0/3
$this->{"server_to_client_callback"} and $this->{"verbose"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"server_to_client_callback"} and $this->{"verbose"} > 1
$this->{"server_to_client_callback"} $this->{"verbose"} > 1
Truth table0%0/3
A and B
A= $this->{"server_to_client_callback"}B= $this->{"verbose"} > 1
ABresult
0X0
100
111
744 0 $msg = _do_callback( $this->{server_to_client_callback}, $msg );
745 0 $this->_logmsg("<<<",$msg);
746 0 return print({$this->{CLIENT}} $msg);
747 }
748  
749 =head2 read_from_server( )
750  
751 Reads a message from the server
752  
753 =head4 Parameters
754  
755 =over
756  
757 =item * --none--
758  
759 =item * Returns - the message read, or undef if the server disconnected.
760  
761 =back
762  
763 =head4 Usage
764  
765 Blocks until a message is received.
766  
767 =cut
768  
769 sub read_from_server()
770 0 ▶{
Subroutine read_from_server: not called CC=2 SCAR=17.9
Pod documented
771 0 my $this=shift;
772 0 my $msg;
773 0 sysread($this->{SERVER},$msg,100000);
774 0 ▶ if(length($msg) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (length $msg == 0) 0 0 0
775 {
776 0 $this->echo("Server disconnected\n");
777 0 return undef;
778 }
779 0 return $msg;
780 }
781  
782 =head2 send_and_receive( )
783  
784 Sends a message to the server and receives a response
785  
786 =head4 Parameters
787  
788 =over
789  
790 =item * the message(s) to be sent
791  
792 =item * Returns - message read, or undef if the server disconnected.
793  
794 =back
795  
796 =head4 Usage
797  
798 Blocks until a message is received. If the server does not always return exactly one message for each message it receives, send_and_receive() will either concatenate messages or block forever.
799  
800 =cut
801  
802 sub send_and_receive($)
803 1 ▶{
Subroutine send_and_receive: called CC=1 SCAR=0.4
Pod documented
804 1 my $this=shift;
805 1 $this->send_to_server(@_);
806 0 return $this->read_from_server(@_);
807 }
808  
809 =head2 connect_to_server( )
810  
811 Connects to the server
812  
813 =head4 Parameters
814  
815 =over
816  
817 =item * --none--
818  
819 =item * Returns --none--
820  
821 =back
822  
823 =head4 Usage
824  
825 This method is automatically called when needed. It only needs to be called directly if you want to be sure that the connection to server succeeds before proceeding.
826  
827 =cut
828  
829 sub connect_to_server()
830 1 ▶{
Subroutine connect_to_server: called CC=7 SCAR=35.4
Pod documented
831 1 my $this=shift;
832 1 ▶ return if $this->{SERVER};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"SERVER"} 0 1 1
833 1 ▶ socket($this->{SERVER}, PF_INET, SOCK_STREAM, $protocol) or die "Can't create socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket $this->{"SERVER"}, 2, 1, $protocol or die "Can't create socket: $!" 0 0 0
834 0 ▶ confess "remote_ip_address unexpectedly not known" if !$this->{remote_ip_address};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"remote_ip_address"} 0 0 0
835 0 ▶ my $remote_ip_aton = inet_aton( $this->{remote_ip_address} ) or croak "Fatal: Cannot resolve internet address: '$this->{remote_ip_address}'\n";
Branch: 1 branch
BranchTrueFalseTotal
my $remote_ip_aton = inet_aton($this->{"remote_ip_address"}) or croak(qq[Fatal: Cannot resolve internet address: '$this->{"remote_ip_address"}'\n]) 0 0 0
836 0 ▶ my $remote_port_address = sockaddr_in($this->{remote_port_num}, $remote_ip_aton ) or die "Fatal: Can't get port address: $!"; # TODO Is die the way to go here? Not sure it isn't. Not sure it is.
Branch: 1 branch
BranchTrueFalseTotal
my $remote_port_address = sockaddr_in($this->{"remote_port_num"}, $remote_ip_aton) or die "Fatal: Can't get port address: $!" 0 0 0
837 0 $this->echo("Connecting to $this->{remote_ip_address}\:$this->{remote_port_num} [verbose=$this->{verbose}]\n");
838 0 ▶ connect($this->{SERVER}, $remote_port_address) or confess "Fatal: Can't connect to $this->{remote_ip_address}:$this->{remote_port_num} using $this->{SERVER}. $!"; # TODO Is die the way to go here? Not sure it isn't. Not sure it is. TODO document error handling, one way or the other.
Branch: 1 branch
BranchTrueFalseTotal
connect $this->{"SERVER"}, $remote_port_address or confess(qq[Fatal: Can't connect to $this->{"remote_ip_address"}:$this->{"remote_port_num"} using $this->{"SERVER"}. $!]) 0 0 0
839 0 $this->{SERVER}->autoflush(1);
840 0 binmode($this->{SERVER});
841 0 return undef;
842 }
843  
844 =head2 disconnect_from_server( )
845  
846 Disconnects from the server
847  
848 =head4 Parameters
849  
850 =over
851  
852 =item * --none--
853  
854 =item * Returns --none--
855  
856 =back
857  
858 =head4 Usage
859  
860 Disconnection is normally triggered by the other party disconnecting, not by us. disconnect_from_server() is only useful with new_client(), and not otherwise supported.
861  
862 =cut
863  
864 sub disconnect_from_server()
865 0 ▶{
Subroutine disconnect_from_server: not called CC=1 SCAR=6.9
Pod documented
866 0 my $this=shift;
867 0 $this->log("initiating disconnect");
868 0 $this->_destroy();
869 0 return undef;
870 }
871  
872 0 ▶sub _pause($){
Subroutine _pause: not called CC=1 SCAR=6.9
873 0 select undef,undef,undef,shift;
874 0 return undef;
875 }
876  
877 0 ▶sub _message_from_client_to_server(){ # TODO Too many too similar sub names, some of which maybe should be public
Subroutine _message_from_client_to_server: not called CC=5 SCAR=34.0
878 0 my $this=shift;
879 # optional sleep to reduce risk of split messages
880 0 ▶ _pause($this->{defrag_delay}) if $this->{defrag_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"defrag_delay"} 0 0 0
881 # It would be possible to be more agressive by repeatedly waiting until there is a break, but that would probably err too much towards concatenating seperate messages - especially under load.
882 0 my $msg;
883 0 sysread($this->{CLIENT},$msg,10000);
884 # (0 length message means connection closed)
885 0 ▶ if(length($msg) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $msg == 0) 0 0 0
886 0 $this->echo("Client disconnected\n");
887 0 $this->_destroy();
888 0 return;
889 }
890 # Send message to server, if any. Else 'send' to callback function and return result to client.
891 0 ▶ if($this->{SERVER}){
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'SERVER'}) { } 0 0 0
elsif ($this->{'server_callback'}) { } 0 0 0
892 0 $this->send_to_server($msg);
893 }elsif($this->{server_callback}){
894 0 $this->send_to_client( $this->{server_callback}($msg) );
895 }else{
896 0 confess "$this->{name}: Did not expect to have neither a connection to a SERVER nor a server_callback";
897 }
898 0 return undef;
899 }
900  
901 0 ▶sub _message_from_server_to_client(){ # TODO Too many too similar sub names
Subroutine _message_from_server_to_client: not called CC=3 SCAR=24.8
902 0 my $this=shift;
903 # sleep to avoid splitting messages
904 0 ▶ _pause($this->{defrag_delay}) if $this->{defrag_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"defrag_delay"} 0 0 0
905 # Read from SERVER and copy to CLIENT
906 0 my $msg = $this->read_from_server();
907 0 ▶ if(!defined $msg){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
908 0 $this->echo("Server disconnected\n");
909 0 $this->_destroy();
910 0 return;
911 }
912 0 $this->send_to_client($msg);
913 0 return undef;
914 }
915  
916 sub _cull_child()
917 0 ▶{
Subroutine _cull_child: not called CC=6 SCAR=37.4
918 0 ▶ my $this=shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $this = shift() or die 0 0 0
919 0 ▶ my $child=shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $child = shift() or die 0 0 0
920 0 for my $i (0 .. @{$this->{children}}){
921 0 ▶ if($child==$this->{children}[$i]){
Branch: 1 branch
BranchTrueFalseTotal
if ($child == $this->{"children"}[$i]) 0 0 0
922 0 ▶ $this->echo("Child $child->{name} is done, cleaning it up") if $this->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 1 0 0 0
923 0 splice @{$this->{children}}, $i,1;
924 0 return;
925 }
926 }
927 0 confess "Child $child->{name} is finished, but I can't find it to clean it up";
928 }
929  
930 # _main_loop is called by listeners and by their 'leave-home' children both. When called by listeners, it also includes stay at home children
931  
932 sub _main_loop()
933 0 ▶{
Subroutine _main_loop: not called CC=27 SCAR=66.3
934 0 my $this=shift;
935 0 my $last_time;
936 my $target_time;
937 0 ▶ if($this->{timer_interval}&&$this->{timer_callback}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"timer_interval"} and $this->{"timer_callback"}) 0 0 0
Condition0%0/3
$this->{"timer_interval"} and $this->{"timer_callback"}
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"timer_interval"} and $this->{"timer_callback"}
$this->{"timer_interval"} $this->{"timer_callback"}
Truth table0%0/3
A and B
A= $this->{"timer_interval"}B= $this->{"timer_callback"}
ABresult
0X0
100
111
938 0 $last_time=time();
939 0 $target_time=$last_time+$this->{timer_interval};
940 }
941 # Main Loop
942 0 mainloop: while(1)
943 {
944 # Build file descriptor list for select call
945 0 my $rin = "";
946 0 ▶ if($this->{LISTEN}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LISTEN"}) 0 0 0
947 0 ▶ confess "LISTEN is unexpectedly not a filehandle" if !fileno($this->{LISTEN});
Branch: 1 branch
BranchTrueFalseTotal
unless fileno $this->{"LISTEN"} 0 0 0
948 0 vec($rin, fileno($this->{LISTEN}), 1) = 1;
949 }
950 0 foreach my $each ($this, @{$this->{children}}) {
951 0 ▶ vec($rin, fileno($each->{CLIENT}), 1) = 1 if $each->{CLIENT}; # TODO if no client, child should probably be dead
Branch: 1 branch
BranchTrueFalseTotal
if $each->{"CLIENT"} 0 0 0
952 0 ▶ vec($rin, fileno($each->{SERVER}), 1) = 1 if $each->{SERVER};
Branch: 1 branch
BranchTrueFalseTotal
if $each->{"SERVER"} 0 0 0
953 }
954 # and listen...
955 0 my $rout = $rin;
956 0 my $delay;
957 0 ▶ if($this->{timer_interval}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'timer_interval'}) { } 0 0 0
958 0 ▶ if(time() > $target_time){
Branch: 1 branch
BranchTrueFalseTotal
if (&NET::MitM::time() > $target_time) 0 0 0
959 0 ▶ $this->{timer_callback}() or last;
Branch: 1 branch
BranchTrueFalseTotal
$this->{"timer_callback"}->() or last 0 0 0
960 0 $last_time=$target_time;
961 0 $target_time+=$this->{timer_interval};
962 }
963 0 $delay=$target_time-time();
964 0 ▶ $delay=0 if($delay<0);
Branch: 1 branch
BranchTrueFalseTotal
if $delay < 0 0 0 0
965 0 ▶ $this->echo("delay=$delay") if $this->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 1 0 0 0
966 }else{
967 0 $delay=undef;
968 }
969 0 select( $rout, "", "", $delay );
970 0 ▶ if( $this->{LISTEN} && vec($rout,fileno($this->{LISTEN}),1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LISTEN"} and vec $rout, fileno $this->{"LISTEN"}, 1) 0 0 0
Condition0%0/3
$this->{"LISTEN"} and vec $rout, fileno $this->{"LISTEN"}, 1
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"LISTEN"} and vec $rout, fileno $this->{"LISTEN"}, 1
$this->{"LISTEN"} vec $rout, fileno $this->{"LISTEN"}, 1
Truth table0%0/3
A and B
A= $this->{"LISTEN"}B= vec $rout, fileno $this->{"LISTEN"}, 1
ABresult
0X0
100
111
971 0 my $child = $this->_spawn_child();
972 0 ▶ push @{$this->{children}}, $child if $child;
Branch: 1 branch
BranchTrueFalseTotal
if $child 0 0 0
973 0 next;
974 }
975 0 foreach my $each($this, @{$this->{children}}) {
976 0 ▶ confess "We have a child with no CLIENT\n" if !$each->{CLIENT} && $each!=$this;
Branch: 1 branch
BranchTrueFalseTotal
if not $each->{"CLIENT"} and $each != $this 0 0 0
Condition0%0/3
not $each->{"CLIENT"} and $each != $this
!ll&&!rl&&r
000
MC/DC0%0/2
not $each->{"CLIENT"} and $each != $this
not $each->{"CLIENT"} $each != $this
Truth table0%0/3
not(A) and B
A= not $each->{"CLIENT"}B= $each != $this
ABresult
0X0
100
111
977 0 ▶ if($each->{CLIENT} && vec($rout,fileno($each->{CLIENT}),1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($each->{"CLIENT"} and vec $rout, fileno $each->{"CLIENT"}, 1) 0 0 0
Condition0%0/3
$each->{"CLIENT"} and vec $rout, fileno $each->{"CLIENT"}, 1
!ll&&!rl&&r
000
MC/DC0%0/2
$each->{"CLIENT"} and vec $rout, fileno $each->{"CLIENT"}, 1
$each->{"CLIENT"} vec $rout, fileno $each->{"CLIENT"}, 1
Truth table0%0/3
A and B
A= $each->{"CLIENT"}B= vec $rout, fileno $each->{"CLIENT"}, 1
ABresult
0X0
100
111
978 0 $each->_message_from_client_to_server(); # TODO Too many too similar sub names
979 0 ▶ if(!$each->{CLIENT}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($each->{"CLIENT"}) 0 0 0
980 # client has disconnected
981 0 ▶ if($each==$this){
Branch: 1 branch
BranchTrueFalseTotal
if ($each == $this) { } 0 0 0
982 # we are the child - OK to exit
983 0 return; #might be better to die or exit at this point instead?
984 }else{
985 # we are the parent - clean up child and keep going
986 0 $this->_cull_child($each);
987 0 last; # _cull_child impacts the children array - not safe to continue without regenerating rout
988 }
989 }
990 }
991 0 ▶ if($each->{SERVER} && vec($rout,fileno($each->{SERVER}),1) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($each->{"SERVER"} and vec $rout, fileno $each->{"SERVER"}, 1) 0 0 0
Condition0%0/3
$each->{"SERVER"} and vec $rout, fileno $each->{"SERVER"}, 1
!ll&&!rl&&r
000
MC/DC0%0/2
$each->{"SERVER"} and vec $rout, fileno $each->{"SERVER"}, 1
$each->{"SERVER"} vec $rout, fileno $each->{"SERVER"}, 1
Truth table0%0/3
A and B
A= $each->{"SERVER"}B= vec $rout, fileno $each->{"SERVER"}, 1
ABresult
0X0
100
111
992 0 $each->_message_from_server_to_client(); # TODO Too many too similar sub names
993 0 ▶ if(!$each->{SERVER}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($each->{"SERVER"}) 0 0 0
994 # client has disconnected
995 0 ▶ if($each==$this){
Branch: 1 branch
BranchTrueFalseTotal
if ($each == $this) { } 0 0 0
996 # we are the child - OK to exit
997 0 return; #might be better to die or exit at this point instead?
998 }else{
999 0 $this->_cull_child($each);
1000 0 last; # _cull_child impacts the children array - not safe to continue without regenerating rout
1001 }
1002 }
1003 }
1004 }
1005 }
1006 0 return undef;
1007 }
1008  
1009 =head2 hhmmss( )
1010  
1011 The default timestamp function - returns localtime in hh:mm:ss format
1012  
1013 =head4 Parameters
1014  
1015 =over
1016  
1017 =item * --none--
1018  
1019 =item * Returns - current time in hh:mm:ss format
1020  
1021 =back
1022  
1023 =head4 Usage
1024  
1025 This function is, by default, called when a message is written to the log file.
1026  
1027 It may be overridden by calling mydate().
1028  
1029 =cut
1030  
1031 sub hhmmss()
1032 2 ▶{
Subroutine hhmmss: called CC=1 SCAR=0.0
Pod documented
1033 2 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
1034 2 return sprintf "%02d:%02d:%02d",$hour,$min,$sec;
1035 }
1036  
1037 =head2 mydate( )
1038  
1039 Override the standard hh:mm:ss datestamp
1040  
1041 =head4 Parameters
1042  
1043 =over
1044  
1045 =item * datestamp_callback - a reference to a function that returns a datestamp
1046  
1047 =item * Returns - a reference to the current or updated callback function
1048  
1049 =back
1050  
1051 =head4 Usage
1052  
1053 For example:
1054  
1055 sub yymmddhhmmss {
1056 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
1057 return sprintf "%02d/%02d/%02d %02d:%02d:%02d",
1058 $year+1900,$mon+1,$mday,$hour,$min,$sec;
1059 }
1060 mydate(\&yymmddhhmmss);
1061  
1062 =cut
1063  
1064 sub mydate(;$)
1065 0 ▶{
Subroutine mydate: not called CC=3 SCAR=24.8
Pod documented
1066 0 my $this=shift;
1067 0 ▶ my $mydate=shift||undef;
Condition0%0/2
shift() || undef
l!l
00
MC/DC0%0/1
shift() || undef
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
1068 0 ▶ if(defined $mydate){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mydate) 0 0 0
1069 0 $this->{mydate} = $mydate;
1070 }
1071 0 return $this->{mydate};
1072 }
1073  
1074 =head2 listen( )
1075  
1076 Listen on local_port and prepare to accept incoming connections
1077  
1078 =head4 Parameters
1079  
1080 =over
1081  
1082 =item * --none--
1083  
1084 =item * Return --none--
1085  
1086 =back
1087  
1088 =head4 Usage
1089  
1090 This method is called by go(). It only needs to be called directly if go() is being bypassed for some reason.
1091  
1092 =cut
1093  
1094 sub listen()
1095 1 ▶{
Subroutine listen: called CC=6 SCAR=28.1
Pod documented
1096 1 my $this=shift;
1097 1 ▶ return if $this->{LISTEN};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"LISTEN"} 0 1 1
1098 1 ▶ $this->echo(sprintf "Server %u listening on port %d (%s)\n",$$,$this->{local_port_num},$this->{parallel}?"parallel":"serial");
Branch: 1 branch
BranchTrueFalseTotal
$this->{'parallel'} ? : 0 1 1
1099 # open tcp/ip socket - see blue camel book pg 349
1100 1 ▶ socket($this->{LISTEN}, PF_INET, SOCK_STREAM, $protocol) or die "Fatal: Can't create socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket $this->{"LISTEN"}, 2, 1, $protocol or die "Fatal: Can't create socket: $!" 0 0 0
1101 0 ▶ bind($this->{LISTEN}, sockaddr_in($this->{local_port_num}, INADDR_ANY)) or die "Fatal: Can't bind socket $this->{local_port_num}: $!";
Branch: 1 branch
BranchTrueFalseTotal
bind $this->{"LISTEN"}, sockaddr_in($this->{"local_port_num"}, "\0\0\0\0") or die qq[Fatal: Can't bind socket $this->{"local_port_num"}: $!] 0 0 0
1102 0 ▶ listen($this->{LISTEN},1) or die "Fatal: Can't listen to socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::listen $this->{"LISTEN"}, 1 or die "Fatal: Can't listen to socket: $!" 0 0 0
1103 0 $this->echo("Waiting on port $this->{local_port_num}\n");
1104 0 return undef;
1105 }
1106  
1107 sub _accept($)
1108 0 ▶{
Subroutine _accept: not called CC=2 SCAR=17.9
1109 # Accept a new connection
1110 0 my $this=shift;
1111 0 my $LISTEN=shift;
1112 0 my $client_paddr = accept($this->{CLIENT}, $LISTEN);
1113 0 $this->{CLIENT}->autoflush(1);
1114 0 binmode($this->{CLIENT});
1115 0 my ($client_port, $client_iaddr) = sockaddr_in( $client_paddr );
1116 0 $this->log("Connection accepted from", inet_ntoa($client_iaddr).":$client_port\n");
1117 0 ▶ $this->connect_to_server() if $this->{remote_ip_address};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"remote_ip_address"} 0 0 0
1118 0 return undef;
1119 }
1120  
1121 1 ▶sub _new_child(){
Subroutine _new_child: called CC=6 SCAR=18.4
1122 1 my $parent=shift;
1123 1 my $child=_new();
1124 1 my $all_good=1;
1125 1 foreach my $key (keys %{$parent}){
1126 8 ▶ if($key=~m/^(LISTEN|children|connections|name|timer_interval|timer_callback)$/){
Branch: 3 branches
BranchTrueFalseTotal
if ($key =~ /^(LISTEN|children|connections|name|timer_interval|timer_callback)$/) { } 1 7 8
elsif ($key =~ /^(parallel|log_file|verbose|mydate|.*callback|(local|remote)_(port_num|ip_address))$/) { } 6 1 7
elsif ($key eq 'LOGFILE') { } 1 0 1
1127 # do nothing - these parameters are not inherited
1128 }elsif($key =~ m/^(parallel|log_file|verbose|mydate|.*callback|(local|remote)_(port_num|ip_address))$/){
1129 6 $child->{$key}=$parent->{$key};
1130 }elsif($key eq "LOGFILE"){
1131 # TODO might want to have a different logfile for each child, or at least, an option to do so.
1132 1 $child->{$key}=$parent->{$key};
1133 }else{
1134 0 warn "internal error - unexpected attribute: $key = {$parent->$key}\n";
1135 0 $all_good=0;
1136 }
1137 }
1138 1 ▶ die "Internal error in _new_child()" unless $all_good;
Branch: 1 branch
BranchTrueFalseTotal
unless $all_good 0 1 1
1139 1 return bless $child;
1140 }
1141  
1142 0 ▶sub _spawn_child(){
Subroutine _spawn_child: not called CC=8 SCAR=42.8
1143 0 my $this=shift;
1144 0 my $child = $this->_new_child();
1145 0 $child->_accept($this->{LISTEN});
1146 0 ▶ confess "We have a child with no CLIENT\n" if !$child->{CLIENT};
Branch: 1 branch
BranchTrueFalseTotal
unless $child->{"CLIENT"} 0 0 0
1147 # hand-off the connection
1148 0 $this->echo("starting connection:",++$this->{connections});
1149 0 ▶ if(!$this->{parallel}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"parallel"}) 0 0 0
1150 0 return $child;
1151 }
1152 0 my $pid = fork();
1153 0 ▶ if(!defined $pid){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif (not $pid) { } 0 0 0
1154 # Error
1155 0 $this->echo("Cannot fork!: $!\nNew connection will run in the current thread\n");
1156 0 return $child;
1157 }elsif(!$pid){
1158 # This is the child process
1159 0 ▶ $child->echo(sprintf"Running %u",$$) if $child->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"verbose"} > 1 0 0 0
1160 0 ▶ confess "We have a child with no CLIENT\n" if !$child->{CLIENT};
Branch: 1 branch
BranchTrueFalseTotal
unless $child->{"CLIENT"} 0 0 0
1161 # The active instanct of the parent is in a different process
1162 # Ideally, we would have the parent go out of scope, but all we can do is clean up the bits that matter
1163 0 close $this->{LISTEN};
1164 0 $child->_main_loop();
1165 0 ▶ $child->echo(sprintf"Exiting %u",$$) if $child->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"verbose"} > 1 0 0 0
1166 0 exit;
1167 }else{
1168 # This is the parent process. The active child instance is in its own process, we clean up what we can
1169 0 $child->_destroy();
1170 0 return undef;
1171 }
1172 }
1173  
1174 sub go()
1175 1 ▶{
Subroutine go: called CC=1 SCAR=1.2
Pod documented
1176 1 my $this=shift;
1177 1 $this->listen();
1178 0 $this->_main_loop();
1179 0 return undef;
1180 }
1181  
1182 sub _destroy()
1183 0 ▶{
Subroutine _destroy: not called CC=3 SCAR=24.8
1184 0 my $this=shift;
1185 0 ▶ close $this->{CLIENT} if($this->{CLIENT});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"CLIENT"} 0 0 0
1186 0 ▶ close $this->{SERVER} if($this->{SERVER});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"SERVER"} 0 0 0
1187 0 $this->{SERVER}=$this->{CLIENT}=undef;
1188 0 return undef;
1189 }
1190  
1191 =head1 Exports
1192  
1193 MitM does not export any functions or variables.
1194 If parallel() is turned on, which by default it is not, MitM sets SIGCHD to IGNORE, and as advertised, it calls fork() once for each new connection.
1195  
1196 =head1 AUTHOR
1197  
1198 Ben AVELING, C<< <ben dot aveling at optusnet dot com dot au> >>
1199  
1200 =head1 BUGS
1201  
1202 Please report any bugs or feature requests to C<bug-NET-MitM at rt.cpan.org>, or through
1203 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=NET-MitM>. I will be notified, and then you'll
1204 automatically be notified of progress on your bug as I make changes.
1205  
1206 =head1 SUPPORT
1207  
1208 You can find documentation for this module with the perldoc command.
1209  
1210 perldoc NET::MitM
1211  
1212 You can also look for information at:
1213  
1214 =over
1215  
1216 =item * RT: CPAN's request tracker (report bugs here)
1217  
1218 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=NET-MitM>
1219  
1220 =item * AnnoCPAN: Annotated CPAN documentation
1221  
1222 L<http://annocpan.org/dist/NET-MitM>
1223  
1224 =item * CPAN Ratings
1225  
1226 L<http://cpanratings.perl.org/d/NET-MitM>
1227  
1228 =item * Search CPAN
1229  
1230 L<http://search.cpan.org/dist/NET-MitM/>
1231  
1232 =back
1233  
1234 =head1 ACKNOWLEDGEMENTS
1235  
1236 I'd like to acknowledge W. Richard Steven's and his fantastic introduction to TCPIP: "TCP/IP Illustrated, Volume 1: The Protocols", Addison-Wesley, 1994. (L<http://www.kohala.com/start/tcpipiv1.html>).
1237 It got me started. Recommend. RIP.
1238 The Blue Camel Book is also pretty useful, and Langworth & chromatic's "Perl Testing, A Developer's Notebook" is also worth a hat tip.
1239  
1240 =head1 ALTERNATIVES
1241  
1242 If what you want is a pure proxy, especially if you want an ssh proxy or support for firewalls, you might want to evaluate Philippe "BooK" Bruhat's L<Net::Proxy>.
1243  
1244 And if you want a full "portable multitasking and networking framework for any event loop", you may be looking for L<POE>.
1245  
1246 =head1 LICENSE AND COPYRIGHT
1247  
1248 Copyleft 2013 Ben AVELING.
1249  
1250 This program is free software; you can redistribute it and/or modify it
1251 under the terms of the the Artistic License (2.0). You may obtain a
1252 copy of the full license at:
1253  
1254 L<http://www.perlfoundation.org/artistic_license_2_0>
1255  
1256 Any use, modification, and distribution of the Standard or Modified
1257 Versions is governed by this Artistic License. By using, modifying or
1258 distributing the Package, you accept this license. Do not use, modify,
1259 or distribute the Package, if you do not accept this license.
1260  
1261 If your Modified Version has been derived from a Modified Version made
1262 by someone other than you, you are nevertheless required to ensure that
1263 your Modified Version complies with the requirements of this license.
1264  
1265 This license does not grant you the right to use any trademark, service
1266 mark, tradename, or logo of the Copyright Holder.
1267  
1268 This license includes the non-exclusive, worldwide, free-of-charge
1269 patent license to make, have made, have, hold and cherish,
1270 use, offer to use, sell, offer to sell, import and
1271 otherwise transfer the Package with respect to any patent claims
1272 licensable by the Copyright Holder that are necessarily infringed by the
1273 Package. If you institute patent litigation (including a cross-claim or
1274 counterclaim) against any party alleging that the Package constitutes
1275 direct or contributory patent infringement, then this Artistic License
1276 to you shall terminate on the date that such litigation is filed.
1277  
1278 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1279 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1280 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1281 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1282 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1283 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1284 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1285 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SO THERE.
1286  
1287 =cut
1288  
1289 4 1; # End of NET::MitM
↑ Summary