1 /*
2 Perl ARP Extension
3 Lookup the MAC address of an ip address
4 BSD code
5 
6 Programmed by Bastian Ballmann
7 Last update: 20.09.2006
8 
9 This program is free software; you can redistribute
10 it and/or modify it under the terms of the
11 GNU General Public License version 2 as published
12 by the Free Software Foundation.
13 
14 This program is distributed in the hope that it will
15 be useful, but WITHOUT ANY WARRANTY; without even
16 the implied warranty of MERCHANTABILITY or FITNESS
17 FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License for more details.
19 */
20 
21 #include <sys/file.h>
22 #include <sys/socket.h>
23 #include <net/if_dl.h>
24 #include <net/route.h>
25 #include <net/if_arp.h>
26 #include <net/if.h>
27 #include <netinet/in.h>
28 #include <netinet/if_ether.h>
29 #include <arpa/inet.h>
30 #include <sys/sysctl.h>
31 #include <sys/types.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "arp.h"
36 
37 #define ROUNDUP(a) \
38         ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
39 
arp_lookup_bsd(const char * dev,const char * ip,char * mac)40 int arp_lookup_bsd(const char *dev, const char *ip, char *mac)
41 {
42   int mib[6];
43   size_t needed;
44   char *lim, *buf, *next;
45 
46   if ( (mac == NULL) || (dev == NULL) || (ip == NULL) )
47     return -1;
48 
49   strncpy(mac,"unknown", HEX_HW_ADDR_LEN);
50   mac[HEX_HW_ADDR_LEN-1] = '\0';
51 
52   mib[0] = CTL_NET;
53   mib[1] = PF_ROUTE;
54   mib[2] = 0;
55   mib[3] = AF_INET;
56   mib[4] = NET_RT_FLAGS;
57   mib[5] = RTF_LLINFO;
58 
59   /* Retrieve routing table */
60 
61   if(sysctl(mib, 6, NULL, &needed, NULL, 0) < 0)
62     {
63       perror("route-sysctl-estimate");
64       exit(1);
65     }
66 
67   if((buf = malloc(needed)) == NULL)
68     {
69       perror("malloc");
70       exit(1);
71     }
72 
73   if(sysctl(mib, 6, buf, &needed, NULL, 0) < 0)
74     {
75       perror("retrieval of routing table");
76       exit(1);
77     }
78 
79   lim = buf + needed;
80   next = buf;
81 
82   /* Search for the requested ip */
83   while (next < lim)
84     {
85       struct rt_msghdr *rtm = (struct rt_msghdr *)next;
86       struct sockaddr_inarp *sinarp = (struct sockaddr_inarp *)(rtm + 1);
87       struct sockaddr_dl *sdl = (struct sockaddr_dl *)((char *)sinarp + ROUNDUP(sinarp->sin_len));
88 
89       if( (sdl->sdl_alen) && (!strcmp(ip,inet_ntoa(sinarp->sin_addr))) )
90 	{
91 	  sprintf(mac,"%s", ether_ntoa((struct ether_addr *)LLADDR(sdl)));
92 	}
93 
94       next += rtm->rtm_msglen;
95     }
96 
97   free(buf);
98   return(0);
99 }
100