1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
4 ; Copyright (C) 2006,2007 by Stelian Ionescu ;
6 ; This program is free software; you can redistribute it and/or modify ;
7 ; it under the terms of the GNU General Public License as published by ;
8 ; the Free Software Foundation; either version 2 of the License, or ;
9 ; (at your option) any later version. ;
11 ; This program is distributed in the hope that it will be useful, ;
12 ; but WITHOUT ANY WARRANTY; without even the implied warranty of ;
13 ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;
14 ; GNU General Public License for more details. ;
16 ; You should have received a copy of the GNU General Public License ;
17 ; along with this program; if not, write to the ;
18 ; Free Software Foundation, Inc., ;
19 ; 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA ;
20 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 ;; (declaim (optimize (speed 2) (safety 2) (space 1) (debug 2)))
23 (declaim (optimize (speed 0) (safety 2) (space 0) (debug 2)))
25 (in-package :net.sockets
)
27 ;; TODO: manage socket options errors
28 (defun sockopt-error (retval level option action
&optional val1 val2
)
29 (declare (type symbol action
))
30 (error "Sockopt error !"))
36 (defun set-socket-option-bool (fd level option value
)
37 (with-foreign-object (optval :int
)
38 (setf (mem-ref optval
:int
) (lisp->c-bool value
))
39 (et:setsockopt fd level option optval et
:size-of-int
)
42 (defun set-socket-option-int (fd level option value
)
43 (with-foreign-object (optval :int
)
44 (setf (mem-ref optval
:int
) value
)
45 (et:setsockopt fd level option optval et
:size-of-int
)
48 (defun set-socket-option-linger (fd level option onoff linger
)
49 (with-foreign-object (optval 'et
:linger
)
50 (with-foreign-slots ((et:linger et
:onoff
) optval et
:linger
)
51 (setf et
:onoff
(lisp->c-bool onoff
)
53 (et:setsockopt fd level option optval et
:size-of-linger
)
56 (defun set-socket-option-timeval (fd level option sec usec
)
57 (with-foreign-object (optval 'et
:timeval
)
58 (with-foreign-slots ((et:tv-sec et
:tv-usec
) optval et
:timeval
)
61 (et:setsockopt fd level option optval et
:size-of-timeval
)
68 (defun get-socket-option-bool (fd level option
)
69 (with-foreign-objects ((optval :int
)
71 (setf (mem-ref optlen
:socklen
) et
:size-of-int
)
72 (et:getsockopt fd level option optval optlen
)
73 (values (c->lisp-bool
(mem-ref optval
:int
)))))
75 (defun get-socket-option-int (fd level option
)
76 (with-foreign-objects ((optval :int
)
78 (setf (mem-ref optlen
:socklen
) et
:size-of-int
)
79 (et:getsockopt fd level option optval optlen
)
80 (values (mem-ref optval
:int
))))
82 (defun get-socket-option-linger (fd level option
)
83 (with-foreign-objects ((optval 'et
:linger
)
85 (setf (mem-ref optlen
:socklen
) et
:size-of-linger
)
86 (et:getsockopt fd level option optval optlen
)
87 (with-foreign-slots ((et:linger et
:onoff
) optval et
:linger
)
88 (values (c->lisp-bool et
:onoff
) et
:linger
))))
90 (defun get-socket-option-timeval (fd level option
)
91 (with-foreign-objects ((optval 'et
:timeval
)
93 (setf (mem-ref optlen
:socklen
) et
:size-of-timeval
)
94 (et:getsockopt fd level option optval optlen
)
95 (with-foreign-slots ((et:tv-sec et
:tv-usec
) optval et
:timeval
)
96 (values et
:tv-sec et
:tv-usec
))))
100 ;; Option definitions
103 (eval-when (:compile-toplevel
:load-toplevel
:execute
)
104 (defun make-sockopt-helper-name (action value-type
)
105 (iolib-utils:concat-symbol action
109 (defparameter +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 (member os
*features
*)
118 `(with-socket-error-filter
119 (,helper-get
(socket-fd socket
) ,level
,optname
))
120 `(error 'option-not-available option-name
))))
122 (defmacro define-set-sockopt
(os eql-name args helper-set level optname
)
123 `(defmethod set-socket-option ((socket socket
) (option-name (eql ,eql-name
)) &key
,@args
)
124 ,@(if (member os
*features
*)
125 `((with-socket-error-filter
126 (,helper-set
(socket-fd socket
) ,level
,optname
,@args
)))
127 `((declare (ignore ,@args
))
128 (error 'option-not-available option-name
)))))
130 (defmacro define-socket-option
(name action optname level argtype os
)
131 (declare (type symbol action
)
132 (type symbol argtype
)
134 (let ((eql-name (iolib-utils:ensure-keyword name
))
135 (args (second (assoc argtype
+helper-args-map
+)))
136 (helper-get (make-sockopt-helper-name :get argtype
))
137 (helper-set (make-sockopt-helper-name :set argtype
)))
141 (when (member action
(list :get
:get-and-set
))
142 `(define-get-sockopt ,os
,eql-name
,helper-get
,level
,optname
))
143 (when (member action
(list :set
:get-and-set
))
144 `(define-set-sockopt ,os
,eql-name
,args
,helper-set
,level
,optname
)))))))
146 ;;;;;;;;;;;;;;;;;;;;;
147 ;; Generic options ;;
148 ;;;;;;;;;;;;;;;;;;;;;
149 (define-socket-option accept-connections
:get et
::so-acceptconn et
::sol-socket
:bool
:unix
)
150 (define-socket-option broadcast
:get-and-set et
::so-broadcast et
::sol-socket
:bool
:unix
)
151 (define-socket-option debug
:get-and-set et
::so-debug et
::sol-socket
:bool
:unix
)
152 (define-socket-option dont-route
:get-and-set et
::so-dontroute et
::sol-socket
:bool
:unix
)
153 (define-socket-option error
:get et
::so-error et
::sol-socket
:int
:unix
)
154 (define-socket-option keep-alive
:get-and-set et
::so-keepalive et
::sol-socket
:bool
:unix
)
155 (define-socket-option linger
:get-and-set et
::so-linger et
::sol-socket
:linger
:unix
)
156 (define-socket-option oob-inline
:get-and-set et
::so-oobinline et
::sol-socket
:bool
:unix
)
157 (define-socket-option receive-buffer
:get-and-set et
::so-rcvbuf et
::sol-socket
:int
:unix
)
158 (define-socket-option send-buffer
:get-and-set et
::so-sndbuf et
::sol-socket
:int
:unix
)
159 (define-socket-option receive-low-water
:get-and-set et
::so-rcvlowat et
::sol-socket
:int
:unix
)
160 (define-socket-option send-low-water
:get-and-set et
::so-sndlowat et
::sol-socket
:int
:unix
)
161 (define-socket-option receive-timeout
:get-and-set et
::so-rcvtimeo et
::sol-socket
:timeval
:unix
)
162 (define-socket-option send-timeout
:get-and-set et
::so-sndtimeo et
::sol-socket
:timeval
:unix
)
163 (define-socket-option reuse-address
:get-and-set et
::so-reuseaddr et
::sol-socket
:bool
:unix
)
164 (define-socket-option type
:get et
::so-type et
::sol-socket
:int
:unix
)
166 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
167 ;; Linux-specific options ;;
168 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;
169 (define-socket-option bsd-compatible
:set et
::so-bsdcompat et
::sol-socket
:bool
:linux
)
170 (define-socket-option bind-to-device
:set et
::so-bindtodevice et
::sol-socket
:int
:linux
)
171 (define-socket-option priority
:get-and-set et
::so-priority et
::sol-socket
:int
:linux
)
173 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
174 ;; FreeBSD-specific options ;;
175 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
176 (define-socket-option reuse-port
:get-and-set et
::so-reuseport et
::sol-socket
:bool
:freebsd
)
177 (define-socket-option use-loopback
:get-and-set et
::so-useloopback et
::sol-socket
:bool
:freebsd
)
178 (define-socket-option no-sigpipe
:get-and-set et
::so-nosigpipe et
::sol-socket
:bool
:freebsd
)
181 ;;;;;;;;;;;;;;;;;;;;;;;;
183 ;;; Still to be done ;;;
185 ;;;;;;;;;;;;;;;;;;;;;;;;
187 ;; TODO: implement "struct ucred" helpers
189 ;; (define-socket-option pass-credentials :get-and-set et::so-passcred et::sol-socket :ucred :freebsd)
190 ;; (define-socket-option peer-credentials :get et::so-peercred et::sol-socket :ucred :freebsd)
193 ;; TODO: implement "struct accept_filter_arg" helpers
195 ;; (define-socket-option accept-filter :get-and-set et::so-acceptfilter et::sol-socket :accept-filter :freebsd)
197 ;; TODO: find out the types of these options
199 ;; (define-socket-option bintime :get-and-set et::so-bintime et::sol-socket :bool :freebsd)
200 ;; (define-socket-option label :get-and-set et::so-label et::sol-socket :bool :freebsd)
201 ;; (define-socket-option peerlabel :get-and-set et::so-peerlabel et::sol-socket :bool :freebsd)
202 ;; (define-socket-option listen-queue-limit :get-and-set et::so-listenqlimit et::sol-socket :int :freebsd)
203 ;; (define-socket-option listen-queue-length :get-and-set et::so-listenqlen et::sol-socket :int :freebsd)
204 ;; (define-socket-option listen-incomplete-queue-length :get-and-set et::so-listenincqlen et::sol-socket :int :freebsd)