blib/lib/IO/Socket/Netlink/Generic.pm

filter: Statementstmt 82.1% 60 / 73 Branchbran 35.7% 5 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 89.4% 17 / 19 Podpod 75.0% 6 / 8 totaltotal 77.1% 88 / 114 CC 8 SCAR 8.8
CC
8
Cov
75%
new_command
6.0
configure
4.4
_get_family
4.2
CRAP 2.4
« blib/lib/IO/Socket/Netlink.pm ↑ Summary blib/lib/Socket/Netlink/Generic_const.pm »
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, 2010-2016 -- leonerd@leonerd.org.uk
5  
6 package IO::Socket::Netlink::Generic;
7  
8 2 ▶use strict;
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::Netlink );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '0.05';
13  
14 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Socket::Netlink::Generic qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
17 NETLINK_GENERIC
18 CTRL_CMD_GETFAMILY
19 2 );
20  
21 2 __PACKAGE__->register_protocol( NETLINK_GENERIC );
22  
23 =head1 NAME
24  
25 C<IO::Socket::Netlink::Generic> - Object interface to C<NETLINK_GENERIC>
26 netlink protocol sockets
27  
28 =head1 SYNOPSIS
29  
30 use IO::Socket::Netlink::Generic;
31  
32 my $genlsock = IO::Socket::Netlink::Generic->new or die "socket: $!";
33  
34 printf "TASKSTATS family ID is %d\n",
35 $genlsock->get_family_by_name( "TASKSTATS" )->{id};
36  
37 =head1 DESCRIPTION
38  
39 This subclass of L<IO::Socket::Netlink> implements the C<NETLINK_GENERIC>
40 protocol. It is itself intended to serve as a base class for particular
41 generic families to extend.
42  
43 =cut
44  
45 =head1 CLASS METHODS
46  
47 =head2 register_family_name
48  
49 $class->register_family_name( $name )
50  
51 Must be called by a subclass implementing a particular protocol family, to
52 declare its family name. The first time a socket in that class is constructed,
53 this name will be looked up into an ID number.
54  
55 =cut
56  
57 2 my %pkg2familyname;
58 my %familyname2pkg;
59  
60 sub register_family_name
61 0 ▶{
Subroutine register_family_name: not called CC=1 SCAR=6.9
Pod documented
62 0 my ( $pkg, $family_name ) = @_;
63  
64 0 $pkg2familyname{$pkg} = $family_name;
65 0 $familyname2pkg{$family_name} = $pkg;
66 }
67  
68 sub new
69 1 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod documented
70 1 my $class = shift;
71 1 $class->SUPER::new( Protocol => NETLINK_GENERIC, @_ );
72 }
73  
74 sub configure
75 1 ▶{
Subroutine configure: called CC=3 SCAR=14.7
Pod undocumented
76 1 my $self = shift;
77 1 my ( $arg ) = @_;
78  
79 1 my $ret = $self->SUPER::configure( $arg );
80  
81 1 my $class = ref $self;
82 1 ▶ if( $class ne __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class ne __PACKAGE__) 0 1 1
83 0 ▶ defined( my $family_name = $pkg2familyname{$class} ) or
Branch: 1 branch
BranchTrueFalseTotal
defined(my $family_name = $pkg2familyname{$class}) or croak("No family name defined for $class") 0 0 0
84 croak "No family name defined for $class";
85  
86 0 my $family_id = $self->get_family_by_name( $family_name )->{id};
87 0 $class->message_class->register_nlmsg_type( $family_id );
88  
89 0 ${*$self}{default_nlmsg_type} = $family_id;
90 }
91  
92 1 return $ret;
93 }
94  
95 sub new_message
96 3 ▶{
Subroutine new_message: called CC=2 SCAR=7.6
Pod documented
97 3 my $self = shift;
98  
99 $self->SUPER::new_message(
100 0 ▶ ( defined ${*$self}{default_nlmsg_type} ? ( nlmsg_type => ${*$self}{default_nlmsg_type} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined do { *$self }->{'default_nlmsg_type'} ? : 0 3 3
101 @_,
102 );
103 }
104  
105 sub new_command
106 0 ▶{
Subroutine new_command: not called CC=2 SCAR=17.9
Pod documented
107 0 my $self = shift;
108  
109 $self->SUPER::new_command(
110 0 ▶ ( defined ${*$self}{default_nlmsg_type} ? ( nlmsg_type => ${*$self}{default_nlmsg_type} ) : () ),
Branch: 1 branch
BranchTrueFalseTotal
defined do { *$self }->{'default_nlmsg_type'} ? : 0 0 0
111 @_,
112 );
113 }
114  
115 sub message_class
116 5 ▶{
Subroutine message_class: called CC=1 SCAR=0.0
Pod undocumented
117 5 return "IO::Socket::Netlink::Generic::_Message";
118 }
119  
120 =head1 METHODS
121  
122 =cut
123  
124 =head2 get_family_by_name
125  
126 $family = $sock->get_family_by_name( $name )
127  
128 =cut
129  
130 sub get_family_by_name
131 1 ▶{
Subroutine get_family_by_name: called CC=1 SCAR=0.0
Pod documented
132 1 my $self = shift;
133 1 my ( $name ) = @_;
134  
135 1 return $self->_get_family( name => $name );
136 }
137  
138 =head2 get_family_by_id
139  
140 $family = $sock->get_family_by_id( $id )
141  
142 =cut
143  
144 sub get_family_by_id
145 1 ▶{
Subroutine get_family_by_id: called CC=1 SCAR=0.0
Pod documented
146 1 my $self = shift;
147 1 my ( $id ) = @_;
148  
149 1 return $self->_get_family( id => $id );
150 }
151  
152 =pod
153  
154 Query the kernel for information on the C<NETLINK_GENERIC> family specifed by
155 name or ID number, and return information about it. Returns a HASH reference
156 containing the following fields:
157  
158 =over 8
159  
160 =item id => NUMBER
161  
162 =item name => STRING
163  
164 =item version => NUMBER
165  
166 =item hdrsize => NUMBER
167  
168 =item maxattr => NUMBER
169  
170 =back
171  
172 =cut
173  
174 sub _get_family
175 2 ▶{
Subroutine _get_family: called CC=4 SCAR=14.5
176 2 my $self = shift;
177 2 my %searchattrs = @_;
178  
179 2 ▶ $self->send_nlmsg( $self->new_request(
Branch: 1 branch
BranchTrueFalseTotal
$self->send_nlmsg($self->new_request("nlmsg_type", 16, "cmd", 3, "nlattrs", \%searchattrs)) or croak("Cannot send - $!") 2 0 2
180 nlmsg_type => NETLINK_GENERIC,
181  
182 cmd => CTRL_CMD_GETFAMILY,
183 nlattrs => \%searchattrs,
184 ) ) or croak "Cannot send - $!";
185  
186 2 ▶ $self->recv_nlmsg( my $message, 32768 ) or
Branch: 1 branch
BranchTrueFalseTotal
$self->recv_nlmsg(my $message, 32768) or croak("Cannot recv - $!") 2 0 2
187 croak "Cannot recv - $!";
188  
189 2 ▶ $message->nlmsg_type == NETLINK_GENERIC or
Branch: 1 branch
BranchTrueFalseTotal
$message->nlmsg_type == 16 or croak("Expected nlmsg_type == NETLINK_GENERIC") 2 0 2
190 croak "Expected nlmsg_type == NETLINK_GENERIC";
191  
192 2 return $message->nlattrs;
193 }
194  
195 package IO::Socket::Netlink::Generic::_Message;
196  
197 2 ▶use base qw( IO::Socket::Netlink::_Message );
Subroutine BEGIN: called CC=1 SCAR=0.0
198  
199 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
200  
201 2 ▶use Socket::Netlink::Generic qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
202 :DEFAULT
203 pack_genlmsghdr unpack_genlmsghdr
204 2 );
205  
206 =head1 MESSAGE OBJECTS
207  
208 Sockets in this class provide the following extra field accessors on their
209 message objects:
210  
211 =cut
212  
213 2 __PACKAGE__->is_subclassed_by_type;
214  
215 2 __PACKAGE__->register_nlmsg_type( NETLINK_GENERIC );
216  
217 =over 8
218  
219 =item * $message->cmd
220  
221 ID number of the command to give to the family
222  
223 =item * $message->version
224  
225 Version number of the interface
226  
227 =item * $message->genlmsg
228  
229 Accessor for the trailing data buffer; intended for subclasses to use
230  
231 =back
232  
233 =cut
234  
235 2 __PACKAGE__->is_header(
236 data => "nlmsg",
237 fields => [
238 [ cmd => 'decimal' ],
239 [ version => 'decimal' ],
240 [ genlmsg => 'bytes' ],
241 ],
242 pack => \&pack_genlmsghdr,
243 unpack => \&unpack_genlmsghdr,
244 );
245  
246 sub nlmsg_string
247 1 ▶{
Subroutine nlmsg_string: called CC=1 SCAR=0.0
248 1 my $self = shift;
249 1 return sprintf "cmd=%d,version=%d,%s", $self->cmd, $self->version, $self->genlmsg_string;
250 }
251  
252 sub genlmsg_string
253 1 ▶{
Subroutine genlmsg_string: called CC=1 SCAR=0.0
254 1 my $self = shift;
255 1 return sprintf "{%d bytes}", length $self->genlmsg;
256 }
257  
258 2 __PACKAGE__->has_nlattrs(
259 "genlmsg",
260 id => [ CTRL_ATTR_FAMILY_ID, "u16" ],
261 name => [ CTRL_ATTR_FAMILY_NAME, "asciiz" ],
262 version => [ CTRL_ATTR_VERSION, "u32" ],
263 hdrsize => [ CTRL_ATTR_HDRSIZE, "u32" ],
264 maxattr => [ CTRL_ATTR_MAXATTR, "u32" ],
265 );
266  
267 =head1 SEE ALSO
268  
269 =over 4
270  
271 =item *
272  
273 L<Socket::Netlink::Generic> - interface to Linux's C<NETLINK_GENERIC> netlink
274 socket protocol
275  
276 =item *
277  
278 L<IO::Socket::Netlink> - Object interface to C<AF_NETLINK> domain sockets
279  
280 =back
281  
282 =head1 AUTHOR
283  
284 Paul Evans <leonerd@leonerd.org.uk>
285  
286 =cut
287  
288 2 0x55AA;
« blib/lib/IO/Socket/Netlink.pm ↑ Summary blib/lib/Socket/Netlink/Generic_const.pm »