summaryrefslogtreecommitdiffstats
path: root/addr.c
diff options
context:
space:
mode:
authordjm@openbsd.org <djm@openbsd.org>2022-10-28 02:29:34 +0000
committerDamien Miller <djm@mindrot.org>2022-10-28 13:39:35 +1100
commit1192588546c29ceec10775125f396555ea71850f (patch)
tree72e61525bc48f50d8854a5d65668e966cb1e8ab2 /addr.c
parent64af4209309461c79c39eda2d13f9d77816c6398 (diff)
upstream: allow ssh-keyscan(1) to accept CIDR address ranges, e.g.
ssh-keyscan 192.168.0.0/24 If a CIDR range is passed, then it will be expanded to all possible addresses in the range including the all-0s and all-1s addresses. bz#976 feedback/ok markus@ OpenBSD-Commit-ID: ce6c5211f936ac0053fd4a2ddb415277931e6c4b
Diffstat (limited to 'addr.c')
-rw-r--r--addr.c73
1 files changed, 72 insertions, 1 deletions
diff --git a/addr.c b/addr.c
index abf3e3d9..db9ab7ac 100644
--- a/addr.c
+++ b/addr.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: addr.c,v 1.5 2022/04/29 04:55:07 djm Exp $ */
+/* $OpenBSD: addr.c,v 1.6 2022/10/28 02:29:34 djm Exp $ */
/*
* Copyright (c) 2004-2008 Damien Miller <djm@mindrot.org>
@@ -228,6 +228,28 @@ addr_and(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
}
int
+addr_or(struct xaddr *dst, const struct xaddr *a, const struct xaddr *b)
+{
+ int i;
+
+ if (dst == NULL || a == NULL || b == NULL || a->af != b->af)
+ return (-1);
+
+ memcpy(dst, a, sizeof(*dst));
+ switch (a->af) {
+ case AF_INET:
+ dst->v4.s_addr |= b->v4.s_addr;
+ return (0);
+ case AF_INET6:
+ for (i = 0; i < 4; i++)
+ dst->addr32[i] |= b->addr32[i];
+ return (0);
+ default:
+ return (-1);
+ }
+}
+
+int
addr_cmp(const struct xaddr *a, const struct xaddr *b)
{
int i;
@@ -278,6 +300,29 @@ addr_is_all0s(const struct xaddr *a)
}
}
+/* Increment the specified address. Note, does not do overflow checking */
+void
+addr_increment(struct xaddr *a)
+{
+ int i;
+ uint32_t n;
+
+ switch (a->af) {
+ case AF_INET:
+ a->v4.s_addr = htonl(ntohl(a->v4.s_addr) + 1);
+ break;
+ case AF_INET6:
+ for (i = 0; i < 4; i++) {
+ /* Increment with carry */
+ n = ntohl(a->addr32[3 - i]) + 1;
+ a->addr32[3 - i] = htonl(n);
+ if (n != 0)
+ break;
+ }
+ break;
+ }
+}
+
/*
* Test whether host portion of address 'a', as determined by 'masklen'
* is all zeros.
@@ -297,6 +342,32 @@ addr_host_is_all0s(const struct xaddr *a, u_int masklen)
return addr_is_all0s(&tmp_result);
}
+#if 0
+int
+addr_host_to_all0s(struct xaddr *a, u_int masklen)
+{
+ struct xaddr tmp_mask;
+
+ if (addr_netmask(a->af, masklen, &tmp_mask) == -1)
+ return (-1);
+ if (addr_and(a, a, &tmp_mask) == -1)
+ return (-1);
+ return (0);
+}
+#endif
+
+int
+addr_host_to_all1s(struct xaddr *a, u_int masklen)
+{
+ struct xaddr tmp_mask;
+
+ if (addr_hostmask(a->af, masklen, &tmp_mask) == -1)
+ return (-1);
+ if (addr_or(a, a, &tmp_mask) == -1)
+ return (-1);
+ return (0);
+}
+
/*
* Parse string address 'p' into 'n'.
* Returns 0 on success, -1 on failure.