3 * Copyright (C) 2000 Henry Spencer.
5 * This library is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU Library General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/lgpl.txt>.
10 * This library is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 * License for more details.
15 * RCSID $Id: sameaddr.c,v 1.7 2000/11/29 16:34:01 henry Exp $
20 static int samenbits(const ip_address *a, const ip_address *b, int n);
23 - addrcmp - compare two addresses
24 * Caution, the order of the tests is subtle: doing type test before
25 * size test can yield cases where a<b, b<c, but a>c.
32 int at = addrtypeof(a);
33 int bt = addrtypeof(b);
34 const unsigned char *ap;
35 const unsigned char *bp;
36 size_t as = addrbytesptr(a, &ap);
37 size_t bs = addrbytesptr(b, &bp);
38 size_t n = (as < bs) ? as : bs; /* min(as, bs) */
39 int c = memcmp(ap, bp, n);
41 if (c != 0) /* bytes differ */
42 return (c < 0) ? -1 : 1;
43 if (as != bs) /* comparison incomplete: lexical order */
44 return (as < bs) ? -1 : 1;
45 if (at != bt) /* bytes same but not same type: break tie */
46 return (at < bt) ? -1 : 1;
51 - sameaddr - are two addresses the same?
58 return (addrcmp(a, b) == 0) ? 1 : 0;
62 - samesubnet - are two subnets the same?
69 if (!sameaddr(&a->addr, &b->addr)) /* also does type check */
71 if (a->maskbits != b->maskbits)
77 - subnetishost - is a subnet in fact a single host?
83 return (a->maskbits == addrlenof(&a->addr)*8) ? 1 : 0;
87 - samesaid - are two SA IDs the same?
94 if (a->spi != b->spi) /* test first, most likely to be different */
96 if (!sameaddr(&a->dst, &b->dst))
98 if (a->proto != b->proto)
104 - sameaddrtype - do two addresses have the same type?
111 return (addrtypeof(a) == addrtypeof(b)) ? 1 : 0;
115 - samesubnettype - do two subnets have the same type?
122 return (subnettypeof(a) == subnettypeof(b)) ? 1 : 0;
126 - addrinsubnet - is this address in this subnet?
133 if (addrtypeof(a) != subnettypeof(s))
135 if (!samenbits(a, &s->addr, s->maskbits))
141 - subnetinsubnet - is one subnet within another?
148 if (subnettypeof(a) != subnettypeof(b))
150 if (a->maskbits < b->maskbits) /* a is bigger than b */
152 if (!samenbits(&a->addr, &b->addr, b->maskbits))
158 - samenbits - do two addresses have the same first n bits?
161 samenbits(a, b, nbits)
166 const unsigned char *ap;
167 const unsigned char *bp;
171 if (addrtypeof(a) != addrtypeof(b))
172 return 0; /* arbitrary */
173 n = addrbytesptr(a, &ap);
175 return 0; /* arbitrary */
176 (void) addrbytesptr(b, &bp);
178 return 0; /* "can't happen" */
180 for (; nbits >= 8 && *ap == *bp; nbits -= 8, ap++, bp++)
184 if (nbits > 0) { /* partial byte */
185 m = ~(0xff >> nbits);
186 if ((*ap & m) != (*bp & m))