1 ;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp -*-
3 ;; Copyright (C) 2006, 2007 Stelian Ionescu
5 ;; This code is free software; you can redistribute it and/or
6 ;; modify it under the terms of the version 2.1 of
7 ;; the GNU Lesser General Public License as published by
8 ;; the Free Software Foundation, as clarified by the
9 ;; preamble found here:
10 ;; http://opensource.franz.com/preamble.html
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
17 ;; You should have received a copy of the GNU Lesser General
18 ;; Public License along with this library; if not, write to the
19 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20 ;; Boston, MA 02110-1301, USA
22 (in-package :net.sockets
)
29 (define-condition invalid-address
()
30 ((address :initarg
:address
:initform nil
:reader address
)
31 (addrtype :initarg
:type
:initform nil
:reader address-type
))
32 (:report
(lambda (condition stream
)
33 (format stream
"Invalid ~A address: ~A"
34 (address-type condition
) (address condition
))))
35 (:documentation
"Condition raised when an address designator is invalid."))
39 ;;; Conversion functions
42 (defun ipaddr-to-dotted (ipaddr)
43 (declare (type ub32 ipaddr
))
44 (format nil
"~A.~A.~A.~A"
45 (ldb (byte 8 24) ipaddr
)
46 (ldb (byte 8 16) ipaddr
)
47 (ldb (byte 8 8) ipaddr
)
48 (ldb (byte 8 0) ipaddr
)))
50 (defun dotted-to-vector (string &key
(errorp t
))
51 (labels ((err (&rest args
)
54 (return-from dotted-to-vector nil
)))
56 (err 'type-error
:datum string
:expected-type
'string
))
58 (err 'invalid-address
:address string
:type
:ipv4
)))
59 (unless (stringp string
)
62 (let ((addr (make-array 4 :element-type
'ub8
))
63 (split (split-sequence #\. string
:count
5)))
64 ;; must have exactly 4 tokens
65 (when (/= 4 (length split
))
68 :for element
:in split
70 :for parsed
:= (parse-number-or-nil element
:ub8
) :do
72 (setf (aref addr index
) parsed
)
76 (defun dotted-to-ipaddr (string)
77 (vector-to-ipaddr (dotted-to-vector string
)))
79 (defun vector-to-dotted (vector)
80 (coercef vector
'ipv4-array
)
81 (format nil
"~A.~A.~A.~A"
87 (defun colon-separated-to-vector (string &key
(errorp t
))
88 (when (not (stringp string
))
90 (error 'type-error
:datum string
91 :expected-type
'string
)
92 (return-from colon-separated-to-vector nil
)))
94 (with-foreign-object (in6-addr :uint16
8)
95 (with-foreign-string (string-pointer string
)
96 (et:bzero in6-addr
16)
98 (et:inet-pton et
:af-inet6
; address family
100 in6-addr
) ; pointer to struct in6_addr
103 (error 'invalid-address
:address string
:type
:ipv6
)
104 (return-from colon-separated-to-vector nil
)))))
105 (in6-addr-to-ipv6-array in6-addr
)))
107 (defun vector-to-colon-separated (vector &key
(case :downcase
) (errorp t
))
109 (coercef vector
'ipv6-array
)
112 (error 'type-error
:datum vector
113 :expected-type
'ipv6-array
)
114 (return-from vector-to-colon-separated nil
))))
116 (with-foreign-object (sin6 'et
:sockaddr-in6
)
117 (with-foreign-pointer (namebuf et
:inet6-addrstrlen bufsize
)
118 (make-sockaddr-in6 sin6 vector
)
119 (et:inet-ntop et
:af-inet6
; address family
120 (foreign-slot-pointer
121 sin6
'et
:sockaddr-in6
'et
:addr
) ; pointer to struct in6_addr
122 namebuf
; destination buffer
123 bufsize
) ; INET6_ADDRSTRLEN
124 (return-from vector-to-colon-separated
125 (let ((str (foreign-string-to-lisp namebuf bufsize
)))
128 (:upcase
(nstring-upcase str
))))))))
130 (defun string-address->vector
(address)
131 (or (dotted-to-vector address
:errorp nil
)
132 (colon-separated-to-vector address
:errorp nil
)))
134 (defun vector-address-or-nil (address)
135 (let (vector addr-type
)
138 ((setf vector
(dotted-to-vector address
:errorp nil
))
139 (setf addr-type
:ipv4
))
140 ((setf vector
(colon-separated-to-vector address
:errorp nil
))
141 (setf addr-type
:ipv6
))))
142 ((vector * 4) (and (ignore-errors (setf vector
(coerce address
'ipv4-array
)))
143 (setf addr-type
:ipv4
)))
144 ((vector * 8) (and (ignore-errors (setf vector
(coerce address
'ipv6-array
)))
145 (setf addr-type
:ipv6
)))
146 (ipv4addr (setf vector
(name address
)
148 (ipv6addr (setf vector
(name address
)
150 (values vector addr-type
)))
154 ;;; Class definitions
157 (defclass sockaddr
()
158 ((name :initarg
:name
:reader name
:type vector
))
159 (:documentation
"Base class for all socket address classes."))
161 (defclass inetaddr
(sockaddr) ()
162 (:documentation
"IP addresses."))
164 (defclass ipv4addr
(inetaddr) ()
165 (:documentation
"IPv4 address."))
167 (defclass ipv6addr
(inetaddr) ()
168 (:documentation
"IPv6 address."))
170 (defclass localaddr
(sockaddr)
171 ((abstract :initform nil
:initarg
:abstract
:reader abstract-p
:type boolean
))
172 (:documentation
"UNIX socket address."))
179 (defmethod print-object ((address ipv4addr
) stream
)
180 (print-unreadable-object (address stream
:type nil
:identity nil
)
181 (format stream
"IPv4 address: ~A"
182 (sockaddr->presentation address
))))
184 (defmethod print-object ((address ipv6addr
) stream
)
185 (print-unreadable-object (address stream
:type nil
:identity nil
)
186 (format stream
"IPv6 address: ~A"
187 (sockaddr->presentation address
))))
189 (defmethod print-object ((address localaddr
) stream
)
190 (print-unreadable-object (address stream
:type nil
:identity nil
)
191 (with-slots (abstract) address
192 (format stream
"Unix socket address: ~A. Abstract: ~:[no~;yes~]"
193 (sockaddr->presentation address
) abstract
))))
195 (defgeneric sockaddr-
>presentation
(addr)
196 (:documentation
"Returns a textual presentation of ADDR."))
198 (defmethod sockaddr->presentation
((addr ipv4addr
))
199 (vector-to-dotted (name addr
)))
201 (defmethod sockaddr->presentation
((addr ipv6addr
))
202 (vector-to-colon-separated (name addr
)))
204 (defmethod sockaddr->presentation
((addr localaddr
))
205 (if (abstract-p addr
)
214 (defun vector-equal (v1 v2
)
215 (and (equal (length v1
) (length v2
))
216 (every #'eql v1 v2
)))
218 (defgeneric sockaddr
= (addr1 addr2
)
219 (:documentation
"Returns T if both arguments are the same socket address."))
221 (defmethod sockaddr= ((addr1 inetaddr
) (addr2 inetaddr
))
222 (vector-equal (name addr1
) (name addr2
)))
224 (defmethod sockaddr= ((addr1 localaddr
) (addr2 localaddr
))
225 (equal (name addr1
) (name addr2
)))
232 (defgeneric copy-sockaddr
(addr)
233 (:documentation
"Returns a copy of ADDR which is SOCKADDR= to the original."))
235 (defmethod copy-sockaddr ((addr ipv4addr
))
236 (make-instance 'ipv4addr
237 :name
(copy-seq (name addr
))))
239 (defmethod copy-sockaddr ((addr ipv6addr
))
240 (make-instance 'ipv6addr
241 :name
(copy-seq (name addr
))))
243 (defmethod copy-sockaddr ((addr localaddr
))
244 (make-instance 'localaddr
245 :name
(copy-seq (name addr
))
246 :abstract
(abstract-p addr
)))
248 (defgeneric map-ipv4-address-
>ipv6
(addr)
249 (:documentation
"Returns an IPv6 address by mapping ADDR onto it."))
250 (defmethod map-ipv4-address->ipv6
((addr ipv4addr
))
251 (make-instance 'ipv6addr
252 :name
(map-ipv4-vector-to-ipv6 (name addr
))))
256 (defun make-address (name)
259 (make-instance 'localaddr
:name name
))
260 ((ignore-errors (coercef name
'ipv4-array
))
261 (make-instance 'ipv4addr
:name name
))
262 ((ignore-errors (coercef name
'ipv6-array
))
263 (make-instance 'ipv6addr
:name name
))
264 (t (error 'invalid-address
:address name
:type
:unknown
))))
266 (defun ensure-address (addr &optional
(family :internet
))
267 (cond ((sockaddrp addr
)
270 (:internet
(check-type addr inetaddr
))
271 (:local
(check-type addr localaddr
)))
274 (if (eql family
:local
)
275 (make-instance 'localaddr
:name addr
)
276 (make-address (string-address->vector addr
))))
277 (t (make-address addr
))))
280 ;;; Well-known addresses
283 (defparameter +ipv4-unspecified
+
284 (make-address #(0 0 0 0)))
286 (defparameter +ipv4-loopback
+
287 (make-address #(127 0 0 1)))
289 (defparameter +ipv6-unspecified
+
290 (make-address #(0 0 0 0 0 0 0 0)))
292 (defparameter +ipv6-loopback
+
293 (make-address #(0 0 0 0 0 0 0 1)))
295 ;; Multicast addresses replacing IPv4 broadcast addresses
296 (defparameter +ipv6-interface-local-all-nodes
+
297 (make-address #(#xFF01
0 0 0 0 0 0 1)))
299 (defparameter +ipv6-link-local-all-nodes
+
300 (make-address #(#xFF02
0 0 0 0 0 0 1)))
302 (defparameter +ipv6-interface-local-all-routers
+
303 (make-address #(#xFF01
0 0 0 0 0 0 2)))
305 (defparameter +ipv6-link-local-all-routers
+
306 (make-address #(#xFF02
0 0 0 0 0 0 2)))
308 (defparameter +ipv6-site-local-all-routers
+
309 (make-address #(#xFF05
0 0 0 0 0 0 2)))
316 ;; General predicates
317 (defgeneric sockaddrp
(address)
318 (:documentation
"Returns T if ADDRESS is a socket address."))
320 (defmethod sockaddrp ((address sockaddr
))
321 (declare (ignore address
))
324 (defmethod sockaddrp (address)
325 (declare (ignore address
))
328 (defgeneric ipv4-address-p
(address)
329 (:documentation
"Returns T if ADDRESS is an IPv4 address object."))
331 (defmethod ipv4-address-p ((address ipv4addr
))
332 (declare (ignore address
))
335 (defmethod ipv4-address-p (address)
336 (declare (ignore address
))
339 (defgeneric ipv6-address-p
(address)
340 (:documentation
"Returns T if ADDRESS is an IPv6 address object."))
342 (defmethod ipv6-address-p ((address ipv6addr
))
343 (declare (ignore address
))
346 (defmethod ipv6-address-p (address)
347 (declare (ignore address
))
350 (defgeneric local-address-p
(address)
351 (:documentation
"Returns T if ADDRESS is local address object."))
353 (defmethod local-address-p ((address localaddr
))
354 (declare (ignore address
))
357 (defmethod local-address-p (address)
358 (declare (ignore address
))
361 (defmethod address-type ((address ipv4addr
))
362 (declare (ignore address
))
365 (defmethod address-type ((address ipv6addr
))
366 (declare (ignore address
))
369 (defmethod address-type ((address localaddr
))
370 (declare (ignore address
))
373 (defmethod address-type (address)
374 (declare (ignore address
))
379 (defgeneric inetaddr-unspecified-p
(addr)
380 (:documentation
"Returns T if ADDR is an \"unspecified\" internet address."))
381 (defmethod inetaddr-unspecified-p ((addr ipv4addr
))
382 (sockaddr= addr
+ipv4-unspecified
+))
384 (defgeneric inetaddr-loopback-p
(addr)
385 (:documentation
"Returns T if ADDR is a loopback internet address."))
386 (defmethod inetaddr-loopback-p ((addr ipv4addr
))
387 (sockaddr= addr
+ipv4-loopback
+))
389 (defgeneric inetaddr-multicast-p
(addr)
390 (:documentation
"Returns T if ADDR is an multicast internet address."))
391 (defmethod inetaddr-multicast-p ((addr ipv4addr
))
392 (eql (logand (aref (name addr
) 0)
396 (defgeneric inetaddr-unicast-p
(addr)
397 (:documentation
"Returns T if ADDR is an unicast internet address."))
398 (defmethod inetaddr-unicast-p ((addr ipv4addr
))
399 (and (not (inetaddr-unspecified-p addr
))
400 (not (inetaddr-loopback-p addr
))
401 (not (inetaddr-multicast-p addr
))))
404 ;; definitions taken from RFC 2460
406 (defmethod inetaddr-unspecified-p ((addr ipv6addr
))
407 (sockaddr= addr
+ipv6-unspecified
+))
409 (defmethod inetaddr-loopback-p ((addr ipv6addr
))
410 (sockaddr= addr
+ipv6-loopback
+))
412 (defgeneric ipv6-ipv4-mapped-p
(addr)
413 (:documentation
"Returns T if ADDR is an IPv6 address representing an IPv4 mapped address."))
414 (defmethod ipv6-ipv4-mapped-p ((addr ipv6addr
))
415 (with-slots (name) addr
416 (and (zerop (aref name
0))
417 (zerop (aref name
1))
418 (zerop (aref name
2))
419 (zerop (aref name
3))
420 (zerop (aref name
4))
421 (eql (aref name
5) #xFFFF
)
422 (< (ldb (byte 8 0) (aref name
6))
424 (< (ldb (byte 8 8) (aref name
6))
426 (< (ldb (byte 8 0) (aref name
7))
428 (< (ldb (byte 8 8) (aref name
7))
431 (defmethod inetaddr-multicast-p ((addr ipv6addr
))
432 (eql (logand (aref (name addr
) 0)
436 (defgeneric ipv6-interface-local-multicast-p
(addr)
437 (:documentation
"Returns T if ADDR is an interface-local IPv6 address."))
438 (defmethod ipv6-interface-local-multicast-p ((addr ipv6addr
))
439 (eql (logand (aref (name addr
) 0)
443 (defgeneric ipv6-link-local-multicast-p
(addr)
444 (:documentation
"Returns T if ADDR is a link-local IPv6 address."))
445 (defmethod ipv6-link-local-multicast-p ((addr ipv6addr
))
446 (eql (logand (aref (name addr
) 0)
450 (defgeneric ipv6-admin-local-multicast-p
(addr)
451 (:documentation
"Returns T if ADDR is a admin-local multicast IPv6 address."))
452 (defmethod ipv6-admin-local-multicast-p ((addr ipv6addr
))
453 (eql (logand (aref (name addr
) 0)
457 (defgeneric ipv6-site-local-multicast-p
(addr)
458 (:documentation
"Returns T if ADDR is an site-local multicast IPv6 address."))
459 (defmethod ipv6-site-local-multicast-p ((addr ipv6addr
))
460 (eql (logand (aref (name addr
) 0)
464 (defgeneric ipv6-organization-local-multicast-p
(addr)
465 (:documentation
"Returns T if ADDR is an organization-local multicast IPv6 address."))
466 (defmethod ipv6-organization-local-multicast-p ((addr ipv6addr
))
467 (eql (logand (aref (name addr
) 0)
471 (defgeneric ipv6-global-multicast-p
(addr)
472 (:documentation
"Returns T if ADDR is a global multicast IPv6 address."))
473 (defmethod ipv6-global-multicast-p ((addr ipv6addr
))
474 (eql (logand (aref (name addr
) 0)
478 (defgeneric ipv6-reserved-multicast-p
(addr)
479 (:documentation
"Returns T if ADDR is a reserved multicast IPv6 address."))
480 (defmethod ipv6-reserved-multicast-p ((addr ipv6addr
))
481 (member (logand (aref (name addr
) 0)
483 (list #xFF00
#xFF03
#xFF0F
)))
485 (defgeneric ipv6-unassigned-multicast-p
(addr)
486 (:documentation
"Returns T if ADDR is an unassigned multicast IPv6 address."))
487 (defmethod ipv6-unassigned-multicast-p ((addr ipv6addr
))
488 (member (logand (aref (name addr
) 0)
490 (list #xFF06
#xFF07
#xFF09
#xFF0A
#xFF0B
#xFF0C
#xFF0D
)))
492 (defgeneric ipv6-transient-multicast-p
(addr)
493 (:documentation
"Returns T if ADDR is a transient multicast IPv6 address."))
494 (defmethod ipv6-transient-multicast-p ((addr ipv6addr
))
495 (eql (logand (aref (name addr
) 0)
499 (defgeneric ipv6-solicited-node-multicast-p
(addr)
500 (:documentation
"Returns T if ADDR is an solicited-node multicast IPv6 address."))
501 (defmethod ipv6-solicited-node-multicast-p ((addr ipv6addr
))
502 (let ((vec (name addr
)))
503 (and (eql (aref vec
0) #xFF02
) ; link-local permanent multicast
505 (eql (logand (aref vec
6)
509 (defgeneric ipv6-link-local-unicast-p
(addr)
510 (:documentation
"Returns T if ADDR is an link-local unicast IPv6 address."))
511 (defmethod ipv6-link-local-unicast-p ((addr ipv6addr
))
512 (eql (aref (name addr
) 0) #xFE80
))
514 (defgeneric ipv6-site-local-unicast-p
(addr)
515 (:documentation
"Returns T if ADDR is an site-local unicast IPv6 address."))
516 (defmethod ipv6-site-local-unicast-p ((addr ipv6addr
))
517 (eql (aref (name addr
) 0) #xFEC0
))
519 (defgeneric ipv6-global-unicast-p
(addr)
520 (:documentation
"Returns T if ADDR is an global unicasst IPv6 address."))
521 (defmethod ipv6-global-unicast-p ((addr ipv6addr
))
522 (and (not (inetaddr-unspecified-p addr
))
523 (not (inetaddr-loopback-p addr
))
524 (not (inetaddr-multicast-p addr
))
525 (not (ipv6-link-local-unicast-p addr
))))
527 (defmethod inetaddr-unicast-p ((addr ipv6addr
))
528 (or (ipv6-link-local-unicast-p addr
)
529 (and (not (inetaddr-unspecified-p addr
))
530 (not (inetaddr-loopback-p addr
))
531 (not (inetaddr-multicast-p addr
)))))
533 (defgeneric ipv6-multicast-type
(addr)
534 (:documentation
"Returns the multicast type of ADDR(which must be IPv6)."))
535 (defmethod ipv6-multicast-type ((addr ipv6addr
))
537 ((ipv6-interface-local-multicast-p addr
) :interface-local
)
538 ((ipv6-link-local-multicast-p addr
) :link-local
)
539 ((ipv6-admin-local-multicast-p addr
) :admin-local
)
540 ((ipv6-site-local-multicast-p addr
) :site-local
)
541 ((ipv6-organization-local-multicast-p addr
) :organization-local
)
542 ((ipv6-global-multicast-p addr
) :global
)
543 ((ipv6-reserved-multicast-p addr
) :reserved
)
544 ((ipv6-unassigned-multicast-p addr
) :unassigned
)))
546 (defgeneric inetaddr-type
(addr)
547 (:documentation
"Returns the address type of ADDR."))
549 (defmethod inetaddr-type ((addr ipv6addr
))
551 ((inetaddr-unspecified-p addr
) (values :ipv6
:unspecified
))
552 ((inetaddr-loopback-p addr
) (values :ipv6
:loopback
))
553 ((inetaddr-multicast-p addr
) (values :ipv6
:multicast
(ipv6-multicast-type addr
)))
554 ((ipv6-link-local-unicast-p addr
) (values :ipv6
:unicast
:link-local
))
555 (t (values :ipv6
:unicast
:global
))))
557 (defmethod inetaddr-type ((addr ipv4addr
))
559 ((inetaddr-unspecified-p addr
) (values :ipv4
:unspecified
))
560 ((inetaddr-loopback-p addr
) (values :ipv4
:loopback
))
561 ((inetaddr-multicast-p addr
) (values :ipv4
:multicast
))
562 ((inetaddr-unicast-p addr
) (values :ipv4
:unicast
))))