Skip to content

Commit a903b36

Browse files
tcp: Moved validation of source address into bind
1 parent f157f37 commit a903b36

2 files changed

Lines changed: 13 additions & 19 deletions

File tree

api/net/tcp/tcp.hpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,11 +608,14 @@ namespace net {
608608
{ return bind(address()); }
609609

610610
/**
611-
* @brief Validate the address by making sure it's allowed in this context.
611+
* @brief Determines if the source address is valid.
612612
*
613-
* @param[in] addr The address
613+
* @param[in] addr The source address
614+
*
615+
* @return True if valid source, False otherwise.
614616
*/
615-
void validate_address(const tcp::Address addr);
617+
bool is_valid_source(const tcp::Address addr) const noexcept
618+
{ return addr == address() or addr == 0; /* temp */ }
616619

617620
/**
618621
* @brief Try to find the listener bound to socket.

src/net/tcp/tcp.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -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

119119
void TCP::connect(Address source, Socket remote, ConnectCallback callback)
120120
{
121-
validate_address(source);
122121
connect(bind(source), remote, std::move(callback));
123122
}
124123

125124
void 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

139137
Connection_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

147144
Connection_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

299295
void 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

307306
Socket 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-
343334
void TCP::add_connection(tcp::Connection_ptr conn) {
344335
// Stat increment number of incoming connections
345336
incoming_connections_++;

0 commit comments

Comments
 (0)