1package NetPacket::IGMP; 2BEGIN { 3 $NetPacket::IGMP::AUTHORITY = 'cpan:YANICK'; 4} 5# ABSTRACT: Assemble and disassemble IGMP (Internet Group Mangement Protocol) packets. 6$NetPacket::IGMP::VERSION = '1.6.0'; 7use strict; 8use warnings; 9 10use parent 'NetPacket'; 11 12our @EXPORT_OK = qw(igmp_strip 13 IGMP_VERSION_RFC998 IGMP_VERSION_RFC1112 14 IGMP_VERSION_RFC2236 IGMP_VERSION_RFC3376 15 IGMP_MSG_HOST_MQUERY IGMP_MSG_HOST_MREPORT 16 IGMP_MSG_HOST_MQUERYv2 IGMP_MSG_HOST_MREPORTv1 17 IGMP_MSG_HOST_MREPORTv2 IGMP_MSG_HOST_LEAVE 18 IGMP_MSG_HOST_MREPORTv3 19 IGMP_IP_NO_HOSTS IGMP_IP_ALL_HOSTS 20 IGMP_IP_ALL_ROUTERS 21); 22 23our %EXPORT_TAGS = ( 24 ALL => [@EXPORT_OK], 25 strip => [qw(igmp_strip)], 26 versions => [qw(IGMP_VERSION_RFC998 IGMP_VERSION_RFC1112 27 IGMP_VERSION_RFC2236 IGMP_VERSION_RFC3376)], 28 msgtypes => [qw(IGMP_MSG_HOST_MQUERY IGMP_MSG_HOST_MREPORT 29 IGMP_MSG_HOST_MQUERYv2 IGMP_MSG_HOST_MREPORTv1 30 IGMP_MSG_HOST_MREPORTv2 IGMP_MSG_HOST_LEAVE 31 IGMP_MSG_HOST_MREPORTv3)], 32 group_addrs => [qw(IGMP_IP_NO_HOSTS IGMP_IP_ALL_HOSTS 33 IGMP_IP_ALL_ROUTERS)] 34); 35 36# 37# Version numbers 38# 39 40use constant IGMP_VERSION_RFC998 => 0; # Version 0 of IGMP (obsolete) 41use constant IGMP_VERSION_RFC1112 => 1; # Version 1 of IGMP 42use constant IGMP_VERSION_RFC2236 => 2; # Version 2 of IGMP 43use constant IGMP_VERSION_RFC3376 => 3; # Version 3 of IGMP 44 45# 46# Message types 47# 48 49use constant IGMP_MSG_HOST_MQUERY => 1; # Host membership query 50use constant IGMP_MSG_HOST_MREPORT => 2; # Host membership report 51 52use constant IGMP_MSG_HOST_MQUERYv2 => 0x11; # Host membership query 53use constant IGMP_MSG_HOST_MREPORTv1 => 0x12; # Host membership report 54use constant IGMP_MSG_HOST_MREPORTv2 => 0x16; # Host membership report 55use constant IGMP_MSG_HOST_LEAVE => 0x17; # Leave group 56 57use constant IGMP_MSG_HOST_MREPORTv3 => 0x22; # Host membership report 58 59# 60# IGMP IP addresses 61# 62 63use constant IGMP_IP_NO_HOSTS => '224.0.0.0'; # Not assigned to anyone 64use constant IGMP_IP_ALL_HOSTS => '224.0.0.1'; # All hosts on local net 65use constant IGMP_IP_ALL_ROUTERS => '224.0.0.2'; # All routers on local net 66 67# Convert 32-bit IP address to "dotted quad" notation 68 69sub to_dotquad { 70 my($net) = @_ ; 71 my($na, $nb, $nc, $nd); 72 73 $na = $net >> 24 & 255; 74 $nb = $net >> 16 & 255; 75 $nc = $net >> 8 & 255; 76 $nd = $net & 255; 77 78 return ("$na.$nb.$nc.$nd"); 79} 80 81# 82# Decode the packet 83# 84 85sub decode { 86 my $class = shift; 87 my($pkt, $parent) = @_; 88 my $self = {}; 89 90 # Class fields 91 92 $self->{_parent} = $parent; 93 $self->{_frame} = $pkt; 94 95 # Decode IGMP packet 96 97 if (defined($pkt)) { 98 my $tmp; 99 100 ($tmp, $self->{subtype}, $self->{cksum}, $self->{group_addr}, 101 $self->{data}) = unpack('CCnNa*', $pkt); 102 103 # Extract bit fields 104 105 $self->{version} = ($tmp & 0xf0) >> 4; 106 $self->{type} = $tmp & 0x0f; 107 108 # Convert to dq notation 109 110 $self->{group_addr} = to_dotquad($self->{group_addr}); 111 } 112 113 # Return a blessed object 114 115 bless($self, $class); 116 return $self; 117} 118 119# 120# Strip header from packet and return the data contained in it. IGMP 121# packets contain no encapsulated data. 122# 123 124undef &igmp_strip; 125*igmp_strip = \&strip; 126 127sub strip { 128 return undef; 129} 130 131# 132# Encode a packet 133# 134 135sub encode { 136 die("Not implemented"); 137} 138 139# Module return value 140 1411; 142 143# autoloaded methods go after the END token (&& pod) below 144 145=pod 146 147=head1 NAME 148 149NetPacket::IGMP - Assemble and disassemble IGMP (Internet Group Mangement Protocol) packets. 150 151=head1 VERSION 152 153version 1.6.0 154 155=head1 SYNOPSIS 156 157 use NetPacket::IGMP; 158 159 $igmp_obj = NetPacket::IGMP->decode($raw_pkt); 160 $igmp_pkt = NetPacket::IGMP->encode(params...); # Not implemented 161 $igmp_data = NetPacket::IGMP::strip($raw_pkt); 162 163=head1 DESCRIPTION 164 165C<NetPacket::IGMP> provides a set of routines for assembling and 166disassembling packets using IGMP (Internet Group Mangement Protocol). 167 168=head2 Methods 169 170=over 171 172=item C<NetPacket::IGMP-E<gt>decode([RAW PACKET])> 173 174Decode the raw packet data given and return an object containing 175instance data. This method will quite happily decode garbage input. 176It is the responsibility of the programmer to ensure valid packet data 177is passed to this method. 178 179=item C<NetPacket::IGMP-E<gt>encode(param =E<gt> value)> 180 181Return an IGMP packet encoded with the instance data specified. Not 182implemented. 183 184=back 185 186=head2 Functions 187 188=over 189 190=item C<NetPacket::IGMP::strip([RAW PACKET])> 191 192Return the encapsulated data (or payload) contained in the IGMP 193packet. This function returns undef as there is no encapsulated data 194in an IGMP packet. 195 196=back 197 198=head2 Instance data 199 200The instance data for the C<NetPacket::IGMP> object consists of 201the following fields. 202 203=over 204 205=item version 206 207The IGMP version of this packet. 208 209=item type 210 211The message type for this packet. 212 213=item len 214 215The length (including length of header) in bytes for this packet. 216 217=item subtype 218 219The message subtype for this packet. 220 221=item cksum 222 223The checksum for this packet. 224 225=item group_addr 226 227The group address specified in this packet. 228 229=item data 230 231The encapsulated data (payload) for this packet. 232 233=back 234 235=head2 Exports 236 237=over 238 239=item default 240 241none 242 243=item exportable 244 245IGMP_VERSION_RFC998 IGMP_VERSION_RFC1112 IGMP_HOST_MQUERY 246IGMP_HOST_MREPORT IGMP_IP_NO_HOSTS IGMP_IP_ALL_HOSTS 247IGMP_IP_ALL_ROUTERS 248 249=item tags 250 251The following tags group together related exportable items. 252 253=over 254 255=item C<:strip> 256 257Import the strip function C<igmp_strip>. 258 259=item C<:versions> 260 261IGMP_VERSION_RFC998 IGMP_VERSION_RFC1112 262 263=item C<:msgtypes> 264 265IGMP_HOST_MQUERY IGMP_HOST_MREPORT 266 267=item C<:group_addrs> 268 269IGMP_IP_NO_HOSTS IGMP_IP_ALL_HOSTS IGMP_IP_ALL_ROUTERS 270 271=item C<:ALL> 272 273All the above exportable items. 274 275=back 276 277=back 278 279=head1 EXAMPLE 280 281The following script dumps UDP frames by IP address and UDP port 282to standard output. 283 284 #!/usr/bin/perl -w 285 286 use strict; 287 use Net::PcapUtils; 288 use NetPacket::Ethernet qw(:strip); 289 use NetPacket::IP; 290 use NetPacket::IGMP; 291 292 sub process_pkt { 293 my($arg, $hdr, $pkt) = @_; 294 295 my $ip_obj = NetPacket::IP->decode(eth_strip($pkt)); 296 my $igmp_obj = NetPacket::IGMP->decode($ip_obj->{data}); 297 298 print("$ip_obj->{src_ip} -> $ip_obj->{dest_ip} ", 299 "$igmp_obj->{type}/$igmp_obj->{subtype} ", 300 "$igmp_obj->{group_addr}\n"); 301 } 302 303 Net::PcapUtils::loop(\&process_pkt, FILTER => 'igmp'); 304 305=head1 TODO 306 307=over 308 309=item Implement encode() function 310 311=back 312 313=head1 COPYRIGHT 314 315Copyright (c) 2001 Tim Potter. 316 317Copyright (c) 1995,1996,1997,1998,1999 ANU and CSIRO on behalf of 318the participants in the CRC for Advanced Computational Systems 319('ACSys'). 320 321This module is free software. You can redistribute it and/or 322modify it under the terms of the Artistic License 2.0. 323 324This program is distributed in the hope that it will be useful, 325but without any warranty; without even the implied warranty of 326merchantability or fitness for a particular purpose. 327 328=head1 AUTHOR 329 330Tim Potter E<lt>tpot@samba.orgE<gt> 331 332=cut 333 334__END__ 335 336 337# any real autoloaded methods go after this line 338