Annotated edit history of
SIOCSIFHWADDR version 2, including all changes.
View license author blame.
Rev |
Author |
# |
Line |
1 |
PerryLorier |
1 |
!!!__S__et __I__/__O__ __C__ontrol: __S__ocket __I__nter__f__ace __H__ard__w__are __Addr__ess |
|
|
2 |
|
|
|
3 |
Sets the interface hardware address. |
|
|
4 |
|
|
|
5 |
|
|
|
6 |
!!EXAMPLE |
2 |
AlexGordeev |
7 |
<verbatim> |
1 |
PerryLorier |
8 |
#include <sys/types.h> /* for socket(2) and related bits and pieces */ |
|
|
9 |
#include <sys/socket.h> /* for socket(2) */ |
|
|
10 |
#include <net/if.h> /* for struct ifreq */ |
|
|
11 |
#include <net/if_arp.h> /* for ARPHRD_ETHER */ |
|
|
12 |
#include <sys/ioctl.h> /* for IOCTL's */ |
|
|
13 |
#include <stdio.h> /* for fprintf etc */ |
|
|
14 |
#include <unistd.h> /* for close */ |
|
|
15 |
|
|
|
16 |
int main(int argc,char **argv) |
|
|
17 |
{ |
|
|
18 |
struct ifreq ifr; |
|
|
19 |
int skfd; |
|
|
20 |
|
|
|
21 |
if (argc<3) { |
|
|
22 |
fprintf(stderr,"usage:\n%s interface hwaddr\n",argv[1]); |
|
|
23 |
return 1; |
|
|
24 |
} |
|
|
25 |
|
|
|
26 |
/* Fill in the structure */ |
|
|
27 |
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", argv[1]); |
|
|
28 |
ifr.ifr_hwaddr.sa_family = ARPHRD_ETHER; |
|
|
29 |
/* TODO: write some code to parse argv[2] into ifr.ifr_hwaddr.sa_data */ |
|
|
30 |
/* memcpy(&ifr.ifr_hwaddr.sa_data, argv[2], sizeof(ether.address)); */ |
|
|
31 |
|
|
|
32 |
/* Create a socket fd */ |
|
|
33 |
skfd = socket(PF_INET,SOCK_STREAM,0); |
|
|
34 |
|
|
|
35 |
/* call the IOCTL */ |
|
|
36 |
if (ioctl(skfd, SIOCSIFHWADDR, &ifr) < 0) { |
|
|
37 |
perror("ioctl(SIOCSIFHWADDR)"); |
|
|
38 |
return 1; |
|
|
39 |
} |
|
|
40 |
|
|
|
41 |
/* cleanup */ |
|
|
42 |
close(skfd); |
|
|
43 |
|
|
|
44 |
/* we're out of here! */ |
|
|
45 |
return 0; |
|
|
46 |
} |
2 |
AlexGordeev |
47 |
</verbatim> |