-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
89 lines (80 loc) · 2.62 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
#include "shared.h"
#include "common.hpp"
#include <errno.h>
MessageFillResult Message::fillFrom(int fd) {
auto ret = recv(fd, &this->length, sizeof(this->length), MSG_DONTWAIT);
if (ret == 0)
return MessageFillResult::ControllerClosed;
if (ret == -1) {
if (errno == EWOULDBLOCK || errno == EAGAIN)
return MessageFillResult::NoMessage;
return MessageFillResult::ControllerClosed;
}
if (ret > 0) {
auto acc = ret;
while (acc < sizeof(this->length)) {
auto tmp = recv(fd,
reinterpret_cast<char*>(&this->length) + acc,
sizeof(this->length) - acc, 0);
if (tmp <= 0)
return MessageFillResult::ControllerClosed;
acc += tmp;
}
this->length = ntohl(this->length);
while (acc < this->length) {
auto tmp = recv(fd,
reinterpret_cast<char*>(&this->length) + acc,
this->length - acc, 0);
if (tmp <= 0)
return MessageFillResult::ControllerClosed;
acc += tmp;
}
return MessageFillResult::Success;
}
assert(0 && "Unkonwn message error");
return MessageFillResult::Success;
}
class {
public:
void init() {
fd = socket(AF_INET, SOCK_STREAM, 0);
assert(fd > 0 && "Allocate socket to controller failed");
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = htons(23233);
addr.sin_addr.s_addr = inet_addr("10.1.0.1");
assert(connect(fd, reinterpret_cast<sockaddr*>(&addr), sizeof(sockaddr)) == 0 && "Failed to connect to controller");
}
bool handleRPC() {
Message msg;
switch (msg.fillFrom(fd)) {
case MessageFillResult::Success:
switch (msg.type) {
case MessageType::AddRule:
userAddLPMRule(msg.addr, msg.cidr, msg.dst_port);
break;
case MessageType::DelRule:
userDelLPMRule(msg.addr, msg.cidr);
break;
}
break;
case MessageFillResult::NoMessage:
return true;
case MessageFillResult::ControllerClosed:
return false;
}
assert(0 && "Unkonwn error");
return false;
}
private:
int fd = 0;
} controller;
int main(int argc, char **argv) {
/** Detect the controller interface **/
controller.init();
/** Main Logic **/
userInit(argc, argv);
while (controller.handleRPC())
userLoop();
return 0;
}