| 1 |
|
| /* |
| 2 |
|
| Perl ARP Extension |
| 3 |
|
| Get the MAC address of an interface |
| 4 |
|
| Linux code |
| 5 |
|
| |
| 6 |
|
| Programmed by Bastian Ballmann |
| 7 |
|
| Last update: 09.02.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 <string.h> |
| 22 |
|
| #include <stdio.h> |
| 23 |
|
| #include <stdlib.h> |
| 24 |
|
| #include <sys/types.h> |
| 25 |
|
| #include <sys/ioctl.h> |
| 26 |
|
| #include <net/ethernet.h> |
| 27 |
|
| #include <net/if.h> |
| 28 |
|
| #include "arp.h" |
| 29 |
|
| |
| 30 |
1 |
| int get_mac_linux(const char *dev, char *mac) |
| 31 |
|
| { |
| 32 |
|
| int sock; |
| 33 |
|
| struct ifreq iface; |
| 34 |
|
| |
| 35 |
1 |
▶ | if ( !strlen(mac) || !strlen(dev) ) |
Branch: 2 branches
| Branch | True | False | Total |
| if ( !strlen(mac) || !strlen(dev) ) |
1 |
0 |
1 |
| if ( !strlen(mac) || !strlen(dev) ) |
0 |
1 |
1 |
|
| 36 |
0 |
| return -1; |
| 37 |
|
| |
| 38 |
|
| /* Set hardware address as unknown */ |
| 39 |
1 |
| strncpy(mac,"unknown", HEX_HW_ADDR_LEN); |
| 40 |
1 |
| mac[HEX_HW_ADDR_LEN-1] = '\0'; |
| 41 |
|
| |
| 42 |
|
| /* Copy device name into the ifreq strcture so that we can look for its |
| 43 |
|
| * hardware address through an ioctl request */ |
| 44 |
1 |
| strncpy(iface.ifr_name, dev, IFNAMSIZ); |
| 45 |
1 |
| iface.ifr_name[IFNAMSIZ-1] = '\0'; |
| 46 |
|
| |
| 47 |
|
| // Open a socket |
| 48 |
1 |
▶ | if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) |
0 |
1 |
1 |
|
| 49 |
|
| { |
| 50 |
0 |
| perror("socket"); |
| 51 |
0 |
| return -1; |
| 52 |
|
| } |
| 53 |
|
| else |
| 54 |
|
| { |
| 55 |
|
| // Get the interface hardware address |
| 56 |
1 |
▶ | if((ioctl(sock, SIOCGIFHWADDR, &iface)) < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if((ioctl(sock, SIOCGIFHWADDR, &iface)) < 0) |
1 |
0 |
1 |
|
| 57 |
|
| { |
| 58 |
1 |
| perror("ioctl SIOCGIFHWADDR"); |
| 59 |
1 |
| close(sock); |
| 60 |
1 |
| return -1; |
| 61 |
|
| } |
| 62 |
|
| else |
| 63 |
|
| { |
| 64 |
0 |
| sprintf(mac,"%02x:%02x:%02x:%02x:%02x:%02x", |
| 65 |
0 |
| iface.ifr_hwaddr.sa_data[0] & 0xff, |
| 66 |
0 |
| iface.ifr_hwaddr.sa_data[1] & 0xff, |
| 67 |
0 |
| iface.ifr_hwaddr.sa_data[2] & 0xff, |
| 68 |
0 |
| iface.ifr_hwaddr.sa_data[3] & 0xff, |
| 69 |
0 |
| iface.ifr_hwaddr.sa_data[4] & 0xff, |
| 70 |
0 |
| iface.ifr_hwaddr.sa_data[5] & 0xff); |
| 71 |
|
| } |
| 72 |
|
| } |
| 73 |
|
| |
| 74 |
0 |
| close(sock); |
| 75 |
0 |
| return 0; |
| 76 |
|
| } |