xref: /dflybsd-src/lib/libc/sys/send.2 (revision a71d6c20fd77500d4ae730b1b9c0228cd1f391da)
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. All advertising materials mentioning features or use of this software
13.\"    must display the following acknowledgement:
14.\"	This product includes software developed by the University of
15.\"	California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\"    may be used to endorse or promote products derived from this software
18.\"    without specific prior written permission.
19.\"
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
31.\"
32.\"     From: @(#)send.2	8.2 (Berkeley) 2/21/94
33.\" $FreeBSD: src/lib/libc/sys/send.2,v 1.10.2.6 2001/12/14 18:34:01 ru Exp $
34.\" $DragonFly: src/lib/libc/sys/send.2,v 1.4 2004/08/27 21:23:32 asmodai Exp $
35.\"
36.Dd February 15, 1995
37.Dt SEND 2
38.Os
39.Sh NAME
40.Nm send ,
41.Nm sendto ,
42.Nm sendmsg
43.Nd send a message from a socket
44.Sh LIBRARY
45.Lb libc
46.Sh SYNOPSIS
47.In sys/types.h
48.In sys/socket.h
49.Ft ssize_t
50.Fn send "int s" "const void *msg" "size_t len" "int flags"
51.Ft ssize_t
52.Fn sendto "int s" "const void *msg" "size_t len" "int flags" "const struct sockaddr *to" "socklen_t tolen"
53.Ft ssize_t
54.Fn sendmsg "int s" "const struct msghdr *msg" "int flags"
55.Sh DESCRIPTION
56.Fn Send ,
57.Fn sendto ,
58and
59.Fn sendmsg
60are used to transmit a message to another socket.
61.Fn Send
62may be used only when the socket is in a
63.Em connected
64state, while
65.Fn sendto
66and
67.Fn sendmsg
68may be used at any time.
69.Pp
70The socket file descriptor is given by
71.Fa s .
72For
73.Fn send
74and
75.Fn sendto
76.Fa msg
77points to a buffer containing the message.
78For
79.Fn sendmsg
80.Fa msg
81points to a msghdr structure.
82The address of the target is given by
83.Fa to
84with
85.Fa tolen
86specifying its size.
87The length of the message is given by
88.Fa len .
89If the message is too long to pass atomically through the
90underlying protocol, the error
91.Er EMSGSIZE
92is returned, and
93the message is not transmitted.
94.Pp
95No indication of failure to deliver is implicit in a
96.Fn send .
97Locally detected errors are indicated by a return value of -1.
98.Pp
99If no messages space is available at the socket to hold
100the message to be transmitted, then
101.Fn send
102normally blocks, unless the socket has been placed in
103non-blocking I/O mode.
104The
105.Xr select 2
106call may be used to determine when it is possible to
107send more data.
108.Pp
109The
110.Fa flags
111parameter may include one or more of the following:
112.Bd -literal
113#define	MSG_OOB		0x1   /* process out-of-band data */
114#define	MSG_PEEK	0x2   /* peek at incoming message */
115#define	MSG_DONTROUTE	0x4   /* bypass routing, use direct interface */
116#define MSG_EOR		0x8   /* data completes record */
117#define	MSG_EOF		0x100 /* data completes transaction */
118.Ed
119.Pp
120The flag
121.Dv MSG_OOB
122is used to send
123.Dq out-of-band
124data on sockets that support this notion (e.g.\&
125.Dv SOCK_STREAM ) ;
126the underlying protocol must also support
127.Dq out-of-band
128data.
129.Dv MSG_EOR
130is used to indicate a record mark for protocols which support the
131concept.
132.Dv MSG_EOF
133requests that the sender side of a socket be shut down, and that an
134appropriate indication be sent at the end of the specified data;
135this flag is only implemented for
136.Dv SOCK_STREAM
137sockets in the
138.Dv PF_INET
139protocol family, and is used to implement Transaction
140.Tn TCP
141(see
142.Xr ttcp 4 ) .
143.Dv MSG_DONTROUTE
144is usually used only by diagnostic or routing programs.
145.Pp
146See
147.Xr recv 2
148for a description of the
149.Fa msghdr
150structure.
151.Sh RETURN VALUES
152The call returns the number of characters sent, or -1
153if an error occurred.
154.Sh ERRORS
155.Fn Send ,
156.Fn sendto ,
157and
158.Fn sendmsg
159fail if:
160.Bl -tag -width Er
161.It Bq Er EBADF
162An invalid descriptor was specified.
163.It Bq Er EACCES
164The destination address is a broadcast address, and
165.Dv SO_BROADCAST
166has not been set on the socket.
167.It Bq Er ENOTSOCK
168The argument
169.Fa s
170is not a socket.
171.It Bq Er EFAULT
172An invalid user space address was specified for a parameter.
173.It Bq Er EMSGSIZE
174The socket requires that message be sent atomically,
175and the size of the message to be sent made this impossible.
176.It Bq Er EAGAIN
177The socket is marked non-blocking and the requested operation
178would block.
179.It Bq Er ENOBUFS
180The system was unable to allocate an internal buffer.
181The operation may succeed when buffers become available.
182.It Bq Er ENOBUFS
183The output queue for a network interface was full.
184This generally indicates that the interface has stopped sending,
185but may be caused by transient congestion.
186.It Bq Er EHOSTUNREACH
187The remote host was unreachable.
188.It Bq Er ECONNREFUSED
189The socket received an ICMP destination unreachable message
190from the last message sent.  This typically means that the
191receiver is not listening on the remote port.
192.It Bq Er EHOSTDOWN
193The remote host was down.
194.It Bq Er EPIPE
195The socket is unable to send anymore data (SS_CANTSENDMORE has
196been set on the socket). This typically means that the socket
197is not connected.
198.El
199.Sh BUGS
200Because
201.Fn sendmsg
202doesn't necessarily block until the data has been transferred, it
203is possible to transfer an open file descriptor across an
204.Dv AF_UNIX
205domain socket
206(see
207.Xr recv 2 ) ,
208then
209.Fn close
210it before it has actually been sent, the result being that the receiver
211gets a closed file descriptor.  It is left to the application to
212implement an acknowlegment mechanism to prevent this from happening.
213.Sh SEE ALSO
214.Xr fcntl 2 ,
215.Xr getsockopt 2 ,
216.Xr recv 2 ,
217.Xr select 2 ,
218.Xr socket 2 ,
219.Xr write 2
220.Sh HISTORY
221The
222.Fn send
223function call appeared in
224.Bx 4.2 .
225