]> git.saurik.com Git - apple/libinfo.git/blame - gen.subproj/ether_addr.c
Libinfo-542.40.3.tar.gz
[apple/libinfo.git] / gen.subproj / ether_addr.c
CommitLineData
03fb6eb0 1/*
f475de6c 2 * Copyright (c) 1999-2018 Apple Inc. All rights reserved.
03fb6eb0
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
ad21edcc
A
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.
03fb6eb0
A
13 *
14 * The Original Code and all software distributed under the License are
ad21edcc 15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
03fb6eb0
A
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
ad21edcc
A
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.
03fb6eb0
A
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 */
f475de6c 37#include "libinfo_common.h"
03fb6eb0
A
38
39#include <stdlib.h>
40#include <string.h>
41#include <stdio.h>
42#include <sys/types.h>
43#include <sys/socket.h>
44#include <net/if.h>
45#include <netinet/in.h>
46#include <netinet/if_ether.h>
47
03fb6eb0
A
48/*
49 * Parses a line from /etc/ethers into its components. The line has the form
50 * 8:0:20:1:17:c8 krypton
51 * where the first part is a 48 bit ethernet addrerss and the second is
52 * the corresponding hosts name.
53 * Returns zero if successful, non-zero otherwise.
54 */
f475de6c 55LIBINFO_EXPORT
d49d4c81
A
56int
57ether_line(const char *s, struct ether_addr *e, char *hostname)
03fb6eb0 58{
d49d4c81 59 int i;
03fb6eb0
A
60 unsigned int t[6];
61
d49d4c81
A
62 i = sscanf(s, " %x:%x:%x:%x:%x:%x %s", &t[0], &t[1], &t[2], &t[3], &t[4], &t[5], hostname);
63 if (i != 7) return (7 - i);
64
65 for (i = 0; i < 6; i++) e->ether_addr_octet[i] = t[i];
66 return 0;
03fb6eb0
A
67}
68
69/*
70 * Converts a 48 bit ethernet number to its string representation.
71 */
d49d4c81 72#define EI(i) (unsigned int)(e->ether_addr_octet[(i)])
f475de6c 73LIBINFO_EXPORT
03fb6eb0 74char *
d49d4c81 75ether_ntoa(const struct ether_addr *e)
03fb6eb0
A
76{
77 static char *s;
78
d49d4c81
A
79 if (s == 0)
80 {
03fb6eb0 81 s = (char *)malloc(18);
d49d4c81 82 if (s == 0) return NULL;
03fb6eb0 83 }
d49d4c81 84
03fb6eb0 85 s[0] = 0;
d49d4c81
A
86 sprintf(s, "%x:%x:%x:%x:%x:%x", EI(0), EI(1), EI(2), EI(3), EI(4), EI(5));
87 return s;
03fb6eb0
A
88}
89
90/*
91 * Converts a ethernet address representation back into its 48 bits.
92 */
f475de6c 93LIBINFO_EXPORT
03fb6eb0 94struct ether_addr *
d49d4c81 95ether_aton(const char *s)
03fb6eb0
A
96{
97 static struct ether_addr *ep;
d49d4c81 98 int i;
03fb6eb0
A
99 unsigned int t[6];
100
d49d4c81 101 if (ep == 0)
03fb6eb0 102 {
d49d4c81
A
103 ep = (struct ether_addr *)calloc(1, sizeof (struct ether_addr));
104 if (ep == 0) return NULL;
03fb6eb0
A
105 }
106
d49d4c81
A
107 i = sscanf(s, " %x:%x:%x:%x:%x:%x", &t[0], &t[1], &t[2], &t[3], &t[4], &t[5]);
108 if (i != 6) return NULL;
03fb6eb0 109
d49d4c81
A
110 for (i = 0; i < 6; i++) ep->ether_addr_octet[i] = t[i];
111 return ep;
03fb6eb0 112}