Skip to content

Commit eac9909

Browse files
authored
Expose the function to check if socket is open (#40)
1 parent 0c5b2be commit eac9909

File tree

8 files changed

+25
-0
lines changed

8 files changed

+25
-0
lines changed

include/comm/Connection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class Connection
6565
virtual std::string get_source() const = 0;
6666
virtual short get_source_family() const = 0;
6767
virtual std::string get_encryption() const = 0;
68+
virtual bool is_open() = 0;
6869

6970
protected:
7071
virtual size_t read(uint8_t* buffer, size_t length) = 0;

src/comm/TCPConnection.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,5 @@ std::string TCPConnection::get_source() const { return _tcp_socket->print_source
107107
short TCPConnection::get_source_family() const { return _tcp_socket->source_family(); }
108108

109109
std::string TCPConnection::get_encryption() const { return "none"; }
110+
111+
bool TCPConnection::is_open() { return _tcp_socket->is_open(); }

src/comm/TCPConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class TCPConnection : public Connection
5555
std::string get_source() const override;
5656
short get_source_family() const override;
5757
std::string get_encryption() const override;
58+
bool is_open() override;
5859

5960
protected:
6061
size_t read(uint8_t* buffer, size_t length) override;

src/comm/TCPSocket.cc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <sys/socket.h>
1010
#include <arpa/inet.h>
1111
#include <unistd.h>
12+
#include <netinet/tcp.h>
1213

1314
#include "util/gcc_util.h"
1415
DISABLE_WARNING(effc++)
@@ -142,3 +143,18 @@ std::string TCPSocket::print_source()
142143
}
143144

144145
short TCPSocket::source_family() { return _source_family; }
146+
147+
bool TCPSocket::is_open()
148+
{
149+
tcp_info socket_info;
150+
socklen_t socket_info_length = sizeof(socket_info);
151+
if (getsockopt(_socket_fd, SOL_TCP, TCP_INFO, &socket_info, &socket_info_length) == -1) {
152+
return false;
153+
}
154+
if (socket_info.tcpi_state == TCP_CLOSE || socket_info.tcpi_state == TCP_CLOSE_WAIT ||
155+
socket_info.tcpi_state == TCP_LAST_ACK || socket_info.tcpi_state == TCP_LISTEN ||
156+
socket_info.tcpi_state == TCP_CLOSING) {
157+
return false;
158+
}
159+
return true;
160+
}

src/comm/TCPSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class TCPSocket
6060
bool set_boolean_option(int level, int option_name, bool value);
6161
bool set_timeval_option(int level, int option_name, timeval value);
6262
void shutdown();
63+
bool is_open();
6364

6465
std::string print_source();
6566
short source_family();

src/comm/TLSConnection.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,5 @@ std::string TLSConnection::get_source() const { return _tls_socket->print_source
9090
short TLSConnection::get_source_family() const { return _tls_socket->source_family(); }
9191

9292
std::string TLSConnection::get_encryption() const { return "tls"; }
93+
94+
bool TLSConnection::is_open() { return _tls_socket->_tcp_socket->is_open(); }

src/comm/TLSConnection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class TLSConnection : public Connection
2525
std::string get_source() const override;
2626
short get_source_family() const override;
2727
std::string get_encryption() const override;
28+
bool is_open() override;
2829

2930
protected:
3031
size_t read(uint8_t* buffer, size_t length) override;

src/comm/TLSSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TLSSocket
2929

3030
void accept();
3131
void connect();
32+
bool is_open();
3233

3334
std::string print_source();
3435
short source_family();

0 commit comments

Comments
 (0)