Skip to content

Commit b4dc9eb

Browse files
authored
Add Connection Source
* Record and expose source, family and encryption of connection * Remove testing classes from public header
1 parent a790a9e commit b4dc9eb

File tree

11 files changed

+75
-14
lines changed

11 files changed

+75
-14
lines changed

include/comm/Connection.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#pragma once
3232

3333
#include <string>
34+
#include <memory>
3435
#include "util/Macros.h"
3536

3637
namespace comm
@@ -60,6 +61,11 @@ class Connection
6061
void set_max_buffer_size(uint32_t max_buffer_size);
6162
bool check_message_size(uint32_t size);
6263

64+
std::string source_family_name(short source_family) const;
65+
virtual std::string get_source() const = 0;
66+
virtual short get_source_family() const = 0;
67+
virtual std::string get_encryption() const = 0;
68+
6369
protected:
6470
virtual size_t read(uint8_t* buffer, size_t length) = 0;
6571
virtual size_t write(const uint8_t* buffer, size_t length) = 0;

src/comm/Connection.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include "comm/Exception.h"
3434
#include "comm/Variables.h"
3535

36+
#include <arpa/inet.h>
37+
3638
using namespace comm;
3739

3840
Connection::Connection(ConnMetrics* metrics)
@@ -125,6 +127,18 @@ void Connection::set_max_buffer_size(uint32_t max_buffer_size)
125127
_max_buffer_size = std::min(MAX_BUFFER_SIZE, _max_buffer_size);
126128
}
127129

130+
std::string Connection::source_family_name(short source_family) const
131+
{
132+
switch (source_family) {
133+
case AF_INET:
134+
return "ipv4";
135+
case AF_UNSPEC:
136+
return "unspec";
137+
default:
138+
return "unk";
139+
}
140+
}
141+
128142
ConnMetrics::~ConnMetrics() = default;
129143

130144
void ConnMetrics::observe_bytes_sent(std::size_t /*bytes_sent*/) {}

src/comm/TCPConnection.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@
4141

4242
using namespace comm;
4343

44-
TCPConnection::TCPConnection(ConnMetrics* metrics) : Connection(metrics), _tcp_socket() {}
45-
4644
TCPConnection::TCPConnection(std::unique_ptr< TCPSocket > tcp_socket, ConnMetrics* metrics)
4745
: Connection(metrics), _tcp_socket(std::move(tcp_socket))
4846
{
@@ -103,3 +101,9 @@ size_t TCPConnection::write(const uint8_t* buffer, size_t length)
103101

104102
return static_cast< size_t >(count);
105103
}
104+
105+
std::string TCPConnection::get_source() const { return _tcp_socket->print_source(); }
106+
107+
short TCPConnection::get_source_family() const { return _tcp_socket->source_family(); }
108+
109+
std::string TCPConnection::get_encryption() const { return "none"; }

src/comm/TCPConnection.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,16 @@ class TCPConnection : public Connection
4545
friend class TLSConnClient;
4646

4747
public:
48-
explicit TCPConnection(ConnMetrics* metrics = nullptr);
4948
explicit TCPConnection(std::unique_ptr< TCPSocket > tcp_socket, ConnMetrics* metrics = nullptr);
5049

5150
MOVEABLE_BY_DEFAULT(TCPConnection);
5251
NOT_COPYABLE(TCPConnection);
5352

5453
std::unique_ptr< TCPSocket > release_socket();
5554
void shutdown();
55+
std::string get_source() const override;
56+
short get_source_family() const override;
57+
std::string get_encryption() const override;
5658

5759
protected:
5860
size_t read(uint8_t* buffer, size_t length) override;

src/comm/TCPSocket.cc

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66

77
#include <cstring>
88
#include <netdb.h>
9-
#include <netinet/ip.h>
109
#include <sys/socket.h>
10+
#include <arpa/inet.h>
1111
#include <unistd.h>
1212

1313
#include "util/gcc_util.h"
@@ -21,8 +21,10 @@ ENABLE_WARNING(effc++)
2121
#include "comm/Variables.h"
2222

2323
using namespace comm;
24-
25-
TCPSocket::TCPSocket(int socket_fd) : _socket_fd(socket_fd) {}
24+
TCPSocket::TCPSocket(int socket_fd, sockaddr_in address)
25+
: _socket_fd(socket_fd), _source_family(AF_INET), _source(std::move(address))
26+
{
27+
}
2628

2729
TCPSocket::~TCPSocket()
2830
{
@@ -66,7 +68,7 @@ std::unique_ptr< TCPSocket > TCPSocket::accept(const std::unique_ptr< TCPSocket
6668
}
6769
// MAGICK can be done here
6870

69-
return std::unique_ptr< TCPSocket >(new TCPSocket(connected_socket));
71+
return std::unique_ptr< TCPSocket >(new TCPSocket(connected_socket, std::move(clnt_addr)));
7072
}
7173

7274
bool TCPSocket::bind(int port)
@@ -104,12 +106,14 @@ bool TCPSocket::connect(const Address& address)
104106
std::unique_ptr< TCPSocket > TCPSocket::create()
105107
{
106108
int tcp_socket = ::socket(AF_INET, SOCK_STREAM, 0);
109+
sockaddr_in source;
110+
memset(&source, 0, sizeof(_source));
107111

108112
if (tcp_socket < 0) {
109113
THROW_EXCEPTION(SocketFail);
110114
}
111115

112-
return std::unique_ptr< TCPSocket >(new TCPSocket(tcp_socket));
116+
return std::unique_ptr< TCPSocket >(new TCPSocket(tcp_socket, std::move(source)));
113117
}
114118

115119
bool TCPSocket::listen() { return ::listen(_socket_fd, MAX_CONN_QUEUE) == 0; }
@@ -127,3 +131,14 @@ bool TCPSocket::set_timeval_option(int level, int option_name, timeval value)
127131
}
128132

129133
void TCPSocket::shutdown() { ::shutdown(_socket_fd, SHUT_RDWR); }
134+
135+
std::string TCPSocket::print_source()
136+
{
137+
if (_source_family == AF_UNSPEC) {
138+
return "";
139+
} else {
140+
return std::string(inet_ntoa(_source.sin_addr));
141+
}
142+
}
143+
144+
short TCPSocket::source_family() { return _source_family; }

src/comm/TCPSocket.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include <memory>
3434
#include <string>
35+
#include <netinet/ip.h>
3536

3637
#include "comm/Address.h"
3738

@@ -60,10 +61,15 @@ class TCPSocket
6061
bool set_timeval_option(int level, int option_name, timeval value);
6162
void shutdown();
6263

64+
std::string print_source();
65+
short source_family();
66+
6367
private:
64-
explicit TCPSocket(int socket_fd);
68+
explicit TCPSocket(int socket_fd, sockaddr_in);
6569

6670
int _socket_fd{-1};
71+
short _source_family{AF_UNSPEC};
72+
struct sockaddr_in _source;
6773
};
6874

6975
}; // namespace comm

src/comm/TLSConnection.cc

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ ENABLE_WARNING(effc++)
2222

2323
using namespace comm;
2424

25-
TLSConnection::TLSConnection(ConnMetrics* metrics) : Connection(metrics), _tls_socket() {}
26-
2725
TLSConnection::TLSConnection(std::unique_ptr< TLSSocket > tls_socket, ConnMetrics* metrics)
2826
: Connection(metrics), _tls_socket(std::move(tls_socket))
2927
{
@@ -86,3 +84,9 @@ size_t TLSConnection::write(const uint8_t* buffer, size_t length)
8684

8785
return static_cast< size_t >(count);
8886
}
87+
88+
std::string TLSConnection::get_source() const { return _tls_socket->print_source(); }
89+
90+
short TLSConnection::get_source_family() const { return _tls_socket->source_family(); }
91+
92+
std::string TLSConnection::get_encryption() const { return "tls"; }

src/comm/TLSConnection.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ namespace comm
1717
class TLSConnection : public Connection
1818
{
1919
public:
20-
explicit TLSConnection(ConnMetrics* metrics = nullptr);
2120
explicit TLSConnection(std::unique_ptr< TLSSocket > tls_socket, ConnMetrics* metrics = nullptr);
2221

2322
MOVEABLE_BY_DEFAULT(TLSConnection);
2423
NOT_COPYABLE(TLSConnection);
2524

25+
std::string get_source() const override;
26+
short get_source_family() const override;
27+
std::string get_encryption() const override;
28+
2629
protected:
2730
size_t read(uint8_t* buffer, size_t length) override;
2831
size_t write(const uint8_t* buffer, size_t length) override;

src/comm/TLSSocket.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,7 @@ std::unique_ptr< TLSSocket > TLSSocket::create(std::unique_ptr< TCPSocket > tcp_
6767

6868
return std::unique_ptr< TLSSocket >(new TLSSocket(std::move(tcp_socket), ssl));
6969
}
70+
71+
std::string TLSSocket::print_source() { return _tcp_socket->print_source(); }
72+
73+
short TLSSocket::source_family() { return _tcp_socket->source_family(); }

src/comm/TLSSocket.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class TLSSocket
3030
void accept();
3131
void connect();
3232

33+
std::string print_source();
34+
short source_family();
35+
3336
private:
3437
explicit TLSSocket(std::unique_ptr< TCPSocket > tcp_socket, SSL* ssl);
3538

0 commit comments

Comments
 (0)