netmasks.c revision 2
2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A#
pragma ident "%Z%%M% %I% %E% SMI" 2N/A * All routines necessary to deal the "netmasks" database. The sources 2N/A * contain mappings between 32 bit Internet addresses and corresponding 2N/A * 32 bit Internet address masks. The addresses are in dotted internet 2N/A * Print a network number such as 129.144 as well as an IP address. 2N/A * Assumes network byte order for both IP addresses and network numbers 2N/A * (Network numbers are normally passed around in host byte order). 2N/A * to be MT safe, use a passed in buffer like otherget*_r APIs. 2N/A /* Omit leading zeros */ 2N/A * Given a 32 bit key look it up in the netmasks database 2N/A * If the key is a network number with the trailing zero's removed 2N/A * (e.g. "192.9.200") this routine can't use inet_ntoa to convert 2N/A * the address to the string key. 2N/A * Returns zero if successful, non-zero otherwise. 2N/A * let the backend do the allocation to store stuff for parsing. 2N/A * To simplify things, we put the dotted internet address form of 2N/A * the network address in the 'name' field as a filter to speed 2N/A * Given a 32 bit internet network number, it finds the corresponding netmask 2N/A * Returns zero if successful, non-zero otherwise. 2N/A * Check both for the (masked) network number and the shifted network 2N/A * number (e.g., both "10.0.0.0" and "10"). 2N/A * Assumes that the caller passes in an unshifted number (or an IP address). 2N/A * Try looking for the network number both with and without 2N/A * the trailing zeros. 2N/A /* Assume already a right-shifted network number */ 2N/A * Find the netmask used for an IP address. 2N/A * Returns zero if successful, non-zero otherwise. 2N/A * Support Variable Length Subnetmasks by looking for the longest 2N/A * matching subnetmask in the database. 2N/A * Start by looking for a match for the full IP address and 2N/A * mask off one rightmost bit after another until we find a match. 2N/A * Note that for a match the found netmask must match what was used 2N/A * for the lookup masking. 2N/A * As a fallback for compatibility finally lookup the network 2N/A * number with and without the trailing zeros. 2N/A * In order to suppress redundant lookups in the name service 2N/A * we keep the previous lookup key and compare against it before 2N/A * Non-VLSM fallback. 2N/A * Try looking for the network number with and without the trailing 2N/A * Parse netmasks entry into its components. The network address is placed 2N/A * in buffer for use by check_addr for 'files' backend, to match the network 2N/A * address. The network address is placed in the buffer as a network order 2N/A * internet address, if buffer is non null. The network order form of the mask 2N/A * itself is placed in 'ent'. 2N/A if (
buffer) {
/* for 'files' backend verification */ 2N/A /* Addr will always be an ipv4 address (32bits) */ 2N/A /* Addr will always be an ipv4 address (32bits) */