MitM.pm

filter: Statementstmt 24.7% 100 / 404 Branchbran 11.3% 24 / 212 Conditioncond 5.8% 3 / 51 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 38.4% 20 / 52 Podpod 100% 28 / 28 totaltotal 22.4% 175 / 778 CC 131 SCAR 55.6
CC
131
Cov
19%
_main_loop
1260.0
_sanity_check_options
56.0
_do_callback
56.0
CRAP 258.7
↑ Summary Mediator.pm »
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.03_02
10  
11 =cut
12  
13 3 our $VERSION = '0.03_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, allowing 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 - a worked example.
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,callback_behaviour=>"modify");
59 $MitM->server_to_client_callback(\&receive,callback_behaviour=>"modify");
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 3 ▶use 5.002; # has been tested with 5.8.9. an earlier version failed with 5.6.2, but that has probably been fixed.
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 3 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
81 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
83 3 ▶use IO::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
84 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
85 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 #BEGIN{eval{require Time::HiRes; import Time::HiRes qw(time)}}; # only needed for high precision time_interval - will still work fine even if missing
87 3 ▶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
88  
89 =head2 new( remote_ip_address, remote_port_num, local_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 3 my $mitm_count=0;
121  
122 2 ▶sub _new(){
Subroutine _new: called CC=1 SCAR=0.0
123 2 my %this;
124 2 $this{verbose} = 1;
125 2 $this{parallel} = 0;
126 2 $this{mydate} = \&hhmmss;
127 2 $this{name} = "MitM".++$mitm_count;
128 2 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_num 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  
162 If logging is on, messages will be logged.
163  
164 By default, go() does not return. The function L<stop_when_idle()> can be called to force go() to return. You may want to L<fork> before calling it.
165  
166 If new_server() was used instead of new(), messages from client are instead passed to the server callback function.
167  
168 =cut
169  
170 # 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?
171  
172 4 ▶sub _set($;$) {
Subroutine _set: called CC=3 SCAR=11.2
173 4 my $this=shift;
174 4 ▶ my $key=shift or confess "missing mandatory parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $key = shift() or confess("missing mandatory parameter") 4 0 4
175 4 my $value=shift;
176 4 ▶ if(defined $value){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) 4 0 4
177 4 $this->{$key} = $value;
178 }
179 4 return $this->{$key};
180 }
181  
182 =head2 name( [name] )
183  
184 Names the object - will be reported back in logging/debug
185  
186 =head4 Parameters
187  
188 =over
189  
190 =item * name - the new name (default is MitM1, MitM2, ...)
191  
192 =item * Returns - the current or new setting
193  
194 =back
195  
196 =head4 Usage
197  
198 For a minimal MitM:
199  
200 use Net::MitM;
201 my $MitM = Net::MitM->new("www.cpan.org", 80, 10080);
202 $MitM->go();
203  
204 =cut
205  
206 2 ▶sub name(;$) {
Subroutine name: called CC=1 SCAR=0.0
Pod documented
207 2 my $this=shift;
208 2 my $value=shift;
209 2 return $this->_set("name", $value);
210 }
211  
212 =head2 verbose( [level] )
213  
214 Turns on/off reporting to stdout.
215  
216 =head4 Parameters
217  
218 =over
219  
220 =item * level - how verbose to be. 0=nothing, 1=normal, 2=debug. The default is 1.
221  
222 =item * Returns - the current or new setting
223  
224 =back
225  
226 =head4 Usage
227  
228 Setting verbose changes the amount of information printed to stdout.
229  
230 =cut
231  
232 0 ▶sub verbose(;$) {
Subroutine verbose: not called CC=1 SCAR=6.9
Pod documented
233 0 my $this=shift;
234 0 my $verbose=shift;
235 #warn "verbose->(",$verbose||"--undef--",")\n";
236 0 return $this->_set("verbose", $verbose);
237 }
238  
239 =head2 client_to_server_callback( callback [callback_behaviour => behaviour] )
240  
241 Set a callback function to monitor/modify each message sent to server
242  
243 =head4 Parameters
244  
245 =over
246  
247 =item * callback - a reference to a function to be called for each message sent to server
248  
249 =item * callback_behaviour - explicitly sets the callback as readonly, modifying or conditional.
250  
251 =item * Returns - the current or new setting
252  
253 =back
254  
255 =head4 Usage
256  
257 If a client_to_server_callback callback is set, it will be called with a copy of each message received from the client before it is sent to the server.
258  
259 What the callback returns determines what will be sent, depending on the value of callback_behaviour:
260  
261 =over
262  
263 =item * If callback_behaviour = "readonly", the return value from the callback is ignored, and the original message is sent.
264  
265 =item * If callback_behaviour = "modify", the return value from the callback is sent instead of the original message, unless the return value is undef, in which case nothing is sent
266  
267 =item * If callback_behaviour = "conditional", which is the default, that the return value from the callback is sent instead of the original message, or if the return value is undef, then the original message is sent.
268  
269 =back
270  
271 For example, to modify messages:
272  
273 use Net::MitM;
274 sub send_($) {$_[0] =~ s/Host: .*:\d+/Host: cpan.org/;}
275 sub receive($) {$_[0] =~ s/www.cpan.org(:\d+)?/localhost:10080/g;}
276 my $MitM = Net::MitM->new("www.cpan.org", 80, 10080);
277 $MitM->client_to_server_callback(\&send, callback_behaviour=>"modify");
278 $MitM->server_to_client_callback(\&receive, callback_behaviour=>"modify");
279 $MitM->go();
280  
281 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.
282  
283 For example, to write messages to a log:
284  
285 sub peek($) {my $msg = shift; print LOG; return $msg;}
286 my $MitM = Net::MitM->new("www.cpan.org", 80, 10080);
287 $MitM->client_to_server_callback(\&peek, callback_behaviour=>"readonly");
288 $MitM->server_to_client_callback(\&peek, callback_behaviour=>"readonly");
289 $MitM->go();
290  
291 For historical reasons, "conditional" is the default. It is not recommended, and may be deprecated in a future release.
292  
293 "conditional" may be used for readonly or modify type behaviour. For readonly behaviour, either return the original message, or return null. For example:
294  
295 sub peek($) {my $msg = shift; print LOG; return $msg;}
296 my $MitM = Net::MitM->new("www.cpan.org", 80, 10080);
297 $MitM->client_to_server_callback(\&peek,callback_behaviour=>"readonly");
298 ...
299  
300 sub peek($) {my $msg = shift; print LOG; return undef;}
301 my $MitM = Net::MitM->new("www.cpan.org", 80, 10080);
302 $MitM->client_to_server_callback(\&peek,callback_behaviour=>"readonly");
303 ...
304  
305 But be careful. This is unlikely to do what you would want:
306 sub peek($) {my $msg = shift; print LOG}
307 my $MitM = Net::MitM->new("www.cpan.org", 80, 10080);
308 $MitM->client_to_server_callback(\&peek,callback_behaviour=>"readonly");
309 ...
310  
311 Assuming print LOG succeeds, print will return a true value (probably 1), and MitM will send that value, not $msg.
312  
313 =cut
314  
315 sub _sanity_check_options($$)
316 0 ▶{
Subroutine _sanity_check_options: not called CC=7 SCAR=40.3
317 0 my $self=shift;
318 0 my $options=shift;
319 0 my $allowed=shift;
320 0 foreach my $key (keys %$options){
321 0 ▶ if(!$allowed->{$key}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($allowed->{$key}) 0 0 0
322 0 ▶ carp "Warning: $key not a supported option. Expected: ",join(" ",map {"'$_'"} keys %$options) unless defined $self->{verbose} && $self->{verbose}<=0;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"verbose"} and $self->{"verbose"} <= 0 0 0 0
Condition0%0/3
defined $self->{"verbose"} and $self->{"verbose"} <= 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"verbose"} and $self->{"verbose"} <= 0
defined $self->{"verbose"} $self->{"verbose"} <= 0
Truth table0%0/3
A and B
A= defined $self->{"verbose"}B= $self->{"verbose"} <= 0
ABresult
0X0
100
111
323 0 return undef;
324 }
325 0 ▶ if( $options->{$key} !~ $allowed->{$key}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($options->{$key} =~ /$allowed->{$key}/) 0 0 0
326 0 ▶ carp "Warning: $key=$options->{$key} not a supported option.\n" unless $self->{verbose}<=0;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"verbose"} <= 0 0 0 0
327 0 return undef;
328 }
329 }
330 0 return 1;
331 }
332  
333 0 ▶sub client_to_server_callback(;$%) {
Subroutine client_to_server_callback: not called CC=2 SCAR=17.9
Pod documented
334 0 my $this=shift;
335 0 my $callback=shift;
336 0 my %options=@_;
337 0 $this->_sanity_check_options(\%options,{callback_behaviour=>qr{^(readonly|modify|conditional)$}});
338 0 ▶ $this->_set("client_to_server_callback_behaviour", $options{callback_behaviour}) if $options{callback_behaviour};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"callback_behaviour"} 0 0 0
339 0 return $this->_set("client_to_server_callback", $callback);
340 }
341  
342 =head2 server_to_client_callback( [callback] [,callback_behaviour=>behaviour] )
343  
344 Set a callback function to monitor/modify each message received from server.
345  
346 =head4 Parameters
347  
348 =over
349  
350 =item * callback - a reference to a function to be called for each inward message
351  
352 =item * callback_behaviour - explicitly sets the callback to readonly, modify or conditional.
353  
354 =item * Returns - the current or new setting of callback
355  
356 =back
357  
358 =head4 Usage
359  
360 If a server_to_client_callback callback is set, it will be called with a copy of each message received from the server before it is sent to the client.
361  
362 What the callback returns determines what will be sent, depending on the value of callback_behaviour:
363  
364 =over
365  
366 =item * If callback_behaviour = "readonly", the return value from the callback is ignored, and the original message is sent.
367  
368 =item * If callback_behaviour = "modify", the return value from the callback is sent instead of the original message, unless the return value is undef, in which case nothing is sent
369  
370 =item * If callback_behaviour = "conditional", which is the default, that the return value from the callback is sent instead of the original message, or if the return value is undef, then the original message is sent.
371  
372 =back
373  
374 =cut
375  
376 0 ▶sub server_to_client_callback(;$%) {
Subroutine server_to_client_callback: not called CC=2 SCAR=17.9
Pod documented
377 0 my $this=shift;
378 0 my $callback=shift;
379 0 my %options=@_;
380 0 ▶ $this->_set("server_to_client_callback_behaviour", $options{callback_behaviour}) if $options{callback_behaviour};
Branch: 1 branch
BranchTrueFalseTotal
if $options{"callback_behaviour"} 0 0 0
381 0 return $this->_set("server_to_client_callback", $callback);
382 }
383  
384 =head2 timer_callback( [interval, callback] )
385  
386 Set a callback function to be called at regular intervals
387  
388 =head4 Parameters
389  
390 =over
391  
392 =item * interval - how often the callback function is to be called - must be > 0 seconds, may be fractional
393  
394 =item * callback - a reference to a function to be called every interval seconds
395  
396 =item * Returns - the current or new setting, as an array
397  
398 =back
399  
400 =head4 Usage
401  
402 If the callback is set, it will be called every interval seconds.
403  
404 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.
405  
406 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.
407  
408 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 IPC if they are to communicate.
409  
410 Historical note: Prior to version 0.03_01, if the callback returned false, mainloop would exit and return control to the caller. (FIXME It still does.) stop_when_idle() can be used to persuade go() to exit. (FIXME check what happens if go() is called after stopping. TODO Add an unconditional stop() method)
411  
412 =cut
413  
414 #FIXME ignore return code from timer_callback. (Or deprecate this function and create a new one?)
415 #FIXME check what happens if go() is called after stopping.
416 #TODO Add an unconditional stop() method
417 #TODO - make callback optional - if the interval is set and the callback is not set, mainloop to return interval seconds after being called.
418 #TODO - Add an idle_timer callback
419  
420 1 ▶sub timer_callback(;$) {
Subroutine timer_callback: called CC=3 SCAR=11.8
Pod documented
421 1 my $this=shift;
422 1 my $interval=shift;
423 1 my $callback=shift;
424 1 ▶ if(defined $interval && $interval==0){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $interval and $interval == 0) 0 1 1
Condition33%1/3
defined $interval and $interval == 0
!ll&&!rl&&r
010
MC/DC0%0/2
defined $interval and $interval == 0
defined $interval $interval == 0
Truth table33%1/3
A and B
A= defined $intervalB= $interval == 0
ABresult
0X0
100
111
425 0 $interval=1;
426 }
427 1 $interval=$this->_set("timer_interval", $interval);
428 1 $callback=$this->_set("timer_callback", $callback);
429 1 return ($interval, $callback);
430 }
431  
432 =head2 parallel( [level] )
433  
434 Turns on/off running in parallel.
435  
436 =head4 Parameters
437  
438 =over
439  
440 =item * level - 0=serial, 1=parallel. Default is 0 (run in serial).
441  
442 =item * Returns - the current or new setting
443  
444 =back
445  
446 =head4 Usage
447  
448 If running in parallel, MitM starts a new process for each new connection using L<fork>.
449  
450 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.
451  
452 =cut
453  
454 0 ▶sub parallel(;$) {
Subroutine parallel: not called CC=2 SCAR=17.9
Pod documented
455 0 my $this=shift;
456 0 my $parallel=shift;
457 0 ▶ if($parallel){
Branch: 1 branch
BranchTrueFalseTotal
if ($parallel) 0 0 0
458 0 $SIG{CLD} = "IGNORE";
459 }
460 0 return $this->_set("parallel", $parallel);
461 }
462  
463 =head2 serial( [level] )
464  
465 Turns on/off running in serial
466  
467 =head4 Parameters
468  
469 =over
470  
471 =item * level - 0=parallel, 1=serial. Default is 1, i.e. run in serial.
472  
473 =item * Returns - the current or new setting
474  
475 =back
476  
477 =head4 Usage
478  
479 Calling this function with level=$l is exactly equivalent to calling parallel with level=!$l.
480  
481 If running in parallel, MitM starts a new process for each new connection using L<fork>.
482  
483 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.
484  
485 =cut
486  
487 0 ▶sub serial(;$) {
Subroutine serial: not called CC=3 SCAR=24.8
Pod documented
488 0 my $this=shift;
489 0 my $level=shift;
490 0 ▶ my $parallel = $this->parallel(defined $level ? ! $level : undef);
Branch: 1 branch
BranchTrueFalseTotal
defined $level ? : 0 0 0
491 0 ▶ return $parallel ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$parallel ? : 0 0 0
492 }
493  
494 =head2 log_file( [log_file_name] ] )
495  
496 log_file() sets, or clears, a log file.
497  
498 =head4 Parameters
499  
500 =over
501  
502 =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.
503  
504 =item * Returns - log file name
505  
506 =back
507  
508 =head4 Usage
509  
510 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.
511  
512 The default timestamp is "hh:mm:ss", eg 19:49:43 - see mydate() and hhmmss().
513  
514 =cut
515  
516 1 ▶sub log_file(;$) {
Subroutine log_file: called CC=9 SCAR=29.5
Pod documented
517 1 my $this=shift;
518 1 my $new_log_file=shift;
519 1 ▶ if(defined $new_log_file){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_log_file) 1 0 1
520 1 ▶ if(!$new_log_file){
Branch: 1 branch
BranchTrueFalseTotal
if (not $new_log_file) { } 0 1 1
521 0 ▶ if($this->{LOGFILE}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LOGFILE"}) 0 0 0
522 0 close($this->{LOGFILE});
523 0 $this->{log_file}=$this->{LOGFILE}=undef;
524 0 ▶ print "Logging turned off\n" if $this->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} 0 0 0
525 }
526 }else{
527 1 my $LOGFILE;
528 1 ▶ if( open($LOGFILE, ">>$new_log_file") ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $LOGFILE, ">>$new_log_file") { } 1 0 1
529 1 binmode($LOGFILE);
530 1 $LOGFILE->autoflush(1); # TODO make this configurable?
531 1 $this->{log_file}=$new_log_file;
532 1 $this->{LOGFILE}=$LOGFILE;
533 }
534 else {
535 0 ▶ print "Failed to open $new_log_file for logging: $!" if $this->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} 0 0 0
536 }
537 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
538 }
539 }
540 1 return $this->{log_file};
541 }
542  
543 =head2 stop_when_idle( boolean )
544  
545 Wait for remaining children to exit, then exit
546  
547 =head4 Parameters
548  
549 =over
550  
551 =item * flag - whether to exit when idle, or not. The default is true (exit when idle).
552  
553 =item * Returns the current status (true=exit when idle, false=keep running)
554  
555 =back
556  
557 =head4 Usage
558  
559 Causes MitM or Server to return from go() once its last child exits.
560  
561 If L<go()> is called after stop_when_idle() then L<stop_when_idle()> only takes effect after at least one child has been created.
562  
563 MitM or Server will exit immediately if there are currently no children or if MitM or Server is running in parrallel.
564 Otherwise it will stop accepting new children and exit when the last child exits.
565  
566 =cut
567  
568 sub stop_when_idle
569 0 ▶{
Subroutine stop_when_idle: not called CC=4 SCAR=30.0
Pod documented
570 0 my $this=shift;
571 0 ▶ if($this->{parent}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'parent'}) { } 0 0 0
572 0 return $this->{parent}->stop_when_idle(@_);
573 }else{
574 0 ▶ my $stop_when_idle=shift||1;
Condition0%0/2
shift() || 1
l!l
00
MC/DC0%0/1
shift() || 1
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
575 0 my $retval= $this->_set("stop_when_idle", $stop_when_idle);
576 0 ▶ $this->log("stop_when_idle set to: ",$this->{stop_when_idle}||'--undefined--');
Condition0%0/2
$this->{'stop_when_idle'} || '--undefined--'
l!l
00
MC/DC0%0/1
$this->{'stop_when_idle'} || '--undefined--'
$this->{'stop_when_idle'}
Truth table0%0/2
A
A= $this->{'stop_when_idle'}
Aresult
11
00
577 0 return $retval;
578 }
579 }
580  
581 =head2 defrag_delay( [delay] )
582  
583 Use a small delay to defragment messages
584  
585 =head4 Parameters
586  
587 =over
588  
589 =item * Delay - seconds to wait - fractions of a second are OK
590  
591 =item * Returns - the current setting.
592  
593 =back
594  
595 =head4 Usage
596  
597 Under TCPIP, there is always a risk that large messages will be fragmented in transit, and that messages sent close together may be concatenated.
598  
599 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).
600  
601 For our purposes, fragmentation and concatenation can make our logs harder to read.
602  
603 Without knowning the protocol, it's not possible to tell for sure if a message has been fragmented or concatenated.
604  
605 A small delay can be used as a way of defragmenting messages, although it increases the risk that separate messages may be concatenated.
606  
607 Eg:
608 $MitM->defrag_delay( 0.1 );
609  
610 =cut
611  
612 0 ▶sub defrag_delay(;$) {
Subroutine defrag_delay: not called CC=1 SCAR=6.9
Pod documented
613 0 my $this=shift;
614 0 my $defrag_delay=shift;
615 0 return $this->_set("defrag_delay",$defrag_delay);
616 }
617  
618 =head2 protocol( [protocol] )
619  
620 Set protocol to tcp (default) or udp
621  
622 =head4 Parameters
623  
624 =over
625  
626 =item * protocol - either 'tcp' or 'udp'
627  
628 =item * Returns - the current setting.
629  
630 =back
631  
632 =head4 Usage
633  
634 Eg:
635 $MitM->protocol( 'udp' );
636  
637 =cut
638  
639 0 ▶sub protocol(;$) {
Subroutine protocol: not called CC=1 SCAR=6.9
Pod documented
640 0 my $this=shift;
641 0 my $protocol=shift;
642 0 return $this->_set("protocol",$protocol);
643 }
644  
645 =head1 SUPPORTING SUBROUTINES/METHODS
646  
647 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.
648  
649 =head2 new_server( local_port_num, callback_function )
650  
651 Returns a very simple server, adequate for simple tasks.
652  
653 =head4 Parameters
654  
655 =over
656  
657 =item * local_port_num - the Port number to listen on
658  
659 =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
660  
661 =item * Returns - a new server
662  
663 =back
664  
665 =head4 Usage
666  
667 sub do_something($){
668 my $in = shift;
669 my $out = ...
670 return $out;
671 }
672  
673 my $server = Net::MitM->new_server(8080,\&do_something) || die;
674 $server->go();
675
676 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.
677  
678 go() does not return. You may want to L<fork> before calling it.
679  
680 See, for another example, samples/echo_server.pl in the MitM distribution.
681  
682 =cut
683  
684 1 ▶sub new_server($%) {
Subroutine new_server: called CC=3 SCAR=11.3
Pod documented
685 1 my $class=shift;
686 1 my $this=_new();
687 1 ▶ $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") 1 0 1
688 1 ▶ $this->{server_callback} = shift or croak "no callback passed";
Branch: 1 branch
BranchTrueFalseTotal
$this->{"server_callback"} = shift() or croak("no callback passed") 1 0 1
689 1 return bless $this, $class;
690 }
691  
692 =head2 new_client( remote_host, remote_port_num )
693  
694 new_client() returns a very simple client, adequate for simple tasks
695  
696 The client returned has a method, send_and_receive(), which sends a message and receives a response.
697  
698 Alternately, send_to_server() may be used to send a message, and receive_from_server() may be used to receive a message.
699  
700 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.
701  
702 =head4 Parameters
703  
704 =over
705  
706 =item * remote_ip_address - the hostname/IP address of the server
707  
708 =item * remote_port_num - the Port number of the server
709  
710 =item * Returns - a new client object
711  
712 =back
713  
714 =head4 Usage
715  
716 my $client = Net::MitM->new_client("localhost", 8080) || die("failed to start test client: $!");
717 $client->connect_to_server();
718 my $resp = $client->send_and_receive("hello");
719 ...
720  
721 See, for example, samples/client.pl or samples/clients.pl in the MitM distribution.
722  
723 =cut
724  
725 0 ▶sub new_client($%) {
Subroutine new_client: not called CC=3 SCAR=24.8
Pod documented
726 0 my $class=shift;
727 0 my $this=_new();
728 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
729 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
730 0 return bless $this, $class;
731 }
732  
733 #FIXME repetition in doco - clean it up
734  
735 =head2 log( string )
736  
737 log is a convenience function that prefixes output with a timestamp and PID information then writes to log_file.
738  
739 =head4 Parameters
740  
741 =over
742  
743 =item * string(s) - one or more strings to be logged
744  
745 =item * Returns --none--
746  
747 =back
748  
749 =head4 Usage
750  
751 log is a convenience function that prefixes output with a timestamp and PID information then writes to log_file.
752  
753 log() does nothing unless log_file is set, which by default, it is not.
754  
755 =cut
756  
757 sub log($@)
758 1 ▶{
Subroutine log: called CC=2 SCAR=7.0
Pod documented
759 1 my $this=shift;
760 1 ▶ printf {$this->{LOGFILE}} "%u/%s %s\n", $$, $this->{mydate}(), "@_" if $this->{LOGFILE};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"LOGFILE"} 1 0 1
761 1 return undef;
762 }
763  
764 =head2 echo( string(s) )
765  
766 Prints to stdout and/or the logfile
767  
768 =head4 Parameters
769  
770 =over
771  
772 =item * string(s) - one or more strings to be echoed (printed)
773  
774 =item * Returns --none--
775  
776 =back
777  
778 =head4 Usage
779  
780 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.
781  
782 =cut
783  
784 sub echo($@)
785 1 ▶{
Subroutine echo: called CC=4 SCAR=16.3
Pod documented
786 1 my $this=shift;
787 1 $this->log("@_");
788 1 ▶ return if !$this->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"verbose"} 0 1 1
789 1 ▶ confess "Did not expect not to have a name" if !$this->{name};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"name"} 0 1 1
790 1 ▶ if($_[0] =~ m/^[<>]{3}$/){
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^[<>]{3}$/) { } 0 1 1
791 0 my $prefix=shift;
792 0 my $msg=join "", @_;
793 0 chomp $msg;
794 0 printf("%s: %u/%s %s %s\n", $this->{name}, $$, $this->{mydate}(), $prefix, $msg);
795 }else{
796 1 printf("%s: %u/%s\n", $this->{name}, $$, join(" ", $this->{mydate}(), @_));
797 }
798 1 return undef;
799 }
800  
801 =head2 send_to_server( string(s) )
802  
803 send_to_server() sends a message to the server
804  
805 =head4 Parameters
806  
807 =over
808  
809 =item * string(s) - one or more strings to be sent
810  
811 =item * Return: true if successful
812  
813 =back
814  
815 =head4 Usage
816  
817 If a callback is set, it will be called before the message is sent.
818  
819 send_to_server() may 'die' if it detects a failure to send.
820  
821 =cut
822  
823 sub _do_callback($$)
824 0 ▶{
Subroutine _do_callback: not called CC=7 SCAR=40.3
825 0 my $this=shift;
826 0 my $direction = shift;
827 0 my $msg = shift;
828 0 my $callback = $this->{$direction."_callback"};
829 0 ▶ if($callback){
Branch: 1 branch
BranchTrueFalseTotal
if ($callback) 0 0 0
830 0 ▶ $this->echo("calling $direction callback ($msg)\n") if $this->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 1 0 0 0
831 0 my $new_msg = $callback->($msg,$this);
832 #warn "~~~ ",$new_msg||"--undef--","\n";
833 0 ▶ my $callback_behaviour = $this->{$direction."_callback_behaviour"} || 'conditional';
Condition0%0/2
$$this{$direction . '_callback_behaviour'} || 'conditional'
l!l
00
MC/DC0%0/1
$$this{$direction . '_callback_behaviour'} || 'conditional'
$$this{$direction . '_callback_behaviour'}
Truth table0%0/2
A
A= $$this{$direction . '_callback_behaviour'}
Aresult
11
00
834 #warn ("callback behaviour is ($callback_behaviour)\n") if $this->{verbose}>1;
835 0 ▶ if($callback_behaviour eq 'modify' || ($callback_behaviour ne 'readonly' && defined $new_msg)){
Branch: 1 branch
BranchTrueFalseTotal
if ($callback_behaviour eq "modify" or $callback_behaviour ne "readonly" and defined $new_msg) 0 0 0
Condition0%0/6
$callback_behaviour eq "modify" or $callback_behaviour ne "readonly" and defined $new_msg
l!l&&r!l&&!r
000
$callback_behaviour ne "readonly" and defined $new_msg
!ll&&!rl&&r
000
MC/DC0%0/3
$callback_behaviour eq "modify" or $callback_behaviour ne "readonly" and defined $new_msg
$callback_behaviour eq "modify" $callback_behaviour ne "readonly" defined $new_msg
Truth table0%0/4
A or B and C
A= $callback_behaviour eq "modify"B= $callback_behaviour ne "readonly"C= defined $new_msg
ABCresult
1XX1
0111
00X0
0100
836 0 $msg = $new_msg;
837 }
838 }
839 #warn "+++ ",$msg||"--undef--","\n";
840 0 return $msg;
841 }
842  
843 sub _logmsg
844 0 ▶{
Subroutine _logmsg: not called CC=2 SCAR=17.9
845 0 my $this = shift;
846 0 my $direction = shift;
847 0 my $msg = shift;
848 0 ▶ if($this->{verbose}>1){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'verbose'} > 1) { } 0 0 0
849 0 $this->echo($direction,"(".length($msg)." bytes) {$msg}\n");
850 }else{
851 # don't print the whole message by default, in case it is either binary &/or long
852 0 $this->echo($direction,"(".length($msg)." bytes)\n");
853 0 $this->log($direction," {{{$msg}}}\n");
854 }
855 }
856  
857 sub send_to_server($@)
858 0 ▶{
Subroutine send_to_server: not called CC=5 SCAR=34.0
Pod documented
859 0 my $this = shift;
860 0 my $msg = shift;
861 0 $this->connect_to_server();
862 0 $msg = $this->_do_callback( 'client_to_server', $msg );
863 0 ▶ if(!defined $msg){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
864 0 ▶ warn "client to server callback says no\n" if $this->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 1 0 0 0
865 0 return undef;
866 }
867 0 $this->_logmsg(">>>",$msg);
868 0 ▶ confess "SERVER being null was unexpected" if !$this->{SERVER};
Branch: 1 branch
BranchTrueFalseTotal
unless $this->{"SERVER"} 0 0 0
869 0 ▶ print({$this->{SERVER}} $msg) || die "Can't send to server: $?";
Branch: 1 branch
BranchTrueFalseTotal
print {$this->{"SERVER"};} $msg or die "Can't send to server: $?" 0 0 0
870 0 return undef;
871 }
872  
873 =head2 send_to_client( string(s) )
874  
875 Sends a message to the client
876  
877 =head4 Parameters
878  
879 =over
880  
881 =item * string(s) - one or more strings to be sent
882  
883 =item * Return: true if successful
884  
885 =back
886  
887 =head4 Usage
888  
889 If a callback is set, it will be called before the message is sent.
890  
891 =cut
892  
893 sub _send_to_client($@)
894 0 ▶{
Subroutine _send_to_client: not called CC=1 SCAR=6.9
895 0 my $this = shift;
896 0 my $msg = shift;
897 0 $this->_logmsg("<<<",$msg);
898 0 return print({$this->{CLIENT}} $msg);
899 }
900  
901 sub send_to_client($@)
902 0 ▶{
Subroutine send_to_client: not called CC=3 SCAR=24.8
Pod documented
903 0 my $this = shift;
904 0 my $msg = shift;
905 0 $msg = $this->_do_callback( 'server_to_client', $msg );
906 0 ▶ if(!defined $msg){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
907 0 ▶ warn "server to client callback says no\n" if $this->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 1 0 0 0
908 return undef
909 0 }
910 0 return $this->_send_to_client($msg);
911 }
912  
913 =head2 receive_from_server( )
914  
915 Receives a message from the server
916  
917 =head4 Parameters
918  
919 =over
920  
921 =item * --none--
922  
923 =item * Returns - the message read, or undef if the server disconnected.
924  
925 =back
926  
927 =head4 Usage
928  
929 Blocks until a message is received.
930  
931 This method used to be called read_from_server(), and may still be called via that name.
932  
933 =cut
934  
935 sub receive_from_server()
936 0 ▶{
Subroutine receive_from_server: not called CC=3 SCAR=24.8
Pod documented
937 0 my $this=shift;
938 0 my $msg;
939 0 ▶ sysread($this->{SERVER},$msg,100000) or confess "Fatal: sysread failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
sysread $this->{"SERVER"}, $msg, 100000 or confess("Fatal: sysread failed: $!") 0 0 0
940 0 ▶ if(length($msg) == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (length $msg == 0) 0 0 0
941 {
942 0 $this->echo("Server disconnected\n");
943 0 return undef;
944 }
945 0 return $msg;
946 }
947  
948 =head2 read_from_server( ) [Deprecated]
949  
950 use instead: receive_from_server( )
951  
952 =cut
953  
954 0 ▶sub read_from_server() { my $this=shift;return $this->receive_from_server(); }
Subroutine read_from_server: not called CC=1 SCAR=6.9
Pod documented
955  
956 =head2 send_and_receive( )
957  
958 Sends a message to the server and receives a response
959  
960 =head4 Parameters
961  
962 =over
963  
964 =item * the message(s) to be sent
965  
966 =item * Returns - message read, or undef if the server disconnected.
967  
968 =back
969  
970 =head4 Usage
971  
972 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.
973  
974 =cut
975  
976 sub send_and_receive($)
977 0 ▶{
Subroutine send_and_receive: not called CC=1 SCAR=6.9
Pod documented
978 0 my $this=shift;
979 0 $this->send_to_server(@_);
980 0 return $this->receive_from_server();
981 }
982  
983 =head2 connect_to_server( )
984  
985 Connects to the server
986  
987 =head4 Parameters
988  
989 =over
990  
991 =item * --none--
992  
993 =item * Returns true if successful
994  
995 =back
996  
997 =head4 Usage
998  
999 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.
1000  
1001 Changed in v0.03_01: return true/false if connect successful/unsuccessful. Previously died if connect fails. Failure to resolve remote internet address/port address is still fatal.
1002  
1003 =cut
1004  
1005 # TODO would be nice to have a way to specify backup server(s) if 1st connection fails. Also nice to have a way to specify round-robin servers for load balancing.
1006  
1007 sub _socket($)
1008 1 ▶{
Subroutine _socket: called CC=5 SCAR=22.7
1009 1 my $this=shift;
1010 1 my $socket=shift;
1011 1 ▶ my $protocol = $this->{protocol}||'tcp';
Condition50%1/2
$this->{'protocol'} || 'tcp'
l!l
01
MC/DC0%0/1
$this->{'protocol'} || 'tcp'
$this->{'protocol'}
Truth table50%1/2
A
A= $this->{'protocol'}
Aresult
11
00
1012 1 ▶ my $proto = getprotobyname($protocol) or die "Can't getprotobyname\n";
Branch: 1 branch
BranchTrueFalseTotal
my $proto = getprotobyname $protocol or die "Can't getprotobyname\n" 0 1 1
1013 0 ▶ my $sock = $protocol eq 'udp' ? SOCK_DGRAM : SOCK_STREAM ;
Branch: 1 branch
BranchTrueFalseTotal
$protocol eq 'udp' ? : 0 0 0
1014
1015 0 ▶ socket($this->{$socket}, PF_INET, $sock, $proto) or confess "Fatal: Can't create $protocol socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
socket $this->{$socket}, 2, $sock, $proto or confess("Fatal: Can't create $protocol socket: $!") 0 0 0
1016 }
1017  
1018 sub connect_to_server()
1019 0 ▶{
Subroutine connect_to_server: not called CC=6 SCAR=37.4
Pod documented
1020 0 my $this=shift;
1021 0 ▶ return if $this->{SERVER};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"SERVER"} 0 0 0
1022 0 $this->_socket("SERVER");
1023 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
1024 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
1025 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
1026 0 $this->echo("Connecting to $this->{remote_ip_address}\:$this->{remote_port_num} [verbose=$this->{verbose}]\n");
1027 0 ▶ my $connect = connect($this->{SERVER}, $remote_port_address) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $connect = connect($this->{"SERVER"}, $remote_port_address) or return undef 0 0 0
1028 0 $this->{SERVER}->autoflush(1);
1029 0 binmode($this->{SERVER});
1030 0 return $connect;
1031 }
1032  
1033 =head2 disconnect_from_server( )
1034  
1035 Disconnects from the server
1036  
1037 =head4 Parameters
1038  
1039 =over
1040  
1041 =item * --none--
1042  
1043 =item * Returns --none--
1044  
1045 =back
1046  
1047 =head4 Usage
1048  
1049 disconnect_from_server closes any connections.
1050  
1051 It is only intended to be called on clients.
1052  
1053 For MitM, like for any server, disconnection is best triggered by the other party disconnecting, not by the server. If a server disconnects while it has an active client connection open and exits or otherwise stops listening, it will not be able to reopen the same port for listening until the old connection has timed out which can take up to a few minutes.
1054  
1055 =cut
1056  
1057 sub disconnect_from_server()
1058 0 ▶{
Subroutine disconnect_from_server: not called CC=1 SCAR=6.9
Pod documented
1059 0 my $this=shift;
1060 0 $this->log("initiating disconnect");
1061 0 $this->_destroy();
1062 0 return undef;
1063 }
1064  
1065 0 ▶sub _pause($){
Subroutine _pause: not called CC=1 SCAR=6.9
1066 # warning - select may return early if, for eg, process catches a signal (if it survives the signal)
1067 0 select undef,undef,undef,shift;
1068 0 return undef;
1069 }
1070  
1071 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
1072 0 my $this=shift;
1073 # optional sleep to reduce risk of split messages
1074 0 ▶ _pause($this->{defrag_delay}) if $this->{defrag_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"defrag_delay"} 0 0 0
1075 # 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.
1076 0 my $msg;
1077 0 sysread($this->{CLIENT},$msg,10000);
1078 # (0 length message means connection closed)
1079 0 ▶ if(length($msg) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $msg == 0) 0 0 0
1080 0 $this->echo("Client disconnected\n");
1081 0 $this->_destroy();
1082 0 return undef;
1083 }
1084 # Send message to server, if any. Else 'send' to callback function and return result to client.
1085 0 ▶ if($this->{SERVER}){
Branch: 2 branches
BranchTrueFalseTotal
if ($this->{'SERVER'}) { } 0 0 0
elsif ($this->{'server_callback'}) { } 0 0 0
1086 0 $this->send_to_server($msg);
1087 }elsif($this->{server_callback}){
1088 0 $this->send_to_client( $this->{server_callback}($msg) );
1089 }else{
1090 0 confess "$this->{name}: Did not expect to have neither a connection to a SERVER nor a server_callback";
1091 }
1092 0 return undef;
1093 }
1094  
1095 =head2 graceful_shut_down( )
1096  
1097 Shut down the server gracefully
1098  
1099 =head4 Parameters
1100  
1101 =over
1102  
1103 =item * --none--
1104  
1105 =item * Returns --none--
1106  
1107 =back
1108  
1109 =head4 Usage
1110  
1111 graceful_shut_down closes the LISTEN socket so that no more clients will be accepted. When the last client has exited, mainloop will exit.
1112  
1113 If running in parallel mode, graceful_shut_down will take effect immediately, the children will keep running. This might change in a future release.
1114  
1115 =cut
1116  
1117 sub graceful_shut_down()
1118 0 ▶{
Subroutine graceful_shut_down: not called CC=1 SCAR=6.9
Pod documented
1119 0 my $this=shift;
1120 0 $this->log("initiating disconnect");
1121 0 $this->_destroy_fh("LISTEN");
1122 0 return undef;
1123 }
1124  
1125 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
1126 0 my $this=shift;
1127 # sleep to avoid splitting messages
1128 0 ▶ _pause($this->{defrag_delay}) if $this->{defrag_delay};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"defrag_delay"} 0 0 0
1129 # Read from SERVER and copy to CLIENT
1130 0 my $msg = $this->receive_from_server();
1131 0 ▶ if(!defined $msg){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $msg) 0 0 0
1132 0 $this->_destroy();
1133 0 return undef;
1134 }
1135 0 $this->send_to_client($msg);
1136 0 return undef;
1137 }
1138  
1139 sub _cull_child()
1140 0 ▶{
Subroutine _cull_child: not called CC=6 SCAR=37.4
1141 0 ▶ my $this=shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $this = shift() or die 0 0 0
1142 0 ▶ my $child=shift or die;
Branch: 1 branch
BranchTrueFalseTotal
my $child = shift() or die 0 0 0
1143 0 for my $i (0 .. @{$this->{children}}){
1144 0 ▶ if($child==$this->{children}[$i]){
Branch: 1 branch
BranchTrueFalseTotal
if ($child == $this->{"children"}[$i]) 0 0 0
1145 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
1146 0 splice @{$this->{children}}, $i,1;
1147 0 return;
1148 }
1149 }
1150 0 confess "Child $child->{name} is finished, but I can't find it to clean it up";
1151 }
1152  
1153 # _main_loop is called both by listeners and by forked children. When called by listeners, it also includes any children running in serial
1154  
1155 3 my $warned_about_deprecation=0;
1156 sub _main_loop()
1157 0 ▶{
Subroutine _main_loop: not called CC=35 SCAR=71.4
1158 0 my $this=shift;
1159 0 my $last_time;
1160 my $target_time;
1161 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
1162 0 $last_time=time();
1163 0 $target_time=$last_time+$this->{timer_interval};
1164 }
1165 # Main Loop
1166 0 MAINLOOP: while(1)
1167 {
1168 # Build file descriptor list for select call
1169 0 my $rin = "";
1170 0 ▶ if($this->{LISTEN}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"LISTEN"}) 0 0 0
1171 0 ▶ confess "LISTEN is unexpectedly not a filehandle" if !fileno($this->{LISTEN});
Branch: 1 branch
BranchTrueFalseTotal
unless fileno $this->{"LISTEN"} 0 0 0
1172 0 vec($rin, fileno($this->{LISTEN}), 1) = 1;
1173 }
1174 0 foreach my $each ($this, @{$this->{children}}) {
1175 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
1176 0 ▶ vec($rin, fileno($each->{SERVER}), 1) = 1 if $each->{SERVER};
Branch: 1 branch
BranchTrueFalseTotal
if $each->{"SERVER"} 0 0 0
1177 }
1178 # and listen...
1179 0 my $rout = $rin;
1180 0 my $delay;
1181 0 ▶ if($this->{timer_interval}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{'timer_interval'}) { } 0 0 0
1182 0 ▶ if(time() > $target_time){
Branch: 1 branch
BranchTrueFalseTotal
if (&Net::MitM::time() > $target_time) 0 0 0
1183 0 my $resp = $this->{timer_callback}($this);
1184 0 ▶ if($resp){
Branch: 1 branch
BranchTrueFalseTotal
if ($resp) { } 0 0 0
1185 # TODO Add a deprecated warning?
1186 }else{
1187 0 last MAINLOOP;
1188 }
1189 0 $last_time=$target_time;
1190 0 $target_time+=$this->{timer_interval};
1191 }
1192 0 $delay=$target_time-time();
1193 0 ▶ $delay=0 if($delay<0);
Branch: 1 branch
BranchTrueFalseTotal
if $delay < 0 0 0 0
1194 0 ▶ $this->echo("delay=$delay") if $this->{verbose} > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 1 0 0 0
1195 }else{
1196 0 $delay=undef;
1197 }
1198 0 my $status=select( $rout, "", "", $delay );
1199 0 ▶ if($status==-1){
Branch: 1 branch
BranchTrueFalseTotal
if ($status == -1) 0 0 0
1200 0 warn "something happened - were we signalled? if so, why do we live?\n";
1201 }
1202 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
1203 0 my $child = $this->_spawn_child();
1204 0 ▶ push @{$this->{children}}, $child if $child;
Branch: 1 branch
BranchTrueFalseTotal
if $child 0 0 0
1205 0 next;
1206 }
1207 0 CHILDREN: foreach my $each($this, @{$this->{children}}) {
1208 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
1209 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
1210 0 $each->_message_from_client_to_server(); # TODO Too many too similar sub names
1211 0 ▶ if(!$each->{CLIENT}){
Branch: 1 branch
BranchTrueFalseTotal
if (not $each->{'CLIENT'}) { } 0 0 0
1212 0 $each->log("No Client\n");
1213 # client has disconnected
1214 0 ▶ if($each==$this){
Branch: 1 branch
BranchTrueFalseTotal
if ($each == $this) { } 0 0 0
1215 # we are the child - OK to exit
1216 0 $each->log("We are the child");
1217 0 return;
1218 }else{
1219 # we are the parent - clean up child
1220 0 $each->log("We are the parent");
1221 0 ▶ $each->log("stop_when_idle is: ",$this->{stop_when_idle}||'--undefined--');
Condition0%0/2
$this->{'stop_when_idle'} || '--undefined--'
l!l
00
MC/DC0%0/1
$this->{'stop_when_idle'} || '--undefined--'
$this->{'stop_when_idle'}
Truth table0%0/2
A
A= $this->{'stop_when_idle'}
Aresult
11
00
1222 0 $each->log("number of children (before cull): ",scalar(@{$this->{children}}));
1223 0 $this->_cull_child($each);
1224 0 $each->log("number of children (after cull): ",scalar(@{$this->{children}}));
1225 # keep going?
1226 0 ▶ if($this->{stop_when_idle} && (!@{$this->{children}})){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"stop_when_idle"} and not @{$this->{"children"};}) 0 0 0
Condition0%0/3
$this->{"stop_when_idle"} and not @{$this->{"children"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"stop_when_idle"} and not @{$this->{"children"};}
$this->{"stop_when_idle"} not @{$this->{"children"};}
Truth table0%0/3
A and not(B)
A= $this->{"stop_when_idle"}B= not @{$this->{"children"};}
ABresult
0X0
100
111
1227 0 $this->log("idle exiting mainloop");
1228 0 return undef;
1229 }
1230 0 ▶ $each->log("continuing: ",$this->{stop_when_idle}?'y':'n',!@{$this->{children}});
Branch: 1 branch
BranchTrueFalseTotal
$this->{'stop_when_idle'} ? : 0 0 0
1231 0 last CHILDREN; # _cull_child impacts the children array - not safe to continue without regenerating rout
1232 }
1233 }else{
1234 0 ▶ $each->echo("We still have a client") if $this->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"verbose"} > 1 0 0 0
1235 }
1236 }
1237 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
1238 0 $each->_message_from_server_to_client(); # TODO Too many too similar sub names
1239 0 ▶ if(!$each->{SERVER}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($each->{"SERVER"}) 0 0 0
1240 # server has disconnected
1241 0 ▶ if($each==$this){
Branch: 1 branch
BranchTrueFalseTotal
if ($each == $this) { } 0 0 0
1242 # we are the child - OK to exit
1243 0 return; #might be better to die or exit at this point instead?
1244 }else{
1245 0 $this->_cull_child($each);
1246 0 ▶ if($this->{stop_when_idle} && !@{$this->{children}}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"stop_when_idle"} and not @{$this->{"children"};}) 0 0 0
Condition0%0/3
$this->{"stop_when_idle"} and not @{$this->{"children"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$this->{"stop_when_idle"} and not @{$this->{"children"};}
$this->{"stop_when_idle"} not @{$this->{"children"};}
Truth table0%0/3
A and not(B)
A= $this->{"stop_when_idle"}B= not @{$this->{"children"};}
ABresult
0X0
100
111
1247 0 $this->log("idle exiting mainloop - server disconnected");
1248 0 return undef;
1249 }
1250 0 last CHILDREN; # _cull_child impacts the children array - not safe to continue without regenerating rout
1251 }
1252 }
1253 }
1254 } # foreach CHILDREN
1255 }
1256 0 return undef;
1257 }
1258  
1259 =head2 hhmmss( )
1260  
1261 The default timestamp function - returns localtime in hh:mm:ss format
1262  
1263 =head4 Parameters
1264  
1265 =over
1266  
1267 =item * --none--
1268  
1269 =item * Returns - current time in hh:mm:ss format
1270  
1271 =back
1272  
1273 =head4 Usage
1274  
1275 This function is, by default, called when a message is written to the log file.
1276  
1277 It may be overridden by calling mydate().
1278  
1279 =cut
1280  
1281 sub hhmmss()
1282 2 ▶{
Subroutine hhmmss: called CC=1 SCAR=0.0
Pod documented
1283 2 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
1284 2 return sprintf "%02d:%02d:%02d",$hour,$min,$sec;
1285 }
1286  
1287 =head2 mydate( )
1288  
1289 Override the standard hh:mm:ss datestamp
1290  
1291 =head4 Parameters
1292  
1293 =over
1294  
1295 =item * datestamp_callback - a reference to a function that returns a datestamp
1296  
1297 =item * Returns - a reference to the current or updated callback function
1298  
1299 =back
1300  
1301 =head4 Usage
1302  
1303 For example:
1304  
1305 sub yymmddhhmmss {
1306 my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime();
1307 return sprintf "%02d/%02d/%02d %02d:%02d:%02d",
1308 $year+1900,$mon+1,$mday,$hour,$min,$sec;
1309 }
1310 mydate(\&yymmddhhmmss);
1311  
1312 =cut
1313  
1314 sub mydate(;$)
1315 0 ▶{
Subroutine mydate: not called CC=3 SCAR=24.8
Pod documented
1316 0 my $this=shift;
1317 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
1318 0 ▶ if(defined $mydate){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mydate) 0 0 0
1319 0 $this->{mydate} = $mydate;
1320 }
1321 0 return $this->{mydate};
1322 }
1323  
1324 =head2 listen( )
1325  
1326 Listen on local_port and prepare to accept incoming connections
1327  
1328 =head4 Parameters
1329  
1330 =over
1331  
1332 =item * --none--
1333  
1334 =item * Return --none--
1335  
1336 =back
1337  
1338 =head4 Usage
1339  
1340 This method is called by go(). It only needs to be called directly if go() is being bypassed for some reason.
1341  
1342 =cut
1343  
1344 sub listen()
1345 1 ▶{
Subroutine listen: called CC=5 SCAR=24.1
Pod documented
1346 1 my $this=shift;
1347 1 ▶ return if $this->{LISTEN};
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"LISTEN"} 0 1 1
1348 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
1349 1 $this->_socket("LISTEN");
1350 0 ▶ bind($this->{LISTEN}, sockaddr_in($this->{local_port_num}, INADDR_ANY)) or confess "Fatal: $this->{name} can't bind LISTEN socket [$this->{LISTEN}] to $this->{local_port_num}: (",$!+0,") $!";
Branch: 1 branch
BranchTrueFalseTotal
bind $this->{"LISTEN"}, sockaddr_in($this->{"local_port_num"}, "\0\0\0\0") or confess(qq[Fatal: $this->{"name"} can't bind LISTEN socket [$this->{"LISTEN"}] to $this->{"local_port_num"}: (], $! + 0, ") $!") 0 0 0
1351 0 ▶ listen($this->{LISTEN},1) or confess "Fatal: Can't listen to socket: $!";
Branch: 1 branch
BranchTrueFalseTotal
CORE::listen $this->{"LISTEN"}, 1 or confess("Fatal: Can't listen to socket: $!") 0 0 0
1352 0 $this->echo("Waiting on port $this->{local_port_num}\n");
1353 0 return undef;
1354 }
1355  
1356 sub _accept($)
1357 0 ▶{
Subroutine _accept: not called CC=4 SCAR=30.0
1358 # Accept a new connection
1359 0 my $this=shift;
1360 0 my $LISTEN=shift;
1361 0 ▶ my $client_paddr = accept($this->{CLIENT}, $LISTEN) or confess "accept failed: $!";
Branch: 1 branch
BranchTrueFalseTotal
my $client_paddr = accept($this->{"CLIENT"}, $LISTEN) or confess("accept failed: $!") 0 0 0
1362 0 $this->{CLIENT}->autoflush(1);
1363 0 binmode($this->{CLIENT});
1364 0 my ($client_port, $client_iaddr) = sockaddr_in( $client_paddr );
1365 0 $this->log("Connection accepted from", inet_ntoa($client_iaddr).":$client_port\n");
1366 0 ▶ if($this->{remote_ip_address}){
Branch: 1 branch
BranchTrueFalseTotal
if ($this->{"remote_ip_address"}) 0 0 0
1367 0 ▶ $this->connect_to_server() or confess "Fatal: Can't connect to $this->{remote_ip_address}:$this->{remote_port_num}: $!";
Branch: 1 branch
BranchTrueFalseTotal
$this->connect_to_server or confess(qq[Fatal: Can't connect to $this->{"remote_ip_address"}:$this->{"remote_port_num"}: $!]) 0 0 0
1368 }
1369 0 $this->{client_port} = $client_port;
1370 0 $this->{client_iaddr} = inet_ntoa($client_iaddr);
1371 0 return undef;
1372 }
1373  
1374 1 ▶sub _new_child(){
Subroutine _new_child: called CC=7 SCAR=19.8
1375 1 my $parent=shift;
1376 1 my $child=_new();
1377 1 my $all_good=1;
1378 1 foreach my $key (keys %{$parent}){
1379 10 ▶ if($key=~m/^(LISTEN|children|connections|timer_interval|timer_callback|is_running|stop_when_idle)$/){
Branch: 4 branches
BranchTrueFalseTotal
if ($key =~ /^(LISTEN|children|connections|timer_interval|timer_callback|is_running|stop_when_idle)$/) { } 2 8 10
elsif ($key =~ /^(parallel|log_file|verbose|mydate|(client_to_server|server_to_client|server)_callback(_behaviour)?|(local|remote)_(port_num|ip_address)|protocol)$/) { } 6 2 8
elsif ($key =~ /^(name)$/) { } 1 1 2
elsif ($key eq 'LOGFILE') { } 1 0 1
1380 # do nothing - these parameters are not inherited
1381 }elsif($key =~ m/^(parallel|log_file|verbose|mydate|(client_to_server|server_to_client|server)_callback(_behaviour)?|(local|remote)_(port_num|ip_address)|protocol)$/){
1382 6 $child->{$key}=$parent->{$key};
1383 }elsif($key =~ m/^(name)$/){
1384 1 $child->{$key}=$parent->{$key}.".jr";
1385 }elsif($key eq "LOGFILE"){
1386 # TODO might want to have a different logfile for each child, or at least, an option to do so.
1387 1 $child->{$key}=$parent->{$key};
1388 }else{
1389 0 warn "internal error - unexpected attribute: $key = {$parent->$key}\n";
1390 0 $all_good=0;
1391 }
1392 }
1393 1 ▶ die "Internal error in _new_child()" unless $all_good;
Branch: 1 branch
BranchTrueFalseTotal
unless $all_good 0 1 1
1394 1 $child->{parent}=$parent;
1395 1 return bless $child;
1396 }
1397  
1398 0 ▶sub _spawn_child(){
Subroutine _spawn_child: not called CC=7 SCAR=40.3
1399 0 my $this=shift;
1400 0 my $child = $this->_new_child();
1401 0 $child->_accept($this->{LISTEN});
1402 0 ▶ confess "We have a child with no CLIENT\n" if !$child->{CLIENT};
Branch: 1 branch
BranchTrueFalseTotal
unless $child->{"CLIENT"} 0 0 0
1403 # hand-off the connection
1404 0 $this->echo("starting connection:",++$this->{connections});
1405 0 ▶ if(!$this->{parallel}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"parallel"}) 0 0 0
1406 0 return $child;
1407 }
1408 0 my $pid = fork();
1409 0 ▶ if(!defined $pid){
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif (not $pid) { } 0 0 0
1410 # Error
1411 0 $this->echo("Cannot fork!: $!\nNew connection will run in the current thread\n");
1412 0 return $child;
1413 }elsif(!$pid){
1414 # This is the child process
1415 0 ▶ $child->echo(sprintf"Running %u",$$) if $child->{verbose}>1;
Branch: 1 branch
BranchTrueFalseTotal
if $child->{"verbose"} > 1 0 0 0
1416 0 ▶ confess "We have a child with no CLIENT\n" if !$child->{CLIENT};
Branch: 1 branch
BranchTrueFalseTotal
unless $child->{"CLIENT"} 0 0 0
1417 # The active instance of the parent is potentially in a different process
1418 # Ideally, we would have the parent go out of scope, but we can clean up the bits that matter
1419 0 close $this->{LISTEN};
1420 0 $this->{LISTEN} = undef;
1421 0 $child->_main_loop();
1422 0 $child->log(sprintf"Exiting %u",$$);
1423 0 exit;
1424 }else{
1425 # This is the parent process. The active child instance is in its own process, we clean up what we can
1426 0 $child->_destroy();
1427 0 return undef;
1428 }
1429 }
1430  
1431 sub go()
1432 0 ▶{
Subroutine go: not called CC=1 SCAR=6.9
Pod documented
1433 0 my $this=shift;
1434 0 $this->log("go");
1435 0 $this->listen();
1436 0 $this->_main_loop();
1437 0 $this->log("stopped");
1438 0 return undef;
1439 }
1440  
1441 #sub _destroy_fh() { my $this=shift; my $file_handle=shift; if($this->{$file_handle}){ $this->log( "$this->{name}: closing $file_handle socket ". ($this->{local_port_num}||"")."\n") if $this->{verbose}; close $this->{$file_handle} or die; $this->{$file_handle}=undef; } return undef; }
1442  
1443 sub _destroy()
1444 0 ▶{
Subroutine _destroy: not called CC=4 SCAR=30.0
1445 0 my $this=shift;
1446 # TODO? Tell children that they are being shutdown?
1447 0 ▶ close $this->{LISTEN} if($this->{LISTEN});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"LISTEN"} 0 0 0
1448 0 ▶ close $this->{CLIENT} if($this->{CLIENT});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"CLIENT"} 0 0 0
1449 0 ▶ close $this->{SERVER} if($this->{SERVER});
Branch: 1 branch
BranchTrueFalseTotal
if $this->{"SERVER"} 0 0 0
1450 0 $this->{LISTEN}=$this->{SERVER}=$this->{CLIENT}=undef;
1451 0 return undef;
1452 }
1453  
1454 =head1 Exports
1455  
1456 MitM does not export any functions or variables.
1457 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.
1458  
1459 =head1 AUTHOR
1460  
1461 Ben AVELING, C<< <ben dot aveling at optusnet dot com dot au> >>
1462  
1463 =head1 BUGS
1464  
1465 Please report any bugs or feature requests to C<bug-Net-MitM at rt.cpan.org>, or through
1466 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Net-MitM>. I will be notified, and then you'll
1467 automatically be notified of progress on your bug as I make changes.
1468  
1469 =head1 SUPPORT
1470  
1471 You can find documentation for this module with the perldoc command.
1472  
1473 perldoc Net::MitM
1474  
1475 You can also look for information at:
1476  
1477 =over
1478  
1479 =item * RT: CPAN's request tracker (report bugs here)
1480  
1481 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Net-MitM>
1482  
1483 =item * AnnoCPAN: Annotated CPAN documentation
1484  
1485 L<http://annocpan.org/dist/Net-MitM>
1486  
1487 =item * CPAN Ratings
1488  
1489 L<http://cpanratings.perl.org/d/Net-MitM>
1490  
1491 =item * Search CPAN
1492  
1493 L<http://search.cpan.org/dist/Net-MitM/>
1494  
1495 =back
1496  
1497 =head1 ACKNOWLEDGEMENTS
1498  
1499 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>).
1500 It got me started. Recommend. RIP.
1501 The Blue Camel Book is also pretty useful, and Langworth & chromatic's "Perl Testing, A Developer's Notebook" is also worth a hat tip.
1502  
1503 =head1 ALTERNATIVES
1504  
1505 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>.
1506  
1507 And if you want a full "portable multitasking and networking framework for any event loop", you may be looking for L<POE>.
1508  
1509 =head1 LICENSE AND COPYRIGHT
1510  
1511 Copyleft 2013 Ben AVELING.
1512  
1513 This program is free software; you can redistribute it and/or modify it
1514 under the terms of the the Artistic License (2.0). You may obtain a
1515 copy of the full license at:
1516  
1517 L<http://www.perlfoundation.org/artistic_license_2_0>
1518  
1519 Any use, modification, and distribution of the Standard or Modified
1520 Versions is governed by this Artistic License. By using, modifying or
1521 distributing the Package, you accept this license. Do not use, modify,
1522 or distribute the Package, if you do not accept this license.
1523  
1524 If your Modified Version has been derived from a Modified Version made
1525 by someone other than you, you are nevertheless required to ensure that
1526 your Modified Version complies with the requirements of this license.
1527  
1528 This license does not grant you the right to use any trademark, service
1529 mark, tradename, or logo of the Copyright Holder.
1530  
1531 This license includes the non-exclusive, worldwide, free-of-charge
1532 patent license to make, have made, have, hold and cherish,
1533 use, offer to use, sell, offer to sell, import and
1534 otherwise transfer the Package with respect to any patent claims
1535 licensable by the Copyright Holder that are necessarily infringed by the
1536 Package. If you institute patent litigation (including a cross-claim or
1537 counterclaim) against any party alleging that the Package constitutes
1538 direct or contributory patent infringement, then this Artistic License
1539 to you shall terminate on the date that such litigation is filed.
1540  
1541 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
1542 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
1543 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
1544 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
1545 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
1546 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
1547 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
1548 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. SO THERE.
1549  
1550 =cut
1551  
1552 3 1; # End of Net::MitM
↑ Summary Mediator.pm »