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