From b94cc6ff75a4755e93906aca22c417550ea1b625 Mon Sep 17 00:00:00 2001 From: Stelian Ionescu Date: Sun, 13 Apr 2008 02:17:08 +0200 Subject: [PATCH] Add ENSURE-SUBNET-MASK. Signed-off-by: Stelian Ionescu --- net.sockets/address-arithmetic.lisp | 22 ++++++++++++++++------ net.sockets/pkgdcl.lisp | 1 + 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/net.sockets/address-arithmetic.lisp b/net.sockets/address-arithmetic.lisp index b37f74d..860fbde 100644 --- a/net.sockets/address-arithmetic.lisp +++ b/net.sockets/address-arithmetic.lisp @@ -36,11 +36,19 @@ (setf (ldb (byte (- 32 cidr) 0) mask) 0) (make-instance 'ipv4-address :name (integer-to-vector mask)))) +(defun ensure-subnet-mask (thing) + "If THING is of type IPV4-ADDRESS it is returned as is; if keyword it must be one of +:A, :B or :C otherwise it's treated as a CIDR suffix." + (etypecase thing + (ipv4-address thing) + (unsigned-byte (make-subnet-mask :cidr thing)) + (keyword (make-subnet-mask :class thing)))) + (defgeneric inet-address-network-portion (address mask) (:documentation "Apply network mask MASK to ADDRESS in order to calculate the network part of ADDRESS.") - (:method ((address ipv4-address) - (mask ipv4-address)) + (:method ((address ipv4-address) mask) + (setf mask (ensure-subnet-mask mask)) (let ((v (make-array 4 :element-type 'ub8)) (av (address-name address)) (mv (address-name mask))) @@ -53,8 +61,8 @@ network part of ADDRESS.") (defgeneric inet-address-host-portion (address mask) (:documentation "Apply network mask MASK to ADDRESS in order to calculate the host part of ADDRESS.") - (:method ((address ipv4-address) - (mask ipv4-address)) + (:method ((address ipv4-address) mask) + (setf mask (ensure-subnet-mask mask)) (let ((v (make-array 4 :element-type 'ub8)) (av (address-name address)) (mv (address-name mask))) @@ -69,7 +77,8 @@ host part of ADDRESS.") NETWORK and MASK.") (:method ((address ipv4-address) (network ipv4-address) - (mask ipv4-address)) + mask) + (setf mask (ensure-subnet-mask mask)) (address= (inet-address-network-portion address mask) (inet-address-network-portion network mask)))) @@ -79,7 +88,8 @@ subnet specified by NETWORK and MASK.") (:method ((address1 ipv4-address) (address2 ipv4-address) (network ipv4-address) - (mask ipv4-address)) + mask) + (setf mask (ensure-subnet-mask mask)) (let ((address1-network (inet-address-network-portion address1 mask)) (address2-network (inet-address-network-portion address2 mask)) (subnet (inet-address-network-portion network mask))) diff --git a/net.sockets/pkgdcl.lisp b/net.sockets/pkgdcl.lisp index 9a63d0b..cb6daa0 100644 --- a/net.sockets/pkgdcl.lisp +++ b/net.sockets/pkgdcl.lisp @@ -150,6 +150,7 @@ ;; Network masks and subnets #:make-subnet-mask + #:ensure-subnet-mask #:inet-address-private-p #:inet-address-network-portion #:inet-address-host-portion -- 2.11.4.GIT