network utils refactor - renaming of socket handle to unique socket and cleanup of its methods

master
Francisco 4 years ago committed by Francisco Paisana
parent aed5c51010
commit bf96d897ee

@ -36,8 +36,8 @@ private:
void write_mac_lte_pdu_to_net(srsran::mac_pcap_base::pcap_pdu_t& pdu); void write_mac_lte_pdu_to_net(srsran::mac_pcap_base::pcap_pdu_t& pdu);
void write_mac_nr_pdu_to_net(srsran::mac_pcap_base::pcap_pdu_t& pdu); void write_mac_nr_pdu_to_net(srsran::mac_pcap_base::pcap_pdu_t& pdu);
srsran::socket_handler_t socket; srsran::unique_socket socket;
struct sockaddr_in client_addr; struct sockaddr_in client_addr;
}; };
} // namespace srsran } // namespace srsran

@ -58,20 +58,19 @@ bool connect_to(int fd, const char* dest_addr_str, int dest_port, sockaddr_in* d
/** /**
* Description: Net socket class with convenience methods for connecting, binding, and opening socket * Description: Net socket class with convenience methods for connecting, binding, and opening socket
*/ */
class socket_handler_t class unique_socket
{ {
public: public:
socket_handler_t() = default; unique_socket() = default;
socket_handler_t(const socket_handler_t&) = delete; unique_socket(const unique_socket&) = delete;
socket_handler_t(socket_handler_t&& other) noexcept; unique_socket(unique_socket&& other) noexcept;
~socket_handler_t(); ~unique_socket();
socket_handler_t& operator=(const socket_handler_t&) = delete; unique_socket& operator=(const unique_socket&) = delete;
socket_handler_t& operator =(socket_handler_t&&) noexcept; unique_socket& operator =(unique_socket&&) noexcept;
void close(); void close();
void reset();
bool is_init() const { return sockfd >= 0; } bool is_open() const { return sockfd >= 0; }
int fd() const { return sockfd; } int fd() const { return sockfd; }
const sockaddr_in& get_addr_in() const { return addr; } const sockaddr_in& get_addr_in() const { return addr; }
std::string get_ip() const { return net_utils::get_ip(addr); } std::string get_ip() const { return net_utils::get_ip(addr); }
@ -80,7 +79,7 @@ public:
bool bind_addr(const char* bind_addr_str, int port); bool bind_addr(const char* bind_addr_str, int port);
bool connect_to(const char* dest_addr_str, int dest_port, sockaddr_in* dest_sockaddr = nullptr); bool connect_to(const char* dest_addr_str, int dest_port, sockaddr_in* dest_sockaddr = nullptr);
bool open_socket(net_utils::addr_family ip, net_utils::socket_type socket_type, net_utils::protocol_type protocol); bool open_socket(net_utils::addr_family ip, net_utils::socket_type socket_type, net_utils::protocol_type protocol);
int get_socket() { return sockfd; }; int get_socket() const { return sockfd; };
protected: protected:
sockaddr_in addr = {}; sockaddr_in addr = {};
@ -89,14 +88,8 @@ protected:
namespace net_utils { namespace net_utils {
bool sctp_init_client(socket_handler_t* socket, net_utils::socket_type socktype, const char* bind_addr_str); bool sctp_init_client(unique_socket* socket, net_utils::socket_type socktype, const char* bind_addr_str);
bool sctp_init_server(socket_handler_t* socket, net_utils::socket_type socktype, const char* bind_addr_str, int port); bool sctp_init_server(unique_socket* socket, net_utils::socket_type socktype, const char* bind_addr_str, int port);
// TODO: for TCP and UDP
bool tcp_make_server(socket_handler_t* socket, const char* bind_addr_str, int port, int nof_connections = 1);
int tcp_accept(socket_handler_t* socket, sockaddr_in* destaddr);
int tcp_read(int remotefd, void* buf, size_t nbytes);
int tcp_send(int remotefd, const void* buf, size_t nbytes);
} // namespace net_utils } // namespace net_utils

@ -28,7 +28,7 @@ uint32_t mac_pcap_net::open(std::string client_ip_addr_,
{ {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (socket.is_init()) { if (socket.is_open()) {
logger.error("PCAP socket writer for %s already running. Close first.", bind_addr_str.c_str()); logger.error("PCAP socket writer for %s already running. Close first.", bind_addr_str.c_str());
return SRSRAN_ERROR; return SRSRAN_ERROR;
} }
@ -39,7 +39,7 @@ uint32_t mac_pcap_net::open(std::string client_ip_addr_,
return SRSRAN_ERROR; return SRSRAN_ERROR;
} }
if (not socket.bind_addr(bind_addr_str.c_str(), bind_udp_port_)) { if (not socket.bind_addr(bind_addr_str.c_str(), bind_udp_port_)) {
socket.reset(); socket.close();
logger.error("Couldn't bind socket %s to write PCAP", bind_addr_str.c_str()); logger.error("Couldn't bind socket %s to write PCAP", bind_addr_str.c_str());
return SRSRAN_ERROR; return SRSRAN_ERROR;
} }
@ -64,7 +64,7 @@ uint32_t mac_pcap_net::close()
{ {
{ {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
if (running == false || socket.is_init() == false) { if (running == false || socket.is_open() == false) {
return SRSRAN_ERROR; return SRSRAN_ERROR;
} }
@ -76,7 +76,7 @@ uint32_t mac_pcap_net::close()
wait_thread_finish(); wait_thread_finish();
// close socket handle // close socket handle
if (socket.is_init()) { if (socket.is_open()) {
std::lock_guard<std::mutex> lock(mutex); std::lock_guard<std::mutex> lock(mutex);
socket.close(); socket.close();
} }
@ -86,7 +86,7 @@ uint32_t mac_pcap_net::close()
void mac_pcap_net::write_pdu(pcap_pdu_t& pdu) void mac_pcap_net::write_pdu(pcap_pdu_t& pdu)
{ {
if (pdu.pdu != nullptr && socket.is_init()) { if (pdu.pdu != nullptr && socket.is_open()) {
switch (pdu.rat) { switch (pdu.rat) {
case srsran_rat_t::lte: case srsran_rat_t::lte:
write_mac_lte_pdu_to_net(pdu); write_mac_lte_pdu_to_net(pdu);

@ -234,63 +234,53 @@ bool connect_to(int fd, const char* dest_addr_str, int dest_port, sockaddr_in* d
* Socket Classes * Socket Classes
*******************************************/ *******************************************/
socket_handler_t::socket_handler_t(socket_handler_t&& other) noexcept unique_socket::unique_socket(unique_socket&& other) noexcept : sockfd(other.sockfd), addr(other.addr)
{ {
sockfd = other.sockfd; other.sockfd = -1;
addr = other.addr;
other.sockfd = 0;
other.addr = {}; other.addr = {};
} }
socket_handler_t::~socket_handler_t() unique_socket::~unique_socket()
{ {
reset(); close();
} }
socket_handler_t& socket_handler_t::operator=(socket_handler_t&& other) noexcept unique_socket& unique_socket::operator=(unique_socket&& other) noexcept
{ {
if (this == &other) {
return *this;
}
addr = other.addr;
sockfd = other.sockfd; sockfd = other.sockfd;
addr = other.addr;
other.sockfd = -1;
other.addr = {}; other.addr = {};
other.sockfd = 0;
return *this; return *this;
} }
void socket_handler_t::close() void unique_socket::close()
{ {
if (sockfd >= 0) { if (sockfd >= 0) {
::close(sockfd); ::close(sockfd);
sockfd = -1; sockfd = -1;
addr = {};
} }
} }
void socket_handler_t::reset() bool unique_socket::bind_addr(const char* bind_addr_str, int port)
{
this->close();
addr = {};
}
bool socket_handler_t::bind_addr(const char* bind_addr_str, int port)
{ {
return net_utils::bind_addr(sockfd, bind_addr_str, port, &addr); return net_utils::bind_addr(sockfd, bind_addr_str, port, &addr);
} }
bool socket_handler_t::connect_to(const char* dest_addr_str, int dest_port, sockaddr_in* dest_sockaddr) bool unique_socket::connect_to(const char* dest_addr_str, int dest_port, sockaddr_in* dest_sockaddr)
{ {
return net_utils::connect_to(sockfd, dest_addr_str, dest_port, dest_sockaddr); return net_utils::connect_to(sockfd, dest_addr_str, dest_port, dest_sockaddr);
} }
bool socket_handler_t::open_socket(net_utils::addr_family ip_type, bool unique_socket::open_socket(net_utils::addr_family ip_type,
net_utils::socket_type socket_type, net_utils::socket_type socket_type,
net_utils::protocol_type protocol) net_utils::protocol_type protocol)
{ {
if (sockfd >= 0) { if (is_open()) {
srslog::fetch_basic_logger(LOGSERVICE).error("Socket is already open."); srslog::fetch_basic_logger(LOGSERVICE).error("Socket is already open.");
return false; return false;
} }
sockfd = net_utils::open_socket(ip_type, socket_type, protocol); sockfd = net_utils::open_socket(ip_type, socket_type, protocol);
return sockfd >= 0; return is_open();
} }
/*********************************************************************** /***********************************************************************
@ -299,24 +289,24 @@ bool socket_handler_t::open_socket(net_utils::addr_family ip_type,
namespace net_utils { namespace net_utils {
bool sctp_init_socket(socket_handler_t* socket, net_utils::socket_type socktype, const char* bind_addr_str, int port) bool sctp_init_socket(unique_socket* socket, net_utils::socket_type socktype, const char* bind_addr_str, int port)
{ {
if (not socket->open_socket(net_utils::addr_family::ipv4, socktype, net_utils::protocol_type::SCTP)) { if (not socket->open_socket(net_utils::addr_family::ipv4, socktype, net_utils::protocol_type::SCTP)) {
return false; return false;
} }
if (not socket->bind_addr(bind_addr_str, port)) { if (not socket->bind_addr(bind_addr_str, port)) {
socket->reset(); socket->close();
return false; return false;
} }
return true; return true;
} }
bool sctp_init_client(socket_handler_t* socket, net_utils::socket_type socktype, const char* bind_addr_str) bool sctp_init_client(unique_socket* socket, net_utils::socket_type socktype, const char* bind_addr_str)
{ {
return sctp_init_socket(socket, socktype, bind_addr_str, 0); return sctp_init_socket(socket, socktype, bind_addr_str, 0);
} }
bool sctp_init_server(socket_handler_t* socket, net_utils::socket_type socktype, const char* bind_addr_str, int port) bool sctp_init_server(unique_socket* socket, net_utils::socket_type socktype, const char* bind_addr_str, int port)
{ {
if (not sctp_init_socket(socket, socktype, bind_addr_str, port)) { if (not sctp_init_socket(socket, socktype, bind_addr_str, port)) {
return false; return false;
@ -329,72 +319,6 @@ bool sctp_init_server(socket_handler_t* socket, net_utils::socket_type socktype,
return true; return true;
} }
/***************************************************************
* TCP Socket
**************************************************************/
bool tcp_make_server(socket_handler_t* socket, const char* bind_addr_str, int port, int nof_connections)
{
if (not socket->open_socket(addr_family::ipv4, socket_type::stream, protocol_type::TCP)) {
return false;
}
if (not socket->bind_addr(bind_addr_str, port)) {
socket->reset();
return false;
}
// Listen for connections
if (listen(socket->fd(), nof_connections) != 0) {
srslog::fetch_basic_logger(LOGSERVICE).error("Failed to listen to incoming TCP connections");
return false;
}
return true;
}
int tcp_accept(socket_handler_t* socket, sockaddr_in* destaddr)
{
socklen_t clilen = sizeof(destaddr);
int connfd = accept(socket->fd(), (struct sockaddr*)&destaddr, &clilen);
if (connfd < 0) {
srslog::fetch_basic_logger(LOGSERVICE).error("Failed to accept connection");
perror("accept");
return -1;
}
return connfd;
}
int tcp_read(int remotefd, void* buf, size_t nbytes)
{
int n = ::read(remotefd, buf, nbytes);
if (n == 0) {
srslog::fetch_basic_logger(LOGSERVICE).info("TCP connection closed");
close(remotefd);
return 0;
}
if (n == -1) {
srslog::fetch_basic_logger(LOGSERVICE).error("Failed to read from TCP socket.");
perror("TCP read");
}
return n;
}
int tcp_send(int remotefd, const void* buf, size_t nbytes)
{
// Loop until all bytes are sent
char* ptr = (char*)buf;
ssize_t nbytes_remaining = nbytes;
while (nbytes_remaining > 0) {
ssize_t i = ::send(remotefd, ptr, nbytes_remaining, 0);
if (i < 1) {
srslog::fetch_basic_logger(LOGSERVICE).error("Failed to send data to TCP socket");
perror("Error calling send()\n");
return i;
}
ptr += i;
nbytes_remaining -= i;
}
return nbytes - nbytes_remaining;
}
} // namespace net_utils } // namespace net_utils
/*************************************************************** /***************************************************************

@ -27,7 +27,7 @@ int test_socket_handler()
int counter = 0; int counter = 0;
srsran::socket_handler_t server_socket, client_socket, client_socket2; srsran::unique_socket server_socket, client_socket, client_socket2;
srsran::rx_multisocket_handler sockhandler("RXSOCKETS", logger); srsran::rx_multisocket_handler sockhandler("RXSOCKETS", logger);
int server_port = 36412; int server_port = 36412;
const char* server_addr = "127.0.100.1"; const char* server_addr = "127.0.100.1";
@ -60,7 +60,7 @@ int test_socket_handler()
for (int32_t i = 0; i < nof_counts; ++i) { for (int32_t i = 0; i < nof_counts; ++i) {
buf[i] = i; buf[i] = i;
// Round-robin between clients // Round-robin between clients
srsran::socket_handler_t* chosen = &client_socket; srsran::unique_socket* chosen = &client_socket;
if (i % 2 == 1) { if (i % 2 == 1) {
chosen = &client_socket2; chosen = &client_socket2;
} }

@ -115,13 +115,13 @@ private:
srsenb::stack_interface_s1ap_lte* stack = nullptr; srsenb::stack_interface_s1ap_lte* stack = nullptr;
srsran::task_sched_handle task_sched; srsran::task_sched_handle task_sched;
srsran::socket_handler_t s1ap_socket; srsran::unique_socket s1ap_socket;
struct sockaddr_in mme_addr = {}; // MME address struct sockaddr_in mme_addr = {}; // MME address
bool mme_connected = false; bool mme_connected = false;
bool running = false; bool running = false;
uint32_t next_enb_ue_s1ap_id = 1; // Next ENB-side UE identifier uint32_t next_enb_ue_s1ap_id = 1; // Next ENB-side UE identifier
uint16_t next_ue_stream_id = 1; // Next UE SCTP stream identifier uint16_t next_ue_stream_id = 1; // Next UE SCTP stream identifier
srsran::unique_timer mme_connect_timer, s1setup_timeout; srsran::unique_timer mme_connect_timer, s1setup_timeout;
// Protocol IEs sent with every UL S1AP message // Protocol IEs sent with every UL S1AP message
asn1::s1ap::tai_s tai; asn1::s1ap::tai_s tai;

@ -211,7 +211,7 @@ void s1ap::s1_setup_proc_t::then(const srsran::proc_state_t& result) const
s1ap_ptr->mme_connect_timer.duration() / 1000); s1ap_ptr->mme_connect_timer.duration() / 1000);
s1ap_ptr->mme_connect_timer.run(); s1ap_ptr->mme_connect_timer.run();
s1ap_ptr->stack->remove_mme_socket(s1ap_ptr->s1ap_socket.get_socket()); s1ap_ptr->stack->remove_mme_socket(s1ap_ptr->s1ap_socket.get_socket());
s1ap_ptr->s1ap_socket.reset(); s1ap_ptr->s1ap_socket.close();
procInfo("S1AP socket closed."); procInfo("S1AP socket closed.");
// Try again with in 10 seconds // Try again with in 10 seconds
} }
@ -263,7 +263,7 @@ int s1ap::init(s1ap_args_t args_, rrc_interface_s1ap* rrc_, srsenb::stack_interf
void s1ap::stop() void s1ap::stop()
{ {
running = false; running = false;
s1ap_socket.reset(); s1ap_socket.close();
} }
void s1ap::get_metrics(s1ap_metrics_t& m) void s1ap::get_metrics(s1ap_metrics_t& m)
@ -499,21 +499,21 @@ bool s1ap::handle_mme_rx_msg(srsran::unique_byte_buffer_t pdu,
logger.info("SCTP Association Shutdown. Association: %d", sri.sinfo_assoc_id); logger.info("SCTP Association Shutdown. Association: %d", sri.sinfo_assoc_id);
srsran::console("SCTP Association Shutdown. Association: %d\n", sri.sinfo_assoc_id); srsran::console("SCTP Association Shutdown. Association: %d\n", sri.sinfo_assoc_id);
stack->remove_mme_socket(s1ap_socket.get_socket()); stack->remove_mme_socket(s1ap_socket.get_socket());
s1ap_socket.reset(); s1ap_socket.close();
} else if (notification->sn_header.sn_type == SCTP_PEER_ADDR_CHANGE && } else if (notification->sn_header.sn_type == SCTP_PEER_ADDR_CHANGE &&
notification->sn_paddr_change.spc_state == SCTP_ADDR_UNREACHABLE) { notification->sn_paddr_change.spc_state == SCTP_ADDR_UNREACHABLE) {
logger.info("SCTP peer addres unreachable. Association: %d", sri.sinfo_assoc_id); logger.info("SCTP peer addres unreachable. Association: %d", sri.sinfo_assoc_id);
srsran::console("SCTP peer address unreachable. Association: %d\n", sri.sinfo_assoc_id); srsran::console("SCTP peer address unreachable. Association: %d\n", sri.sinfo_assoc_id);
stack->remove_mme_socket(s1ap_socket.get_socket()); stack->remove_mme_socket(s1ap_socket.get_socket());
s1ap_socket.reset(); s1ap_socket.close();
} }
} else if (pdu->N_bytes == 0) { } else if (pdu->N_bytes == 0) {
logger.error("SCTP return 0 bytes. Closing socket"); logger.error("SCTP return 0 bytes. Closing socket");
s1ap_socket.reset(); s1ap_socket.close();
} }
// Restart MME connection procedure if we lost connection // Restart MME connection procedure if we lost connection
if (not s1ap_socket.is_init()) { if (not s1ap_socket.is_open()) {
mme_connected = false; mme_connected = false;
if (not s1setup_proc.launch()) { if (not s1setup_proc.launch()) {
logger.error("Failed to initiate MME connection procedure."); logger.error("Failed to initiate MME connection procedure.");

Loading…
Cancel
Save