xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.lib/pppoe/common.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * PPPoE common utilities and data.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Copyright 2000-2002 Sun Microsystems, Inc.  All rights reserved.
26*0Sstevel@tonic-gate  * Use is subject to license terms.
27*0Sstevel@tonic-gate  */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #include <stdio.h>
32*0Sstevel@tonic-gate #include <unistd.h>
33*0Sstevel@tonic-gate #include <string.h>
34*0Sstevel@tonic-gate #include <errno.h>
35*0Sstevel@tonic-gate #include <netdb.h>
36*0Sstevel@tonic-gate #include <assert.h>
37*0Sstevel@tonic-gate #include <stropts.h>
38*0Sstevel@tonic-gate #include <sys/types.h>
39*0Sstevel@tonic-gate #include <inet/common.h>
40*0Sstevel@tonic-gate #include <netinet/in.h>
41*0Sstevel@tonic-gate #include <net/sppptun.h>
42*0Sstevel@tonic-gate #include <net/pppoe.h>
43*0Sstevel@tonic-gate #include <arpa/inet.h>
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include "common.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate /* Not all functions are used by all applications.  Let lint know this. */
48*0Sstevel@tonic-gate /*LINTLIBRARY*/
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate /* Common I/O buffers */
51*0Sstevel@tonic-gate uint32_t pkt_input[PKT_INPUT_LEN / sizeof (uint32_t)];
52*0Sstevel@tonic-gate uint32_t pkt_octl[PKT_OCTL_LEN / sizeof (uint32_t)];
53*0Sstevel@tonic-gate uint32_t pkt_output[PKT_OUTPUT_LEN / sizeof (uint32_t)];
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate const char tunnam[] = "/dev/" PPP_TUN_NAME;
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate const ether_addr_t ether_bcast = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate /*
60*0Sstevel@tonic-gate  * Wrapper for standard strerror() function -- the standard allows
61*0Sstevel@tonic-gate  * that routine to return NULL, and that's inconvenient to handle.
62*0Sstevel@tonic-gate  * This function never returns NULL.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate const char *
65*0Sstevel@tonic-gate mystrerror(int err)
66*0Sstevel@tonic-gate {
67*0Sstevel@tonic-gate 	const char *estr;
68*0Sstevel@tonic-gate 	static char ebuf[64];
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate 	if ((estr = strerror(err)) != NULL)
71*0Sstevel@tonic-gate 		return (estr);
72*0Sstevel@tonic-gate 	(void) snprintf(ebuf, sizeof (ebuf), "Error:%d", err);
73*0Sstevel@tonic-gate 	return (ebuf);
74*0Sstevel@tonic-gate }
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate /*
77*0Sstevel@tonic-gate  * Wrapper for standard perror() function -- the standard definition
78*0Sstevel@tonic-gate  * of perror doesn't include the program name in the output and is
79*0Sstevel@tonic-gate  * thus inconvenient to use.
80*0Sstevel@tonic-gate  */
81*0Sstevel@tonic-gate void
82*0Sstevel@tonic-gate myperror(const char *emsg)
83*0Sstevel@tonic-gate {
84*0Sstevel@tonic-gate 	(void) fprintf(stderr, "%s: %s: %s\n", myname, emsg,
85*0Sstevel@tonic-gate 	    mystrerror(errno));
86*0Sstevel@tonic-gate }
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate /*
89*0Sstevel@tonic-gate  * Wrapper for standard getmsg() function.  Completely discards any
90*0Sstevel@tonic-gate  * fragmented messages because we don't expect ever to see these from
91*0Sstevel@tonic-gate  * a properly functioning tunnel driver.  Returns flags
92*0Sstevel@tonic-gate  * (MORECTL|MOREDATA) as seen by interface.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate int
95*0Sstevel@tonic-gate mygetmsg(int fd, struct strbuf *ctrl, struct strbuf *data, int *flags)
96*0Sstevel@tonic-gate {
97*0Sstevel@tonic-gate 	int retv;
98*0Sstevel@tonic-gate 	int hadflags;
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	hadflags = getmsg(fd, ctrl, data, flags);
101*0Sstevel@tonic-gate 	if (hadflags <= 0 || !(hadflags & (MORECTL | MOREDATA)))
102*0Sstevel@tonic-gate 		return (hadflags);
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 	do {
105*0Sstevel@tonic-gate 		if (flags != NULL)
106*0Sstevel@tonic-gate 			*flags = 0;
107*0Sstevel@tonic-gate 		retv = getmsg(fd, ctrl, data, flags);
108*0Sstevel@tonic-gate 	} while (retv > 0 || (retv < 0 && errno == EINTR));
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate 	/*
111*0Sstevel@tonic-gate 	 * What remains at this point is the tail end of the
112*0Sstevel@tonic-gate 	 * truncated message.  Toss it.
113*0Sstevel@tonic-gate 	 */
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	return (retv < 0 ? retv : hadflags);
116*0Sstevel@tonic-gate }
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate  * Common wrapper function for STREAMS I_STR ioctl.  Returns -1 on
120*0Sstevel@tonic-gate  * failure, 0 for success.
121*0Sstevel@tonic-gate  */
122*0Sstevel@tonic-gate int
123*0Sstevel@tonic-gate strioctl(int fd, int cmd, void *ptr, int ilen, int olen)
124*0Sstevel@tonic-gate {
125*0Sstevel@tonic-gate 	struct strioctl	str;
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 	str.ic_cmd = cmd;
128*0Sstevel@tonic-gate 	str.ic_timout = 0;	/* Default timeout; 15 seconds */
129*0Sstevel@tonic-gate 	str.ic_len = ilen;
130*0Sstevel@tonic-gate 	str.ic_dp = ptr;
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &str) == -1) {
133*0Sstevel@tonic-gate 		return (-1);
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate 	if (str.ic_len != olen) {
136*0Sstevel@tonic-gate 		errno = EINVAL;
137*0Sstevel@tonic-gate 		return (-1);
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 	return (0);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate 
142*0Sstevel@tonic-gate /*
143*0Sstevel@tonic-gate  * Format a PPPoE header in the user's buffer.  The returned pointer
144*0Sstevel@tonic-gate  * is either identical to the first argument, or is NULL if it's not
145*0Sstevel@tonic-gate  * usable.  On entry, dptr should point to the first byte after the
146*0Sstevel@tonic-gate  * Ethertype field, codeval should be one of the POECODE_* values, and
147*0Sstevel@tonic-gate  * sessionid should be the assigned session ID number or one of the
148*0Sstevel@tonic-gate  * special POESESS_* values.
149*0Sstevel@tonic-gate  */
150*0Sstevel@tonic-gate poep_t *
151*0Sstevel@tonic-gate poe_mkheader(void *dptr, uint8_t codeval, int sessionid)
152*0Sstevel@tonic-gate {
153*0Sstevel@tonic-gate 	poep_t *poep;
154*0Sstevel@tonic-gate 
155*0Sstevel@tonic-gate 	/* Discard obvious junk. */
156*0Sstevel@tonic-gate 	assert(dptr != NULL && IS_P2ALIGNED(dptr, sizeof (poep_t *)));
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 	/* Initialize the header */
159*0Sstevel@tonic-gate 	poep = (poep_t *)dptr;
160*0Sstevel@tonic-gate 	poep->poep_version_type = POE_VERSION;
161*0Sstevel@tonic-gate 	poep->poep_code = codeval;
162*0Sstevel@tonic-gate 	poep->poep_session_id = htons(sessionid);
163*0Sstevel@tonic-gate 	poep->poep_length = htons(0);
164*0Sstevel@tonic-gate 	return (poep);
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate  * Validate that a given tag is intact.  This is intended to be used
169*0Sstevel@tonic-gate  * in tag-parsing loops before attempting to access the tag data.
170*0Sstevel@tonic-gate  */
171*0Sstevel@tonic-gate boolean_t
172*0Sstevel@tonic-gate poe_tagcheck(const poep_t *poep, int length, const uint8_t *tptr)
173*0Sstevel@tonic-gate {
174*0Sstevel@tonic-gate 	int plen;
175*0Sstevel@tonic-gate 	const uint8_t *tstart, *tend;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	if (poep == NULL || !IS_P2ALIGNED(poep, sizeof (uint16_t)) ||
178*0Sstevel@tonic-gate 	    tptr == NULL || length < sizeof (*poep))
179*0Sstevel@tonic-gate 		return (B_FALSE);
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	plen = poe_length(poep);
182*0Sstevel@tonic-gate 	if (plen + sizeof (*poep) > length)
183*0Sstevel@tonic-gate 		return (B_FALSE);
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate 	tstart = (const uint8_t *)(poep+1);
186*0Sstevel@tonic-gate 	tend = tstart + plen;
187*0Sstevel@tonic-gate 
188*0Sstevel@tonic-gate 	/*
189*0Sstevel@tonic-gate 	 * Note careful dereference of tptr; it might be near the end
190*0Sstevel@tonic-gate 	 * already, so we have to range check it before dereferencing
191*0Sstevel@tonic-gate 	 * to get the actual tag length.  Yes, it looks like we have
192*0Sstevel@tonic-gate 	 * duplicate array end checks.  No, they're not duplicates.
193*0Sstevel@tonic-gate 	 */
194*0Sstevel@tonic-gate 	if (tptr < tstart || tptr+POET_HDRLEN > tend ||
195*0Sstevel@tonic-gate 	    tptr+POET_HDRLEN+POET_GET_LENG(tptr) > tend)
196*0Sstevel@tonic-gate 		return (B_FALSE);
197*0Sstevel@tonic-gate 	return (B_TRUE);
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate static int
201*0Sstevel@tonic-gate poe_tag_insert(poep_t *poep, uint16_t ttype, const void *data, size_t dlen)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	int plen;
204*0Sstevel@tonic-gate 	uint8_t *dp;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	plen = poe_length(poep);
207*0Sstevel@tonic-gate 	if (data == NULL)
208*0Sstevel@tonic-gate 		dlen = 0;
209*0Sstevel@tonic-gate 	if (sizeof (*poep) + plen + POET_HDRLEN + dlen > PPPOE_MSGMAX)
210*0Sstevel@tonic-gate 		return (-1);
211*0Sstevel@tonic-gate 	dp = (uint8_t *)(poep + 1) + plen;
212*0Sstevel@tonic-gate 	POET_SET_TYPE(dp, ttype);
213*0Sstevel@tonic-gate 	POET_SET_LENG(dp, dlen);
214*0Sstevel@tonic-gate 	if (dlen > 0)
215*0Sstevel@tonic-gate 		(void) memcpy(POET_DATA(dp), data, dlen);
216*0Sstevel@tonic-gate 	poep->poep_length = htons(plen + POET_HDRLEN + dlen);
217*0Sstevel@tonic-gate 	return (0);
218*0Sstevel@tonic-gate }
219*0Sstevel@tonic-gate 
220*0Sstevel@tonic-gate /*
221*0Sstevel@tonic-gate  * Add a tag with text string data to a PPPoE packet being
222*0Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
223*0Sstevel@tonic-gate  */
224*0Sstevel@tonic-gate int
225*0Sstevel@tonic-gate poe_add_str(poep_t *poep, uint16_t ttype, const char *str)
226*0Sstevel@tonic-gate {
227*0Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, str, strlen(str)));
228*0Sstevel@tonic-gate }
229*0Sstevel@tonic-gate 
230*0Sstevel@tonic-gate /*
231*0Sstevel@tonic-gate  * Add a tag with 32-bit integer data to a PPPoE packet being
232*0Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
233*0Sstevel@tonic-gate  */
234*0Sstevel@tonic-gate int
235*0Sstevel@tonic-gate poe_add_long(poep_t *poep, uint16_t ttype, uint32_t val)
236*0Sstevel@tonic-gate {
237*0Sstevel@tonic-gate 	val = htonl(val);
238*0Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, &val, sizeof (val)));
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate /*
242*0Sstevel@tonic-gate  * Add a tag with two 32-bit integers to a PPPoE packet being
243*0Sstevel@tonic-gate  * constructed.  Returns -1 if it doesn't fit, or 0 for success.
244*0Sstevel@tonic-gate  */
245*0Sstevel@tonic-gate int
246*0Sstevel@tonic-gate poe_two_longs(poep_t *poep, uint16_t ttype, uint32_t val1, uint32_t val2)
247*0Sstevel@tonic-gate {
248*0Sstevel@tonic-gate 	uint32_t vals[2];
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	vals[0] = htonl(val1);
251*0Sstevel@tonic-gate 	vals[1] = htonl(val2);
252*0Sstevel@tonic-gate 	return (poe_tag_insert(poep, ttype, vals, sizeof (vals)));
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate /*
256*0Sstevel@tonic-gate  * Copy a single tag and its data from one PPPoE packet to a PPPoE
257*0Sstevel@tonic-gate  * packet being constructed.  Returns -1 if it doesn't fit, or 0 for
258*0Sstevel@tonic-gate  * success.
259*0Sstevel@tonic-gate  */
260*0Sstevel@tonic-gate int
261*0Sstevel@tonic-gate poe_tag_copy(poep_t *poep, const uint8_t *tagp)
262*0Sstevel@tonic-gate {
263*0Sstevel@tonic-gate 	int tlen;
264*0Sstevel@tonic-gate 	int plen;
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	tlen = POET_GET_LENG(tagp) + POET_HDRLEN;
267*0Sstevel@tonic-gate 	plen = poe_length(poep);
268*0Sstevel@tonic-gate 	if (sizeof (*poep) + plen + tlen > PPPOE_MSGMAX)
269*0Sstevel@tonic-gate 		return (-1);
270*0Sstevel@tonic-gate 	(void) memcpy((uint8_t *)(poep + 1) + plen, tagp, tlen);
271*0Sstevel@tonic-gate 	poep->poep_length = htons(tlen + plen);
272*0Sstevel@tonic-gate 	return (0);
273*0Sstevel@tonic-gate }
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate struct tag_list {
276*0Sstevel@tonic-gate 	int tl_type;
277*0Sstevel@tonic-gate 	const char *tl_name;
278*0Sstevel@tonic-gate };
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate /* List of PPPoE data tag types. */
281*0Sstevel@tonic-gate static const struct tag_list tag_list[] = {
282*0Sstevel@tonic-gate 	{ POETT_END, "End-Of-List" },
283*0Sstevel@tonic-gate 	{ POETT_SERVICE, "Service-Name" },
284*0Sstevel@tonic-gate 	{ POETT_ACCESS, "AC-Name" },
285*0Sstevel@tonic-gate 	{ POETT_UNIQ, "Host-Uniq" },
286*0Sstevel@tonic-gate 	{ POETT_COOKIE, "AC-Cookie" },
287*0Sstevel@tonic-gate 	{ POETT_VENDOR, "Vendor-Specific" },
288*0Sstevel@tonic-gate 	{ POETT_RELAY, "Relay-Session-Id" },
289*0Sstevel@tonic-gate 	{ POETT_NAMERR, "Service-Name-Error" },
290*0Sstevel@tonic-gate 	{ POETT_SYSERR, "AC-System-Error" },
291*0Sstevel@tonic-gate 	{ POETT_GENERR, "Generic-Error" },
292*0Sstevel@tonic-gate 	{ POETT_MULTI, "Multicast-Capable" },
293*0Sstevel@tonic-gate 	{ POETT_HURL, "Host-URL" },
294*0Sstevel@tonic-gate 	{ POETT_MOTM, "Message-Of-The-Minute" },
295*0Sstevel@tonic-gate 	{ POETT_RTEADD, "IP-Route-Add" },
296*0Sstevel@tonic-gate 	{ 0, NULL }
297*0Sstevel@tonic-gate };
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate /* List of PPPoE message code numbers. */
300*0Sstevel@tonic-gate static const struct tag_list code_list[] = {
301*0Sstevel@tonic-gate 	{ POECODE_DATA, "Data" },
302*0Sstevel@tonic-gate 	{ POECODE_PADO, "Active Discovery Offer" },
303*0Sstevel@tonic-gate 	{ POECODE_PADI, "Active Discovery Initiation" },
304*0Sstevel@tonic-gate 	{ POECODE_PADR, "Active Discovery Request" },
305*0Sstevel@tonic-gate 	{ POECODE_PADS, "Active Discovery Session-confirmation" },
306*0Sstevel@tonic-gate 	{ POECODE_PADT, "Active Discovery Terminate" },
307*0Sstevel@tonic-gate 	{ POECODE_PADM, "Active Discovery Message" },
308*0Sstevel@tonic-gate 	{ POECODE_PADN, "Active Discovery Network" },
309*0Sstevel@tonic-gate 	{ 0, NULL }
310*0Sstevel@tonic-gate };
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate /*
313*0Sstevel@tonic-gate  * Given a tag type number, return a pointer to a string describing
314*0Sstevel@tonic-gate  * the tag.
315*0Sstevel@tonic-gate  */
316*0Sstevel@tonic-gate const char *
317*0Sstevel@tonic-gate poe_tagname(uint16_t tagtype)
318*0Sstevel@tonic-gate {
319*0Sstevel@tonic-gate 	const struct tag_list *tlp;
320*0Sstevel@tonic-gate 	static char tname[32];
321*0Sstevel@tonic-gate 
322*0Sstevel@tonic-gate 	for (tlp = tag_list; tlp->tl_name != NULL; tlp++)
323*0Sstevel@tonic-gate 		if (tagtype == tlp->tl_type)
324*0Sstevel@tonic-gate 			return (tlp->tl_name);
325*0Sstevel@tonic-gate 	(void) sprintf(tname, "Tag%d", tagtype);
326*0Sstevel@tonic-gate 	return (tname);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate /*
330*0Sstevel@tonic-gate  * Given a PPPoE message code number, return a pointer to a string
331*0Sstevel@tonic-gate  * describing the message.
332*0Sstevel@tonic-gate  */
333*0Sstevel@tonic-gate const char *
334*0Sstevel@tonic-gate poe_codename(uint8_t codetype)
335*0Sstevel@tonic-gate {
336*0Sstevel@tonic-gate 	const struct tag_list *tlp;
337*0Sstevel@tonic-gate 	static char tname[32];
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate 	for (tlp = code_list; tlp->tl_name != NULL; tlp++)
340*0Sstevel@tonic-gate 		if (codetype == tlp->tl_type)
341*0Sstevel@tonic-gate 			return (tlp->tl_name);
342*0Sstevel@tonic-gate 	(void) sprintf(tname, "Code%d", codetype);
343*0Sstevel@tonic-gate 	return (tname);
344*0Sstevel@tonic-gate }
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate /*
347*0Sstevel@tonic-gate  * Given a tunnel driver address structure, return a pointer to a
348*0Sstevel@tonic-gate  * string naming that Ethernet host.
349*0Sstevel@tonic-gate  */
350*0Sstevel@tonic-gate const char *
351*0Sstevel@tonic-gate ehost2(const struct ether_addr *ea)
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate 	static char hbuf[MAXHOSTNAMELEN+1];
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	if (ea == NULL)
356*0Sstevel@tonic-gate 		return ("NULL");
357*0Sstevel@tonic-gate 	if (ether_ntohost(hbuf, ea) == 0)
358*0Sstevel@tonic-gate 		return (hbuf);
359*0Sstevel@tonic-gate 	return (ether_ntoa(ea));
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate 
362*0Sstevel@tonic-gate const char *
363*0Sstevel@tonic-gate ehost(const ppptun_atype *pap)
364*0Sstevel@tonic-gate {
365*0Sstevel@tonic-gate 	return (ehost2((const struct ether_addr *)pap));
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate /*
369*0Sstevel@tonic-gate  * Given an Internet address (in network byte order), return a pointer
370*0Sstevel@tonic-gate  * to a string naming the host.
371*0Sstevel@tonic-gate  */
372*0Sstevel@tonic-gate const char *
373*0Sstevel@tonic-gate ihost(uint32_t haddr)
374*0Sstevel@tonic-gate {
375*0Sstevel@tonic-gate 	struct hostent *hp;
376*0Sstevel@tonic-gate 	struct sockaddr_in sin;
377*0Sstevel@tonic-gate 
378*0Sstevel@tonic-gate 	(void) memset(&sin, '\0', sizeof (sin));
379*0Sstevel@tonic-gate 	sin.sin_addr.s_addr = haddr;
380*0Sstevel@tonic-gate 	hp = gethostbyaddr((const char *)&sin, sizeof (sin), AF_INET);
381*0Sstevel@tonic-gate 	if (hp != NULL)
382*0Sstevel@tonic-gate 		return (hp->h_name);
383*0Sstevel@tonic-gate 	return (inet_ntoa(sin.sin_addr));
384*0Sstevel@tonic-gate }
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate int
387*0Sstevel@tonic-gate hexdecode(char chr)
388*0Sstevel@tonic-gate {
389*0Sstevel@tonic-gate 	if (chr >= '0' && chr <= '9')
390*0Sstevel@tonic-gate 		return ((int)(chr - '0'));
391*0Sstevel@tonic-gate 	if (chr >= 'a' && chr <= 'F')
392*0Sstevel@tonic-gate 		return ((int)(chr - 'a' + 10));
393*0Sstevel@tonic-gate 	return ((int)(chr - 'A' + 10));
394*0Sstevel@tonic-gate }
395