-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathifctrl.c
176 lines (128 loc) · 3.23 KB
/
ifctrl.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "ifctrl.h"
int set_ifname(struct ifreq* ifr, char* ifname){
size_t if_name_len = strlen(ifname);
if (if_name_len > sizeof(ifr->ifr_name)){
printf("Interface name is too long!\n");
return -1;
}
memcpy(ifr->ifr_name, ifname, if_name_len);
return 0;
}
int ifctrl_get_ifindex(char* ifname){
struct ifreq ifr;
int sock;
if (( sock = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ){
printf("socket error\n");
return -1;
}
if (set_ifname(&ifr, ifname) != 0) return -1;
if (ioctl(sock,SIOCGIFINDEX,&ifr)==-1) {
printf("%s",strerror(errno));
return -1;
}
return ifr.ifr_ifindex;
}
int ifctrl_get_ifflags(char* ifname, short* flags){
struct ifreq ifr;
if (set_ifname(&ifr, ifname) != 0) return -1;
int sock;
if (( sock = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ){
printf("socket error\n");
return -1;
}
if (ioctl(sock,SIOCGIFFLAGS,&ifr)==-1) {
printf("%s\n",strerror(errno));
return -1;
}
*flags = ifr.ifr_flags;
return 0;
}
int ifctrl_set_ifflags(char* ifname, short flags){
struct ifreq ifr;
if (set_ifname(&ifr, ifname) != 0) return -1;
ifr.ifr_flags = flags;
int sock;
if (( sock = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ){
printf("socket error\n");
return -1;
}
if (ioctl(sock,SIOCSIFFLAGS,&ifr)==-1) {
printf("%s\n",strerror(errno));
return -1;
}
return 0;
}
int ifctrl_is_up(char* ifname){
short flags;
if (ifctrl_get_ifflags(ifname, &flags) != 0){
return -1;
}
return flags & IFF_UP;
}
int ifctrl_get_wireless_mode(char* ifname){
struct iwreq ifr;
size_t if_name_len = strlen(ifname);
if (if_name_len > sizeof(ifr.ifr_name)){
printf("Interface name is too long!\n");
return -1;
}
memcpy(ifr.ifr_name, ifname, if_name_len);
int sock;
if (( sock = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ){
printf("socket error\n");
return -1;
}
if (ioctl(sock,SIOCGIWMODE,&ifr)==-1) {
printf("%s\n",strerror(errno));
return -1;
}
return ifr.u.mode;
}
int ifctrl_startstop(char* ifname, int on){
struct ifreq ifr;
if (set_ifname(&ifr, ifname) != 0) return -1;
short flags;
if (ifctrl_get_ifflags(ifname, &flags) != 0) return -1;
if(on){
flags = flags | IFF_UP;
printf("Starting device...\n");
} else {
flags = flags & !IFF_UP;
printf("Stopping device...\n");
}
if (ifctrl_set_ifflags(ifname, flags) != 0) return -1;
if (on) printf("Device started.\n"); else printf("Device stopped.\n");
return 0;
}
int ifctrl_set_wireless_mode(char* ifname, int mode){
struct iwreq ifr;
size_t if_name_len = strlen(ifname);
int sock;
if (( sock = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 ){
printf("socket error\n");
return -1;
}
if (ifctrl_startstop(ifname, 0) == -1) return -1;
if (if_name_len > sizeof(ifr.ifr_name)){
printf("Interface name is too long!\n");
return -1;
}
memcpy(ifr.ifr_name, ifname, if_name_len);
printf("Changing wireless mode...\n");
ifr.u.mode = mode;
if (ioctl(sock,SIOCSIWMODE,&ifr)==-1) {
printf("%s\n",strerror(errno));
return -1;
}
if (ifctrl_startstop(ifname, 1) == -1) return -1;
printf("Wireless mode changed.\n");
return 0;
}