xref: /onnv-gate/usr/src/lib/libbc/libc/net/ether_addr.c (revision 722:636b850d4ee9)
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  */
22*722Smuffin /*
23*722Smuffin  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
24*722Smuffin  * Use is subject to license terms.
25*722Smuffin  */
26*722Smuffin 
27*722Smuffin #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * All routines necessary to deal with the file /etc/ethers.  The file
310Sstevel@tonic-gate  * contains mappings from 48 bit ethernet addresses to their corresponding
320Sstevel@tonic-gate  * hosts name.  The addresses have an ascii representation of the form
330Sstevel@tonic-gate  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
340Sstevel@tonic-gate  * bytes are always in network order.
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate 
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <sys/types.h>
390Sstevel@tonic-gate #include <sys/socket.h>
400Sstevel@tonic-gate #include <net/if.h>
410Sstevel@tonic-gate #include <netinet/in.h>
420Sstevel@tonic-gate #include <net/if_arp.h>
430Sstevel@tonic-gate #include <netinet/if_ether.h>
440Sstevel@tonic-gate 
450Sstevel@tonic-gate static char *domain; /* NIS domain name */
460Sstevel@tonic-gate static int usingyellow;
470Sstevel@tonic-gate static char *ethers = "/etc/ethers";
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*
500Sstevel@tonic-gate  * Parses a line from /etc/ethers into its components.  The line has the form
510Sstevel@tonic-gate  * 8:0:20:1:17:c8	krypton
520Sstevel@tonic-gate  * where the first part is a 48 bit ethernet addrerss and the second is
530Sstevel@tonic-gate  * the corresponding hosts name.
540Sstevel@tonic-gate  * Returns zero if successful, non-zero otherwise.
55*722Smuffin  *
56*722Smuffin  * Arguments
57*722Smuffin  *	s:		the string to be parsed
58*722Smuffin  *	e:		ethernet address struct to be filled in
59*722Smuffin  *	hostname:	hosts name to be set
600Sstevel@tonic-gate  */
61*722Smuffin int
ether_line(char * s,struct ether_addr * e,char * hostname)62*722Smuffin ether_line(char *s, struct ether_addr *e, char *hostname)
630Sstevel@tonic-gate {
64*722Smuffin 	int i;
650Sstevel@tonic-gate 	unsigned int t[6];
660Sstevel@tonic-gate 
670Sstevel@tonic-gate 	i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
680Sstevel@tonic-gate 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
690Sstevel@tonic-gate 	if (i != 7) {
700Sstevel@tonic-gate 		return (7 - i);
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 	for (i = 0; i < 6; i++)
730Sstevel@tonic-gate 		e->ether_addr_octet[i] = t[i];
740Sstevel@tonic-gate 	return (0);
750Sstevel@tonic-gate }
760Sstevel@tonic-gate 
770Sstevel@tonic-gate /*
780Sstevel@tonic-gate  * Converts a 48 bit ethernet number to its string representation.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate #define EI(i)	(unsigned int)(e->ether_addr_octet[(i)])
810Sstevel@tonic-gate char *
ether_ntoa(struct ether_addr * e)82*722Smuffin ether_ntoa(struct ether_addr *e)
830Sstevel@tonic-gate {
840Sstevel@tonic-gate 	static char *s;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (s == 0) {
870Sstevel@tonic-gate 		s = (char *)malloc(18);
880Sstevel@tonic-gate 		if (s == 0)
890Sstevel@tonic-gate 			return (0);
900Sstevel@tonic-gate 	}
910Sstevel@tonic-gate 	s[0] = 0;
920Sstevel@tonic-gate 	sprintf(s, "%x:%x:%x:%x:%x:%x",
930Sstevel@tonic-gate 	    EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
940Sstevel@tonic-gate 	return (s);
950Sstevel@tonic-gate }
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * Converts a ethernet address representation back into its 48 bits.
990Sstevel@tonic-gate  */
1000Sstevel@tonic-gate struct ether_addr *
ether_aton(char * s)101*722Smuffin ether_aton(char *s)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	static struct ether_addr *ep;
104*722Smuffin 	int i;
1050Sstevel@tonic-gate 	unsigned int t[6];
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	if (ep == 0) {
1080Sstevel@tonic-gate 		ep = (struct ether_addr *)calloc(1, sizeof (struct ether_addr));
1090Sstevel@tonic-gate 		if (ep == 0)
1100Sstevel@tonic-gate 			return (0);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 	i = sscanf(s, " %x:%x:%x:%x:%x:%x",
1130Sstevel@tonic-gate 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
1140Sstevel@tonic-gate 	if (i != 6)
1150Sstevel@tonic-gate 	    return ((struct ether_addr *)NULL);
1160Sstevel@tonic-gate 	for (i = 0; i < 6; i++)
1170Sstevel@tonic-gate 		ep->ether_addr_octet[i] = t[i];
1180Sstevel@tonic-gate 	return (ep);
1190Sstevel@tonic-gate }
120