Home | History | Annotate | Line # | Download | only in stdethers
stdethers.c revision 1.3
      1 /*	$NetBSD: stdethers.c,v 1.3 1997/07/18 21:57:08 thorpej 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/types.h>
     35 #include <sys/param.h>
     36 #include <sys/socket.h>
     37 #include <net/if.h>
     38 #include <net/if_ether.h>
     39 #include <netinet/in.h>
     40 #ifndef NTOA_FIX	/* XXX the below is wrong with old ARP code */
     41 #include <net/if_ether.h>
     42 #endif
     43 #include <ctype.h>
     44 #include <err.h>
     45 #include <limits.h>
     46 #include <stdio.h>
     47 #include <string.h>
     48 
     49 #include "protos.h"
     50 
     51 int	main __P((int, char *[]));
     52 void	usage __P((void));
     53 
     54 extern	char *__progname;		/* from crt0.o */
     55 
     56 #ifndef NTOA_FIX
     57 #define	NTOA(x) ether_ntoa(x)
     58 #else
     59 char	*working_ntoa __P((struct ether_addr *));
     60 
     61 #define NTOA(x) working_ntoa(x)
     62 
     63 /*
     64  * As of 1995-12-02 NetBSD has an SunOS 4 incompatible ether_ntoa.
     65  * The code in usr/lib/libc/net/ethers seems to do the correct thing
     66  * when asking YP but not when returning string from ether_ntoa.
     67  */
     68 
     69 char *
     70 working_ntoa(e)
     71 	struct ether_addr *e;
     72 {
     73 	static char a[] = "xx:xx:xx:xx:xx:xx";
     74 
     75 	sprintf(a, "%x:%x:%x:%x:%x:%x",
     76 	    e->ether_addr_octet[0], e->ether_addr_octet[1],
     77 	    e->ether_addr_octet[2], e->ether_addr_octet[3],
     78 	    e->ether_addr_octet[4], e->ether_addr_octet[5]);
     79 	return a;
     80 }
     81 #endif
     82 
     83 int
     84 main(argc, argv)
     85 	int argc;
     86 	char *argv[];
     87 {
     88 	FILE *data_file;
     89 	char data_line[_POSIX2_LINE_MAX];
     90 	int line_no = 0, len;
     91 	char *fname;
     92 	struct ether_addr eth_addr;
     93 	char hostname[MAXHOSTNAMELEN];
     94 
     95 	if (argc > 2)
     96 		usage();
     97 
     98 	if (argc == 2) {
     99 		fname = argv[1];
    100 		data_file = fopen(fname, "r");
    101 		if (data_file == NULL)
    102 			err(1, "%s", fname);
    103 	} else {
    104 		fname = "<stdin>";
    105 		data_file = stdin;
    106 	}
    107 
    108 	while (read_line(data_file, data_line, sizeof(data_line))) {
    109 		line_no++;
    110 		len = strlen(data_line);
    111 
    112 		if (len > 1) {
    113 			if (data_line[0] == '#') {
    114 				continue;
    115 			}
    116 		} else
    117 			continue;
    118 
    119 		/*
    120 		 * Check if we have the whole line
    121 		 */
    122 		if (data_line[len - 1] != '\n') {
    123 			warnx("%s line %d: line to long, skipping",
    124 			    fname, line_no);
    125 			continue;
    126 		} else
    127 			data_line[len - 1] = '\0';
    128 
    129 		if (ether_line(data_line, &eth_addr, hostname) == 0)
    130 			printf("%s\t%s\n", NTOA(&eth_addr), hostname);
    131 		else
    132 			warnx("ignoring line %d: `%s'", line_no, data_line);
    133 	}
    134 
    135 	exit(0);
    136 }
    137 
    138 void
    139 usage()
    140 {
    141 
    142 	fprintf(stderr, "usage: %s [file]\n", __progname);
    143 	exit(1);
    144 }
    145