| 1 | package Socket::MsgHdr; |
| 2 | |
| 3 | use 5.006; |
| 4 | use strict; |
| 5 | #use bytes; |
| 6 | |
| 7 | our @EXPORT = qw( sendmsg recvmsg ); |
| 8 | our @EXPORT_OK = qw( pack_cmsghdr unpack_cmsghdr ); # Undocumented! |
| 9 | our $VERSION = '0.06'; |
| 10 | |
| 11 | # Forcibly export our sendmsg, recvmsg methods |
| 12 | BEGIN { |
| 13 | *IO::Socket::sendmsg = \&sendmsg; |
| 14 | *IO::Socket::recvmsg = \&recvmsg; |
| 15 | } |
| 16 | |
| 17 | # Define our simple accessor/mutator methods |
| 18 | |
| 19 | sub flags { |
| 20 | my $self = shift; |
| 21 | $self->{flags} = shift if @_; |
| 22 | $self->{flags} = 0 unless defined $self->{flags}; |
| 23 | $self->{flags}; |
| 24 | } |
| 25 | |
| 26 | BEGIN { |
| 27 | for my $attr (qw|name buf control|) { |
| 28 | no strict 'refs'; |
| 29 | |
| 30 | *{$attr} = sub { |
| 31 | my $self = shift; |
| 32 | $self->{$attr} = shift if @_; |
| 33 | $self->{$attr} = '' unless defined $self->{$attr}; |
| 34 | $self->{$attr}; |
| 35 | }; |
| 36 | } |
| 37 | |
| 38 | foreach my $attr (qw|name buf control|) { |
| 39 | no strict 'refs'; |
| 40 | *{$attr . "len"} = sub { |
| 41 | my $self = shift; |
| 42 | my $olen = length($self->$attr); |
| 43 | return $olen unless @_; |
| 44 | my $nlen = shift; |
| 45 | |
| 46 | if ($nlen != $olen) { |
| 47 | $self->{$attr} = $olen > $nlen ? |
| 48 | substr($self->{$attr}, 0, $nlen) : |
| 49 | "\x00" x $nlen; |
| 50 | } |
| 51 | $nlen; |
| 52 | }; |
| 53 | } |
| 54 | |
| 55 | } |
| 56 | |
| 57 | # XS functions |
| 58 | # ============ |
| 59 | # |
| 60 | # -- sendmsg, recvmsg, pack_cmsghdr, unpack_cmsghdr |
| 61 | # |
| 62 | require XSLoader; |
| 63 | XSLoader::load('Socket::MsgHdr', $VERSION); |
| 64 | |
| 65 | # Module import |
| 66 | # ============= |
| 67 | # |
| 68 | sub import { |
| 69 | require Exporter; |
| 70 | goto &Exporter::import; |
| 71 | } |
| 72 | |
| 73 | # Constructor |
| 74 | # =========== |
| 75 | # |
| 76 | sub new { |
| 77 | my $class = shift; |
| 78 | my $self = { name => '', |
| 79 | control => '', |
| 80 | buf => '', |
| 81 | flags => 0 }; |
| 82 | |
| 83 | bless $self, $class; |
| 84 | |
| 85 | my %args = @_; |
| 86 | foreach my $m (keys %args) { |
| 87 | $self->$m($args{$m}); |
| 88 | } |
| 89 | |
| 90 | return $self; |
| 91 | } |
| 92 | |
| 93 | # Methods |
| 94 | # ======= |
| 95 | # |
| 96 | # -- cmsghdr |
| 97 | # |
| 98 | sub cmsghdr { |
| 99 | my $self = shift; |
| 100 | unless (@_) { return &unpack_cmsghdr($self->{control}); } |
| 101 | $self->{control} = &pack_cmsghdr(@_); |
| 102 | } |
| 103 | |
| 104 | # -- name, buf, control, flags |
| 105 | # -- namelen, buflen, controllen |
| 106 | # (loaded in INIT) |
| 107 | |
| 108 | 1; |
| 109 | __END__ |
| 110 | # begin POD |
| 111 | |
| 112 | =head1 NAME |
| 113 | |
| 114 | Socket::MsgHdr - sendmsg, recvmsg and ancillary data operations |
| 115 | |
| 116 | =head1 SYNOPSIS |
| 117 | |
| 118 | use Socket::MsgHdr; |
| 119 | use Socket; |
| 120 | |
| 121 | # sendto() behavior |
| 122 | my $echo = sockaddr_in(7, inet_aton("10.20.30.40")); |
| 123 | my $outMsg = new Socket::MsgHdr(buf => "Testing echo service", |
| 124 | name => $echo); |
| 125 | sendmsg(OUT, $outMsg, 0) or die "sendmsg: $!\n"; |
| 126 | |
| 127 | # recvfrom() behavior, OO-style |
| 128 | my $msgHdr = new Socket::MsgHdr(buflen => 512) |
| 129 | |
| 130 | $msgHdr->buflen(8192); # maybe 512 wasn't enough! |
| 131 | $msgHdr->namelen(256); # only 16 bytes needed for IPv4 |
| 132 | |
| 133 | die "recvmsg: $!\n" unless defined recvmsg(IN, $msgHdr, 0); |
| 134 | |
| 135 | my ($port, $iaddr) = sockaddr_in($msgHdr->name()); |
| 136 | my $dotted = inet_ntoa($iaddr); |
| 137 | print "$dotted:$port said: " . $msgHdr->buf() . "\n"; |
| 138 | |
| 139 | # Pack ancillary data for sending |
| 140 | $outHdr->cmsghdr(SOL_SOCKET, # cmsg_level |
| 141 | SCM_RIGHTS, # cmsg_type |
| 142 | pack("i", fileno(STDIN))); # cmsg_data |
| 143 | sendmsg(OUT, $outHdr); |
| 144 | |
| 145 | # Unpack the same |
| 146 | my $inHdr = Socket::MsgHdr->new(buflen => 8192, controllen => 256); |
| 147 | recvmsg(IN, $inHdr, $flags); |
| 148 | my ($level, $type, $data) = $inHdr->cmsghdr(); |
| 149 | my $new_fileno = unpack('i', $data); |
| 150 | open(NewFH, '<&=' . $new_fileno); # voila! |
| 151 | |
| 152 | =head1 DESCRIPTION |
| 153 | |
| 154 | Socket::MsgHdr provides advanced socket messaging operations via L<sendmsg> |
| 155 | and L<recvmsg>. Like their C counterparts, these functions accept few |
| 156 | parameters, instead stuffing a lot of information into a complex structure. |
| 157 | |
| 158 | This structure describes the message sent or received (L<buf>), the peer on |
| 159 | the other end of the socket (L<name>), and ancillary or so-called control |
| 160 | information (L<cmsghdr>). This ancillary data may be used for file descriptor |
| 161 | passing, IPv6 operations, and a host of implemenation-specific extensions. |
| 162 | |
| 163 | =head2 FUNCTIONS |
| 164 | |
| 165 | =over 4 |
| 166 | |
| 167 | =item sendmsg SOCKET, MSGHDR |
| 168 | |
| 169 | =item sendmsg SOCKET, MSGHDR, FLAGS |
| 170 | |
| 171 | Send a message as described by C<Socket::MsgHdr> MSGHDR over SOCKET, |
| 172 | optionally as specified by FLAGS (default 0). MSGHDR should supply |
| 173 | at least a I<buf> member, and connectionless socket senders might |
| 174 | also supply a I<name> member. Ancillary data may be sent via |
| 175 | I<control>. |
| 176 | |
| 177 | Returns number of bytes sent, or undef on failure. |
| 178 | |
| 179 | =item recvmsg SOCKET, MSGHDR |
| 180 | |
| 181 | =item recvmsg SOCKET, MSGHDR, FLAGS |
| 182 | |
| 183 | Receive a message as requested by C<Socket::MsgHdr> MSGHDR from |
| 184 | SOCKET, optionally as specified by FLAGS (default 0). The caller |
| 185 | requests I<buflen> bytes in MSGHDR, possibly also recording up to |
| 186 | I<namelen> bytes of the sender's (packed) address and perhaps |
| 187 | I<controllen> bytes of ancillary data. |
| 188 | |
| 189 | Returns number of bytes received, or undef on failure. I<buflen> |
| 190 | et. al. are updated to reflect the actual lengths of received data. |
| 191 | |
| 192 | =back |
| 193 | |
| 194 | =head2 Socket::MsgHdr |
| 195 | |
| 196 | =over 4 |
| 197 | |
| 198 | =item new [PARAMETERS] |
| 199 | |
| 200 | Return a new Socket::MsgHdr object. Optional PARAMETERS may specify method |
| 201 | names (C<buf>, C<name>, C<control>, C<flags> or their corresponding I<...len> |
| 202 | methods where applicable) and values, sparing an explicit call to those |
| 203 | methods. |
| 204 | |
| 205 | =item buf [SCALAR] |
| 206 | |
| 207 | =item buflen LENGTH |
| 208 | |
| 209 | C<buf> gets the current message buffer or sets it to SCALAR. C<buflen> |
| 210 | allocates LENGTH bytes for use in L<recvmsg>. |
| 211 | |
| 212 | =item name [SCALAR] |
| 213 | |
| 214 | =item namelen LENGTH |
| 215 | |
| 216 | Get or set the socket name (address) buffer, an attribute analogous to the |
| 217 | optional TO and FROM parameters of L<perlfunc/send> and L<perlfunc/recv>. |
| 218 | Note that socket names are packed structures. |
| 219 | |
| 220 | =item controllen LENGTH |
| 221 | |
| 222 | Prepare the ancillary data buffer to receive LENGTH bytes. There is a |
| 223 | corresponding C<control> method, but its use is discouraged -- you have to |
| 224 | L<perlfunc/pack> the C<struct cmsghdr> yourself. Instead see L<cmsghdr> below |
| 225 | for convenient access to the control member. |
| 226 | |
| 227 | =item flags [FLAGS] |
| 228 | |
| 229 | Get or set the Socket::MsgHdr flags, distinct from the L<sendmsg> or |
| 230 | L<recvmsg> flags. Example: |
| 231 | |
| 232 | $hdr = new Socket::MsgHdr (buflen => 512, controllen => 3); |
| 233 | recvmsg(IN, $hdr); |
| 234 | if ($hdr->flags & MSG_CTRUNC) { # &Socket::MSG_CTRUNC |
| 235 | warn "Yikes! Ancillary data was truncated\n"; |
| 236 | } |
| 237 | |
| 238 | =item cmsghdr |
| 239 | |
| 240 | =item cmsghdr LEVEL, TYPE, DATA [ LEVEL, TYPE, DATA ... ] |
| 241 | |
| 242 | Without arguments, this method returns a list of "LEVEL, TYPE, DATA, ...", or |
| 243 | an empty list if there is no ancillary data. With arguments, this method |
| 244 | copies and flattens its parameters into the internal control buffer. |
| 245 | |
| 246 | In any case, DATA is in a message-specific format which likely requires |
| 247 | special treatment (packing or unpacking). |
| 248 | |
| 249 | Examples: |
| 250 | |
| 251 | my @cmsg = $hdr->cmsghdr(); |
| 252 | while (my ($level, $type, $data) = splice(@cmsg, 0, 3)) { |
| 253 | warn "unknown cmsg LEVEL\n", next unless $level == IPPROTO_IPV6; |
| 254 | warn "unknown cmsg TYPE\n", next unless $type == IPV6_PKTINFO; |
| 255 | ... |
| 256 | } |
| 257 | |
| 258 | my $data = pack("i" x @filehandles, map {fileno $_} @filehandles); |
| 259 | my $hdr->cmsghdr(SOL_SOCKET, SCM_RIGHTS, $data); |
| 260 | sendmsg(S, $hdr); |
| 261 | |
| 262 | =back |
| 263 | |
| 264 | =head2 EXPORT |
| 265 | |
| 266 | C<Socket::MsgHdr> exports L<sendmsg> and L<recvmsg> by default into the |
| 267 | caller's namespace, and in any case these methods into the IO::Socket |
| 268 | namespace. |
| 269 | |
| 270 | =head1 BUGS |
| 271 | |
| 272 | The underlying XS presently makes use of RFC 2292 CMSG_* manipulation macros, |
| 273 | which may not be available on all systems supporting sendmsg/recvmsg as known |
| 274 | to 4.3BSD Reno/POSIX.1g. Older C<struct msghdr> definitions with |
| 275 | C<msg_accrights> members (instead of C<msg_control>) are not supported at all. |
| 276 | |
| 277 | There is no Socket::CMsgHdr, which may be a good thing. Examples are meager, |
| 278 | see the t/ directory for send(to) and recv(from) emulations in terms of this |
| 279 | module. |
| 280 | |
| 281 | =head1 SEE ALSO |
| 282 | |
| 283 | L<sendmsg(2)>, L<recvmsg(2)>, L<File::FDpasser>, L<RFC 2292|https://tools.ietf.org/html/rfc2292> |
| 284 | |
| 285 | =head1 AUTHOR |
| 286 | |
| 287 | Michael J. Pomraning, co-maintained by Felipe Gasper |
| 288 | |
| 289 | =head1 COPYRIGHT AND LICENSE |
| 290 | |
| 291 | Copyright 2003, 2010 by Michael J. Pomraning |
| 292 | |
| 293 | This library is free software; you can redistribute it and/or modify |
| 294 | it under the same terms as Perl itself. |
| 295 | |
| 296 | =cut |