File: macaddr.c

package info (click to toggle)
wireless-tools 23-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 356 kB
  • ctags: 924
  • sloc: ansic: 4,391; makefile: 82; sh: 69
file content (53 lines) | stat: -rw-r--r-- 1,059 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/*
 *	macaddr
 *
 *	Program to return the MAC address of an Ethernet
 *	adapter.  This was written to help configure the
 *	adapter based on the MAC address rather than the
 *	name.
 *
 *	Version 1.0	Eric Dittman	2001-10-19
 *
 *	This is released unther the GPL license.
 */

#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/in.h>
#include <linux/socket.h>
#include <linux/if.h>

int main(int argc, char** argv) {

	int devsock;
	struct ifreq ifbuffer;
	int i;

	if (argc != 2) {
		printf("Usage: macaddr interface\n");
		exit(1);
	}

	devsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);

	if (devsock == -1) {
		printf("Failed opening socket\n");
		exit (1);
	}

	memset(&ifbuffer, 0, sizeof(ifbuffer));
	strcpy(ifbuffer.ifr_name, argv[1]);
	if (ioctl(devsock, SIOCGIFHWADDR, &ifbuffer) == -1) {
		printf("There is no MACADDR for %s\n", argv[1]);
		exit(1);
	}
	close (devsock);

	for (i = 0; i < IFHWADDRLEN; i++)
		printf("%02X", (unsigned char) ifbuffer.ifr_ifru.ifru_hwaddr.sa_data[i]);
	printf("\n");

	exit(0);

}