1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
3 ;;; socket-options.lisp --- Setter and getters for various socket options.
5 ;;; Copyright (C) 2006-2007, Stelian Ionescu <sionescu@common-lisp.net>
7 ;;; This code is free software; you can redistribute it and/or
8 ;;; modify it under the terms of the version 2.1 of
9 ;;; the GNU Lesser General Public License as published by
10 ;;; the Free Software Foundation, as clarified by the
11 ;;; preamble found here:
12 ;;; http://opensource.franz.com/preamble.html
14 ;;; This program is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU Lesser General
20 ;;; Public License along with this library; if not, write to the
21 ;;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
22 ;;; Boston, MA 02110-1301, USA
24 (in-package :net.sockets
)
26 ;;; TODO: manage socket options errors
27 (defun sockopt-error (retval level option action
&optional val1 val2
)
28 (declare (ignore retval level option action val1 val2
))
29 (error "Sockopt error !"))
33 ;;; This interface looks nice but doesn't work so well for the linger
34 ;;; and timeout options. Figure out a good solution. Possible ones
37 ;;; * don't worry, tell the user to use GET/SET-SOCKET-OPTION
38 ;;; * socket-linger-option and socket-timeval-option accessors
39 ;;; * use separate accessors for each and every option, like
42 (defun socket-option (socket option-name
)
43 (get-socket-option socket option-name
))
45 (defun (setf socket-option
) (value socket option-name
)
46 (set-socket-option socket option-name
:value value
))
50 (defun set-socket-option-bool (fd level option value
)
51 (with-foreign-object (optval :int
)
52 (setf (mem-ref optval
:int
) (lisp->c-bool value
))
53 (setsockopt fd level option optval size-of-int
)
56 (defun set-socket-option-int (fd level option value
)
57 (with-foreign-object (optval :int
)
58 (setf (mem-ref optval
:int
) value
)
59 (setsockopt fd level option optval size-of-int
)
62 (defun set-socket-option-linger (fd level option new-onoff new-linger
)
63 (with-foreign-object (optval 'linger
)
64 (with-foreign-slots ((linger onoff
) optval linger
)
65 (setf onoff
(lisp->c-bool new-onoff
)
67 (setsockopt fd level option optval size-of-linger
)
70 (defun set-socket-option-timeval (fd level option sec usec
)
71 (with-foreign-object (optval 'nix
::timeval
)
72 (with-foreign-slots ((nix::sec nix
::usec
) optval nix
::timeval
)
75 (setsockopt fd level option optval nix
::size-of-timeval
)
80 (defun get-socket-option-bool (fd level option
)
81 (with-foreign-object (optval :int
)
82 (with-socklen (optlen size-of-int
)
83 (getsockopt fd level option optval optlen
)
84 (mem-ref optval
:boolean
))))
86 (defun get-socket-option-int (fd level option
)
87 (with-foreign-object (optval :int
)
88 (with-socklen (optlen size-of-int
)
89 (getsockopt fd level option optval optlen
)
90 (mem-ref optval
:int
))))
92 (defun get-socket-option-linger (fd level option
)
93 (with-foreign-object (optval 'linger
)
94 (with-socklen (optlen size-of-linger
)
95 (getsockopt fd level option optval optlen
)
96 (with-foreign-slots ((linger onoff
) optval linger
)
97 (values (not (zerop onoff
)) linger
)))))
99 (defun get-socket-option-timeval (fd level option
)
100 (with-foreign-object (optval 'nix
::timeval
)
101 (with-socklen (optlen nix
::size-of-timeval
)
102 (getsockopt fd level option optval optlen
)
103 (with-foreign-slots ((nix::sec nix
::usec
) optval nix
::timeval
)
104 (values nix
::sec nix
::usec
)))))
106 ;;;; Option Definitions
108 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
109 (defvar +helper-args-map
+
112 (:linger
(onoff linger
))
113 (:timeval
(sec usec
)))))
115 (defmacro define-get-sockopt
(os eql-name helper-get level optname
)
116 `(defmethod get-socket-option ((socket socket
) (option-name (eql ,eql-name
)))
117 ,(if (or (eq os
:any
) (featurep os
))
118 `(,helper-get
(socket-fd socket
) ,level
,optname
)
119 `(error 'option-not-available option-name
))))
121 (defmacro define-set-sockopt
(os eql-name args helper-set level optname
)
122 `(defmethod set-socket-option
123 ((socket socket
) (option-name (eql ,eql-name
)) &key
,@args
)
124 ,@(if (or (eq os
:any
) (featurep os
))
125 `((,helper-set
(socket-fd socket
) ,level
,optname
,@args
))
126 `((declare (ignore ,@args
))
127 (error 'option-not-available option-name
)))))
129 (defmacro define-socket-option
(name action optname level argtype os
)
130 (declare (type symbol action
)
131 (type symbol argtype
)
132 (type (or symbol list
) os
))
133 (flet ((make-helper-name (action value-type
)
134 (format-symbol t
"~A~A~A"
135 action
'#:-socket-option- value-type
)))
136 (let ((eql-name (make-keyword name
))
137 (args (second (assoc argtype
+helper-args-map
+)))
138 (helper-get (make-helper-name :get argtype
))
139 (helper-set (make-helper-name :set argtype
)))
144 (when (member action
(list :get
:get-and-set
))
145 `(define-get-sockopt ,os
,eql-name
,helper-get
,level
,optname
))
146 (when (member action
(list :set
:get-and-set
))
147 `(define-set-sockopt ,os
,eql-name
,args
,helper-set
,level
150 (defmacro define-socket-options
(action level os
&body options
)
152 ,@(loop :for
(name optname argtype
) :in options
:collect
153 `(define-socket-option ,name
,action
154 ,optname
,level
,argtype
,os
))))
158 (define-socket-options :get sol-socket
:any
159 (accept-connections so-acceptconn
:bool
)
160 (error so-error
:int
)
163 (define-socket-options :get-and-set sol-socket
:any
164 (broadcast so-broadcast
:bool
)
165 (debug so-debug
:bool
)
166 (dont-route so-dontroute
:bool
)
167 (keep-alive so-keepalive
:bool
)
168 (linger so-linger
:linger
)
169 (oob-inline so-oobinline
:bool
)
170 (receive-buffer so-rcvbuf
:int
)
171 (send-buffer so-sndbuf
:int
)
172 (receive-low-water so-rcvlowat
:int
)
173 (send-low-water so-sndlowat
:int
)
174 (receive-timeout so-rcvtimeo
:timeval
)
175 (send-timeout so-sndtimeo
:timeval
)
176 (reuse-address so-reuseaddr
:bool
))
178 ;;;; Linux-specific Options
180 (define-socket-options :set sol-socket
:linux
181 (bsd-compatible so-bsdcompat
:bool
)
182 (bind-to-device so-bindtodevice
:int
))
184 (define-socket-option priority
:get-and-set
185 so-priority sol-socket
:int
:linux
)
187 ;;;; FreeBSD-specific options
189 (define-socket-options :get-and-set sol-socket
:freebsd
190 (reuse-port so-reuseport
:bool
)
191 (use-loopback so-useloopback
:bool
)
192 (no-sigpipe so-nosigpipe
:bool
))
196 ;; TODO: implement "struct ucred" helpers
198 ;; (define-socket-option pass-credentials :get-and-set et:so-passcred et:sol-socket :ucred (:or :linux :freebsd))
199 ;; (define-socket-option peer-credentials :get et:so-peercred et:sol-socket :ucred (:or :linux :freebsd))
202 ;; TODO: implement "struct accept_filter_arg" helpers
204 ;; (define-socket-option accept-filter :get-and-set et:so-acceptfilter et:sol-socket :accept-filter :freebsd)
206 ;; TODO: find out the types of these options
208 ;; (define-socket-option bintime :get-and-set et:so-bintime et:sol-socket :bool :freebsd)
209 ;; (define-socket-option label :get-and-set et:so-label et:sol-socket :bool :freebsd)
210 ;; (define-socket-option peerlabel :get-and-set et:so-peerlabel et:sol-socket :bool :freebsd)
211 ;; (define-socket-option listen-queue-limit :get-and-set et:so-listenqlimit et:sol-socket :int :freebsd)
212 ;; (define-socket-option listen-queue-length :get-and-set et:so-listenqlen et:sol-socket :int :freebsd)
213 ;; (define-socket-option listen-incomplete-queue-length :get-and-set et:so-listenincqlen et:sol-socket :int :freebsd)
218 (define-socket-option tcp-nodelay
:get-and-set
219 tcp-nodelay ipproto-tcp
:bool
:any
)
221 (define-socket-option tcp-maxseg
:get-and-set
222 tcp-maxseg ipproto-tcp
:int
(:or
:linux
:freebsd
))
224 ;;;; Linux-specific TCP Options
226 (define-socket-options :get-and-set ipproto-tcp
:linux
227 (tcp-cork tcp-cork
:bool
)
228 (tcp-defer-accept tcp-defer-accept
:int
)
229 (tcp-keepcnt tcp-keepcnt
:int
)
230 (tcp-keepidle tcp-keepidle
:int
)
231 (tcp-keepintvl tcp-keepintvl
:int
)
232 (tcp-linger2 tcp-linger2
:int
)
233 (tcp-quickack tcp-quickack
:bool
)
234 (tcp-syncnt tcp-syncnt
:int
)
235 (tcp-window-clamp tcp-window-clamp
:int
))
237 ;; TODO: implement "struct tcp_info" helper
238 ;; (define-socket-option tcp-info :get et::tcp-info et:ipproto-tcp :tcp-info :linux)
240 ;;;; FreeBSD-specific TCP Options
242 (define-socket-options :get-and-set ipproto-tcp
:freebsd
243 (tcp-noopt tcp-noopt
:bool
)
244 (tcp-nopush tcp-nopush
:bool
))