@@ -95,7 +95,7 @@ Listener& TCP::listen(tcp::Socket socket, ConnectCallback cb)
9595 auto & listener = listeners_.emplace (socket,
9696 std::make_unique<tcp::Listener>(*this , socket, std::move (cb))
9797 ).first ->second ;
98- debug (" <TCP::bind > Bound to socket %s \n " , socket.to_string ());
98+ debug (" <TCP::listen > Bound to socket %s \n " , socket.to_string ());
9999 return *listener;
100100}
101101
@@ -118,13 +118,11 @@ void TCP::connect(Socket remote, ConnectCallback callback)
118118
119119void TCP::connect (Address source, Socket remote, ConnectCallback callback)
120120{
121- validate_address (source);
122121 connect (bind (source), remote, std::move (callback));
123122}
124123
125124void TCP::connect (Socket local, Socket remote, ConnectCallback callback)
126125{
127- validate_address (local.address ());
128126 bind (local);
129127 create_connection (local, remote, std::move (callback))->open (true );
130128}
@@ -138,15 +136,13 @@ Connection_ptr TCP::connect(Socket remote)
138136
139137Connection_ptr TCP::connect (Address source, Socket remote)
140138{
141- validate_address (source);
142139 auto conn = create_connection (bind (source), remote);
143140 conn->open (true );
144141 return conn;
145142}
146143
147144Connection_ptr TCP::connect (Socket local, Socket remote)
148145{
149- validate_address (local.address ());
150146 bind (local);
151147 auto conn = create_connection (local, remote);
152148 conn->open (true );
@@ -298,6 +294,9 @@ bool TCP::is_bound(const Socket socket) const
298294
299295void TCP::bind (const Socket socket)
300296{
297+ if (UNLIKELY ( is_valid_source (socket.address ()) == false ))
298+ throw TCP_error{" Cannot bind to address: " + socket.address ().to_string ()};
299+
301300 if (UNLIKELY ( is_bound (socket) ))
302301 throw TCP_error{" Socket is already in use: " + socket.to_string ()};
303302
@@ -306,7 +305,9 @@ void TCP::bind(const Socket socket)
306305
307306Socket TCP::bind (const Address addr)
308307{
309- // assume address is already verified
308+ if (UNLIKELY ( is_valid_source (addr) == false ))
309+ throw TCP_error{" Cannot bind to address: " + addr.to_string ()};
310+
310311 auto & port_util = ports_[addr];
311312 const auto port = port_util.get_next_ephemeral ();
312313 // we know the port is not bound, else the above would throw
@@ -330,16 +331,6 @@ bool TCP::unbind(const Socket socket)
330331 return false ;
331332}
332333
333- void TCP::validate_address (const Address addr)
334- {
335- // TODO:
336- // Ask inet if the address is allowed
337- // if not
338- // throw TCP_error{"Address not allowed."};
339- if (addr != address () or addr != 0 ) // temp
340- throw TCP_error{" Cannot bind to address: " + addr.to_string ()};
341- }
342-
343334void TCP::add_connection (tcp::Connection_ptr conn) {
344335 // Stat increment number of incoming connections
345336 incoming_connections_++;
0 commit comments