Net::Write::Layer2 - object for a link layer (layer 2) descriptor


Net-Write documentation Contained in the Net-Write distribution.

Index


Code Index:

NAME

Top

Net::Write::Layer2 - object for a link layer (layer 2) descriptor

SYNOPSIS

Top

   use Net::Write::Layer2;

   my $desc = Net::Write::Layer2->new(
      dev => 'eth0',
   );

   $desc->open;
   $desc->send('G'x666);
   $desc->close;

DESCRIPTION

Top

This is the class for creating a layer 2 descriptor.

ATTRIBUTES

Top

dev

The string specifying network interface to use.

Under Unix-like systems, this is in this format: \w+\d+ (example: eth0).

Under Windows systems, this is more complex; example: \Device\NPF_{0749A9BC-C665-4C55-A4A7-34AC2FBAB70F}

METHODS

Top

new

Object constructor. You MUST pass a valid dev attribute. There is no default value.

open

Open the interface.

send (scalar)

Send raw data to the network.

close

Close the descriptor.

CAVEATS

Top

Writing junk to loopback interface on BSD systems will not work.

SEE ALSO

Top

Net::Write::Layer

AUTHOR

Top

Patrice <GomoR> Auffret

COPYRIGHT AND LICENSE

Top


Net-Write documentation Contained in the Net-Write distribution.

#
# $Id: Layer2.pm 1636 2009-06-10 18:38:24Z gomor $
#
package Net::Write::Layer2;
use strict;
use warnings;

require Net::Write::Layer;
our @ISA = qw(Net::Write::Layer);
__PACKAGE__->cgBuildIndices;

no strict 'vars';

use Carp;
use Net::Pcap;

sub new {
   my $self = shift->SUPER::new(@_);

   croak("@{[(caller(0))[3]]}: you must pass `dev' parameter\n")
      unless $self->[$__dev];

   $self;
}

sub open {
   my $self = shift;

   my $err;
   my $pd = Net::Pcap::open_live(
      $self->[$__dev],
      0,
      0,
      1000,
      \$err,
   );
   unless ($pd) {
      croak("@{[(caller(0))[3]]}: Net::Pcap::open_live: @{[$self->dev]}: ".
            "$err\n");
   }

   $self->[$___io] = $pd;

   1;
}

sub send {
   my $self = shift;
   my ($raw) = @_;

   while (1) {
      if (Net::Pcap::sendpacket($self->[$___io], $raw) < 0) {
         if ($!{ENOBUFS}) {
            $self->cgDebugPrint(2, "ENOBUFS, sleeping for 1 second");
            sleep 1;
            next;
         }
         elsif ($!{EHOSTDOWN}) {
            $self->cgDebugPrint(2, "host is down");
            last;
         }
         carp("@{[(caller(0))[3]]}: ".Net::Pcap::geterr($self->[$___io])."\n");
         return undef;
      }
      last;
   }

   1;
}

sub close {
   my $self = shift;
   if ($self->[$___io]) {
      Net::Pcap::close($self->[$___io]);
      $self->[$___io] = undef;
   }
}

1;

__END__