-
Notifications
You must be signed in to change notification settings - Fork 39
/
macaddr.c
54 lines (44 loc) · 1.06 KB
/
macaddr.c
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
54
/*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "iwlib-private.h"
int main(int argc, char** argv)
{
int devsock;
struct ifreq ifbuffer;
char buf[20];
if ((argc != 2) || (argv[1][0] == '-')) {
printf("Usage: macaddr interface\n");
exit(1);
}
devsock = socket(AF_INET, SOCK_STREAM, 0);
if (devsock == -1) {
perror("Failed opening socket");
exit (1);
}
memset(&ifbuffer, 0, sizeof(ifbuffer));
strncpy(ifbuffer.ifr_name, argv[1], sizeof(ifbuffer.ifr_name));
if (ioctl(devsock, SIOCGIFHWADDR, &ifbuffer) == -1) {
fprintf(stderr, "There is no MACADDR for %s\n", argv[1]);
exit(1);
}
close(devsock);
puts(iw_saether_ntop(&ifbuffer.ifr_ifru.ifru_hwaddr, buf));
exit(0);
}