Skip to content

Instantly share code, notes, and snippets.

@rongyi
Created July 29, 2013 05:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rongyi/6102334 to your computer and use it in GitHub Desktop.
Save rongyi/6102334 to your computer and use it in GitHub Desktop.
get arp type
#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