| 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 <errno.h> |
| 22 |
|
| #include <stdio.h> |
| 23 |
|
| #include <stdlib.h> |
| 24 |
|
| #include <string.h> |
| 25 |
|
| #include <unistd.h> |
| 26 |
|
| #include <sys/socket.h> |
| 27 |
|
| #include <sys/ioctl.h> |
| 28 |
|
| #include <netinet/in.h> |
| 29 |
|
| #include <arpa/inet.h> |
| 30 |
|
| #include <net/if_arp.h> |
| 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 |
3 |
| 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 |
3 |
| err = 1; |
| 55 |
|
| |
| 56 |
|
| /* A device name must be a null terminated string whose length is less |
| 57 |
|
| * than 16 bytes */ |
| 58 |
3 |
▶ | if ( !strlen(device) || (strlen(device) >= 16) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !strlen(device) || (strlen(device) >= 16) ) |
2 |
1 |
3 |
| if ( !strlen(device) || (strlen(device) >= 16) ) |
1 |
1 |
2 |
|
| 59 |
2 |
| fprintf(stderr, "No valid device name found.\n"); |
| 60 |
|
| |
| 61 |
|
| /* Is there a buffer allocated to store the hardware address? */ |
| 62 |
1 |
▶ | else if (hw_addr == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (hw_addr == NULL) |
0 |
1 |
1 |
|
| 63 |
0 |
| fprintf(stderr, "No memory allocated to store the hardware address.\n"); |
| 64 |
|
| |
| 65 |
|
| /* Make sure the ip address is valid */ |
| 66 |
1 |
▶ | else if ( !strlen(ip) || (inet_aton(ip, &ipaddr) == 0) ) |
Branch: 2 branches
| Branch | True | False | Total |
| else if ( !strlen(ip) || (inet_aton(ip, &ipaddr) == 0) ) |
1 |
0 |
1 |
| else if ( !strlen(ip) || (inet_aton(ip, &ipaddr) == 0) ) |
1 |
0 |
1 |
|
| 67 |
1 |
| fprintf(stderr, "Invalid ip address.\n"); |
| 68 |
|
| |
| 69 |
|
| /* Create the socket */ |
| 70 |
0 |
▶ | else if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| else if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == -1) |
0 |
0 |
0 |
|
| 71 |
0 |
| perror("Socket"); |
| 72 |
|
| |
| 73 |
|
| else { |
| 74 |
|
| |
| 75 |
|
| /* Set up the protocol address */ |
| 76 |
0 |
| memset(&areq, 0, sizeof(areq)); |
| 77 |
0 |
| sin = (struct sockaddr_in *) &areq.arp_pa; |
| 78 |
0 |
| sin->sin_family = AF_INET; |
| 79 |
0 |
| sin->sin_addr = ipaddr; |
| 80 |
|
| |
| 81 |
|
| /* Set up the hardware address */ |
| 82 |
0 |
| sin = (struct sockaddr_in *) &areq.arp_ha; |
| 83 |
0 |
| sin->sin_family = ARPHRD_ETHER; |
| 84 |
0 |
| strcpy(areq.arp_dev, device); |
| 85 |
|
| |
| 86 |
|
| /* Carry out the request */ |
| 87 |
0 |
▶ | if (ioctl(s, SIOCGARP, &areq) == -1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (ioctl(s, SIOCGARP, &areq) == -1) |
0 |
0 |
0 |
|
| 88 |
0 |
| 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 |
0 |
| close(s); |
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
3 |
| return err; |
| 106 |
|
| } |