.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "Socket::MsgHdr 3pm" .TH Socket::MsgHdr 3pm "2022-10-19" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Socket::MsgHdr \- sendmsg, recvmsg and ancillary data operations .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& use Socket::MsgHdr; \& use Socket; \& \& # sendto() behavior \& my $echo = sockaddr_in(7, inet_aton("10.20.30.40")); \& my $outMsg = new Socket::MsgHdr(buf => "Testing echo service", \& name => $echo); \& sendmsg(OUT, $outMsg, 0) or die "sendmsg: $!\en"; \& \& # recvfrom() behavior, OO\-style \& my $msgHdr = new Socket::MsgHdr(buflen => 512) \& \& $msgHdr\->buflen(8192); # maybe 512 wasn\*(Aqt enough! \& $msgHdr\->namelen(256); # only 16 bytes needed for IPv4 \& \& die "recvmsg: $!\en" unless defined recvmsg(IN, $msgHdr, 0); \& \& my ($port, $iaddr) = sockaddr_in($msgHdr\->name()); \& my $dotted = inet_ntoa($iaddr); \& print "$dotted:$port said: " . $msgHdr\->buf() . "\en"; \& \& # Pack ancillary data for sending \& $outHdr\->cmsghdr(SOL_SOCKET, # cmsg_level \& SCM_RIGHTS, # cmsg_type \& pack("i", fileno(STDIN))); # cmsg_data \& sendmsg(OUT, $outHdr); \& \& # Unpack the same \& my $inHdr = Socket::MsgHdr\->new(buflen => 8192, controllen => 256); \& recvmsg(IN, $inHdr, $flags); \& my ($level, $type, $data) = $inHdr\->cmsghdr(); \& my $new_fileno = unpack(\*(Aqi\*(Aq, $data); \& open(NewFH, \*(Aq<&=\*(Aq . $new_fileno); # voila! .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Socket::MsgHdr provides advanced socket messaging operations via sendmsg and recvmsg. Like their C counterparts, these functions accept few parameters, instead stuffing a lot of information into a complex structure. .PP This structure describes the message sent or received (buf), the peer on the other end of the socket (name), and ancillary or so-called control information (cmsghdr). This ancillary data may be used for file descriptor passing, IPv6 operations, and a host of implemenation-specific extensions. .SS "\s-1FUNCTIONS\s0" .IX Subsection "FUNCTIONS" .IP "sendmsg \s-1SOCKET, MSGHDR\s0" 4 .IX Item "sendmsg SOCKET, MSGHDR" .PD 0 .IP "sendmsg \s-1SOCKET, MSGHDR, FLAGS\s0" 4 .IX Item "sendmsg SOCKET, MSGHDR, FLAGS" .PD Send a message as described by \f(CW\*(C`Socket::MsgHdr\*(C'\fR \s-1MSGHDR\s0 over \s-1SOCKET,\s0 optionally as specified by \s-1FLAGS\s0 (default 0). \s-1MSGHDR\s0 should supply at least a \fIbuf\fR member, and connectionless socket senders might also supply a \fIname\fR member. Ancillary data may be sent via \&\fIcontrol\fR. .Sp Returns number of bytes sent, or undef on failure. .IP "recvmsg \s-1SOCKET, MSGHDR\s0" 4 .IX Item "recvmsg SOCKET, MSGHDR" .PD 0 .IP "recvmsg \s-1SOCKET, MSGHDR, FLAGS\s0" 4 .IX Item "recvmsg SOCKET, MSGHDR, FLAGS" .PD Receive a message as requested by \f(CW\*(C`Socket::MsgHdr\*(C'\fR \s-1MSGHDR\s0 from \&\s-1SOCKET,\s0 optionally as specified by \s-1FLAGS\s0 (default 0). The caller requests \fIbuflen\fR bytes in \s-1MSGHDR,\s0 possibly also recording up to \&\fInamelen\fR bytes of the sender's (packed) address and perhaps \&\fIcontrollen\fR bytes of ancillary data. .Sp Returns number of bytes received, or undef on failure. \fIbuflen\fR et. al. are updated to reflect the actual lengths of received data. .SS "Socket::MsgHdr" .IX Subsection "Socket::MsgHdr" .IP "new [\s-1PARAMETERS\s0]" 4 .IX Item "new [PARAMETERS]" Return a new Socket::MsgHdr object. Optional \s-1PARAMETERS\s0 may specify method names (\f(CW\*(C`buf\*(C'\fR, \f(CW\*(C`name\*(C'\fR, \f(CW\*(C`control\*(C'\fR, \f(CW\*(C`flags\*(C'\fR or their corresponding \fI...len\fR methods where applicable) and values, sparing an explicit call to those methods. .IP "buf [\s-1SCALAR\s0]" 4 .IX Item "buf [SCALAR]" .PD 0 .IP "buflen \s-1LENGTH\s0" 4 .IX Item "buflen LENGTH" .PD \&\f(CW\*(C`buf\*(C'\fR gets the current message buffer or sets it to \s-1SCALAR.\s0 \f(CW\*(C`buflen\*(C'\fR allocates \s-1LENGTH\s0 bytes for use in recvmsg. .IP "name [\s-1SCALAR\s0]" 4 .IX Item "name [SCALAR]" .PD 0 .IP "namelen \s-1LENGTH\s0" 4 .IX Item "namelen LENGTH" .PD Get or set the socket name (address) buffer, an attribute analogous to the optional \s-1TO\s0 and \s-1FROM\s0 parameters of \*(L"send\*(R" in perlfunc and \*(L"recv\*(R" in perlfunc. Note that socket names are packed structures. .IP "controllen \s-1LENGTH\s0" 4 .IX Item "controllen LENGTH" Prepare the ancillary data buffer to receive \s-1LENGTH\s0 bytes. There is a corresponding \f(CW\*(C`control\*(C'\fR method, but its use is discouraged \*(-- you have to \&\*(L"pack\*(R" in perlfunc the \f(CW\*(C`struct cmsghdr\*(C'\fR yourself. Instead see cmsghdr below for convenient access to the control member. .IP "flags [\s-1FLAGS\s0]" 4 .IX Item "flags [FLAGS]" Get or set the Socket::MsgHdr flags, distinct from the sendmsg or recvmsg flags. Example: .Sp .Vb 5 \& $hdr = new Socket::MsgHdr (buflen => 512, controllen => 3); \& recvmsg(IN, $hdr); \& if ($hdr\->flags & MSG_CTRUNC) { # &Socket::MSG_CTRUNC \& warn "Yikes! Ancillary data was truncated\en"; \& } .Ve .IP "cmsghdr" 4 .IX Item "cmsghdr" .PD 0 .IP "cmsghdr \s-1LEVEL, TYPE, DATA\s0 [ \s-1LEVEL, TYPE, DATA ...\s0 ]" 4 .IX Item "cmsghdr LEVEL, TYPE, DATA [ LEVEL, TYPE, DATA ... ]" .PD Without arguments, this method returns a list of \*(L"\s-1LEVEL, TYPE, DATA, ...\*(R",\s0 or an empty list if there is no ancillary data. With arguments, this method copies and flattens its parameters into the internal control buffer. .Sp In any case, \s-1DATA\s0 is in a message-specific format which likely requires special treatment (packing or unpacking). .Sp Examples: .Sp .Vb 6 \& my @cmsg = $hdr\->cmsghdr(); \& while (my ($level, $type, $data) = splice(@cmsg, 0, 3)) { \& warn "unknown cmsg LEVEL\en", next unless $level == IPPROTO_IPV6; \& warn "unknown cmsg TYPE\en", next unless $type == IPV6_PKTINFO; \& ... \& } \& \& my $data = pack("i" x @filehandles, map {fileno $_} @filehandles); \& my $hdr\->cmsghdr(SOL_SOCKET, SCM_RIGHTS, $data); \& sendmsg(S, $hdr); .Ve .SS "\s-1EXPORT\s0" .IX Subsection "EXPORT" \&\f(CW\*(C`Socket::MsgHdr\*(C'\fR exports sendmsg and recvmsg by default into the caller's namespace, and in any case these methods into the IO::Socket namespace. .SH "BUGS" .IX Header "BUGS" The underlying \s-1XS\s0 presently makes use of \s-1RFC 2292\s0 CMSG_* manipulation macros, which may not be available on all systems supporting sendmsg/recvmsg as known to 4.3BSD Reno/POSIX.1g. Older \f(CW\*(C`struct msghdr\*(C'\fR definitions with \&\f(CW\*(C`msg_accrights\*(C'\fR members (instead of \f(CW\*(C`msg_control\*(C'\fR) are not supported at all. .PP There is no Socket::CMsgHdr, which may be a good thing. Examples are meager, see the t/ directory for send(to) and recv(from) emulations in terms of this module. .SH "SEE ALSO" .IX Header "SEE ALSO" \&\fBsendmsg\fR\|(2), \fBrecvmsg\fR\|(2), File::FDpasser, \s-1RFC 2292\s0 .SH "AUTHOR" .IX Header "AUTHOR" Michael J. Pomraning, co-maintained by Felipe Gasper .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" Copyright 2003, 2010 by Michael J. Pomraning .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.