/
router.cpp
278 lines (247 loc) · 7.14 KB
/
router.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
#include "libf2f/router.h"
#include "libf2f/connection.h"
#include "libf2f/protocol.h"
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <boost/foreach.hpp>
namespace libf2f {
using namespace std;
Router::Router( boost::shared_ptr<boost::asio::ip::tcp::acceptor> accp,
Protocol * p, boost::function<std::string()> uuidf )
: m_acceptor( accp ),
m_protocol( p ),
seen_connections(0),
m_uuidgen( uuidf )
{
cout << "Testing uuid generator... " << flush;
string uuid = m_uuidgen();
if( uuid.length() != 36 )
{
cout << "ERROR length must be 36." << endl;
throw;
}
cout << "OK" << endl;
p->set_router( this );
// Start an accept operation for a new connection.
connection_ptr new_conn = new_connection();
m_acceptor->async_accept(new_conn->socket(),
boost::bind(&Router::handle_accept, this,
boost::asio::placeholders::error, new_conn));
}
connection_ptr
Router::new_connection()
{
return connection_ptr( new Connection( m_acceptor->io_service(), this ) );
}
std::string
Router::gen_uuid()
{
return m_uuidgen();
}
void
Router::stop()
{
while( m_connections.size() )
{
// this removes the connection from m_connections:
m_connections.front()->fin();
}
}
void
Router::connection_terminated( connection_ptr conn )
{
unregister_connection( conn );
m_protocol->connection_terminated( conn );
}
/// Handle completion of a accept operation.
void
Router::handle_accept(const boost::system::error_code& e, connection_ptr conn)
{
if(e)
{
// Log it and return. Since we are not starting a new
// accept operation the io_service will run out of work to do and the
// Servent will exit.
std::cerr << e.message() << std::endl;
return;
}
if( !m_protocol->new_incoming_connection(conn) )
{
// cout << "Rejecting connection " << conn->str() << endl;
// don't register it (so it autodestructs)
}
else
{
register_connection( conn );
conn->async_read();
}
// Start an accept operation for a new connection.
connection_ptr new_conn = new_connection();
m_acceptor->async_accept(new_conn->socket(),
boost::bind(&Router::handle_accept, this,
boost::asio::placeholders::error, new_conn));
}
void
Router::register_connection( connection_ptr conn )
{
boost::mutex::scoped_lock lk(m_connections_mutex);
vector<connection_ptr>::iterator it;
for( it=m_connections.begin() ; it < m_connections.end() ; ++it )
{
if( *it == conn )
{
// already registered, wtf?
cout << "ERROR connection already registered!" << endl;
assert(false);
return;
}
}
m_connections.push_back( conn );
//cout << connections_str() << endl;
}
void
Router::unregister_connection( connection_ptr conn )
{
boost::mutex::scoped_lock lk(m_connections_mutex);
vector<connection_ptr>::iterator it;
for( it=m_connections.begin() ; it < m_connections.end() ; ++it )
{
if( *it == conn )
{
m_connections.erase( it );
//cout << "Router::unregistered " << conn->str() << endl;
}
}
//cout << connections_str() << endl;
}
connection_ptr
Router::get_connection_by_name( const std::string &name )
{
vector<connection_ptr>::iterator it;
boost::mutex::scoped_lock lk(m_connections_mutex);
for( it=m_connections.begin() ; it < m_connections.end() ; ++it )
{
if( (*it)->name() == name )
{
return *it;
}
}
return connection_ptr();
}
/// debug usage - get list of connections
string
Router::connections_str()
{
ostringstream os;
os << "<connections>" << endl;
BOOST_FOREACH( connection_ptr conn, m_connections )
{
os << conn->str() << endl;
}
os << "</connections>" << endl;
return os.str();
}
vector<string>
Router::get_connected_names()
{
vector<string> v;
boost::mutex::scoped_lock lk(m_connections_mutex);
BOOST_FOREACH( connection_ptr conn, m_connections )
{
v.push_back( conn->name() );
}
return v;
}
/// this is the default msg recvd callback passed to new connections
/// it does some basic sanity checks, then fires the callback
void
Router::message_received( message_ptr msgp, connection_ptr conn )
{
//cout << "router::message_received from " << conn->str()
// << " " << msgp->str() << endl;
/* // Not relevant for darknet configurations:
if( msgp->hops() > 3 )
{
cout << "Dropping, hop count: " << msgp->hops() << endl;
return;
}
*/
if( msgp->length() > 16384 ) // hard limit
{
cout << "f2f router: Dropping, msg length: " << msgp->length() << endl;
return;
}
m_protocol->message_received( msgp, conn );
}
/// Connect out to a remote Servent at endpoint
void
Router::connect_to_remote(boost::asio::ip::tcp::endpoint &endpoint)
{
map<string,string> props;
connect_to_remote( endpoint, props );
}
void
Router::connect_to_remote(boost::asio::ip::tcp::endpoint &endpoint, const map<string,string>& props)
{
cout << "router::connect_to_remote(" << endpoint.address().to_string()<<":"
<< endpoint.port()<<")" << endl;
connection_ptr new_conn = new_connection();
typedef pair<string,string> pair_t;
BOOST_FOREACH( pair_t p, props )
{
new_conn->set( p.first, p.second );
}
// Start an asynchronous connect operation.
new_conn->socket().async_connect(endpoint,
boost::bind(&Router::handle_connect, this,
boost::asio::placeholders::error, endpoint, new_conn));
}
/// Handle completion of a connect operation.
void
Router::handle_connect( const boost::system::error_code& e,
boost::asio::ip::tcp::endpoint &endpoint,
connection_ptr conn )
{
if (e)
{
std::cerr << "Failed to connect out to remote Servent: "
<< e.message() << std::endl;
return;
}
/// Successfully established connection.
m_protocol->new_outgoing_connection( conn );
register_connection( conn );
conn->async_read(); // start read loop for this connection
}
/// apply fun to all connections
void
Router::foreach_conns( boost::function<void(connection_ptr)> fun )
{
boost::mutex::scoped_lock lk(m_connections_mutex);
BOOST_FOREACH( connection_ptr conn, m_connections )
{
fun( conn );
}
}
void
Router::foreach_conns_except( boost::function<void(connection_ptr)> fun, connection_ptr conn )
{
boost::mutex::scoped_lock lk(m_connections_mutex);
BOOST_FOREACH( connection_ptr c, m_connections )
{
if( c == conn ) continue;
fun( c );
}
}
void
Router::send_all( message_ptr msgp )
{
//foreach_conns( boost::bind(&Connection::async_write, _1, msgp) );
boost::mutex::scoped_lock lk(m_connections_mutex);
BOOST_FOREACH( connection_ptr conn, m_connections )
{
//cout << "Sending " << msgp->str() << " to " << conn->str() << endl;
conn->async_write( msgp );
}
}
} //ns