1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; --- Socket creation.
6 (in-package :net.sockets
)
8 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
9 (defvar *socket-type-map
*
10 '(((:ipv4
:stream
:active
:default
) . socket-stream-internet-active
)
11 ((:ipv6
:stream
:active
:default
) . socket-stream-internet-active
)
12 ((:ipv4
:stream
:passive
:default
) . socket-stream-internet-passive
)
13 ((:ipv6
:stream
:passive
:default
) . socket-stream-internet-passive
)
14 ((:local
:stream
:active
:default
) . socket-stream-local-active
)
15 ((:local
:stream
:passive
:default
) . socket-stream-local-passive
)
16 ((:local
:datagram
:active
:default
) . socket-datagram-local-active
)
17 ((:ipv4
:datagram
:active
:default
) . socket-datagram-internet-active
)
18 ((:ipv6
:datagram
:active
:default
) . socket-datagram-internet-active
)))
20 ;; FIXME: should match :default to whatever protocol is the default.
21 (defun select-socket-class (address-family type connect protocol
)
22 (or (cdr (assoc (list address-family type connect protocol
) *socket-type-map
*
24 (error "No socket class found !!"))))
26 (defun create-socket (family type connect external-format
&key
27 fd input-buffer-size output-buffer-size
)
28 (make-instance (select-socket-class family type connect
:default
)
29 :address-family family
:file-descriptor fd
30 :external-format external-format
31 :input-buffer-size input-buffer-size
32 :output-buffer-size output-buffer-size
))
34 (define-compiler-macro create-socket
(&whole form family type connect external-format
35 &key fd input-buffer-size output-buffer-size
)
37 ((and (constantp family
) (constantp type
) (constantp connect
))
38 `(make-instance ',(select-socket-class family type connect
:default
)
39 :address-family
,family
:file-descriptor
,fd
40 :external-format
,external-format
41 :input-buffer-size
,input-buffer-size
42 :output-buffer-size
,output-buffer-size
))
45 (defmacro with-close-on-error
((var value
) &body body
)
46 "Bind `VAR' to `VALUE' and execute `BODY' as implicit PROGN.
47 If a non-local exit occurs during the execution of `BODY',
48 call CLOSE with :ABORT T on `VAR'."
50 (unwind-protect-case () ,@body
51 (:abort
(close ,var
:abort t
)))))
53 (defmacro %create-internet-socket
(family &rest args
)
55 (:ipv4
(create-socket :ipv4
,@args
))
56 (:ipv6
(create-socket :ipv6
,@args
))))
58 (defmacro with-guard-against-non-list-args-and-destructuring-bind-errors
59 (form args
&body body
)
61 (handler-case (progn ,@body
)
62 (error (err) `(error ,err
)))
65 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
66 (defun make-first-level-name (family type connect
)
67 (format-symbol :net.sockets
"%~A-~A-~A-~A-~A" :make family type connect
:socket
)))
69 (defmacro define-socket-creator
((socket-family socket-type socket-connect
)
70 (family ef key
&rest args
) &body body
)
71 (assert (eql '&key key
))
72 (flet ((maybe-quote-default-value (arg)
73 (cond ((symbolp arg
) arg
)
74 ((consp arg
) (list (first arg
) `(quote ,(second arg
))))))
76 (cond ((symbolp arg
) arg
)
77 ((consp arg
) (first arg
))))
79 `(list (quote ,(car form
)) ,@(cdr form
))))
80 (let* ((arg-names (mapcar #'arg-name args
))
81 (first-level-function (make-first-level-name socket-family socket-type socket-connect
))
82 (second-level-function (format-symbol t
"%~A" first-level-function
))
83 (first-level-body `(,second-level-function family ef
,@arg-names
)))
85 (declaim (inline ,second-level-function
))
86 (defun ,second-level-function
(,family
,ef
,@arg-names
) ,@body
)
87 (defun ,first-level-function
(arguments family ef
)
88 (destructuring-bind (&key
,@args
) arguments
,first-level-body
))
89 (define-compiler-macro ,first-level-function
(&whole form arguments family ef
)
90 (with-guard-against-non-list-args-and-destructuring-bind-errors
92 ;; Must quote default values in order for them not to be evaluated
93 ;; in the compilation environment
94 (destructuring-bind (&key
,@(mapcar #'maybe-quote-default-value args
))
96 ,(quotify first-level-body
))))))))
98 ;;; Internet Stream Active Socket creation
100 (defun %%init-internet-stream-active-socket
(socket keepalive nodelay reuse-address
101 local-host local-port remote-host remote-port
)
102 (setf (socket-option socket
:no-sigpipe
:if-does-not-exist nil
) t
)
103 (when keepalive
(setf (socket-option socket
:keep-alive
) t
))
104 (when nodelay
(setf (socket-option socket
:tcp-nodelay
) t
))
106 (bind-address socket
(ensure-hostname local-host
)
108 :reuse-address reuse-address
))
109 (when (plusp remote-port
)
110 (connect socket
(ensure-hostname remote-host
)
114 (define-socket-creator (:internet
:stream
:active
)
115 (family ef
&key keepalive nodelay
(reuse-address t
)
116 local-host
(local-port 0)
117 (remote-host +any-host
+) (remote-port 0)
118 input-buffer-size output-buffer-size
)
119 (with-close-on-error (socket (%create-internet-socket family
:stream
:active ef
120 :input-buffer-size input-buffer-size
121 :output-buffer-size output-buffer-size
))
122 (%%init-internet-stream-active-socket socket keepalive nodelay reuse-address
123 local-host local-port remote-host remote-port
)))
125 ;;; Internet Stream Passive Socket creation
127 (defun %%init-internet-stream-passive-socket
(socket interface reuse-address
128 local-host local-port backlog
)
131 (setf (socket-option socket
:bind-to-device
) interface
))
132 (bind-address socket
(ensure-hostname local-host
)
134 :reuse-address reuse-address
)
135 (listen-on socket
:backlog backlog
))
138 (define-socket-creator (:internet
:stream
:passive
)
139 (family ef
&key interface
(reuse-address t
)
140 (local-host +any-host
+) (local-port 0)
141 (backlog *default-backlog-size
*))
142 (with-close-on-error (socket (%create-internet-socket family
:stream
:passive ef
))
143 (%%init-internet-stream-passive-socket socket interface reuse-address
144 local-host local-port backlog
)))
146 ;;; Local Stream Active Socket creation
148 (defun %%init-local-stream-active-socket
(socket local-filename remote-filename
)
149 (setf (socket-option socket
:no-sigpipe
:if-does-not-exist nil
) t
)
151 (bind-address socket
(ensure-address local-filename
:family
:local
)))
152 (when remote-filename
153 (connect socket
(ensure-address remote-filename
:family
:local
)))
156 (define-socket-creator (:local
:stream
:active
)
157 (family ef
&key local-filename remote-filename
158 input-buffer-size output-buffer-size
)
159 (declare (ignore family
))
160 (with-close-on-error (socket (create-socket :local
:stream
:active ef
161 :input-buffer-size input-buffer-size
162 :output-buffer-size output-buffer-size
))
163 (%%init-local-stream-active-socket socket local-filename remote-filename
)))
165 ;;; Local Stream Passive Socket creation
167 (defun %%init-local-stream-passive-socket
(socket local-filename reuse-address backlog
)
169 (bind-address socket
(ensure-address local-filename
:family
:local
)
170 :reuse-address reuse-address
)
171 (listen-on socket
:backlog backlog
))
174 (define-socket-creator (:local
:stream
:passive
)
175 (family ef
&key local-filename
(reuse-address t
)
176 (backlog *default-backlog-size
*))
177 (declare (ignore family
))
178 (with-close-on-error (socket (create-socket :local
:stream
:passive ef
))
179 (%%init-local-stream-passive-socket socket local-filename reuse-address backlog
)))
181 ;;; Internet Datagram Socket creation
183 (defun %%init-internet-datagram-active-socket
(socket broadcast interface reuse-address
184 local-host local-port remote-host remote-port
)
185 (setf (socket-option socket
:no-sigpipe
:if-does-not-exist nil
) t
)
186 (when broadcast
(setf (socket-option socket
:broadcast
) t
))
188 (bind-address socket
(ensure-hostname local-host
)
190 :reuse-address reuse-address
)
192 (setf (socket-option socket
:bind-to-device
) interface
)))
193 (when (plusp remote-port
)
194 (connect socket
(ensure-hostname remote-host
)
198 (define-socket-creator (:internet
:datagram
:active
)
199 (family ef
&key broadcast interface
(reuse-address t
)
200 local-host
(local-port 0)
201 (remote-host +any-host
+) (remote-port 0))
202 (with-close-on-error (socket (%create-internet-socket family
:datagram
:active ef
))
203 (%%init-internet-datagram-active-socket socket broadcast interface reuse-address
204 local-host local-port remote-host remote-port
)))
206 ;;; Local Datagram Socket creation
208 (defun %%init-local-datagram-active-socket
(socket local-filename remote-filename
)
209 (setf (socket-option socket
:no-sigpipe
:if-does-not-exist nil
) t
)
211 (bind-address socket
(ensure-address local-filename
:family
:local
)))
212 (when remote-filename
213 (connect socket
(ensure-address remote-filename
:family
:local
)))
216 (define-socket-creator (:local
:datagram
:active
)
217 (family ef
&key local-filename remote-filename
)
218 (declare (ignore family
))
219 (with-close-on-error (socket (create-socket :local
:datagram
:active ef
))
220 (%%init-local-datagram-active-socket socket local-filename remote-filename
)))
224 (defun make-socket (&rest args
&key
(address-family :internet
) (type :stream
)
225 (connect :active
) (ipv6 *ipv6
*)
226 (external-format :default
) &allow-other-keys
)
227 "Creates a socket instance of the appropriate subclass of SOCKET."
228 (check-type address-family
(member :internet
:local
:ipv4
:ipv6
) "one of :INTERNET, :LOCAL, :IPV4 or :IPV6")
229 (check-type type
(member :stream
:datagram
) "either :STREAM or :DATAGRAM")
230 (check-type connect
(member :active
:passive
) "either :ACTIVE or :PASSIVE")
231 (let ((args (remove-from-plist args
:address-family
:type
:connect
:external-format
:ipv6
)))
232 (when (eq :ipv4 address-family
) (setf ipv6 nil
))
234 (when (eq :internet address-family
) (setf address-family
+default-inet-address-family
+))
235 (multiple-value-case ((address-family type connect
))
236 (((:ipv4
:ipv6
) :stream
:active
)
237 (%make-internet-stream-active-socket args address-family external-format
))
238 (((:ipv4
:ipv6
) :stream
:passive
)
239 (%make-internet-stream-passive-socket args address-family external-format
))
240 ((:local
:stream
:active
)
241 (%make-local-stream-active-socket args
:local external-format
))
242 ((:local
:stream
:passive
)
243 (%make-local-stream-passive-socket args
:local external-format
))
244 (((:ipv4
:ipv6
) :datagram
)
245 (%make-internet-datagram-active-socket args address-family external-format
))
247 (%make-local-datagram-active-socket args
:local external-format
))))))
249 (define-compiler-macro make-socket
(&whole form
&rest args
&key
(address-family :internet
) (type :stream
)
250 (connect :active
) (ipv6 '*ipv6
* ipv6p
)
251 (external-format :default
) &allow-other-keys
)
253 ((and (constantp address-family
) (constantp type
) (constantp connect
))
254 (check-type address-family
(member :internet
:local
:ipv4
:ipv6
) "one of :INTERNET, :LOCAL, :IPV4 or :IPV6")
255 (check-type type
(member :stream
:datagram
) "either :STREAM or :DATAGRAM")
256 (check-type connect
(member :active
:passive
) "either :ACTIVE or :PASSIVE")
257 (let* ((family (if (member address-family
'(:ipv4
:ipv6
)) :internet address-family
))
258 (lower-function (make-first-level-name family type connect
))
259 (newargs (remove-from-plist args
:address-family
:type
:connect
:external-format
:ipv6
)))
261 (:internet
(setf address-family
'+default-inet-address-family
+))
262 (:ipv4
(setf ipv6 nil ipv6p t
)))
263 (let ((expansion `(,lower-function
(list ,@newargs
) ,address-family
,external-format
)))
264 (if ipv6p
`(let ((*ipv6
* ,ipv6
)) ,expansion
) expansion
))))
267 (defmacro with-open-socket
((var &rest args
) &body body
)
268 "VAR is bound to a socket created by passing ARGS to
269 MAKE-SOCKET and BODY is executed as implicit PROGN. The socket
270 is automatically closed upon exit."
271 `(with-open-stream (,var
(make-socket ,@args
)) ,@body
))
273 (defmacro with-accept-connection
((var passive-socket
&rest args
) &body body
)
274 "VAR is bound to a socket created by passing PASSIVE-SOCKET and ARGS to
275 ACCEPT-CONNECTION and BODY is executed as implicit PROGN. The socket
276 is automatically closed upon exit."
277 `(with-open-stream (,var
(accept-connection ,passive-socket
,@args
)) ,@body
))
279 ;;; MAKE-SOCKET-FROM-FD
281 ;;; FIXME: must come up with a way to find out
282 ;;; whether a socket is active or passive
283 (defun make-socket-from-fd (fd &key
(connect :active
) (external-format :default
)
284 input-buffer-size output-buffer-size
)
285 "Creates an socket instance of the appropriate subclass of SOCKET using `FD'.
286 The connection type of the socket must be specified(:ACTIVE or :PASSIVE).
287 The address family and type of the socket is automatically discovered using OS functions. Buffer sizes
288 for the new socket can also be specified using `INPUT-BUFFER-SIZE' and `OUTPUT-BUFFER-SIZE'."
289 (flet ((%get-address-family
(fd)
290 (with-sockaddr-storage-and-socklen (ss size
)
291 (%getsockname fd ss size
)
292 (eswitch ((foreign-slot-value ss
'sockaddr-storage
'family
) :test
#'=)
297 (eswitch ((get-socket-option-int fd sol-socket so-type
) :test
#'=)
298 (sock-stream :stream
)
299 (sock-dgram :datagram
))))
300 (create-socket (%get-address-family fd
)
302 connect external-format
:fd fd
303 :input-buffer-size input-buffer-size
304 :output-buffer-size output-buffer-size
)))
308 (defun make-socket-pair (&key
(type :stream
) (protocol :default
) (external-format :default
)
309 input-buffer-size output-buffer-size
)
310 "Creates a pair of sockets connected to each other.
311 The socket type can be either :STREAM or :DATAGRAM. Currently OSes can only create :LOCAL sockets this way.
312 Buffer sizes for the new sockets can also be specified using `INPUT-BUFFER-SIZE' and `OUTPUT-BUFFER-SIZE'."
313 (flet ((%make-socket-pair
(fd)
314 (make-socket-from-fd fd
:external-format external-format
315 :input-buffer-size input-buffer-size
316 :output-buffer-size output-buffer-size
)))
317 (multiple-value-bind (fd1 fd2
)
318 (multiple-value-call #'%socketpair
319 (translate-make-socket-keywords-to-constants :local type protocol
))
320 (values (%make-socket-pair fd1
)
321 (%make-socket-pair fd2
)))))
323 ;;; SEND/RECEIVE-FILE-DESCRIPTOR
325 (defun call-with-buffers-for-fd-passing (fn)
326 (with-foreign-object (msg 'msghdr
)
327 (bzero msg size-of-msghdr
)
328 (with-foreign-pointer (buffer #.
(isys:%sys-cmsg-space size-of-int
) buffer-size
)
329 (bzero buffer buffer-size
)
330 (with-foreign-slots ((control controllen
) msg msghdr
)
332 controllen buffer-size
)
333 (let ((cmsg (isys:%sys-cmsg-firsthdr msg
)))
334 (with-foreign-slots ((len level type
) cmsg cmsghdr
)
335 (setf len
(isys:%sys-cmsg-len size-of-int
)
338 (funcall fn msg cmsg
)))))))
340 (defmacro with-buffers-for-fd-passing
((msg-var cmsg-var
) &body body
)
341 `(call-with-buffers-for-fd-passing (lambda (,msg-var
,cmsg-var
) ,@body
)))
343 (defmethod send-file-descriptor ((socket local-socket
) file-descriptor
)
344 (with-buffers-for-fd-passing (msg cmsg
)
345 (let ((data (isys:%sys-cmsg-data cmsg
)))
346 (setf (mem-aref data
:int
) file-descriptor
)
347 (%sendmsg
(fd-of socket
) msg
0)
350 (defmethod receive-file-descriptor ((socket local-socket
))
351 (with-buffers-for-fd-passing (msg cmsg
)
352 (let ((data (isys:%sys-cmsg-data cmsg
)))
353 (%recvmsg
(fd-of socket
) msg
0)
354 (mem-aref data
:int
))))