line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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 Socket::Netlink; |
7
|
|
|
|
|
|
|
|
8
|
10
|
|
|
10
|
|
122239
|
use strict; |
|
10
|
|
|
|
|
21
|
|
|
10
|
|
|
|
|
354
|
|
9
|
10
|
|
|
10
|
|
52
|
use warnings; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
398
|
|
10
|
|
|
|
|
|
|
|
11
|
10
|
|
|
10
|
|
57
|
use Carp; |
|
10
|
|
|
|
|
18
|
|
|
10
|
|
|
|
|
1034
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
14
|
|
|
|
|
|
|
|
15
|
10
|
|
|
10
|
|
66
|
use Exporter 'import'; |
|
10
|
|
|
|
|
19
|
|
|
10
|
|
|
|
|
1222
|
|
16
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
17
|
|
|
|
|
|
|
pack_sockaddr_nl unpack_sockaddr_nl |
18
|
|
|
|
|
|
|
pack_nlmsghdr unpack_nlmsghdr |
19
|
|
|
|
|
|
|
pack_nlmsgerr unpack_nlmsgerr |
20
|
|
|
|
|
|
|
pack_nlattrs unpack_nlattrs |
21
|
|
|
|
|
|
|
); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
require XSLoader; |
24
|
|
|
|
|
|
|
XSLoader::load( __PACKAGE__, $VERSION ); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 NAME |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
C - interface to Linux's C socket family |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head1 SYNOPSIS |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
use Socket; |
33
|
|
|
|
|
|
|
use Socket::Netlink qw( :DEFAULT pack_nlmsghdr unpack_nlmsghdr ); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
socket( my $sock, PF_NETLINK, SOCK_RAW, 0 ) or die "socket: $!"; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
send( $sock, pack_nlmsghdr( 18, NLM_F_REQUEST|NLM_F_DUMP, 0, 0, |
38
|
|
|
|
|
|
|
"\0\0\0\0\0\0\0\0" ), 0 ) |
39
|
|
|
|
|
|
|
or die "send: $!"; |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
recv( $sock, my $buffer, 65536, 0 ) or die "recv: $!"; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
printf "Received type=%d flags=%x:\n%v02x\n", |
44
|
|
|
|
|
|
|
( unpack_nlmsghdr( $buffer ) )[ 0, 1, 4 ]; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=head1 DESCRIPTION |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
This module contains the low-level constants and structure handling functions |
49
|
|
|
|
|
|
|
required to use Linux's C socket family. It is suggested to use |
50
|
|
|
|
|
|
|
the high-level object interface to this instead; see L. |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=cut |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head1 CONSTANTS |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
The following constants are exported |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=over 8 |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item PF_NETLINK |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
The packet family (for C calls) |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item AF_NETLINK |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
The address family |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=back |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=cut |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head1 ADDRESS FUNCTIONS |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
The following pair of functions operate on C address structures. |
75
|
|
|
|
|
|
|
The meainings of the parameters are: |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=over 8 |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item pid |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
The unique endpoint number for this netlink socket. If given as 0 to the |
82
|
|
|
|
|
|
|
C syscall, the kernel will allocate an endpoint number of the |
83
|
|
|
|
|
|
|
process's PID. |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=item groups |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
A 32-bit bitmask of the multicast groups to join. |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
=back |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 pack_sockaddr_nl |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
$addr = pack_sockaddr_nl( $pid, $groups ) |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
Returns a C structure with the fields packed into it. |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
=head2 unpack_sockaddr_nl |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
( $pid, $groups ) = unpack_sockaddr_nl( $addr ) |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
Takes a C structure and returns the unpacked fields from it. |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=cut |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=head1 STRUCTURE FUNCTIONS |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
The following function pairs operate on structure types used by netlink |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head2 pack_nlmsghdr |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
$buffer = pack_nlmsghdr( $type, $flags, $seq, $pid, $body ) |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head2 unpack_nlmsghdr |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
( $type, $flags, $seq, $pid, $body, $morebuffer ) = unpack_nlmsghdr( $buffer ) |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Pack or unpack a C and its payload body. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
Because a single netlink message can contain more than payload body, the |
120
|
|
|
|
|
|
|
C function will return the remaining buffer after unpacking |
121
|
|
|
|
|
|
|
the first message, in case there are others. If there are no more, the |
122
|
|
|
|
|
|
|
C<$morebuffer> list element will not be returned. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
while( defined $buffer ) { |
125
|
|
|
|
|
|
|
( my ( $type, $flags, $seq, $pid, $body ), $buffer ) = unpack_nlmsghdr( $buffer ); |
126
|
|
|
|
|
|
|
... |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
There is no similar functionallity for C; simply concatenate |
130
|
|
|
|
|
|
|
multiple results together to send more than one message. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 pack_nlmsgerr |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
$buffer = pack_nlmsgerr( $error, $msg ) |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 unpack_nlmsgerr |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
( $error, $msg ) = unpack_nlmsgerr( $buffer ) |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
Pack or unpack a C. The kernel expects or reports negative |
141
|
|
|
|
|
|
|
integers in its structures; these functions take or return normal positive |
142
|
|
|
|
|
|
|
error values suitable for use with C<$!>. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head2 pack_nlattrs |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
$buffer = pack_nlattrs( %attrs ) |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
=head2 unpack_nlattrs |
149
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
%attrs = unpack_nlattrs( $buffer ) |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
Pack or unpack a list of netlink attributes. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
These functions take or return even-sized lists of C<$type, $value> pairs. |
155
|
|
|
|
|
|
|
The type will be the number in the netlink attribute message, and the value |
156
|
|
|
|
|
|
|
will be a plain packed string buffer. It is the caller's responsibilty to |
157
|
|
|
|
|
|
|
further pack/unpack this buffer as appropriate for the specific type. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
Because these functions take/return even-sized lists, they may be passed or |
160
|
|
|
|
|
|
|
returned into hashes. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head1 SEE ALSO |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=over 4 |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
C - netlink - Communication between kernel and userspace (AF_NETLINK) |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=item * |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
L - Object interface to C domain sockets |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=back |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=head1 AUTHOR |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Paul Evans |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=cut |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
0x55AA; |