/*-1* SPDX-License-Identifier: BSD-4-Clause2*3* Copyright (c) 1995 Bill Paul <[email protected]>.4* Copyright (c) 2007 Robert N. M. Watson5* All rights reserved.6*7* Redistribution and use in source and binary forms, with or without8* modification, are permitted provided that the following conditions9* are met:10* 1. Redistributions of source code must retain the above copyright11* notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15* 3. All advertising materials mentioning features or use of this software16* must display the following acknowledgement:17* This product includes software developed by Bill Paul.18* 4. Neither the name of the author nor the names of any co-contributors19* may be used to endorse or promote products derived from this software20* without specific prior written permission.21*22* THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND23* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE24* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE25* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE26* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL27* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS28* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)29* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT30* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY31* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF32* SUCH DAMAGE.33*34* ethernet address conversion and lookup routines35*36* Written by Bill Paul <[email protected]>37* Center for Telecommunications Research38* Columbia University, New York City39*/4041#include <sys/param.h>42#include <sys/socket.h>4344#include <net/ethernet.h>4546#ifdef YP47#include <rpc/rpc.h>48#include <rpcsvc/yp_prot.h>49#include <rpcsvc/ypclnt.h>50#endif5152#include <paths.h>53#include <stdio.h>54#include <stdlib.h>55#include <string.h>5657#ifndef _PATH_ETHERS58#define _PATH_ETHERS "/etc/ethers"59#endif6061/*62* Parse a string of text containing an ethernet address and hostname and63* separate it into its component parts.64*/65int66ether_line(const char *l, struct ether_addr *e, char *hostname)67{68int i, o[6];6970i = sscanf(l, "%x:%x:%x:%x:%x:%x %s", &o[0], &o[1], &o[2], &o[3],71&o[4], &o[5], hostname);72if (i == 7) {73for (i = 0; i < 6; i++)74e->octet[i] = o[i];75return (0);76} else {77return (-1);78}79}8081/*82* Convert an ASCII representation of an ethernet address to binary form.83*/84struct ether_addr *85ether_aton_r(const char *a, struct ether_addr *e)86{87int i;88unsigned int o0, o1, o2, o3, o4, o5;8990i = sscanf(a, "%x:%x:%x:%x:%x:%x", &o0, &o1, &o2, &o3, &o4, &o5);91if (i != 6)92return (NULL);93e->octet[0]=o0;94e->octet[1]=o1;95e->octet[2]=o2;96e->octet[3]=o3;97e->octet[4]=o4;98e->octet[5]=o5;99return (e);100}101102struct ether_addr *103ether_aton(const char *a)104{105static struct ether_addr e;106107return (ether_aton_r(a, &e));108}109110/*111* Convert a binary representation of an ethernet address to an ASCII string.112*/113char *114ether_ntoa_r(const struct ether_addr *n, char *a)115{116int i;117118i = sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", n->octet[0],119n->octet[1], n->octet[2], n->octet[3], n->octet[4], n->octet[5]);120if (i < 17)121return (NULL);122return (a);123}124125char *126ether_ntoa(const struct ether_addr *n)127{128static char a[18];129130return (ether_ntoa_r(n, a));131}132133/*134* Map an ethernet address to a hostname. Use either /etc/ethers or NIS/YP.135*/136int137ether_ntohost(char *hostname, const struct ether_addr *e)138{139FILE *fp;140char buf[BUFSIZ + 2];141struct ether_addr local_ether;142char local_host[MAXHOSTNAMELEN];143#ifdef YP144char *result;145int resultlen;146char *ether_a;147char *yp_domain;148#endif149150if ((fp = fopen(_PATH_ETHERS, "re")) == NULL)151return (1);152while (fgets(buf,BUFSIZ,fp)) {153if (buf[0] == '#')154continue;155#ifdef YP156if (buf[0] == '+') {157if (yp_get_default_domain(&yp_domain))158continue;159ether_a = ether_ntoa(e);160if (yp_match(yp_domain, "ethers.byaddr", ether_a,161strlen(ether_a), &result, &resultlen)) {162continue;163}164strncpy(buf, result, resultlen);165buf[resultlen] = '\0';166free(result);167}168#endif169if (!ether_line(buf, &local_ether, local_host)) {170if (!bcmp((char *)&local_ether.octet[0],171(char *)&e->octet[0], 6)) {172/* We have a match. */173strcpy(hostname, local_host);174fclose(fp);175return(0);176}177}178}179fclose(fp);180return (1);181}182183/*184* Map a hostname to an ethernet address using /etc/ethers or NIS/YP.185*/186int187ether_hostton(const char *hostname, struct ether_addr *e)188{189FILE *fp;190char buf[BUFSIZ + 2];191struct ether_addr local_ether;192char local_host[MAXHOSTNAMELEN];193#ifdef YP194char *result;195int resultlen;196char *yp_domain;197#endif198199if ((fp = fopen(_PATH_ETHERS, "re")) == NULL)200return (1);201while (fgets(buf,BUFSIZ,fp)) {202if (buf[0] == '#')203continue;204#ifdef YP205if (buf[0] == '+') {206if (yp_get_default_domain(&yp_domain))207continue;208if (yp_match(yp_domain, "ethers.byname", hostname,209strlen(hostname), &result, &resultlen)) {210continue;211}212strncpy(buf, result, resultlen);213buf[resultlen] = '\0';214free(result);215}216#endif217if (!ether_line(buf, &local_ether, local_host)) {218if (!strcmp(hostname, local_host)) {219/* We have a match. */220bcopy((char *)&local_ether.octet[0],221(char *)&e->octet[0], 6);222fclose(fp);223return(0);224}225}226}227fclose(fp);228return (1);229}230231232