| 1 |
/* $Id: ethers.c,v 1.1 2000/04/08 20:50:28 dugsong Exp $ */ |
| 2 |
/* $OpenBSD: ethers.c,v 1.10 1998/11/18 23:28:54 deraadt Exp $ */ |
| 3 |
|
| 4 |
/* |
| 5 |
* Copyright (c) 1998 Todd C. Miller <[email protected]> |
| 6 |
* All rights reserved. |
| 7 |
* |
| 8 |
* Redistribution and use in source and binary forms, with or without |
| 9 |
* modification, are permitted provided that the following conditions |
| 10 |
* are met: |
| 11 |
* 1. Redistributions of source code must retain the above copyright |
| 12 |
* notice, this list of conditions and the following disclaimer. |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* 3. The name of the author may not be used to endorse or promote products |
| 17 |
* derived from this software without specific prior written permission. |
| 18 |
* |
| 19 |
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 |
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
| 21 |
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 |
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 23 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; |
| 25 |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 26 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 27 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 28 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
*/ |
| 30 |
|
| 31 |
/* |
| 32 |
* ethers(3) a la Sun. |
| 33 |
* Originally Written by Roland McGrath <[email protected]> 10/14/93. |
| 34 |
* Substantially modified by Todd C. Miller <[email protected]> |
| 35 |
*/ |
| 36 |
|
| 37 |
#include <sys/types.h> |
| 38 |
#include <sys/socket.h> |
| 39 |
#include <net/if.h> |
| 40 |
#include <netinet/in.h> |
| 41 |
#include <netinet/if_ether.h> |
| 42 |
#include <errno.h> |
| 43 |
#include <stdio.h> |
| 44 |
#include <stdlib.h> |
| 45 |
#include <string.h> |
| 46 |
#include <ctype.h> |
| 47 |
|
| 48 |
char * |
| 49 |
ether_ntoa(e) |
| 50 |
struct ether_addr *e; |
| 51 |
{ |
| 52 |
static char a[] = "xx:xx:xx:xx:xx:xx"; |
| 53 |
|
| 54 |
if (e->ether_addr_octet[0] > 0xFF || e->ether_addr_octet[1] > 0xFF || |
| 55 |
e->ether_addr_octet[2] > 0xFF || e->ether_addr_octet[3] > 0xFF || |
| 56 |
e->ether_addr_octet[4] > 0xFF || e->ether_addr_octet[5] > 0xFF) { |
| 57 |
errno = EINVAL; |
| 58 |
return (NULL); |
| 59 |
} |
| 60 |
|
| 61 |
(void)sprintf(a, "%02x:%02x:%02x:%02x:%02x:%02x", |
| 62 |
e->ether_addr_octet[0], e->ether_addr_octet[1], |
| 63 |
e->ether_addr_octet[2], e->ether_addr_octet[3], |
| 64 |
e->ether_addr_octet[4], e->ether_addr_octet[5]); |
| 65 |
|
| 66 |
return (a); |
| 67 |
} |
| 68 |
|
| 69 |
static char * |
| 70 |
_ether_aton(s, e) |
| 71 |
char *s; |
| 72 |
struct ether_addr *e; |
| 73 |
{ |
| 74 |
int i; |
| 75 |
long l; |
| 76 |
char *pp; |
| 77 |
|
| 78 |
while (isspace(*s)) |
| 79 |
s++; |
| 80 |
|
| 81 |
/* expect 6 hex octets separated by ':' or space/NUL if last octet */ |
| 82 |
for (i = 0; i < 6; i++) { |
| 83 |
l = strtol(s, &pp, 16); |
| 84 |
if (pp == s || l > 0xFF || l < 0) |
| 85 |
return (NULL); |
| 86 |
if (!(*pp == ':' || (i == 5 && (isspace(*pp) || *pp == '\0')))) |
| 87 |
return (NULL); |
| 88 |
e->ether_addr_octet[i] = (u_char)l; |
| 89 |
s = pp + 1; |
| 90 |
} |
| 91 |
|
| 92 |
/* return character after the octets ala strtol(3) */ |
| 93 |
return (pp); |
| 94 |
} |
| 95 |
|
| 96 |
struct ether_addr * |
| 97 |
ether_aton(s) |
| 98 |
char *s; |
| 99 |
{ |
| 100 |
static struct ether_addr n; |
| 101 |
|
| 102 |
return (_ether_aton(s, &n) ? &n : NULL); |
| 103 |
} |
| 104 |
|