File Coverage

arp_lookup_linux.c
Criterion Covered Total %
statement 17 29 58.6
branch 7 14 50.0
condition n/a
subroutine n/a
pod n/a
total 24 43 55.8


line stmt bran cond sub pod time code
1             /*
2             Perl ARP Extension
3             Lookup the MAC address of an ip address
4             Linux code
5              
6             Programmed by Bastian Ballmann and Alexander Mueller
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
22             #include
23             #include
24             #include
25             #include
26             #include
27             #include
28             #include
29             #include
30             #include
31              
32             /*
33             * Search for a hardware address linked to an IP address on a device
34             *
35             * @device: network interface name we are going to query
36             * @ip: ip address (IPv4 numbers-and-dots notation) whose hardware address
37             * is going to be looked for
38             * @hw_addr: buffer containing the hardware mac_address
39             *
40             * \returns 0 if a hardware address has been found. @mac is set accordingly as
41             * a null terminated string.
42             * 1 if an error occured
43             */
44             int
45 1           arp_lookup_linux (
46             const char *device, const char *ip, char *hw_addr)
47             {
48             int s;
49             unsigned char err;
50             struct in_addr ipaddr;
51             struct arpreq areq;
52             struct sockaddr_in *sin;
53              
54 1           err = 1;
55              
56             /* A device name must be a null terminated string whose length is less
57             * than 16 bytes */
58 1 50         if ( !strlen(device) || (strlen(device) >= 16) )
    50          
59 0           fprintf(stderr, "No valid device name found.\n");
60              
61             /* Is there a buffer allocated to store the hardware address? */
62 1 50         else if (hw_addr == NULL)
63 0           fprintf(stderr, "No memory allocated to store the hardware address.\n");
64              
65             /* Make sure the ip address is valid */
66 1 50         else if ( !strlen(ip) || (inet_aton(ip, &ipaddr) == 0) )
    50          
67 0           fprintf(stderr, "Invalid ip address.\n");
68              
69             /* Create the socket */
70 1 50         else if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1)
71 0           perror("Socket");
72              
73             else {
74              
75             /* Set up the protocol address */
76 1           memset(&areq, 0, sizeof(areq));
77 1           sin = (struct sockaddr_in *) &areq.arp_pa;
78 1           sin->sin_family = AF_INET;
79 1           sin->sin_addr = ipaddr;
80              
81             /* Set up the hardware address */
82 1           sin = (struct sockaddr_in *) &areq.arp_ha;
83 1           sin->sin_family = ARPHRD_ETHER;
84 1           strcpy(areq.arp_dev, device);
85              
86             /* Carry out the request */
87 1 50         if (ioctl(s, SIOCGARP, &areq) == -1)
88 1           perror("SIOCGARP");
89              
90             else {
91 0           sprintf(hw_addr, "%02x:%02x:%02x:%02x:%02x:%02x",
92 0           areq.arp_ha.sa_data[0] & 0xFF,
93 0           areq.arp_ha.sa_data[1] & 0xFF,
94 0           areq.arp_ha.sa_data[2] & 0xFF,
95 0           areq.arp_ha.sa_data[3] & 0xFF,
96 0           areq.arp_ha.sa_data[4] & 0xFF,
97 0           areq.arp_ha.sa_data[5] & 0xFF);
98 0           err = 0;
99             }
100              
101             /* Close the current socket */
102 1           close(s);
103             }
104              
105 1           return err;
106             }