Add class SOCKET-RAW-NETLINK
[iolib.git] / src / sockets / base-sockets.lisp
blob41ecf855fa543623476339fbed4887e059af4332
1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; --- Base socket classes.
4 ;;;
6 (in-package :iolib.sockets)
8 ;;;; Sockets
10 (defclass socket (dual-channel-fd-mixin)
11 ((address-family :initarg :address-family :accessor socket-address-family)
12 (protocol :initarg :protocol :accessor socket-protocol)
13 (bound :initform nil :reader socket-bound-p :type boolean))
14 (:documentation "Base class for sockets."))
15 (unset-method-docstring #'socket-address-family () '(socket))
16 (set-function-docstring 'socket-address-family "Return the address family of a socket.")
17 (unset-method-docstring #'socket-protocol () '(socket))
18 (set-function-docstring 'socket-protocol "Return the protocol of a socket.")
20 (defgeneric make-socket (&key address-family type connect ipv6 external-format
21 &allow-other-keys)
22 (:documentation "Create an instance of a subclass of SOCKET. ADDRESS-FAMILY, TYPE, CONNECT and IPV6
23 are used to specify the kind of socket to create.
25 * ADDRESS-FAMILY - :INTERNET or :LOCAL (or :FILE as synonim)
26 * TYPE - :STREAM, :DATAGRAM or :RAW
27 * CONNECT - :ACTIVE or :PASSIVE
28 * IPV6 - if NIL, create an IPv4 socket, otherwise an IPv6 socket.
30 To initialize the socket, the following keyword arguments can be used depending on ADDRESS-FAMILY, TYPE and CONNECT:
31 * :local-host - a hostname designator or NIL. If non-null the socket will be bound to this address
32 * :local-port - a port designator or NIL. If LOCAL-HOST is non-null, bind the socket to this port. If NIL, choose a random port
33 * :remote-host - a hostname designator or NIL. If non-null the socket will be connected to this address
34 * :remote-port - a port designator. If REMOTE-HOST is non-null, connect the socket to this port
35 * :local-filename - a string or NIL. If non-null the socket will be bound to this file
36 * :remote-filename - a string or NIL. If non-null the socket will be connected to this file
37 * :backlog - a positive integer or NIL. Specifies the length of the incomming connection queue and can't be larger than +MAX-BACKLOG-SIZE+. If NIL, default is *DEFAULT-BACKLOG-SIZE*
38 * :reuse-address: a boolean(default T). set option SO_REUSEADDR if LOCAL-HOST is non-null
39 * :keepalive - a boolean. set option SO_KEEPALIVE
40 * :nodelay - a boolean. set option SO_NODELAY
41 * :interface - a string. set option SO_BINDTODEVICE to this interface
42 * :input-buffer-size - a positive integer. Create the stream input buffer of this size
43 * :output-buffer-size - a positive integer. Create the stream output buffer of this size
45 Glossary:
46 * hostname designator: an instance of INET-ADDRESS or any object accepted by LOOKUP-HOST. IPV6 is passed to LOOKUP-HOST as is
47 * port designator: any object accepted by LOOKUP-SERVICE
49 :address-family :INTERNET :type :STREAM :connect :ACTIVE
50 * Valid keyword args: :LOCAL-HOST, :LOCAL-PORT, :REMOTE-HOST, :REMOTE-PORT, :REUSE-ADDRESS, :KEEPALIVE, :NODELAY, :INPUT-BUFFER-SIZE and :OUTPUT-BUFFER-SIZE
52 :address-family :INTERNET :type :STREAM :connect :PASSIVE
53 * Valid keyword args: :LOCAL-HOST, :LOCAL-PORT, :BACKLOG, :REUSE-ADDRESS, :INTERFACE and :NODELAY
55 :address-family :INTERNET :type :STREAM :connect :ACTIVE
56 * Valid keyword args: :LOCAL-FILENAME, :REMOTE-FILENAME, :INPUT-BUFFER-SIZE and :OUTPUT-BUFFER-SIZE
58 :address-family :INTERNET :type :STREAM :connect :PASSIVE
59 * Valid keyword args: :LOCAL-FILENAME, :REMOTE-FILENAME, :BACKLOG and :REUSE-ADDRESS
61 :address-family :INTERNET :type :DATAGRAM
62 * Valid keyword args: :LOCAL-HOST, :LOCAL-PORT, :REMOTE-HOST, :REMOTE-PORT, :REUSE-ADDRESS, :INTERFACE and :BROADCAST
64 :address-family :LOCAL :type :DATAGRAM
65 * Valid keyword args: :LOCAL-FILENAME and :REMOTE-FILENAME
67 :address-family :INTERNET :type :RAW
68 * Valid keyword args: :INCLUDE-HEADERS"))
70 (defgeneric make-socket-from-fd (fd &key dup connect external-format
71 input-buffer-size output-buffer-size)
72 (:documentation "Create a socket instance of the appropriate subclass of SOCKET using FD.
73 The connection type of the socket must be specified - :ACTIVE or :PASSIVE.
74 The address family and type of the socket are automatically discovered using OS functions. Buffer sizes
75 for the new socket can also be specified using INPUT-BUFFER-SIZE and OUTPUT-BUFFER-SIZE."))
77 (defgeneric make-socket-pair (&key type protocol external-format
78 input-buffer-size output-buffer-size)
79 (:documentation "Create a pair of sockets connected to each other.
80 The socket type must be either :STREAM or :DATAGRAM. Currently OSes can only create :LOCAL sockets this way.
81 Buffer sizes for the new sockets can also be specified using INPUT-BUFFER-SIZE and OUTPUT-BUFFER-SIZE."))
83 (defgeneric socket-os-fd (socket)
84 (:documentation "Returns the OS file descriptor of SOCKET."))
86 (defgeneric socket-type (socket)
87 (:documentation "Returns the socket type of SOCKET - :STREAM or :DATAGRAM."))
89 (defgeneric socket-open-p (socket)
90 (:documentation "Returns a boolean indicating whether or not the file descriptor of SOCKET is open."))
92 (defgeneric local-name (socket)
93 (:documentation "For INTERNET sockets, returns two values: the local host and the local port.
94 For LOCAL sockets, returns the local filename."))
96 (defgeneric local-host (socket)
97 (:documentation "Returns the local host of SOCKET.
98 Works only on INTERNET sockets."))
100 (defgeneric local-port (socket)
101 (:documentation "Returns the local port of SOCKET - an (UNSIGNED-BYTE 16).
102 Works only on INTERNET sockets."))
104 (defgeneric local-filename (socket)
105 (:documentation "Returns the local filename of SOCKET.
106 Works only on LOCAL sockets."))
108 (defgeneric remote-name (socket)
109 (:documentation "For INTERNET sockets, returns two values: the remote host and the remote port.
110 For REMOTE sockets, returns the remote filename."))
112 (defgeneric remote-host (socket)
113 (:documentation "Returns the remote host of SOCKET.
114 Works only on INTERNET sockets."))
116 (defgeneric remote-port (socket)
117 (:documentation "Returns the remote port of SOCKET - an (UNSIGNED-BYTE 16).
118 Works only on INTERNET sockets."))
120 (defgeneric remote-filename (socket)
121 (:documentation "Returns the remote filename of SOCKET.
122 Works only on LOCAL sockets."))
124 (defgeneric socket-option (socket option-name)
125 (:documentation "Returns the value(s) of OS option OPTION-NAME on SOCKET.
126 For a complete list of supported options see src/sockets/socket-options.lisp."))
128 (defclass stream-socket (socket) ()
129 (:default-initargs :type :stream)
130 (:documentation "Mixin for sockets of type SOCK_STREAM."))
132 (defclass datagram-socket (socket) ()
133 (:default-initargs :type :datagram)
134 (:documentation "Mixin for sockets of type SOCK_DGRAM."))
136 (defclass raw-socket (socket) ()
137 (:default-initargs :type :raw)
138 (:documentation "Mixin for sockets of type SOCK_RAW."))
140 (defgeneric disconnect (socket)
141 (:documentation "Disassociates SOCKET from any remote address.
142 Works only on DATAGRAM sockets."))
144 (define-symbol-macro +default-inet-address-family+
145 (if *ipv6* :ipv6 :ipv4))
147 (defclass internet-socket (socket) ()
148 (:default-initargs :address-family +default-inet-address-family+)
149 (:documentation "Mixin for sockets of domain AF_INET or AF_INET6."))
151 (defclass local-socket (socket) ()
152 (:default-initargs :address-family :local)
153 (:documentation "Mixin for sockets of domain AF_LOCAL."))
155 (defclass netlink-socket (socket) ()
156 (:default-initargs :address-family :netlink)
157 (:documentation "Mixin for sockets of domain AF_NETLINK."))
159 (defgeneric send-file-descriptor (socket file-descriptor)
160 (:documentation "Send FILE-DESCRIPTOR through SOCKET.
161 The receiving process must use RECEIVE-FILE-DESCRIPTOR to receive the
162 file descriptor in order for it to be valid in the receiving process."))
164 (defgeneric receive-file-descriptor (socket)
165 (:documentation "Receive a file descriptor as ancillary data through SOCKET."))
167 (defun socket-read-fn (fd buffer nbytes)
168 (debug-only
169 (assert buffer)
170 (assert fd))
171 (%recvfrom fd buffer nbytes 0 (null-pointer) (null-pointer)))
173 (defun socket-write-fn (fd buffer nbytes)
174 (debug-only
175 (assert buffer)
176 (assert fd))
177 (%sendto fd buffer nbytes 0 (null-pointer) 0))
179 (defclass active-socket (socket dual-channel-gray-stream) ()
180 (:default-initargs :read-fn #'socket-read-fn
181 :write-fn #'socket-write-fn)
182 (:documentation "Mixin class for active(client) sockets."))
184 (defgeneric connect (socket address &key &allow-other-keys)
185 (:documentation "Connects SOCKET to ADDRESS. For INTERNET sockets you can specify
186 the port to connect to using keyword argument PORT. The default value of PORT is 0,
187 which usually means letting the OS choose a random port to connect to.
188 WAIT specifies how long to wait for a connection: NIL means \"return immediately\", a non-negative real
189 specifies a timeout in seconds and T means \"wait forever\"."))
191 (defgeneric socket-connected-p (socket)
192 (:documentation "Returns a boolean specifying whether or not SOCKET is connected."))
194 (defgeneric shutdown (socket &key read write)
195 (:documentation "Shut down all or part of a connection. If READ it non-NIL, further receptions are
196 disallowed; if WRITE is non-NIL, further transmissions are disallowed. CLOSE must still be called on
197 SOCKET in order to release OS resources."))
199 (defgeneric receive-from (socket &rest args &key &allow-other-keys)
200 (:documentation "Receives data from SOCKET. If BUFFER is specified
201 START and END are used as bounding index. In that case BUFFER must be
202 an array and its ARRAY-ELEMENT-TYPE be either (UNSIGNED-BYTE 8) or T.
203 If BUFFER is not specified an (UNSIGNED-BYTE 8) buffer of size SIZE
204 will be allocated.
206 Some flags can also be passed to recvfrom(2):
207 * :OUT-OF-BAND for receiving out-of-band data - only for STREAM sockets
208 * :PEEK for keeping the returned data in the kernel buffers
209 * :WAIT-ALL for waiting until the entire buffer can be filled
210 * :DONT-WAIT for making only the current call non-blocking
212 The first two values returned are the buffer and the number of elements that have been copied into the buffer.
213 For INTERNET DATAGRAM sockets, two additional values are returned: the host and port of the remote peer
214 from which the data was received.
215 For LOCAL DATAGRAM sockets, one additional values is returned: the filename of the remote peer
216 from which the data was received."))
218 (defgeneric send-to (socket buffer &rest args &key &allow-other-keys)
219 (:documentation "Send the contents of BUFFER to SOCKET.
220 BUFFER must be a vector that can be coerced to a (SIMPLE-ARRAY (UNSIGNED-BYTE 8) (*)).
221 START and END are used a bounding index on BUFFER.
222 For disconnected datagram sockets, REMOTE-HOST and REMOTE-PORT or REMOTE-FILENAME are used
223 as destination for the data.
225 Some flags can also be passed to sendto(2):
226 * :OUT-OF-BAND for receiving out-of-band data - only for stream sockets
227 * :DONT-WAIT for making only the current call non-blocking
228 * :DONT-ROUTE for sending only to hosts on directly connected networks, not using gateways
229 * :CONFIRM for signalling progress on the link layer - only available on Linux and only with DATAGRAM sockets
230 * :MORE for telling the kernel that there is more data to send - only available on Linux
232 Returns the number of bytes sent."))
234 (defclass passive-socket (socket)
235 ((listening :initform nil :reader socket-listening-p :type boolean)
236 (external-format :initarg :external-format :reader external-format-of)
237 (active-class :initarg :active-class :reader active-class
238 :type symbol :allocation :class))
239 (:default-initargs :external-format :default)
240 (:documentation "Mixin class for PASSIVE(server) sockets."))
242 (defgeneric bind-address (socket address &key &allow-other-keys)
243 (:documentation "Sets the local address of SOCKET to ADDRESS(and PORT for INTERNET sockets).
244 REUSE-ADDRESS sets the SO_REUSEADDR socket option on SOCKET."))
246 (defgeneric listen-on (socket &key &allow-other-keys)
247 (:documentation "Start allowing incoming connections on SOCKET.
248 BACKLOG specifies the maximum length of the queue of pending connections."))
250 (defgeneric accept-connection (passive-socket &key &allow-other-keys)
251 (:documentation "Extracts the first connection from the queue of pending connections on SOCKET.
252 WAIT specifies how long to wait for a connection: NIL means \"return immediately\", a non-negative real
253 specifies a timeout in seconds and T means \"wait forever\".
254 EXTERNAL-FORMAT optionally specifies the external format of the new socket - the default being
255 that of SOCKET. Buffer sizes for the new socket can also be specified using INPUT-BUFFER-SIZE
256 and OUTPUT-BUFFER-SIZE.
257 If a connection is received, returns two or three values: the newly created socket, the remote peer
258 address and the remote port if applicable."))
260 (defclass socket-stream-internet-active
261 (active-socket stream-socket internet-socket) ()
262 (:documentation "Class representing active sockets of type SOCK_STREAM and domain AF_INET or AF_INET6."))
264 (defclass socket-stream-internet-passive
265 (passive-socket stream-socket internet-socket) ()
266 (:default-initargs :active-class 'socket-stream-internet-active)
267 (:documentation "Class representing passive sockets of type SOCK_STREAM and domain AF_INET or AF_INET6."))
269 (defclass socket-stream-local-active
270 (active-socket stream-socket local-socket) ()
271 (:documentation "Class representing active sockets of type SOCK_STREAM and domain AF_LOCAL."))
273 (defclass socket-stream-local-passive
274 (passive-socket stream-socket local-socket) ()
275 (:default-initargs :active-class 'socket-stream-local-active)
276 (:documentation "Class representing passive sockets of type SOCK_STREAM and domain AF_LOCAL."))
278 (defclass socket-datagram-internet
279 (datagram-socket internet-socket) ()
280 (:documentation "Class representing active sockets of type SOCK_DGRAM and domain AF_INET or AF_INET6."))
282 (defclass socket-datagram-local
283 (datagram-socket local-socket) ()
284 (:documentation "Class representing active sockets of type SOCK_DGRAM and domain AF_LOCAL."))
286 (defclass socket-raw-internet
287 (internet-socket raw-socket) ()
288 (:default-initargs :type :raw)
289 (:documentation "Class representing active sockets of type SOCK_RAW and domain AF_LOCAL."))
291 (defclass socket-raw-netlink
292 (netlink-socket raw-socket) ()
293 (:default-initargs :type :raw)
294 (:documentation "Class representing active sockets of type SOCK_RAW and domain AF_NETLINK."))