xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/pppoe/common.c (revision 937:e808c6c8cf8e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * PPPoE common utilities and data.
240Sstevel@tonic-gate  *
25*937Srshoaib  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
260Sstevel@tonic-gate  * Use is subject to license terms.
270Sstevel@tonic-gate  */
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
300Sstevel@tonic-gate 
310Sstevel@tonic-gate #include <stdio.h>
320Sstevel@tonic-gate #include <unistd.h>
330Sstevel@tonic-gate #include <string.h>
340Sstevel@tonic-gate #include <errno.h>
350Sstevel@tonic-gate #include <netdb.h>
360Sstevel@tonic-gate #include <assert.h>
370Sstevel@tonic-gate #include <stropts.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <inet/common.h>
400Sstevel@tonic-gate #include <netinet/in.h>
410Sstevel@tonic-gate #include <net/sppptun.h>
420Sstevel@tonic-gate #include <net/pppoe.h>
430Sstevel@tonic-gate #include <arpa/inet.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate #include "common.h"
460Sstevel@tonic-gate 
470Sstevel@tonic-gate /* Not all functions are used by all applications.  Let lint know this. */
480Sstevel@tonic-gate /*LINTLIBRARY*/
490Sstevel@tonic-gate 
500Sstevel@tonic-gate /* Common I/O buffers */
510Sstevel@tonic-gate uint32_t pkt_input[PKT_INPUT_LEN / sizeof (uint32_t)];
520Sstevel@tonic-gate uint32_t pkt_octl[PKT_OCTL_LEN / sizeof (uint32_t)];
530Sstevel@tonic-gate uint32_t pkt_output[PKT_OUTPUT_LEN / sizeof (uint32_t)];
540Sstevel@tonic-gate 
550Sstevel@tonic-gate const char tunnam[] = "/dev/" PPP_TUN_NAME;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate const ether_addr_t ether_bcast = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
580Sstevel@tonic-gate 
590Sstevel@tonic-gate /*
600Sstevel@tonic-gate  * Wrapper for standard strerror() function -- the standard allows
610Sstevel@tonic-gate  * that routine to return NULL, and that's inconvenient to handle.
620Sstevel@tonic-gate  * This function never returns NULL.
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate const char *
mystrerror(int err)650Sstevel@tonic-gate mystrerror(int err)
660Sstevel@tonic-gate {
670Sstevel@tonic-gate 	const char *estr;
680Sstevel@tonic-gate 	static char ebuf[64];
690Sstevel@tonic-gate 
700Sstevel@tonic-gate 	if ((estr = strerror(err)) != NULL)
710Sstevel@tonic-gate 		return (estr);
720Sstevel@tonic-gate 	(void) snprintf(ebuf, sizeof (ebuf), "Error:%d", err);
730Sstevel@tonic-gate 	return (ebuf);
740Sstevel@tonic-gate }
750Sstevel@tonic-gate 
760Sstevel@tonic-gate /*
770Sstevel@tonic-gate  * Wrapper for standard perror() function -- the standard definition
780Sstevel@tonic-gate  * of perror doesn't include the program name in the output and is
790Sstevel@tonic-gate  * thus inconvenient to use.
800Sstevel@tonic-gate  */
810Sstevel@tonic-gate void
myperror(const char * emsg)820Sstevel@tonic-gate myperror(const char *emsg)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", myname, emsg,
850Sstevel@tonic-gate 	    mystrerror(errno));
860Sstevel@tonic-gate }
870Sstevel@tonic-gate 
880Sstevel@tonic-gate /*
890Sstevel@tonic-gate  * Wrapper for standard getmsg() function.  Completely discards any
900Sstevel@tonic-gate  * fragmented messages because we don't expect ever to see these from
910Sstevel@tonic-gate  * a properly functioning tunnel driver.  Returns flags
920Sstevel@tonic-gate  * (MORECTL|MOREDATA) as seen by interface.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate int
mygetmsg(int fd,struct strbuf * ctrl,struct strbuf * data,int * flags)950Sstevel@tonic-gate mygetmsg(int fd, struct strbuf *ctrl, struct strbuf *data, int *flags)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	int retv;
980Sstevel@tonic-gate 	int hadflags;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	hadflags = getmsg(fd, ctrl, data, flags);
1010Sstevel@tonic-gate 	if (hadflags <= 0 || !(hadflags & (MORECTL | MOREDATA)))
1020Sstevel@tonic-gate 		return (hadflags);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	do {
1050Sstevel@tonic-gate 		if (flags != NULL)
1060Sstevel@tonic-gate 			*flags = 0;
1070Sstevel@tonic-gate 		retv = getmsg(fd, ctrl, data, flags);
1080Sstevel@tonic-gate 	} while (retv > 0 || (retv < 0 && errno == EINTR));
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate 	/*
1110Sstevel@tonic-gate 	 * What remains at this point is the tail end of the
1120Sstevel@tonic-gate 	 * truncated message.  Toss it.
1130Sstevel@tonic-gate 	 */
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (retv < 0 ? retv : hadflags);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate /*
1190Sstevel@tonic-gate  * Common wrapper function for STREAMS I_STR ioctl.  Returns -1 on
1200Sstevel@tonic-gate  * failure, 0 for success.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate int
strioctl(int fd,int cmd,void * ptr,int ilen,int olen)1230Sstevel@tonic-gate strioctl(int fd, int cmd, void *ptr, int ilen, int olen)
1240Sstevel@tonic-gate {
1250Sstevel@tonic-gate 	struct strioctl	str;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	str.ic_cmd = cmd;
1280Sstevel@tonic-gate 	str.ic_timout = 0;	/* Default timeout; 15 seconds */
1290Sstevel@tonic-gate 	str.ic_len = ilen;
1300Sstevel@tonic-gate 	str.ic_dp = ptr;
1310Sstevel@tonic-gate 
1320Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &str) == -1) {
1330Sstevel@tonic-gate 		return (-1);
1340Sstevel@tonic-gate 	}
1350Sstevel@tonic-gate 	if (str.ic_len != olen) {
1360Sstevel@tonic-gate 		errno = EINVAL;
1370Sstevel@tonic-gate 		return (-1);
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	return (0);
1400Sstevel@tonic-gate }
1410Sstevel@tonic-gate 
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate  * Format a PPPoE header in the user's buffer.  The returned pointer
1440Sstevel@tonic-gate  * is either identical to the first argument, or is NULL if it's not
1450Sstevel@tonic-gate  * usable.  On entry, dptr should point to the first byte after the
1460Sstevel@tonic-gate  * Ethertype field, codeval should be one of the POECODE_* values, and
1470Sstevel@tonic-gate  * sessionid should be the assigned session ID number or one of the
1480Sstevel@tonic-gate  * special POESESS_* values.
1490Sstevel@tonic-gate  */
1500Sstevel@tonic-gate poep_t *
poe_mkheader(void * dptr,uint8_t codeval,int sessionid)1510Sstevel@tonic-gate poe_mkheader(void *dptr, uint8_t codeval, int sessionid)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate 	poep_t *poep;
1540Sstevel@tonic-gate 
1550Sstevel@tonic-gate 	/* Discard obvious junk. */
1560Sstevel@tonic-gate 	assert(dptr != NULL && IS_P2ALIGNED(dptr, sizeof (poep_t *)));
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	/* Initialize the header */
1590Sstevel@tonic-gate 	poep = (poep_t *)dptr;
1600Sstevel@tonic-gate 	poep->poep_version_type = POE_VERSION;
1610Sstevel@tonic-gate 	poep->poep_code = codeval;
1620Sstevel@tonic-gate 	poep->poep_session_id = htons(sessionid);
1630Sstevel@tonic-gate 	poep->poep_length = htons(0);
1640Sstevel@tonic-gate 	return (poep);
1650Sstevel@tonic-gate }
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Validate that a given tag is intact.  This is intended to be used
1690Sstevel@tonic-gate  * in tag-parsing loops before attempting to access the tag data.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate boolean_t
poe_tagcheck(const poep_t * poep,int length,const uint8_t * tptr)1720Sstevel@tonic-gate poe_tagcheck(const poep_t *poep, int length, const uint8_t *tptr)
1730Sstevel@tonic-gate {
1740Sstevel@tonic-gate 	int plen;
1750Sstevel@tonic-gate 	const uint8_t *tstart, *tend;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	if (poep == NULL || !IS_P2ALIGNED(poep, sizeof (uint16_t)) ||
1780Sstevel@tonic-gate 	    tptr == NULL || length < sizeof (*poep))
1790Sstevel@tonic-gate 		return (B_FALSE);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	plen = poe_length(poep);
1820Sstevel@tonic-gate 	if (plen + sizeof (*poep) > length)
1830Sstevel@tonic-gate 		return (B_FALSE);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	tstart = (const uint8_t *)(poep+1);
1860Sstevel@tonic-gate 	tend = tstart + plen;
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	/*
1890Sstevel@tonic-gate 	 * Note careful dereference of tptr; it might be near the end
1900Sstevel@tonic-gate 	 * already, so we have to range check it before dereferencing
1910Sstevel@tonic-gate 	 * to get the actual tag length.  Yes, it looks like we have
1920Sstevel@tonic-gate 	 * duplicate array end checks.  No, they're not duplicates.
1930Sstevel@tonic-gate 	 */
1940Sstevel@tonic-gate 	if (tptr < tstart || tptr+POET_HDRLEN > tend ||
1950Sstevel@tonic-gate 	    tptr+POET_HDRLEN+POET_GET_LENG(tptr) > tend)
1960Sstevel@tonic-gate 		return (B_FALSE);
1970Sstevel@tonic-gate 	return (B_TRUE);
1980Sstevel@tonic-gate }
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate static int
poe_tag_insert(poep_t * poep,uint16_t ttype,const void * data,size_t dlen)2010Sstevel@tonic-gate poe_tag_insert(poep_t *poep, uint16_t ttype, const void *data, size_t dlen)
2020Sstevel@tonic-gate {
2030Sstevel@tonic-gate 	int plen;
2040Sstevel@tonic-gate 	uint8_t *dp;
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 	plen = poe_length(poep);
2070Sstevel@tonic-gate 	if (data == NULL)
2080Sstevel@tonic-gate 		dlen = 0;
2090Sstevel@tonic-gate 	if (sizeof (*poep) + plen + POET_HDRLEN + dlen > PPPOE_MSGMAX)
2100Sstevel@tonic-gate 		return (-1);
2110Sstevel@tonic-gate 	dp = (uint8_t *)(poep + 1) + plen;
2120Sstevel@tonic-gate 	POET_SET_TYPE(dp, ttype);
2130Sstevel@tonic-gate 	POET_SET_LENG(dp, dlen);
2140Sstevel@tonic-gate 	if (dlen > 0)
2150Sstevel@tonic-gate 		(void) memcpy(POET_DATA(dp), data, dlen);
2160Sstevel@tonic-gate 	poep->poep_length = htons(plen + POET_HDRLEN + dlen);
2170Sstevel@tonic-gate 	return (0);
2180Sstevel@tonic-gate }
2190Sstevel@tonic-gate 
2200Sstevel@tonic-gate /*
2210Sstevel@tonic-gate  * Add a tag with text string data to a PPPoE packet being
2220Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2230Sstevel@tonic-gate  */
2240Sstevel@tonic-gate int
poe_add_str(poep_t * poep,uint16_t ttype,const char * str)2250Sstevel@tonic-gate poe_add_str(poep_t *poep, uint16_t ttype, const char *str)
2260Sstevel@tonic-gate {
2270Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, str, strlen(str)));
2280Sstevel@tonic-gate }
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate /*
2310Sstevel@tonic-gate  * Add a tag with 32-bit integer data to a PPPoE packet being
2320Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2330Sstevel@tonic-gate  */
2340Sstevel@tonic-gate int
poe_add_long(poep_t * poep,uint16_t ttype,uint32_t val)2350Sstevel@tonic-gate poe_add_long(poep_t *poep, uint16_t ttype, uint32_t val)
2360Sstevel@tonic-gate {
2370Sstevel@tonic-gate 	val = htonl(val);
2380Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, &val, sizeof (val)));
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate 
2410Sstevel@tonic-gate /*
2420Sstevel@tonic-gate  * Add a tag with two 32-bit integers to a PPPoE packet being
2430Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
2440Sstevel@tonic-gate  */
2450Sstevel@tonic-gate int
poe_two_longs(poep_t * poep,uint16_t ttype,uint32_t val1,uint32_t val2)2460Sstevel@tonic-gate poe_two_longs(poep_t *poep, uint16_t ttype, uint32_t val1, uint32_t val2)
2470Sstevel@tonic-gate {
2480Sstevel@tonic-gate 	uint32_t vals[2];
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	vals[0] = htonl(val1);
2510Sstevel@tonic-gate 	vals[1] = htonl(val2);
2520Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, vals, sizeof (vals)));
2530Sstevel@tonic-gate }
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate /*
2560Sstevel@tonic-gate  * Copy a single tag and its data from one PPPoE packet to a PPPoE
2570Sstevel@tonic-gate  * packet being constructed.  Returns -1 if it doesn't fit, or 0 for
2580Sstevel@tonic-gate  * success.
2590Sstevel@tonic-gate  */
2600Sstevel@tonic-gate int
poe_tag_copy(poep_t * poep,const uint8_t * tagp)2610Sstevel@tonic-gate poe_tag_copy(poep_t *poep, const uint8_t *tagp)
2620Sstevel@tonic-gate {
2630Sstevel@tonic-gate 	int tlen;
2640Sstevel@tonic-gate 	int plen;
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 	tlen = POET_GET_LENG(tagp) + POET_HDRLEN;
2670Sstevel@tonic-gate 	plen = poe_length(poep);
2680Sstevel@tonic-gate 	if (sizeof (*poep) + plen + tlen > PPPOE_MSGMAX)
2690Sstevel@tonic-gate 		return (-1);
2700Sstevel@tonic-gate 	(void) memcpy((uint8_t *)(poep + 1) + plen, tagp, tlen);
2710Sstevel@tonic-gate 	poep->poep_length = htons(tlen + plen);
2720Sstevel@tonic-gate 	return (0);
2730Sstevel@tonic-gate }
2740Sstevel@tonic-gate 
2750Sstevel@tonic-gate struct tag_list {
2760Sstevel@tonic-gate 	int tl_type;
2770Sstevel@tonic-gate 	const char *tl_name;
2780Sstevel@tonic-gate };
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate /* List of PPPoE data tag types. */
2810Sstevel@tonic-gate static const struct tag_list tag_list[] = {
2820Sstevel@tonic-gate 	{ POETT_END, "End-Of-List" },
2830Sstevel@tonic-gate 	{ POETT_SERVICE, "Service-Name" },
2840Sstevel@tonic-gate 	{ POETT_ACCESS, "AC-Name" },
2850Sstevel@tonic-gate 	{ POETT_UNIQ, "Host-Uniq" },
2860Sstevel@tonic-gate 	{ POETT_COOKIE, "AC-Cookie" },
2870Sstevel@tonic-gate 	{ POETT_VENDOR, "Vendor-Specific" },
2880Sstevel@tonic-gate 	{ POETT_RELAY, "Relay-Session-Id" },
2890Sstevel@tonic-gate 	{ POETT_NAMERR, "Service-Name-Error" },
2900Sstevel@tonic-gate 	{ POETT_SYSERR, "AC-System-Error" },
2910Sstevel@tonic-gate 	{ POETT_GENERR, "Generic-Error" },
2920Sstevel@tonic-gate 	{ POETT_MULTI, "Multicast-Capable" },
2930Sstevel@tonic-gate 	{ POETT_HURL, "Host-URL" },
2940Sstevel@tonic-gate 	{ POETT_MOTM, "Message-Of-The-Minute" },
2950Sstevel@tonic-gate 	{ POETT_RTEADD, "IP-Route-Add" },
2960Sstevel@tonic-gate 	{ 0, NULL }
2970Sstevel@tonic-gate };
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate /* List of PPPoE message code numbers. */
3000Sstevel@tonic-gate static const struct tag_list code_list[] = {
3010Sstevel@tonic-gate 	{ POECODE_DATA, "Data" },
3020Sstevel@tonic-gate 	{ POECODE_PADO, "Active Discovery Offer" },
3030Sstevel@tonic-gate 	{ POECODE_PADI, "Active Discovery Initiation" },
3040Sstevel@tonic-gate 	{ POECODE_PADR, "Active Discovery Request" },
3050Sstevel@tonic-gate 	{ POECODE_PADS, "Active Discovery Session-confirmation" },
3060Sstevel@tonic-gate 	{ POECODE_PADT, "Active Discovery Terminate" },
3070Sstevel@tonic-gate 	{ POECODE_PADM, "Active Discovery Message" },
3080Sstevel@tonic-gate 	{ POECODE_PADN, "Active Discovery Network" },
3090Sstevel@tonic-gate 	{ 0, NULL }
3100Sstevel@tonic-gate };
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate /*
3130Sstevel@tonic-gate  * Given a tag type number, return a pointer to a string describing
3140Sstevel@tonic-gate  * the tag.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate const char *
poe_tagname(uint16_t tagtype)3170Sstevel@tonic-gate poe_tagname(uint16_t tagtype)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	const struct tag_list *tlp;
3200Sstevel@tonic-gate 	static char tname[32];
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	for (tlp = tag_list; tlp->tl_name != NULL; tlp++)
3230Sstevel@tonic-gate 		if (tagtype == tlp->tl_type)
3240Sstevel@tonic-gate 			return (tlp->tl_name);
3250Sstevel@tonic-gate 	(void) sprintf(tname, "Tag%d", tagtype);
3260Sstevel@tonic-gate 	return (tname);
3270Sstevel@tonic-gate }
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate /*
3300Sstevel@tonic-gate  * Given a PPPoE message code number, return a pointer to a string
3310Sstevel@tonic-gate  * describing the message.
3320Sstevel@tonic-gate  */
3330Sstevel@tonic-gate const char *
poe_codename(uint8_t codetype)3340Sstevel@tonic-gate poe_codename(uint8_t codetype)
3350Sstevel@tonic-gate {
3360Sstevel@tonic-gate 	const struct tag_list *tlp;
3370Sstevel@tonic-gate 	static char tname[32];
3380Sstevel@tonic-gate 
3390Sstevel@tonic-gate 	for (tlp = code_list; tlp->tl_name != NULL; tlp++)
3400Sstevel@tonic-gate 		if (codetype == tlp->tl_type)
3410Sstevel@tonic-gate 			return (tlp->tl_name);
3420Sstevel@tonic-gate 	(void) sprintf(tname, "Code%d", codetype);
3430Sstevel@tonic-gate 	return (tname);
3440Sstevel@tonic-gate }
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate /*
3470Sstevel@tonic-gate  * Given a tunnel driver address structure, return a pointer to a
3480Sstevel@tonic-gate  * string naming that Ethernet host.
3490Sstevel@tonic-gate  */
3500Sstevel@tonic-gate const char *
ehost2(const struct ether_addr * ea)3510Sstevel@tonic-gate ehost2(const struct ether_addr *ea)
3520Sstevel@tonic-gate {
3530Sstevel@tonic-gate 	static char hbuf[MAXHOSTNAMELEN+1];
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate 	if (ea == NULL)
3560Sstevel@tonic-gate 		return ("NULL");
3570Sstevel@tonic-gate 	if (ether_ntohost(hbuf, ea) == 0)
3580Sstevel@tonic-gate 		return (hbuf);
3590Sstevel@tonic-gate 	return (ether_ntoa(ea));
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate const char *
ehost(const ppptun_atype * pap)3630Sstevel@tonic-gate ehost(const ppptun_atype *pap)
3640Sstevel@tonic-gate {
3650Sstevel@tonic-gate 	return (ehost2((const struct ether_addr *)pap));
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate  * Given an Internet address (in network byte order), return a pointer
3700Sstevel@tonic-gate  * to a string naming the host.
3710Sstevel@tonic-gate  */
3720Sstevel@tonic-gate const char *
ihost(uint32_t haddr)3730Sstevel@tonic-gate ihost(uint32_t haddr)
3740Sstevel@tonic-gate {
3750Sstevel@tonic-gate 	struct hostent *hp;
3760Sstevel@tonic-gate 	struct sockaddr_in sin;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	(void) memset(&sin, '\0', sizeof (sin));
3790Sstevel@tonic-gate 	sin.sin_addr.s_addr = haddr;
3800Sstevel@tonic-gate 	hp = gethostbyaddr((const char *)&sin, sizeof (sin), AF_INET);
3810Sstevel@tonic-gate 	if (hp != NULL)
3820Sstevel@tonic-gate 		return (hp->h_name);
3830Sstevel@tonic-gate 	return (inet_ntoa(sin.sin_addr));
3840Sstevel@tonic-gate }
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate int
hexdecode(char chr)3870Sstevel@tonic-gate hexdecode(char chr)
3880Sstevel@tonic-gate {
3890Sstevel@tonic-gate 	if (chr >= '0' && chr <= '9')
3900Sstevel@tonic-gate 		return ((int)(chr - '0'));
391*937Srshoaib 	if (chr >= 'a' && chr <= 'f')
3920Sstevel@tonic-gate 		return ((int)(chr - 'a' + 10));
3930Sstevel@tonic-gate 	return ((int)(chr - 'A' + 10));
3940Sstevel@tonic-gate }
395