Created
July 29, 2013 05:42
-
-
Save rongyi/6102334 to your computer and use it in GitHub Desktop.
get arp type
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <unistd.h> | |
#include <linux/ethtool.h> | |
#include <sys/socket.h> | |
#include <sys/ioctl.h> | |
#include <linux/if.h> | |
#include <string.h> | |
#include <errno.h> | |
#include <net/if_arp.h> | |
static int arptype(const char *device) | |
{ | |
struct ifreq ifr; | |
memset(&ifr, 0, sizeof(ifr)); | |
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name)); | |
int fd = socket(PF_PACKET, SOCK_DGRAM, 0); | |
if (fd == -1) { | |
perror("get raw socket fail"); | |
return -1; | |
} | |
if (ioctl(fd, SIOCGIFHWADDR, &ifr) == -1) { | |
perror("ioctl SIOCGIFHWADDR fail"); | |
return -1; | |
} | |
return ifr.ifr_hwaddr.sa_family; | |
} | |
int main() | |
{ | |
const char *device = "eth0"; | |
int arp_type = arptype(device); | |
switch (arp_type) { | |
case ARPHRD_ETHER: | |
printf("device %s's arptype is ARPHRD_ETHER'\n", device); | |
break; | |
default: | |
printf("device %s's arptype is %d'\n", device, arp_type); | |
} | |
if (arp_type == -1) { | |
fprintf(stderr, "fail to get arp type\n"); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment