-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnf-hishape-user.cc
227 lines (214 loc) · 7.68 KB
/
nf-hishape-user.cc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
* Copyright 2007-2008 Deutsches Forschungszentrum fuer Kuenstliche Intelligenz
* or its licensors, as applicable.
*
* You may not use this file except under the terms of the accompanying license.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Project: nf-HiShape
* File: nf-hishape-user.cc
* Purpose: userland tool for the nf-HiShape kernel module
* Responsible: Markus Goldstein
* Primary Repository: https://github.com/Markus-Go/nf-hishape
* Web Sites: www.madm.dfki.de, www.goldiges.de
*/
#include "nf-hishape_util.h"
void printUsage(int argc, char *argv[]);
long getLong(char* optarg);
void test(char* optarg);
int main(int argc,char *argv[]){
if(argc==1){
printUsage(argc, argv);
return EXIT_FAILURE;
}
static struct option long_options[] = {
{"list", no_argument, 0, 'L'},
{"flush", no_argument, 0, 'F'},
{"set", required_argument, 0, 'S'},
{"interface", required_argument, 0, 'i'},
{"any_device", no_argument, 0, 'a'},
{"print_interface", no_argument, 0, 'p'},
{"limit", required_argument, 0, 'l'},
{"from", required_argument, 0, 'f'},
{"to", required_argument, 0, 't'},
{"help", no_argument, 0, 'h'},
{"test", required_argument, 0, 'x'},
{0, 0, 0, 0}
};
char device[MAX_DEVICE_LENGTH];
int c = 0;
list<HiShapeRange> list_from_file;
HiShapeRange hs;
int n;
int handle;
int option_index = 0;
struct in_addr from, to;
int limit;
bool fromSet = false;
bool toSet = false;
bool limitSet = false;
while ((c = getopt_long(argc, argv, "LFiahpS:l:f:t:x:",long_options, &option_index)) != -1){
switch(c){
// -- list the ranges --
case 'L':
print_elements(getHiShapeRanges());
break;
// -- flush the ranges --
case 'F':
setHiShapeRanges(list_from_file);
break;
// -- set the ranges --
case 'S':
FILE *fp;
if((fp=fopen(optarg,"r"))==0){
perror("Could not open file");
break;
}
fscanf(fp,"%d",&n);
// printf("n = %d",n);
for(int j=0;j<n;j++){
fscanf(fp,"%d %d %d",&hs.from,&hs.to,&hs.limit);
list_from_file.push_back(hs);
}
n = setHiShapeRanges(list_from_file);
//print_elements(list_from_file);
fclose(fp);
break;
// -- set the interface --
case 'i':
handle = open(DEVICE_FILE_NAME, 0);
if(handle < 0) {
perror("could not open device");
} else {
if ((errno = ioctl(handle, IOCTL_HISHAPE_SET_DEVICE, optarg)) < 0) {
perror("IOCTL_HISHAPE_SET_DEVICE failed");
}
close(handle);
}
break;
// -- unset the interface (set to 'any') --
case 'a':
handle = open(DEVICE_FILE_NAME, 0);
if(handle < 0) {
perror("could not open device");
} else {
if ((errno = ioctl(handle, IOCTL_HISHAPE_SET_DEVICE, NULL)) < 0) {
perror("IOCTL_HISHAPE_SET_DEVICE failed");
}
close(handle);
}
break;
// -- print the interface --
case 'p':
handle = open(DEVICE_FILE_NAME, 0);
if(handle < 0) {
perror("could not open device");
} else {
if ((errno = ioctl(handle, IOCTL_HISHAPE_GET_DEVICE, device)) < 0) {
perror("IOCTL_HISHAPE_GET_DEVICE failed");
} else {
printf("interface is %s\n", device[0]!='\0'?device:"any device");
}
close(handle);
}
break;
// -- parse 'from-ip' --
case 'f':
if(!inet_aton(optarg, &from)) {
from.s_addr = getLong(optarg);
}
fromSet = true;
break;
// -- parse 'to-ip' --
case 't':
if(!inet_aton(optarg, &to)) {
to.s_addr = getLong(optarg);
}
toSet = true;
break;
// -- parse 'limit' --
case 'l':
limit = getLong(optarg);
limitSet = true;
break;
// -- print usage --
case 'h':
printUsage(argc, argv);
break;
case 'x':
test(optarg);
break;
}
}
// -- adding a new range if possible (no overlap) --
if(fromSet && toSet && limitSet) {
list<HiShapeRange> ranges = getHiShapeRanges();
HiShapeRange newRange;
newRange.from = (uint32_t)from.s_addr;
newRange.to = (uint32_t)to.s_addr;
newRange.limit = limit;
ranges.push_back(newRange);
setHiShapeRanges(ranges);
}
return EXIT_SUCCESS;
}
/**
* parses a string for a non-negativ number. exits the programm on parsing error
* or if the number is smaller than zero
* @param optarg the string to be parsed
* @return the number
*/
long getLong(char* optarg) {
char* endptr;
errno = 0;
long dummy = strtol(optarg, &endptr, 10);
if(errno != 0 || endptr == optarg || dummy < 0) {
fprintf(stderr, "%s is not valid\n", optarg);
exit(EXIT_FAILURE);
} else {
return dummy;
}
}
void printUsage(int argc, char *argv[]) {
printf("Usage: %s [OPTION...]\n\n", argv[0]);
printf(" Options:\n\n");
printf(" -L, --list List the ranges\n");
printf(" -F, --flush Flush the ranges\n");
printf(" -S, --set=FILE Load ranges from FILE\n");
printf(" -i, --interface=DEVICE Set interface to DEVICE\n");
printf(" -a, --any_device Unset interface\n");
printf(" -p, --print_interface Print interface\n");
printf(" -f, --from Start ip address for a new range (integer or dotted notation)\n");
printf(" -t, --to End ip address for a new range (integer or dotted notation)\n");
printf(" -l, --limit Limit of the new range in kbyte/s\n");
printf(" -h, --help Print this message and exit\n");
printf("\n");
}
void test(char* optarg) {
int handle = open(DEVICE_FILE_NAME, 0);
if(handle < 0) {
perror("could not open device");
} else {
long n = atol(optarg);
if ((errno = ioctl(handle, IOCTL_HISHAPE_RESERVE, n)) != SUCCESS) {
perror("IOCTL_HISHAPE_RESERVE failed");
} else {
HiShapeRange newRange;
for(uint32_t i=0; i<n; i++) {
newRange.from = i << 16;
newRange.to = (i << 16) + 255 * 256 + 255;
newRange.limit = i%2?500:100;
if ((errno=ioctl(handle, IOCTL_HISHAPE_ADD, &newRange)) != SUCCESS) {
perror("IOCTL_HISHAPE_ADD failed");
break;
}
}
}
close(handle);
}
}