blib/lib/IO/EventMux/Socket/MsgHdr.pm

filter: Statementstmt 70.5% 67 / 95 Branchbran 36.8% 14 / 38 Conditioncond 10.0% 2 / 20 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 95.0% 19 / 20 Podpod 100% 10 / 10 totaltotal 58.5% 113 / 193 CC 28 SCAR 48.8
CC
28
Cov
54%
socket_errors
342.0
_set_length
5.1
new
2.0
CRAP 132.3
↑ Summary lib/IO/EventMux/Socket/MsgHdr.xs »
1 package IO::EventMux::Socket::MsgHdr;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 our $VERSION = '0.02';
6  
7 =head1 NAME
8  
9 IO::EventMux::Socket::MsgHdr - sendmsg, recvmsg and ancillary data operations
10  
11 =head1 SYNOPSIS
12  
13 use IO::EventMux::Socket::MsgHdr;
14 use Socket;
15  
16 # sendto() behavior
17 my $echo = sockaddr_in(7, inet_aton("10.20.30.40"));
18 my $outMsg = new IO::EventMux::Socket::MsgHdr(buf => "Testing echo service",
19 name => $echo);
20 sendmsg(OUT, $outMsg, 0) or die "sendmsg: $!\n";
21  
22 # recvfrom() behavior, OO-style
23 my $msgHdr = new IO::EventMux::Socket::MsgHdr(buflen => 512)
24  
25 $msgHdr->buflen(8192); # maybe 512 wasn't enough!
26 $msgHdr->namelen(256); # only 16 bytes needed for IPv4
27
28 die "recvmsg: $!\n" unless defined recvmsg(IN, $msgHdr, 0);
29  
30 my ($port, $iaddr) = sockaddr_in($msgHdr->name());
31 my $dotted = inet_ntoa($iaddr);
32 print "$dotted:$port said: " . $msgHdr->buf() . "\n";
33  
34 # Pack ancillary data for sending
35 $outHdr->cmsghdr(SOL_SOCKET, # cmsg_level
36 SCM_RIGHTS, # cmsg_type
37 pack("i", fileno(STDIN))); # cmsg_data
38 sendmsg(OUT, $msgHdr);
39  
40 # Unpack the same
41 my $inHdr = IO::EventMux::Socket::MsgHdr->new(buflen => 8192, controllen => 256);
42 recvmsg(IN, $inHdr, $flags);
43 my ($level, $type, $data) = $inHdr->cmsghdr();
44 my $new_fileno = unpack('i', $data);
45 open(NewFH, '<&=' . $new_fileno); # voila!
46  
47 =head1 DESCRIPTION
48  
49 IO::EventMux::Socket::MsgHdr is a fork of L<Socket::MsgHdr> as the old author
50 did not respond in regards to a cleanup patch to get rid of warnings in both
51 modules and tests. This fork has since restructured the module so it's simpler
52 to understand and maintain.
53  
54 IO::EventMux::Socket::MsgHdr provides advanced socket messaging operations via
55 L<sendmsg> and L<recvmsg>. Like their C counterparts, these functions accept
56 few parameters, instead stuffing a lot of information into a complex structure.
57  
58 This structure describes the message sent or received (C<buf>), the peer on
59 the other end of the socket (L<name>), and ancillary or so-called control
60 information (L<cmsghdr>). This ancillary data may be used for file descriptor
61 passing, IPv6 operations, and a host of implementation-specific extensions.
62  
63 =cut
64  
65 =head1 METHODS
66  
67 =over
68  
69 =cut
70  
71 3 ▶use base "Exporter";
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 3 our @EXPORT = qw(sendmsg recvmsg);
74 3 our @EXPORT_OK = qw(pack_cmsghdr unpack_cmsghdr socket_errors);
75  
76 3 ▶use Errno qw(EPROTO ECONNREFUSED ETIMEDOUT EMSGSIZE ECONNREFUSED EHOSTUNREACH
Subroutine BEGIN: called CC=1 SCAR=0.0
77 3 ENETUNREACH EACCES EAGAIN ENOTCONN ECONNRESET EWOULDBLOCK);
78 3 ▶use Fcntl qw(F_GETFL F_SETFL O_NONBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
79 3 ▶use POSIX qw(strerror);
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 3 ▶use Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 3 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
83 3 SOL_IP => 0,
84 IP_RECVERR => 11,
85 SO_EE_ORIGIN_NONE => 0,
86 SO_EE_ORIGIN_LOCAL => 1,
87 SO_EE_ORIGIN_ICMP => 2,
88 SO_EE_ORIGIN_ICMP6 => 3,
89 3 };
90  
91  
92 =item new()
93  
94 Return a new IO::EventMux::Socket::MsgHdr object. Optional PARAMETERS may specify method
95 names (C<buf>, C<name>, C<control>, C<flags> or their corresponding I<...len>
96 methods where applicable) and values, sparing an explicit call to those
97 methods.
98  
99 =cut
100  
101 25 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
102 25 my $class = shift;
103 25 my $self = { name => undef,
104 control => undef,
105 flags => 0 };
106
107 25 bless $self, $class;
108  
109 25 my %args = @_;
110 25 foreach my $m (keys %args) {
111 35 $self->$m($args{$m});
112 }
113  
114 25 return $self;
115 }
116  
117  
118 =item name [SCALAR]
119  
120 Get or set the socket name (address) buffer, an attribute analogous to the
121 optional TO and FROM parameters of L<perlfunc/send> and L<perlfunc/recv>.
122 Note that socket names are packed structures.
123  
124 =cut
125  
126 19 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
127 19 my ($self, $var) = @_;
128 19 ▶ $self->{name} = $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 4 15 19
129 19 $self->{name};
130 }
131  
132 =item namelen LENGTH
133  
134 =cut
135  
136 9 ▶sub namelen {
Subroutine namelen: called CC=1 SCAR=0.0
Pod documented
137 9 my ($self, $nlen) = @_;
138 9 $self->_set_length("name", $nlen);
139 }
140  
141 =item buf [SCALAR]
142  
143 =cut
144  
145 19 ▶sub buf {
Subroutine buf: called CC=2 SCAR=6.9
Pod documented
146 19 my ($self, $var) = @_;
147 19 ▶ $self->{buf} = $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 11 8 19
148 19 $self->{buf};
149 }
150  
151  
152 =item buflen LENGTH
153  
154 C<buf> gets the current message buffer or sets it to SCALAR. C<buflen>
155 allocates LENGTH bytes for use in L<recvmsg>.
156  
157 =cut
158  
159 10 ▶sub buflen {
Subroutine buflen: called CC=1 SCAR=0.0
Pod documented
160 10 my ($self, $nlen) = @_;
161 10 $self->_set_length("buf",$nlen);
162 }
163  
164 =item control()
165  
166 =cut
167  
168 5 ▶sub control {
Subroutine control: called CC=2 SCAR=6.9
Pod documented
169 5 my ($self, $var) = @_;
170 5 ▶ $self->{control} = $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 1 4 5
171 5 $self->{control};
172 }
173  
174  
175 =item controllen LENGTH
176  
177 Prepare the ancillary data buffer to receive LENGTH bytes. There is a
178 corresponding C<control> method, but its use is discouraged -- you have to
179 L<perlfunc/pack> the C<struct cmsghdr> yourself. Instead see L<cmsghdr> below
180 for convenient access to the control member.
181  
182 =cut
183  
184 7 ▶sub controllen {
Subroutine controllen: called CC=1 SCAR=0.0
Pod documented
185 7 my ($self, $nlen) = @_;
186 7 $self->_set_length("control",$nlen);
187 }
188  
189 =item flags [FLAGS]
190  
191 Get or set the IO::EventMux::Socket::MsgHdr flags, distinct from the L<sendmsg> or
192 L<recvmsg> flags. Example:
193  
194 $hdr = new IO::EventMux::Socket::MsgHdr (buflen => 512, controllen => 3);
195 recvmsg(IN, $hdr);
196 if ($hdr->flags & MSG_CTRUNC) { # &Socket::MSG_CTRUNC
197 warn "Yikes! Ancillary data was truncated\n";
198 }
199  
200 =cut
201  
202 2 ▶sub flags {
Subroutine flags: called CC=2 SCAR=6.9
Pod documented
203 2 my ($self, $var) = @_;
204 2 ▶ $self->{flags} = $var if defined $var;
Branch: 1 branch
BranchTrueFalseTotal
if defined $var 1 1 2
205 2 $self->{flags};
206 }
207  
208 =item cmsghdr LEVEL, TYPE, DATA [ LEVEL, TYPE, DATA ... ]
209  
210 Without arguments, this method returns a list of "LEVEL, TYPE, DATA, ...", or
211 an empty list if there is no ancillary data. With arguments, this method
212 copies and flattens its parameters into the internal control buffer.
213  
214 In any case, DATA is in a message-specific format which likely requires
215 special treatment (packing or unpacking).
216  
217 Examples:
218  
219 my @cmsg = $hdr->cmsghdr();
220 while (my ($level, $type, $data) = splice(@cmsg, 0, 3)) {
221 warn "unknown cmsg LEVEL\n", next unless $level == IPPROTO_IPV6;
222 warn "unknown cmsg TYPE\n", next unless $type == IPV6_PKTINFO;
223 ...
224 }
225  
226 my $data = pack("i" x @filehandles, map {fileno $_} @filehandles);
227 my $hdr->cmsghdr(SOL_SOCKET, SCM_RIGHTS, $data);
228 sendmsg(S, $hdr);
229  
230 =cut
231  
232 5 ▶sub cmsghdr {
Subroutine cmsghdr: called CC=2 SCAR=6.9
Pod documented
233 5 my $self = shift;
234 5 ▶ unless (@_) { return &unpack_cmsghdr($self->{control}); }
Branch: 1 branch
BranchTrueFalseTotal
unless (@_) 2 3 5
235 3 $self->{control} = &pack_cmsghdr(@_);
236 }
237  
238 =item sendmsg SOCKET, MSGHDR
239  
240 =item sendmsg SOCKET, MSGHDR, FLAGS
241  
242 Send a message as described by C<IO::EventMux::Socket::MsgHdr> MSGHDR over SOCKET,
243 optionally as specified by FLAGS (default 0). MSGHDR should supply
244 at least a I<buf> member, and connectionless socket senders might
245 also supply a I<name> member. Ancillary data may be sent via
246 I<control>.
247  
248 Returns number of bytes sent, or undef on failure.
249  
250 =item recvmsg SOCKET, MSGHDR
251  
252 =item recvmsg SOCKET, MSGHDR, FLAGS
253  
254 Receive a message as requested by C<IO::EventMux::Socket::MsgHdr> MSGHDR from
255 SOCKET, optionally as specified by FLAGS (default 0). The caller
256 requests I<buflen> bytes in MSGHDR, possibly also recording up to
257 I<namelen> bytes of the sender's (packed) address and perhaps
258 I<controllen> bytes of ancillary data.
259  
260 Returns number of bytes received, or undef on failure. I<buflen>
261 et. al. are updated to reflect the actual lengths of received data.
262  
263 =item pack_cmsghdr
264  
265 =item unpack_cmsghdr
266  
267 =cut
268  
269 3 require XSLoader;
270 3 XSLoader::load('IO::EventMux::Socket::MsgHdr', $VERSION);
271  
272  
273 # Module import
274 # =============
275 #
276 3 ▶sub import {
Subroutine import: called CC=1 SCAR=0.0
277 3 require Exporter;
278 3 goto &Exporter::import;
279 }
280  
281 26 ▶sub _set_length {
Subroutine _set_length: called CC=5 SCAR=16.2
282 26 my ($self, $attr, $nlen) = @_;
283 26 ▶ my $olen = length($self->{$attr} or '');
Condition100%2/2
$self->{$attr} || ''
l!l
818
MC/DC100%1/1
$self->{$attr} || ''
$self->{$attr}
Truth table100%2/2
A
A= $self->{$attr}
Aresult
11
00
284 26 ▶ return $olen unless defined $nlen;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $nlen 9 17 26
285
286 17 ▶ if ($nlen != $olen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nlen != $olen) 17 0 17
287 $self->{$attr} = $olen > $nlen
288 17 ▶ ? substr($self->{$attr}, 0, $nlen)
Branch: 1 branch
BranchTrueFalseTotal
$olen > $nlen ? : 0 17 17
289 : "\x00" x $nlen;
290 }
291 17 return $nlen;
292 }
293  
294 =item B<socket_errors($socket)>
295  
296 Read "MSG_ERRQUEUE" errors on socket and decode ICMP error msg
297  
298 =cut
299  
300 0 ▶sub socket_errors {
Subroutine socket_errors: not called CC=18 SCAR=58.3
Pod documented
301 0 my ($sock) = @_;
302
303 0 my @results;
304 0 my $msgHdr = new IO::EventMux::Socket::MsgHdr(
305 buflen => 512,
306 controllen => 256,
307 namelen => 16,
308 );
309
310 # Copy errors to msgHdr
311 0 my $old_errno = $!;
312 0 my $rv = recvmsg($sock, $msgHdr, MSG_ERRQUEUE);
313 0 ▶ if(not defined $rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
314 0 ▶ if($old_errno != $! and $! != EAGAIN) {
Branch: 1 branch
BranchTrueFalseTotal
if ($old_errno != $! and $! != 11) 0 0 0
Condition0%0/3
$old_errno != $! and $! != 11
!ll&&!rl&&r
000
MC/DC0%0/2
$old_errno != $! and $! != 11
$old_errno != $! $! != 11
Truth table0%0/3
A and B
A= $old_errno != $!B= $! != 11
ABresult
0X0
100
111
315 0 print "error(socket_errors):$!\n";
316 }
317 0 return;
318 }
319
320 # Unpack errors
321 0 my @cmsg = $msgHdr->cmsghdr();
322 0 ▶ while (my ($level, $type, $data) = splice(@cmsg, 0, 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@cmsg, 0, 3)) 0 0 0
323 0 ▶ if($level == SOL_IP and $type == IP_RECVERR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($level == 0 and $type == 11) { } 0 0 0
Condition0%0/3
$level == 0 and $type == 11
!ll&&!rl&&r
000
MC/DC0%0/2
$level == 0 and $type == 11
$level == 0 $type == 11
Truth table0%0/3
A and B
A= $level == 0B= $type == 11
ABresult
0X0
100
111
324 0 my ($from, $dst_ip, $dst_port, $pkt);
325  
326 # struct sock_extended_err from man recvmsg
327 0 my ($ee_errno, $ee_origin, $ee_type, $ee_code, $ee_pad,
328 $ee_info, $ee_data, $ee_other) = unpack("ICCCCIIa*", $data);
329
330 0 ▶ if($ee_origin == SO_EE_ORIGIN_NONE) {
Branch: 4 branches
BranchTrueFalseTotal
if ($ee_origin == 0) { } 0 0 0
elsif ($ee_origin == 1) { } 0 0 0
elsif ($ee_origin == 2) { } 0 0 0
elsif ($ee_origin == 3) { } 0 0 0
331 0 print "error(socket_errors): origin is none??\n";
332 0 next;
333
334 } elsif($ee_origin == SO_EE_ORIGIN_LOCAL) {
335 0 $from = 'localhost';
336  
337 } elsif($ee_origin == SO_EE_ORIGIN_ICMP) {
338 # Get offender ip($from)(the one who sent the ICMP message)
339 # and $dst_ip and $dst_port from packet in ICMP packet.
340 0 ($from, $dst_ip, $dst_port) = (
341 inet_ntoa((unpack_sockaddr_in($ee_other))[1]),
342 inet_ntoa((unpack_sockaddr_in($msgHdr->name))[1]),
343 (unpack_sockaddr_in($msgHdr->name))[0]
344 );
345
346 # Get what's left of the packet
347 0 $pkt = $msgHdr->buf;
348
349 } elsif($ee_origin == SO_EE_ORIGIN_ICMP6) {
350 0 die "IPv6 not supported, patches welcome";
351 }
352  
353 0 ▶ if($ee_errno == ECONNREFUSED) {
Branch: 3 branches
BranchTrueFalseTotal
if ($ee_errno == 111) { } 0 0 0
elsif ($ee_errno == 90) { } 0 0 0
elsif ($ee_errno == 110 or $ee_errno == 71 or $ee_errno == 113 or $ee_errno == 101 or $ee_errno == 13) { } 0 0 0
354 0 push(@results, {
355 type => 'error',
356 errno => $ee_errno,
357 error => strerror($ee_errno),
358 from => $from,
359 dst_ip => $dst_ip,
360 dst_port => $dst_port,
361 data => $pkt,
362 fh => $sock,
363 });
364 } elsif($ee_errno == EMSGSIZE) {
365 0 push(@results, {
366 type => 'error',
367 errno => $ee_errno,
368 error => strerror($ee_errno),
369 mtu => $ee_info,
370 fh => $sock,
371 });
372 ▶ } elsif($ee_errno == ETIMEDOUT or $ee_errno == EPROTO
Condition0%0/12
$ee_errno == 110 or $ee_errno == 71 or $ee_errno == 113 or $ee_errno == 101 or $ee_errno == 13
l!l&&r!l&&!r
000
$ee_errno == 110 or $ee_errno == 71 or $ee_errno == 113 or $ee_errno == 101
l!l&&r!l&&!r
000
$ee_errno == 110 or $ee_errno == 71 or $ee_errno == 113
l!l&&r!l&&!r
000
$ee_errno == 110 or $ee_errno == 71
l!l&&r!l&&!r
000
MC/DC0%0/5
$ee_errno == 110 or $ee_errno == 71 or $ee_errno == 113 or $ee_errno == 101 or $ee_errno == 13
$ee_errno == 110 $ee_errno == 71 $ee_errno == 113 $ee_errno == 101 $ee_errno == 13
Truth table0%0/6
A or B or C or D or E
A= $ee_errno == 110B= $ee_errno == 71C= $ee_errno == 113D= $ee_errno == 101E= $ee_errno == 13
ABCDEresult
1XXXX1
01XXX1
001XX1
0001X1
000011
000000
373 or $ee_errno == EHOSTUNREACH or $ee_errno == ENETUNREACH
374 or $ee_errno == EACCES) {
375 0 push(@results, {
376 type => 'error',
377 fh => $sock,
378 errno => $ee_errno,
379 error => strerror($ee_errno),
380 });
381 } else {
382 0 push(@results, {
383 type => 'error',
384 fh => $sock,
385 errno => $ee_errno,
386 error => strerror($ee_errno),
387 });
388 }
389
390 } else {
391 0 print "error(socket_errors): unknown type: $type and/or $level\n";
392 }
393 }
394 0 return @results;
395 }
396  
397 =back
398  
399 =head1 EXPORT
400  
401 C<IO::EventMux::Socket::MsgHdr> exports L<sendmsg> and L<recvmsg> by default into the
402 caller's namespace, and in any case these methods into the IO::Socket
403 namespace.
404  
405 =head2 BUGS
406  
407 The underlying XS presently makes use of RFC 2292 CMSG_* manipulation macros,
408 which may not be available on all systems supporting sendmsg/recvmsg as known
409 to 4.3BSD Reno/POSIX.1g. Older C<struct msghdr> definitions with
410 C<msg_accrights> members (instead of C<msg_control>) are not supported at all.
411  
412 There is no Socket::CMsgHdr, which may be a good thing. Examples are meager,
413 see the t/ directory for send(to) and recv(from) emulations in terms of this
414 module.
415  
416 =head1 SEE ALSO
417  
418 L<sendmsg(2)>, L<recvmsg(2)>, L<"RFC 2292">
419  
420 =head1 AUTHOR
421  
422 Troels Liebe Bentsen <tlb@rapanden.dk>
423  
424 =head1 COPYRIGHT AND LICENSE
425  
426 Copyright(C) 2007-2008 by Troels Liebe Bentsen
427 Copyright(C) 2003 by Michael J. Pomraning
428  
429 This library is free software; you can redistribute it and/or modify
430 it under the same terms as Perl itself.
431  
432 =cut
433  
434 3 1;
↑ Summary lib/IO/EventMux/Socket/MsgHdr.xs »