1*e11c3f44Smeem /* 2*e11c3f44Smeem * CDDL HEADER START 3*e11c3f44Smeem * 4*e11c3f44Smeem * The contents of this file are subject to the terms of the 5*e11c3f44Smeem * Common Development and Distribution License (the "License"). 6*e11c3f44Smeem * You may not use this file except in compliance with the License. 7*e11c3f44Smeem * 8*e11c3f44Smeem * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9*e11c3f44Smeem * or http://www.opensolaris.org/os/licensing. 10*e11c3f44Smeem * See the License for the specific language governing permissions 11*e11c3f44Smeem * and limitations under the License. 12*e11c3f44Smeem * 13*e11c3f44Smeem * When distributing Covered Code, include this CDDL HEADER in each 14*e11c3f44Smeem * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15*e11c3f44Smeem * If applicable, add the following below this CDDL HEADER, with the 16*e11c3f44Smeem * fields enclosed by brackets "[]" replaced with your own identifying 17*e11c3f44Smeem * information: Portions Copyright [yyyy] [name of copyright owner] 18*e11c3f44Smeem * 19*e11c3f44Smeem * CDDL HEADER END 20*e11c3f44Smeem */ 21*e11c3f44Smeem 22*e11c3f44Smeem /* 23*e11c3f44Smeem * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 24*e11c3f44Smeem * Use is subject to license terms. 25*e11c3f44Smeem */ 26*e11c3f44Smeem 27*e11c3f44Smeem #include <unistd.h> 28*e11c3f44Smeem #include <netinet/in.h> 29*e11c3f44Smeem #include <libinetutil.h> 30*e11c3f44Smeem 31*e11c3f44Smeem extern int getnetmaskbyaddr(const struct in_addr, struct in_addr *); 32*e11c3f44Smeem 33*e11c3f44Smeem /* 34*e11c3f44Smeem * Internet utility functions. 35*e11c3f44Smeem */ 36*e11c3f44Smeem 37*e11c3f44Smeem /* 38*e11c3f44Smeem * Given a host-order address, calculate client's default net mask. 39*e11c3f44Smeem * Consult netmasks database to see if net is further subnetted. 40*e11c3f44Smeem * We'll only snag the first netmask that matches our criteria. 41*e11c3f44Smeem * We return the resultant netmask in host order. 42*e11c3f44Smeem */ 43*e11c3f44Smeem void 44*e11c3f44Smeem get_netmask4(const struct in_addr *n_addrp, struct in_addr *s_addrp) 45*e11c3f44Smeem { 46*e11c3f44Smeem struct in_addr hp, tp; 47*e11c3f44Smeem 48*e11c3f44Smeem /* 49*e11c3f44Smeem * First check if VLSM is in use. 50*e11c3f44Smeem */ 51*e11c3f44Smeem hp.s_addr = htonl(n_addrp->s_addr); 52*e11c3f44Smeem if (getnetmaskbyaddr(hp, &tp) == 0) { 53*e11c3f44Smeem s_addrp->s_addr = ntohl(tp.s_addr); 54*e11c3f44Smeem return; 55*e11c3f44Smeem } 56*e11c3f44Smeem 57*e11c3f44Smeem /* 58*e11c3f44Smeem * Fall back on standard classed networks. 59*e11c3f44Smeem */ 60*e11c3f44Smeem if (IN_CLASSA(n_addrp->s_addr)) 61*e11c3f44Smeem s_addrp->s_addr = IN_CLASSA_NET; 62*e11c3f44Smeem else if (IN_CLASSB(n_addrp->s_addr)) 63*e11c3f44Smeem s_addrp->s_addr = IN_CLASSB_NET; 64*e11c3f44Smeem else if (IN_CLASSC(n_addrp->s_addr)) 65*e11c3f44Smeem s_addrp->s_addr = IN_CLASSC_NET; 66*e11c3f44Smeem else 67*e11c3f44Smeem s_addrp->s_addr = IN_CLASSE_NET; 68*e11c3f44Smeem } 69*e11c3f44Smeem 70*e11c3f44Smeem /* 71*e11c3f44Smeem * Checks if the IP addresses `ssp1' and `ssp2' are equal. 72*e11c3f44Smeem */ 73*e11c3f44Smeem boolean_t 74*e11c3f44Smeem sockaddrcmp(const struct sockaddr_storage *ssp1, 75*e11c3f44Smeem const struct sockaddr_storage *ssp2) 76*e11c3f44Smeem { 77*e11c3f44Smeem struct in_addr addr1, addr2; 78*e11c3f44Smeem const struct in6_addr *addr6p1, *addr6p2; 79*e11c3f44Smeem 80*e11c3f44Smeem if (ssp1->ss_family != ssp2->ss_family) 81*e11c3f44Smeem return (B_FALSE); 82*e11c3f44Smeem 83*e11c3f44Smeem if (ssp1 == ssp2) 84*e11c3f44Smeem return (B_TRUE); 85*e11c3f44Smeem 86*e11c3f44Smeem switch (ssp1->ss_family) { 87*e11c3f44Smeem case AF_INET: 88*e11c3f44Smeem addr1 = ((const struct sockaddr_in *)ssp1)->sin_addr; 89*e11c3f44Smeem addr2 = ((const struct sockaddr_in *)ssp2)->sin_addr; 90*e11c3f44Smeem return (addr1.s_addr == addr2.s_addr); 91*e11c3f44Smeem case AF_INET6: 92*e11c3f44Smeem addr6p1 = &((const struct sockaddr_in6 *)ssp1)->sin6_addr; 93*e11c3f44Smeem addr6p2 = &((const struct sockaddr_in6 *)ssp2)->sin6_addr; 94*e11c3f44Smeem return (IN6_ARE_ADDR_EQUAL(addr6p1, addr6p2)); 95*e11c3f44Smeem } 96*e11c3f44Smeem return (B_FALSE); 97*e11c3f44Smeem } 98