1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
|
#!/usr/bin/perl
#
# $Id: Packet.pm,v 1.3 2001/10/06 22:19:14 levine Exp $
#
# Copyright (C) 2001 James D. Levine (jdl@vinecorp.com)
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
####################################################################
####################################################################
#
# packet
#
# Basic Packet abstraction with utility methods
#
####################################################################
use strict;
package NWatch::packet;
sub protocol_name { ""; }
sub full_protocol_name
{
my( $self ) = shift;
# print "fpn: next is " . $self->next() . "\n";
# print "fpn: defined next is " . (defined ($self->next())) . "\n";
return $self->protocol_name() .
(
( defined $self->next() ) ? ( ":" .$self->next()->full_protocol_name() ) : ""
);
}
sub new
{
my( $class, $data ) = @_;
my $self =
{
data => $data, # the entire packet
fields => {}, # unpacked fields, referenced by name
next => undef, # proto stacked next on top
};
$self->{"creation-time"} = 0+time;
bless $self, $class;
# print "Packet: setting creation-time to ". $self->{"creation-time"} . "\n";
$self;
}
# return age in seconds
sub age
{
time- $_[0]->{ 'creation-time' };
}
sub set
{
my( $self, $field, $value ) = @_;
$self->{fields}->{ $field } = $value;
}
sub get
{
my( $self, $field ) = @_;
return $self->{fields}->{ $field };
}
sub set_or_get
{
my $field = shift; my $self = shift; my $val = shift;
$self->{$field} = $val if defined $val;
return $self->{$field};
}
sub next { set_or_get('next', @_); }
sub data { set_or_get('data', @_); }
sub top_field
{
my( $self, $fieldname ) = @_;
my $p = $self;
while( defined $p->next )
{
$p = $p->next;
}
return $p->get( $fieldname ) if defined $p;
return undef;
}
sub field_path
{
my( $self, $path ) = @_;
my @elements = split ":", $path;
return $self->retrieve_path( \@elements );
}
sub retrieve_path
{
my( $self, $elems_ref ) = @_;
# print "retrieve_path: elem count is: " . $#$elems_ref . "\n";
# print "self is $self \n";
return undef if $#$elems_ref == -1;
if( $#$elems_ref == 0 )
{
my $e = shift @$elems_ref;
# print "end of path -- last elem is $e \n";
my $val = $self->get( $e );
# print "value returned will be $val \n";
return $val;
}
# print "retrieve_path: first elem is: " . $elems_ref->[0] . "\n";
# print "retrieve_path: next is " . $self->next() . "\n";
my $next_elem = shift @$elems_ref;
if( ( defined $self->next() ) && ( $self->next()->protocol_name() eq $next_elem ) )
{
return $self->next()->retrieve_path( $elems_ref );
}
else
{
return undef;
}
}
sub fieldpath_relative
{
my( $self, $path ) = @_;
my @elements = split ":", $path;
return undef if $#elements == -1; # nothing in path
# last element in path is the field of the top protocol
my $field = pop @elements;
# the remaining (preceeding) elements in the path are a path from
# this protocol up the stack to the top protocol
# for example, "ipv4:tcp:source_address"
# references the "source_address" field in the tcp proto
# follow the path to retrieve the protocol, or undef if not in stack
# if it's an empty path, proto_path returns $self
my $proto = $self->proto_path( \@elements );
return $proto->get( $field ) if( defined $proto );
return undef;
}
# returns 1 if $self is an instance of the supplied protocol path
sub proto_isa
{
my( $self, $proto_path_str ) = @_;
# print "proto_isa looking for $proto_path_str \n";
# print "self is $self\n";
# print "self protoname is " . $self->protocol_name() . "\n";
my @elements = split ":", $proto_path_str;
return 0 if $#elements == -1; # nothing in path
# first element must match self's protocol name
my $first_elem = shift @elements;
return 0 if $first_elem ne $self->protocol_name();
my $pak = $self->proto_path( \@elements );
# print "pak is $pak\n";
# print "pak protoname is " . $pak->protocol_name() . "\n" if defined $pak;
return( defined $pak );
}
sub proto_path
{
my( $self, $elems_ref ) = @_;
# print "proto_path: looking for " . ( join ":", @$elems_ref ) . "\n";
return $self if $#$elems_ref == -1; # empty path, I'm it
my $next_elem = shift @$elems_ref;
# print "proto_path: next_elem is " . $next_elem . " and self protocol_name() is " . $self->protocol_name() . "\n";
# print "proto_path: self->next is " . $self->next() . "\n";
# print "proto_path: self->next is " . $self->next() . "\n";
if( ( defined $self->next() ) && ( $self->next()->protocol_name() eq $next_elem ) )
{
return $self->next()->proto_path( $elems_ref );
}
else
{
return undef;
}
}
1;
|