Home | History | Annotate | Line # | Download | only in stdethers
stdethers.c revision 1.5
      1 /*	$NetBSD: stdethers.c,v 1.5 1997/10/13 04:00:27 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Mats O Jansson <moj (at) stacken.kth.se>
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by Mats O Jansson
     18  * 4. The name of the author may not be used to endorse or promote products
     19  *    derived from this software without specific prior written permission.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     22  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     23  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
     25  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31  * SUCH DAMAGE.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: stdethers.c,v 1.5 1997/10/13 04:00:27 lukem Exp $");
     37 #endif
     38 
     39 #include <sys/types.h>
     40 #include <sys/param.h>
     41 #include <sys/socket.h>
     42 #include <net/if.h>
     43 #include <net/if_ether.h>
     44 #include <netinet/in.h>
     45 #ifndef NTOA_FIX	/* XXX the below is wrong with old ARP code */
     46 #include <net/if_ether.h>
     47 #endif
     48 #include <ctype.h>
     49 #include <err.h>
     50 #include <limits.h>
     51 #include <stdio.h>
     52 #include <string.h>
     53 
     54 #include "protos.h"
     55 
     56 int	main __P((int, char *[]));
     57 void	usage __P((void));
     58 
     59 extern	char *__progname;		/* from crt0.o */
     60 
     61 #ifndef NTOA_FIX
     62 #define	NTOA(x) ether_ntoa(x)
     63 #else
     64 char	*working_ntoa __P((struct ether_addr *));
     65 
     66 #define NTOA(x) working_ntoa(x)
     67 
     68 /*
     69  * As of 1995-12-02 NetBSD has an SunOS 4 incompatible ether_ntoa.
     70  * The code in usr/lib/libc/net/ethers seems to do the correct thing
     71  * when asking YP but not when returning string from ether_ntoa.
     72  */
     73 
     74 char *
     75 working_ntoa(e)
     76 	struct ether_addr *e;
     77 {
     78 	static char a[] = "xx:xx:xx:xx:xx:xx";
     79 
     80 	sprintf(a, "%x:%x:%x:%x:%x:%x",
     81 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
     82 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
     83 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
     84 	return a;
     85 }
     86 #endif
     87 
     88 int
     89 main(argc, argv)
     90 	int argc;
     91 	char *argv[];
     92 {
     93 	FILE *data_file;
     94 	char data_line[_POSIX2_LINE_MAX];
     95 	int line_no = 0, len;
     96 	char *fname;
     97 	struct ether_addr eth_addr;
     98 	char hostname[MAXHOSTNAMELEN];
     99 
    100 	if (argc > 2)
    101 		usage();
    102 
    103 	if (argc == 2) {
    104 		fname = argv[1];
    105 		data_file = fopen(fname, "r");
    106 		if (data_file == NULL)
    107 			err(1, "%s", fname);
    108 	} else {
    109 		fname = "<stdin>";
    110 		data_file = stdin;
    111 	}
    112 
    113 	while (read_line(data_file, data_line, sizeof(data_line))) {
    114 		line_no++;
    115 		len = strlen(data_line);
    116 
    117 		if (len <= 1 || data_line[0] == '#')
    118 			continue;
    119 
    120 		/*
    121 		 * Check if we have the whole line
    122 		 */
    123 		if (data_line[len - 1] != '\n') {
    124 			warnx("%s line %d: line to long, skipping",
    125 			    fname, line_no);
    126 			continue;
    127 		} else
    128 			data_line[len - 1] = '\0';
    129 
    130 		if (ether_line(data_line, &eth_addr, hostname) == 0)
    131 			printf("%s\t%s\n", NTOA(&eth_addr), hostname);
    132 		else
    133 			warnx("ignoring line %d: `%s'", line_no, data_line);
    134 	}
    135 
    136 	exit(0);
    137 }
    138 
    139 void
    140 usage()
    141 {
    142 
    143 	fprintf(stderr, "usage: %s [file]\n", __progname);
    144 	exit(1);
    145 }
    146