-
Notifications
You must be signed in to change notification settings - Fork 2
/
netsnoop.cc
204 lines (187 loc) · 5.96 KB
/
netsnoop.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
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
#include <iostream>
#include <functional>
#include <thread>
#include <condition_variable>
#include "netsnoop.h"
#include "net_snoop_client.h"
#include "net_snoop_server.h"
void StartClient();
void StartServer();
auto g_option = std::make_shared<Option>();
/**
* @brief usage:
* start server: netsnoop -s 0.0.0.0 4000 -vv
* start client: netsnoop -c 127.0.0.1 4000 -vv
*
*/
int main(int argc, char *argv[])
{
if (argc < 2 || !strcmp(argv[1], "-h"))
{
std::cout << "usage: \n"
" netsnoop -s <local ip> 4000 (start server)\n"
" netsnoop -c <server ip> 4000 (start client)\n"
" --------\n"
" command:\n"
" ping count 10 (test delay)\n"
" send count 1000 (test unicast)\n"
" send count 1000 multicast true (test multicast)\n"
" send speed 500 time 3000 (test unicast)\n"
" \n"
" version: "
<< VERSION(v) << " (" << __DATE__ << " " << __TIME__ << ")" << std::endl;
return 0;
}
SockInit init;
#ifdef _DEBUG
Logger::SetGlobalLogLevel(LLDEBUG);
#else
Logger::SetGlobalLogLevel(LLERROR);
#endif // _DEBUG
strncpy(g_option->ip_remote, "127.0.0.1", sizeof(g_option->ip_remote) - 1);
strncpy(g_option->ip_local, "0.0.0.0", sizeof(g_option->ip_local) - 1);
strncpy(g_option->ip_multicast, "239.3.3.3", sizeof(g_option->ip_multicast) - 1);
g_option->port = 4000;
if (argc > 2)
{
strncpy(g_option->ip_remote, argv[2], sizeof(g_option->ip_remote) - 1);
strncpy(g_option->ip_local, argv[2], sizeof(g_option->ip_local) - 1);
}
if (argc > 3)
{
g_option->port = atoi(argv[3]);
}
if (argc > 4)
{
Logger::SetGlobalLogLevel(LogLevel(LLERROR - strlen(argv[4]) + 1));
}
if (argc > 1)
{
if (!strcmp(argv[1], "-s"))
{
StartServer();
}
else if (!strcmp(argv[1], "-c"))
{
StartClient();
}
}
return 0;
}
void StartClient()
{
NetSnoopClient client(g_option);
client.OnConnected = [] {
std::clog << "connect to " << g_option->ip_remote << ":" << g_option->port << " (" << g_option->ip_multicast << ")" << std::endl;
};
client.OnStopped = [](std::shared_ptr<Command> oldcommand, std::shared_ptr<NetStat> stat) {
std::cout << "peer finish: " << oldcommand->GetCmd() << " || " << (stat ? stat->ToString() : "NULL") << std::endl;
};
auto t = std::thread([&client]() {
LOGVP("client run.");
client.Run();
});
t.join();
}
void StartServer()
{
auto notify_thread = std::thread([] {
LOGVP("notify running...");
Udp multicast;
multicast.Initialize();
multicast.BindMulticastInterface(g_option->ip_local);
multicast.Connect("239.3.3.4", 4001);
//sleep to wait server start
usleep(100 * 1000);
while (true)
{
multicast.Send(g_option->ip_local, strlen(g_option->ip_local));
sleep(3);
}
});
notify_thread.detach();
int count = 0;
NetSnoopServer server(g_option);
server.OnPeerConnected = [&](const Peer *peer) {
count++;
std::clog << "peer connect(" << count << "): " << peer->GetCookie() << std::endl;
};
server.OnPeerDisconnected = [&](const Peer *peer) {
count--;
std::clog << "peer disconnect(" << count << "): " << peer->GetCookie() << std::endl;
};
server.OnPeerStopped = [&](const Peer *peer, std::shared_ptr<NetStat> netstat) {
std::clog << "peer stoped: (" << peer->GetCookie() << ") " << peer->GetCommand()->GetCmd().c_str()
<< " || " << (netstat ? netstat->ToString() : "NULL") << std::endl;
};
auto t = std::thread([&]() {
LOGVP("server running...");
server.Run();
});
t.detach();
std::mutex mtx;
std::condition_variable cv;
std::stringstream ss;
std::string key;
int value = 0;
std::string cmd;
while (true)
{
std::cout << "command:" << std::flush;
std::getline(std::cin, cmd);
if(std::cin.eof())
break;
if (cmd.empty())
continue;
#pragma region resolve script command
if(cmd.rfind("peers ",0)==0)
{
ss.str(cmd);
ss.seekg(0);
ss >> key >> value;
if(value<=0)
{
std::clog << "command format error: " << ss.str() << std::endl;
continue;
}
std::clog << "wait "<< value <<" peers." << std::endl;
while(count<value)
{
sleep(1);
}
std::clog << "connect "<< value <<" peers." << std::endl;
continue;
}
if(cmd.rfind("sleep ",0)==0)
{
ss.str(cmd);
ss.seekg(0);
ss >> key >> value;
if(value<=0)
{
std::clog << "command format error: " << ss.str() << std::endl;
continue;
}
std::clog << "sleep "<< value <<" seconds." << std::endl;
sleep(value);
continue;
}
#pragma endregion
auto command = CommandFactory::New(cmd);
if (!command)
{
std::clog << "command '" << cmd << "' is not supported." << std::endl;
}
else
{
command->RegisterCallback([&](const Command *oldcommand, std::shared_ptr<NetStat> stat) {
std::cout << "command finish: " << oldcommand->GetCmd() << " || " << (stat ? stat->ToString() : "NULL") << std::endl;
cv.notify_all();
});
server.PushCommand(command);
std::unique_lock<std::mutex> lock(mtx);
cv.wait(lock);
}
std::cout << std::endl;
}
}