1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
3 ;;; --- Various socket methods.
6 (in-package :net.sockets
)
8 (defvar *socket-type-map
*
9 '(((:ipv4
:stream
:active
:default
) . socket-stream-internet-active
)
10 ((:ipv6
:stream
:active
:default
) . socket-stream-internet-active
)
11 ((:ipv4
:stream
:passive
:default
) . socket-stream-internet-passive
)
12 ((:ipv6
:stream
:passive
:default
) . socket-stream-internet-passive
)
13 ((:local
:stream
:active
:default
) . socket-stream-local-active
)
14 ((:local
:stream
:passive
:default
) . socket-stream-local-passive
)
15 ((:local
:datagram
:active
:default
) . socket-datagram-local-active
)
16 ((:ipv4
:datagram
:active
:default
) . socket-datagram-internet-active
)
17 ((:ipv6
:datagram
:active
:default
) . socket-datagram-internet-active
)))
19 ;;; FIXME: should match :default to whatever protocol is the default.
20 (defun select-socket-class (address-family type connect protocol
)
21 (or (cdr (assoc (list address-family type connect protocol
) *socket-type-map
*
23 (error "No socket class found !!")))
25 ;;;; Shared Initialization
27 (defun translate-make-socket-keywords-to-constants (address-family type protocol
)
28 (let ((sf (ecase address-family
34 (:datagram sock-dgram
)))
36 ((integerp protocol
) protocol
)
37 ((eq :default protocol
) 0)
38 (t (lookup-protocol protocol
)))))
41 (defmethod socket-os-fd ((socket socket
))
44 (defmethod initialize-instance :after
((socket socket
) &key
45 file-descriptor address-family type
47 (with-accessors ((fd fd-of
) (fam socket-address-family
) (proto socket-protocol
))
49 (setf fd
(or file-descriptor
50 (multiple-value-call #'%socket
51 (translate-make-socket-keywords-to-constants
52 address-family type protocol
))))
53 (setf fam address-family
56 (defmethod (setf external-format-of
) (external-format (socket passive-socket
))
57 (setf (slot-value socket
'external-format
)
58 (babel:ensure-external-format external-format
)))
60 (defmethod initialize-instance :after
((socket passive-socket
) &key external-format
61 input-buffer-size output-buffer-size
)
62 ;; Makes CREATE-SOCKET simpler
63 (declare (ignore input-buffer-size output-buffer-size
))
64 (setf (external-format-of socket
) external-format
))
66 (defmethod socket-type ((socket stream-socket
))
69 (defmethod socket-type ((socket datagram-socket
))
72 (defun ipv6-socket-p (socket)
73 "Return T if SOCKET is an AF_INET6 socket."
74 (eq :ipv6
(socket-address-family socket
)))
78 (defun sock-fam (socket)
79 (ecase (socket-address-family socket
)
83 (defmethod print-object ((socket socket-stream-internet-active
) stream
)
84 (print-unreadable-object (socket stream
:identity t
)
85 (format stream
"active ~A stream socket" (sock-fam socket
))
86 (if (socket-connected-p socket
)
87 (multiple-value-bind (host port
) (remote-name socket
)
88 (format stream
" connected to ~A/~A"
89 (address-to-string host
) port
))
90 (format stream
", ~:[closed~;unconnected~]" (fd-of socket
)))))
92 (defmethod print-object ((socket socket-stream-internet-passive
) stream
)
93 (print-unreadable-object (socket stream
:identity t
)
94 (format stream
"passive ~A stream socket" (sock-fam socket
))
95 (if (socket-bound-p socket
)
96 (multiple-value-bind (host port
) (local-name socket
)
97 (format stream
" ~:[bound to~;waiting @~] ~A/~A"
98 (socket-listening-p socket
)
99 (address-to-string host
) port
))
100 (format stream
", ~:[closed~;unbound~]" (fd-of socket
)))))
102 (defmethod print-object ((socket socket-stream-local-active
) stream
)
103 (print-unreadable-object (socket stream
:identity t
)
104 (format stream
"active local stream socket")
105 (if (socket-connected-p socket
)
106 (format stream
" connected to ~S"
107 (address-to-string (remote-filename socket
)))
108 (format stream
", ~:[closed~;unconnected~]" (fd-of socket
)))))
110 (defmethod print-object ((socket socket-stream-local-passive
) stream
)
111 (print-unreadable-object (socket stream
:identity t
)
112 (format stream
"passive local stream socket")
113 (if (socket-bound-p socket
)
114 (format stream
" ~:[bound to~;waiting @~] ~S"
115 (socket-listening-p socket
)
116 (address-to-string (local-filename socket
)))
117 (format stream
", ~:[closed~;unbound~]" (fd-of socket
)))))
119 (defmethod print-object ((socket socket-datagram-local-active
) stream
)
120 (print-unreadable-object (socket stream
:identity t
)
121 (format stream
"local datagram socket")
122 (if (socket-connected-p socket
)
123 (format stream
" connected to ~S"
124 (address-to-string (remote-filename socket
)))
126 (format stream
" waiting @ ~S" (address-to-string (local-filename socket
)))
127 (format stream
", closed" )))))
129 (defmethod print-object ((socket socket-datagram-internet-active
) stream
)
130 (print-unreadable-object (socket stream
:identity t
)
131 (format stream
"~A datagram socket" (sock-fam socket
))
132 (if (socket-connected-p socket
)
133 (multiple-value-bind (host port
) (remote-name socket
)
134 (format stream
" connected to ~A/~A"
135 (address-to-string host
) port
))
137 (multiple-value-bind (host port
) (local-name socket
)
138 (format stream
" waiting @ ~A/~A"
139 (address-to-string host
) port
))
140 (format stream
", closed" )))))
144 (defmethod close :around
((socket socket
) &key abort
)
145 (declare (ignore abort
))
148 (nix:close
(fd-of socket
)))
149 (setf (fd-of socket
) nil
150 (slot-value socket
'bound
) nil
)
153 (defmethod close :around
((socket passive-socket
) &key abort
)
154 (declare (ignore abort
))
156 (setf (slot-value socket
'listening
) nil
)
159 (defmethod close ((socket socket
) &key abort
)
160 (declare (ignore socket abort
)))
162 (defmethod socket-open-p ((socket socket
))
164 (with-sockaddr-storage-and-socklen (ss size
)
166 (%getsockname
(fd-of socket
) ss size
)
168 (socket-connection-reset-error () nil
)
169 (:no-error
(_) (declare (ignore _
)) t
)))))
173 (defun %local-name
(socket)
174 (with-sockaddr-storage-and-socklen (ss size
)
175 (%getsockname
(fd-of socket
) ss size
)
176 (sockaddr-storage->sockaddr ss
)))
178 (defmethod local-name ((socket socket
))
179 (%local-name socket
))
181 (defmethod local-host ((socket internet-socket
))
182 (nth-value 0 (%local-name socket
)))
184 (defmethod local-port ((socket internet-socket
))
185 (nth-value 1 (%local-name socket
)))
187 (defmethod local-filename ((socket local-socket
))
188 (%local-name socket
))
192 (defun %remote-name
(socket)
193 (with-sockaddr-storage-and-socklen (ss size
)
194 (%getpeername
(fd-of socket
) ss size
)
195 (sockaddr-storage->sockaddr ss
)))
197 (defmethod remote-name ((socket socket
))
198 (%remote-name socket
))
200 (defmethod remote-host ((socket internet-socket
))
201 (nth-value 0 (%remote-name socket
)))
203 (defmethod remote-port ((socket internet-socket
))
204 (nth-value 1 (%remote-name socket
)))
206 (defmethod remote-filename ((socket local-socket
))
207 (%remote-name socket
))
211 (defmethod bind-address :before
((socket internet-socket
) address
212 &key
(reuse-address t
))
213 (declare (ignore address
))
215 (setf (socket-option socket
:reuse-address
) t
)))
217 (defun bind-ipv4-address (fd address port
)
218 (with-sockaddr-in (sin address port
)
219 (%bind fd sin size-of-sockaddr-in
)))
221 (defun bind-ipv6-address (fd address port
)
222 (with-sockaddr-in6 (sin6 address port
)
223 (%bind fd sin6 size-of-sockaddr-in6
)))
225 (defmethod bind-address ((socket internet-socket
) (address ipv4-address
)
227 (if (ipv6-socket-p socket
)
228 (bind-ipv6-address (fd-of socket
)
229 (map-ipv4-vector-to-ipv6 (address-name address
))
231 (bind-ipv4-address (fd-of socket
) (address-name address
) port
))
234 (defmethod bind-address ((socket internet-socket
) (address ipv6-address
)
236 (bind-ipv6-address (fd-of socket
) (address-name address
) port
)
239 (defmethod bind-address ((socket local-socket
) (address local-address
) &key
)
240 (with-sockaddr-un (sun (address-name address
))
241 (%bind
(fd-of socket
) sun size-of-sockaddr-un
))
244 (defmethod bind-address :after
((socket socket
) (address address
) &key
)
245 (setf (slot-value socket
'bound
) t
))
249 (defmethod listen-on ((socket passive-socket
) &key backlog
)
250 (unless backlog
(setf backlog
(min *default-backlog-size
*
251 +max-backlog-size
+)))
252 (check-type backlog unsigned-byte
"a non-negative integer")
253 (%listen
(fd-of socket
) backlog
)
254 (setf (slot-value socket
'listening
) t
)
257 (defmethod listen-on ((socket active-socket
) &key
)
258 (error "You can't listen on active sockets."))
262 (defmethod accept-connection ((socket active-socket
) &key
)
263 (error "You can't accept connections on active sockets."))
265 (defmethod accept-connection ((socket passive-socket
) &key external-format
266 input-buffer-size output-buffer-size
267 (wait t
) (timeout nil
))
268 (flet ((make-client-socket (fd)
269 (make-instance (active-class socket
)
271 :external-format
(or external-format
272 (external-format-of socket
))
273 :input-buffer-size input-buffer-size
274 :output-buffer-size output-buffer-size
)))
275 (ignore-some-conditions (iomux:poll-timeout
)
276 (when wait
(iomux:wait-until-fd-ready
(fd-of socket
) :read timeout t
))
277 (with-sockaddr-storage-and-socklen (ss size
)
278 (ignore-some-conditions (nix:ewouldblock
)
279 (make-client-socket (%accept
(fd-of socket
) ss size
)))))))
283 (defun ipv4-connect (fd address port
)
284 (with-sockaddr-in (sin address port
)
285 (%connect fd sin size-of-sockaddr-in
)))
287 (defun ipv6-connect (fd address port
)
288 (with-sockaddr-in6 (sin6 address port
)
289 (%connect fd sin6 size-of-sockaddr-in6
)))
291 (defun call-with-socket-to-wait-connect (socket thunk wait timeout
)
294 (nix:ewouldblock
(err)
297 (iomux:wait-until-fd-ready
(fd-of socket
) :write timeout t
)
298 (let ((errcode (socket-option socket
:error
)))
299 (unless (zerop errcode
)
300 (signal-socket-error errcode
))))
303 (defmacro with-socket-to-wait-connect
((socket wait timeout
) &body body
)
304 `(call-with-socket-to-wait-connect ,socket
#'(lambda () ,@body
) ,wait
,timeout
))
306 (defmethod connect ((socket internet-socket
) (address inet-address
)
307 &key
(port 0) (wait t
) (timeout nil
))
308 (let ((name (address-name address
)))
309 (with-socket-to-wait-connect (socket wait timeout
)
311 ((ipv6-socket-p socket
)
312 (when (ipv4-address-p address
)
313 (setf name
(map-ipv4-vector-to-ipv6 name
)))
314 (ipv6-connect (fd-of socket
) name port
))
315 (t (ipv4-connect (fd-of socket
) name port
)))))
318 (defmethod connect ((socket local-socket
) (address local-address
) &key
)
319 (with-sockaddr-un (sun (address-name address
))
320 (%connect
(fd-of socket
) sun size-of-sockaddr-un
))
323 (defmethod connect ((socket passive-socket
) address
&key
)
324 (declare (ignore address
))
325 (error "You cannot connect passive sockets."))
327 (defmethod socket-connected-p ((socket socket
))
329 (with-sockaddr-storage-and-socklen (ss size
)
331 (%getpeername
(fd-of socket
) ss size
)
332 (socket-not-connected-error () nil
)
333 (:no-error
(_) (declare (ignore _
)) t
)))))
337 (defmethod disconnect :before
((socket socket
))
338 (unless (typep socket
'datagram-socket
)
339 (error "You can only disconnect active datagram sockets.")))
341 (defmethod disconnect ((socket datagram-socket
))
342 (with-foreign-object (sin 'sockaddr-in
)
343 (bzero sin size-of-sockaddr-in
)
344 (setf (foreign-slot-value sin
'sockaddr-in
'addr
) af-unspec
)
345 (%connect
(fd-of socket
) sin size-of-sockaddr-in
)
350 (defmethod shutdown ((socket socket
) &key read write
)
351 (assert (or read write
) (read write
)
352 "You must select at least one direction to shut down.")
353 (%shutdown
(fd-of socket
)
354 (multiple-value-case ((read write
))
360 ;;;; Socket flag definition
362 (defmacro define-socket-flag
(place name value platform
)
363 (let ((val (cond ((or (not platform
)
364 (featurep platform
)) value
)
365 ((not (featurep platform
)) 0))))
366 `(pushnew (cons ,name
,val
) ,place
)))
368 (defmacro define-socket-flags
(place &body definitions
)
370 (destructuring-bind (name value
&optional platform
) form
371 `(define-socket-flag ,place
,name
,value
,platform
))))
373 ,@(mapcar #'dflag definitions
))))
377 (defvar *sendto-flags
* ())
379 (define-socket-flags *sendto-flags
*
380 (:dont-route msg-dontroute
)
381 (:dont-wait msg-dontwait
(:not
:windows
))
382 (:out-of-band msg-oob
)
383 (:more msg-more
:linux
)
384 (:confirm msg-confirm
:linux
))
386 (defun %normalize-send-buffer
(buff start end ef
)
387 (check-bounds buff start end
)
389 (ub8-sarray (values buff start
(- end start
)))
390 (string (let ((vector (%to-octets buff ef start end
)))
391 (values vector
0 (length vector
))))
392 (vector (values (coerce buff
'ub8-sarray
)
393 start
(- end start
)))))
395 (defun %%send-to
(fd ss got-peer buffer start end flags ef
)
396 (multiple-value-bind (buff start-offset bufflen
)
397 (%normalize-send-buffer buffer start end ef
)
398 (with-pointer-to-vector-data (buff-sap buff
)
399 (incf-pointer buff-sap start-offset
)
402 (return-from %%send-to
403 (%sendto fd buff-sap bufflen flags
404 (if got-peer ss
(null-pointer))
405 (if got-peer
(sockaddr-size ss
) 0)))
407 :report
"Ignore this socket condition"
408 (return-from %%send-to
0))
409 (continue (&optional
(wait 0))
410 :report
"Try to send data again"
411 (when (plusp wait
) (sleep wait
))))))))
413 (defun %inet-send-to
(socket buffer start end remote-host remote-port flags
)
414 (with-sockaddr-storage (ss)
416 (sockaddr->sockaddr-storage ss
(ensure-hostname remote-host
)
417 (ensure-numerical-service remote-port
)))
418 (%%send-to
(fd-of socket
) ss
(if remote-host t
) buffer start end flags
419 (external-format-of socket
))))
421 (defun %local-send-to
(socket buffer start end remote-filename flags
)
422 (with-sockaddr-storage (ss)
423 (when remote-filename
424 (sockaddr->sockaddr-storage ss
(ensure-address remote-filename
:family
:local
) 0))
425 (%%send-to
(fd-of socket
) ss
(if remote-filename t
) buffer start end flags
426 (external-format-of socket
))))
428 (defmethod send-to ((socket internet-socket
) buffer
&rest args
429 &key
(start 0) end remote-host
(remote-port 0) (ipv6 *ipv6
*))
431 (%inet-send-to socket buffer start end remote-host remote-port
432 (compute-flags *sendto-flags
* args
))))
434 (defmethod send-to ((socket local-socket
) buffer
&rest args
435 &key
(start 0) end remote-filename
)
436 (%local-send-to socket buffer start end remote-filename
437 (compute-flags *sendto-flags
* args
)))
439 (define-compiler-macro send-to
(&whole form socket buffer
&rest args
440 &key
(start 0) end remote-host
(remote-port 0)
441 remote-filename
(ipv6 '*ipv6
*) &allow-other-keys
)
442 (let ((flags (compute-flags *sendto-flags
* args
)))
444 (once-only (socket buffer start end remote-host
445 remote-port remote-filename flags
)
448 (let ((*ipv6
* ,ipv6
))
449 (%inet-send-to
,socket
,buffer
,start
,end
450 ,remote-host
,remote-port
,flags
)))
452 (%local-send-to
,socket
,buffer
,start
,end
453 ,remote-filename
,flags
)))))
458 (defvar *recvfrom-flags
* ())
460 (define-socket-flags *recvfrom-flags
*
461 (:out-of-band msg-oob
)
463 (:wait-all msg-waitall
(:not
:windows
))
464 (:dont-wait msg-dontwait
(:not
:windows
)))
466 (defun allocate-ub8-buffer-for-string (length ef
)
467 (let* ((units-per-char (babel-encodings:enc-max-units-per-char
468 (babel:external-format-encoding ef
)))
469 (length (* units-per-char length
)))
470 (values (make-array length
:element-type
'ub8
)
473 (defun %normalize-receive-buffer
(buff start end ef
)
475 (ub8-sarray (values buff start
(- end start
)))
476 (string (allocate-ub8-buffer-for-string (- end start
) ef
))))
478 (defun %%receive-from
(fd ss size buffer start end flags ef
)
479 (check-bounds buffer start end
)
480 (multiple-value-bind (buff start-offset bufflen
)
481 (%normalize-receive-buffer buffer start end ef
)
482 (with-pointer-to-vector-data (buff-sap buff
)
483 (incf-pointer buff-sap start-offset
)
486 (let ((nbytes (%recvfrom fd buff-sap bufflen flags ss size
)))
487 (return-from %%receive-from
489 ;; FIXME: convert the octets directly into the buffer
490 (let ((str (babel:octets-to-string buff
:start
0 :end nbytes
491 :encoding
(babel:external-format-encoding ef
)
493 (replace buffer str
:start1 start
:end1 end
)
497 :report
"Ignore this socket condition"
498 (return-from %%receive-from
0))
499 (continue (&optional
(wait 0))
500 :report
"Try to receive data again"
501 (when (plusp wait
) (sleep wait
))))))))
503 (declaim (inline %receive-from-stream-socket
))
504 (defun %receive-from-stream-socket
(socket buffer start end flags
)
505 (with-sockaddr-storage-and-socklen (ss size
)
506 (let ((nelements (%%receive-from
(fd-of socket
) ss size buffer start end
507 flags
(external-format-of socket
))))
508 (values buffer nelements
))))
510 (declaim (inline %receive-from-datagram-socket
))
511 (defun %receive-from-datagram-socket
(socket buffer start end flags
)
512 (with-sockaddr-storage-and-socklen (ss size
)
513 (let ((nelements (%%receive-from
(fd-of socket
) ss size buffer start end
514 flags
(external-format-of socket
))))
515 (multiple-value-call #'values buffer nelements
516 (sockaddr-storage->sockaddr ss
)))))
518 (defun %receive-from
(socket buffer start end size flags
)
520 (check-type size unsigned-byte
"a non-negative integer")
521 (setf buffer
(make-array size
:element-type
'ub8
)
524 (stream-socket (%receive-from-stream-socket socket buffer start end flags
))
525 (datagram-socket (%receive-from-datagram-socket socket buffer start end flags
))))
527 (defmethod receive-from ((socket active-socket
) &rest args
528 &key buffer size
(start 0) end
)
529 (%receive-from socket buffer start end size
530 (compute-flags *recvfrom-flags
* args
)))
532 (define-compiler-macro receive-from
(&whole form socket
&rest args
533 &key buffer size
(start 0) end
&allow-other-keys
)
534 (let ((flags (compute-flags *recvfrom-flags
* args
)))
535 (cond (flags `(%receive-from
,socket
,buffer
,start
,end
,size
,flags
))