/
route_manager.cpp
505 lines (406 loc) · 11.6 KB
/
route_manager.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
// #include <iostream>
#include "route_manager.h"
void* maintain_thread(void*);
void* cellinfo_thread(void*);
CRoute_manager::CRoute_manager()
{
readconf("./conf.json");
if( (maintain_servfd = create_bind_socket("8080")) == -1 )
exit(-1);
if(listen(maintain_servfd, 10) == -1)
exit(-1);
if( (info_fd = create_bind_socket("9090")) == -1 )
exit(-1);
if(make_socket_non_blocking(info_fd) == -1)
exit(1);
if(listen(info_fd, SOMAXCONN) == -1)
exit(-1);
if( pthread_create(&mt_thread_id,NULL,maintain_thread,(void*)this) != 0 )
exit(-1);
if( pthread_create(&info_thread_id,NULL,cellinfo_thread,(void*)this) != 0 )
exit(-1);
// if( pthread_create(&thread_id,NULL,request_cellinfo_thread,(void*)this) != 0 )
// exit(-1);
}
CRoute_manager::~CRoute_manager()
{
}
void CRoute_manager::readconf(const char* filepath)
{
Json::Reader reader;
Json::Value root;
ifstream is;
is.open(filepath,ios::binary);
if(reader.parse(is,root))
{
int set_num = root["cell_addr"].size();
for (int i = 0 ; i < set_num ; i++)
{
ipset_t tmp;
tmp.lower_bounds = root["cell_addr"][i]["lower"].asInt();
tmp.upper_bounds = root["cell_addr"][i]["upper"].asInt();
tmp.ip1 = ip_str2num( root["cell_addr"][i]["IP1"].asString() );
tmp.ip2 = ip_str2num( root["cell_addr"][i]["IP2"].asString() );
tmp.ip3 = ip_str2num( root["cell_addr"][i]["IP3"].asString() );
if (tmp.lower_bounds > tmp.upper_bounds)
{
tmp.lower_bounds = tmp.upper_bounds;
}
_route_table.push_back(tmp);
}
access_iplist[0] = root["Access_ip"].asString();
access_portlist[0] = root["Access_port"].asInt();
}
is.close();
}
vector<ipset_t>::iterator CRoute_manager::search_bounds(const string& ip, unsigned int& lower,unsigned int& upper)
{
unsigned int ipnum;
ipnum = ip_str2num(ip);
vector<ipset_t>::iterator it;
for( it = _route_table.begin(); it != _route_table.end(); it++)
{
if ( (*it).ip1 == ipnum || (*it).ip2 == ipnum || (*it).ip3 == ipnum )
{
lower = (*it).lower_bounds;
upper = (*it).upper_bounds;
return it;
}
}
}
int CRoute_manager::parse_moverequest(Json::Value root)
{
memset(celladdr_buf,0,sizeof(celladdr_buf));
string tmp;
tmp = root["old_ip"]["IP1"].asString();
celladdr_buf[0].sin_family = AF_INET;
celladdr_buf[0].sin_port = htons(CELLPORT);
inet_pton(AF_INET, tmp.c_str(), &celladdr_buf[0].sin_addr);
tmp = root["old_ip"]["IP2"].asString();
celladdr_buf[1].sin_family = AF_INET;
celladdr_buf[1].sin_port = htons(CELLPORT);
inet_pton(AF_INET, tmp.c_str(), &celladdr_buf[1].sin_addr);
tmp = root["old_ip"]["IP3"].asString();
celladdr_buf[2].sin_family = AF_INET;
celladdr_buf[2].sin_port = htons(CELLPORT);
inet_pton(AF_INET, tmp.c_str(), &celladdr_buf[2].sin_addr);
if( (flag = root["flag"].asInt() ) == 1) //表示扩容
{
unsigned int lower,upper;
from_set = search_bounds(tmp,lower,upper);
range_mid = (lower + upper) / 2;
}
else if( (flag = root["flag"].asInt() ) == -1) //表示缩容
{
unsigned int lower,upper;
from_set = search_bounds(tmp,lower,upper);
to_set = search_bounds(root["new_ip"]["IP1"].asString(),lower,upper);
range_mid = (*from_set).lower_bounds;
}
move_req[0].new_ip = ip_str2num( root["new_ip"]["IP1"].asString() );
move_req[1].new_ip = ip_str2num( root["new_ip"]["IP2"].asString() );
move_req[2].new_ip = ip_str2num( root["new_ip"]["IP3"].asString() );
move_req[0].new_range = range_mid;
move_req[1].new_range = range_mid;
move_req[2].new_range = range_mid;
return 0;
}
bool CRoute_manager::do_datamove_request()
{
bool all_done = false;
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
char buf[20];
int t;
int success_times = 0;
connect(sockfd, (struct sockaddr*)&celladdr_buf[0] , sizeof(celladdr_buf[0]));
if( write(sockfd, (void*)&move_req[0], sizeof(move_req[0])) <= 0 )
{
perror("write error!");
close(sockfd);
}
if (read(sockfd,buf,20) <= 0)
{
perror("read error!");
close(sockfd);
}
memcpy(&t, buf, sizeof(t));
if( t == 1)
success_times++;
close(sockfd);
///****************************************************///
connect(sockfd, (struct sockaddr*)&celladdr_buf[1] , sizeof(celladdr_buf[1]));
if( write(sockfd, (void*)&move_req[1], sizeof(move_req[1])) <= 0 )
{
perror("write error!");
close(sockfd);
}
if (read(sockfd,buf,20) <= 0)
{
perror("read error!");
close(sockfd);
}
memcpy(&t, buf, sizeof (t));
if( t == 1)
success_times++;
close(sockfd);
///****************************************************///
connect(sockfd, (struct sockaddr*)&celladdr_buf[2] , sizeof(celladdr_buf[2]));
if( write(sockfd, (void*)&move_req[2], sizeof(move_req[2])) <= 0 )
{
perror("write error!");
close(sockfd);
}
if (read(sockfd,buf,20) <= 0)
{
perror("read error!");
close(sockfd);
}
memcpy(&t, buf, sizeof (t));
if( t == 1)
success_times++;
close(sockfd);
///****************************************************///
if(success_times == 3)
all_done = true;
if(all_done == true)
{
if( flag == 1)
{
ipset_t new_set;
new_set.ip1 = move_req[0].new_ip;
new_set.ip2 = move_req[1].new_ip;
new_set.ip3 = move_req[2].new_ip;
new_set.lower_bounds = range_mid;
new_set.upper_bounds = (*from_set).upper_bounds;
(*from_set).upper_bounds = range_mid;
add_routetable(new_set);
}
else if(flag == -1)
{
(*to_set).upper_bounds = (*from_set).upper_bounds;
del_routetable(from_set);
}
}
return all_done;
}
void CRoute_manager::add_routetable(ipset_t new_set)
{
_route_table.push_back(new_set);
}
void CRoute_manager::del_routetable(vector<ipset_t>::iterator it)
{
_route_table.erase(it);
}
int CRoute_manager::send_table2access(string& access_ip)
{
int sockfd = socket(AF_INET, SOCK_STREAM, 0);
sockaddr_in access_addr;
access_addr.sin_family = AF_INET;
access_addr.sin_port = htons(access_portlist[0]);
inet_pton(AF_INET, access_ip.c_str(), &access_addr.sin_addr);
connect(sockfd, (struct sockaddr*)&access_addr, sizeof (access_addr));
ipset_t* access_buf = new ipset_t[_route_table.size()];
ipset_t* p = access_buf;
for(unsigned int i = 0; i < _route_table.size(); i++)
{
p[i] = _route_table[i];
p++;
}
int ret = write(sockfd,(void*)access_buf, sizeof(access_buf));
if(ret == -1)
{
perror("write access error!");
return -1;
}
close(sockfd);
return 0;
}
string CRoute_manager::prepare_infodata() // 和运维采用json传递数据
{
vector<ipset_t>::iterator it;
Json::Value root;
for( it = _route_table.begin(); it != _route_table.end() ; it++)
{
Json::Value set;
set["IP1"] = Json::Value( ip_num2str((*it).ip1) );
set["IP2"] = Json::Value( ip_num2str((*it).ip2) );
set["IP3"] = Json::Value( ip_num2str((*it).ip3) );
set["lower"] = Json::Value( (*it).lower_bounds );
set["uppper"] = Json::Value( (*it).upper_bounds );
root["cell_addr"].append(set);
}
Json::FastWriter writer;
string jsonstr = writer.write(root);
return jsonstr;
}
// 接收运维请求
void* maintain_thread(void *p)
{
// pthread_detach(pthread_self());
cout<<"enter maintain thread"<<endl;
CRoute_manager* pCRoute = (CRoute_manager*)p;
struct sockaddr in_addr;
socklen_t in_len;
int infd;
in_len = sizeof (in_addr);
while(1)
{
infd = accept (pCRoute->maintain_servfd, &in_addr, &in_len);
if(infd == -1)
{
perror("accept error!\n");
continue;
}
int cnt = read(infd, pCRoute->mt_recvbuf, BUFLEN);
if(cnt < 0)
{
perror("read error!");
exit(-1);
}
else if(cnt == 0) // thd other side closed
{
close(infd);
continue;
}
Json::Reader reader;
Json::Value root;
if(reader.parse(pCRoute->mt_recvbuf,root))
{
if(root["Act"].asString() == string("Update"))
{
pCRoute->parse_moverequest(root);
if( pCRoute->do_datamove_request() )
{
pCRoute->send_table2access(pCRoute->access_iplist[0]);
}
}
if(root["Act"].asString() == string("Request"))
{
string str = pCRoute->prepare_infodata();
strcpy(pCRoute->stat_sendbuf,str.c_str());
write(infd, pCRoute->stat_sendbuf, strlen(str.c_str()));
}
}
}
}
// 可能作为服务器接收Cell请求
void * cellinfo_thread(void* p)
{
CRoute_manager* pCRoute = (CRoute_manager*)p;
struct epoll_event event;
struct epoll_event* events;
int efd;
efd = epoll_create(20);
event.data.fd = pCRoute->info_fd;
event.events = EPOLLIN | EPOLLET; //设置成边沿触发
if(epoll_ctl(efd,EPOLL_CTL_ADD,pCRoute->info_fd,&event) == -1)
exit(-1);
events = (epoll_event*)calloc(EVENTSNUM, sizeof(epoll_event));
while(1)
{
int n = epoll_wait(efd,events,EVENTSNUM,-1);
for(int i = 0; i < n; i++)
{
if((events[i].events & EPOLLERR) ||
(events[i].events & EPOLLHUP) ||
(!(events[i].events & EPOLLIN)))
{
perror("epoll error!");
close(events[i].data.fd);
continue;
}
else if(events[i].data.fd == pCRoute->info_fd)
{
while(1)
{
struct sockaddr in_addr;
socklen_t in_len;
int infd;
in_len = sizeof(in_addr);
infd = accept (pCRoute->info_fd, &in_addr, &in_len);
if (infd == -1)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
{
/* We have processed all incoming
connections. */
break;
}
else
{
perror ("accept");
break;
}
}
if(make_socket_non_blocking (infd) == -1)
exit(-1);
event.data.fd = infd;
event.events = EPOLLIN | EPOLLET;
if(epoll_ctl(efd,EPOLL_CTL_ADD,infd,&event) == -1)
exit(-1);
}
continue;
}
else if(events[i].events & EPOLLIN)
{
int n = 0;
int nread;
int fd = events[i].data.fd;
while ((nread = read(fd, pCRoute->infobuf + n, BUFLEN)) > 0)
{
n += nread;
}
if (nread == -1 && errno != EAGAIN)
{
perror("read error");
continue;
}
if (nread == 0)
{
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
epoll_ctl(efd,EPOLL_CTL_DEL,fd,&event);
close(fd);
continue;
}
event.data.fd = fd;
event.events = EPOLLOUT | EPOLLET;
epoll_ctl(efd,EPOLL_CTL_MOD,fd,&event);
}
else if(events[i].events & EPOLLOUT)
{
int nwrite, data_size = strlen(pCRoute->infobuf);
int n = data_size;
int fd = events[i].data.fd;
while (n > 0)
{
nwrite = write(fd, pCRoute->infobuf + data_size - n, n);
if (nwrite < n)
{
if (nwrite == -1 && errno != EAGAIN)
{
perror("write error");
break;
}
}
if(nwrite == 0) // the other side closed
{
event.data.fd = fd;
event.events = EPOLLOUT | EPOLLET;
epoll_ctl(efd,EPOLL_CTL_DEL,fd,&event);
close(fd);
break;
}
n -= nwrite;
}
// 重新监听EPOLLIN事件
if(n == 0) //
{
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
epoll_ctl(efd,EPOLL_CTL_MOD,fd,&event);
}
}
}
}
}