C/ethtool physid

From Attie's Wiki
Revision as of 11:44, 21 January 2013 by Attie (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The following is equivalent to running ethtool -p eth0.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <asm/types.h>
 
/* -- the following is taken from ethtool/ethtool-copy.h -- */
struct ethtool_value {
	__u32 cmd;
	__u32 data;
};
#define ETHTOOL_PHYS_ID            0x0000001c /* identify the NIC */
 
/* -- the following is taken from ethtool/internal.h -- */
#define SIOCETHTOOL                0x8946
 
int main(void) {
	int fd;
	struct ethtool_value edata;
	struct ifreq ifr;
 
	if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
		perror("socket()");
		return 1;
	}
 
	memset(&edata, 0, sizeof(edata));
	edata.cmd = ETHTOOL_PHYS_ID;
	edata.data = 0; // phys_id_time
 
	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, "eth0");
	ifr.ifr_data = (void*)&edata;
 
	if (ioctl(fd, SIOCETHTOOL, &ifr) < 0) {
		perror("ioctl()");
		return 1;
	}
 
	return 0;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox