1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
3 ;;; address.lisp --- IP address classes.
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 ;;;; Class Definitions
29 ((name :initarg
:name
:reader address-name
:type vector
))
30 (:documentation
"Base class for all socket address classes."))
32 (defclass inet-address
(address) ()
33 (:documentation
"Base class for IPv4 and IPv6 addresses."))
35 (defclass ipv4-address
(inet-address) ()
36 (:documentation
"IPv4 address. Its low-level representation
37 can be accessed as vector of type IPV4-ARRAY through the
38 ADDRESS-NAME reader."))
40 (defclass ipv6-address
(inet-address) ()
41 (:documentation
"IPv6 address. Its low-level representation
42 can be accessed as vector of type IPV6-ARRAY through the
43 ADDRESS-NAME reader."))
45 (defclass local-address
(address)
46 ((abstract :initform nil
:initarg
:abstract
47 :reader abstract-address-p
:type boolean
))
48 (:documentation
"UNIX socket address."))
50 ;;;; Conversion functions
52 (defun integer-to-dotted (integer)
53 "Convert a 32-bit unsigned integer to a dotted string."
54 (check-type integer ub32
)
55 (format nil
"~A.~A.~A.~A"
56 (ldb (byte 8 24) integer
)
57 (ldb (byte 8 16) integer
)
58 (ldb (byte 8 8) integer
)
59 (ldb (byte 8 0) integer
)))
61 (defun dotted-to-vector (address)
62 "Convert a dotted IPv4 address to a (simple-array (unsigned-byte 8) 4)."
63 (check-type address string
)
64 (let ((addr (make-array 4 :element-type
'ub8
:initial-element
0))
65 (split (split-sequence #\. address
:count
5)))
66 (flet ((set-array-value (index str
)
67 (setf (aref addr index
)
68 (or (parse-number-or-nil str
:ub8
)
69 (error 'parse-error
)))))
70 (let ((len (length split
)))
73 (set-array-value 3 (nth (1- len
) split
))
74 (loop :for n
:in split
75 :for index
:below
(1- len
)
76 :do
(set-array-value index n
))))
79 (defun dotted-to-integer (address)
80 "Convert a dotted IPv4 address to a 32-bit unsigned integer."
81 (vector-to-integer (dotted-to-vector address
)))
83 (defun vector-to-dotted (vector)
84 "Convert an 4-element vector to a dotted string."
85 (coercef vector
'ipv4-array
)
86 (let ((*print-pretty
* nil
))
87 (with-output-to-string (s)
88 (princ (aref vector
0) s
) (princ #\. s
)
89 (princ (aref vector
1) s
) (princ #\. s
)
90 (princ (aref vector
2) s
) (princ #\. s
)
91 (princ (aref vector
3) s
))))
93 ;;; TODO: add tests against inet_pton(). Optimize if necessary.
94 ;;; <http://java.sun.com/javase/6/docs/api/java/net/Inet6Address.html#format>
95 (defun colon-separated-to-vector (string)
96 "Convert a colon-separated IPv6 address to a (simple-array ub16 8)."
97 (check-type string string
)
98 (when (< (length string
) 2)
100 (flet ((handle-trailing-and-leading-colons (string)
102 (end (length string
))
104 (trailing-colon-p nil
)
105 (tokens-from-leading-or-trailing-zeros 0))
106 (when (char= #\
: (char string
0))
108 (unless (char= #\
: (char string
1))
110 (setq tokens-from-leading-or-trailing-zeros
1)))
111 (when (char= #\
: (char string
(- end
1)))
112 (setq trailing-colon-p t
)
113 (unless (char= #\
: (char string
(- end
2)))
114 (incf tokens-from-leading-or-trailing-zeros
))
116 (values start end start-i trailing-colon-p
117 tokens-from-leading-or-trailing-zeros
)))
119 (= 0 (length string
)))
120 ;; we need to use this instead of dotted-to-vector because
121 ;; abbreviated IPv4 addresses are invalid in this context.
122 (ipv4-string-to-ub16-list (string)
123 (let ((tokens (split-sequence #\. string
)))
124 (when (= (length tokens
) 4)
125 (let ((ipv4 (map 'vector
127 (let ((x (ignore-errors
128 (parse-integer string
))))
129 (if (or (null x
) (not (<= 0 x
#xff
)))
133 (list (dpb (aref ipv4
0) (byte 8 8) (aref ipv4
1))
134 (dpb (aref ipv4
2) (byte 8 8) (aref ipv4
3)))))))
135 (parse-hex-ub16 (string)
136 (let ((x (ignore-errors (parse-integer string
:radix
16))))
137 (if (or (null x
) (not (<= 0 x
#xffff
)))
140 (multiple-value-bind (start end start-i trailing-colon-p extra-tokens
)
141 (handle-trailing-and-leading-colons string
)
142 (let* ((vector (make-array 8 :element-type
'ub16
:initial-element
0))
143 (tokens (split-sequence #\
: string
:start start
:end end
))
144 (empty-tokens (count-if #'emptyp tokens
))
145 (token-count (+ (length tokens
) extra-tokens
)))
146 (unless trailing-colon-p
147 (let ((ipv4 (ipv4-string-to-ub16-list (car (last tokens
)))))
150 (setq tokens
(nconc (butlast tokens
) ipv4
)))))
151 (when (or (> token-count
8) (> empty-tokens
1)
152 (and (zerop empty-tokens
) (/= token-count
8)))
153 (error 'parse-error
))
154 (loop for i from start-i and token in tokens do
156 ((integerp token
) (setf (aref vector i
) token
))
157 ((emptyp token
) (incf i
(- 8 token-count
)))
158 (t (setf (aref vector i
) (parse-hex-ub16 token
)))))
161 (defun ipv4-on-ipv6-mapped-vector-p (vector)
162 (and (dotimes (i 5 t
)
163 (when (plusp (aref vector i
))
165 (= (aref vector
5) #xffff
)))
167 (defun vector-to-colon-separated (vector &optional
(case :downcase
))
168 "Convert an 8-element vector to a colon-separated IPv6
169 address. CASE may be :DOWNCASE or :UPCASE."
170 (coercef vector
'ipv6-array
)
171 (check-type case
(member :upcase
:downcase
))
172 (labels ((find-zeros ()
173 (loop :for i
:from
0 :upto
6
174 :if
(and (zerop (aref vector i
))
175 (zerop (aref vector
(1+ i
))))
177 (values i
(or (position-if #'plusp vector
:start
(1+ i
))
179 (princ-subvec (start end s
)
180 (loop :for i
:from start
:below end
181 :do
(princ #\
: s
) (princ (aref vector i
) s
))))
182 (let ((s (make-string-output-stream)))
184 ((ipv4-on-ipv6-mapped-vector-p vector
)
186 (let ((*print-base
* 10))
187 (princ (ldb (byte 8 8) (aref vector
6)) s
) (princ #\. s
)
188 (princ (ldb (byte 8 0) (aref vector
6)) s
) (princ #\. s
)
189 (princ (ldb (byte 8 8) (aref vector
7)) s
) (princ #\. s
)
190 (princ (ldb (byte 8 0) (aref vector
7)) s
)))
192 (let ((*print-base
* 16))
193 (multiple-value-bind (start end
) (find-zeros)
196 (princ (aref vector
0) s
)
197 (princ-subvec 1 start s
))
200 (princ-subvec end
8 s
)
202 (t (princ (aref vector
0) s
)
203 (princ-subvec 1 8 s
)))))))
204 (let ((str (get-output-stream-string s
)))
206 (:downcase
(nstring-downcase str
))
207 (:upcase
(nstring-upcase str
)))))))
209 (defmacro ignore-parse-errors
(&body body
)
210 ;; return first value only
211 `(values (ignore-some-conditions (parse-error) ,@body
)))
213 (defun string-address-to-vector (address)
214 "Convert a string address (dotted or colon-separated) to a vector address.
215 If the string is not a valid address, return NIL."
216 (or (ignore-parse-errors (dotted-to-vector address
))
217 (ignore-parse-errors (colon-separated-to-vector address
))))
219 (defun address-to-vector (address)
220 "Convert any representation of an internet address to a vector.
221 Allowed inputs are: unsigned 32-bit integers, strings, vectors
222 and INET-ADDRESS objects. If the address is valid, two values
223 are returned: the vector and the address type (:IPV4 or IPV6),
224 otherwise NIL is returned."
228 (number (and (ignore-parse-errors
229 (setf vector
(integer-to-vector address
)))
230 (setf addr-type
:ipv4
)))
232 ((ignore-parse-errors (setf vector
(dotted-to-vector address
)))
233 (setf addr-type
:ipv4
))
234 ((ignore-parse-errors
235 (setf vector
(colon-separated-to-vector address
)))
236 (setf addr-type
:ipv6
))))
237 ((vector * 4) (and (ignore-parse-errors
238 (setf vector
(coerce address
'ipv4-array
)))
239 (setf addr-type
:ipv4
)))
240 ((vector * 8) (and (ignore-parse-errors
241 (setf vector
(coerce address
'ipv6-array
)))
242 (setf addr-type
:ipv6
)))
243 (ipv4-address (setf vector
(address-name address
)
245 (ipv6-address (setf vector
(address-name address
)
248 (values vector addr-type
))))
250 (defun ensure-address (address &optional
(family :internet
))
251 "If FAMILY is :LOCAL, a LOCAL-ADDRESS is instantiated with
252 ADDRESS as its NAME slot. If FAMILY is :INTERNET, an appropriate
253 subtype of INET-ADDRESS is instantiated after guessing the
254 address type through ADDRESS-TO-VECTOR. If the address is not
255 valid, a CL:PARSE-ERROR is signalled.
257 When ADDRESS is already an instance of the ADDRESS class, a check
258 is made to see if it matches the FAMILY argument and it is
259 returned unmodified."
263 (address (check-type address inet-address
) address
)
264 (t (make-address (or (address-to-vector address
)
265 (error 'parse-error
))))))
268 (string (make-instance 'local-address
:name address
))
269 (address (check-type address local-address
) address
)))))
273 (defgeneric address-to-string
(address)
274 (:documentation
"Returns a textual presentation of ADDRESS."))
276 (defmethod address-to-string ((address ipv4-address
))
277 (vector-to-dotted (address-name address
)))
279 (defmethod address-to-string ((address ipv6-address
))
280 (vector-to-colon-separated (address-name address
)))
282 (defmethod address-to-string ((address local-address
))
283 (if (abstract-address-p address
)
285 (address-name address
)))
287 (defmethod print-object ((address ipv4-address
) stream
)
288 (print-unreadable-object (address stream
:type nil
:identity nil
)
289 (format stream
"IPv4 address: ~A" (address-to-string address
))))
291 (defmethod print-object ((address ipv6-address
) stream
)
292 (print-unreadable-object (address stream
:type nil
:identity nil
)
293 (format stream
"IPv6 address: ~A" (address-to-string address
))))
295 (defmethod print-object ((address local-address
) stream
)
296 (print-unreadable-object (address stream
:type nil
:identity nil
)
297 (format stream
"Unix socket address: ~A. Abstract: ~:[no~;yes~]"
298 (address-to-string address
) (abstract-address-p address
))))
300 ;;;; Equality Methods
302 (defun vector-equal (v1 v2
)
303 (and (= (length v1
) (length v2
))
304 (every #'eql v1 v2
)))
306 (defgeneric address
= (addr1 addr2
)
307 (:documentation
"Returns T if both arguments are the same socket address."))
309 (defmethod address= ((addr1 inet-address
) (addr2 inet-address
))
310 (vector-equal (address-name addr1
) (address-name addr2
)))
312 (defmethod address= ((addr1 local-address
) (addr2 local-address
))
313 (equal (address-name addr1
) (address-name addr2
)))
315 (defun address-equal-p (addr1 addr2
&optional
(family :internet
))
316 "Returns T if both arguments are designators for the same socket address."
317 (address= (ensure-address addr1 family
)
318 (ensure-address addr2 family
)))
322 (defgeneric copy-address
(address)
324 "Returns a copy of ADDRESS which is ADDRESS= to the original."))
326 (defmethod copy-address ((addr ipv4-address
))
327 (make-instance 'ipv4-address
:name
(copy-seq (address-name addr
))))
329 (defmethod copy-address ((addr ipv6-address
))
330 (make-instance 'ipv6-address
:name
(copy-seq (address-name addr
))))
332 (defmethod copy-address ((addr local-address
))
333 (make-instance 'local-address
334 :name
(copy-seq (address-name addr
))
335 :abstract
(abstract-address-p addr
)))
337 ;;; Returns an IPv6 address by mapping ADDR onto it.
338 (defun map-ipv4-address-to-ipv6 (address)
339 (make-instance 'ipv6-address
340 :name
(map-ipv4-vector-to-ipv6 (address-name address
))))
344 (defun make-address (name)
345 "Constructs an ADDRESS object. NAME should be of type
346 IPV4-ARRAY, IPV6-ARRAY or STRING in which case an instance of
347 IPV4-ADDRESS, IPV6-ADDRESS or LOCAL-ADDRESS, respectively, will
348 be created. Otherwise, a TYPE-ERROR is signalled. See also
351 ((ignore-errors (coercef name
'ipv4-array
))
352 (make-instance 'ipv4-address
:name name
))
353 ((ignore-errors (coercef name
'ipv6-array
))
354 (make-instance 'ipv6-address
:name name
))
355 ((stringp name
) (make-instance 'local-address
:name name
))
356 (t (error 'type-error
:datum name
357 :expected-type
'(or string ipv4-array ipv6-array
)))))