forked from Jackarain/portmap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetmap.cpp
221 lines (193 loc) · 6.73 KB
/
netmap.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
#include "stdafx.h"
#include "netmap.hpp"
#include "logger.hpp"
int portmap_session::m_user_cnt = 0;
boost::mutex portmap_session::m_user_mtx;
portmap_session::portmap_session(boost::asio::io_service& io_service, tcp::endpoint& ep)
: m_local_socket(io_service)
, m_remote_socket(io_service)
, m_remote_host(ep)
{}
portmap_session::~portmap_session()
{
boost::system::error_code ignored_ec;
log_info("Id: " << m_user_cnt << ", IP: " <<
m_local_host.address().to_string(ignored_ec).c_str() << " : "
<< m_local_host.port() << " 离开.\n");
{
boost::mutex::scoped_lock lock(m_user_mtx);
m_user_cnt--;
}
}
void portmap_session::start()
{
boost::system::error_code ignored_ec;
{
boost::mutex::scoped_lock lock(m_user_mtx);
m_user_cnt++;
}
m_local_socket.set_option(tcp::no_delay(true), ignored_ec);
m_local_host = m_local_socket.remote_endpoint(ignored_ec);
log_info("Id: " << m_user_cnt << ", IP: "
<< m_local_host.address().to_string(ignored_ec).c_str()
<< " : " << m_local_host.port() << " 进来.\n");
// 发起远程连接.
m_remote_socket.async_connect(m_remote_host,
make_custom_alloc_handler(m_allocator,
boost::bind(&portmap_session::remote_connect, shared_from_this(),
boost::asio::placeholders::error)));
}
void portmap_session::close()
{
boost::system::error_code ignored_ec;
// 远程和本地链接都将关闭.
m_local_socket.shutdown(
boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
m_local_socket.close(ignored_ec);
m_remote_socket.shutdown(
boost::asio::ip::tcp::socket::shutdown_both, ignored_ec);
m_remote_socket.close(ignored_ec);
}
void portmap_session::handle_local_read(const boost::system::error_code& error,
int bytes_transferred)
{
if (!error)
{
// 发送到remote.
remote_write(m_local_buffer.data(), bytes_transferred);
// 继续接收local数据.
m_local_socket.async_read_some(boost::asio::buffer(m_local_buffer),
make_custom_alloc_handler(m_allocator,
boost::bind(&portmap_session::handle_local_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
else
{
close();
}
}
void portmap_session::handle_local_write(boost::shared_ptr<std::vector<char> > buffer,
size_t bytes_transferred, const boost::system::error_code& error)
{
if (!error)
{
// 继续发送未发送完成的数据.
if (bytes_transferred != buffer->size())
{
local_write(&(*(buffer->begin() + bytes_transferred)),
buffer->size() - bytes_transferred);
}
}
else
{
close();
}
}
void portmap_session::local_write(const char* buffer, int buffer_length)
{
boost::shared_ptr<std::vector<char> > buf(new
std::vector<char>(buffer, buffer + buffer_length));
// 发送数据.
m_local_socket.async_write_some(boost::asio::buffer(*buf),
make_custom_alloc_handler(m_allocator,
boost::bind(&portmap_session::handle_local_write,
shared_from_this(), buf,
boost::asio::placeholders::bytes_transferred,
boost::asio::placeholders::error)));
}
void portmap_session::remote_connect(const boost::system::error_code& err)
{
if (!err)
{
boost::system::error_code ignored_ec;
m_remote_socket.set_option(tcp::no_delay(true), ignored_ec);
// 连接成功, 发起一个读请求.
m_remote_socket.async_read_some(boost::asio::buffer(m_remote_buffer),
make_custom_alloc_handler(m_allocator,
boost::bind(&portmap_session::handle_remote_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
// 读取本地链接数据.
m_local_socket.async_read_some(boost::asio::buffer(m_local_buffer),
make_custom_alloc_handler(m_allocator,
boost::bind(&portmap_session::handle_local_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
else
{
close();
}
}
void portmap_session::handle_remote_read(const boost::system::error_code& error,
int bytes_transferred)
{
if (!error)
{
// 发送到local.
local_write(m_remote_buffer.data(), bytes_transferred);
// 继续接收remote的数据.
m_remote_socket.async_read_some(boost::asio::buffer(m_remote_buffer),
make_custom_alloc_handler(m_allocator,
boost::bind(&portmap_session::handle_remote_read, shared_from_this(),
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred)));
}
else
{
close();
}
}
void portmap_session::handle_remote_write(boost::shared_ptr<std::vector<char> > buffer,
size_t bytes_transferred, const boost::system::error_code& error)
{
if (!error)
{
// 继续发送未发送完成的数据.
if (bytes_transferred != buffer->size())
{
remote_write(&(*(buffer->begin() + bytes_transferred)),
buffer->size() - bytes_transferred);
}
}
else
{
close();
}
}
void portmap_session::remote_write(const char* buffer, int buffer_length)
{
boost::shared_ptr<std::vector<char> > buf(new
std::vector<char>(buffer, buffer + buffer_length));
// 发送数据.
m_remote_socket.async_write_some(boost::asio::buffer(*buf),
make_custom_alloc_handler(m_allocator,
boost::bind(&portmap_session::handle_remote_write,
shared_from_this(), buf,
boost::asio::placeholders::bytes_transferred,
boost::asio::placeholders::error)));
}
netmap_server::netmap_server(boost::asio::io_service& io_service, short server_port,
tcp::endpoint& ep, const std::string& dump_file)
: m_io_service(io_service)
, m_acceptor(io_service, tcp::endpoint(tcp::v4(), server_port))
, m_remote_host(ep)
{
portmap_session_ptr new_session(new portmap_session(m_io_service, m_remote_host));
m_acceptor.async_accept(new_session->socket(),
boost::bind(&netmap_server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
void netmap_server::handle_accept(portmap_session_ptr new_session,
const boost::system::error_code& error)
{
if (!error)
{
new_session->start();
new_session.reset(new portmap_session(m_io_service, m_remote_host));
m_acceptor.async_accept(new_session->socket(),
boost::bind(&netmap_server::handle_accept, this, new_session,
boost::asio::placeholders::error));
}
}