1 ;;;; -*- Mode: Lisp; indent-tabs-mode: nil -*-
3 ;;; --- IP address classes and main methods.
6 (in-package :iolib.sockets
)
12 (:documentation
"Base class for all socket address classes."))
14 (defclass named-address
()
15 ((name :initarg
:name
:reader address-name
:type vector
))
16 (:documentation
"Base class for socket address with a name."))
18 (defclass inet-address
(named-address) ()
19 (:documentation
"Base class for IPv4 and IPv6 addresses."))
21 (defclass ipv4-address
(inet-address) ()
22 (:documentation
"IPv4 address. Its low-level representation
23 can be accessed as vector of type IPV4-ARRAY through the
24 ADDRESS-NAME reader."))
26 (defclass ipv6-address
(inet-address) ()
27 (:documentation
"IPv6 address. Its low-level representation
28 can be accessed as vector of type IPV6-ARRAY through the
29 ADDRESS-NAME reader."))
31 (defclass local-address
(named-address)
32 ((abstract :initform nil
:initarg
:abstract
33 :reader abstract-address-p
:type boolean
))
34 (:documentation
"UNIX socket address."))
35 (unset-method-docstring #'abstract-address-p
() '(local-address))
36 (set-function-docstring 'abstract-address-p
"Return T if ADDRESS is a LOCAL-ADDRESS that lives in the abstract namespace.")
39 (defclass netlink-address
(address)
40 ((multicast-groups :initform
0 :initarg
:multicast-groups
41 :reader netlink-address-multicast-groups
)))
43 (defmethod initialize-instance :after
((address local-address
) &key
)
44 (with-slots (name) address
47 (pathname (setf name
(namestring name
))))))
49 (defmethod make-load-form ((address inet-address
) &optional env
)
50 (declare (ignore env
))
51 `(make-instance ,(class-of address
)
52 :name
,(address-name address
)))
54 (defmethod make-load-form ((address local-address
) &optional env
)
55 (declare (ignore env
))
56 `(make-instance ,(class-of address
)
57 :name
,(address-name address
)
58 :abstract
,(abstract-address-p address
)))
60 ;;;; Conversion functions for SOCKADDR_* structs
62 (defun sockaddr-in->sockaddr
(sin)
63 (with-foreign-slots ((addr port
) sin sockaddr-in
)
64 (values (make-instance 'ipv4-address
65 :name
(integer-to-vector (ntohl addr
)))
68 (defun sockaddr-in6->sockaddr
(sin6)
69 (with-foreign-slots ((addr port
) sin6 sockaddr-in6
)
70 (values (make-instance 'ipv6-address
71 :name
(in6-addr-to-ipv6-array addr
))
74 (defun parse-un-path (path)
75 (foreign-string-to-lisp path
:max-chars
(1- unix-path-max
)))
77 (defun sockaddr-un->sockaddr
(sun)
78 (with-foreign-slots ((path) sun sockaddr-un
)
79 (multiple-value-bind (name abstract
)
80 (if (zerop (mem-aref path
:uint8
0))
81 (values (parse-un-path (inc-pointer path
1)) t
)
82 (values (parse-un-path path
) nil
))
83 (make-instance 'local-address
:name name
:abstract abstract
))))
86 (defun sockaddr-nl->sockaddr
(snl)
87 (with-foreign-slots ((groups port
) snl sockaddr-nl
)
88 (values (make-instance 'netlink-address
:multicast-groups groups
)
91 (defun sockaddr-storage->sockaddr
(ss)
92 (with-foreign-slots ((family) ss sockaddr-storage
)
93 (switch (family :test
#'=)
94 (af-inet (sockaddr-in->sockaddr ss
))
95 (af-inet6 (sockaddr-in6->sockaddr ss
))
96 (af-local (sockaddr-un->sockaddr ss
))
98 (af-netlink (sockaddr-nl->sockaddr ss
)))))
100 (defun sockaddr->sockaddr-storage
(ss sockaddr
&optional
(port 0))
102 (ipv4-address (make-sockaddr-in ss
(address-name sockaddr
) port
))
103 (ipv6-address (make-sockaddr-in6 ss
(address-name sockaddr
) port
))
104 (local-address (make-sockaddr-un ss
(address-name sockaddr
)
105 (abstract-address-p sockaddr
)))
107 (netlink-address (make-sockaddr-nl ss
(multicast-groups sockaddr
) port
))))
109 (defun sockaddr-size (ss)
110 (with-foreign-slots ((family) ss sockaddr-storage
)
111 (switch (family :test
#'=)
112 (af-inet (isys:sizeof
'sockaddr-in
))
113 (af-inet6 (isys:sizeof
'sockaddr-in6
))
114 (af-local (isys:sizeof
'sockaddr-un
))
116 (af-netlink (isys:sizeof
'sockaddr-nl
)))))
118 ;;;; Conversion functions
120 (defun integer-to-dotted (integer)
121 "Convert an (UNSIGNED-BYTE 32) IPv4 address to a dotted string."
122 (check-type integer ub32
"an '(unsigned-byte 32)")
123 (let ((*print-pretty
* nil
) (*print-base
* 10))
124 (format nil
"~A.~A.~A.~A"
125 (ldb (byte 8 24) integer
)
126 (ldb (byte 8 16) integer
)
127 (ldb (byte 8 8) integer
)
128 (ldb (byte 8 0) integer
))))
130 (defun dotted-to-vector (address)
131 "Convert a dotted IPv4 address to a (SIMPLE-ARRAY (UNSIGNED-BYTE 8) 4)."
132 (check-type address string
"a string")
133 (let ((addr (make-array 4 :element-type
'ub8
:initial-element
0))
134 (split (split-sequence #\. address
:count
5)))
135 (flet ((set-array-value (index str
)
136 (setf (aref addr index
)
137 (ensure-number str
:type
'ub8
))))
138 (let ((len (length split
)))
140 (error 'parse-error
))
141 (set-array-value 3 (nth (1- len
) split
))
142 (loop :for n
:in split
143 :for index
:below
(1- len
)
144 :do
(set-array-value index n
))))
147 (defun dotted-to-integer (address)
148 "Convert a dotted IPv4 address to an (UNSIGNED-BYTE 32)."
149 (vector-to-integer (dotted-to-vector address
)))
151 (defun vector-to-dotted (vector)
152 "Convert an 4-element vector to a dotted string."
153 (coercef vector
'ipv4-array
)
154 (let ((*print-pretty
* nil
) (*print-base
* 10))
155 (with-output-to-string (s)
156 (princ (aref vector
0) s
) (princ #\. s
)
157 (princ (aref vector
1) s
) (princ #\. s
)
158 (princ (aref vector
2) s
) (princ #\. s
)
159 (princ (aref vector
3) s
))))
161 ;;; TODO: add tests against inet_pton(). Optimize if necessary.
162 ;;; <http://java.sun.com/javase/6/docs/api/java/net/Inet6Address.html#format>
163 (defun colon-separated-to-vector (string)
164 "Convert a colon-separated IPv6 address to a (SIMPLE-ARRAY (UNSIGNED-BYTE 16) 8)."
165 (check-type string string
"a string")
166 (when (< (length string
) 2)
167 (error 'parse-error
))
168 (flet ((handle-trailing-and-leading-colons (string)
170 (end (length string
))
172 (trailing-colon-p nil
)
173 (tokens-from-leading-or-trailing-zeros 0))
174 (when (char= #\
: (char string
0))
176 (unless (char= #\
: (char string
1))
178 (setq tokens-from-leading-or-trailing-zeros
1)))
179 (when (char= #\
: (char string
(- end
1)))
180 (setq trailing-colon-p t
)
181 (unless (char= #\
: (char string
(- end
2)))
182 (incf tokens-from-leading-or-trailing-zeros
))
184 (values start end start-i trailing-colon-p
185 tokens-from-leading-or-trailing-zeros
)))
186 ;; we need to use this instead of dotted-to-vector because
187 ;; abbreviated IPv4 addresses are invalid in this context.
188 (ipv4-string-to-ub16-list (string)
189 (let ((tokens (split-sequence #\. string
)))
190 (when (= (length tokens
) 4)
191 (let ((ipv4 (map 'vector
193 (let ((x (ignore-errors
194 (parse-integer string
))))
195 (if (or (null x
) (not (<= 0 x
#xff
)))
199 (list (dpb (aref ipv4
0) (byte 8 8) (aref ipv4
1))
200 (dpb (aref ipv4
2) (byte 8 8) (aref ipv4
3)))))))
201 (parse-hex-ub16 (string)
202 (ensure-number string
:type
'ub16
:radix
16)))
203 (multiple-value-bind (start end start-i trailing-colon-p extra-tokens
)
204 (handle-trailing-and-leading-colons string
)
205 (let* ((vector (make-array 8 :element-type
'ub16
:initial-element
0))
206 (tokens (split-sequence #\
: string
:start start
:end end
))
207 (empty-tokens (count-if #'emptyp tokens
))
208 (token-count (+ (length tokens
) extra-tokens
)))
209 (unless trailing-colon-p
210 (let ((ipv4 (ipv4-string-to-ub16-list (lastcar tokens
))))
213 (setq tokens
(nconc (butlast tokens
) ipv4
)))))
214 (when (or (> token-count
8) (> empty-tokens
1)
215 (and (zerop empty-tokens
) (/= token-count
8)))
216 (error 'parse-error
))
217 (loop for i from start-i and token in tokens do
219 ((integerp token
) (setf (aref vector i
) token
))
220 ((emptyp token
) (incf i
(- 8 token-count
)))
221 (t (setf (aref vector i
) (parse-hex-ub16 token
)))))
224 (defun ipv4-on-ipv6-mapped-vector-p (vector)
225 (and (dotimes (i 5 t
)
226 (when (plusp (aref vector i
))
228 (= (aref vector
5) #xffff
)))
230 (defun princ-ipv4-on-ipv6-mapped-address (vector s
)
232 (let ((*print-base
* 10) (*print-pretty
* nil
))
233 (princ (ldb (byte 8 8) (aref vector
6)) s
) (princ #\. s
)
234 (princ (ldb (byte 8 0) (aref vector
6)) s
) (princ #\. s
)
235 (princ (ldb (byte 8 8) (aref vector
7)) s
) (princ #\. s
)
236 (princ (ldb (byte 8 0) (aref vector
7)) s
)))
238 (defun vector-to-colon-separated (vector &optional
(case :downcase
))
239 "Convert an (SIMPLE-ARRAY (UNSIGNED-BYTE 16) 8) to a colon-separated IPv6
240 address. CASE may be :DOWNCASE or :UPCASE."
241 (coercef vector
'ipv6-array
)
242 (check-type case
(member :upcase
:downcase
) "either :UPCASE or :DOWNCASE")
243 (let ((s (make-string-output-stream)))
244 (flet ((find-zeros ()
245 (let ((start (position 0 vector
:start
1 :end
7)))
248 (position-if #'plusp vector
:start start
:end
7)))))
249 (princ-subvec (start end
)
250 (loop :for i
:from start
:below end
251 :do
(princ (aref vector i
) s
) (princ #\
: s
))))
253 ((ipv4-on-ipv6-mapped-vector-p vector
)
254 (princ-ipv4-on-ipv6-mapped-address vector s
))
256 (let ((*print-base
* 16) (*print-pretty
* nil
))
257 (when (plusp (aref vector
0)) (princ (aref vector
0) s
))
259 (multiple-value-bind (start end
) (find-zeros)
260 (cond (start (princ-subvec 1 start
)
262 (when end
(princ-subvec end
7)))
263 (t (princ-subvec 1 7))))
264 (when (plusp (aref vector
7)) (princ (aref vector
7) s
))))))
265 (let ((str (get-output-stream-string s
)))
267 (:downcase
(nstring-downcase str
))
268 (:upcase
(nstring-upcase str
))))))
270 (defmacro ignore-parse-errors
(&body body
)
271 ;; return first value only
272 `(values (ignore-some-conditions (parse-error) ,@body
)))
274 (defun string-address-to-vector (address)
275 "Convert a string address (dotted or colon-separated) to a vector address.
276 If the string is not a valid address, return NIL."
277 (or (ignore-parse-errors (dotted-to-vector address
))
278 (ignore-parse-errors (colon-separated-to-vector address
))))
280 (defun address-to-vector (address)
281 "Convert any representation of an internet address to a vector.
282 Allowed inputs are: unsigned 32-bit integers, strings, vectors
283 and INET-ADDRESS objects. If the address is valid, two values
284 are returned: the vector and the address type (:IPV4 or IPV6),
285 otherwise NIL is returned."
286 (let (vector addr-type
)
288 (number (and (ignore-parse-errors
289 (setf vector
(integer-to-vector address
)))
290 (setf addr-type
:ipv4
)))
292 ((ignore-parse-errors (setf vector
(dotted-to-vector address
)))
293 (setf addr-type
:ipv4
))
294 ((ignore-parse-errors
295 (setf vector
(colon-separated-to-vector address
)))
296 (setf addr-type
:ipv6
))))
297 ((vector * 4) (and (ignore-parse-errors
298 (setf vector
(coerce address
'ipv4-array
)))
299 (setf addr-type
:ipv4
)))
300 ((vector * 8) (and (ignore-parse-errors
301 (setf vector
(coerce address
'ipv6-array
)))
302 (setf addr-type
:ipv6
)))
303 (ipv4-address (setf vector
(copy-seq (address-name address
))
305 (ipv6-address (setf vector
(copy-seq (address-name address
))
308 (values vector addr-type
))))
310 (defun ensure-address (address &key
(family :internet
) abstract
(errorp t
))
311 "If FAMILY is :LOCAL, a LOCAL-ADDRESS is instantiated with
312 ADDRESS as its NAME slot. If FAMILY is :INTERNET, an appropriate
313 subtype of INET-ADDRESS is instantiated after guessing the
314 address type through ADDRESS-TO-VECTOR. If the address is invalid
315 and ERRORP is not NIL, then a CL:PARSE-ERROR is signalled,
316 otherwise NIL is returned.
318 When ADDRESS is already an instance of the ADDRESS class, a check
319 is made to see if it matches the FAMILY argument and it is
320 returned unmodified."
326 (check-type address inet-address
"an INET address"))
327 ((not (typep address
'inet-address
))
330 (t (let ((vector (address-to-vector address
)))
332 (vector (make-address vector
))
333 (errorp (error 'parse-error
)))))))
336 (string (make-instance 'local-address
:name address
:abstract abstract
))
339 (check-type address local-address
"a local address"))
340 ((not (typep address
'local-address
))
346 (defgeneric address-to-string
(address)
347 (:documentation
"Returns a textual presentation of ADDRESS."))
349 (defmethod address-to-string ((address ipv4-address
))
350 (vector-to-dotted (address-name address
)))
352 (defmethod address-to-string ((address ipv6-address
))
353 (vector-to-colon-separated (address-name address
)))
355 (defmethod address-to-string ((address local-address
))
356 (format nil
"~:[~;@~]~S" (abstract-address-p address
)
357 (address-name address
)))
360 (defmethod address-to-string ((address netlink-address
))
361 (format nil
"~A" (netlink-address-multicast-groups address
)))
363 (defmethod print-object ((address inet-address
) stream
)
364 (let ((namestring (address-to-string address
)))
365 (if (or *print-readably
* *print-escape
*)
366 (format stream
"#/~S/~A" 'ip namestring
)
367 (write-string namestring stream
))))
369 (defmethod print-object ((address local-address
) stream
)
370 (print-unreadable-object (address stream
:type nil
:identity nil
)
371 (format stream
"Unix socket address: ~A"
372 (address-to-string address
))))
375 (defmethod print-object ((address netlink-address
) stream
)
376 (print-unreadable-object (address stream
:type nil
:identity nil
)
377 (format stream
"Netlink socket address: ~A"
378 (address-to-string address
))))
382 (define-literal-reader ip
(stream)
383 (loop :with sstr
:= (make-string-output-stream)
384 :for char
:= (read-char stream nil nil
)
386 :do
(cond ((or (digit-char-p char
16)
387 (member char
'(#\.
#\
:) :test
#'char
=))
388 (write-char char sstr
))
390 (unread-char char stream
)
392 :finally
(return (or (ensure-address (get-output-stream-string sstr
)
394 (error 'reader-error
:stream stream
)))))
396 ;;;; Equality Methods
398 (defun vector-equal (v1 v2
)
399 (and (= (length v1
) (length v2
))
400 (every #'eql v1 v2
)))
402 (defgeneric address
= (addr1 addr2
)
403 (:documentation
"Returns T if both arguments are the same socket address."))
405 (defmethod address= ((addr1 inet-address
) (addr2 inet-address
))
406 (vector-equal (address-name addr1
) (address-name addr2
)))
408 (defmethod address= ((addr1 local-address
) (addr2 local-address
))
409 (equal (address-name addr1
) (address-name addr2
)))
411 (defun address-equal-p (addr1 addr2
&optional
(family :internet
))
412 "Returns T if both arguments are designators for the same socket address."
413 (address= (ensure-address addr1
:family family
)
414 (ensure-address addr2
:family family
)))
418 (defgeneric copy-address
(address)
420 "Returns a copy of ADDRESS which is ADDRESS= to the original."))
422 (defmethod copy-address ((addr ipv4-address
))
423 (make-instance 'ipv4-address
:name
(copy-seq (address-name addr
))))
425 (defmethod copy-address ((addr ipv6-address
))
426 (make-instance 'ipv6-address
:name
(copy-seq (address-name addr
))))
428 (defmethod copy-address ((addr local-address
))
429 (make-instance 'local-address
430 :name
(copy-seq (address-name addr
))
431 :abstract
(abstract-address-p addr
)))
433 (defun map-ipv4-address-to-ipv6 (address)
434 "Returns an IPv6 address by mapping ADDRESS onto it."
435 (make-instance 'ipv6-address
436 :name
(map-ipv4-vector-to-ipv6 (address-name address
))))
438 (defun map-ipv6-address-to-ipv4 (address)
439 "Extracts the IPv4 part of an IPv6-mapped IPv4 address.
440 Signals an error if ADDRESS is not an IPv6-mapped IPv4 address."
441 (assert (ipv6-ipv4-mapped-p address
) (address)
442 "Not an IPv6-mapped IPv4 address: ~A" address
)
443 (make-instance 'ipv4-address
444 :name
(map-ipv6-vector-to-ipv4 (address-name address
))))
448 (defun make-address (name)
449 "Constructs an ADDRESS object. NAME should be of type
450 IPV4-ARRAY, IPV6-ARRAY or STRING in which case an instance of
451 IPV4-ADDRESS, IPV6-ADDRESS or LOCAL-ADDRESS, respectively, will
452 be created. Otherwise, a TYPE-ERROR is signalled. See also
455 ((ignore-errors (coercef name
'ipv4-array
))
456 (make-instance 'ipv4-address
:name name
))
457 ((ignore-errors (coercef name
'ipv6-array
))
458 (make-instance 'ipv6-address
:name name
))
459 ((stringp name
) (make-instance 'local-address
:name name
))
460 (t (error 'type-error
:datum name
461 :expected-type
'(or string ipv4-array ipv6-array
)))))