2 * extract parts of an ip_subnet, and related
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: subnettypeof.c,v 1.4 2000/09/08 18:03:45 henry Exp $
21 - subnettypeof - get the address type of an ip_subnet
27 return src->addr.u.v4.sin_family;
31 - networkof - get the network address of a subnet
42 - maskof - get the mask of a subnet, as an address
50 unsigned char buf[16];
51 size_t n = addrlenof(&src->addr);
54 if (src->maskbits > n*8 || n > sizeof(buf))
55 return; /* "can't happen" */
58 for (b = src->maskbits; b >= 8; b -= 8)
61 *p++ = (0xff << (8 - b)) & 0xff;
65 (void) initaddr(buf, n, addrtypeof(&src->addr), dst);
69 - masktocount - convert a mask, expressed as an address, to a bit count
71 int /* -1 if not valid mask */
73 const ip_address *src;
76 unsigned const char *bp;
78 unsigned const char *p;
79 unsigned const char *stop;
81 n = addrbytesptr(src, &bp);
89 while (p < stop && *p == 0xff) {
93 if (p < stop && *p != 0) { /* boundary in mid-byte */
100 return -1; /* bits not contiguous */
102 while (p < stop && *p == 0)