getether.c revision 2830:5228d1267a01
1/*
2 * CDDL HEADER START
3 *
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.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21/*
22 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23 * Use is subject to license terms.
24 */
25
26#pragma ident	"%Z%%M%	%I%	%E% SMI"
27
28/*
29 * Routines to handle ether_*to* calls in nscd
30 */
31
32#include <stdlib.h>
33#include <net/if.h>
34#include <netinet/in.h>
35#include <netinet/if_ether.h>
36#include <string.h>
37#include "cache.h"
38
39#define	host_db	ctx->nsc_db[0]
40#define	addr_db	ctx->nsc_db[1]
41
42#define	NSC_NAME_ETHERS_HOSTTON	"ether_hostton"
43#define	NSC_NAME_ETHERS_NTOHOST	"ether_ntohost"
44
45static void ether_getlogstr(char *, char *, size_t, nss_XbyY_args_t *);
46static int ether_compar(const void *, const void *);
47static uint_t ether_gethash(nss_XbyY_key_t *, int);
48
49void
50ether_init_ctx(nsc_ctx_t *ctx) {
51	ctx->dbname = NSS_DBNAM_ETHERS;
52	ctx->file_name = "/etc/ethers";
53	ctx->db_count = 2;
54	host_db = make_cache(nsc_key_cis,
55			NSS_DBOP_ETHERS_HOSTTON,
56			NSC_NAME_ETHERS_HOSTTON,
57			NULL, NULL, NULL, nsc_ht_default, -1);
58
59	addr_db = make_cache(nsc_key_other,
60			NSS_DBOP_ETHERS_NTOHOST,
61			NSC_NAME_ETHERS_NTOHOST,
62			ether_compar,
63			ether_getlogstr,
64			ether_gethash, nsc_ht_default, -1);
65}
66
67static int
68ether_compar(const void *n1, const void *n2) {
69	nsc_entry_t	*e1, *e2;
70	int		res;
71
72	e1 = (nsc_entry_t *)n1;
73	e2 = (nsc_entry_t *)n2;
74	if (ether_cmp(e1->key.ether, e2->key.ether) != 0) {
75		res = memcmp(e1->key.ether, e2->key.ether,
76			sizeof (struct ether_addr));
77		return (_NSC_INT_KEY_CMP(res, 0));
78	}
79	return (0);
80}
81
82static uint_t
83ether_gethash(nss_XbyY_key_t *key, int htsize) {
84	return (db_gethash(key->ether, sizeof (struct ether_addr),
85			htsize));
86}
87
88static void
89ether_getlogstr(char *name, char *whoami, size_t len, nss_XbyY_args_t *argp) {
90	struct ether_addr *e;
91	e = (struct ether_addr *)argp->key.ether;
92	(void) snprintf(whoami, len, "%s [key=%x:%x:%x:%x:%x:%x]",
93			name,
94			e->ether_addr_octet[0], e->ether_addr_octet[1],
95			e->ether_addr_octet[2], e->ether_addr_octet[3],
96			e->ether_addr_octet[4], e->ether_addr_octet[5]);
97}
98