OSDN Git Service

8c5a93a299f5256d9afac49d06ae16aea794da4d
[android-x86/external-wireless-tools.git] / wireless_tools / macaddr.c
1 /*
2  *      macaddr
3  *
4  *      Program to return the MAC address of an Ethernet
5  *      adapter.  This was written to help configure the
6  *      adapter based on the MAC address rather than the
7  *      name.
8  *
9  *      Version 1.0     Eric Dittman    2001-10-19
10  *
11  *      This is released unther the GPL license.
12  */
13
14 #include <sys/ioctl.h>
15 #include <sys/types.h>
16 #include <sys/socket.h>
17 #include <linux/in.h>
18 #include <linux/socket.h>
19 #include <linux/if.h>
20
21 int main(int argc, char** argv) {
22
23         int devsock;
24         struct ifreq ifbuffer;
25         int i;
26
27         if (argc != 2) {
28                 printf("Usage: macaddr interface\n");
29                 exit(1);
30         }
31
32         devsock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
33
34         if (devsock == -1) {
35                 printf("Failed opening socket\n");
36                 exit (1);
37         }
38
39         memset(&ifbuffer, 0, sizeof(ifbuffer));
40         strcpy(ifbuffer.ifr_name, argv[1]);
41         if (ioctl(devsock, SIOCGIFHWADDR, &ifbuffer) == -1) {
42                 printf("There is no MACADDR for %s\n", argv[1]);
43                 exit(1);
44         }
45         close (devsock);
46
47         for (i = 0; i < IFHWADDRLEN; i++)
48                 printf("%02X", (unsigned char) ifbuffer.ifr_ifru.ifru_hwaddr.sa_data[i]);
49         printf("\n");
50
51         exit(0);
52
53 }