8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libsocket / inet / netmasks.c
blobf9e5ef4049056dd5c769a42f99df594b6e7b9a90
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
22 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #pragma ident "%Z%%M% %I% %E% SMI"
29 * All routines necessary to deal the "netmasks" database. The sources
30 * contain mappings between 32 bit Internet addresses and corresponding
31 * 32 bit Internet address masks. The addresses are in dotted internet
32 * address notation.
35 #include <stdio.h>
36 #include <ctype.h>
37 #include <string.h>
38 #include <stdlib.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <net/if.h>
42 #include <netinet/in.h>
43 #include <arpa/inet.h>
44 #include <nss_dbdefs.h>
46 int str2addr(const char *, int, void *, char *, int);
48 static DEFINE_NSS_DB_ROOT(db_root);
50 void
51 _nss_initf_netmasks(nss_db_params_t *p)
53 p->name = NSS_DBNAM_NETMASKS;
54 p->default_config = NSS_DEFCONF_NETMASKS;
58 * Print a network number such as 129.144 as well as an IP address.
59 * Assumes network byte order for both IP addresses and network numbers
60 * (Network numbers are normally passed around in host byte order).
61 * to be MT safe, use a passed in buffer like otherget*_r APIs.
63 static char *
64 inet_nettoa(struct in_addr in, char *result, int len)
66 uint32_t addr = in.s_addr;
67 uchar_t *up = (uchar_t *)&addr;
69 if (result == NULL)
70 return (NULL);
72 /* Omit leading zeros */
73 if (up[0]) {
74 (void) snprintf(result, len, "%d.%d.%d.%d",
75 up[0], up[1], up[2], up[3]);
76 } else if (up[1]) {
77 (void) snprintf(result, len, "%d.%d.%d", up[1], up[2], up[3]);
78 } else if (up[2]) {
79 (void) snprintf(result, len, "%d.%d", up[2], up[3]);
80 } else {
81 (void) snprintf(result, len, "%d", up[3]);
83 return (result);
87 * Given a 32 bit key look it up in the netmasks database
88 * based on the "netmasks" policy in /etc/nsswitch.conf.
89 * If the key is a network number with the trailing zero's removed
90 * (e.g. "192.9.200") this routine can't use inet_ntoa to convert
91 * the address to the string key.
92 * Returns zero if successful, non-zero otherwise.
94 static int
95 getnetmaskbykey(const struct in_addr addr, struct in_addr *mask)
97 nss_XbyY_args_t arg;
98 nss_status_t res;
99 char tmp[NSS_LINELEN_NETMASKS];
102 * let the backend do the allocation to store stuff for parsing.
103 * To simplify things, we put the dotted internet address form of
104 * the network address in the 'name' field as a filter to speed
105 * up the lookup.
107 if (inet_nettoa(addr, tmp, NSS_LINELEN_NETMASKS) == NULL)
108 return (NSS_NOTFOUND);
110 NSS_XbyY_INIT(&arg, mask, NULL, 0, str2addr);
111 arg.key.name = tmp;
112 res = nss_search(&db_root, _nss_initf_netmasks,
113 NSS_DBOP_NETMASKS_BYNET, &arg);
114 (void) NSS_XbyY_FINI(&arg);
115 return (arg.status = res);
119 * Given a 32 bit internet network number, it finds the corresponding netmask
120 * address based on the "netmasks" policy in /etc/nsswitch.conf.
121 * Returns zero if successful, non-zero otherwise.
122 * Check both for the (masked) network number and the shifted network
123 * number (e.g., both "10.0.0.0" and "10").
124 * Assumes that the caller passes in an unshifted number (or an IP address).
127 getnetmaskbynet(const struct in_addr net, struct in_addr *mask)
129 struct in_addr net1, net2;
130 uint32_t i;
132 i = ntohl(net.s_addr);
135 * Try looking for the network number both with and without
136 * the trailing zeros.
138 if ((i & IN_CLASSA_NET) == 0) {
139 /* Assume already a right-shifted network number */
140 net2.s_addr = htonl(i);
141 if ((i & IN_CLASSB_NET) != 0) {
142 net1.s_addr = htonl(i << IN_CLASSC_NSHIFT);
143 } else if ((i & IN_CLASSC_NET) != 0) {
144 net1.s_addr = htonl(i << IN_CLASSB_NSHIFT);
145 } else {
146 net1.s_addr = htonl(i << IN_CLASSA_NSHIFT);
148 } else if (IN_CLASSA(i)) {
149 net1.s_addr = htonl(i & IN_CLASSA_NET);
150 net2.s_addr = htonl(i >> IN_CLASSA_NSHIFT);
151 } else if (IN_CLASSB(i)) {
152 net1.s_addr = htonl(i & IN_CLASSB_NET);
153 net2.s_addr = htonl(i >> IN_CLASSB_NSHIFT);
154 } else {
155 net1.s_addr = htonl(i & IN_CLASSC_NET);
156 net2.s_addr = htonl(i >> IN_CLASSC_NSHIFT);
159 if (getnetmaskbykey(net1, mask) == 0) {
160 return (0);
162 if (getnetmaskbykey(net2, mask) == 0) {
163 return (0);
165 return (-1);
169 * Find the netmask used for an IP address.
170 * Returns zero if successful, non-zero otherwise.
172 * Support Variable Length Subnetmasks by looking for the longest
173 * matching subnetmask in the database.
174 * Start by looking for a match for the full IP address and
175 * mask off one rightmost bit after another until we find a match.
176 * Note that for a match the found netmask must match what was used
177 * for the lookup masking.
178 * As a fallback for compatibility finally lookup the network
179 * number with and without the trailing zeros.
180 * In order to suppress redundant lookups in the name service
181 * we keep the previous lookup key and compare against it before
182 * doing the lookup.
185 getnetmaskbyaddr(const struct in_addr addr, struct in_addr *mask)
187 struct in_addr prevnet, net;
188 uint32_t i, maskoff;
190 i = ntohl(addr.s_addr);
191 prevnet.s_addr = 0;
192 mask->s_addr = 0;
194 for (maskoff = 0xFFFFFFFF; maskoff != 0; maskoff = maskoff << 1) {
195 net.s_addr = htonl(i & maskoff);
197 if (net.s_addr != prevnet.s_addr) {
198 if (getnetmaskbykey(net, mask) != 0) {
199 mask->s_addr = 0;
202 if (htonl(maskoff) == mask->s_addr)
203 return (0);
205 prevnet.s_addr = net.s_addr;
209 * Non-VLSM fallback.
210 * Try looking for the network number with and without the trailing
211 * zeros.
213 return (getnetmaskbynet(addr, mask));
217 * Parse netmasks entry into its components. The network address is placed
218 * in buffer for use by check_addr for 'files' backend, to match the network
219 * address. The network address is placed in the buffer as a network order
220 * internet address, if buffer is non null. The network order form of the mask
221 * itself is placed in 'ent'.
224 str2addr(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
226 int retval;
227 struct in_addr *mask = (struct in_addr *)ent;
228 const char *p, *limit, *start;
229 struct in_addr addr;
230 int i;
231 char tmp[NSS_LINELEN_NETMASKS];
233 p = instr;
234 limit = p + lenstr;
235 retval = NSS_STR_PARSE_PARSE;
237 while (p < limit && isspace(*p)) /* skip leading whitespace */
238 p++;
240 if (buffer) { /* for 'files' backend verification */
241 for (start = p, i = 0; p < limit && !isspace(*p); p++)
242 i++;
243 if (p < limit && i < buflen) {
244 (void) memcpy(tmp, start, i);
245 tmp[i] = '\0';
246 addr.s_addr = inet_addr(tmp);
247 /* Addr will always be an ipv4 address (32bits) */
248 if (addr.s_addr == 0xffffffffUL)
249 return (NSS_STR_PARSE_PARSE);
250 else {
251 (void) memcpy(buffer, (char *)&addr,
252 sizeof (struct in_addr));
254 } else
255 return (NSS_STR_PARSE_ERANGE);
258 while (p < limit && isspace(*p)) /* skip intermediate */
259 p++;
261 if (mask) {
262 for (start = p, i = 0; p < limit && !isspace(*p); p++)
263 i++;
264 if (p <= limit) {
265 if ((i + 1) > NSS_LINELEN_NETMASKS)
266 return (NSS_STR_PARSE_ERANGE);
267 (void) memcpy(tmp, start, i);
268 tmp[i] = '\0';
269 addr.s_addr = inet_addr(tmp);
270 /* Addr will always be an ipv4 address (32bits) */
271 if (addr.s_addr == 0xffffffffUL)
272 retval = NSS_STR_PARSE_PARSE;
273 else {
274 mask->s_addr = addr.s_addr;
275 retval = NSS_STR_PARSE_SUCCESS;
280 return (retval);