1.\" $OpenBSD: socreate.9,v 1.6 2016/07/27 13:02:50 phessler Exp $ 2.\" 3.\" Copyright (c) 2006 Robert N. M. Watson 4.\" All rights reserved. 5.\" 6.\" Redistribution and use in source and binary forms, with or without 7.\" modification, are permitted provided that the following conditions 8.\" are met: 9.\" 1. Redistributions of source code must retain the above copyright 10.\" notice, this list of conditions and the following disclaimer. 11.\" 2. Redistributions in binary form must reproduce the above copyright 12.\" notice, this list of conditions and the following disclaimer in the 13.\" documentation and/or other materials provided with the distribution. 14.\" 15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25.\" SUCH DAMAGE. 26.\" 27.\" $FreeBSD: src/share/man/man9/socket.9,v 1.2 2006/12/16 10:32:10 rwatson Exp $ 28.\" 29.Dd $Mdocdate: July 27 2016 $ 30.Dt SOCREATE 9 31.Os 32.Sh NAME 33.Nm sobind , 34.Nm soclose , 35.Nm soconnect , 36.Nm socreate , 37.Nm soreceive , 38.Nm so_upcall , 39.Nm sosetopt , 40.Nm sogetopt , 41.Nm sosend , 42.Nm soshutdown 43.Nd kernel socket interface 44.Sh SYNOPSIS 45.In sys/socket.h 46.In sys/socketvar.h 47.Ft int 48.Fn sobind "struct socket *so" "struct mbuf *nam" "struct proc *p" 49.Ft void 50.Fn soclose "struct socket *so" 51.Ft int 52.Fn soconnect "struct socket *so" "struct mbuf *nam" 53.Ft int 54.Fo socreate 55.Fa "int dom" "struct socket **aso" "int type" "int proto" 56.Fc 57.Ft int 58.Fo soreceive 59.Fa "struct socket *so" "struct mbuf **paddr" "struct uio *uio" 60.Fa "struct mbuf **mp0" "struct mbuf **controlp" "int *flagsp" 61.Fa "socklen_t controllen" 62.Fc 63.Ft void 64.Fn (*so_upcall) "struct socket *so" "caddr_t arg" "int waitflag" 65.Ft int 66.Fn sosetopt "struct socket *so" "int level" "int optname" "struct mbuf *m0" 67.Ft int 68.Fn sogetopt "struct socket *so" "int level" "int optname" "struct mbuf **mp" 69.Ft int 70.Fo sosend 71.Fa "struct socket *so" "struct mbuf *addr" "struct uio *uio" 72.Fa "struct mbuf *top" "struct mbuf *control" "int flags" 73.Fc 74.Ft int 75.Fn soshutdown "struct socket *so" "int how" 76.Sh DESCRIPTION 77The kernel socket 78programming interface permits in-kernel consumers to interact with 79local and network socket objects in a manner similar to that permitted using 80the 81.Xr socket 2 82user API. 83These interfaces are appropriate for use by distributed file systems and 84other network-aware kernel services. 85While the user API operates on file descriptors, the kernel interfaces 86operate directly on 87.Vt "struct socket" 88pointers. 89.Pp 90Except where otherwise indicated, 91.Nm 92functions may sleep. 93.Ss Creating and Destroying Sockets 94A new socket may be created using 95.Fn socreate . 96As with 97.Xr socket 2 , 98arguments specify the requested domain, type, and protocol via 99.Fa dom , type , 100and 101.Fa proto . 102The socket is returned via 103.Fa aso 104on success. 105.Em Warning : 106authorization of the socket creation operation will be performed 107using 108.Dv curproc 109for some protocols (such as raw sockets). 110.Pp 111Sockets may be closed and freed using 112.Fn soclose , 113which has similar semantics to 114.Xr close 2 . 115.Ss Connections and Addresses 116The 117.Fn sobind 118function is equivalent to the 119.Xr bind 2 120system call, and binds the socket 121.Fa so 122to the address 123.Fa nam . 124The operation would be authorized using the credential on process 125.Fa p . 126.Pp 127The 128.Fn soconnect 129function is equivalent to the 130.Xr connect 2 131system call, and initiates a connection on the socket 132.Fa so 133to the address 134.Fa nam . 135The operation will be authorized using the credential on 136.Dv curproc . 137Unlike the user system call, 138.Fn soconnect 139returns immediately; the caller may 140.Xr tsleep 9 141on 142.Fa so->so_timeo 143and wait for the 144.Dv SS_ISCONNECTING 145flag to clear or 146.Fa so->so_error 147to become non-zero. 148If 149.Fn soconnect 150fails, the caller must manually clear the 151.Dv SS_ISCONNECTING 152flag. 153.Pp 154The 155.Fn soshutdown 156function is equivalent to the 157.Xr shutdown 2 158system call, and causes part or all of a connection on a socket to be closed 159down. 160.Ss Socket Options 161The 162.Fn sogetopt 163function is equivalent to the 164.Xr getsockopt 2 165system call, and retrieves a socket option on socket 166.Fa so . 167The 168.Fn sosetopt 169function is equivalent to the 170.Xr setsockopt 2 171system call, and sets a socket option on socket 172.Fa so . 173.Pp 174The next two arguments in both 175.Fn sogetopt 176and 177.Fn sosetopt 178are 179.Fa level 180and 181.Fa optname 182describing the protocol level and socket option. 183The last argument is either a pointer to a prefilled mbuf 184.Fa m0 185or a pointer to a mbuf pointer 186.Fa mp 187which will point to the retrieved data. 188.Ss Socket I/O 189The 190.Fn soreceive 191function is equivalent to the 192.Xr recvmsg 2 193system call, and attempts to receive bytes of data from the socket 194.Fa so , 195optionally blocking and awaiting data if none is ready to read. 196Data may be retrieved directly to kernel or user memory via the 197.Fa uio 198argument, or as an mbuf chain returned to the caller via 199.Fa mp0 , 200avoiding a data copy. 201If 202.Fa mp0 203is not 204.Dv NULL , 205.Fa uio 206must still be passed with uio_resid set to specify the maximum 207amount of data to be returned to the caller via an mbuf chain. 208The caller may optionally retrieve a socket address on a protocol with the 209.Dv PR_ADDR 210capability by providing storage via a 211.Pf non- Dv NULL 212.Fa paddr 213argument. 214The caller may optionally retrieve up to 215.Fa controllen 216bytes of control data in mbufs via a 217.Pf non- Dv NULL 218.Fa controlp 219argument. 220Optional flags may be passed to 221.Fn soreceive 222via a 223.Pf non- Dv NULL 224.Fa flagsp 225argument, and use the same flag name space as the 226.Xr recvmsg 2 227system call. 228.Pp 229When the 230.Fn so_upcall 231function pointer is not NULL, it is called when 232.Xr soreceive 9 233matches an incoming connection. 234.Pp 235The 236.Fn sosend 237function is equivalent to the 238.Xr sendmsg 2 239system call, and attempts to send bytes of data via the socket 240.Fa so , 241optionally blocking if data cannot be immediately sent. 242Data may be sent directly from kernel or user memory via the 243.Fa uio 244argument, or as an mbuf chain via 245.Fa top , 246avoiding a data copy. 247Only one of the 248.Fa uio 249or 250.Fa top 251pointers may be 252.Pf non- Dv NULL . 253An optional destination address may be specified via a 254.Pf non- Dv NULL 255.Fa addr 256argument, which may result in an implicit connect if supported by the 257protocol. 258The caller may optionally send control data mbufs via a 259.Pf non- Dv NULL 260.Fa control 261argument. 262Flags may be passed to 263.Fn sosend 264using the 265.Fa flags 266argument, and use the same flag name space as the 267.Xr sendmsg 2 268system call. 269.Pp 270Kernel callers running in interrupt context, or with a mutex held, will wish to 271use non-blocking sockets and pass the 272.Dv MSG_DONTWAIT 273flag in order to prevent these functions from sleeping. 274.Sh SEE ALSO 275.Xr bind 2 , 276.Xr close 2 , 277.Xr connect 2 , 278.Xr getsockopt 2 , 279.Xr recv 2 , 280.Xr send 2 , 281.Xr setsockopt 2 , 282.Xr shutdown 2 , 283.Xr socket 2 , 284.Xr tsleep 9 285.Sh HISTORY 286The 287.Xr socket 2 288system call appeared in 289.Bx 4.2 . 290This manual page was introduced in 291.Fx 7.0 292and ported to 293.Ox 4.5 . 294.Sh AUTHORS 295This manual page was written by 296.An Robert Watson . 297.Sh BUGS 298The use of credentials hung from explicitly passed processes, 299and the credential on 300.Dv curproc , 301and the cached credential from socket creation time is inconsistent, and may 302lead to unexpected behaviour. 303.Pp 304The caller may need to manually clear 305.Dv SS_ISCONNECTING 306if 307.Fn soconnect 308returns an error. 309.Pp 310The 311.Dv MSG_DONTWAIT 312flag is not implemented for 313.Fn sosend . 314.Pp 315This manual page does not describe how to register socket upcalls or monitor 316a socket for readability/writability without using blocking I/O. 317