Added function INET-ADDRESS-P
[iolib.git] / sockets / address-predicates.lisp
blob65120ef4e77c5631216c5638aaaf8d6249cc60e4
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Indent-tabs-mode: NIL -*-
2 ;;;
3 ;;; address-predicates.lisp --- Predicate functions for addresses.
4 ;;;
5 ;;; Copyright (C) 2006-2007, Stelian Ionescu <sionescu@common-lisp.net>
6 ;;;
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
13 ;;;
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.
18 ;;;
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 ;;;; Well-known addresses
28 ;;; Defines a constant address. We need ENSURE-ADDRESS at compile
29 ;;; time which is why these functions are in a separate file from
30 ;;; address.lisp.
31 (defmacro define-address (name address-string docstring)
32 `(define-constant ,name (ensure-address ,address-string)
33 :documentation ,(format nil "~A (~A)" docstring address-string)
34 :test 'address=))
36 (define-address +ipv4-unspecified+ "0.0.0.0" "Unspecified IPv4 address.")
37 (define-address +ipv4-loopback+ "127.0.0.1" "Loopback IPv4 address.")
38 (define-address +ipv6-unspecified+ "::" "Unspecified IPv6 address.")
39 (define-address +ipv6-loopback+ "::1" "Loopback IPv6 address.")
41 ;;;; Multicast addresses replacing IPv4 broadcast addresses
43 (define-address +ipv6-interface-local-all-nodes+ "ff01::1"
44 "Interface local all nodes address.")
46 (define-address +ipv6-link-local-all-nodes+ "ff02::1"
47 "Link local all nodes address.")
49 (define-address +ipv6-interface-local-all-routers+ "ff01::2"
50 "Interface local all routers address.")
52 (define-address +ipv6-link-local-all-routers+ "ff02::2"
53 "Link local all routers address.")
55 (define-address +ipv6-site-local-all-routers+ "ff05::2"
56 "Site local all routers address.")
58 ;;;; Predicates
60 (defun addressp (address)
61 "Returns T if ADDRESS is an object of class ADDRESS. Does not
62 return T for other low-level address representations."
63 (typep address 'address))
65 (defun inet-address-p (address)
66 "Returns T if ADDRESS is an Inet address object."
67 (typep address 'inet-address))
69 (defun ipv4-address-p (address)
70 "Returns T if ADDRESS is an IPv4 address object."
71 (typep address 'ipv4-address))
73 (defun ipv6-address-p (address)
74 "Returns T if ADDRESS is an IPv6 address object."
75 (typep address 'ipv6-address))
77 (defun local-address-p (address)
78 "Returns T if ADDRESS is a local address object."
79 (typep address 'local-address))
81 (defgeneric address-type (address)
82 (:documentation "Returns a keyword symbol denoting the kind of
83 ADDRESS (:IPV4, :IPV6 or :LOCAL). If ADDRESS is not a known
84 address object, NIL is returned.")
85 (:method ((address ipv4-address)) :ipv4)
86 (:method ((address ipv6-address)) :ipv6)
87 (:method ((address local-address)) :local)
88 (:method (address)
89 (declare (ignore address))
90 nil))
92 (defgeneric inet-address-unspecified-p (addr)
93 (:documentation "Returns T if ADDR is an \"unspecified\" internet address.")
94 (:method ((address ipv4-address))
95 (address= address +ipv4-unspecified+))
96 (:method ((address ipv6-address))
97 (address= address +ipv6-unspecified+)))
99 (defgeneric inet-address-loopback-p (address)
100 (:documentation "Returns T if ADDRESS is a loopback internet address.")
101 (:method ((address ipv4-address))
102 (address= address +ipv4-loopback+))
103 (:method ((address ipv6-address))
104 (address= address +ipv6-loopback+)))
106 (defgeneric inet-address-multicast-p (address)
107 (:documentation "Returns T if ADDRESS is an multicast internet address.")
108 (:method ((address ipv4-address))
109 (= (logand (aref (address-name address) 0) #xE0)
110 #xE0))
111 (:method ((address ipv6-address))
112 (= (logand (aref (address-name address) 0) #xFF00)
113 #xFF00)))
115 (defgeneric inet-address-unicast-p (address)
116 (:documentation "Returns T if ADDRESS is an unicast internet address.")
117 (:method ((address ipv4-address))
118 (and (not (inet-address-unspecified-p address))
119 (not (inet-address-loopback-p address))
120 (not (inet-address-multicast-p address))))
121 (:method ((address ipv6-address))
122 (or (ipv6-link-local-unicast-p address)
123 (and (not (inet-address-unspecified-p address))
124 (not (inet-address-loopback-p address))
125 (not (inet-address-multicast-p address))))))
127 (defun ipv6-ipv4-mapped-p (address)
128 "Returns T if ADDRESS is an IPv6 address representing an IPv4
129 mapped address."
130 (check-type address ipv6-address)
131 (ipv4-on-ipv6-mapped-vector-p (address-name address)))
133 (defun ipv6-interface-local-multicast-p (address)
134 "Returns T if ADDRESS is an interface-local IPv6 address."
135 (check-type address ipv6-address)
136 (= (logand (aref (address-name address) 0) #xFF0F)
137 #xFF01))
139 (defun ipv6-link-local-multicast-p (address)
140 "Returns T if ADDRESS is a link-local IPv6 address."
141 (check-type address ipv6-address)
142 (= (logand (aref (address-name address) 0) #xFF0F)
143 #xFF02))
145 (defun ipv6-admin-local-multicast-p (address)
146 "Returns T if ADDRESS is a admin-local multicast IPv6 address."
147 (check-type address ipv6-address)
148 (= (logand (aref (address-name address) 0) #xFF0F)
149 #xFF04))
151 (defun ipv6-site-local-multicast-p (address)
152 "Returns T if ADDRESS is an site-local multicast IPv6 address."
153 (check-type address ipv6-address)
154 (= (logand (aref (address-name address) 0) #xFF0F)
155 #xFF05))
157 (defun ipv6-organization-local-multicast-p (address)
158 "Returns T if ADDRESS is an organization-local multicast IPv6 address."
159 (check-type address ipv6-address)
160 (= (logand (aref (address-name address) 0) #xFF0F)
161 #xFF08))
163 (defun ipv6-global-multicast-p (address)
164 "Returns T if ADDRESS is a global multicast IPv6 address."
165 (check-type address ipv6-address)
166 (= (logand (aref (address-name address) 0) #xFF0F)
167 #xFF0E))
169 (defun ipv6-reserved-multicast-p (address)
170 "Returns T if ADDRESS is a reserved multicast IPv6 address."
171 (check-type address ipv6-address)
172 (member (logand (aref (address-name address) 0) #xFF0F)
173 '(#xFF00 #xFF03 #xFF0F)))
175 (defun ipv6-unassigned-multicast-p (address)
176 "Returns T if ADDRESS is an unassigned multicast IPv6 address."
177 (check-type address ipv6-address)
178 (member (logand (aref (address-name address) 0) #xFF0F)
179 '(#xFF06 #xFF07 #xFF09 #xFF0A #xFF0B #xFF0C #xFF0D)))
181 (defun ipv6-transient-multicast-p (address)
182 "Returns T if ADDRESS is a transient multicast IPv6 address."
183 (check-type address ipv6-address)
184 (= (logand (aref (address-name address) 0) #xFF10)
185 #xFF10))
187 (defun ipv6-solicited-node-multicast-p (address)
188 "Returns T if ADDRESS is a solicited-node multicast IPv6 address."
189 (check-type address ipv6-address)
190 (let ((vec (address-name address)))
191 (and (= (aref vec 0) #xFF02) ; link-local permanent multicast
192 (= (aref vec 5) 1)
193 (= (logand (aref vec 6) #xFF00)
194 #xFF00))))
196 (defun ipv6-link-local-unicast-p (address)
197 "Returns T if ADDRESS is an link-local unicast IPv6 address."
198 (check-type address ipv6-address)
199 (= (aref (address-name address) 0) #xFE80))
201 (defun ipv6-site-local-unicast-p (address)
202 "Returns T if ADDRESS is an site-local unicast IPv6 address."
203 (check-type address ipv6-address)
204 (= (aref (address-name address) 0) #xFEC0))
206 (defun ipv6-global-unicast-p (address)
207 "Returns T if ADDRESS is an global unicasst IPv6 address."
208 (check-type address ipv6-address)
209 (and (not (inet-address-unspecified-p address))
210 (not (inet-address-loopback-p address))
211 (not (inet-address-multicast-p address))
212 (not (ipv6-link-local-unicast-p address))))
214 (defun ipv6-multicast-type (address)
215 "Returns the multicast type of ADDRESS or NIL if it's not a
216 multicast address."
217 (check-type address ipv6-address)
218 (cond
219 ((ipv6-interface-local-multicast-p address) :interface-local)
220 ((ipv6-link-local-multicast-p address) :link-local)
221 ((ipv6-admin-local-multicast-p address) :admin-local)
222 ((ipv6-site-local-multicast-p address) :site-local)
223 ((ipv6-organization-local-multicast-p address) :organization-local)
224 ((ipv6-global-multicast-p address) :global)
225 ((ipv6-reserved-multicast-p address) :reserved)
226 ((ipv6-unassigned-multicast-p address) :unassigned)))
228 (defun ipv6-unicast-type (address)
229 "Returns the unicast type of ADDRESS or NIL if it's not a
230 unicast address."
231 (check-type address ipv6-address)
232 (cond
233 ((ipv6-link-local-unicast-p address) :link-local)
234 ((ipv6-site-local-unicast-p address) :site-local)
235 ((ipv6-global-unicast-p address) :global)))
237 (defgeneric inet-address-type (address)
238 (:documentation "Returns the address type of ADDRESS as 2 values:
240 * protocol, one of :IPV4 or :IPV6
241 * kind, one of :UNSPECIFIED, :LOOPBACK, :MULTICAST or :UNICAST
243 For unicast or multicast IPv6 addresses, a third value is
244 returned which corresponds to the return value of
245 IPV6-UNICAST-TYPE or IPV6-MULTICAST-TYPE, respectively." ))
247 (defmethod inet-address-type ((addr ipv6-address))
248 (cond
249 ((inet-address-unspecified-p addr) (values :ipv6 :unspecified))
250 ((inet-address-loopback-p addr) (values :ipv6 :loopback))
251 ((inet-address-multicast-p addr)
252 (values :ipv6 :multicast (ipv6-multicast-type addr)))
253 (t (values :ipv6 :unicast (ipv6-unicast-type addr)))))
255 (defmethod inet-address-type ((addr ipv4-address))
256 (values :ipv4
257 (cond
258 ((inet-address-unspecified-p addr) :unspecified)
259 ((inet-address-loopback-p addr) :loopback)
260 ((inet-address-multicast-p addr) :multicast)
261 ((inet-address-unicast-p addr) :unicast))))