xref: /freebsd/lib/libsys/send.2 (revision 8269e7673cf033aba67dab8264fe719920c70f87)
1*8269e767SBrooks Davis.\" Copyright (c) 1983, 1991, 1993
2*8269e767SBrooks Davis.\"	The Regents of the University of California.  All rights reserved.
3*8269e767SBrooks Davis.\"
4*8269e767SBrooks Davis.\" Redistribution and use in source and binary forms, with or without
5*8269e767SBrooks Davis.\" modification, are permitted provided that the following conditions
6*8269e767SBrooks Davis.\" are met:
7*8269e767SBrooks Davis.\" 1. Redistributions of source code must retain the above copyright
8*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer.
9*8269e767SBrooks Davis.\" 2. Redistributions in binary form must reproduce the above copyright
10*8269e767SBrooks Davis.\"    notice, this list of conditions and the following disclaimer in the
11*8269e767SBrooks Davis.\"    documentation and/or other materials provided with the distribution.
12*8269e767SBrooks Davis.\" 3. Neither the name of the University nor the names of its contributors
13*8269e767SBrooks Davis.\"    may be used to endorse or promote products derived from this software
14*8269e767SBrooks Davis.\"    without specific prior written permission.
15*8269e767SBrooks Davis.\"
16*8269e767SBrooks Davis.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17*8269e767SBrooks Davis.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8269e767SBrooks Davis.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8269e767SBrooks Davis.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20*8269e767SBrooks Davis.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8269e767SBrooks Davis.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8269e767SBrooks Davis.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8269e767SBrooks Davis.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8269e767SBrooks Davis.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8269e767SBrooks Davis.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8269e767SBrooks Davis.\" SUCH DAMAGE.
27*8269e767SBrooks Davis.\"
28*8269e767SBrooks Davis.Dd April 27, 2020
29*8269e767SBrooks Davis.Dt SEND 2
30*8269e767SBrooks Davis.Os
31*8269e767SBrooks Davis.Sh NAME
32*8269e767SBrooks Davis.Nm send ,
33*8269e767SBrooks Davis.Nm sendto ,
34*8269e767SBrooks Davis.Nm sendmsg ,
35*8269e767SBrooks Davis.Nm sendmmsg
36*8269e767SBrooks Davis.Nd send message(s) from a socket
37*8269e767SBrooks Davis.Sh LIBRARY
38*8269e767SBrooks Davis.Lb libc
39*8269e767SBrooks Davis.Sh SYNOPSIS
40*8269e767SBrooks Davis.In sys/socket.h
41*8269e767SBrooks Davis.Ft ssize_t
42*8269e767SBrooks Davis.Fn send "int s" "const void *msg" "size_t len" "int flags"
43*8269e767SBrooks Davis.Ft ssize_t
44*8269e767SBrooks Davis.Fn sendto "int s" "const void *msg" "size_t len" "int flags" "const struct sockaddr *to" "socklen_t tolen"
45*8269e767SBrooks Davis.Ft ssize_t
46*8269e767SBrooks Davis.Fn sendmsg "int s" "const struct msghdr *msg" "int flags"
47*8269e767SBrooks Davis.Ft ssize_t
48*8269e767SBrooks Davis.Fn sendmmsg "int s" "struct mmsghdr * restrict msgvec" "size_t vlen" "int flags"
49*8269e767SBrooks Davis.Sh DESCRIPTION
50*8269e767SBrooks DavisThe
51*8269e767SBrooks Davis.Fn send
52*8269e767SBrooks Davisand
53*8269e767SBrooks Davis.Fn sendmmsg
54*8269e767SBrooks Davisfunctions,
55*8269e767SBrooks Davisand
56*8269e767SBrooks Davis.Fn sendto
57*8269e767SBrooks Davisand
58*8269e767SBrooks Davis.Fn sendmsg
59*8269e767SBrooks Davissystem calls
60*8269e767SBrooks Davisare used to transmit one or more messages (with the
61*8269e767SBrooks Davis.Fn sendmmsg
62*8269e767SBrooks Daviscall) to
63*8269e767SBrooks Davisanother socket.
64*8269e767SBrooks DavisThe
65*8269e767SBrooks Davis.Fn send
66*8269e767SBrooks Davisfunction
67*8269e767SBrooks Davismay be used only when the socket is in a
68*8269e767SBrooks Davis.Em connected
69*8269e767SBrooks Davisstate.
70*8269e767SBrooks DavisThe functions
71*8269e767SBrooks Davis.Fn sendto ,
72*8269e767SBrooks Davis.Fn sendmsg
73*8269e767SBrooks Davisand
74*8269e767SBrooks Davis.Fn sendmmsg
75*8269e767SBrooks Davismay be used at any time if the socket is connectionless-mode.
76*8269e767SBrooks DavisIf the socket is connection-mode, the protocol
77*8269e767SBrooks Davismust support implied connect (currently
78*8269e767SBrooks Davis.Xr tcp 4
79*8269e767SBrooks Davisis the only protocol with support) or the socket must be in a
80*8269e767SBrooks Davisconnected state before use.
81*8269e767SBrooks Davis.Pp
82*8269e767SBrooks DavisThe address of the target is given by
83*8269e767SBrooks Davis.Fa to
84*8269e767SBrooks Daviswith
85*8269e767SBrooks Davis.Fa tolen
86*8269e767SBrooks Davisspecifying its size, or the equivalent
87*8269e767SBrooks Davis.Fa msg_name
88*8269e767SBrooks Davisand
89*8269e767SBrooks Davis.Fa msg_namelen
90*8269e767SBrooks Davisin
91*8269e767SBrooks Davis.Fa struct msghdr .
92*8269e767SBrooks DavisIf the socket is in a connected state, the target address passed to
93*8269e767SBrooks Davis.Fn sendto ,
94*8269e767SBrooks Davis.Fn sendmsg
95*8269e767SBrooks Davisor
96*8269e767SBrooks Davis.Fn sendmmsg
97*8269e767SBrooks Davisis ignored.
98*8269e767SBrooks DavisThe length of the message is given by
99*8269e767SBrooks Davis.Fa len .
100*8269e767SBrooks DavisIf the message is too long to pass atomically through the
101*8269e767SBrooks Davisunderlying protocol, the error
102*8269e767SBrooks Davis.Er EMSGSIZE
103*8269e767SBrooks Davisis returned, and
104*8269e767SBrooks Davisthe message is not transmitted.
105*8269e767SBrooks Davis.Pp
106*8269e767SBrooks DavisThe
107*8269e767SBrooks Davis.Fn sendmmsg
108*8269e767SBrooks Davisfunction sends multiple messages at a call.
109*8269e767SBrooks DavisThey are given by the
110*8269e767SBrooks Davis.Fa msgvec
111*8269e767SBrooks Davisvector along with
112*8269e767SBrooks Davis.Fa vlen
113*8269e767SBrooks Davisspecifying the vector size.
114*8269e767SBrooks DavisThe number of octets sent per each message is placed in the
115*8269e767SBrooks Davis.Fa msg_len
116*8269e767SBrooks Davisfield of each processed element of the vector after transmission.
117*8269e767SBrooks Davis.Pp
118*8269e767SBrooks DavisNo indication of failure to deliver is implicit in a
119*8269e767SBrooks Davis.Fn send .
120*8269e767SBrooks DavisLocally detected errors are indicated by a return value of -1.
121*8269e767SBrooks Davis.Pp
122*8269e767SBrooks DavisIf no messages space is available at the socket to hold
123*8269e767SBrooks Davisthe message to be transmitted, then
124*8269e767SBrooks Davis.Fn send
125*8269e767SBrooks Davisnormally blocks, unless the socket has been placed in
126*8269e767SBrooks Davisnon-blocking I/O mode.
127*8269e767SBrooks DavisThe
128*8269e767SBrooks Davis.Xr select 2
129*8269e767SBrooks Davissystem call may be used to determine when it is possible to
130*8269e767SBrooks Davissend more data.
131*8269e767SBrooks Davis.Pp
132*8269e767SBrooks DavisThe
133*8269e767SBrooks Davis.Fa flags
134*8269e767SBrooks Davisargument may include one or more of the following:
135*8269e767SBrooks Davis.Bd -literal
136*8269e767SBrooks Davis#define	MSG_OOB		0x00001 /* process out-of-band data */
137*8269e767SBrooks Davis#define	MSG_DONTROUTE	0x00004 /* bypass routing, use direct interface */
138*8269e767SBrooks Davis#define	MSG_EOR		0x00008 /* data completes record */
139*8269e767SBrooks Davis#define	MSG_DONTWAIT	0x00080 /* do not block */
140*8269e767SBrooks Davis#define	MSG_EOF		0x00100 /* data completes transaction */
141*8269e767SBrooks Davis#define	MSG_NOSIGNAL	0x20000 /* do not generate SIGPIPE on EOF */
142*8269e767SBrooks Davis.Ed
143*8269e767SBrooks Davis.Pp
144*8269e767SBrooks DavisThe flag
145*8269e767SBrooks Davis.Dv MSG_OOB
146*8269e767SBrooks Davisis used to send
147*8269e767SBrooks Davis.Dq out-of-band
148*8269e767SBrooks Davisdata on sockets that support this notion (e.g.\&
149*8269e767SBrooks Davis.Dv SOCK_STREAM ) ;
150*8269e767SBrooks Davisthe underlying protocol must also support
151*8269e767SBrooks Davis.Dq out-of-band
152*8269e767SBrooks Davisdata.
153*8269e767SBrooks Davis.Dv MSG_EOR
154*8269e767SBrooks Davisis used to indicate a record mark for protocols which support the
155*8269e767SBrooks Davisconcept.
156*8269e767SBrooks DavisThe
157*8269e767SBrooks Davis.Dv MSG_DONTWAIT
158*8269e767SBrooks Davisflag request the call to return when it would block otherwise.
159*8269e767SBrooks Davis.Dv MSG_EOF
160*8269e767SBrooks Davisrequests that the sender side of a socket be shut down, and that an
161*8269e767SBrooks Davisappropriate indication be sent at the end of the specified data;
162*8269e767SBrooks Davisthis flag is only implemented for
163*8269e767SBrooks Davis.Dv SOCK_STREAM
164*8269e767SBrooks Davissockets in the
165*8269e767SBrooks Davis.Dv PF_INET
166*8269e767SBrooks Davisprotocol family.
167*8269e767SBrooks Davis.Dv MSG_DONTROUTE
168*8269e767SBrooks Davisis usually used only by diagnostic or routing programs.
169*8269e767SBrooks Davis.Dv MSG_NOSIGNAL
170*8269e767SBrooks Davisis used to prevent
171*8269e767SBrooks Davis.Dv SIGPIPE
172*8269e767SBrooks Davisgeneration when writing a socket that
173*8269e767SBrooks Davismay be closed.
174*8269e767SBrooks Davis.Pp
175*8269e767SBrooks DavisSee
176*8269e767SBrooks Davis.Xr recv 2
177*8269e767SBrooks Davisfor a description of the
178*8269e767SBrooks Davis.Fa msghdr
179*8269e767SBrooks Davisstructure and the
180*8269e767SBrooks Davis.Fa mmsghdr
181*8269e767SBrooks Davisstructure.
182*8269e767SBrooks Davis.Sh RETURN VALUES
183*8269e767SBrooks DavisThe
184*8269e767SBrooks Davis.Fn send ,
185*8269e767SBrooks Davis.Fn sendto
186*8269e767SBrooks Davisand
187*8269e767SBrooks Davis.Fn sendmsg
188*8269e767SBrooks Daviscalls
189*8269e767SBrooks Davisreturn the number of octets sent.
190*8269e767SBrooks DavisThe
191*8269e767SBrooks Davis.Fn sendmmsg
192*8269e767SBrooks Daviscall returns the number of messages sent.
193*8269e767SBrooks DavisIf an error occurred a value of -1 is returned.
194*8269e767SBrooks Davis.Sh ERRORS
195*8269e767SBrooks DavisThe
196*8269e767SBrooks Davis.Fn send
197*8269e767SBrooks Davisand
198*8269e767SBrooks Davis.Fn sendmmsg
199*8269e767SBrooks Davisfunctions and
200*8269e767SBrooks Davis.Fn sendto
201*8269e767SBrooks Davisand
202*8269e767SBrooks Davis.Fn sendmsg
203*8269e767SBrooks Davissystem calls
204*8269e767SBrooks Davisfail if:
205*8269e767SBrooks Davis.Bl -tag -width Er
206*8269e767SBrooks Davis.It Bq Er EBADF
207*8269e767SBrooks DavisAn invalid descriptor was specified.
208*8269e767SBrooks Davis.It Bq Er EACCES
209*8269e767SBrooks DavisThe destination address is a broadcast address, and
210*8269e767SBrooks Davis.Dv SO_BROADCAST
211*8269e767SBrooks Davishas not been set on the socket.
212*8269e767SBrooks Davis.It Bq Er ENOTCONN
213*8269e767SBrooks DavisThe socket is connection-mode but is not connected.
214*8269e767SBrooks Davis.It Bq Er ENOTSOCK
215*8269e767SBrooks DavisThe argument
216*8269e767SBrooks Davis.Fa s
217*8269e767SBrooks Davisis not a socket.
218*8269e767SBrooks Davis.It Bq Er EFAULT
219*8269e767SBrooks DavisAn invalid user space address was specified for an argument.
220*8269e767SBrooks Davis.It Bq Er EMSGSIZE
221*8269e767SBrooks DavisThe socket requires that message be sent atomically,
222*8269e767SBrooks Davisand the size of the message to be sent made this impossible.
223*8269e767SBrooks Davis.It Bq Er EAGAIN
224*8269e767SBrooks DavisThe socket is marked non-blocking, or
225*8269e767SBrooks Davis.Dv MSG_DONTWAIT
226*8269e767SBrooks Davisis specified, and the requested operation would block.
227*8269e767SBrooks Davis.It Bq Er ENOBUFS
228*8269e767SBrooks DavisThe system was unable to allocate an internal buffer.
229*8269e767SBrooks DavisThe operation may succeed when buffers become available.
230*8269e767SBrooks Davis.It Bq Er ENOBUFS
231*8269e767SBrooks DavisThe output queue for a network interface was full.
232*8269e767SBrooks DavisThis generally indicates that the interface has stopped sending,
233*8269e767SBrooks Davisbut may be caused by transient congestion.
234*8269e767SBrooks Davis.It Bq Er EHOSTUNREACH
235*8269e767SBrooks DavisThe remote host was unreachable.
236*8269e767SBrooks Davis.It Bq Er EISCONN
237*8269e767SBrooks DavisA destination address was specified and the socket is already connected.
238*8269e767SBrooks Davis.It Bq Er ECONNREFUSED
239*8269e767SBrooks DavisThe socket received an ICMP destination unreachable message
240*8269e767SBrooks Davisfrom the last message sent.
241*8269e767SBrooks DavisThis typically means that the
242*8269e767SBrooks Davisreceiver is not listening on the remote port.
243*8269e767SBrooks Davis.It Bq Er EHOSTDOWN
244*8269e767SBrooks DavisThe remote host was down.
245*8269e767SBrooks Davis.It Bq Er ENETDOWN
246*8269e767SBrooks DavisThe remote network was down.
247*8269e767SBrooks Davis.It Bq Er EADDRNOTAVAIL
248*8269e767SBrooks DavisThe process using a
249*8269e767SBrooks Davis.Dv SOCK_RAW
250*8269e767SBrooks Davissocket was jailed and the source
251*8269e767SBrooks Davisaddress specified in the IP header did not match the IP
252*8269e767SBrooks Davisaddress bound to the prison.
253*8269e767SBrooks Davis.It Bq Er EPIPE
254*8269e767SBrooks DavisThe socket is unable to send anymore data
255*8269e767SBrooks Davis.Dv ( SBS_CANTSENDMORE
256*8269e767SBrooks Davishas been set on the socket).
257*8269e767SBrooks DavisThis typically means that the socket
258*8269e767SBrooks Davisis not connected.
259*8269e767SBrooks Davis.El
260*8269e767SBrooks Davis.Sh SEE ALSO
261*8269e767SBrooks Davis.Xr connect 2 ,
262*8269e767SBrooks Davis.Xr fcntl 2 ,
263*8269e767SBrooks Davis.Xr getsockopt 2 ,
264*8269e767SBrooks Davis.Xr recv 2 ,
265*8269e767SBrooks Davis.Xr select 2 ,
266*8269e767SBrooks Davis.Xr socket 2 ,
267*8269e767SBrooks Davis.Xr write 2 ,
268*8269e767SBrooks Davis.Xr CMSG_DATA 3
269*8269e767SBrooks Davis.Sh HISTORY
270*8269e767SBrooks DavisThe
271*8269e767SBrooks Davis.Fn send
272*8269e767SBrooks Davisfunction appeared in
273*8269e767SBrooks Davis.Bx 4.2 .
274*8269e767SBrooks DavisThe
275*8269e767SBrooks Davis.Fn sendmmsg
276*8269e767SBrooks Davisfunction appeared in
277*8269e767SBrooks Davis.Fx 11.0 .
278*8269e767SBrooks Davis.Sh BUGS
279*8269e767SBrooks DavisBecause
280*8269e767SBrooks Davis.Fn sendmsg
281*8269e767SBrooks Davisdoes not necessarily block until the data has been transferred, it
282*8269e767SBrooks Davisis possible to transfer an open file descriptor across an
283*8269e767SBrooks Davis.Dv AF_UNIX
284*8269e767SBrooks Davisdomain socket
285*8269e767SBrooks Davis(see
286*8269e767SBrooks Davis.Xr recv 2 ) ,
287*8269e767SBrooks Davisthen
288*8269e767SBrooks Davis.Fn close
289*8269e767SBrooks Davisit before it has actually been sent, the result being that the receiver
290*8269e767SBrooks Davisgets a closed file descriptor.
291*8269e767SBrooks DavisIt is left to the application to
292*8269e767SBrooks Davisimplement an acknowledgment mechanism to prevent this from happening.
293