17c478bdstevel@tonic-gate/*
27c478bdstevel@tonic-gate * CDDL HEADER START
37c478bdstevel@tonic-gate *
47c478bdstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bdstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bdstevel@tonic-gate * (the "License").  You may not use this file except in compliance
77c478bdstevel@tonic-gate * with the License.
87c478bdstevel@tonic-gate *
97c478bdstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bdstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bdstevel@tonic-gate * See the License for the specific language governing permissions
127c478bdstevel@tonic-gate * and limitations under the License.
137c478bdstevel@tonic-gate *
147c478bdstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bdstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bdstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bdstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bdstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bdstevel@tonic-gate *
207c478bdstevel@tonic-gate * CDDL HEADER END
217c478bdstevel@tonic-gate */
227c478bdstevel@tonic-gate/*
237c478bdstevel@tonic-gate * PPPoE common utilities and data.
247c478bdstevel@tonic-gate *
2532529ecrshoaib * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
267c478bdstevel@tonic-gate * Use is subject to license terms.
277c478bdstevel@tonic-gate */
287c478bdstevel@tonic-gate
297c478bdstevel@tonic-gate#pragma ident	"%Z%%M%	%I%	%E% SMI"
307c478bdstevel@tonic-gate
317c478bdstevel@tonic-gate#include <stdio.h>
327c478bdstevel@tonic-gate#include <unistd.h>
337c478bdstevel@tonic-gate#include <string.h>
347c478bdstevel@tonic-gate#include <errno.h>
357c478bdstevel@tonic-gate#include <netdb.h>
367c478bdstevel@tonic-gate#include <assert.h>
377c478bdstevel@tonic-gate#include <stropts.h>
387c478bdstevel@tonic-gate#include <sys/types.h>
397c478bdstevel@tonic-gate#include <inet/common.h>
407c478bdstevel@tonic-gate#include <netinet/in.h>
417c478bdstevel@tonic-gate#include <net/sppptun.h>
427c478bdstevel@tonic-gate#include <net/pppoe.h>
437c478bdstevel@tonic-gate#include <arpa/inet.h>
447c478bdstevel@tonic-gate
457c478bdstevel@tonic-gate#include "common.h"
467c478bdstevel@tonic-gate
477c478bdstevel@tonic-gate/* Not all functions are used by all applications.  Let lint know this. */
487c478bdstevel@tonic-gate/*LINTLIBRARY*/
497c478bdstevel@tonic-gate
507c478bdstevel@tonic-gate/* Common I/O buffers */
517c478bdstevel@tonic-gateuint32_t pkt_input[PKT_INPUT_LEN / sizeof (uint32_t)];
527c478bdstevel@tonic-gateuint32_t pkt_octl[PKT_OCTL_LEN / sizeof (uint32_t)];
537c478bdstevel@tonic-gateuint32_t pkt_output[PKT_OUTPUT_LEN / sizeof (uint32_t)];
547c478bdstevel@tonic-gate
557c478bdstevel@tonic-gateconst char tunnam[] = "/dev/" PPP_TUN_NAME;
567c478bdstevel@tonic-gate
577c478bdstevel@tonic-gateconst ether_addr_t ether_bcast = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
587c478bdstevel@tonic-gate
597c478bdstevel@tonic-gate/*
607c478bdstevel@tonic-gate * Wrapper for standard strerror() function -- the standard allows
617c478bdstevel@tonic-gate * that routine to return NULL, and that's inconvenient to handle.
627c478bdstevel@tonic-gate * This function never returns NULL.
637c478bdstevel@tonic-gate */
647c478bdstevel@tonic-gateconst char *
657c478bdstevel@tonic-gatemystrerror(int err)
667c478bdstevel@tonic-gate{
677c478bdstevel@tonic-gate	const char *estr;
687c478bdstevel@tonic-gate	static char ebuf[64];
697c478bdstevel@tonic-gate
707c478bdstevel@tonic-gate	if ((estr = strerror(err)) != NULL)
717c478bdstevel@tonic-gate		return (estr);
727c478bdstevel@tonic-gate	(void) snprintf(ebuf, sizeof (ebuf), "Error:%d", err);
737c478bdstevel@tonic-gate	return (ebuf);
747c478bdstevel@tonic-gate}
757c478bdstevel@tonic-gate
767c478bdstevel@tonic-gate/*
777c478bdstevel@tonic-gate * Wrapper for standard perror() function -- the standard definition
787c478bdstevel@tonic-gate * of perror doesn't include the program name in the output and is
797c478bdstevel@tonic-gate * thus inconvenient to use.
807c478bdstevel@tonic-gate */
817c478bdstevel@tonic-gatevoid
827c478bdstevel@tonic-gatemyperror(const char *emsg)
837c478bdstevel@tonic-gate{
847c478bdstevel@tonic-gate	(void) fprintf(stderr, "%s: %s: %s\n", myname, emsg,
857c478bdstevel@tonic-gate	    mystrerror(errno));
867c478bdstevel@tonic-gate}
877c478bdstevel@tonic-gate
887c478bdstevel@tonic-gate/*
897c478bdstevel@tonic-gate * Wrapper for standard getmsg() function.  Completely discards any
907c478bdstevel@tonic-gate * fragmented messages because we don't expect ever to see these from
917c478bdstevel@tonic-gate * a properly functioning tunnel driver.  Returns flags
927c478bdstevel@tonic-gate * (MORECTL|MOREDATA) as seen by interface.
937c478bdstevel@tonic-gate */
947c478bdstevel@tonic-gateint
957c478bdstevel@tonic-gatemygetmsg(int fd, struct strbuf *ctrl, struct strbuf *data, int *flags)
967c478bdstevel@tonic-gate{
977c478bdstevel@tonic-gate	int retv;
987c478bdstevel@tonic-gate	int hadflags;
997c478bdstevel@tonic-gate
1007c478bdstevel@tonic-gate	hadflags = getmsg(fd, ctrl, data, flags);
1017c478bdstevel@tonic-gate	if (hadflags <= 0 || !(hadflags & (MORECTL | MOREDATA)))
1027c478bdstevel@tonic-gate		return (hadflags);
1037c478bdstevel@tonic-gate
1047c478bdstevel@tonic-gate	do {
1057c478bdstevel@tonic-gate		if (flags != NULL)
1067c478bdstevel@tonic-gate			*flags = 0;
1077c478bdstevel@tonic-gate		retv = getmsg(fd, ctrl, data, flags);
1087c478bdstevel@tonic-gate	} while (retv > 0 || (retv < 0 && errno == EINTR));
1097c478bdstevel@tonic-gate
1107c478bdstevel@tonic-gate	/*
1117c478bdstevel@tonic-gate	 * What remains at this point is the tail end of the
1127c478bdstevel@tonic-gate	 * truncated message.  Toss it.
1137c478bdstevel@tonic-gate	 */
1147c478bdstevel@tonic-gate
1157c478bdstevel@tonic-gate	return (retv < 0 ? retv : hadflags);
1167c478bdstevel@tonic-gate}
1177c478bdstevel@tonic-gate
1187c478bdstevel@tonic-gate/*
1197c478bdstevel@tonic-gate * Common wrapper function for STREAMS I_STR ioctl.  Returns -1 on
1207c478bdstevel@tonic-gate * failure, 0 for success.
1217c478bdstevel@tonic-gate */
1227c478bdstevel@tonic-gateint
1237c478bdstevel@tonic-gatestrioctl(int fd, int cmd, void *ptr, int ilen, int olen)
1247c478bdstevel@tonic-gate{
1257c478bdstevel@tonic-gate	struct strioctl	str;
1267c478bdstevel@tonic-gate
1277c478bdstevel@tonic-gate	str.ic_cmd = cmd;
1287c478bdstevel@tonic-gate	str.ic_timout = 0;	/* Default timeout; 15 seconds */
1297c478bdstevel@tonic-gate	str.ic_len = ilen;
1307c478bdstevel@tonic-gate	str.ic_dp = ptr;
1317c478bdstevel@tonic-gate
1327c478bdstevel@tonic-gate	if (ioctl(fd, I_STR, &str) == -1) {
1337c478bdstevel@tonic-gate		return (-1);
1347c478bdstevel@tonic-gate	}
1357c478bdstevel@tonic-gate	if (str.ic_len != olen) {
1367c478bdstevel@tonic-gate		errno = EINVAL;
1377c478bdstevel@tonic-gate		return (-1);
1387c478bdstevel@tonic-gate	}
1397c478bdstevel@tonic-gate	return (0);
1407c478bdstevel@tonic-gate}
1417c478bdstevel@tonic-gate
1427c478bdstevel@tonic-gate/*
1437c478bdstevel@tonic-gate * Format a PPPoE header in the user's buffer.  The returned pointer
1447c478bdstevel@tonic-gate * is either identical to the first argument, or is NULL if it's not
1457c478bdstevel@tonic-gate * usable.  On entry, dptr should point to the first byte after the
1467c478bdstevel@tonic-gate * Ethertype field, codeval should be one of the POECODE_* values, and
1477c478bdstevel@tonic-gate * sessionid should be the assigned session ID number or one of the
1487c478bdstevel@tonic-gate * special POESESS_* values.
1497c478bdstevel@tonic-gate */
1507c478bdstevel@tonic-gatepoep_t *
1517c478bdstevel@tonic-gatepoe_mkheader(void *dptr, uint8_t codeval, int sessionid)
1527c478bdstevel@tonic-gate{
1537c478bdstevel@tonic-gate	poep_t *poep;
1547c478bdstevel@tonic-gate
1557c478bdstevel@tonic-gate	/* Discard obvious junk. */
1567c478bdstevel@tonic-gate	assert(dptr != NULL && IS_P2ALIGNED(dptr, sizeof (poep_t *)));
1577c478bdstevel@tonic-gate
1587c478bdstevel@tonic-gate	/* Initialize the header */
1597c478bdstevel@tonic-gate	poep = (poep_t *)dptr;
1607c478bdstevel@tonic-gate	poep->poep_version_type = POE_VERSION;
1617c478bdstevel@tonic-gate	poep->poep_code = codeval;
1627c478bdstevel@tonic-gate	poep->poep_session_id = htons(sessionid);
1637c478bdstevel@tonic-gate	poep->poep_length = htons(0);
1647c478bdstevel@tonic-gate	return (poep);
1657c478bdstevel@tonic-gate}
1667c478bdstevel@tonic-gate
1677c478bdstevel@tonic-gate/*
1687c478bdstevel@tonic-gate * Validate that a given tag is intact.  This is intended to be used
1697c478bdstevel@tonic-gate * in tag-parsing loops before attempting to access the tag data.
1707c478bdstevel@tonic-gate */
1717c478bdstevel@tonic-gateboolean_t
1727c478bdstevel@tonic-gatepoe_tagcheck(const poep_t *poep, int length, const uint8_t *tptr)
1737c478bdstevel@tonic-gate{
1747c478bdstevel@tonic-gate	int plen;
1757c478bdstevel@tonic-gate	const uint8_t *tstart, *tend;
1767c478bdstevel@tonic-gate
1777c478bdstevel@tonic-gate	if (poep == NULL || !IS_P2ALIGNED(poep, sizeof (uint16_t)) ||
1787c478bdstevel@tonic-gate	    tptr == NULL || length < sizeof (*poep))
1797c478bdstevel@tonic-gate		return (B_FALSE);
1807c478bdstevel@tonic-gate
1817c478bdstevel@tonic-gate	plen = poe_length(poep);
1827c478bdstevel@tonic-gate	if (plen + sizeof (*poep) > length)
1837c478bdstevel@tonic-gate		return (B_FALSE);
1847c478bdstevel@tonic-gate
1857c478bdstevel@tonic-gate	tstart = (const uint8_t *)(poep+1);
1867c478bdstevel@tonic-gate	tend = tstart + plen;
1877c478bdstevel@tonic-gate
1887c478bdstevel@tonic-gate	/*
1897c478bdstevel@tonic-gate	 * Note careful dereference of tptr; it might be near the end
1907c478bdstevel@tonic-gate	 * already, so we have to range check it before dereferencing
1917c478bdstevel@tonic-gate	 * to get the actual tag length.  Yes, it looks like we have
1927c478bdstevel@tonic-gate	 * duplicate array end checks.  No, they're not duplicates.
1937c478bdstevel@tonic-gate	 */
1947c478bdstevel@tonic-gate	if (tptr < tstart || tptr+POET_HDRLEN > tend ||
1957c478bdstevel@tonic-gate	    tptr+POET_HDRLEN+POET_GET_LENG(tptr) > tend)
1967c478bdstevel@tonic-gate		return (B_FALSE);
1977c478bdstevel@tonic-gate	return (B_TRUE);
1987c478bdstevel@tonic-gate}
1997c478bdstevel@tonic-gate
2007c478bdstevel@tonic-gatestatic int
2017c478bdstevel@tonic-gatepoe_tag_insert(poep_t *poep, uint16_t ttype, const void *data, size_t dlen)
2027c478bdstevel@tonic-gate{
2037c478bdstevel@tonic-gate	int plen;
2047c478bdstevel@tonic-gate	uint8_t *dp;
2057c478bdstevel@tonic-gate
2067c478bdstevel@tonic-gate	plen = poe_length(poep);
2077c478bdstevel@tonic-gate	if (data == NULL)
2087c478bdstevel@tonic-gate		dlen = 0;
2097c478bdstevel@tonic-gate	if (sizeof (*poep) + plen + POET_HDRLEN + dlen > PPPOE_MSGMAX)
2107c478bdstevel@tonic-gate		return (-1);
2117c478bdstevel@tonic-gate	dp = (uint8_t *)(poep + 1) + plen;
2127c478bdstevel@tonic-gate	POET_SET_TYPE(dp, ttype);
2137c478bdstevel@tonic-gate	POET_SET_LENG(dp, dlen);
2147c478bdstevel@tonic-gate	if (dlen > 0)
2157c478bdstevel@tonic-gate		(void) memcpy(POET_DATA(dp), data, dlen);
2167c478bdstevel@tonic-gate	poep->poep_length = htons(plen + POET_HDRLEN + dlen);
2177c478bdstevel@tonic-gate	return (0);
2187c478bdstevel@tonic-gate}
2197c478bdstevel@tonic-gate
2207c478bdstevel@tonic-gate/*
2217c478bdstevel@tonic-gate * Add a tag with text string data to a PPPoE packet being
2227c478bdstevel@tonic-gate * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2237c478bdstevel@tonic-gate */
2247c478bdstevel@tonic-gateint
2257c478bdstevel@tonic-gatepoe_add_str(poep_t *poep, uint16_t ttype, const char *str)
2267c478bdstevel@tonic-gate{
2277c478bdstevel@tonic-gate	return (poe_tag_insert(poep, ttype, str, strlen(str)));
2287c478bdstevel@tonic-gate}
2297c478bdstevel@tonic-gate
2307c478bdstevel@tonic-gate/*
2317c478bdstevel@tonic-gate * Add a tag with 32-bit integer data to a PPPoE packet being
2327c478bdstevel@tonic-gate * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2337c478bdstevel@tonic-gate */
2347c478bdstevel@tonic-gateint
2357c478bdstevel@tonic-gatepoe_add_long(poep_t *poep, uint16_t ttype, uint32_t val)
2367c478bdstevel@tonic-gate{
2377c478bdstevel@tonic-gate	val = htonl(val);
2387c478bdstevel@tonic-gate	return (poe_tag_insert(poep, ttype, &val, sizeof (val)));
2397c478bdstevel@tonic-gate}
2407c478bdstevel@tonic-gate
2417c478bdstevel@tonic-gate/*
2427c478bdstevel@tonic-gate * Add a tag with two 32-bit integers to a PPPoE packet being
2437c478bdstevel@tonic-gate * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2447c478bdstevel@tonic-gate */
2457c478bdstevel@tonic-gateint
2467c478bdstevel@tonic-gatepoe_two_longs(poep_t *poep, uint16_t ttype, uint32_t val1, uint32_t val2)
2477c478bdstevel@tonic-gate{
2487c478bdstevel@tonic-gate	uint32_t vals[2];
2497c478bdstevel@tonic-gate
2507c478bdstevel@tonic-gate	vals[0] = htonl(val1);
2517c478bdstevel@tonic-gate	vals[1] = htonl(val2);
2527c478bdstevel@tonic-gate	return (poe_tag_insert(poep, ttype, vals, sizeof (vals)));
2537c478bdstevel@tonic-gate}
2547c478bdstevel@tonic-gate
2557c478bdstevel@tonic-gate/*
2567c478bdstevel@tonic-gate * Copy a single tag and its data from one PPPoE packet to a PPPoE
2577c478bdstevel@tonic-gate * packet being constructed.  Returns -1 if it doesn't fit, or 0 for
2587c478bdstevel@tonic-gate * success.
2597c478bdstevel@tonic-gate */
2607c478bdstevel@tonic-gateint
2617c478bdstevel@tonic-gatepoe_tag_copy(poep_t *poep, const uint8_t *tagp)
2627c478bdstevel@tonic-gate{
2637c478bdstevel@tonic-gate	int tlen;
2647c478bdstevel@tonic-gate	int plen;
2657c478bdstevel@tonic-gate
2667c478bdstevel@tonic-gate	tlen = POET_GET_LENG(tagp) + POET_HDRLEN;
2677c478bdstevel@tonic-gate	plen = poe_length(poep);
2687c478bdstevel@tonic-gate	if (sizeof (*poep) + plen + tlen > PPPOE_MSGMAX)
2697c478bdstevel@tonic-gate		return (-1);
2707c478bdstevel@tonic-gate	(void) memcpy((uint8_t *)(poep + 1) + plen, tagp, tlen);
2717c478bdstevel@tonic-gate	poep->poep_length = htons(tlen + plen);
2727c478bdstevel@tonic-gate	return (0);
2737c478bdstevel@tonic-gate}
2747c478bdstevel@tonic-gate
2757c478bdstevel@tonic-gatestruct tag_list {
2767c478bdstevel@tonic-gate	int tl_type;
2777c478bdstevel@tonic-gate	const char *tl_name;
2787c478bdstevel@tonic-gate};
2797c478bdstevel@tonic-gate
2807c478bdstevel@tonic-gate/* List of PPPoE data tag types. */
2817c478bdstevel@tonic-gatestatic const struct tag_list tag_list[] = {
2827c478bdstevel@tonic-gate	{ POETT_END, "End-Of-List" },
2837c478bdstevel@tonic-gate	{ POETT_SERVICE, "Service-Name" },
2847c478bdstevel@tonic-gate	{ POETT_ACCESS, "AC-Name" },
2857c478bdstevel@tonic-gate	{ POETT_UNIQ, "Host-Uniq" },
2867c478bdstevel@tonic-gate	{ POETT_COOKIE, "AC-Cookie" },
2877c478bdstevel@tonic-gate	{ POETT_VENDOR, "Vendor-Specific" },
2887c478bdstevel@tonic-gate	{ POETT_RELAY, "Relay-Session-Id" },
2897c478bdstevel@tonic-gate	{ POETT_NAMERR, "Service-Name-Error" },
2907c478bdstevel@tonic-gate	{ POETT_SYSERR, "AC-System-Error" },
2917c478bdstevel@tonic-gate	{ POETT_GENERR, "Generic-Error" },
2927c478bdstevel@tonic-gate	{ POETT_MULTI, "Multicast-Capable" },
2937c478bdstevel@tonic-gate	{ POETT_HURL, "Host-URL" },
2947c478bdstevel@tonic-gate	{ POETT_MOTM, "Message-Of-The-Minute" },
2957c478bdstevel@tonic-gate	{ POETT_RTEADD, "IP-Route-Add" },
2967c478bdstevel@tonic-gate	{ 0, NULL }
2977c478bdstevel@tonic-gate};
2987c478bdstevel@tonic-gate
2997c478bdstevel@tonic-gate/* List of PPPoE message code numbers. */
3007c478bdstevel@tonic-gatestatic const struct tag_list code_list[] = {
3017c478bdstevel@tonic-gate	{ POECODE_DATA, "Data" },
3027c478bdstevel@tonic-gate	{ POECODE_PADO, "Active Discovery Offer" },
3037c478bdstevel@tonic-gate	{ POECODE_PADI, "Active Discovery Initiation" },
3047c478bdstevel@tonic-gate	{ POECODE_PADR, "Active Discovery Request" },
3057c478bdstevel@tonic-gate	{ POECODE_PADS, "Active Discovery Session-confirmation" },
3067c478bdstevel@tonic-gate	{ POECODE_PADT, "Active Discovery Terminate" },
3077c478bdstevel@tonic-gate	{ POECODE_PADM, "Active Discovery Message" },
3087c478bdstevel@tonic-gate	{ POECODE_PADN, "Active Discovery Network" },
3097c478bdstevel@tonic-gate	{ 0, NULL }
3107c478bdstevel@tonic-gate};
3117c478bdstevel@tonic-gate
3127c478bdstevel@tonic-gate/*
3137c478bdstevel@tonic-gate * Given a tag type number, return a pointer to a string describing
3147c478bdstevel@tonic-gate * the tag.
3157c478bdstevel@tonic-gate */
3167c478bdstevel@tonic-gateconst char *
3177c478bdstevel@tonic-gatepoe_tagname(uint16_t tagtype)
3187c478bdstevel@tonic-gate{
3197c478bdstevel@tonic-gate	const struct tag_list *tlp;
3207c478bdstevel@tonic-gate	static char tname[32];
3217c478bdstevel@tonic-gate
3227c478bdstevel@tonic-gate	for (tlp = tag_list; tlp->tl_name != NULL; tlp++)
3237c478bdstevel@tonic-gate		if (tagtype == tlp->tl_type)
3247c478bdstevel@tonic-gate			return (tlp->tl_name);
3257c478bdstevel@tonic-gate	(void) sprintf(tname, "Tag%d", tagtype);
3267c478bdstevel@tonic-gate	return (tname);
3277c478bdstevel@tonic-gate}
3287c478bdstevel@tonic-gate
3297c478bdstevel@tonic-gate/*
3307c478bdstevel@tonic-gate * Given a PPPoE message code number, return a pointer to a string
3317c478bdstevel@tonic-gate * describing the message.
3327c478bdstevel@tonic-gate */
3337c478bdstevel@tonic-gateconst char *
3347c478bdstevel@tonic-gatepoe_codename(uint8_t codetype)
3357c478bdstevel@tonic-gate{
3367c478bdstevel@tonic-gate	const struct tag_list *tlp;
3377c478bdstevel@tonic-gate	static char tname[32];
3387c478bdstevel@tonic-gate
3397c478bdstevel@tonic-gate	for (tlp = code_list; tlp->tl_name != NULL; tlp++)
3407c478bdstevel@tonic-gate		if (codetype == tlp->tl_type)
3417c478bdstevel@tonic-gate			return (tlp->tl_name);
3427c478bdstevel@tonic-gate	(void) sprintf(tname, "Code%d", codetype);
3437c478bdstevel@tonic-gate	return (tname);
3447c478bdstevel@tonic-gate}
3457c478bdstevel@tonic-gate
3467c478bdstevel@tonic-gate/*
3477c478bdstevel@tonic-gate * Given a tunnel driver address structure, return a pointer to a
3487c478bdstevel@tonic-gate * string naming that Ethernet host.
3497c478bdstevel@tonic-gate */
3507c478bdstevel@tonic-gateconst char *
3517c478bdstevel@tonic-gateehost2(const struct ether_addr *ea)
3527c478bdstevel@tonic-gate{
3537c478bdstevel@tonic-gate	static char hbuf[MAXHOSTNAMELEN+1];
3547c478bdstevel@tonic-gate
3557c478bdstevel@tonic-gate	if (ea == NULL)
3567c478bdstevel@tonic-gate		return ("NULL");
3577c478bdstevel@tonic-gate	if (ether_ntohost(hbuf, ea) == 0)
3587c478bdstevel@tonic-gate		return (hbuf);
3597c478bdstevel@tonic-gate	return (ether_ntoa(ea));
3607c478bdstevel@tonic-gate}
3617c478bdstevel@tonic-gate
3627c478bdstevel@tonic-gateconst char *
3637c478bdstevel@tonic-gateehost(const ppptun_atype *pap)
3647c478bdstevel@tonic-gate{
3657c478bdstevel@tonic-gate	return (ehost2((const struct ether_addr *)pap));
3667c478bdstevel@tonic-gate}
3677c478bdstevel@tonic-gate
3687c478bdstevel@tonic-gate/*
3697c478bdstevel@tonic-gate * Given an Internet address (in network byte order), return a pointer
3707c478bdstevel@tonic-gate * to a string naming the host.
3717c478bdstevel@tonic-gate */
3727c478bdstevel@tonic-gateconst char *
3737c478bdstevel@tonic-gateihost(uint32_t haddr)
3747c478bdstevel@tonic-gate{
3757c478bdstevel@tonic-gate	struct hostent *hp;
3767c478bdstevel@tonic-gate	struct sockaddr_in sin;
3777c478bdstevel@tonic-gate
3787c478bdstevel@tonic-gate	(void) memset(&sin, '\0', sizeof (sin));
3797c478bdstevel@tonic-gate	sin.sin_addr.s_addr = haddr;
3807c478bdstevel@tonic-gate	hp = gethostbyaddr((const char *)&sin, sizeof (sin), AF_INET);
3817c478bdstevel@tonic-gate	if (hp != NULL)
3827c478bdstevel@tonic-gate		return (hp->h_name);
3837c478bdstevel@tonic-gate	return (inet_ntoa(sin.sin_addr));
3847c478bdstevel@tonic-gate}
3857c478bdstevel@tonic-gate
3867c478bdstevel@tonic-gateint
3877c478bdstevel@tonic-gatehexdecode(char chr)
3887c478bdstevel@tonic-gate{
3897c478bdstevel@tonic-gate	if (chr >= '0' && chr <= '9')
3907c478bdstevel@tonic-gate		return ((int)(chr - '0'));
39132529ecrshoaib	if (chr >= 'a' && chr <= 'f')
3927c478bdstevel@tonic-gate		return ((int)(chr - 'a' + 10));
3937c478bdstevel@tonic-gate	return ((int)(chr - 'A' + 10));
3947c478bdstevel@tonic-gate}
395