1/* 2 * Copyright (c) 1999-2008 Apple Inc. All rights reserved. 3 * 4 * @APPLE_LICENSE_HEADER_START@ 5 * 6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights 7 * Reserved. This file contains Original Code and/or Modifications of 8 * Original Code as defined in and that are subject to the Apple Public 9 * Source License Version 1.1 (the "License"). You may not use this file 10 * except in compliance with the License. Please obtain a copy of the 11 * License at http://www.apple.com/publicsource and read it before using 12 * this file. 13 * 14 * The Original Code and all software distributed under the License are 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER 16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the 19 * License for the specific language governing rights and limitations 20 * under the License. 21 * 22 * @APPLE_LICENSE_HEADER_END@ 23 */ 24#if !defined(lint) && defined(SCCSIDS) 25static char sccsid[] = "@(#)ether_addr.c 1.2 88/05/10 4.0NFSSRC; from 1.9 88/02/08 Copyr 1985 Sun Micro"; 26#endif 27 28/* 29 * Copyright (c) 1985 by Sun Microsystems, Inc. 30 * 31 * All routines necessary to deal with the file /etc/ethers. The file 32 * contains mappings from 48 bit ethernet addresses to their corresponding 33 * hosts name. The addresses have an ascii representation of the form 34 * "x:x:x:x:x:x" where x is a hex number between 0x00 and 0xff; the 35 * bytes are always in network order. 36 */ 37 38#include <stdlib.h> 39#include <string.h> 40#include <stdio.h> 41#include <sys/types.h> 42#include <sys/socket.h> 43#include <net/if.h> 44#include <netinet/in.h> 45#include <netinet/if_ether.h> 46 47/* 48 * Parses a line from /etc/ethers into its components. The line has the form 49 * 8:0:20:1:17:c8 krypton 50 * where the first part is a 48 bit ethernet addrerss and the second is 51 * the corresponding hosts name. 52 * Returns zero if successful, non-zero otherwise. 53 */ 54int 55ether_line(const char *s, struct ether_addr *e, char *hostname) 56{ 57 int i; 58 unsigned int t[6]; 59 60 i = sscanf(s, " %x:%x:%x:%x:%x:%x %s", &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname); 61 if (i != 7) return (7 - i); 62 63 for (i = 0; i < 6; i++) e->ether_addr_octet[i] = t[i]; 64 return 0; 65} 66 67/* 68 * Converts a 48 bit ethernet number to its string representation. 69 */ 70#define EI(i) (unsigned int)(e->ether_addr_octet[(i)]) 71char * 72ether_ntoa(const struct ether_addr *e) 73{ 74 static char *s; 75 76 if (s == 0) 77 { 78 s = (char *)malloc(18); 79 if (s == 0) return NULL; 80 } 81 82 s[0] = 0; 83 sprintf(s, "%x:%x:%x:%x:%x:%x", EI(0), EI(1), EI(2), EI(3), EI(4), EI(5)); 84 return s; 85} 86 87/* 88 * Converts a ethernet address representation back into its 48 bits. 89 */ 90struct ether_addr * 91ether_aton(const char *s) 92{ 93 static struct ether_addr *ep; 94 int i; 95 unsigned int t[6]; 96 97 if (ep == 0) 98 { 99 ep = (struct ether_addr *)calloc(1, sizeof (struct ether_addr)); 100 if (ep == 0) return NULL; 101 } 102 103 i = sscanf(s, " %x:%x:%x:%x:%x:%x", &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]); 104 if (i != 6) return NULL; 105 106 for (i = 0; i < 6; i++) ep->ether_addr_octet[i] = t[i]; 107 return ep; 108} 109