-
Notifications
You must be signed in to change notification settings - Fork 6
/
nf-hishape_util.cc
147 lines (125 loc) · 4.25 KB
/
nf-hishape_util.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
/*
* 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_util.cc
* Purpose: library of the 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"
using namespace std;
bool compare_HiShapeRange(const HiShapeRange &a,const HiShapeRange &b) {
return a.from < b.from;
}
bool check_overlapping(list<HiShapeRange> l) {
bool overlap = false;
HiShapeRange tmp;
l.sort(compare_HiShapeRange);
iter i = l.begin();
while(i!=l.end()){
if((*i).to <= (*i).from){
overlap = true;
break;
}
tmp = *i;
if(++i==l.end()){
break;
}
if((*i).from <=tmp.to ){
overlap = true;
break;
}
}
return overlap;
}
inline void copy(HiShapeRange &dst, const HiShapeRange &source) {
dst.from = source.from;
dst.to = source.to;
dst.limit = source.limit;
return;
}
bool getHiShapeArray(HiShapeRanges *dst, list<HiShapeRange> src) {
if(check_overlapping(src)){ // does the list overlap? list ist sorted before beeing checked
cerr << "the source list overlaps \n";
return false;
}
dst->length = src.size();
uint32_t n = dst->length;
dst->ranges = (HiShapeRange*) malloc(n*sizeof(HiShapeRange));
unsigned int i = 0;
iter it = src.begin();
do{
copy(dst->ranges[i],(*it++));
}while(++i < n);
return true;
}
int setHiShapeRanges(list<HiShapeRange> ranges) {
HiShapeRanges dst;
if(getHiShapeArray(&dst,ranges)){
int h = open(DEVICE_FILE_NAME, 0);
if(h < 0) {
perror("could not open device");
return -1;
}
if ((errno = ioctl(h, IOCTL_HISHAPE_SET_RANGES, &dst)) < 0){
perror("IOCTL_HISHAPE_SET_RANGES failed");
close(h);
return -1;
}
close(h);
return 1;
}
cout << "IOCTL_HISHAPE_SET_RANGES failed, the list may be overlaping\n";
return -1;
}
list<HiShapeRange> getHiShapeRanges() {
list<HiShapeRange> list;
int h = open(DEVICE_FILE_NAME, 0);
if(h < 0) {
//perror("could not open device,the list returned is empty");
cerr << "could not open device,the list returned is empty\n";
return list;
}
HiShapeRanges dst;
dst.ranges = (HiShapeRange*)malloc(65535*sizeof(HiShapeRange));
dst.length = 65535;
if ((errno = ioctl(h, IOCTL_HISHAPE_GET_RANGES, &dst)) < 0){
//perror("IOCTL_HISHAPE_SET_RANGES failed, the returned list is empty");
perror("IOCTL_HISHAPE_SET_RANGES failed, the returned list is empty");
} else {
uint32_t i = 0;
for(i=0; i<dst.length; i++) {
list.push_back(dst.ranges[i]);
}
}
close(h);
return list;
}
//prints the list
void print_elements(list<HiShapeRange> list1) {
cout << " " << setw(15) << left << "from" << " | " << setw(15) << left << "to" << " | " << left << "limit" << endl;
cout << "----------------------------------------------\n";
char from[16];
char to[16];
struct in_addr ipFrom, ipTo;
for(iter it = list1.begin(); it != list1.end(); it++) {
ipFrom.s_addr = htonl((*it).from);
ipTo.s_addr = htonl((*it).to);
strcpy(from, inet_ntoa(ipFrom));
strcpy(to, inet_ntoa(ipTo));
// cout << setw(15) << left << (*it).from << " | " << setw(15) << left << (*it).to << " | " << setw(15) << left << (*it).limit <<endl;
cout << " " << setw(15) << left << from << " | " << setw(15) << left << to << " | " << left << (*it).limit << "kb/s" <<endl;
}
return;
}