ether_addr.c revision 2
2N/A/*
2N/A * CDDL HEADER START
2N/A *
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 *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
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 *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1990, 2011, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
2N/A/* All Rights Reserved */
2N/A
2N/A/*
2N/A * University Copyright- Copyright (c) 1982, 1986, 1988
2N/A * The Regents of the University of California
2N/A * All Rights Reserved
2N/A *
2N/A * University Acknowledgment- Portions of this document are derived from
2N/A * software developed by the University of California, Berkeley, and its
2N/A * contributors.
2N/A */
2N/A
2N/A/*
2N/A * All routines necessary to deal the "ethers" database. The sources
2N/A * contain mappings between 48 bit ethernet addresses and corresponding
2N/A * hosts names. The addresses have an ascii representation of the form
2N/A * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the
2N/A * bytes are always in network order.
2N/A */
2N/A
2N/A#include <stdio.h>
2N/A#include <ctype.h>
2N/A#include <string.h>
2N/A#include <stdlib.h>
2N/A#include <sys/types.h>
2N/A#include <thread.h>
2N/A#include <pthread.h>
2N/A#include <sys/socket.h>
2N/A#include <net/if.h>
2N/A#include <netinet/in.h>
2N/A#include <netinet/if_ether.h>
2N/A#include <nss_dbdefs.h>
2N/A
2N/Aint str2ether(const char *, int, void *, char *, int);
2N/A
2N/Astatic DEFINE_NSS_DB_ROOT(db_root);
2N/A
2N/Avoid
2N/A_nss_initf_ethers(nss_db_params_t *p)
2N/A{
2N/A p->name = NSS_DBNAM_ETHERS;
2N/A p->default_config = NSS_DEFCONF_ETHERS;
2N/A}
2N/A
2N/A/*
2N/A * Given a host's name, this routine finds the corresponding 48 bit
2N/A * ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
2N/A * Returns zero if successful, non-zero otherwise.
2N/A */
2N/Aint
2N/Aether_hostton(
2N/A const char *host, /* function input */
2N/A struct ether_addr *e /* function output */
2N/A)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A /*
2N/A * let the backend do the allocation to store stuff for parsing.
2N/A */
2N/A NSS_XbyY_INIT(&arg, e, NULL, 0, str2ether);
2N/A arg.key.name = host;
2N/A res = nss_search(&db_root, _nss_initf_ethers,
2N/A NSS_DBOP_ETHERS_HOSTTON, &arg);
2N/A (void) NSS_XbyY_FINI(&arg);
2N/A return (arg.status = res);
2N/A}
2N/A
2N/A/*
2N/A * Given a 48 bit ethernet address, it finds the corresponding hostname
2N/A * ethernet address based on the "ethers" policy in /etc/nsswitch.conf.
2N/A * Returns zero if successful, non-zero otherwise.
2N/A */
2N/Aint
2N/Aether_ntohost(
2N/A char *host, /* function output */
2N/A const struct ether_addr *e /* function input */
2N/A)
2N/A{
2N/A nss_XbyY_args_t arg;
2N/A nss_status_t res;
2N/A
2N/A /*
2N/A * let the backend do the allocation to store stuff for parsing.
2N/A */
2N/A NSS_XbyY_INIT(&arg, NULL, host, 0, str2ether);
2N/A arg.key.ether = (void *)e;
2N/A res = nss_search(&db_root, _nss_initf_ethers,
2N/A NSS_DBOP_ETHERS_NTOHOST, &arg);
2N/A /* memcpy(host, ether_res.host, strlen(ether_res.host)); */
2N/A (void) NSS_XbyY_FINI(&arg);
2N/A return (arg.status = res);
2N/A}
2N/A
2N/A/*
2N/A * Parses a line from "ethers" database into its components. The line has
2N/A * the form 8:0:20:1:17:c8 krypton
2N/A * where the first part is a 48 bit ethernet address and the second is
2N/A * the corresponding hosts name.
2N/A * Returns zero if successful, non-zero otherwise.
2N/A */
2N/Aint
2N/Aether_line(
2N/A const char *s, /* the string to be parsed */
2N/A struct ether_addr *e, /* ethernet address struct to be filled in */
2N/A char *hostname /* hosts name to be set */
2N/A)
2N/A{
2N/A int i;
2N/A uint_t t[6];
2N/A
2N/A /* LINTED unbounded string specifier */
2N/A i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
2N/A &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
2N/A if (i != 7) {
2N/A return (7 - i);
2N/A }
2N/A for (i = 0; i < 6; i++)
2N/A e->ether_addr_octet[i] = (uchar_t)t[i];
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Parses a line from "ethers" database into its components.
2N/A * Useful for the vile purposes of the backends that
2N/A * expect a str2ether() format.
2N/A *
2N/A * This function, after parsing the instr line, will
2N/A * place the resulting struct ether_addr in b->buf.result only if
2N/A * b->buf.result is initialized (not NULL). I.e. it always happens
2N/A * for "files" backend (that needs to parse input line and
2N/A * then do a match for the ether key) and happens for "nis"
2N/A * backend only if the call was ether_hostton.
2N/A *
2N/A * Also, it will place the resulting hostname into b->buf.buffer
2N/A * only if b->buf.buffer is initialized. I.e. it always happens
2N/A * for "files" backend (that needs to parse input line and
2N/A * then do a match for the host key) and happens for "nis"
2N/A * backend only if the call was ether_ntohost.
2N/A *
2N/A * Cannot use the sscanf() technique for parsing because instr
2N/A * is a read-only, not necessarily null-terminated, buffer.
2N/A *
2N/A * Return values: 0 = success, 1 = parse error, 2 = erange ...
2N/A * The structure pointer passed in is a structure in the caller's space
2N/A * wherein the field pointers would be set to areas in the buffer if
2N/A * need be. instring and buffer should be separate areas.
2N/A */
2N/A#define DIGIT(x) (isdigit(x) ? (x) - '0' : \
2N/A islower(x) ? (x) + 10 - 'a' : (x) + 10 - 'A')
2N/A#define lisalnum(x) (isdigit(x) || \
2N/A ((x) >= 'a' && (x) <= 'z') || ((x) >= 'A' && (x) <= 'Z'))
2N/A/* ARGSUSED */
2N/Aint
2N/Astr2ether(const char *instr, int lenstr, void *ent, char *buffer, int buflen)
2N/A{
2N/A uchar_t *ether = (uchar_t *)ent;
2N/A char *host = buffer;
2N/A const char *p, *limit, *start;
2N/A ptrdiff_t i;
2N/A
2N/A p = instr;
2N/A limit = p + lenstr;
2N/A
2N/A /* skip beginning whitespace, if any */
2N/A while (p < limit && isspace(*p))
2N/A p++;
2N/A
2N/A if (ether) { /* parse ether */
2N/A for (i = 0; i < 6; i++) {
2N/A int j = 0, n = 0;
2N/A
2N/A start = p;
2N/A while (p < limit && lisalnum(start[j])) {
2N/A /* don't worry about overflow here */
2N/A n = 16 * n + DIGIT(start[j]);
2N/A j++;
2N/A p++;
2N/A }
2N/A if (*p != ':' && i < 5) {
2N/A return (NSS_STR_PARSE_PARSE);
2N/A } else {
2N/A p++;
2N/A *(ether + i) = (uchar_t)n;
2N/A }
2N/A }
2N/A } else { /* skip ether */
2N/A while (p < limit && !isspace(*p))
2N/A p++;
2N/A }
2N/A if (host) { /* parse host */
2N/A while (p < limit && isspace(*p)) /* skip whitespace */
2N/A p++;
2N/A start = p;
2N/A while (p < limit && !isspace(*p)) /* skip hostname */
2N/A p++;
2N/A if ((i = (p - start)) < MAXHOSTNAMELEN) {
2N/A (void) memcpy(host, start, i);
2N/A host[i] = '\0';
2N/A } else
2N/A return (NSS_STR_PARSE_ERANGE); /* failure */
2N/A }
2N/A return (NSS_STR_PARSE_SUCCESS);
2N/A}
2N/A
2N/Atypedef struct {
2N/A char ea_string[18];
2N/A struct ether_addr ea_addr;
2N/A} eabuf_t;
2N/A
2N/Astatic eabuf_t *
2N/Aea_buf(void)
2N/A{
2N/A static thread_key_t key = THR_ONCE_KEY;
2N/A static eabuf_t ea_main;
2N/A eabuf_t *eabuf;
2N/A
2N/A if (thr_main())
2N/A return (&ea_main);
2N/A
2N/A if (thr_keycreate_once(&key, free) != 0)
2N/A return (NULL);
2N/A eabuf = pthread_getspecific(key);
2N/A if (eabuf == NULL) {
2N/A eabuf = malloc(sizeof (eabuf_t));
2N/A (void) thr_setspecific(key, eabuf);
2N/A }
2N/A return (eabuf);
2N/A}
2N/A
2N/A/*
2N/A * Converts a 48 bit ethernet number to its string representation.
2N/A */
2N/Achar *
2N/Aether_ntoa(const struct ether_addr *e)
2N/A{
2N/A eabuf_t *eabuf;
2N/A char *s;
2N/A
2N/A if ((eabuf = ea_buf()) == NULL)
2N/A return (NULL);
2N/A s = eabuf->ea_string;
2N/A (void) sprintf(s, "%x:%x:%x:%x:%x:%x",
2N/A e->ether_addr_octet[0], e->ether_addr_octet[1],
2N/A e->ether_addr_octet[2], e->ether_addr_octet[3],
2N/A e->ether_addr_octet[4], e->ether_addr_octet[5]);
2N/A return (s);
2N/A}
2N/A
2N/A/*
2N/A * Converts an ethernet address representation back into its 48 bits.
2N/A */
2N/Astruct ether_addr *
2N/Aether_aton(const char *s)
2N/A{
2N/A eabuf_t *eabuf;
2N/A struct ether_addr *e;
2N/A int i;
2N/A uint_t t[6];
2N/A
2N/A if ((eabuf = ea_buf()) == NULL)
2N/A return (NULL);
2N/A e = &eabuf->ea_addr;
2N/A i = sscanf(s, " %x:%x:%x:%x:%x:%x",
2N/A &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
2N/A if (i != 6)
2N/A return (NULL);
2N/A for (i = 0; i < 6; i++)
2N/A e->ether_addr_octet[i] = (uchar_t)t[i];
2N/A return (e);
2N/A}