-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswitch13list.cpp
More file actions
190 lines (159 loc) · 6.35 KB
/
switch13list.cpp
File metadata and controls
190 lines (159 loc) · 6.35 KB
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
#include "switch13list.h"
namespace pfswitch13{
/// Parse the json object given.
bool Switch13List::parse(std::list<vigil::json_object*> *connections,const std::string &as){
using namespace vigil;
using namespace vigil::json;
//std::vector<Switch13*> temp; // temp storage for the paths
Switch13List temp;
std::list<json_object*>::iterator pit=connections->begin(); // connection iterator
for(;pit!=connections->end();++pit){
ipv4_addr src_ip;
ipv4_addr dst_ip;
std::string name;
json_object *flow=get_dict_value(*pit,"flow"); // flow data
if(!flow || flow->type!=json_object::JSONT_DICT){
std::cerr<<"json error: flow\n";
clear();
return false;
}
json_object *ftmp=get_dict_value(flow,"src_ip");
if(!ftmp && ftmp->type!=json_object::JSONT_STRING){
std::cerr<<"json error: src_ip\n";
clear();
return false;
}
src_ip=ipaddr(*(char**)ftmp->object); //ipaddr can be converted to ipv4_addr
ftmp=get_dict_value(flow,"dst_ip");
if(!ftmp && ftmp->type!=json_object::JSONT_STRING){
std::cerr<<"json error: dst_ip\n";
clear();
return false;
}
dst_ip=ipaddr(*(char**)ftmp->object); //ipaddr can be converted to ipv4_addr
ftmp=get_dict_value(flow,"name");
if(!ftmp && ftmp->type!=json_object::JSONT_STRING){
std::cerr<<"json error: name\n";
clear();
return false;
}
name=std::string(*(char**)ftmp->object); //convert name
json_object *paths=get_dict_value(*pit,"paths"); // flow data
if(paths && paths->type==json_object::JSONT_ARRAY){
if(!temp.parse_paths(name,src_ip,dst_ip,as,
(std::list<vigil::json_object*> *)paths->object)){
temp.clear();
clear();
return false;
}
}
else{
std::cerr<<"json error: paths\n";
clear();
return false;
}
this->consume(temp);
}
return true;
}
bool Switch13List::parse_paths(const std::string &name,ipv4_addr src_ip,ipv4_addr dst_ip,const std::string &as,
std::list<vigil::json_object*> *paths){
using namespace vigil;
using namespace vigil::json;
size_t elements=paths->size();
if(!elements) return false; // empty paths!
PathHdr *hdr=new PathHdr[elements+1];
PathHdr *hdr_tmp=hdr;
unsigned in_port,dpid;
unsigned curr_path=0;
std::list<json_object*>::iterator pit=paths->begin(); // path iterator
for(;pit!=paths->end();++pit,curr_path++){
if((*pit)->type!=json_object::JSONT_DICT){
return false;
}
if(!parse_path(name,src_ip,dst_ip,as,
(vigil::json_object*)(*pit),hdr_tmp++,&dpid,&in_port)){
return false;
}
else if(curr_path==elements-1){
if(elements>1){
std::cerr<<"creating switch S4\n";
push_back(new SwitchS4((unsigned)dpid,name,src_ip,dst_ip,SwitchData(0,as,in_port,0),&hdr));
}
else{
std::cerr<<"creating switch S1\n";
push_back(new SwitchS1((unsigned)dpid,name,src_ip,dst_ip,SwitchData(hdr->pid,as,in_port,hdr->out_port)));
}
}
}
return true;
}
bool Switch13List::parse_path(const std::string &name,ipv4_addr src_ip,ipv4_addr dst_ip,const std::string &as,
vigil::json_object *path,
PathHdr *hdr,unsigned *idpid,unsigned *iport){
using namespace vigil;
using namespace vigil::json;
int pid=0;
int dpid=0;
int in_port=0;
int out_port=0;
json_object *ptmp=get_dict_value(path,"pid"); // path tmp
if(ptmp && ptmp->type==json_object::JSONT_INTEGER){
pid=*((int*)ptmp->object);
}
else{
std::cerr<<"json error: pid\n";
return false;
}
ptmp=get_dict_value(path,"actions");
if(!ptmp || ptmp->type!=json_object::JSONT_ARRAY){
std::cerr<<"json error: actions\n";
return false;
}
std::cerr<<"reading actions\n";
std::list<json_object*> *actions=(std::list<json_object*>*)ptmp->object;
std::list<json_object*>::iterator ait=actions->begin(); // actions iterator
size_t position=0,max_pos=actions->size();
for(;ait!=actions->end();++ait,position++){
json_object *atmp=get_dict_value(*ait,"dpid");
if(atmp && atmp->type==json_object::JSONT_INTEGER){
dpid=*((int*)atmp->object);
}
else{
std::cerr<<"json error: dpid\n";
return false;
}
atmp=get_dict_value(*ait,"in_port");
if(atmp && atmp->type==json_object::JSONT_INTEGER){
in_port=*((int*)atmp->object);
}
else{
std::cerr<<"json error: in_port\n";
return false;
}
atmp=get_dict_value(*ait,"out_port");
if(atmp && atmp->type==json_object::JSONT_INTEGER){
out_port=*((int*)atmp->object);
}
else{
std::cerr<<"json error: out_port\n";
return false;
}
if(position==0){
*idpid=dpid;
*iport=in_port;
hdr->pid=pid;
hdr->out_port=out_port;
}
else if(position==max_pos-1){ // last
push_back(new SwitchS3((unsigned)dpid,name,SwitchData(pid,as,in_port,out_port)));
std::cerr<<"creating switch S3\n";
}
else{
push_back(new SwitchS2((unsigned)dpid,name,SwitchData(pid,as,in_port,out_port)));
std::cerr<<"creating switch S2\n";
}
}
return true;
}
};