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.")
38 (defclass netlink-address
(address)
39 ((multicast-groups :initform
0 :initarg
:multicast-groups
40 :reader netlink-address-multicast-groups
)))
42 (defmethod initialize-instance :after
((address local-address
) &key
)
43 (with-slots (name) address
46 (pathname (setf name
(namestring name
))))))
48 (defmethod make-load-form ((address inet-address
) &optional env
)
49 (declare (ignore env
))
50 `(make-instance ,(class-of address
)
51 :name
,(address-name address
)))
53 (defmethod make-load-form ((address local-address
) &optional env
)
54 (declare (ignore env
))
55 `(make-instance ,(class-of address
)
56 :name
,(address-name address
)
57 :abstract
,(abstract-address-p address
)))
59 ;;;; Conversion functions for SOCKADDR_* structs
61 (defun sockaddr-in->sockaddr
(sin)
62 (with-foreign-slots ((addr port
) sin sockaddr-in
)
63 (values (make-instance 'ipv4-address
64 :name
(integer-to-vector (ntohl addr
)))
67 (defun sockaddr-in6->sockaddr
(sin6)
68 (with-foreign-slots ((addr port
) sin6 sockaddr-in6
)
69 (values (make-instance 'ipv6-address
70 :name
(in6-addr-to-ipv6-array addr
))
73 (defun parse-un-path (path)
74 (foreign-string-to-lisp path
:max-chars
(1- unix-path-max
)))
76 (defun sockaddr-un->sockaddr
(sun)
77 (with-foreign-slots ((path) sun sockaddr-un
)
78 (multiple-value-bind (name abstract
)
79 (if (zerop (mem-aref path
:uint8
0))
80 (values (parse-un-path (inc-pointer path
1)) t
)
81 (values (parse-un-path path
) nil
))
82 (make-instance 'local-address
:name name
:abstract abstract
))))
84 (defun sockaddr-nl->sockaddr
(snl)
85 (with-foreign-slots ((groups port
) snl sockaddr-nl
)
86 (values (make-instance 'netlink-address
:multicast-groups groups
)
89 (defun sockaddr-storage->sockaddr
(ss)
90 (with-foreign-slots ((family) ss sockaddr-storage
)
91 (switch (family :test
#'=)
92 (af-inet (sockaddr-in->sockaddr ss
))
93 (af-inet6 (sockaddr-in6->sockaddr ss
))
94 (af-local (sockaddr-un->sockaddr ss
))
95 (af-netlink (sockaddr-nl->sockaddr ss
)))))
97 (defun sockaddr->sockaddr-storage
(ss sockaddr
&optional
(port 0))
99 (ipv4-address (make-sockaddr-in ss
(address-name sockaddr
) port
))
100 (ipv6-address (make-sockaddr-in6 ss
(address-name sockaddr
) port
))
101 (local-address (make-sockaddr-un ss
(address-name sockaddr
)
102 (abstract-address-p sockaddr
)))
103 (netlink-address (make-sockaddr-nl ss
(multicast-groups sockaddr
) port
))))
105 (defun sockaddr-size (ss)
106 (with-foreign-slots ((family) ss sockaddr-storage
)
107 (switch (family :test
#'=)
108 (af-inet (isys:sizeof
'sockaddr-in
))
109 (af-inet6 (isys:sizeof
'sockaddr-in6
))
110 (af-local (isys:sizeof
'sockaddr-un
))
111 (af-netlink (isys:sizeof
'sockaddr-nl
)))))
113 ;;;; Conversion functions
115 (defun integer-to-dotted (integer)
116 "Convert an (UNSIGNED-BYTE 32) IPv4 address to a dotted string."
117 (check-type integer ub32
"an '(unsigned-byte 32)")
118 (let ((*print-pretty
* nil
) (*print-base
* 10))
119 (format nil
"~A.~A.~A.~A"
120 (ldb (byte 8 24) integer
)
121 (ldb (byte 8 16) integer
)
122 (ldb (byte 8 8) integer
)
123 (ldb (byte 8 0) integer
))))
125 (defun dotted-to-vector (address)
126 "Convert a dotted IPv4 address to a (SIMPLE-ARRAY (UNSIGNED-BYTE 8) 4)."
127 (check-type address string
"a string")
128 (let ((addr (make-array 4 :element-type
'ub8
:initial-element
0))
129 (split (split-sequence #\. address
:count
5)))
130 (flet ((set-array-value (index str
)
131 (setf (aref addr index
)
132 (ensure-number str
:type
'ub8
))))
133 (let ((len (length split
)))
135 (error 'parse-error
))
136 (set-array-value 3 (nth (1- len
) split
))
137 (loop :for n
:in split
138 :for index
:below
(1- len
)
139 :do
(set-array-value index n
))))
142 (defun dotted-to-integer (address)
143 "Convert a dotted IPv4 address to an (UNSIGNED-BYTE 32)."
144 (vector-to-integer (dotted-to-vector address
)))
146 (defun vector-to-dotted (vector)
147 "Convert an 4-element vector to a dotted string."
148 (coercef vector
'ipv4-array
)
149 (let ((*print-pretty
* nil
) (*print-base
* 10))
150 (with-output-to-string (s)
151 (princ (aref vector
0) s
) (princ #\. s
)
152 (princ (aref vector
1) s
) (princ #\. s
)
153 (princ (aref vector
2) s
) (princ #\. s
)
154 (princ (aref vector
3) s
))))
156 ;;; TODO: add tests against inet_pton(). Optimize if necessary.
157 ;;; <http://java.sun.com/javase/6/docs/api/java/net/Inet6Address.html#format>
158 (defun colon-separated-to-vector (string)
159 "Convert a colon-separated IPv6 address to a (SIMPLE-ARRAY (UNSIGNED-BYTE 16) 8)."
160 (check-type string string
"a string")
161 (when (< (length string
) 2)
162 (error 'parse-error
))
163 (flet ((handle-trailing-and-leading-colons (string)
165 (end (length string
))
167 (trailing-colon-p nil
)
168 (tokens-from-leading-or-trailing-zeros 0))
169 (when (char= #\
: (char string
0))
171 (unless (char= #\
: (char string
1))
173 (setq tokens-from-leading-or-trailing-zeros
1)))
174 (when (char= #\
: (char string
(- end
1)))
175 (setq trailing-colon-p t
)
176 (unless (char= #\
: (char string
(- end
2)))
177 (incf tokens-from-leading-or-trailing-zeros
))
179 (values start end start-i trailing-colon-p
180 tokens-from-leading-or-trailing-zeros
)))
181 ;; we need to use this instead of dotted-to-vector because
182 ;; abbreviated IPv4 addresses are invalid in this context.
183 (ipv4-string-to-ub16-list (string)
184 (let ((tokens (split-sequence #\. string
)))
185 (when (= (length tokens
) 4)
186 (let ((ipv4 (map 'vector
188 (let ((x (ignore-errors
189 (parse-integer string
))))
190 (if (or (null x
) (not (<= 0 x
#xff
)))
194 (list (dpb (aref ipv4
0) (byte 8 8) (aref ipv4
1))
195 (dpb (aref ipv4
2) (byte 8 8) (aref ipv4
3)))))))
196 (parse-hex-ub16 (string)
197 (ensure-number string
:type
'ub16
:radix
16)))
198 (multiple-value-bind (start end start-i trailing-colon-p extra-tokens
)
199 (handle-trailing-and-leading-colons string
)
200 (let* ((vector (make-array 8 :element-type
'ub16
:initial-element
0))
201 (tokens (split-sequence #\
: string
:start start
:end end
))
202 (empty-tokens (count-if #'emptyp tokens
))
203 (token-count (+ (length tokens
) extra-tokens
)))
204 (unless trailing-colon-p
205 (let ((ipv4 (ipv4-string-to-ub16-list (lastcar tokens
))))
208 (setq tokens
(nconc (butlast tokens
) ipv4
)))))
209 (when (or (> token-count
8) (> empty-tokens
1)
210 (and (zerop empty-tokens
) (/= token-count
8)))
211 (error 'parse-error
))
212 (loop for i from start-i and token in tokens do
214 ((integerp token
) (setf (aref vector i
) token
))
215 ((emptyp token
) (incf i
(- 8 token-count
)))
216 (t (setf (aref vector i
) (parse-hex-ub16 token
)))))
219 (defun ipv4-on-ipv6-mapped-vector-p (vector)
220 (and (dotimes (i 5 t
)
221 (when (plusp (aref vector i
))
223 (= (aref vector
5) #xffff
)))
225 (defun princ-ipv4-on-ipv6-mapped-address (vector s
)
227 (let ((*print-base
* 10) (*print-pretty
* nil
))
228 (princ (ldb (byte 8 8) (aref vector
6)) s
) (princ #\. s
)
229 (princ (ldb (byte 8 0) (aref vector
6)) s
) (princ #\. s
)
230 (princ (ldb (byte 8 8) (aref vector
7)) s
) (princ #\. s
)
231 (princ (ldb (byte 8 0) (aref vector
7)) s
)))
233 (defun vector-to-colon-separated (vector &optional
(case :downcase
))
234 "Convert an (SIMPLE-ARRAY (UNSIGNED-BYTE 16) 8) to a colon-separated IPv6
235 address. CASE may be :DOWNCASE or :UPCASE."
236 (coercef vector
'ipv6-array
)
237 (check-type case
(member :upcase
:downcase
) "either :UPCASE or :DOWNCASE")
238 (let ((s (make-string-output-stream)))
239 (flet ((find-zeros ()
240 (let ((start (position 0 vector
:start
1 :end
7)))
243 (position-if #'plusp vector
:start start
:end
7)))))
244 (princ-subvec (start end
)
245 (loop :for i
:from start
:below end
246 :do
(princ (aref vector i
) s
) (princ #\
: s
))))
248 ((ipv4-on-ipv6-mapped-vector-p vector
)
249 (princ-ipv4-on-ipv6-mapped-address vector s
))
251 (let ((*print-base
* 16) (*print-pretty
* nil
))
252 (when (plusp (aref vector
0)) (princ (aref vector
0) s
))
254 (multiple-value-bind (start end
) (find-zeros)
255 (cond (start (princ-subvec 1 start
)
257 (when end
(princ-subvec end
7)))
258 (t (princ-subvec 1 7))))
259 (when (plusp (aref vector
7)) (princ (aref vector
7) s
))))))
260 (let ((str (get-output-stream-string s
)))
262 (:downcase
(nstring-downcase str
))
263 (:upcase
(nstring-upcase str
))))))
265 (defmacro ignore-parse-errors
(&body body
)
266 ;; return first value only
267 `(values (ignore-some-conditions (parse-error) ,@body
)))
269 (defun string-address-to-vector (address)
270 "Convert a string address (dotted or colon-separated) to a vector address.
271 If the string is not a valid address, return NIL."
272 (or (ignore-parse-errors (dotted-to-vector address
))
273 (ignore-parse-errors (colon-separated-to-vector address
))))
275 (defun address-to-vector (address)
276 "Convert any representation of an internet address to a vector.
277 Allowed inputs are: unsigned 32-bit integers, strings, vectors
278 and INET-ADDRESS objects. If the address is valid, two values
279 are returned: the vector and the address type (:IPV4 or IPV6),
280 otherwise NIL is returned."
281 (let (vector addr-type
)
283 (number (and (ignore-parse-errors
284 (setf vector
(integer-to-vector address
)))
285 (setf addr-type
:ipv4
)))
287 ((ignore-parse-errors (setf vector
(dotted-to-vector address
)))
288 (setf addr-type
:ipv4
))
289 ((ignore-parse-errors
290 (setf vector
(colon-separated-to-vector address
)))
291 (setf addr-type
:ipv6
))))
292 ((vector * 4) (and (ignore-parse-errors
293 (setf vector
(coerce address
'ipv4-array
)))
294 (setf addr-type
:ipv4
)))
295 ((vector * 8) (and (ignore-parse-errors
296 (setf vector
(coerce address
'ipv6-array
)))
297 (setf addr-type
:ipv6
)))
298 (ipv4-address (setf vector
(copy-seq (address-name address
))
300 (ipv6-address (setf vector
(copy-seq (address-name address
))
303 (values vector addr-type
))))
305 (defun ensure-address (address &key
(family :internet
) abstract
(errorp t
))
306 "If FAMILY is :LOCAL, a LOCAL-ADDRESS is instantiated with
307 ADDRESS as its NAME slot. If FAMILY is :INTERNET, an appropriate
308 subtype of INET-ADDRESS is instantiated after guessing the
309 address type through ADDRESS-TO-VECTOR. If the address is invalid
310 and ERRORP is not NIL, then a CL:PARSE-ERROR is signalled,
311 otherwise NIL is returned.
313 When ADDRESS is already an instance of the ADDRESS class, a check
314 is made to see if it matches the FAMILY argument and it is
315 returned unmodified."
321 (check-type address inet-address
"an INET address"))
322 ((not (typep address
'inet-address
))
325 (t (let ((vector (address-to-vector address
)))
327 (vector (make-address vector
))
328 (errorp (error 'parse-error
)))))))
331 (string (make-instance 'local-address
:name address
:abstract abstract
))
334 (check-type address local-address
"a local address"))
335 ((not (typep address
'local-address
))
341 (defgeneric address-to-string
(address)
342 (:documentation
"Returns a textual presentation of ADDRESS."))
344 (defmethod address-to-string ((address ipv4-address
))
345 (vector-to-dotted (address-name address
)))
347 (defmethod address-to-string ((address ipv6-address
))
348 (vector-to-colon-separated (address-name address
)))
350 (defmethod address-to-string ((address local-address
))
351 (format nil
"~:[~;@~]~S" (abstract-address-p address
)
352 (address-name address
)))
354 (defmethod address-to-string ((address netlink-address
))
355 (format nil
"~A" (netlink-address-multicast-groups address
)))
357 (defmethod print-object ((address inet-address
) stream
)
358 (let ((namestring (address-to-string address
)))
359 (if (or *print-readably
* *print-escape
*)
360 (format stream
"#/~S/~A" 'ip namestring
)
361 (write-string namestring stream
))))
363 (defmethod print-object ((address local-address
) stream
)
364 (print-unreadable-object (address stream
:type nil
:identity nil
)
365 (format stream
"Unix socket address: ~A"
366 (address-to-string address
))))
368 (defmethod print-object ((address netlink-address
) stream
)
369 (print-unreadable-object (address stream
:type nil
:identity nil
)
370 (format stream
"Netlink socket address: ~A"
371 (address-to-string address
))))
375 (define-literal-reader ip
(stream)
376 (loop :with sstr
:= (make-string-output-stream)
377 :for char
:= (read-char stream nil nil
)
379 :do
(cond ((or (digit-char-p char
16)
380 (member char
'(#\.
#\
:) :test
#'char
=))
381 (write-char char sstr
))
383 (unread-char char stream
)
385 :finally
(return (or (ensure-address (get-output-stream-string sstr
)
387 (error 'reader-error
:stream stream
)))))
389 ;;;; Equality Methods
391 (defun vector-equal (v1 v2
)
392 (and (= (length v1
) (length v2
))
393 (every #'eql v1 v2
)))
395 (defgeneric address
= (addr1 addr2
)
396 (:documentation
"Returns T if both arguments are the same socket address."))
398 (defmethod address= ((addr1 inet-address
) (addr2 inet-address
))
399 (vector-equal (address-name addr1
) (address-name addr2
)))
401 (defmethod address= ((addr1 local-address
) (addr2 local-address
))
402 (equal (address-name addr1
) (address-name addr2
)))
404 (defun address-equal-p (addr1 addr2
&optional
(family :internet
))
405 "Returns T if both arguments are designators for the same socket address."
406 (address= (ensure-address addr1
:family family
)
407 (ensure-address addr2
:family family
)))
411 (defgeneric copy-address
(address)
413 "Returns a copy of ADDRESS which is ADDRESS= to the original."))
415 (defmethod copy-address ((addr ipv4-address
))
416 (make-instance 'ipv4-address
:name
(copy-seq (address-name addr
))))
418 (defmethod copy-address ((addr ipv6-address
))
419 (make-instance 'ipv6-address
:name
(copy-seq (address-name addr
))))
421 (defmethod copy-address ((addr local-address
))
422 (make-instance 'local-address
423 :name
(copy-seq (address-name addr
))
424 :abstract
(abstract-address-p addr
)))
426 (defun map-ipv4-address-to-ipv6 (address)
427 "Returns an IPv6 address by mapping ADDRESS onto it."
428 (make-instance 'ipv6-address
429 :name
(map-ipv4-vector-to-ipv6 (address-name address
))))
431 (defun map-ipv6-address-to-ipv4 (address)
432 "Extracts the IPv4 part of an IPv6-mapped IPv4 address.
433 Signals an error if ADDRESS is not an IPv6-mapped IPv4 address."
434 (assert (ipv6-ipv4-mapped-p address
) (address)
435 "Not an IPv6-mapped IPv4 address: ~A" address
)
436 (make-instance 'ipv4-address
437 :name
(map-ipv6-vector-to-ipv4 (address-name address
))))
441 (defun make-address (name)
442 "Constructs an ADDRESS object. NAME should be of type
443 IPV4-ARRAY, IPV6-ARRAY or STRING in which case an instance of
444 IPV4-ADDRESS, IPV6-ADDRESS or LOCAL-ADDRESS, respectively, will
445 be created. Otherwise, a TYPE-ERROR is signalled. See also
448 ((ignore-errors (coercef name
'ipv4-array
))
449 (make-instance 'ipv4-address
:name name
))
450 ((ignore-errors (coercef name
'ipv6-array
))
451 (make-instance 'ipv6-address
:name name
))
452 ((stringp name
) (make-instance 'local-address
:name name
))
453 (t (error 'type-error
:datum name
454 :expected-type
'(or string ipv4-array ipv6-array
)))))