xref: /titanic_51/usr/src/lib/libbc/libc/net/ether_addr.c (revision 5d54f3d8999eac1762fe0a8c7177d20f1f201fae)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
22*5d54f3d8Smuffin /*
23*5d54f3d8Smuffin  * Copyright 1990 Sun Microsystems, Inc.  All rights reserved.
24*5d54f3d8Smuffin  * Use is subject to license terms.
25*5d54f3d8Smuffin  */
26*5d54f3d8Smuffin 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * All routines necessary to deal with the file /etc/ethers.  The file
317c478bd9Sstevel@tonic-gate  * contains mappings from 48 bit ethernet addresses to their corresponding
327c478bd9Sstevel@tonic-gate  * hosts name.  The addresses have an ascii representation of the form
337c478bd9Sstevel@tonic-gate  * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff;  the
347c478bd9Sstevel@tonic-gate  * bytes are always in network order.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/socket.h>
407c478bd9Sstevel@tonic-gate #include <net/if.h>
417c478bd9Sstevel@tonic-gate #include <netinet/in.h>
427c478bd9Sstevel@tonic-gate #include <net/if_arp.h>
437c478bd9Sstevel@tonic-gate #include <netinet/if_ether.h>
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate static char *domain; /* NIS domain name */
467c478bd9Sstevel@tonic-gate static int usingyellow;
477c478bd9Sstevel@tonic-gate static char *ethers = "/etc/ethers";
487c478bd9Sstevel@tonic-gate 
497c478bd9Sstevel@tonic-gate /*
507c478bd9Sstevel@tonic-gate  * Parses a line from /etc/ethers into its components.  The line has the form
517c478bd9Sstevel@tonic-gate  * 8:0:20:1:17:c8	krypton
527c478bd9Sstevel@tonic-gate  * where the first part is a 48 bit ethernet addrerss and the second is
537c478bd9Sstevel@tonic-gate  * the corresponding hosts name.
547c478bd9Sstevel@tonic-gate  * Returns zero if successful, non-zero otherwise.
55*5d54f3d8Smuffin  *
56*5d54f3d8Smuffin  * Arguments
57*5d54f3d8Smuffin  *	s:		the string to be parsed
58*5d54f3d8Smuffin  *	e:		ethernet address struct to be filled in
59*5d54f3d8Smuffin  *	hostname:	hosts name to be set
607c478bd9Sstevel@tonic-gate  */
61*5d54f3d8Smuffin int
62*5d54f3d8Smuffin ether_line(char *s, struct ether_addr *e, char *hostname)
637c478bd9Sstevel@tonic-gate {
64*5d54f3d8Smuffin 	int i;
657c478bd9Sstevel@tonic-gate 	unsigned int t[6];
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	i = sscanf(s, " %x:%x:%x:%x:%x:%x %s",
687c478bd9Sstevel@tonic-gate 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
697c478bd9Sstevel@tonic-gate 	if (i != 7) {
707c478bd9Sstevel@tonic-gate 		return (7 - i);
717c478bd9Sstevel@tonic-gate 	}
727c478bd9Sstevel@tonic-gate 	for (i = 0; i < 6; i++)
737c478bd9Sstevel@tonic-gate 		e->ether_addr_octet[i] = t[i];
747c478bd9Sstevel@tonic-gate 	return (0);
757c478bd9Sstevel@tonic-gate }
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Converts a 48 bit ethernet number to its string representation.
797c478bd9Sstevel@tonic-gate  */
807c478bd9Sstevel@tonic-gate #define EI(i)	(unsigned int)(e->ether_addr_octet[(i)])
817c478bd9Sstevel@tonic-gate char *
82*5d54f3d8Smuffin ether_ntoa(struct ether_addr *e)
837c478bd9Sstevel@tonic-gate {
847c478bd9Sstevel@tonic-gate 	static char *s;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	if (s == 0) {
877c478bd9Sstevel@tonic-gate 		s = (char *)malloc(18);
887c478bd9Sstevel@tonic-gate 		if (s == 0)
897c478bd9Sstevel@tonic-gate 			return (0);
907c478bd9Sstevel@tonic-gate 	}
917c478bd9Sstevel@tonic-gate 	s[0] = 0;
927c478bd9Sstevel@tonic-gate 	sprintf(s, "%x:%x:%x:%x:%x:%x",
937c478bd9Sstevel@tonic-gate 	    EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
947c478bd9Sstevel@tonic-gate 	return (s);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate  * Converts a ethernet address representation back into its 48 bits.
997c478bd9Sstevel@tonic-gate  */
1007c478bd9Sstevel@tonic-gate struct ether_addr *
101*5d54f3d8Smuffin ether_aton(char *s)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 	static struct ether_addr *ep;
104*5d54f3d8Smuffin 	int i;
1057c478bd9Sstevel@tonic-gate 	unsigned int t[6];
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate 	if (ep == 0) {
1087c478bd9Sstevel@tonic-gate 		ep = (struct ether_addr *)calloc(1, sizeof (struct ether_addr));
1097c478bd9Sstevel@tonic-gate 		if (ep == 0)
1107c478bd9Sstevel@tonic-gate 			return (0);
1117c478bd9Sstevel@tonic-gate 	}
1127c478bd9Sstevel@tonic-gate 	i = sscanf(s, " %x:%x:%x:%x:%x:%x",
1137c478bd9Sstevel@tonic-gate 	    &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
1147c478bd9Sstevel@tonic-gate 	if (i != 6)
1157c478bd9Sstevel@tonic-gate 	    return ((struct ether_addr *)NULL);
1167c478bd9Sstevel@tonic-gate 	for (i = 0; i < 6; i++)
1177c478bd9Sstevel@tonic-gate 		ep->ether_addr_octet[i] = t[i];
1187c478bd9Sstevel@tonic-gate 	return (ep);
1197c478bd9Sstevel@tonic-gate }
120