| Net-Packet documentation | Contained in the Net-Packet distribution. |
Net::Packet::CDP - Cisco Discovery Protocol layer 4 object
use Net::Packet::Consts qw(:cdp);
require Net::Packet::CDP;
# Build a layer
my $layer = Net::Packet::CDP->new(
version => 2,
ttl => 180,
checksum => 0,
);
$layer->pack;
print 'RAW: '.unpack('H*', $layer->raw)."\n";
# Read a raw layer
my $layer = Net::Packet::CDP->new(raw => $raw);
print $layer->print."\n";
print 'PAYLOAD: '.unpack('H*', $layer->payload)."\n"
if $layer->payload;
This modules implements the encoding and decoding of the Cisco Discovery Protocol layer.
See also Net::Packet::Layer and Net::Packet::Layer4 for other attributes and methods.
All these type attributes keep a pointer to the respective object. That is, for typeDeviceId, you have a pointer to a Net::Packet::CDP::TypeDeviceId object, if applicable.
Object constructor. You can pass attributes that will overwrite default ones. Default values:
version: 2
ttl: 180
checksum: 0
Packs all attributes into a raw format, in order to inject to network. Returns 1 on success, undef otherwise.
Unpacks raw data from network and stores attributes into the object. Returns 1 on success, undef otherwise.
Load them: use Net::Packet::Consts qw(:cdp);
CDP header length.
Various supported CDP types.
Patrice <GomoR> Auffret
Copyright (c) 2004-2009, Patrice <GomoR> Auffret
You may distribute this module under the terms of the Artistic license. See LICENSE.Artistic file in the source distribution archive.
NetPacket, Net::RawIP, Net::RawSock
| Net-Packet documentation | Contained in the Net-Packet distribution. |
# # $Id: CDP.pm 1640 2009-11-09 17:58:27Z gomor $ # package Net::Packet::CDP; use strict; use warnings; require Net::Packet::Layer4; our @ISA = qw(Net::Packet::Layer4); use Net::Packet::Consts qw(:cdp :layer); require Net::Packet::CDP::TypeDeviceId; require Net::Packet::CDP::TypeAddresses; require Net::Packet::CDP::TypePortId; require Net::Packet::CDP::TypeCapabilities; require Net::Packet::CDP::TypeSoftwareVersion; our @AS = qw( version ttl checksum typeDeviceId typeAddresses typePortId typeCapabilities typeSoftwareVersion ); __PACKAGE__->cgBuildIndices; __PACKAGE__->cgBuildAccessorsScalar(\@AS); no strict 'vars'; sub new { shift->SUPER::new( version => 2, ttl => 180, checksum => 0, @_, ); } sub getLength { NP_CDP_HDR_LEN } sub pack { my $self = shift; $self->[$__raw] = $self->SUPER::pack('CCn', $self->[$__version], $self->[$__ttl], $self->[$__checksum], ) or return undef; if ($self->[$__typeDeviceId]) { $self->[$__raw] .= $self->[$__typeDeviceId]->pack or return undef; } if ($self->[$__typeAddresses]) { $self->[$__raw] .= $self->[$__typeAddresses]->pack or return undef; } if ($self->[$__typePortId]) { $self->[$__raw] .= $self->[$__typePortId]->pack or return undef; } if ($self->[$__typeCapabilities]) { $self->[$__raw] .= $self->[$__typeCapabilities]->pack or return undef; } if ($self->[$__typeSoftwareVersion]) { $self->[$__raw] .= $self->[$__typeSoftwareVersion]->pack or return undef; } 1; } sub unpack { my $self = shift; my ($version, $ttl, $checksum, $payload) = $self->SUPER::unpack('CCn a*', $self->[$__raw]) or return undef; $self->[$__version] = $version; $self->[$__ttl] = $ttl; $self->[$__checksum] = $checksum; my $tail = CORE::unpack('H*', $payload); if ($tail =~ /^0001/) { my $type = Net::Packet::CDP::TypeDeviceId->new(raw => $payload); $self->[$__typeDeviceId] = $type; $payload = $type->payload; $tail = CORE::unpack('H*', $payload); } if ($tail =~ /^0002/) { my $type = Net::Packet::CDP::TypeAddresses->new(raw => $payload); $self->[$__typeAddresses] = $type; $payload = $type->payload; $tail = CORE::unpack('H*', $payload); } if ($tail =~ /^0003/) { my $type = Net::Packet::CDP::TypePortId->new(raw => $payload); $self->[$__typePortId] = $type; $payload = $type->payload; $tail = CORE::unpack('H*', $payload); } if ($tail =~ /^0004/) { my $type = Net::Packet::CDP::TypeCapabilities->new(raw => $payload); $self->[$__typeCapabilities] = $type; $payload = $type->payload; $tail = CORE::unpack('H*', $payload); } if ($tail =~ /^0005/) { my $type = Net::Packet::CDP::TypeSoftwareVersion->new(raw => $payload); $self->[$__typeSoftwareVersion] = $type; $payload = $type->payload; $tail = CORE::unpack('H*', $payload); } $self->[$__payload] = $payload; 1; } sub encapsulate { my $types = { NP_LAYER_NONE() => NP_LAYER_NONE(), }; $types->{NP_LAYER_NONE()} || NP_LAYER_UNKNOWN(); } sub print { my $self = shift; my $buf = ''; my $l = $self->layer; my $i = $self->is; $buf .= sprintf "$l:+$i: version:%d ttl:%d checksum:0x%04x", $self->[$__version], $self->[$__ttl], $self->[$__checksum]; if ($self->[$__typeDeviceId]) { $buf .= "\n".$self->[$__typeDeviceId]->print; } if ($self->[$__typeAddresses]) { $buf .= "\n".$self->[$__typeAddresses]->print; } if ($self->[$__typePortId]) { $buf .= "\n".$self->[$__typePortId]->print; } if ($self->[$__typeCapabilities]) { $buf .= "\n".$self->[$__typeCapabilities]->print; } if ($self->[$__typeSoftwareVersion]) { $buf .= "\n".$self->[$__typeSoftwareVersion]->print; } $buf; } 1; __END__