#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <netdb.h>
#include <nlist.h>
#include <net/if.h>
#include <netinet/if_ether.h>


#define KERNEL "/vmunix"
#define MEMORY "/dev/kmem"

struct nlist nl[] = {
#define	X_IFNET	0
	{ "_ifnet" },
	{ "" },
};

/*
 * Dump the entire arp table
 */
u_char *physaddr(net)
char	*net;
{
	int mf, unit;
	struct ifnet in, *ifp;
	char *cp;
	static u_char addr[sizeof(struct ether_addr)];
	char nambuf[IFNAMSIZ];

	for (cp = net; (cp < (net + IFNAMSIZ)) && *cp; cp++)
	  if (*cp >= '0' && *cp <= '9')
	    break;
	if (*cp == '\0' || cp == (net + IFNAMSIZ))
	  return((u_char *) 0);
	unit = *cp - '0';

	nlist(KERNEL, nl);
	if(nl[X_IFNET].n_type == 0) {
		fprintf(stderr, "physaddr: %s: bad namelist\n", KERNEL);
		exit(1);
	}
	mf = open(MEMORY, 0);
	if(mf < 0) {
		fprintf(stderr, "physaddr: cannot open %s\n", MEMORY);
		exit(1);
	}
	ifp = (struct ifnet *)nl[X_IFNET].n_value;
	lseek(mf, (long)ifp, 0);
	read(mf, &ifp, sizeof(ifp));
	for (; ifp; ifp=in.if_next) {
	    lseek(mf, (long)ifp, 0);
	    read(mf, &in, sizeof(in));
	    lseek(mf, (long)in.if_name, 0);
	    read(mf, nambuf, IFNAMSIZ);
	    if (bcmp(nambuf, net, (unsigned)(cp - net)))
	      continue;
	    if (unit == in.if_unit)
	      break;
	}
	lseek(mf, ((long)ifp) + sizeof(in), 0);
	read(mf, &addr[0], sizeof(struct ether_addr));
	close(mf);
	return(&addr[0]);
}



ether_print(cp)
	u_char *cp;
{
	printf("%02x:%02x:%02x:%02x:%02x:%02x\n", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]);
}

