Code:
struct nlist nl[] = {
#define X_ARPTAB 0
{ "_arptab" },
#define X_ARPTAB_SIZE 1
{ "_arptab_size" },
#define N_SYSMAP 2
{ "_Sysmap" },
#define N_SYSSIZE 3
{ "_Syssize" },
{ "" },
};
static struct pte *Sysmap;
/*
* Dump the entire arp table
*/
dump(kernel, mem)
char *kernel, *mem;
{
extern int h_errno;
struct arptab *at;
struct hostent *hp;
int bynumber, mf, arptab_size, sz;
char *host, *malloc();
off_t lseek();
if (nlist(kernel, nl) < 0 || nl[X_ARPTAB_SIZE].n_type == 0) {
fprintf(stderr, "arp: %s: bad namelist\n", kernel);
exit(1);
}
mf = open(mem, O_RDONLY);
if (mf < 0) {
fprintf(stderr, "arp: cannot open %s\n", mem);
exit(1);
}
if (kflag) {
off_t off;
Sysmap = (struct pte *)
malloc((u_int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
if (!Sysmap) {
fputs("arp: can't get memory for Sysmap.\n", stderr);
exit(1);
}
off = nl[N_SYSMAP].n_value & ~KERNBASE;
(void)lseek(mf, off, L_SET);
(void)read(mf, (char *)Sysmap,
(int)(nl[N_SYSSIZE].n_value * sizeof(struct pte)));
}
klseek(mf, (long)nl[X_ARPTAB_SIZE].n_value, L_SET);
read(mf, &arptab_size, sizeof arptab_size);
if (arptab_size <= 0 || arptab_size > 1000) {
fprintf(stderr, "arp: %s: namelist wrong\n", kernel);
exit(1);
}
sz = arptab_size * sizeof (struct arptab);
at = (struct arptab *)malloc((u_int)sz);
if (at == NULL) {
fputs("arp: can't get memory for arptab.\n", stderr);
exit(1);
}
klseek(mf, (long)nl[X_ARPTAB].n_value, L_SET);
if (read(mf, (char *)at, sz) != sz) {
perror("arp: error reading arptab");
exit(1);
}
close(mf);
for (bynumber = 0; arptab_size-- > 0; at++) {
if (at->at_iaddr.s_addr == 0 || at->at_flags == 0)
continue;
if (bynumber == 0)
hp = gethostbyaddr((caddr_t)&at->at_iaddr,
sizeof at->at_iaddr, AF_INET);
else
hp = 0;
if (hp)
host = hp->h_name;
else {
host = "?";
if (h_errno == TRY_AGAIN)
bynumber = 1;
}
printf("%s (%s) at ", host, inet_ntoa(at->at_iaddr));
if (at->at_flags & ATF_COM)
ether_print(at->at_enaddr);
else
printf("(incomplete)");
if (at->at_flags & ATF_PERM)
printf(" permanent");
if (at->at_flags & ATF_PUBL)
printf(" published");
if (at->at_flags & ATF_USETRAILERS)
printf(" trailers");
printf("\n");
}
}
/*
* Seek into the kernel for a value.
*/
klseek(fd, base, off)
int fd, off;
off_t base;
{
off_t lseek();
if (kflag) { /* get kernel pte */
base &= ~KERNBASE;
base = ctob(Sysmap[btop(base)].pg_pfnum) + (base & PGOFSET);
}
(void)lseek(fd, base, off);
}