Analysis/TCPMonologue.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 16 totaltotal 0.0% 0 / 164 CC 7 SCAR 17.6
CC
7
Cov
0%
which_pkts
20.0
add_packet
12.0
as_string
6.0
CRAP 5.8
1 package Net::Analysis::TCPMonologue;
2 # $Id: TCPMonologue.pm 136 2005-10-21 00:14:54Z abworrall $
3  
4 use 5.008000;
5 our $VERSION = '0.02';
6 use strict;
7 use warnings;
8  
9 use overload
10 q("") => sub { $_[0]->as_string() },
11 'eq' => sub { return "$_[0]" eq "$_[1]" }; # Needed for Test::is_deeply
12  
13 use Carp qw(carp croak confess);
14 use Params::Validate qw(:all);
15  
16 use Net::Analysis::Packet qw(:all);
17  
18 # {{{ POD
19  
20 =head1 NAME
21  
22 Net::Analysis::TCPMonologue - simple class to contain a TCP monologue
23  
24 =head1 SYNOPSIS
25  
26 use Net::Analysis::Packet;
27 use Net::Analysis::Monologue;
28  
29 my $mono = Net::Analysis::Monologue->new();
30 $mono->add_packet($pkt);
31  
32 if ($mono->data() =~ /foo/) {
33 print "Mono contained 'foo'\n";
34 }
35  
36 print "Monologue was " .$mono->length().
37 "b long, over " .$mono->t_elapsed ()." seconds\n";
38  
39 =head1 DESCRIPTION
40  
41 A TCP monologue is a series of packets that travel in one direction, e.g. a
42 HTTP response. A monologue ends when a data packet travels in the other
43 direction. Pairs of monologues will often be linked as transactions.
44  
45 As packets are added, this object updates some info. It assumes that the packet
46 you've added belongs in the monologue, and that you're adding them in the
47 correct order.
48  
49 The payload of the monologue lives in C<$mono->{data}>.
50  
51 =head1 METHODS
52  
53 =cut
54  
55 # }}}
56  
57 #### Public methods
58 #
59 # {{{ new
60  
61 # {{{ POD
62  
63 =head2 new ( )
64  
65 Takes no arguments.
66  
67 =cut
68  
69 # }}}
70  
71 sub new {
72 my ($class) = shift;
73  
74 my %h; # = validate (@_, {});
75  
76 my ($self) = bless ({}, $class);
77  
78 return $self;
79 }
80  
81 # }}}
82 # {{{ add_packet
83  
84 =head2 add_packet ($pkt)
85  
86 Adds any data, increments the packet counter, and keeps note of the time.
87  
88 =cut
89  
90 sub add_packet {
91 my ($self, $pkt) = @_;
92  
93 if (!exists $self->{data}) {
94 # No data ? Must be the first packet; trigger some init
95 $self->_init_from_first_packet($pkt);
96 }
97  
98 # Keep track of which packets crontibuted which bytes
99 push (@{$self->{which_pkts}}, [length($self->{data}), $pkt]);
100  
101 $self->{n_packets}++;
102 $self->{data} .= $pkt->[PKT_SLOT_DATA];
103  
104 # Now update the 'last packet' time counters
105 if (pkt_time($pkt) > $self->{time}) {
106 $self->{time} = pkt_time($pkt);
107 }
108  
109 #print "Adding packet $pkt to $self\n";
110  
111 return 1;
112 }
113  
114 # }}}
115 # {{{ add_mono
116  
117 =head2 add_mono ($other_mono)
118  
119 Walks through the packets in C<$other_mono>, and adds each one to C<$self>.
120  
121 =cut
122  
123 sub add_mono {
124 my ($self) = shift;
125 my ($other_mono) = @_;
126  
127 foreach my $pkt (@{ $other_mono->_data_packets }) {
128 $self->add_packet ($pkt);
129 }
130 }
131  
132 # }}}
133 # {{{ data
134  
135 =head2 data ()
136  
137 The actual data of the monologue; the bytes sent.
138  
139 =cut
140  
141 sub data {
142 my ($self) = @_;
143 return $self->{data};
144 }
145  
146 # }}}
147  
148 # {{{ t_start
149  
150 =head2 t_start ()
151  
152 Returns an object representing the time the monologue started. Can be treated
153 like a float, giving fractional epoch seconds. Only accurate to the
154 microsecond.
155  
156 =cut
157  
158 sub t_start {
159 my ($self) = @_;
160 return $self->{t_start};
161 }
162  
163 # }}}
164 # {{{ t_end
165  
166 =head2 t_end ()
167  
168 Same as C<t_start()>, but giving the time the monologue ended (or the last
169 packet so far, if you call it before the monologue has ended.)
170  
171 =cut
172  
173 sub t_end {
174 my ($self) = @_;
175 return $self->{time};
176 }
177  
178 # }}}
179 # {{{ t_elapsed
180  
181 =head2 t_elapsed ()
182  
183 Returns an object representing C<t_end - t_start> for this monologue. Can be
184 treated like a float, giving duration in fractional seconds.
185  
186 =cut
187  
188 sub t_elapsed {
189 my ($self) = @_;
190 return ($self->{time} - $self->{t_start});
191 }
192  
193 # }}}
194 # {{{ n_packets
195  
196 =head2 n_packets ()
197  
198 How many data packets were in the monologue.
199  
200 =cut
201  
202 sub n_packets {
203 my ($self) = @_;
204 return ($self->{n_packets});
205 }
206  
207 # }}}
208 # {{{ length
209  
210 =head2 length ()
211  
212 How long the monologue data was, in bytes. Excludes all the various packet
213 headers.
214  
215 =cut
216  
217 sub length {
218 my ($self) = @_;
219 return length($self->{data});
220 }
221  
222 # }}}
223 # {{{ first_packet
224  
225 =head2 first_packet ()
226  
227 Returns the first L<Net::Analysis::Packet> in the monologue. You can use it to
228 extract any TCP or IP information about the monologue.
229  
230 =cut
231  
232 sub first_packet {
233 my ($self) = @_;
234 return $self->{first_packet};
235 }
236  
237 # }}}
238 # {{{ which_pkt
239  
240 # Deprecated; use which_pkts instead
241  
242 sub which_pkt {
243 my ($self, $n) = @_;
244 return $self->which_pkts($n)->[0];
245 }
246  
247 # }}}
248 # {{{ which_pkts
249  
250 =head2 which_pkts ($byte_offset_start [, $byte_offset_end])
251  
252 Given a byte offset from within the monologue, and optionally an
253 offset later in the monologue, return the packets which contributed
254 bytes between the offsets (or an empty arrayref if none found). Bytes
255 are counted from zero (i.e. offset '0' would be the first byte).
256  
257 This can be useful to retrieve timestamps of areas inside long-lived
258 monologues.
259  
260 =cut
261  
262 sub which_pkts {
263 my ($self, $in_s, $in_e) = @_;
264  
265 $in_e ||= $in_s;
266 die "bad args to which_pkts; end < start" if ($in_e < $in_s);
267  
268 return [] if ($in_s < 0 || $in_s >= CORE::length($self->{data}));
269  
270 my @ret;
271 for my $row (@{ $self->{which_pkts} }) {
272 my ($pkt_s, $pkt) = @$row;
273 my ($pkt_e) = $pkt_s + CORE::length($pkt->[PKT_SLOT_DATA]) - 1;
274  
275 # If our desired range can't overlap, go to next
276 next if ($in_e < $pkt_s || $in_s > $pkt_e);
277 push (@ret, $pkt);
278 }
279  
280 return \@ret;
281 }
282  
283 # }}}
284 # {{{ socketpair_key
285  
286 =head2 socketpair_key ()
287  
288 Returns the socketpair key of the TCP session containing the
289 monologue.
290  
291 =cut
292  
293 sub socketpair_key {
294 my ($self) = @_;
295 $self->{socketpair_key};
296 }
297  
298 # }}}
299  
300 # {{{ as_string
301  
302 sub as_string {
303 my ($self) = @_;
304 my $str = '';
305  
306 if (exists $self->{data}) {
307 my $dur = $self->t_elapsed();
308 $str .= sprintf ("[Mono from %21.21s]%10.06fs, %3dpkts, %6db",
309 $self->{from}, $dur, $self->n_packets(),
310 $self->length());
311 } else {
312 $str .= "[Mono undefined]";
313 }
314  
315 return $str;
316 }
317  
318 # }}}
319  
320 # {{{ _data_packets
321  
322 =head2 _data_packets ()
323  
324 Returns an array-ref, containing all the packets which contributed data to
325 the monologue.
326  
327 NOTE: this method may go away sometime.
328  
329 =cut
330  
331 sub _data_packets {
332 my ($self, $n) = @_;
333  
334 my @ret = map {$_->[1]} @{ $self->{which_pkts} };
335  
336 return \@ret;
337 }
338  
339 # }}}
340  
341 #### Private helper methods
342 #
343 # {{{ _init_from_first_packet
344  
345 sub _init_from_first_packet {
346 my ($self, $pkt) = @_;
347  
348 $self->{n_packets} = 0;
349 $self->{data} = '';
350  
351 $self->{socketpair_key} = $pkt->[PKT_SLOT_SOCKETPAIR_KEY];
352 $self->{to} = $pkt->[PKT_SLOT_TO];
353 $self->{from} = $pkt->[PKT_SLOT_FROM];
354 $self->{time} = pkt_time($pkt) + 0; # Make a cloned copy
355  
356 # Keep copies of the first noted time, and the packet itself
357 $self->{t_start} = $self->{time};
358 $self->{first_packet} = $pkt;
359  
360 # Keep track of which packets contributed which bytes
361 $self->{which_pkts} = [];
362 }
363  
364 # }}}
365  
366 1;
367 __END__