Minor fixes.
[iolib.git] / sockets / common.lisp
blob4d16ad086682470f795bc6c6a66c72d34718f0fb
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; common.lisp --- Various helpers for bsd-sockets.
4 ;;;
5 ;;; Copyright (C) 2006-2008, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
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
13 ;;;
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.
18 ;;;
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 ;;;; Types
28 (deftype ipv4-array () '(ub8-sarray 4))
29 (deftype ipv6-array () '(ub16-sarray 8))
31 ;;;; Byte-swap functions
33 (defun htons (short)
34 #+little-endian
35 (logior (ash (logand (the ub16 short) #x00FF) 8)
36 (ash (logand (the ub16 short) #xFF00) -8))
37 #+big-endian short)
39 (defun ntohs (short)
40 (htons short))
42 (defun htonl (long)
43 #+little-endian
44 (logior (ash (logand (the ub32 long) #x000000FF) 24)
45 (ash (logand (the ub32 long) #x0000FF00) 8)
46 (ash (logand (the ub32 long) #x00FF0000) -8)
47 (ash (logand (the ub32 long) #xFF000000) -24))
48 #+big-endian long)
50 (defun ntohl (long)
51 (htonl long))
53 ;;;; Conversion between address formats
55 (defun copy-simple-array-ub16-to-alien-vector (lisp-vec alien-vec)
56 (declare (type ipv6-array lisp-vec))
57 (dotimes (i 8)
58 (setf (mem-aref alien-vec :uint16 i)
59 (htons (aref lisp-vec i)))))
61 (defun map-ipv4-vector-to-ipv6 (addr)
62 (declare (type ipv4-array addr))
63 (let ((ipv6addr (make-array 8 :element-type 'ub16
64 :initial-element 0)))
65 ;; setting the IPv4 marker
66 (setf (aref ipv6addr 5) #xFFFF)
67 ;; setting the first two bytes
68 (setf (aref ipv6addr 6) (+ (ash (aref addr 0) 8)
69 (aref addr 1)))
70 ;; setting the last two bytes
71 (setf (aref ipv6addr 7) (+ (ash (aref addr 2) 8)
72 (aref addr 3)))
73 (values ipv6addr)))
75 (defun map-ipv6-vector-to-ipv4 (addr)
76 (declare (type ipv6-array addr))
77 (let ((ipv4addr (make-array 4 :element-type 'ub8
78 :initial-element 0)))
79 (setf (aref ipv4addr 0) (ldb (byte 8 8) (aref addr 6)))
80 (setf (aref ipv4addr 1) (ldb (byte 8 0) (aref addr 6)))
81 (setf (aref ipv4addr 2) (ldb (byte 8 8) (aref addr 7)))
82 (setf (aref ipv4addr 3) (ldb (byte 8 0) (aref addr 7)))
83 (values ipv4addr)))
85 ;;; From CLOCC's PORT library.
86 (defun vector-to-integer (vector)
87 "Convert a vector to a 32-bit unsigned integer."
88 (coercef vector 'ipv4-array)
89 (+ (ash (aref vector 0) 24)
90 (ash (aref vector 1) 16)
91 (ash (aref vector 2) 8)
92 (aref vector 3)))
94 (defun integer-to-vector (ipaddr)
95 "Convert a 32-bit unsigned integer to a vector."
96 (check-type ipaddr ub32 "an '(unsigned-byte 32)")
97 (let ((vector (make-array 4 :element-type 'ub8)))
98 (setf (aref vector 0) (ldb (byte 8 24) ipaddr)
99 (aref vector 1) (ldb (byte 8 16) ipaddr)
100 (aref vector 2) (ldb (byte 8 8) ipaddr)
101 (aref vector 3) (ldb (byte 8 0) ipaddr))
102 vector))
104 (defun in6-addr-to-ipv6-array (in6-addr)
105 (let ((vector (make-array 8 :element-type 'ub16)))
106 (dotimes (i 8)
107 (setf (aref vector i)
108 (ntohs (mem-aref in6-addr :uint16 i))))
109 vector))
111 ;;;; Constructors for SOCKADDR_* structs
113 (defun make-sockaddr-in (sin ub8-vector &optional (portno 0))
114 (declare (type ipv4-array ub8-vector) (type ub16 portno))
115 (bzero sin size-of-sockaddr-in)
116 (with-foreign-slots ((family addr port) sin sockaddr-in)
117 (setf family af-inet)
118 (setf addr (htonl (vector-to-integer ub8-vector)))
119 (setf port (htons portno)))
120 (values sin))
122 (defmacro with-sockaddr-in ((var address &optional (port 0)) &body body)
123 `(with-foreign-object (,var 'sockaddr-in)
124 (make-sockaddr-in ,var ,address ,port)
125 ,@body))
127 (defun make-sockaddr-in6 (sin6 ub16-vector &optional (portno 0))
128 (declare (type ipv6-array ub16-vector) (type ub16 portno))
129 (bzero sin6 size-of-sockaddr-in6)
130 (with-foreign-slots ((family addr port) sin6 sockaddr-in6)
131 (setf family af-inet6)
132 (copy-simple-array-ub16-to-alien-vector ub16-vector addr)
133 (setf port (htons portno)))
134 (values sin6))
136 (defmacro with-sockaddr-in6 ((var address &optional port) &body body)
137 `(with-foreign-object (,var 'sockaddr-in6)
138 (make-sockaddr-in6 ,var ,address ,port)
139 ,@body))
141 (defun make-sockaddr-un (sun string)
142 (declare (type string string))
143 (bzero sun size-of-sockaddr-un)
144 (with-foreign-slots ((family path) sun sockaddr-un)
145 (setf family af-local)
146 (with-foreign-string (c-string string)
147 (loop :for off :below (1- unix-path-max)
148 :do (setf (mem-aref path :uint8 off)
149 (mem-aref c-string :uint8 off)))))
150 (values sun))
152 (defmacro with-sockaddr-un ((var address) &body body)
153 `(with-foreign-object (,var 'sockaddr-un)
154 (make-sockaddr-un ,var ,address)
155 ,@body))
157 ;;;; Conversion functions for SOCKADDR_* structs
159 (defun sockaddr-in->sockaddr (sin)
160 (with-foreign-slots ((addr port) sin sockaddr-in)
161 (values (make-instance 'ipv4-address
162 :name (integer-to-vector (ntohl addr)))
163 (ntohs port))))
165 (defun sockaddr-in6->sockaddr (sin6)
166 (with-foreign-slots ((addr port) sin6 sockaddr-in6)
167 (values (make-instance 'ipv6-address
168 :name (in6-addr-to-ipv6-array addr))
169 (ntohs port))))
171 (defun sockaddr-un->sockaddr (sun)
172 (with-foreign-slots ((path) sun sockaddr-un)
173 (let ((name (make-string (1- unix-path-max)))
174 (abstract nil))
175 (cond ((zerop (mem-aref path :uint8 0))
176 ;; abstract address
177 (setf abstract t)
178 (loop :for sindex :from 0 :below (1- unix-path-max)
179 :for pindex :from 1 :below unix-path-max
180 :do (setf (schar name sindex)
181 (code-char (mem-aref path :uint8 pindex)))))
183 ;; address is in the filesystem
184 (setf name (foreign-string-to-lisp path))))
185 (make-instance 'local-address
186 :name name
187 :abstract abstract))))
189 (defun sockaddr-storage->sockaddr (ss)
190 (with-foreign-slots ((family) ss sockaddr-storage)
191 (ecase family
192 (#.af-inet (sockaddr-in->sockaddr ss))
193 (#.af-inet6 (sockaddr-in6->sockaddr ss))
194 (#.af-local (sockaddr-un->sockaddr ss)))))
196 (defun sockaddr->sockaddr-storage (ss sockaddr &optional (port 0))
197 (etypecase sockaddr
198 (ipv4-address (make-sockaddr-in ss (address-name sockaddr) port))
199 (ipv6-address (make-sockaddr-in6 ss (address-name sockaddr) port))
200 (local-address (make-sockaddr-un ss (address-name sockaddr)))))
202 ;;;; Misc
204 (defmacro check-bounds (sequence start end)
205 (with-gensyms (length)
206 `(let ((,length (length ,sequence)))
207 (unless ,end
208 (setq ,end ,length))
209 (unless (<= ,start ,end ,length)
210 (error "Wrong sequence bounds. start: ~S end: ~S" ,start ,end)))))
212 (defun %to-octets (buff ef start end)
213 (babel:string-to-octets buff :start start :end end
214 :encoding (babel:external-format-encoding ef)))
216 (defun parse-number-or-nil (value &optional (type :any) (radix 10))
217 (check-type value (or string unsigned-byte) "a string or an unsigned-byte")
218 (let ((parsed
219 (if (stringp value)
220 (ignore-errors (parse-integer value :radix radix
221 :junk-allowed nil))
222 value)))
223 (and parsed
224 ;; if it's a number and its type is ok return it
225 (typep parsed (ecase type
226 (:any t) (:ub8 'ub8)
227 (:ub16 'ub16) (:ub32 'ub32)))
228 (values parsed))))
230 (defun ensure-string-or-unsigned-byte (thing &optional (type :any) (radix 10))
231 (or (and (symbolp thing) (string-downcase thing))
232 (parse-number-or-nil thing type radix)
233 thing))
235 (defun lisp->c-bool (val)
236 (if val 1 0))
238 (defmacro with-socklen ((var value) &body body)
239 `(with-foreign-object (,var 'socklen)
240 (setf (mem-ref ,var 'socklen) ,value)
241 ,@body))
243 (defun memq (value list)
244 (member value list :test #'eq))
246 (defmacro multiple-value-case (values &body body)
247 (setf values (ensure-list values))
248 (assert values () "Must provide at least one value to test")
249 (labels ((%do-var (var val)
250 (cond
251 ((and (symbolp var) (string= var "_"))
253 ((consp var)
254 `(memq ,val ',var))
256 `(eq ,val ',var))))
257 (%do-clause (c)
258 (destructuring-bind ((&rest vals) &rest code) c
259 `((and ,@(mapcar #'%do-var vals values)) ,@code)))
260 (%do-last-clause (c)
261 (when c
262 (destructuring-bind (test &rest code) c
263 (if (member test '(otherwise t))
264 `((t ,@code))
265 `(,(%do-clause c)))))))
266 `(cond ,@(append (mapcar #'%do-clause (butlast body))
267 (%do-last-clause (car (last body)))))))