OSDN Git Service

v24
[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 <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <sys/ioctl.h>
19 #include <sys/types.h>
20 #include <sys/socket.h>
21
22 #include "iwlib.h"
23
24 int main(int argc, char** argv)
25 {
26
27         int devsock;
28         struct ifreq ifbuffer;
29
30         if ((argc != 2) || (argv[1][0] == '-')) {
31                 printf("Usage: macaddr interface\n");
32                 exit(1);
33         }
34
35         devsock = socket(AF_INET, SOCK_STREAM, 0);
36
37         if (devsock == -1) {
38                 perror("Failed opening socket");
39                 exit (1);
40         }
41
42         memset(&ifbuffer, 0, sizeof(ifbuffer));
43         strncpy(ifbuffer.ifr_name, argv[1], sizeof(ifbuffer.ifr_name));
44         if (ioctl(devsock, SIOCGIFHWADDR, &ifbuffer) == -1) {
45                 fprintf(stderr, "There is no MACADDR for %s\n", argv[1]);
46                 exit(1);
47         }
48         close(devsock);
49
50         puts(iw_ether_ntoa((struct ether_addr *) ifbuffer.ifr_ifru.ifru_hwaddr.sa_data));
51
52         exit(0);
53 }