From owner-freebsd-net@FreeBSD.ORG Fri Aug 15 04:44:46 2003 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E384237B401 for ; Fri, 15 Aug 2003 04:44:46 -0700 (PDT) Received: from raven.ravenbrook.com (raven.ravenbrook.com [193.82.131.18]) by mx1.FreeBSD.org (Postfix) with ESMTP id BE4D943FA3 for ; Fri, 15 Aug 2003 04:44:45 -0700 (PDT) (envelope-from nb@ravenbrook.com) Received: from thrush.ravenbrook.com (thrush.ravenbrook.com [193.112.141.249]) by raven.ravenbrook.com (8.12.6/8.12.6) with ESMTP id h7FBihuL070383 for ; Fri, 15 Aug 2003 12:44:43 +0100 (BST) (envelope-from nb@ravenbrook.com) Received: from thrush.ravenbrook.com (localhost [127.0.0.1]) by thrush.ravenbrook.com (8.12.9/8.12.9) with ESMTP id h7FBgAnH075998 for ; Fri, 15 Aug 2003 12:42:10 +0100 (BST) (envelope-from nb@thrush.ravenbrook.com) From: Nick Barnes To: freebsd-net@freebsd.org In-Reply-To: Message from Nick Barnes <75602.1060946037@thrush.ravenbrook.com> Date: Fri, 15 Aug 2003 12:42:10 +0100 Message-ID: <75997.1060947730@thrush.ravenbrook.com> Sender: nb@ravenbrook.com Subject: Re: Translate MAC address to IP address X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 15 Aug 2003 11:44:47 -0000 At 2003-08-15 11:13:57+0000, I wrote: > Next week I may spend the time to extend my "ethercount" program, > using the "pingall" code and the guts of "arp -a", to report using IP > addresses instead of MACs. FYI, here are the relevant guts of "arp -na": /* Usual BSD copyright notice goes here */ #include #include #include #include #include #include #include #include #include #include #include #define ROUNDUP(a) \ ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long)) int main(int argc, char *argv[]) { int mib[6]; size_t needed; char *lim, *buf, *next; if (argc != 1) { (void)fprintf(stderr, "Usage: %s\n", argv[0]); exit(1); } mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; mib[3] = AF_INET; mib[4] = NET_RT_FLAGS; mib[5] = RTF_LLINFO; if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) errx(1, "route-sysctl-estimate"); if ((buf = malloc(needed)) == NULL) errx(1, "malloc"); if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) errx(1, "actual retrieval of routing table"); lim = buf + needed; next = buf; while (next < lim) { struct rt_msghdr *rtm = (struct rt_msghdr *)next; struct sockaddr_inarp *sinarp = (struct sockaddr_inarp *)(rtm + 1); struct sockaddr_dl *sdl = (struct sockaddr_dl *)((char *)sinarp + ROUNDUP(sinarp->sin_len)); if (sdl->sdl_alen) { /* complete ARP entry */ printf("%s at ", inet_ntoa(sinarp->sin_addr)); printf("%s", ether_ntoa((struct ether_addr *)LLADDR(sdl))); printf("\n"); } next += rtm->rtm_msglen; } free(buf); return(0); }