1*84d9c625SLionel Sambuc.\" $NetBSD: send.2,v 1.31 2013/07/14 14:29:09 njoly Exp $ 22fe8fb19SBen Gras.\" 32fe8fb19SBen Gras.\" Copyright (c) 1983, 1991, 1993 42fe8fb19SBen Gras.\" The Regents of the University of California. All rights reserved. 52fe8fb19SBen Gras.\" 62fe8fb19SBen Gras.\" Redistribution and use in source and binary forms, with or without 72fe8fb19SBen Gras.\" modification, are permitted provided that the following conditions 82fe8fb19SBen Gras.\" are met: 92fe8fb19SBen Gras.\" 1. Redistributions of source code must retain the above copyright 102fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer. 112fe8fb19SBen Gras.\" 2. Redistributions in binary form must reproduce the above copyright 122fe8fb19SBen Gras.\" notice, this list of conditions and the following disclaimer in the 132fe8fb19SBen Gras.\" documentation and/or other materials provided with the distribution. 142fe8fb19SBen Gras.\" 3. Neither the name of the University nor the names of its contributors 152fe8fb19SBen Gras.\" may be used to endorse or promote products derived from this software 162fe8fb19SBen Gras.\" without specific prior written permission. 172fe8fb19SBen Gras.\" 182fe8fb19SBen Gras.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 192fe8fb19SBen Gras.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 202fe8fb19SBen Gras.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 212fe8fb19SBen Gras.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 222fe8fb19SBen Gras.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 232fe8fb19SBen Gras.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 242fe8fb19SBen Gras.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 252fe8fb19SBen Gras.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 262fe8fb19SBen Gras.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 272fe8fb19SBen Gras.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 282fe8fb19SBen Gras.\" SUCH DAMAGE. 292fe8fb19SBen Gras.\" 302fe8fb19SBen Gras.\" @(#)send.2 8.2 (Berkeley) 2/21/94 312fe8fb19SBen Gras.\" 32f14fb602SLionel Sambuc.Dd June 22, 2012 332fe8fb19SBen Gras.Dt SEND 2 342fe8fb19SBen Gras.Os 352fe8fb19SBen Gras.Sh NAME 362fe8fb19SBen Gras.Nm send , 372fe8fb19SBen Gras.Nm sendto , 38f14fb602SLionel Sambuc.Nm sendmsg , 39f14fb602SLionel Sambuc.Nm sendmmsg 402fe8fb19SBen Gras.Nd send a message from a socket 412fe8fb19SBen Gras.Sh LIBRARY 422fe8fb19SBen Gras.Lb libc 432fe8fb19SBen Gras.Sh SYNOPSIS 442fe8fb19SBen Gras.In sys/socket.h 452fe8fb19SBen Gras.Ft ssize_t 462fe8fb19SBen Gras.Fn send "int s" "const void *msg" "size_t len" "int flags" 472fe8fb19SBen Gras.Ft ssize_t 482fe8fb19SBen Gras.Fn sendto "int s" "const void *msg" "size_t len" "int flags" "const struct sockaddr *to" "socklen_t tolen" 492fe8fb19SBen Gras.Ft ssize_t 502fe8fb19SBen Gras.Fn sendmsg "int s" "const struct msghdr *msg" "int flags" 51f14fb602SLionel Sambuc.Ft int 52f14fb602SLionel Sambuc.Fn sendmmsg "int s" "struct mmsghdr *mmsg" "unsigned int vlen" "unsigned int flags" 532fe8fb19SBen Gras.Sh DESCRIPTION 542fe8fb19SBen Gras.Fn send , 552fe8fb19SBen Gras.Fn sendto , 56f14fb602SLionel Sambuc.Fn sendmsg , 572fe8fb19SBen Grasand 58f14fb602SLionel Sambuc.Fn sendmmsg 592fe8fb19SBen Grasare used to transmit a message to another socket. 602fe8fb19SBen Gras.Fn send 612fe8fb19SBen Grasmay be used only when the socket is in a 622fe8fb19SBen Gras.Em connected 632fe8fb19SBen Grasstate, while 64f14fb602SLionel Sambuc.Fn sendto , 652fe8fb19SBen Gras.Fn sendmsg 66f14fb602SLionel Sambucand 67f14fb602SLionel Sambuc.Fn sendmmsg 682fe8fb19SBen Grasmay be used at any time. 692fe8fb19SBen Gras.Pp 70f14fb602SLionel SambucThe 71f14fb602SLionel Sambuc.Fn sendmmsg 72f14fb602SLionel Sambuccall be used to send multiple messages in the same call using an array of 73f14fb602SLionel Sambuc.Fa mmsghdr 74f14fb602SLionel Sambucelements with the following form, as defined in 75f14fb602SLionel Sambuc.Ao Pa sys/socket.h Ac : 76f14fb602SLionel Sambuc.Pp 77f14fb602SLionel Sambuc.Bd -literal 78f14fb602SLionel Sambucstruct mmsghdr { 79f14fb602SLionel Sambuc struct msghdr msg_hdr; /* the message to be sent */ 80f14fb602SLionel Sambuc unsigned int msg_len; /* number of bytes transmitted */ 81f14fb602SLionel Sambuc}; 82f14fb602SLionel Sambuc.Ed 83f14fb602SLionel Sambuc.Pp 84f14fb602SLionel SambucThe 85f14fb602SLionel Sambuc.Fa msg_len 86f14fb602SLionel Sambucmember contains the number of bytes sent for each 87f14fb602SLionel Sambuc.Fa msg_hdr 88f14fb602SLionel Sambucmember. 89f14fb602SLionel SambucThe array has 90f14fb602SLionel Sambuc.Fa vlen 91f14fb602SLionel Sambucelements, which is limited to 92f14fb602SLionel Sambuc.Dv 1024 . 93f14fb602SLionel SambucIf there is an error, a number fewer than 94f14fb602SLionel Sambuc.Fa vlen 95f14fb602SLionel Sambucmay be returned, and the error may be retrieved using 96f14fb602SLionel Sambuc.Xr getsockopt 2 97f14fb602SLionel Sambucwith 98f14fb602SLionel Sambuc.Dv SO_ERROR . 99f14fb602SLionel Sambuc.Pp 1002fe8fb19SBen GrasThe address of the target is given by 1012fe8fb19SBen Gras.Fa to 1022fe8fb19SBen Graswith 1032fe8fb19SBen Gras.Fa tolen 1042fe8fb19SBen Grasspecifying its size. 1052fe8fb19SBen GrasThe length of the message is given by 1062fe8fb19SBen Gras.Fa len . 1072fe8fb19SBen GrasIf the message is too long to pass atomically through the 1082fe8fb19SBen Grasunderlying protocol, the error 1092fe8fb19SBen Gras.Er EMSGSIZE 1102fe8fb19SBen Grasis returned, and 1112fe8fb19SBen Grasthe message is not transmitted. 1122fe8fb19SBen Gras.Pp 1132fe8fb19SBen GrasNo indication of failure to deliver is implicit in a 1142fe8fb19SBen Gras.Fn send . 1152fe8fb19SBen GrasLocally detected errors are indicated by a return value of \-1. 1162fe8fb19SBen Gras.Pp 1172fe8fb19SBen GrasIf no messages space is available at the socket to hold 1182fe8fb19SBen Grasthe message to be transmitted, then 1192fe8fb19SBen Gras.Fn send 1202fe8fb19SBen Grasnormally blocks, unless the socket has been placed in 1212fe8fb19SBen Grasnon-blocking I/O mode. 1222fe8fb19SBen GrasThe 1232fe8fb19SBen Gras.Xr select 2 1242fe8fb19SBen Grasor 1252fe8fb19SBen Gras.Xr poll 2 1262fe8fb19SBen Grascall may be used to determine when it is possible to 1272fe8fb19SBen Grassend more data. 128f14fb602SLionel SambucUnfortunately this does not work when the interface queue which is used to 129f14fb602SLionel Sambucsend the message is full, and the call returns 130f14fb602SLionel Sambuc.Er ENOBUFS . 1312fe8fb19SBen Gras.Pp 1322fe8fb19SBen GrasThe 1332fe8fb19SBen Gras.Fa flags 1342fe8fb19SBen Grasparameter may include one or more of the following: 1352fe8fb19SBen Gras.Bd -literal 1362fe8fb19SBen Gras#define MSG_OOB 0x0001 /* process out-of-band data */ 1372fe8fb19SBen Gras#define MSG_PEEK 0x0002 /* peek at incoming message */ 1382fe8fb19SBen Gras#define MSG_DONTROUTE 0x0004 /* bypass routing, use direct interface */ 1392fe8fb19SBen Gras#define MSG_EOR 0x0008 /* data completes record */ 1402fe8fb19SBen Gras#define MSG_NOSIGNAL 0x0400 /* do not generate SIGPIPE on EOF */ 1412fe8fb19SBen Gras.Ed 1422fe8fb19SBen Gras.Pp 1432fe8fb19SBen GrasThe flag 1442fe8fb19SBen Gras.Dv MSG_OOB 1452fe8fb19SBen Grasis used to send 1462fe8fb19SBen Gras.Dq out-of-band 1472fe8fb19SBen Grasdata on sockets that support this notion (e.g. 1482fe8fb19SBen Gras.Dv SOCK_STREAM ) ; 1492fe8fb19SBen Grasthe underlying protocol must also support 1502fe8fb19SBen Gras.Dq out-of-band 1512fe8fb19SBen Grasdata. 1522fe8fb19SBen Gras.Dv MSG_EOR 1532fe8fb19SBen Grasis used to indicate a record mark for protocols which support the 1542fe8fb19SBen Grasconcept. 1552fe8fb19SBen Gras.\" .Dv MSG_EOF 1562fe8fb19SBen Gras.\" requests that the sender side of a socket be shut down, and that an 1572fe8fb19SBen Gras.\" appropriate indication be sent at the end of the specified data; 1582fe8fb19SBen Gras.\" this flag is only implemented for 1592fe8fb19SBen Gras.\" .Dv SOCK_STREAM 1602fe8fb19SBen Gras.\" sockets in the 1612fe8fb19SBen Gras.\" .Dv PF_INET 1622fe8fb19SBen Gras.\" protocol family, and is used to implement Transaction 1632fe8fb19SBen Gras.\" .Tn TCP 1642fe8fb19SBen Gras.\" (see 1652fe8fb19SBen Gras.\" .Xr ttcp 4 ) . 1662fe8fb19SBen Gras.Dv MSG_DONTROUTE 1672fe8fb19SBen Grasis usually used only by diagnostic or routing programs. 1682fe8fb19SBen Gras.Pp 1692fe8fb19SBen GrasSee 1702fe8fb19SBen Gras.Xr recv 2 1712fe8fb19SBen Grasfor a description of the 1722fe8fb19SBen Gras.Fa msghdr 1732fe8fb19SBen Grasstructure. 1742fe8fb19SBen Gras.Dv MSG_NOSIGNAL 1752fe8fb19SBen Grasis used to prevent 1762fe8fb19SBen Gras.Dv SIGPIPE 1772fe8fb19SBen Grasgeneration when writing a socket that 1782fe8fb19SBen Grasmay be closed. 1792fe8fb19SBen Gras.Sh RETURN VALUES 180f14fb602SLionel SambucThe 1812fe8fb19SBen Gras.Fn send , 1822fe8fb19SBen Gras.Fn sendto , 1832fe8fb19SBen Grasand 1842fe8fb19SBen Gras.Fn sendmsg 185f14fb602SLionel Sambuccalls return the number of characters sent, or \-1 186f14fb602SLionel Sambucif an error occurred. 187f14fb602SLionel SambucThe 188f14fb602SLionel Sambuc.Fn sendmmsg 189f14fb602SLionel Sambuccall returns the number of messages sent, or \-1 190f14fb602SLionel Sambucif an error occured. 191f14fb602SLionel Sambuc.Sh ERRORS 192f14fb602SLionel Sambuc.Fn send , 193f14fb602SLionel Sambuc.Fn sendto , 194f14fb602SLionel Sambuc.Fn sendmsg , 195f14fb602SLionel Sambucand 196f14fb602SLionel Sambuc.Fn sendmmsg 1972fe8fb19SBen Grasfail if: 1982fe8fb19SBen Gras.Bl -tag -width Er 199f14fb602SLionel Sambuc.It Bq Er EACCES 200f14fb602SLionel SambucThe SO_BROADCAST option is not set on the socket, and a broadcast address 201f14fb602SLionel Sambucwas given as the destination. 202f14fb602SLionel Sambuc.It Bq Er EAFNOSUPPORT 203f14fb602SLionel SambucAddresses in the specified address family cannot be used with this socket. 204f14fb602SLionel Sambuc.It Bq Er EAGAIN|EWOULDBLOCK 205f14fb602SLionel SambucThe socket is marked non-blocking and the requested operation 206f14fb602SLionel Sambucwould block. 2072fe8fb19SBen Gras.It Bq Er EBADF 2082fe8fb19SBen GrasAn invalid descriptor was specified. 209f14fb602SLionel Sambuc.It Bq Er EDSTADDRREQ 210f14fb602SLionel SambucIn a non-connected socket a destination address has not been specified. 211f14fb602SLionel Sambuc.It Bq Er EFAULT 212f14fb602SLionel SambucAn invalid user space address was specified for a parameter. 213f14fb602SLionel Sambuc.It Bq Er EHOSTDOWN 214f14fb602SLionel SambucThe destination is a host on the local subnet and does not respond to 215f14fb602SLionel Sambuc.Xr arp 4 . 216f14fb602SLionel Sambuc.It Bq Er EHOSTUNREACH 217f14fb602SLionel SambucThe destination for the message is unreachable. 218f14fb602SLionel Sambuc.It Bq Er EINVAL 219f14fb602SLionel SambucThe total length of the I/O is more than can be expressed by the ssize_t 220f14fb602SLionel Sambucreturn value. 221f14fb602SLionel Sambuc.It Bq Er EMSGSIZE 222f14fb602SLionel SambucThe socket requires that message be sent atomically, 223f14fb602SLionel Sambucand the size of the message to be sent made this impossible. 224f14fb602SLionel Sambuc.It Bq Er ENOBUFS 225f14fb602SLionel SambucThe system was unable to allocate an internal buffer. 226f14fb602SLionel SambucThe operation may succeed when buffers become available. 227f14fb602SLionel Sambuc.Pp 228f14fb602SLionel SambucAn alternative reason: the output queue for a network interface was full. 229f14fb602SLionel SambucThis generally indicates that the interface has stopped sending, 230f14fb602SLionel Sambucbut may be caused by transient congestion. 2312fe8fb19SBen Gras.It Bq Er ENOTSOCK 2322fe8fb19SBen GrasThe argument 2332fe8fb19SBen Gras.Fa s 2342fe8fb19SBen Grasis not a socket. 2352fe8fb19SBen Gras.It Bq Er EPIPE 2362fe8fb19SBen GrasIn a connected socket the connection has been broken. 2372fe8fb19SBen Gras.El 2382fe8fb19SBen Gras.Pp 2392fe8fb19SBen Gras.Fn sendto 2402fe8fb19SBen Graswill also fail if: 2412fe8fb19SBen Gras.Bl -tag -width Er 2422fe8fb19SBen Gras.It Bq Er EISCONN 2432fe8fb19SBen GrasA destination address was specified and the socket is already connected. 2442fe8fb19SBen Gras.El 2452fe8fb19SBen Gras.Pp 2462fe8fb19SBen Gras.Fn sendmsg 247f14fb602SLionel Sambucand 248f14fb602SLionel Sambuc.Fn sendmmsg 2492fe8fb19SBen Graswill also fail if: 2502fe8fb19SBen Gras.Bl -tag -width Er 2512fe8fb19SBen Gras.It Bq Er EMSGSIZE 2522fe8fb19SBen GrasThe 2532fe8fb19SBen Gras.Fa msg_iovlen 2542fe8fb19SBen Grasmember of the 2552fe8fb19SBen Gras.Fa msg 2562fe8fb19SBen Grasstructure is less than or equal to 0 2572fe8fb19SBen Grasor is greater than 258*84d9c625SLionel Sambuc.Brq Dv IOV_MAX . 2592fe8fb19SBen Gras.El 2602fe8fb19SBen Gras.Sh SEE ALSO 2612fe8fb19SBen Gras.Xr fcntl 2 , 2622fe8fb19SBen Gras.Xr getsockopt 2 , 2632fe8fb19SBen Gras.Xr recv 2 , 2642fe8fb19SBen Gras.Xr select 2 , 2652fe8fb19SBen Gras.Xr socket 2 , 2662fe8fb19SBen Gras.Xr write 2 2672fe8fb19SBen Gras.Sh HISTORY 2682fe8fb19SBen GrasThe 2692fe8fb19SBen Gras.Fn send 2702fe8fb19SBen Grasfunction call appeared in 2712fe8fb19SBen Gras.Bx 4.2 . 272f14fb602SLionel SambucThe 273f14fb602SLionel Sambuc.Fn sendmmsg 274f14fb602SLionel Sambucfunction call appeared in 275f14fb602SLionel Sambuc.Tn Linux 3.0 276f14fb602SLionel Sambucand 277f14fb602SLionel Sambuc.Nx 7.0 . 278