| 1 | package Linux::Perl::sendmsg; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | =encoding utf-8 | ||||||||||
| 7 | |||||||||||
| 8 | =head1 NAME | ||||||||||
| 9 | |||||||||||
| 10 | Linux::Perl::sendmsg | ||||||||||
| 11 | |||||||||||
| 12 | =head1 SYNOPSIS | ||||||||||
| 13 | |||||||||||
| 14 | my $bytes = Linux::Perl::sendmsg->sendmsg( | ||||||||||
| 15 | fd => $fd, | ||||||||||
| 16 | name => $name, | ||||||||||
| 17 | iovec => [ \$str1, \$str2, .. ], | ||||||||||
| 18 | control => [ $level, $type, $data ], | ||||||||||
| 19 | flags => \@flags, | ||||||||||
| 20 | ); | ||||||||||
| 21 | |||||||||||
| 22 | You can alternatively use your platform-specific module, e.g., | ||||||||||
| 23 | L<Linux::Perl::sendmsg::x86_64>. | ||||||||||
| 24 | |||||||||||
| 25 | =head1 DESCRIPTION | ||||||||||
| 26 | |||||||||||
| 27 | This module provides a Linux-specific C<sendmsg()> implementation. | ||||||||||
| 28 | See C<man 2 sendmsg> for what this can do differently from C<send()> | ||||||||||
| 29 | and C<sendto>. | ||||||||||
| 30 | |||||||||||
| 31 | =cut | ||||||||||
| 32 | |||||||||||
| 33 | 3 | ▶ | use parent 'Linux::Perl::Base'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 34 | |||||||||||
| 35 | 3 | ▶ | use Linux::Perl; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 36 | 3 | ▶ | use Linux::Perl::MsgHdr; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 37 | 3 | ▶ | use Linux::Perl::ParseFlags; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 38 | |||||||||||
| 39 | 3 | ▶ | use constant { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 40 | 3 | _EAGAIN => 11, | |||||||||
| 41 | |||||||||||
| 42 | _flag_CONFIRM => 0x800, | ||||||||||
| 43 | _flag_DONTROUTE => 4, | ||||||||||
| 44 | _flag_DONTWAIT => 0x40, | ||||||||||
| 45 | _flag_EOR => 0x80, | ||||||||||
| 46 | _flag_MORE => 0x8000, | ||||||||||
| 47 | _flag_NOSIGNAL => 0x4000, | ||||||||||
| 48 | _flag_OOB => 1, | ||||||||||
| 49 | 3 | }; | |||||||||
| 50 | |||||||||||
| 51 | =head1 METHODS | ||||||||||
| 52 | |||||||||||
| 53 | =head2 $bytes = I<CLASS>->sendmsg( %OPTS ) | ||||||||||
| 54 | |||||||||||
| 55 | If EAGAIN/EWOULDBLOCK is encountered, undef is returned. | ||||||||||
| 56 | |||||||||||
| 57 | %OPTS correspond to the system call arguments: | ||||||||||
| 58 | |||||||||||
| 59 | =over | ||||||||||
| 60 | |||||||||||
| 61 | =item * C<fd> | ||||||||||
| 62 | |||||||||||
| 63 | =item * C<name> - irrelevant for connected sockets | ||||||||||
| 64 | |||||||||||
| 65 | =item * C<iovec> - Optional, a reference to an array of string references | ||||||||||
| 66 | |||||||||||
| 67 | =item * C<control> - Optional, a reference to an array of: $LEVEL, $TYPE, $DATA. | ||||||||||
| 68 | See below for examples. If you don’t use this, you might as well use Perl’s | ||||||||||
| 69 | C<send()> built-in. | ||||||||||
| 70 | |||||||||||
| 71 | =item * C<flags> - Optional, a reference to an array of any/all of: C<CONFIRM>, | ||||||||||
| 72 | C<DONTROUTE>, C<DONTWAIT>, C<EOR>, C<MORE>, C<NOSIGNAL>, C<OOB>. | ||||||||||
| 73 | |||||||||||
| 74 | =back | ||||||||||
| 75 | |||||||||||
| 76 | =cut | ||||||||||
| 77 | |||||||||||
| 78 | # fd, flags, name, iov, control(level, type, data) | ||||||||||
| 79 | 6 | ▶ | sub sendmsg { | ||||||||
|
Subroutine
sendmsg: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||
| 80 | 6 | my ($class, %opts) = @_; | |||||||||
| 81 | |||||||||||
| 82 | 6 | $class = $class->_get_arch_module(); | |||||||||
| 83 | |||||||||||
| 84 | my $flags = Linux::Perl::ParseFlags::parse( | ||||||||||
| 85 | $class, | ||||||||||
| 86 | 6 | $opts{'flags'}, | |||||||||
| 87 | ); | ||||||||||
| 88 | |||||||||||
| 89 | 6 | local @Linux::Perl::_TOLERATE_ERRNO = ( _EAGAIN() ); | |||||||||
| 90 | |||||||||||
| 91 | my $ret = Linux::Perl::call( | ||||||||||
| 92 | $class->NR_sendmsg(), | ||||||||||
| 93 | 6 | 0 + $opts{'fd'}, | |||||||||
| 94 | Linux::Perl::MsgHdr::pack_msghdr(%opts), | ||||||||||
| 95 | 0 + $flags, | ||||||||||
| 96 | ); | ||||||||||
| 97 | |||||||||||
| 98 | 6 | ▶ | return( (-1 == $ret) ? undef : $ret ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | #---------------------------------------------------------------------- | ||||||||||
| 102 | |||||||||||
| 103 | =head1 CONTROL EXAMPLES | ||||||||||
| 104 | |||||||||||
| 105 | =head2 Sending credentials via local socket | ||||||||||
| 106 | |||||||||||
| 107 | use Socket; | ||||||||||
| 108 | |||||||||||
| 109 | control => [ | ||||||||||
| 110 | Socket::SOL_SOCKET(), Socket::SCM_CREDENTIALS(), | ||||||||||
| 111 | pack( 'I!*', $$, $>, split( m< >, $) ) ), | ||||||||||
| 112 | ] | ||||||||||
| 113 | |||||||||||
| 114 | =head2 Passing open file descriptors via local socket | ||||||||||
| 115 | |||||||||||
| 116 | control => [ | ||||||||||
| 117 | Socket::SOL_SOCKET(), Socket::SCM_RIGHTS(), | ||||||||||
| 118 | pack( 'I!*', @file_descriptors ), | ||||||||||
| 119 | ] | ||||||||||
| 120 | |||||||||||
| 121 | Also see L<Socket::MsgHdr>’s documentation for another example. | ||||||||||
| 122 | |||||||||||
| 123 | =head1 TODO | ||||||||||
| 124 | |||||||||||
| 125 | I’m not sure if C<recvmsg()> is feasible to implement in pure Perl, | ||||||||||
| 126 | but that would be a natural complement to this module. | ||||||||||
| 127 | |||||||||||
| 128 | =head1 SEE ALSO | ||||||||||
| 129 | |||||||||||
| 130 | L<Socket::MsgHdr> provides both C<sendmsg()> and C<recvmsg()>. | ||||||||||
| 131 | |||||||||||
| 132 | =cut | ||||||||||
| 133 | |||||||||||
| 134 | 3 | 1; | |||||||||