Skip to content

Commit 1e5f8eb

Browse files
authored
Merge pull request #1257 from AndreasAakesson/dev
tcp: Redefined bind => listen and support for virtual IPs
2 parents 98eb1bc + a903b36 commit 1e5f8eb

16 files changed

Lines changed: 443 additions & 174 deletions

File tree

api/net/inet_common.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,10 @@ namespace net {
108108
static constexpr uint16_t USER_START {1024};
109109
static constexpr uint16_t USER_END {49151};
110110
static constexpr uint16_t DYNAMIC_START {49152};
111-
static constexpr uint16_t DYNAMIC_END {65534}; // 65535 never assigned
111+
static constexpr uint16_t DYNAMIC_END {65535}; // 65535 should never be assigned
112+
113+
static constexpr bool is_dynamic(const uint16_t port) noexcept
114+
{ return port > USER_END; }
112115
}
113116

114117
/**

api/net/tcp/connection.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
5050

5151
public:
5252
/** Connection identifier */
53-
using Tuple = std::pair<port_t, Socket>;
53+
using Tuple = std::pair<Socket, Socket>;
5454
/** Interface for TCP states */
5555
class State;
5656
/** Disconnect event */
@@ -517,7 +517,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
517517
* @return A "tuple" of [[local port], [remote ip, remote port]]
518518
*/
519519
Connection::Tuple tuple() const noexcept
520-
{ return {local_port_, remote_}; }
520+
{ return {local_, remote_}; }
521521

522522
/// --- State checks --- ///
523523

@@ -610,14 +610,15 @@ class Connection : public std::enable_shared_from_this<Connection> {
610610
* @return A 16 bit unsigned port number
611611
*/
612612
port_t local_port() const noexcept
613-
{ return local_port_; }
613+
{ return local_.port(); }
614614

615615
/**
616616
* @brief The local Socket bound to this connection.
617617
*
618618
* @return A TCP Socket
619619
*/
620-
Socket local() const noexcept;
620+
Socket local() const noexcept
621+
{ return local_; }
621622

622623
/**
623624
* @brief The remote Socket bound to this connection.
@@ -777,7 +778,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
777778
* @param[in] remote The remote socket
778779
* @param[in] callback The connection callback
779780
*/
780-
Connection(TCP& host, port_t local_port, Socket remote, ConnectCallback callback = nullptr);
781+
Connection(TCP& host, Socket local, Socket remote, ConnectCallback callback = nullptr);
781782

782783
Connection(const Connection&) = delete;
783784
Connection(Connection&&) = delete;
@@ -819,7 +820,7 @@ class Connection : public std::enable_shared_from_this<Connection> {
819820
TCP& host_;
820821

821822
/* End points. */
822-
port_t local_port_;
823+
Socket local_;
823824
Socket remote_;
824825

825826
/** The current state the Connection is in. Handles most of the logic. */

api/net/tcp/listener.hpp

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Listener {
4141

4242
public:
4343

44-
Listener(TCP& host, port_t port, ConnectCallback cb = nullptr);
44+
Listener(TCP& host, Socket local, ConnectCallback cb = nullptr);
4545

4646
Listener& on_accept(AcceptCallback cb)
4747
{
@@ -59,14 +59,14 @@ class Listener {
5959

6060
/**
6161
* @brief Returns the local socket identified with this Listener
62-
* @details Creates a temporary identifier for the Listener,
63-
* in form of Address to the current stack (TCP) and the port_
64-
* @return The local Socket
62+
*
63+
* @return The local Socket the listener is bound to
6564
*/
66-
Socket local() const;
65+
Socket local() const noexcept
66+
{ return local_; }
6767

68-
constexpr port_t port() const
69-
{ return port_; }
68+
port_t port() const noexcept
69+
{ return local_.port(); }
7070

7171
auto syn_queue_size() const
7272
{ return syn_queue_.size(); }
@@ -88,17 +88,13 @@ class Listener {
8888

8989
private:
9090
friend class net::TCP;
91-
TCP& host_;
92-
const port_t port_;
93-
SynQueue syn_queue_;
91+
TCP& host_;
92+
Socket local_;
93+
SynQueue syn_queue_;
9494

95-
/** */
96-
AcceptCallback on_accept_;
97-
98-
/** */
95+
AcceptCallback on_accept_;
9996
ConnectCallback on_connect_;
100-
101-
CloseCallback _on_close_;
97+
CloseCallback _on_close_;
10298

10399
bool default_on_accept(Socket);
104100

0 commit comments

Comments
 (0)