1.\" $OpenBSD: socreate.9,v 1.2 2009/01/29 17:57:18 jmc 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: January 29 2009 $ 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 sosetopt , 39.Nm sogetopt , 40.Nm sosend , 41.Nm soshutdown 42.Nd kernel socket interface 43.Sh SYNOPSIS 44.In sys/socket.h 45.In sys/socketvar.h 46.Ft int 47.Fn sobind "struct socket *so" "struct mbuf *nam" "struct proc *p" 48.Ft void 49.Fn soclose "struct socket *so" 50.Ft int 51.Fn soconnect "struct socket *so" "struct mbuf *nam" 52.Ft int 53.Fo socreate 54.Fa "int dom" "struct socket **aso" "int type" "int proto" 55.Fc 56.Ft int 57.Fo soreceive 58.Fa "struct socket *so" "struct mbuf **paddr" "struct uio *uio" 59.Fa "struct mbuf **mp0" "struct mbuf **controlp" "int *flagsp" 60.Fc 61.Ft int 62.Fn sosetopt "struct socket *so" "int level" "int optname" "struct mbuf *m0" 63.Ft int 64.Fn sogetopt "struct socket *so" "int level" "int optname" "struct mbuf **mp" 65.Ft int 66.Fo sosend 67.Fa "struct socket *so" "struct mbuf *addr" "struct uio *uio" 68.Fa "struct mbuf *top" "struct mbuf *control" "int flags" 69.Fc 70.Ft int 71.Fn soshutdown "struct socket *so" "int how" 72.Sh DESCRIPTION 73The kernel socket 74programming interface permits in-kernel consumers to interact with 75local and network socket objects in a manner similar to that permitted using 76the 77.Xr socket 2 78user API. 79These interfaces are appropriate for use by distributed file systems and 80other network-aware kernel services. 81While the user API operates on file descriptors, the kernel interfaces 82operate directly on 83.Vt "struct socket" 84pointers. 85.Pp 86Except where otherwise indicated, 87.Nm 88functions may sleep. 89.Ss Creating and Destroying Sockets 90A new socket may be created using 91.Fn socreate . 92As with 93.Xr socket 2 , 94arguments specify the requested domain, type, and protocol via 95.Fa dom , type , 96and 97.Fa proto . 98The socket is returned via 99.Fa aso 100on success. 101.Em Warning : 102authorization of the socket creation operation will be performed 103using 104.Dv curproc 105for some protocols (such as raw sockets). 106.Pp 107Sockets may be closed and freed using 108.Fn soclose , 109which has similar semantics to 110.Xr close 2 . 111.Ss Connections and Addresses 112The 113.Fn sobind 114function is equivalent to the 115.Xr bind 2 116system call, and binds the socket 117.Fa so 118to the address 119.Fa nam . 120The operation would be authorized using the credential on process 121.Fa p . 122.Pp 123The 124.Fn soconnect 125function is equivalent to the 126.Xr connect 2 127system call, and initiates a connection on the socket 128.Fa so 129to the address 130.Fa nam . 131The operation will be authorized using the credential on 132.Dv curproc . 133Unlike the user system call, 134.Fn soconnect 135returns immediately; the caller may 136.Xr tsleep 9 137on 138.Fa so-\*(Gtso_timeo 139and wait for the 140.Dv SS_ISCONNECTING 141flag to clear or 142.Fa so-\*(Gtso_error 143to become non-zero. 144If 145.Fn soconnect 146fails, the caller must manually clear the 147.Dv SS_ISCONNECTING 148flag. 149.Pp 150The 151.Fn soshutdown 152function is equivalent to the 153.Xr shutdown 2 154system call, and causes part or all of a connection on a socket to be closed 155down. 156.Ss Socket Options 157The 158.Fn sogetopt 159function is equivalent to the 160.Xr getsockopt 2 161system call, and retrieves a socket option on socket 162.Fa so . 163The 164.Fn sosetopt 165function is equivalent to the 166.Xr setsockopt 2 167system call, and sets a socket option on socket 168.Fa so . 169.Pp 170The next two arguments in both 171.Fn sogetopt 172and 173.Fn sosetopt 174are 175.Fa level 176and 177.Fa optname 178describing the protocol level and socket option. 179The last argument is either a pointer to a prefilled mbuf 180.Fa m0 181or a pointer to a mbuf pointer 182.Fa mp 183which will point to the retrieved data. 184.Ss Socket I/O 185The 186.Fn soreceive 187function is equivalent to the 188.Xr recvmsg 2 189system call, and attempts to receive bytes of data from the socket 190.Fa so , 191optionally blocking and awaiting data if none is ready to read. 192Data may be retrieved directly to kernel or user memory via the 193.Fa uio 194argument, or as an mbuf chain returned to the caller via 195.Fa mp0 , 196avoiding a data copy. 197Only one of the 198.Fa uio 199or 200.Fa mp0 201pointers may be 202.Pf non- Dv NULL . 203The caller may optionally retrieve a socket address on a protocol with the 204.Dv PR_ADDR 205capability by providing storage via a 206.Pf non- Dv NULL 207.Fa paddr 208argument. 209The caller may optionally retrieve control data mbufs via a 210.Pf non- Dv NULL 211.Fa controlp 212argument. 213Optional flags may be passed to 214.Fn soreceive 215via a 216.Pf non- Dv NULL 217.Fa flagsp 218argument, and use the same flag name space as the 219.Xr recvmsg 2 220system call. 221.Pp 222The 223.Fn sosend 224function is equivalent to the 225.Xr sendmsg 2 226system call, and attempts to send bytes of data via the socket 227.Fa so , 228optionally blocking if data cannot be immediately sent. 229Data may be sent directly from kernel or user memory via the 230.Fa uio 231argument, or as an mbuf chain via 232.Fa top , 233avoiding a data copy. 234Only one of the 235.Fa uio 236or 237.Fa top 238pointers may be 239.Pf non- Dv NULL . 240An optional destination address may be specified via a 241.Pf non- Dv NULL 242.Fa addr 243argument, which may result in an implicit connect if supported by the 244protocol. 245The caller may optionally send control data mbufs via a 246.Pf non- Dv NULL 247.Fa control 248argument. 249Flags may be passed to 250.Fn sosend 251using the 252.Fa flags 253argument, and use the same flag name space as the 254.Xr sendmsg 2 255system call. 256.Pp 257Kernel callers running in interrupt context, or with a mutex held, will wish to 258use non-blocking sockets and pass the 259.Dv MSG_DONTWAIT 260flag in order to prevent these functions from sleeping. 261.Sh SEE ALSO 262.Xr bind 2 , 263.Xr close 2 , 264.Xr connect 2 , 265.Xr getsockopt 2 , 266.Xr recv 2 , 267.Xr send 2 , 268.Xr setsockopt 2 , 269.Xr shutdown 2 , 270.Xr socket 2 , 271.Xr tsleep 9 272.Sh HISTORY 273The 274.Xr socket 2 275system call appeared in 276.Bx 4.2 . 277This manual page was introduced in 278.Fx 7.0 279and ported to 280.Ox 4.5 . 281.Sh AUTHORS 282This manual page was written by 283.An Robert Watson . 284.Sh BUGS 285The use of credentials hung from explicitly passed processes, 286and the credential on 287.Dv curproc , 288and the cached credential from socket creation time is inconsistent, and may 289lead to unexpected behaviour. 290.Pp 291The caller may need to manually clear 292.Dv SS_ISCONNECTING 293if 294.Fn soconnect 295returns an error. 296.Pp 297The 298.Dv MSG_DONTWAIT 299flag is not implemented for 300.Fn sosend . 301.Pp 302This manual page does not describe how to register socket upcalls or monitor 303a socket for readability/writability without using blocking I/O. 304