-
Notifications
You must be signed in to change notification settings - Fork 2
/
net_snoop_client.cc
271 lines (244 loc) · 7.97 KB
/
net_snoop_client.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
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
#include "context2.h"
#include "command.h"
#include "net_snoop_client.h"
int NetSnoopClient::Run()
{
int result;
char buf[100] = {0};
fd_set read_fds, write_fds;
FD_ZERO(&read_fds);
FD_ZERO(&write_fds);
context_ = std::make_shared<Context>();
auto context = context_;
if ((result = Connect()) != 0)
return result;
while (true)
{
memcpy(&read_fds, &context->read_fds, sizeof(read_fds));
memcpy(&write_fds, &context->write_fds, sizeof(write_fds));
#ifdef _DEBUG
for (int i = 0; i < context_->max_fd + 1; i++)
{
if (FD_ISSET(i, &read_fds))
{
LOGVP("want read: %d", i);
}
if (FD_ISSET(i, &write_fds))
{
LOGVP("want write: %d", i);
}
}
#endif
LOGVP("client[%d] selecting",control_sock_->GetFd());
result = select(context->max_fd + 1, &read_fds, &write_fds, NULL, NULL);
LOGVP("client[%d] selected",control_sock_->GetFd());
#ifdef _DEBUG
for (int i = 0; i < context_->max_fd + 1; i++)
{
if (FD_ISSET(i, &read_fds))
{
LOGVP("can read: %d", i);
}
if (FD_ISSET(i, &write_fds))
{
LOGVP("can write: %d", i);
}
}
#endif
if (result <= 0)
{
PSOCKETERROR("select error");
return -1;
}
if (FD_ISSET(data_sock_->GetFd(), &write_fds))
{
result = SendData();
ASSERT_RETURN(result>=0,-1);
}
if (FD_ISSET(data_sock_->GetFd(), &read_fds))
{
result = RecvData(data_sock_);
if(result<=0) LOGWP("data sock recv error.");
// in a long delay network(>100ms),we may recv a port unreachable ICMP packet.
//ASSERT_RETURN(result>0,-1);
}
if (FD_ISSET(multicast_sock_->GetFd(), &read_fds))
{
result = RecvData(multicast_sock_);
ASSERT_RETURN(result>0,-1);
}
if (FD_ISSET(control_sock_->GetFd(), &write_fds))
{
if ((result = SendCommand()) < 0)
{
LOGEP("client send cmd error.");
break;
}
}
if (FD_ISSET(control_sock_->GetFd(), &read_fds))
{
if ((result = RecvCommand()) == ERR_DEFAULT)
{
LOGEP("client recv cmd error.");
break;
}
else if(result == ERR_SOCKET_CLOSED)
{
LOGWP("client control socket closed.");
break;
}
}
}
return -1;
}
int NetSnoopClient::Connect()
{
int result;
control_sock_ = std::make_shared<Tcp>();
result = control_sock_->Initialize();
ASSERT(result > 0);
#ifndef WIN32
struct timeval timeout;
// wait up to 10 senconds for connect()
timeout.tv_sec = 10;
timeout.tv_usec = 0;
result = setsockopt(control_sock_->GetFd(), SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
ASSERT(result>=0);
#endif // WIN32
result = control_sock_->Connect(option_->ip_remote, option_->port);
if(result<0)
{
LOGEP("connect to %s:%d error.",option_->ip_remote,option_->port);
return -1;
}
#ifndef WIN32
timeout.tv_sec = 0;
timeout.tv_usec = 0;
result = setsockopt(control_sock_->GetFd(), SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout));
ASSERT(result>=0);
#endif // WIN32
data_sock_ = std::make_shared<Udp>();
result = data_sock_->Initialize();
result = data_sock_->Connect(option_->ip_remote, option_->port);
ASSERT_RETURN(result >= 0,-1,"data socket connect server error.");
std::string ip_local;
int port_local;
result = data_sock_->GetLocalAddress(ip_local, port_local);
ASSERT(result >= 0);
multicast_sock_ = std::make_shared<Udp>();
result = multicast_sock_->Initialize();
result = multicast_sock_->Bind("0.0.0.0",option_->port);
ASSERT_RETURN(result >= 0,-1,"multicast socket bind error: %s:%d",ip_local.c_str(),option_->port);
result = multicast_sock_->JoinMUlticastGroup(option_->ip_multicast,ip_local);
ASSERT_RETURN(result>=0,-1);
// only recv the target's multicast packets,windows can not do this
// result = multicast_sock_->Connect(option_->ip_remote, option_->port);
// ASSERT_RETURN(result >= 0,-1,"multicast socket connect server error.");
cookie_ = "cookie:" + ip_local + ":" + std::to_string(port_local);
result = control_sock_->Send(cookie_.c_str(), cookie_.length());
ASSERT_RETURN(result >= 0,-1);
// TODO: optimize this code, wait 100 millseconds for server creating the data sock
usleep(500*1000);
// to make a hole in the firewall.
result = data_sock_->Send(cookie_.c_str(), cookie_.length());
ASSERT_RETURN(result >= 0,-1);
context_->control_fd = control_sock_->GetFd();
context_->data_fd = data_sock_->GetFd();
context_->SetReadFd(control_sock_->GetFd());
context_->SetReadFd(data_sock_->GetFd());
context_->SetReadFd(multicast_sock_->GetFd());
if(OnConnected) OnConnected();
return 0;
}
int NetSnoopClient::RecvCommand()
{
int result;
std::string cmd(MAX_UDP_LENGTH, '\0');
if ((result = control_sock_->Recv(&cmd[0], cmd.length())) < 0)
{
return ERR_DEFAULT;
}
if(result == 0)
{
// socket closed.
return ERR_SOCKET_CLOSED;
}
cmd.resize(result);
auto command = CommandFactory::New(cmd);
if (!command)
return ERR_ILLEGAL_DATA;
LOGDP("recv new command: %s",command->GetCmd().c_str());
if(receiver_ && command->is_private)
{
auto stop_command = std::dynamic_pointer_cast<StopCommand>(command);
if(stop_command)
{
return receiver_->Stop();
}
return receiver_->RecvPrivateCommand(command);
}
#ifndef WIN32
// clear data socket data.
// std::string buf(MAX_UDP_LENGTH,0);
// while ((result = recv(data_sock_->GetFd(),&buf[0],buf.length(),MSG_DONTWAIT))>0)
// {
// LOGDP("illegal data recved(%d).",result);
// }
#endif // !WIN32
if(!command->is_multicast)
{
// to make a hole in the firewall.
result = data_sock_->Send(cookie_.c_str(), cookie_.length());
ASSERT_RETURN(result >= 0,ERR_DEFAULT,"send cookie error.");
}
auto ack_command = std::make_shared<AckCommand>();
result = control_sock_->Send(ack_command->GetCmd().c_str(),ack_command->GetCmd().length());
ASSERT_RETURN(result>0,ERR_DEFAULT,"send ack command error.");
auto channel = std::shared_ptr<CommandChannel>(new CommandChannel{
command,context_,control_sock_,command->is_multicast?multicast_sock_:data_sock_
});
receiver_ = command->CreateCommandReceiver(channel);
ASSERT(receiver_);
receiver_->OnStopped = OnStopped;
return receiver_->Start();
}
int NetSnoopClient::SendCommand()
{
ASSERT(receiver_);
receiver_->out_of_command_packets_ = illegal_packets_;
int result = receiver_->SendPrivateCommand();
receiver_ = NULL;
illegal_packets_ = 0;
return result;
}
int NetSnoopClient::RecvData(std::shared_ptr<Sock> data_sock)
{
int result;
if(!receiver_ || receiver_->GetDataFd() != data_sock->GetFd())
{
std::string buf(MAX_UDP_LENGTH,'\0');
result = data_sock->Recv(&buf[0],buf.length());
if(result<=0)
{
LOGWP("recv data error(%d).",data_sock->GetFd());
return result;
}
buf.resize(result);
illegal_packets_++;
LOGWP("recv out of command data(%d): %s",data_sock->GetFd(),Tools::GetDataSum(buf).c_str());
return result;
}
result = receiver_->Recv();
return result;
}
int NetSnoopClient::SendData()
{
if(!receiver_)
{
context_->ClrWriteFd(data_sock_->GetFd());
LOGWP("send out of command data(%d).",data_sock_->GetFd());
return 0;
}
int result = receiver_->Send();
return result;
}