1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
3 ;;; common.lisp --- Various helpers for bsd-sockets.
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
)
28 (deftype ipv4-array
() '(ub8-sarray 4))
29 (deftype ipv6-array
() '(ub16-sarray 8))
31 ;;;; Byte-swap functions
34 (check-type short ub16
"a 16-bit unsigned number")
36 (logior (ash (logand (the ub16 short
) #x00FF
) 8)
37 (ash (logand (the ub16 short
) #xFF00
) -
8))
44 (check-type long ub32
"a 32-bit unsigned number")
46 (logior (ash (logand (the ub32 long
) #x000000FF
) 24)
47 (ash (logand (the ub32 long
) #x0000FF00
) 8)
48 (ash (logand (the ub32 long
) #x00FF0000
) -
8)
49 (ash (logand (the ub32 long
) #xFF000000
) -
24))
55 ;;;; Conversion between address formats
57 (defun copy-simple-array-ub16-to-alien-vector (lisp-vec alien-vec
)
58 (declare (type ipv6-array lisp-vec
))
60 (setf (mem-aref alien-vec
:uint16 i
)
61 (htons (aref lisp-vec i
)))))
63 (defun map-ipv4-vector-to-ipv6 (addr)
64 (declare (type ipv4-array addr
))
65 (let ((ipv6addr (make-array 8 :element-type
'ub16
67 ;; setting the IPv4 marker
68 (setf (aref ipv6addr
5) #xFFFF
)
69 ;; setting the first two bytes
70 (setf (aref ipv6addr
6) (+ (ash (aref addr
0) 8)
72 ;; setting the last two bytes
73 (setf (aref ipv6addr
7) (+ (ash (aref addr
2) 8)
77 ;;; From CLOCC's PORT library.
78 (defun vector-to-integer (vector)
79 "Convert a vector to a 32-bit unsigned integer."
80 (coercef vector
'ipv4-array
)
81 (+ (ash (aref vector
0) 24)
82 (ash (aref vector
1) 16)
83 (ash (aref vector
2) 8)
86 (defun integer-to-vector (ipaddr)
87 "Convert a 32-bit unsigned integer to a vector."
88 (check-type ipaddr ub32
)
89 (let ((vector (make-array 4 :element-type
'ub8
)))
90 (setf (aref vector
0) (ldb (byte 8 24) ipaddr
)
91 (aref vector
1) (ldb (byte 8 16) ipaddr
)
92 (aref vector
2) (ldb (byte 8 8) ipaddr
)
93 (aref vector
3) (ldb (byte 8 0) ipaddr
))
96 (defun in6-addr-to-ipv6-array (in6-addr)
97 (let ((vector (make-array 8 :element-type
'ub16
)))
100 (ntohs (mem-aref in6-addr
:uint16 i
))))
103 ;;;; Constructors for SOCKADDR_* structs
105 (defun make-sockaddr-in (sin ub8-vector
&optional
(portno 0))
106 (declare (type ipv4-array ub8-vector
) (type ub16 portno
))
107 (bzero sin size-of-sockaddr-in
)
108 (with-foreign-slots ((family addr port
) sin sockaddr-in
)
109 (setf family af-inet
)
110 (setf addr
(htonl (vector-to-integer ub8-vector
)))
111 (setf port
(htons portno
)))
114 (defmacro with-sockaddr-in
((var address
&optional
(port 0)) &body body
)
115 `(with-foreign-object (,var
'sockaddr-in
)
116 (make-sockaddr-in ,var
,address
,port
)
119 (defun make-sockaddr-in6 (sin6 ub16-vector
&optional
(portno 0))
120 (declare (type ipv6-array ub16-vector
) (type ub16 portno
))
121 (bzero sin6 size-of-sockaddr-in6
)
122 (with-foreign-slots ((family addr port
) sin6 sockaddr-in6
)
123 (setf family af-inet6
)
124 (copy-simple-array-ub16-to-alien-vector ub16-vector addr
)
125 (setf port
(htons portno
)))
128 (defmacro with-sockaddr-in6
((var address
&optional port
) &body body
)
129 `(with-foreign-object (,var
'sockaddr-in6
)
130 (make-sockaddr-in6 ,var
,address
,port
)
134 (defun make-sockaddr-un (sun string
)
135 (declare (type string string
))
136 (bzero sun size-of-sockaddr-un
)
137 (with-foreign-slots ((family path
) sun sockaddr-un
)
138 (setf family af-local
)
139 (with-foreign-string (c-string string
)
140 (loop :for off
:below
(1- unix-path-max
)
141 :do
(setf (mem-aref path
:uint8 off
)
142 (mem-aref c-string
:uint8 off
)))))
146 (defmacro with-sockaddr-un
((var address
) &body body
)
147 `(with-foreign-object (,var
'sockaddr-un
)
148 (make-sockaddr-un ,var
,address
)
151 ;;;; Conversion functions for SOCKADDR_* structs
153 (defun sockaddr-in->sockaddr
(sin)
154 (with-foreign-slots ((addr port
) sin sockaddr-in
)
155 (values (make-instance 'ipv4-address
156 :name
(integer-to-vector (ntohl addr
)))
159 (defun sockaddr-in6->sockaddr
(sin6)
160 (with-foreign-slots ((addr port
) sin6 sockaddr-in6
)
161 (values (make-instance 'ipv6-address
162 :name
(in6-addr-to-ipv6-array addr
))
166 (defun sockaddr-un->sockaddr
(sun)
167 (with-foreign-slots ((path) sun sockaddr-un
)
168 (let ((name (make-string (1- unix-path-max
)))
170 (if (zerop (mem-aref path
:uint8
0))
174 (loop :for sindex
:from
0 :below
(1- unix-path-max
)
175 :for pindex
:from
1 :below unix-path-max
176 :do
(setf (schar name sindex
)
177 (code-char (mem-aref path
:uint8 pindex
)))))
178 ;; address is in the filesystem
179 (setf name
(foreign-string-to-lisp path
)))
180 (make-instance 'local-address
182 :abstract abstract
))))
184 (defun sockaddr-storage->sockaddr
(ss)
185 (with-foreign-slots ((family) ss sockaddr-storage
)
187 (#.af-inet
(sockaddr-in->sockaddr ss
))
188 (#.af-inet6
(sockaddr-in6->sockaddr ss
))
189 #-windows
(#.af-local
(sockaddr-un->sockaddr ss
)))))
191 (defun sockaddr->sockaddr-storage
(ss sockaddr
&optional
(port 0))
193 (ipv4-address (make-sockaddr-in ss
(address-name sockaddr
) port
))
194 (ipv6-address (make-sockaddr-in6 ss
(address-name sockaddr
) port
))
195 #-windows
(local-address (make-sockaddr-un ss
(address-name sockaddr
)))))
199 (defmacro check-bounds
(sequence start end
)
200 (with-unique-names (length)
201 `(let ((,length
(length ,sequence
)))
204 (unless (<= ,start
,end
,length
)
205 (error "Wrong sequence bounds. start: ~S end: ~S" ,start
,end
)))))
207 (defun %to-octets
(buff ef start end
)
208 (babel:string-to-octets buff
:start start
:end end
209 :encoding
(babel:external-format-encoding ef
)))
211 (defun parse-number-or-nil (value &optional
(type :any
) (radix 10))
212 (check-type value
(or string unsigned-byte
))
215 (ignore-errors (parse-integer value
:radix radix
219 ;; if it's a number and its type is ok return it
220 (typep parsed
(ecase type
222 (:ub16
'ub16
) (:ub32
'ub32
)))
225 (defun lisp->c-bool
(val)
228 (defmacro with-socklen
((var value
) &body body
)
229 `(with-foreign-object (,var
'socklen
)
230 (setf (mem-ref ,var
'socklen
) ,value
)