| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||||||||||||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||||||||||||||||||||
| 3 | # | ||||||||||||||||||||||||||||
| 4 | # (C) Paul Evans, 2009-2025 -- leonerd@leonerd.org.uk | ||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | package IO::Socket::Packet 0.12; | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use v5.14; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use base qw( IO::Socket ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use POSIX qw( EAGAIN ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 2 | ▶ | use Socket qw( AF_INET SOCK_STREAM SOCK_RAW ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | use Socket::Packet qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | AF_PACKET ETH_P_ALL | ||||||||||||||||||||||||||||
| 19 | pack_sockaddr_ll unpack_sockaddr_ll | ||||||||||||||||||||||||||||
| 20 | pack_packet_mreq | ||||||||||||||||||||||||||||
| 21 | unpack_tpacket_stats | ||||||||||||||||||||||||||||
| 22 | siocgstamp siocgstampns | ||||||||||||||||||||||||||||
| 23 | siocgifindex siocgifname | ||||||||||||||||||||||||||||
| 24 | recv_len | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | SOL_PACKET | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | PACKET_ADD_MEMBERSHIP | ||||||||||||||||||||||||||||
| 29 | PACKET_DROP_MEMBERSHIP | ||||||||||||||||||||||||||||
| 30 | PACKET_STATISTICS | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | PACKET_MR_MULTICAST | ||||||||||||||||||||||||||||
| 33 | PACKET_MR_PROMISC | ||||||||||||||||||||||||||||
| 34 | PACKET_MR_ALLMULTI | ||||||||||||||||||||||||||||
| 35 | 2 | ); | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 2 | __PACKAGE__->register_domain( AF_PACKET ); | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 NAME | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | C<IO::Socket::Packet> - Object interface to C<AF_PACKET> domain sockets | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | use IO::Socket::Packet; | ||||||||||||||||||||||||||||
| 46 | use Socket::Packet qw( unpack_sockaddr_ll ); | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | my $sock = IO::Socket::Packet->new( IfIndex => 0 ); | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | while( my ( $protocol, $ifindex, $hatype, $pkttype, $addr ) = | ||||||||||||||||||||||||||||
| 51 | $sock->recv_unpack( my $packet, 8192, 0 ) ) { | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ... | ||||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | This class provides an object interface to C<PF_PACKET> sockets on Linux. It | ||||||||||||||||||||||||||||
| 59 | is built upon L<IO::Socket> and inherits all the methods defined by this base | ||||||||||||||||||||||||||||
| 60 | class. | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =cut | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | =head1 CONSTRUCTOR | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | =cut | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | =head2 new | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | $sock = IO::Socket::Packet->new( %args ); | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | Creates a new C<IO::Socket::Packet> object. If any arguments are passed it | ||||||||||||||||||||||||||||
| 73 | will be configured to contain a newly created socket handle, and be C<bind>ed | ||||||||||||||||||||||||||||
| 74 | as required by the arguments. The recognised arguments are: | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =over 8 | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =item Type => INT | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | The socktype to use; should be either C<SOCK_RAW> or C<SOCK_DGRAM>. It not | ||||||||||||||||||||||||||||
| 81 | supplied a default of C<SOCK_RAW> will be used. | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =item Protocol => INT | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Ethernet protocol number to bind to. To capture all protocols, use the | ||||||||||||||||||||||||||||
| 86 | C<ETH_P_ALL> constant (or omit this key, which implies that as a default). | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =item IfIndex => INT | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | If supplied, binds the socket to the specified interface index. To bind to all | ||||||||||||||||||||||||||||
| 91 | interfaces, use 0 (or omit this key, which implies that as a default). | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =item IfName => STRING | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | If supplied, binds the socket to the interface with the specified name. | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | =back | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | sub configure | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
configure: not called CC=11 SCAR=48.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 0 | my ( $arg ) = @_; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | my $type = $arg->{Type} || SOCK_RAW; | ||||||||||||||||||||||||||
|
Condition0%0/2
$arg->{'Type'} || 3
MC/DC0%0/1
$arg->{'Type'} || 3
$arg->{'Type'}
Truth table0%0/2
A
A= $arg->{'Type'}
| |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | $self->socket( AF_PACKET, $type, 0 ) or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # bind() arguments | ||||||||||||||||||||||||||||
| 111 | 0 | my ( $protocol, $ifindex ); | |||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 0 | ▶ | $protocol = $arg->{Protocol} if exists $arg->{Protocol}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | $ifindex = $arg->{IfIndex} if exists $arg->{IfIndex}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 0 | ▶ | if( !defined $ifindex and exists $arg->{IfName} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $ifindex and exists $arg->{"IfName"}
MC/DC0%0/2
not defined $ifindex and exists $arg->{"IfName"}
not defined $ifindex
exists $arg->{"IfName"}
Truth table0%0/3
not(A) and B
A= not defined $ifindexB= exists $arg->{"IfName"}
| |||||||||||||||||||||||||||||
| 117 | 0 | $ifindex = siocgifindex( $self, $arg->{IfName} ); | |||||||||||||||||||||||||||
| 118 | 0 | ▶ | defined $ifindex or return undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | $self->bind( pack_sockaddr_ll( | ||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/2
$ifindex || 0
MC/DC0%0/1
$ifindex || 0
$ifindex
Truth table0%0/2
A
A= $ifindex
| |||||||||||||||||||||||||||||
| 122 | defined $protocol ? $protocol : ETH_P_ALL, | ||||||||||||||||||||||||||||
| 123 | $ifindex || 0, | ||||||||||||||||||||||||||||
| 124 | 0, 0, '' ) ) or return undef; | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | return $self; | |||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head1 METHODS | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =cut | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =head2 recv_len | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | ( $addr, $len ) = $sock->recv_len( $buffer, $maxlen, $flags ); | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | Similar to Perl's C<recv> builtin, except it returns the packet length as an | ||||||||||||||||||||||||||||
| 138 | explict return value. This may be useful if C<$flags> contains the | ||||||||||||||||||||||||||||
| 139 | C<MSG_TRUNC> flag, obtaining the true length of the packet on the wire, even | ||||||||||||||||||||||||||||
| 140 | if this is longer than the data written in the buffer. | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | =cut | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # don't actually need to implement it; the imported symbol works fine | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | =head2 recv_unpack | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | ( $protocol, $ifindex, $hatype, $pkttype, $addr, $len ) = | ||||||||||||||||||||||||||||
| 149 | $sock->recv_unpack( $buffer, $size, $flags ); | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | This method is a combination of C<recv_len> and C<unpack_sockaddr_ll>. If it | ||||||||||||||||||||||||||||
| 152 | successfully receives a packet, it unpacks the address and returns the fields | ||||||||||||||||||||||||||||
| 153 | from it, and the length of the received packet. If it fails, it returns an | ||||||||||||||||||||||||||||
| 154 | empty list. | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | If the ring-buffer has been set using C<setup_rx_ring>, it will automatically | ||||||||||||||||||||||||||||
| 157 | be used by this method. | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | =cut | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | sub recv_unpack | ||||||||||||||||||||||||||||
| 162 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
recv_unpack: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | if( defined ${*$self}{packet_rx_ring} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | 0 | ▶ | defined $self->wait_ring_frame( my $buffer, \my %info ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # Copy to caller | ||||||||||||||||||||||||||||
| 169 | 0 | $_[0] = $buffer; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 0 | $self->done_ring_frame; | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 0 | ${*$self}{packet_ts_sec} = $info{tp_sec}; | |||||||||||||||||||||||||||
| 174 | 0 | ${*$self}{packet_ts_nsec} = $info{tp_nsec}; | |||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | return ( $info{sll_protocol}, | ||||||||||||||||||||||||||||
| 177 | $info{sll_ifindex}, | ||||||||||||||||||||||||||||
| 178 | $info{sll_hatype}, | ||||||||||||||||||||||||||||
| 179 | $info{sll_pkttype}, | ||||||||||||||||||||||||||||
| 180 | $info{sll_addr}, | ||||||||||||||||||||||||||||
| 181 | 0 | $info{tp_len} ); | |||||||||||||||||||||||||||
| 182 | } | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 0 | ▶ | my ( $addr, $len ) = $self->recv_len( @_ ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 185 | 0 | return unpack_sockaddr_ll( $addr ), $len; | |||||||||||||||||||||||||||
| 186 | } | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head2 protocol | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | $protocol = $sock->protocol; | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | Returns the ethertype protocol the socket is bound to. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =cut | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub protocol | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
protocol: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 199 | 0 | return (unpack_sockaddr_ll($self->sockname))[0]; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =head2 ifindex | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | $ifindex = $sock->ifindex; | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | Returns the interface index the socket is bound to. | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =cut | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | sub ifindex | ||||||||||||||||||||||||||||
| 211 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ifindex: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 212 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 213 | 0 | return (unpack_sockaddr_ll($self->sockname))[1]; | |||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head2 ifname | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | $ifname = $sock->ifname; | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Returns the name of the interface the socket is bound to. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =cut | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | sub ifname | ||||||||||||||||||||||||||||
| 225 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ifname: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 226 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 227 | 0 | return siocgifname( $self, $self->ifindex ); | |||||||||||||||||||||||||||
| 228 | } | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =head2 hatype | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | $hatype = $sock->hatype; | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | Returns the hardware address type for the interface the socket is bound to. | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | sub hatype | ||||||||||||||||||||||||||||
| 239 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
hatype: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 240 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 241 | 0 | return (unpack_sockaddr_ll($self->sockname))[2]; | |||||||||||||||||||||||||||
| 242 | } | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | =head2 timestamp | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | $time = $sock->timestamp; | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | ( $sec, $usec ) = $sock->timestamp; | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Returns the timestamp of the last received packet on the socket (as obtained | ||||||||||||||||||||||||||||
| 251 | by the C<SIOCGSTAMP> C<ioctl>). In scalar context, returns a single | ||||||||||||||||||||||||||||
| 252 | floating-point value in UNIX epoch seconds. In list context, returns the | ||||||||||||||||||||||||||||
| 253 | number of seconds, and the number of microseconds. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | If the ring-buffer has been set using C<setup_rx_ring>, this method returns | ||||||||||||||||||||||||||||
| 256 | the timestamp of the last packet received from it. | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | =cut | ||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | sub timestamp | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
timestamp: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 262 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 0 | ▶ | if( defined ${*$self}{packet_ts_sec} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 265 | 0 | my $sec = delete ${*$self}{packet_ts_sec}; | |||||||||||||||||||||||||||
| 266 | 0 | my $nsec = delete ${*$self}{packet_ts_nsec}; | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | return wantarray ? ( $sec, int($nsec/1000) ) : $sec + $nsec/1_000_000_000; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | return siocgstamp( $self ); | |||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head2 timestamp_nano | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | $time = $sock->timestamp_nano; | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | ( $sec, $nsec ) = $sock->timestamp_nano; | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | Returns the nanosecond-precise timestamp of the last received packet on the | ||||||||||||||||||||||||||||
| 281 | socket (as obtained by the C<SIOCGSTAMPNS> C<ioctl>). In scalar context, | ||||||||||||||||||||||||||||
| 282 | returns a single floating-point value in UNIX epoch seconds. In list context, | ||||||||||||||||||||||||||||
| 283 | returns the number of seconds, and the number of nanoseconds. | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | If the ring-buffer has been set using C<setup_rx_ring>, this method returns | ||||||||||||||||||||||||||||
| 286 | the timestamp of the last packet received from it. | ||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||
| 288 | =cut | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | sub timestamp_nano | ||||||||||||||||||||||||||||
| 291 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
timestamp_nano: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 292 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 0 | ▶ | if( defined ${*$self}{packet_ts_sec} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 295 | 0 | my $sec = delete ${*$self}{packet_ts_sec}; | |||||||||||||||||||||||||||
| 296 | 0 | my $nsec = delete ${*$self}{packet_ts_nsec}; | |||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 0 | ▶ | return wantarray ? ( $sec, $nsec ) : $sec + $nsec/1_000_000_000; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 0 | return siocgstampns( $self ); | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =head1 INTERFACE NAME UTILITIES | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | The following methods are utilities around C<siocgifindex> and C<siocgifname>. | ||||||||||||||||||||||||||||
| 307 | If called on an object, they use the encapsulated socket. If called as class | ||||||||||||||||||||||||||||
| 308 | methods, they will create a temporary socket to pass to the kernel, then close | ||||||||||||||||||||||||||||
| 309 | it again. | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | =cut | ||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||
| 313 | =head2 ifname2index | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | $ifindex = $sock->ifname2index( $ifname ); | ||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||
| 317 | $ifindex = IO::Socket::Packet->ifname2index( $ifname ); | ||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||
| 319 | Returns the name for the given interface index, or C<undef> if it doesn't | ||||||||||||||||||||||||||||
| 320 | exist. | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | =cut | ||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||
| 324 | sub ifname2index | ||||||||||||||||||||||||||||
| 325 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ifname2index: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||
| 326 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 327 | 1 | my ( $ifname ) = @_; | |||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | 1 | my $sock; | |||||||||||||||||||||||||||
| 330 | 1 | ▶ | if( ref $self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 331 | 0 | $sock = $self; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | else { | ||||||||||||||||||||||||||||
| 334 | 1 | ▶ | socket( $sock, AF_INET, SOCK_STREAM, 0 ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 335 | croak "Cannot socket(AF_INET) - $!"; | ||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | 1 | return siocgifindex( $sock, $ifname ); | |||||||||||||||||||||||||||
| 339 | } | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head2 ifindex2name | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | $ifname = $sock->ifindex2name( $ifindex ); | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | $ifname = IO::Socket::Packet->ifindex2name( $ifindex ); | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | Returns the index for the given interface name, or C<undef> if it doesn't | ||||||||||||||||||||||||||||
| 348 | exist. | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | =cut | ||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||
| 352 | sub ifindex2name | ||||||||||||||||||||||||||||
| 353 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
ifindex2name: called CC=3 SCAR=11.6
Pod
documented
| |||||||||||||||||||||||||||||
| 354 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 355 | 2 | my ( $ifindex ) = @_; | |||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | 2 | my $sock; | |||||||||||||||||||||||||||
| 358 | 2 | ▶ | if( ref $self ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 359 | 0 | $sock = $self; | |||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | else { | ||||||||||||||||||||||||||||
| 362 | 2 | ▶ | socket( $sock, AF_INET, SOCK_STREAM, 0 ) or | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 363 | croak "Cannot socket(AF_INET) - $!"; | ||||||||||||||||||||||||||||
| 364 | } | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 2 | return siocgifname( $sock, $ifindex ); | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | sub _make_sockopt_int | ||||||||||||||||||||||||||||
| 370 | 2 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_make_sockopt_int: called CC=1 SCAR=3.5
| |||||||||||||||||||||||||||||
| 371 | 2 | my ( $optname ) = @_; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | # IO::Socket automatically handles the pack/unpack in this case | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 376 | 0 | my $sock = shift; | |||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | 0 | ▶ | if( @_ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 0 | $sock->setsockopt( SOL_PACKET, $optname, $_[0] ); | |||||||||||||||||||||||||||
| 380 | } | ||||||||||||||||||||||||||||
| 381 | else { | ||||||||||||||||||||||||||||
| 382 | 0 | return $sock->getsockopt( SOL_PACKET, $optname ); | |||||||||||||||||||||||||||
| 383 | } | ||||||||||||||||||||||||||||
| 384 | 2 | }; | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =head1 SOCKET OPTION ACCESSORS | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | =cut | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =head2 add_multicast | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | $sock->add_multicast( $addr, $ifindex ); | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | Adds the given multicast address on the given interface index. If the | ||||||||||||||||||||||||||||
| 396 | interface index is not supplied, C<< $sock->ifindex >> is used. | ||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||
| 398 | =cut | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | sub add_multicast | ||||||||||||||||||||||||||||
| 401 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
add_multicast: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 402 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 403 | 0 | my ( $addr, $ifindex ) = @_; | |||||||||||||||||||||||||||
| 404 | 0 | ▶ | defined $ifindex or $ifindex = $self->ifindex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | 0 | $self->setsockopt( SOL_PACKET, PACKET_ADD_MEMBERSHIP, | |||||||||||||||||||||||||||
| 407 | pack_packet_mreq( $ifindex, PACKET_MR_MULTICAST, $addr ) | ||||||||||||||||||||||||||||
| 408 | ); | ||||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | =head2 drop_multicast | ||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | $sock->drop_multicast( $addr, $ifindex ); | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | Drops the given multicast address on the given interface index. If the | ||||||||||||||||||||||||||||
| 416 | interface index is not supplied, C<< $sock->ifindex >> is used. | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | =cut | ||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | sub drop_multicast | ||||||||||||||||||||||||||||
| 421 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
drop_multicast: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 422 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 423 | 0 | my ( $addr, $ifindex ) = @_; | |||||||||||||||||||||||||||
| 424 | 0 | ▶ | defined $ifindex or $ifindex = $self->ifindex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | 0 | $self->setsockopt( SOL_PACKET, PACKET_DROP_MEMBERSHIP, | |||||||||||||||||||||||||||
| 427 | pack_packet_mreq( $ifindex, PACKET_MR_MULTICAST, $addr ) | ||||||||||||||||||||||||||||
| 428 | ); | ||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =head2 promisc | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | $sock->promisc( $promisc, $ifindex ); | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | Sets or clears the PACKET_MR_PROMISC flag on the given interface. If the | ||||||||||||||||||||||||||||
| 436 | interface index is not supplied, C<< $sock->ifindex >> is used. | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | =cut | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | sub promisc | ||||||||||||||||||||||||||||
| 441 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
promisc: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 442 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 443 | 0 | my ( $value, $ifindex ) = @_; | |||||||||||||||||||||||||||
| 444 | 0 | ▶ | defined $ifindex or $ifindex = $self->ifindex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | 0 | ▶ | $self->setsockopt( SOL_PACKET, $value ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 447 | pack_packet_mreq( $ifindex, PACKET_MR_PROMISC, "" ) | ||||||||||||||||||||||||||||
| 448 | ); | ||||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | |||||||||||||||||||||||||||||
| 451 | =head2 allmulti | ||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | $sock->allmulti( $allmulti, $ifindex ); | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | Sets or clears the PACKET_MR_ALLMULTI flag on the given interface. If the | ||||||||||||||||||||||||||||
| 456 | interface index is not supplied, C<< $sock->ifindex >> is used. | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | =cut | ||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||
| 460 | sub allmulti | ||||||||||||||||||||||||||||
| 461 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
allmulti: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 462 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 463 | 0 | my ( $value, $ifindex ) = @_; | |||||||||||||||||||||||||||
| 464 | 0 | ▶ | defined $ifindex or $ifindex = $self->ifindex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||
| 466 | 0 | ▶ | $self->setsockopt( SOL_PACKET, $value ? PACKET_ADD_MEMBERSHIP : PACKET_DROP_MEMBERSHIP, | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 467 | pack_packet_mreq( $ifindex, PACKET_MR_ALLMULTI, "" ) | ||||||||||||||||||||||||||||
| 468 | ); | ||||||||||||||||||||||||||||
| 469 | } | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | =head2 statistics | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | $stats = $sock->statistics; | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | Returns the socket statistics. This will be a two-field hash containing | ||||||||||||||||||||||||||||
| 476 | counts C<packets>, the total number of packets the socket has seen, and | ||||||||||||||||||||||||||||
| 477 | C<drops>, the number of packets that could not stored because the buffer was | ||||||||||||||||||||||||||||
| 478 | full. | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | =cut | ||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||
| 482 | sub statistics | ||||||||||||||||||||||||||||
| 483 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
statistics: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 484 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | 0 | ▶ | my $stats = $self->getsockopt( SOL_PACKET, PACKET_STATISTICS ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 487 | or return; | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 0 | my %stats; | |||||||||||||||||||||||||||
| 490 | 0 | @stats{qw( packets drops)} = unpack_tpacket_stats( $stats ); | |||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 0 | return \%stats; | |||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | =head2 origdev | ||||||||||||||||||||||||||||
| 496 | |||||||||||||||||||||||||||||
| 497 | $val = $sock->origdev; | ||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | $sock->origdev( $val ); | ||||||||||||||||||||||||||||
| 500 | |||||||||||||||||||||||||||||
| 501 | Return or set the value of the C<PACKET_ORIGDEV> socket option. | ||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | =cut | ||||||||||||||||||||||||||||
| 504 | |||||||||||||||||||||||||||||
| 505 | 2 | ▶ | if( defined &Socket::Packet::PACKET_ORIGDEV ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 506 | 2 | *origdev = _make_sockopt_int( Socket::Packet::PACKET_ORIGDEV() ); | |||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | =head1 RING-BUFFER METHODS | ||||||||||||||||||||||||||||
| 510 | |||||||||||||||||||||||||||||
| 511 | These methods operate on the high-performance memory-mapped capture buffer. | ||||||||||||||||||||||||||||
| 512 | |||||||||||||||||||||||||||||
| 513 | An example of how to use these methods for packet capture is included in the | ||||||||||||||||||||||||||||
| 514 | module distribution; see F<examples/capture-rxring.pl> for more detail. | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | =cut | ||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||
| 518 | =head2 setup_rx_ring | ||||||||||||||||||||||||||||
| 519 | |||||||||||||||||||||||||||||
| 520 | $size = $sock->setup_rx_ring( $frame_size, $frame_nr, $block_size ); | ||||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | Sets up the ring-buffer on the object. This method is identical to the | ||||||||||||||||||||||||||||
| 523 | C<Socket::Packet> function C<setup_rx_ring>, except that the ring-buffer | ||||||||||||||||||||||||||||
| 524 | variable is stored transparently within the C<$sock> object; the caller does | ||||||||||||||||||||||||||||
| 525 | not need to manage it. | ||||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | Once this buffer is enabled, the C<recv_len>, C<timestamp> and | ||||||||||||||||||||||||||||
| 528 | C<timestamp_nano> methods will automatically use it instead of the regular | ||||||||||||||||||||||||||||
| 529 | C<recv()>+C<ioctl()> interface. | ||||||||||||||||||||||||||||
| 530 | |||||||||||||||||||||||||||||
| 531 | =cut | ||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||
| 533 | sub setup_rx_ring | ||||||||||||||||||||||||||||
| 534 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
setup_rx_ring: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 535 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 536 | 0 | my ( $frame_size, $frame_nr, $block_size ) = @_; | |||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||
| 538 | 0 | my $ret = Socket::Packet::setup_rx_ring( $self, $frame_size, $frame_nr, $block_size ); | |||||||||||||||||||||||||||
| 539 | 0 | ▶ | ${*$self}{packet_rx_ring} = 1 if defined $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 540 | |||||||||||||||||||||||||||||
| 541 | 0 | return $ret; | |||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||
| 544 | =head2 get_ring_frame | ||||||||||||||||||||||||||||
| 545 | |||||||||||||||||||||||||||||
| 546 | $len = $sock->get_ring_frame( $buffer, \%info ); | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | Receives the next packet from the ring-buffer. If there are no packets waiting | ||||||||||||||||||||||||||||
| 549 | it will return undef. This method aliases the C<$buffer> variable to the | ||||||||||||||||||||||||||||
| 550 | C<mmap()>ed packet buffer. | ||||||||||||||||||||||||||||
| 551 | |||||||||||||||||||||||||||||
| 552 | For detail on the C<%info> hash, see L<Socket::Packet>'s C<get_ring_frame()> | ||||||||||||||||||||||||||||
| 553 | function. | ||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | Once the caller has finished with the C<$buffer> data, the C<done_ring_frame> | ||||||||||||||||||||||||||||
| 556 | method should be called to hand the frame buffer back to the kernel. | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | =cut | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | sub get_ring_frame | ||||||||||||||||||||||||||||
| 561 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
get_ring_frame: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 562 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | 0 | return Socket::Packet::get_ring_frame( $self, $_[0], $_[1] ); | |||||||||||||||||||||||||||
| 565 | } | ||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | =head2 wait_ring_frame | ||||||||||||||||||||||||||||
| 568 | |||||||||||||||||||||||||||||
| 569 | $len = $sock->wait_ring_frame( $buffer, \%info ); | ||||||||||||||||||||||||||||
| 570 | |||||||||||||||||||||||||||||
| 571 | If a packet is ready, this method sets C<$buffer> and C<%info> as per the | ||||||||||||||||||||||||||||
| 572 | C<get_ring_frame> method. If there are no packets waiting and the socket is | ||||||||||||||||||||||||||||
| 573 | in blocking mode, it will C<select()> on the socket until a packet is | ||||||||||||||||||||||||||||
| 574 | available. If the socket is in non-blocking mode, it will return false with | ||||||||||||||||||||||||||||
| 575 | C<$!> set to C<EAGAIN>. | ||||||||||||||||||||||||||||
| 576 | |||||||||||||||||||||||||||||
| 577 | For detail on the C<%info> hash, see L<Socket::Packet>'s C<get_ring_frame()> | ||||||||||||||||||||||||||||
| 578 | function. | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | Once the caller has finished with the C<$buffer> data, the C<done_ring_frame> | ||||||||||||||||||||||||||||
| 581 | method should be called to hand the frame buffer back to the kernel. | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | =cut | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | sub wait_ring_frame | ||||||||||||||||||||||||||||
| 586 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
wait_ring_frame: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 587 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 0 | my $len; | |||||||||||||||||||||||||||
| 590 | 0 | ▶ | while( !defined( $len = $self->get_ring_frame( $_[0], $_[1] ) ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 591 | 0 | ▶ | $! = EAGAIN, return if not $self->blocking; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 592 | |||||||||||||||||||||||||||||
| 593 | 0 | my $rvec = ''; | |||||||||||||||||||||||||||
| 594 | 0 | vec( $rvec, fileno $self, 1 ) = 1; | |||||||||||||||||||||||||||
| 595 | 0 | ▶ | select( $rvec, undef, undef, undef ) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 596 | } | ||||||||||||||||||||||||||||
| 597 | |||||||||||||||||||||||||||||
| 598 | 0 | return $len; | |||||||||||||||||||||||||||
| 599 | } | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | =head2 done_ring_frame | ||||||||||||||||||||||||||||
| 602 | |||||||||||||||||||||||||||||
| 603 | $sock->done_ring_frame; | ||||||||||||||||||||||||||||
| 604 | |||||||||||||||||||||||||||||
| 605 | Hands the current ring-buffer frame back to the kernel. | ||||||||||||||||||||||||||||
| 606 | |||||||||||||||||||||||||||||
| 607 | =cut | ||||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | sub done_ring_frame | ||||||||||||||||||||||||||||
| 610 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
done_ring_frame: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 611 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 612 | |||||||||||||||||||||||||||||
| 613 | 0 | Socket::Packet::done_ring_frame( $self ); | |||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||
| 615 | |||||||||||||||||||||||||||||
| 616 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =over 4 | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | =item * | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | L<Socket::Packet> - interface to Linux's C<PF_PACKET> socket family | ||||||||||||||||||||||||||||
| 623 | |||||||||||||||||||||||||||||
| 624 | =back | ||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||
| 626 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||
| 628 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | =cut | ||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||
| 632 | 2 | 0x55AA; | |||||||||||||||||||||||||||