chiark / gitweb /
portability: Supply system-specific wossname for CLisp and ECL.
[zone] / net.lisp
CommitLineData
9c44003b
MW
1;;; -*-lisp-*-
2;;;
3;;; $Id$
4;;;
5;;; Network (numbering) tools
6;;;
7;;; (c) 2006 Straylight/Edgeware
8;;;
9
10;;;----- Licensing notice ---------------------------------------------------
11;;;
12;;; This program is free software; you can redistribute it and/or modify
13;;; it under the terms of the GNU General Public License as published by
14;;; the Free Software Foundation; either version 2 of the License, or
15;;; (at your option) any later version.
16;;;
17;;; This program is distributed in the hope that it will be useful,
18;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;;; GNU General Public License for more details.
21;;;
22;;; You should have received a copy of the GNU General Public License
23;;; along with this program; if not, write to the Free Software Foundation,
24;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25
26;;;--------------------------------------------------------------------------
27;;; Packaging.
28
29(defpackage #:net
85c39c01 30 (:use #:common-lisp #:mdw.base #:mdw.str #:collect)
9c44003b
MW
31 (:export #:ipaddr #:string-ipaddr #:ipaddr-byte #:ipaddr-string #:ipaddrp
32 #:integer-netmask #:ipmask #:ipmask-cidl-slash #:make-ipnet
33 #:string-ipnet #:ipnet #:ipnet-net #:ipnet-mask #:with-ipnet
34 #:ipnet-pretty #:ipnet-string #:ipnet-broadcast #:ipnet-hosts
35 #:ipnet-host #:ipaddr-networkp #:ipnet-subnetp
36 #:ipnet-changeable-bytes
37 #:host-find# #:host-create #:defhost #:parse-ipaddr
38 #:resolve-hostname #:canonify-hostname
39 #:net #:net-find #:net-get-as-ipnet #:net-create #:defnet
40 #:net-next-host #:net-host))
41
42(in-package #:net)
43
44;;;--------------------------------------------------------------------------
45;;; Basic types.
46
47(defun mask (n)
48 "Return 2^N - 1: i.e., a mask of N set bits."
49 (1- (ash 1 n)))
50
51(deftype u32 ()
52 "The type of unsigned 32-bit values."
53 '(unsigned-byte 32))
54
55(deftype ipaddr ()
56 "The type of IP (version 4) addresses."
57 'u32)
58
59;;;--------------------------------------------------------------------------
60;;; Various random utilities.
61
62(defun count-low-zero-bits (n)
63 "Return the number of low-order zero bits in the integer N."
64 (if (zerop n) nil
65 (loop for i from 0
66 until (logbitp i n)
67 finally (return i))))
68
69;;;--------------------------------------------------------------------------
70;;; Simple messing with IP addresses.
71
72(defun string-ipaddr (str &key (start 0) (end nil))
73 "Parse STR as an IP address in dotted-quad form and return the integer
2f1d381d
MW
74 equivalent. STR may be anything at all: it's converted as if by
75 `stringify'. The START and END arguments may be used to parse out a
76 substring."
9c44003b
MW
77 (setf str (stringify str))
78 (unless end
79 (setf end (length str)))
80 (let ((addr 0) (noct 0))
81 (loop
82 (let* ((pos (position #\. str :start start :end end))
83 (i (parse-integer str :start start :end (or pos end))))
84 (unless (<= 0 i 256)
85 (error "IP address octet out of range"))
86 (setf addr (+ (* addr 256) i))
87 (incf noct)
88 (unless pos
89 (return))
90 (setf start (1+ pos))))
91 (unless (= noct 4)
92 (error "Wrong number of octets in IP address"))
93 addr))
94
95(defun ipaddr-byte (ip n)
96 "Return byte N (from most significant downwards) of an IP address."
97 (assert (<= 0 n 3))
98 (logand #xff (ash ip (* -8 (- 3 n)))))
99
100(defun ipaddr-string (ip)
101 "Transform the address IP into a string in dotted-quad form."
102 (check-type ip ipaddr)
103 (join-strings #\. (collecting ()
104 (dotimes (i 4)
105 (collect (ipaddr-byte ip i))))))
106
107(defun ipaddrp (ip)
108 "Answer true if IP is a valid IP address in integer form."
109 (typep ip 'ipaddr))
110
111(defun ipaddr (ip)
112 "Convert IP to an IP address. If it's an integer, return it unchanged;
2f1d381d 113 otherwise convert by `string-ipaddr'."
9c44003b
MW
114 (typecase ip
115 (ipaddr ip)
116 (t (string-ipaddr ip))))
117
118;;;--------------------------------------------------------------------------
119;;; Netmasks.
120
121(defun integer-netmask (i)
122 "Given an integer I, return a netmask with its I top bits set."
123 (- (ash 1 32) (ash 1 (- 32 i))))
124
125(defun ipmask (ip)
126 "Transform IP into a netmask. If it's a small integer then it's converted
2f1d381d
MW
127 by `integer-netmask'; if nil, then all-bits-set; otherwise convert using
128 `ipaddr'."
9c44003b
MW
129 (typecase ip
130 (null (mask 32))
131 ((integer 0 32) (integer-netmask ip))
132 (t (ipaddr ip))))
133
134(defun ipmask-cidl-slash (mask)
135 "Given a netmask MASK, return an integer N such that (integer-netmask N) =
2f1d381d 136 MASK, or nil if this is impossible."
9c44003b
MW
137 (dotimes (i 33)
138 (when (= mask (integer-netmask i))
139 (return i))))
140
141;;;--------------------------------------------------------------------------
142;;; Networks: pairing an address and netmask.
143
144(defun make-ipnet (net mask)
145 "Construct an IP-network object given the NET and MASK; these are
2f1d381d 146 transformed as though by `ipaddr' and `ipmask'."
9c44003b
MW
147 (let ((net (ipaddr net))
148 (mask (ipmask mask)))
149 (cons (logand net mask) mask)))
150
151(defun string-ipnet (str &key (start 0) (end nil))
152 "Parse an IP-network from the string STR."
153 (setf str (stringify str))
154 (unless end (setf end (length str)))
155 (let ((sl (position #\/ str :start start :end end)))
156 (if sl
157 (make-ipnet (parse-ipaddr (subseq str start sl))
158 (if (find #\. str :start (1+ sl) :end end)
159 (string-ipaddr str :start (1+ sl) :end end)
160 (integer-netmask (parse-integer str
161 :start (1+ sl)
162 :end end))))
163 (make-ipnet (parse-ipaddr (subseq str start end))
164 (integer-netmask 32)))))
165
166(defun ipnet (net)
2f1d381d
MW
167 "Construct an IP-network object from the given argument. A number of forms
168 are acceptable:
9c44003b 169
2f1d381d
MW
170 * ADDR -- a single address (equivalent to ADDR 32)
171 * (NET . MASK|nil) -- a single-object representation.
172 * IPNET -- return an equivalent (`equal', not necessarily `eql')
173 version."
9c44003b
MW
174 (cond ((or (stringp net) (symbolp net)) (string-ipnet net))
175 (t (apply #'make-ipnet (pairify net 32)))))
176
177(defun ipnet-net (ipn)
178 "Return the base network address of IPN."
179 (car ipn))
180
181(defun ipnet-mask (ipn)
182 "Return the netmask of IPN."
183 (cdr ipn))
184
185(defmacro with-ipnet ((net mask) ipn &body body)
186 "Evaluate BODY with NET and MASK bound to the base address and netmask of
2f1d381d
MW
187 IPN. Either NET or MASK (or, less usefully, both) may be nil if not
188 wanted."
9c44003b
MW
189 (with-gensyms tmp
190 `(let ((,tmp ,ipn))
191 (let (,@(and net `((,net (ipnet-net ,tmp))))
192 ,@(and mask `((,mask (ipnet-mask ,tmp)))))
193 ,@body))))
194
195(defun ipnet-pretty (ipn)
196 "Convert IPN to a pretty cons-cell form."
197 (with-ipnet (net mask) ipn
198 (cons (ipaddr-string net)
199 (or (ipmask-cidl-slash mask) (ipaddr-string mask)))))
200
201(defun ipnet-string (ipn)
202 "Convert IPN to a string."
203 (with-ipnet (net mask) ipn
204 (format nil "~A/~A"
205 (ipaddr-string net)
206 (or (ipmask-cidl-slash mask) (ipaddr-string mask)))))
207
208(defun ipnet-broadcast (ipn)
209 "Return the broadcast address for the network IPN."
210 (with-ipnet (net mask) ipn
211 (logior net (logxor (mask 32) mask))))
212
213(defun ipnet-hosts (ipn)
214 "Return the number of available addresses in network IPN."
215 (ash 1 (- 32 (logcount (ipnet-mask ipn)))))
216
217(defun ipnet-host (ipn host)
218 "Return the address of the given HOST in network IPN. This works even with
2f1d381d 219 a non-contiguous netmask."
9c44003b
MW
220 (check-type host u32)
221 (with-ipnet (net mask) ipn
222 (let ((i 0) (m 1) (a net) (h host))
223 (loop
224 (when (>= i 32)
225 (error "Host index ~D out of range for network ~A"
226 host (ipnet-pretty ipn)))
227 (cond ((zerop h)
228 (return a))
229 ((logbitp i mask)
230 (setf h (ash h 1)))
231 (t
232 (setf a (logior a (logand m h)))
233 (setf h (logandc2 h m))))
234 (setf m (ash m 1))
235 (incf i)))))
236
237(defun ipaddr-networkp (ip ipn)
238 "Returns true if address IP is within network IPN."
239 (with-ipnet (net mask) ipn
240 (= net (logand ip mask))))
241
242(defun ipnet-subnetp (ipn subn)
243 "Returns true if SUBN is a (non-strict) subnet of IPN."
244 (with-ipnet (net mask) ipn
245 (with-ipnet (subnet submask) subn
246 (and (= net (logand subnet mask))
247 (= submask (logior mask submask))))))
248
249(defun ipnet-changeable-bytes (mask)
250 "Answers how many low-order bytes of MASK are (entirely or partially)
2f1d381d 251 changeable. This is used when constructing reverse zones."
9c44003b
MW
252 (dotimes (i 4 4)
253 (when (/= (ipaddr-byte mask i) 255)
254 (return (- 4 i)))))
255
256;;;--------------------------------------------------------------------------
257;;; Name resolution.
258
9c44003b
MW
259(defun resolve-hostname (name)
260 "Resolve a hostname to an IP address using the DNS, or return nil."
7d593efd 261 #+cmu (let ((he (ext:lookup-host-entry name)))
51a6847e
MW
262 (and he (ext:host-entry-addr he)))
263 #+clisp (let ((he (ext:resolve-host-ipaddr name)))
264 (and he (string-ipaddr (car (ext:hostent-addr-list he)))))
265 #+ecl (nth-value 2 (ext:lookup-host-entry name))
266 #-(or cmu clisp ecl) nil)
9c44003b 267
9c44003b
MW
268(defun canonify-hostname (name)
269 "Resolve a hostname to canonical form using the DNS, or return nil."
7d593efd 270 #+cmu (let ((he (ext:lookup-host-entry name)))
51a6847e
MW
271 (and he (ext:host-entry-name he)))
272 #+clisp (let ((he (ext:resolve-host-ipaddr name)))
273 (and he (ext:hostent-name he)))
274 #+ecl (nth-value 0 (ext:lookup-host-entry name))
275 #-(or cmu clisp ecl) name)
9c44003b
MW
276
277;;;--------------------------------------------------------------------------
278;;; Host names and specifiers.
279
280(defun parse-ipaddr (addr)
281 "Convert the string ADDR into an IP address: tries all sorts of things:
282
2f1d381d
MW
283 (NET [INDEX]) index a network: NET is a network name defined by
284 defnet; INDEX is an index or one of the special
285 symbols understood by net-host, and defaults to :next
286
287 INTEGER an integer IP address
288
289 IPADDR an IP address in dotted-quad form
290
291 HOST a host name defined by defhost
292
293 DNSNAME a name string to look up in the DNS"
9c44003b
MW
294 (cond ((listp addr)
295 (destructuring-bind
296 (net host)
297 (pairify addr :next)
298 (net-host (or (net-find net)
299 (error "Network ~A not found" net))
300 host)))
301 ((ipaddrp addr) addr)
302 (t
303 (setf addr (string-downcase (stringify addr)))
304 (or (host-find addr)
305 (and (plusp (length addr))
306 (digit-char-p (char addr 0))
307 (string-ipaddr addr))
308 (resolve-hostname (stringify addr))
309 (error "Host name ~A unresolvable" addr)))))
310
311(defvar *hosts* (make-hash-table :test #'equal)
312 "The table of known hostnames.")
313
314(defun host-find (name)
315 "Find a host by NAME."
316 (gethash (string-downcase (stringify name)) *hosts*))
317
318(defun (setf host-find) (addr name)
319 "Make NAME map to ADDR (must be an ipaddr in integer form)."
320 (setf (gethash (string-downcase (stringify name)) *hosts*) addr))
321
322(defun host-create (name addr)
323 "Make host NAME map to ADDR (anything acceptable to parse-ipaddr)."
324 (setf (host-find name) (parse-ipaddr addr)))
325
326(defmacro defhost (name addr)
327 "Main host definition macro. Neither NAME nor ADDR is evaluated."
328 `(progn
329 (host-create ',name ',addr)
330 ',name))
331
332;;;--------------------------------------------------------------------------
333;;; Network names and specifiers.
334
335(defstruct (net (:predicate netp))
336 "A network structure. Slots:
337
2f1d381d
MW
338 NAME The network's name, as a string
339 IPNET The network base address and mask
340 HOSTS Number of hosts in the network
341 NEXT Index of the next unassigned host"
9c44003b
MW
342 name
343 ipnet
344 hosts
345 next)
346
347(defvar *networks* (make-hash-table :test #'equal)
348 "The table of known networks.")
349
350(defun net-find (name)
351 "Find a network by NAME."
352 (gethash (string-downcase (stringify name)) *networks*))
353
354(defun (setf net-find) (net name)
355 "Make NAME map to NET."
356 (setf (gethash (string-downcase (stringify name)) *networks*) net))
357
358(defun net-get-as-ipnet (form)
359 "Transform FORM into an ipnet. FORM may be a network name, or something
360acceptable to the ipnet function."
361 (let ((net (net-find form)))
362 (if net (net-ipnet net)
363 (ipnet form))))
364
365(defun process-net-form (root addr subnets)
366 "Unpack a net-form. The return value is a list of entries, each of which
2f1d381d
MW
367 is a list of the form (NAME ADDR MASK). The first entry is merely repeats
368 the given ROOT and ADDR arguments (unpacking ADDR into separate network
369 address and mask). The SUBNETS are then processed: they are a list of
370 items of the form (NAME NUM-HOSTS . SUBNETS), where NAME names the subnet,
371 NUM-HOSTS is the number of hosts in it, and SUBNETS are its sub-subnets in
372 the same form. An error is signalled if a net's subnets use up more hosts
373 than the net has to start with."
9c44003b
MW
374 (labels ((frob (subnets limit finger)
375 (when subnets
376 (destructuring-bind (name size &rest subs) (car subnets)
377 (when (> (count-low-zero-bits size)
378 (count-low-zero-bits finger))
379 (error "Bad subnet size for ~A." name))
380 (when (> (+ finger size) limit)
381 (error "Subnet ~A out of range." name))
382 (append (and name
383 (list (list name finger (- (ash 1 32) size))))
384 (frob subs (+ finger size) finger)
385 (frob (cdr subnets) limit (+ finger size)))))))
386 (let ((ipn (ipnet addr)))
387 (with-ipnet (net mask) ipn
388 (unless (ipmask-cidl-slash mask)
389 (error "Bad mask for subnet form."))
390 (cons (list root net mask)
391 (frob subnets (+ net (ipnet-hosts ipn) 1) net))))))
392
393(defun net-create (name net)
394 "Construct a new network called NAME and add it to the map. The ARGS
2f1d381d 395 describe the new network, in a form acceptable to the ipnet function."
9c44003b
MW
396 (let ((ipn (ipnet net)))
397 (setf (net-find name)
398 (make-net :name (string-downcase (stringify name))
399 :ipnet ipn
400 :hosts (ipnet-hosts ipn)
401 :next 1))))
402
403(defmacro defnet (name net &rest subnets)
404 "Main network definition macro. None of the arguments is evaluated."
405 `(progn
406 ,@(loop for (name addr mask) in (process-net-form name net subnets)
407 collect `(net-create ',name '(,addr . ,mask)))
408 ',name))
409
410(defun net-next-host (net)
411 "Given a NET, return the IP address (as integer) of the next available
2f1d381d 412 address in the network."
9c44003b
MW
413 (unless (< (net-next net) (net-hosts net))
414 (error "No more hosts left in network ~A" (net-name net)))
415 (let ((next (net-next net)))
416 (incf (net-next net))
417 (net-host net next)))
418
419(defun net-host (net host)
420 "Return the given HOST on the NEXT. HOST may be an index (in range, of
2f1d381d
MW
421 course), or one of the keywords:
422
423 :NEXT next host, as by net-next-host
424 :NET network base address
425 :BROADCAST network broadcast address"
9c44003b
MW
426 (case host
427 (:next (net-next-host net))
428 (:net (ipnet-net (net-ipnet net)))
429 (:broadcast (ipnet-broadcast (net-ipnet net)))
430 (t (ipnet-host (net-ipnet net) host))))
431
432;;;----- That's all, folks --------------------------------------------------