-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
312 lines (295 loc) · 10.4 KB
/
main.cpp
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include <iostream>
#include <vector>
#include <map>
#include <chrono>
#include "antlr/antlr4-runtime.h"
#include "parser/NFCompilerLexer.h"
#include "parser/NFCompilerParser.h"
#include "parser/NFCompilerVisitor.h"
#include "symboltable.h"
#include "instructions.h"
#include "trace.h"
//#include "pkt.h"
using namespace std;
using namespace antlr4;
map<string, string> pkt_fields {
{"sip", "L3+96"},
{"dip", "L3+128"},
{"proto", "L3+72"},
{"sport", "L4+0"},
{"dport", "L4+16"},
{"flag_ack", "L4+107"},
{"flag_rst", "L4+109"},
{"flag_syn", "L4+110"},
{"flag_fin", "L4+111"}
};
void create_trace(NFCompilerVisitor visitor, int index, int np, int resubmit_id, bool tl) {
trace t(index+1, tl);
int resubmit_id_temp = 0;
vector<Symbol *> svt = visitor.ST.state_vars();
vector<Symbol *>::iterator its;
for (its = svt.begin(); its != svt.end(); its++) {
string stvar = (*its)->getName();
t.add_decl_node(stvar);
string stval = (*its)->getValue();
if(stval != "") {
try {
int stvalc = stoi(stval);
t.add_assign_in(stvar, stvalc);
} catch (const invalid_argument& ia) {
t.add_ct_node(stvar, "", stval, "=", 0);
}
}
}
vector<struct entry*>::iterator it;
for (it = visitor.entries.begin(); it != visitor.entries.end(); it++) {
vector<struct match_entry_flow*> mef = (*it)->m_f;
vector<struct match_entry_flow*>::iterator it2;
vector<struct match_entry_state*> mes = (*it)->m_s;
vector<struct match_entry_state*>::iterator it3;
vector<struct tracenode*> tmp1, tmp3;
struct action_flow *af = (*it)->a_f;
vector<struct action_state *> asv = ((*it)->a_s);
vector<struct action_state *>::iterator itas;
struct tracenode *t1, *t3;
for (it2 = mef.begin(); it2 != mef.end(); it2++) {
struct match_flow *mf_temp = (*it2)->mf;
string v = mf_temp->var;
if (visitor.ST.find(v) != NULL) {
vector<string> g = visitor.ST.getGranularitybyName(v);
string c = visitor.ST.getValuebyName(v);
int val = stoi(c);
if (mf_temp->match) {
t1 = t.new_assert_node(pkt_fields[g[0]], "==", val);
} else {
t1 = t.new_assert_node(pkt_fields[g[0]], "!=", val);
}
tmp1.push_back(t1);
} else if (visitor.CT.find(v) != NULL) {
if (mf_temp->match) {
t1 = t.new_ct_node(visitor.CT.getc1byName(v) + to_string(index+1), visitor.CT.getop1byName(v), visitor.CT.getc2byName(v), visitor.CT.getop2byName(v), visitor.CT.getcvalbyName(v));
tmp1.push_back(t1);
} else if (!mf_temp->match && visitor.CT.getop2byName(v) == "<=") {
t1 = t.new_ct_node(visitor.CT.getc1byName(v) + to_string(index+1), visitor.CT.getop1byName(v), visitor.CT.getc2byName(v), ">", visitor.CT.getcvalbyName(v));
tmp1.push_back(t1);
}
}
}
for (itas = asv.begin(); itas != asv.end(); itas++) {
struct action_state *as = (*itas);
if(as != NULL && as->state_var != "") {
string astval = as->state_val;
if (visitor.ST.find(astval) != NULL) {
string astc = visitor.ST.getValuebyName(astval);
int astn = stoi(astc);
struct tracenode *astvar = t.new_assert_node(as->state_var, "=", astn);
tmp3.push_back(astvar);
} else if (astval != "") {
try {
int astval_n = stoi(astval);
string astval_n_str = to_string(astval_n);
struct tracenode *astn = t.new_ct_node(as->state_var, "", astval_n_str, "=", 0);
tmp3.push_back(astn);
} catch (...) {
char plus = '+';
char mul = '*';
char minus = '-';
size_t plus_found = astval.find(plus);
size_t mul_found = astval.find(mul);
size_t minus_found = astval.find(minus);
if (plus_found != string::npos) {
int inc_val = 0;
auto start = 0U;
start = plus_found + 1;
plus_found = astval.find(plus, start);
inc_val = stoi(astval.substr(start, plus_found));
if(visitor.ST.find(as->state_var)) {
string astval_i = visitor.ST.getValuebyName(as->state_var);
try {
int astval_i1 = stoi(astval_i);
astval_i1 = astval_i1 + inc_val;
struct tracenode *astn = t.new_ct_node(as->state_var, "", to_string(astval_i1), "=", 0);
tmp3.push_back(astn);
} catch (...) {
}
}
} else if (minus_found != string::npos) {
int dec_val = 0;
auto start = 0U;
start = minus_found + 1;
minus_found = astval.find(minus, start);
dec_val = stoi(astval.substr(start, minus_found));
if(visitor.ST.find(as->state_var)) {
string astval_i = visitor.ST.getValuebyName(as->state_var);
try {
int astval_i1 = stoi(astval_i);
astval_i1 = astval_i1 - dec_val;
struct tracenode *astn = t.new_ct_node(as->state_var, "", to_string(astval_i1), "=", 0);
tmp3.push_back(astn);
} catch (...) {
}
}
} else if (mul_found != string::npos) {
string mul_val;
auto start = 0U;
start = mul_found + 1;
mul_found = astval.find(mul, start);
mul_val = astval.substr(start, mul_found);
if(visitor.ST.find(mul_val)) {
mul_val = visitor.ST.getValuebyName(mul_val);
}
if (visitor.ST.find(as->state_var)) {
string astval_i = visitor.ST.getValuebyName(as->state_var);
try {
double astval_i1 = (double)stoi(astval_i);
double mul_val_ = ::atof(mul_val.c_str());
astval_i1 = astval_i1 * mul_val_;
struct tracenode *astn = t.new_ct_node(as->state_var, "", to_string(astval_i1), "=", 0);
tmp3.push_back(astn);
} catch (...) {
}
}
} else {
string astval_n = astval + to_string(index+1);
struct tracenode *astn = t.new_ct_node(as->state_var, "", astval_n, "=", 0);
tmp3.push_back(astn);
}
}
}
}
}
if(af->action == "DROP")
t3 = t.new_assert_node("DROP", "", 0);
else if(af->action == "pass")
t3 = t.new_assert_node("pass", "", 0);
else if(af->action == "resubmit") {
//resubmit_id = index;
t3 = t.new_assert_node("resubmit", "", 0);
} else
t3 = NULL;
tmp3.push_back(t3);
for (it3 = mes.begin(); it3 != mes.end(); it3++) {
struct match_state *ms_temp = (*it3)->ms;
if(ms_temp != NULL && ms_temp->state_var != "") {
string stval = ms_temp->state_val;
if (visitor.ST.find(stval) != NULL) {
string stc = visitor.ST.getValuebyName(stval);
int stn = stoi(stc);
struct tracenode *tvar = t.new_assert_node(ms_temp->state_var, ms_temp->op, stn);
tmp1.push_back(tvar);
} else {
int stn = stoi(stval);
struct tracenode *tvar = t.new_assert_node(ms_temp->state_var, ms_temp->op, stn);
tmp1.push_back(tvar);
}
}
}
t.add_mlrite_nodes(tmp3, tmp1);
}
//auto total_start = chrono::high_resolution_clock::now();
//t.print_all_paths();
//auto start = chrono::high_resolution_clock::now();
t.execute();
//auto stop = chrono::high_resolution_clock::now();
//t.print_all_paths();
//auto total_stop = chrono::high_resolution_clock::now();
//auto total_duration = chrono::duration_cast<chrono::microseconds>(total_stop - total_start);
//auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
//cout << "time taken in z3 : " << duration.count() << " out of total " << total_duration.count() << endl;
vector<vector <struct tracenode *>> paths = t.return_all_paths();
vector<vector<struct tracenode *>>::iterator it_path;
for (it_path = paths.begin(); it_path != paths.end(); it_path++) {
resubmit_id_temp++;
if (resubmit_id > 0 && resubmit_id_temp == resubmit_id) {
vector<struct tracenode *> path = (*it_path);
cout << "PACKET: " << index+1 << endl;
t.print_path(path);
vector<struct tracenode *>::iterator it_node;
struct tracenode *last_node;
for (it_node = path.begin(); it_node != path.end(); it_node++) {
if((*it_node)->op == "=" || (*it_node)->decl == 2) {
if (visitor.ST.find((*it_node)->a)) {
if ((*it_node)->b == "") {
visitor.ST.modify((*it_node)->a, to_string((*it_node)->value));
} else {
//string new_val = (*it_node)->b + to_string(index+1);
string new_val = (*it_node)->b;
visitor.ST.modify((*it_node)->a, new_val);
}
}
}
if ((*it_node)->decl == 0)
last_node = (*it_node);
}
//cout << "before creat trace resubmit id " << resubmit_id << endl;
if (index < np-1 && last_node->a == "resubmit" ) {
cout << "resubmit id " << resubmit_id_temp << endl;
create_trace(visitor, index+1, np, resubmit_id_temp, tl);
} else if (index < np - 1) {
create_trace(visitor, index+1, np, 0, tl);
}
break;
} else {
vector<struct tracenode *> path = (*it_path);
cout << "PACKET: " << index+1 << endl;
t.print_path(path);
vector<struct tracenode *>::iterator it_node;
struct tracenode *last_node;
for (it_node = path.begin(); it_node != path.end(); it_node++) {
if((*it_node)->op == "=" || (*it_node)->decl == 2) {
if (visitor.ST.find((*it_node)->a)) {
if ((*it_node)->b == "") {
visitor.ST.modify((*it_node)->a, to_string((*it_node)->value));
} else {
//string new_val = (*it_node)->b + to_string(index+1);
string new_val = (*it_node)->b;
visitor.ST.modify((*it_node)->a, new_val);
}
}
}
if ((*it_node)->decl == 0)
last_node = (*it_node);
}
//cout << "before creat trace resubmit id " << resubmit_id << endl;
if (index < np-1 && last_node->a == "resubmit" ) {
cout << "resubmit id " << resubmit_id_temp << endl;
create_trace(visitor, index+1, np, resubmit_id_temp, tl);
} else if (index < np - 1) {
create_trace(visitor, index+1, np, 0, tl);
}
}
}
}
int main(int argc, char *argv[]) {
char *filename;
if (argc > 1) {
filename = argv[1];
} else {
cout << "input filename" << endl;
return 0;
}
ifstream stream(filename, ios::in);
ANTLRInputStream input(stream);
NFCompilerLexer lexer(&input);
CommonTokenStream tokens(&lexer);
NFCompilerParser parser(&tokens);
NFCompilerParser::ProgramContext *nf_name = parser.program();
//cout << "program name " << nf_name->IDENT()->getText() << endl;
NFCompilerVisitor visitor;
antlrcpp::Any v = visitor.visitProgram(nf_name);
//visitor.ST.printST();
//visitor.print_entries();
//visitor.CT.printCT();
int np;
cout << " No. of packets: ";
cin >> np;
bool tlogic;
cout << "Timer logic: ";
cin >> tlogic;
auto start = chrono::high_resolution_clock::now();
create_trace(visitor, 0, np, 0, tlogic);
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::microseconds>(stop - start);
cout << "time taken " << duration.count() << endl;
return 0;
}