1*2830Sdjl /*
2*2830Sdjl * CDDL HEADER START
3*2830Sdjl *
4*2830Sdjl * The contents of this file are subject to the terms of the
5*2830Sdjl * Common Development and Distribution License (the "License").
6*2830Sdjl * You may not use this file except in compliance with the License.
7*2830Sdjl *
8*2830Sdjl * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*2830Sdjl * or http://www.opensolaris.org/os/licensing.
10*2830Sdjl * See the License for the specific language governing permissions
11*2830Sdjl * and limitations under the License.
12*2830Sdjl *
13*2830Sdjl * When distributing Covered Code, include this CDDL HEADER in each
14*2830Sdjl * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*2830Sdjl * If applicable, add the following below this CDDL HEADER, with the
16*2830Sdjl * fields enclosed by brackets "[]" replaced with your own identifying
17*2830Sdjl * information: Portions Copyright [yyyy] [name of copyright owner]
18*2830Sdjl *
19*2830Sdjl * CDDL HEADER END
20*2830Sdjl */
21*2830Sdjl /*
22*2830Sdjl * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
23*2830Sdjl * Use is subject to license terms.
24*2830Sdjl */
25*2830Sdjl
26*2830Sdjl #pragma ident "%Z%%M% %I% %E% SMI"
27*2830Sdjl
28*2830Sdjl /*
29*2830Sdjl * Routines to handle getnet* calls in nscd
30*2830Sdjl */
31*2830Sdjl
32*2830Sdjl #include <string.h>
33*2830Sdjl #include <stdlib.h>
34*2830Sdjl #include <sys/types.h>
35*2830Sdjl #include <sys/socket.h>
36*2830Sdjl #include <netinet/in.h>
37*2830Sdjl #include <arpa/inet.h>
38*2830Sdjl #include "cache.h"
39*2830Sdjl
40*2830Sdjl #define nam_db ctx->nsc_db[0]
41*2830Sdjl #define addr_db ctx->nsc_db[1]
42*2830Sdjl
43*2830Sdjl #define NSC_NAME_NETWORKS_BYNAME "getnetbyname"
44*2830Sdjl #define NSC_NAME_NETWORKS_BYADDR "getnetbyaddr"
45*2830Sdjl
46*2830Sdjl static int netaddr_compar(const void *, const void *);
47*2830Sdjl static uint_t netaddr_gethash(nss_XbyY_key_t *, int);
48*2830Sdjl static void netaddr_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
49*2830Sdjl
50*2830Sdjl void
net_init_ctx(nsc_ctx_t * ctx)51*2830Sdjl net_init_ctx(nsc_ctx_t *ctx) {
52*2830Sdjl ctx->dbname = NSS_DBNAM_NETWORKS;
53*2830Sdjl ctx->db_count = 2;
54*2830Sdjl ctx->file_name = "/etc/inet/networks";
55*2830Sdjl
56*2830Sdjl nam_db = make_cache(nsc_key_ces,
57*2830Sdjl NSS_DBOP_NETWORKS_BYNAME,
58*2830Sdjl NSC_NAME_NETWORKS_BYNAME,
59*2830Sdjl NULL, NULL, NULL, nsc_ht_default, -1);
60*2830Sdjl
61*2830Sdjl addr_db = make_cache(nsc_key_other,
62*2830Sdjl NSS_DBOP_NETWORKS_BYADDR,
63*2830Sdjl NSC_NAME_NETWORKS_BYADDR,
64*2830Sdjl netaddr_compar,
65*2830Sdjl netaddr_getlogstr,
66*2830Sdjl netaddr_gethash, nsc_ht_default, -1);
67*2830Sdjl }
68*2830Sdjl
69*2830Sdjl static void
netaddr_getlogstr(char * name,char * whoami,size_t len,nss_XbyY_args_t * argp)70*2830Sdjl netaddr_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
71*2830Sdjl if (argp->key.netaddr.type == AF_INET) {
72*2830Sdjl uint32_t net;
73*2830Sdjl uchar_t *up;
74*2830Sdjl
75*2830Sdjl net = htonl(argp->key.netaddr.net);
76*2830Sdjl up = (uchar_t *)&net;
77*2830Sdjl
78*2830Sdjl if (up[0])
79*2830Sdjl (void) snprintf(whoami, len, "%s [key=%d.%d.%d.%d]",
80*2830Sdjl name,
81*2830Sdjl up[0], up[1], up[2], up[3]);
82*2830Sdjl else if (up[1])
83*2830Sdjl (void) snprintf(whoami, len, "%s [key=%d.%d.%d]",
84*2830Sdjl name,
85*2830Sdjl up[1], up[2], up[3]);
86*2830Sdjl else if (up[2])
87*2830Sdjl (void) snprintf(whoami, len, "%s [key=%d.%d]",
88*2830Sdjl name,
89*2830Sdjl up[2], up[3]);
90*2830Sdjl else
91*2830Sdjl (void) snprintf(whoami, len, "%s [key=%d]",
92*2830Sdjl name,
93*2830Sdjl up[3]);
94*2830Sdjl } else {
95*2830Sdjl (void) snprintf(whoami, len, "%s [key=%d, %d]",
96*2830Sdjl name,
97*2830Sdjl argp->key.netaddr.net, argp->key.netaddr.type);
98*2830Sdjl }
99*2830Sdjl }
100*2830Sdjl
101*2830Sdjl static int
netaddr_compar(const void * n1,const void * n2)102*2830Sdjl netaddr_compar(const void *n1, const void *n2) {
103*2830Sdjl nsc_entry_t *e1, *e2;
104*2830Sdjl
105*2830Sdjl e1 = (nsc_entry_t *)n1;
106*2830Sdjl e2 = (nsc_entry_t *)n2;
107*2830Sdjl
108*2830Sdjl if (e1->key.netaddr.type > e2->key.netaddr.type)
109*2830Sdjl return (1);
110*2830Sdjl else if (e1->key.netaddr.type < e2->key.netaddr.type)
111*2830Sdjl return (-1);
112*2830Sdjl
113*2830Sdjl return (_NSC_INT_KEY_CMP(e1->key.netaddr.net, e2->key.netaddr.net));
114*2830Sdjl }
115*2830Sdjl
116*2830Sdjl static uint_t
netaddr_gethash(nss_XbyY_key_t * key,int htsize)117*2830Sdjl netaddr_gethash(nss_XbyY_key_t *key, int htsize) {
118*2830Sdjl return (db_gethash(&key->netaddr.net,
119*2830Sdjl sizeof (key->netaddr.net), htsize));
120*2830Sdjl }
121