1 (in-package :sb-bsd-sockets)
4 getsockopt(socket, level, int optname, void *optval, socklen_t *optlen)
5 setsockopt(socket, level, int optname, void *optval, socklen_t optlen)
6 ^ SOL_SOCKET or a protocol number
8 In terms of providing a useful interface, we have to face up to the
9 fact that most of these take different data types - some are integers,
10 some are booleans, some are foreign struct instances, etc etc
12 (define-socket-option lisp-name doc level number mangle-arg size mangle-return)
14 macro-expands to two functions that define lisp-name and (setf ,lisp-name)
15 and calls the functions mangle-arg and mangle-return on outgoing and incoming
18 Parameters passed to the function thus defined (lisp-name)
19 are all passed directly into mangle-arg. mangle-arg should return an
20 alien pointer - this is passed unscathed to the foreign routine, so
21 wants to have type (* t). Note that even for options that have
22 integer arguments, this is still a pointer to said integer.
24 size is the size of the buffer that the return of mangle-arg points
25 to, and also of the buffer that we should allocate for getsockopt
28 mangle-return is called with an alien buffer and should turn it into
29 something that the caller will want.
31 Code for options that not every system has should be conditionalised:
33 (if (boundp 'sockint::IP_RECVIF)
34 (define-socket-option so-receive-interface nil (getprotobyname "ip")
35 sockint::IP_RECVIF ... ))
38 (defmacro define-socket-option
39 (lisp-name documentation
40 level number buffer-type mangle-arg mangle-return mangle-setf-buffer
41 &optional features info)
43 (if (numberp (eval level))
45 `(get-protocol-by-name ,(string-downcase (symbol-name level)))))
46 (supportedp (or (null features) (featurep features))))
49 (defun ,lisp-name (socket)
50 ,@(when documentation (list (concatenate 'string documentation " " info)))
52 `(sb-alien:with-alien ((size sb-alien:int)
53 (buffer ,buffer-type))
54 (setf size (sb-alien:alien-size ,buffer-type :bytes))
55 (if (= -1 (sockint::getsockopt (socket-file-descriptor socket)
57 (sb-alien:addr buffer)
58 (sb-alien:addr size)))
59 (socket-error "getsockopt")
60 (,mangle-return buffer size)))
61 `(error 'unsupported-operator
62 :format-control "Socket option ~S is not supported in this platform."
63 :format-arguments (list ',lisp-name))))
64 (defun (setf ,lisp-name) (new-val socket)
66 `(sb-alien:with-alien ((buffer ,buffer-type))
67 (setf buffer ,(if mangle-arg
68 `(,mangle-arg new-val)
70 (when (= -1 (sockint::setsockopt (socket-file-descriptor socket)
72 (,mangle-setf-buffer buffer)
73 ,(if (eql buffer-type 'sb-alien:c-string)
75 `(sb-alien:alien-size ,buffer-type :bytes))))
76 (socket-error "setsockopt")))
77 `(error 'unsupported-operator
78 :format-control "Socket option ~S is not supported on this platform."
79 :format-arguments (list ',lisp-name)))))))
81 ;;; sockopts that have integer arguments
83 (defun foreign-int-to-integer (buffer size)
84 (assert (= size (sb-alien:alien-size sb-alien:int :bytes)))
87 (defmacro define-socket-option-int (name level number &optional features (info ""))
88 `(define-socket-option ,name nil ,level ,number
89 sb-alien:int nil foreign-int-to-integer sb-alien:addr ,features ,info))
91 (define-socket-option-int
92 sockopt-receive-low-water sockint::sol-socket sockint::so-rcvlowat)
93 (define-socket-option-int
94 sockopt-send-low-water sockint::sol-socket sockint::so-sndlowat)
95 (define-socket-option-int
96 sockopt-type sockint::sol-socket sockint::so-type)
97 (define-socket-option-int
98 sockopt-send-buffer sockint::sol-socket sockint::so-sndbuf)
99 (define-socket-option-int
100 sockopt-receive-buffer sockint::sol-socket sockint::so-rcvbuf)
101 (define-socket-option-int
102 sockopt-priority sockint::sol-socket sockint::so-priority :linux
103 "Available only on Linux.")
105 ;;; boolean options are integers really
107 (defun foreign-int-to-bool (x size)
108 (if (zerop (foreign-int-to-integer x size))
112 (defun bool-to-foreign-int (val)
115 (defmacro define-socket-option-bool (name level c-name &optional features (info ""))
116 `(define-socket-option ,name
117 ,(format nil "~@<Return the value of the ~A socket option for SOCKET. ~
118 This can also be updated with SETF.~:@>"
119 (symbol-name c-name))
121 sb-alien:int bool-to-foreign-int foreign-int-to-bool sb-alien:addr
124 (define-socket-option-bool
125 sockopt-reuse-address sockint::sol-socket sockint::so-reuseaddr)
126 (define-socket-option-bool
127 sockopt-keep-alive sockint::sol-socket sockint::so-keepalive)
128 (define-socket-option-bool
129 sockopt-oob-inline sockint::sol-socket sockint::so-oobinline)
130 (define-socket-option-bool
131 sockopt-bsd-compatible sockint::sol-socket sockint::so-bsdcompat :linux
132 "Available only on Linux.")
133 (define-socket-option-bool
134 sockopt-pass-credentials sockint::sol-socket sockint::so-passcred :linux
135 "Available only on Linux.")
136 (define-socket-option-bool
137 sockopt-debug sockint::sol-socket sockint::so-debug)
138 (define-socket-option-bool
139 sockopt-dont-route sockint::sol-socket sockint::so-dontroute)
140 (define-socket-option-bool
141 sockopt-broadcast sockint::sol-socket sockint::so-broadcast)
143 (define-socket-option-bool sockopt-tcp-nodelay :tcp sockint::tcp-nodelay)
145 (defun identity-1 (x &rest args)
146 (declare (ignore args))
149 (define-socket-option sockopt-bind-to-device nil sockint::sol-socket
150 sockint::so-bindtodevice sb-alien:c-string identity identity-1 identity
151 :linux "Available only on Linux")
153 ;;; other kinds of socket option
155 ;;; so_peercred takes a ucre structure
156 ;;; so_linger struct linger {
157 ; int l_onoff; /* linger active */
158 ; int l_linger; /* how many seconds to linger for */
163 (sockopt-reuse-address 2)
165 (defun echo-server ()
166 (let ((s (make-inet-socket :stream (get-protocol-by-name "tcp"))))
167 (setf (sockopt-reuse-address s) t)
168 (setf (sockopt-bind-to-device s) "lo")
169 (socket-bind s (make-inet-address "127.0.0.1") 3459)
172 (let* ((s1 (socket-accept s))
173 (stream (socket-make-stream s1 :input t :output t :buffering :none)))
174 (let ((line (read-line stream)))
175 (format t "got one ~A ~%" line)
176 (format stream "~A~%" line))