|
NAMEsocket —
kernel socket interface
SYNOPSIS#include <sys/socket.h>
#include <sys/socketvar.h>
void
int
int
int
void
int
int
int
void
struct sockaddr *
void
void
int
void
int
struct socket *
int
int
int
int
int
int
int
void
int
int
int
int
void
void
void
void
int
int
int
int
DESCRIPTIONThe kernelsocket programming interface permits
in-kernel consumers to interact with local and network socket objects in a
manner similar to that permitted using the
socket(2)
user API. These interfaces are appropriate for use by distributed file systems
and other network-aware kernel services. While the user API operates on file
descriptors, the kernel interfaces operate directly on struct
socket pointers. Some portions of the kernel API exist only to implement
the user API, and are not expected to be used by kernel code. The portions of
the socket API used by socket consumers and implementations of network
protocols will differ; some routines are only useful for protocol
implementors.
Except where otherwise indicated, Creating and Destroying SocketsA new socket may be created usingsocreate (). As with
socket(2),
arguments specify the requested domain, type, and protocol via
dom, type, and
proto. The socket is returned via
aso on success. In addition, the credential used to
authorize operations associated with the socket will be passed via
cred (and will be cached for the lifetime of the
socket), and the thread performing the operation via td.
Warning: authorization of the socket creation operation will
be performed using the thread credential for some protocols (such as raw
sockets).
Sockets may be closed and freed using
In certain circumstances, it is appropriate to destroy a socket
without waiting for it to disconnect, for which
Connections and AddressesThesobind () function is equivalent to the
bind(2)
system call, and binds the socket so to the address
nam. The operation would be authorized using the
credential on thread td.
The A call to The Sockets are transitioned from non-listening status to listening
with Socket OptionsThesogetopt () function is equivalent to the
getsockopt(2)
system call, and retrieves a socket option on socket so.
The sosetopt () function is equivalent to the
setsockopt(2)
system call, and sets a socket option on socket so.
The second argument in both
Socket UpcallsIn order for the owner of a socket to be notified when the socket is ready to send or receive data, an upcall may be registered on the socket. The upcall is a function that will be called by the socket framework when a socket buffer associated with the given socket is ready for reading or writing.soupcall_set () is used to register a socket upcall.
The function func is registered, and the pointer
arg will be passed as its second argument when it is
called by the framework. The possible values for which
are SO_RCV and SO_SND , which
register upcalls for receive and send events, respectively. The upcall
function func () must return either
SU_OK or SU_ISCONNECTED ,
depending on whether or not a call to
soisconnected
should be made by the socket framework after the upcall returns. The upcall
func cannot call
soisconnected
itself due to lock ordering with the socket buffer lock. Only
SO_RCV upcalls should return
SU_ISCONNECTED . When a SO_RCV
upcall returns SU_ISCONNECTED , the upcall will be
removed from the socket.
Upcalls are removed from their socket by
Socket Destructor CallbackA kernel system can use thesodtor_set () function to set
a destructor for a socket. The destructor is called when the socket is about
to be freed. The destructor is called before the protocol detach routine. The
destructor can serve as a callback to initiate additional cleanup actions.
Socket I/OThesoreceive () function is equivalent to the
recvmsg(2)
system call, and attempts to receive bytes of data from the socket
so, optionally blocking awaiting for data if none is
ready to read. Data may be retrieved directly to kernel or user memory via the
uio argument, or as an mbuf chain returned to the caller
via mp0, avoiding a data copy. The
uio must always be non-NULL . If
mp0 is non-NULL , only the
uio_resid of uio is used. The
caller may optionally retrieve a socket address on a protocol with the
PR_ADDR capability by providing storage via
non-NULL psa argument. The
caller may optionally retrieve control data mbufs via a
non-NULL controlp argument.
Optional flags may be passed to soreceive () via a
non-NULL flagsp argument, and
use the same flag name space as the
recvmsg(2)
system call.
The Kernel callers running in
ithread(9)
context, or with a mutex held, will wish to use non-blocking sockets and
pass the A socket can be queried for readability, writability, out-of-band
data, or end-of-file using Calls to Socket Utility FunctionsThe uid of a socket's credential may be compared against a uid withsocheckuid ().
A copy of an existing struct sockaddr may be
made using Protocol implementations notify the socket layer of the arrival of
out-of-band data using An “external-format” version of a
struct socket can be created using
Protocol ImplementationsProtocols must supply an implementation forsolisten ();
such protocol implementations can call back into the socket layer using
solisten_proto_check () and
solisten_proto () to check and set the socket-layer
listen state. These callbacks are provided so that the protocol implementation
can order the socket layer and protocol locks as necessary. Protocols must
supply an implementation of soreceive (); the functions
soreceive_stream (),
soreceive_dgram (), and
soreceive_generic () are supplied for use by such
implementations.
Protocol implementations can use
Protocols must supply an implementation for
The functions When a protocol creates a new socket structure, it is necessary to
reserve socket buffer space for that socket, by calling
When a protocol needs to wake up threads waiting for the socket to
become ready to read or write, variants of
The functions SEE ALSObind(2), close(2), connect(2), getsockopt(2), recv(2), send(2), setsockopt(2), shutdown(2), socket(2), ng_ksocket(4), ithread(9), msleep(9), ucred(9)HISTORYThe socket(2) system call appeared in 4.2BSD. This manual page was introduced in FreeBSD 7.0.AUTHORSThis manual page was written by Robert Watson andBenjamin Kaduk. BUGSThe use of explicitly passed credentials, credentials hung from explicitly passed threads, the credential oncurthread , and the
cached credential from socket creation time is inconsistent, and may lead to
unexpected behaviour. It is possible that several of the
td arguments should be cred
arguments, or simply not be present at all.
The caller may need to manually clear
The This manual page does not describe how to register socket upcalls or monitor a socket for readability/writability without using blocking I/O. The
Visit the GSP FreeBSD Man Page Interface. |