srsLTE/lib/include/srslte/common/network_utils.h

166 lines
5.9 KiB
C
Raw Normal View History

/**
2019-11-12 01:36:41 -08:00
*
* \section COPYRIGHT
2019-11-12 01:36:41 -08:00
*
* Copyright 2013-2020 Software Radio Systems Limited
2019-11-12 01:36:41 -08:00
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the distribution.
2019-11-12 01:36:41 -08:00
*
*/
#ifndef SRSLTE_RX_SOCKET_HANDLER_H
#define SRSLTE_RX_SOCKET_HANDLER_H
#include "srslte/common/buffer_pool.h"
#include "srslte/common/threads.h"
#include <arpa/inet.h>
2019-11-12 01:36:41 -08:00
#include <functional>
#include <map>
#include <mutex>
#include <netinet/in.h>
2019-11-12 01:36:41 -08:00
#include <netinet/sctp.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
2019-11-12 01:36:41 -08:00
#include <queue>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> // for the pipe
namespace srslte {
namespace net_utils {
enum class addr_family { ipv4 = AF_INET, ipv6 = AF_INET6 };
enum class socket_type : int { none = -1, datagram = SOCK_DGRAM, stream = SOCK_STREAM, seqpacket = SOCK_SEQPACKET };
enum class protocol_type : int { NONE = -1, SCTP = IPPROTO_SCTP, TCP = IPPROTO_TCP, UDP = IPPROTO_UDP };
enum class ppid_values : uint32_t { S1AP = 18 };
const char* protocol_to_string(protocol_type p);
2019-11-15 09:25:01 -08:00
// Convenience addr functions
bool set_sockaddr(sockaddr_in* addr, const char* ip_str, int port);
2019-11-15 09:25:01 -08:00
bool set_sockaddr(sockaddr_in6* addr, const char* ip_str, int port);
std::string get_ip(const sockaddr_in& addr);
2019-11-15 09:25:01 -08:00
std::string get_ip(const sockaddr_in6& addr);
int get_port(const sockaddr_in& addr);
net_utils::socket_type get_addr_family(int fd);
2019-11-15 09:25:01 -08:00
// Convenience socket functions
2020-03-25 07:57:29 -07:00
int open_socket(net_utils::addr_family ip, net_utils::socket_type socket_type, net_utils::protocol_type protocol);
bool bind_addr(int fd, const sockaddr_in& addr_in);
bool bind_addr(int fd, const char* bind_addr_str, int port, sockaddr_in* addr_result = nullptr);
bool connect_to(int fd, const char* dest_addr_str, int dest_port, sockaddr_in* dest_sockaddr = nullptr);
2019-11-15 09:25:01 -08:00
} // namespace net_utils
2019-11-12 01:36:41 -08:00
/**
* Description: Net socket class with convenience methods for connecting, binding, and opening socket
*/
class socket_handler_t
{
public:
socket_handler_t() = default;
socket_handler_t(const socket_handler_t&) = delete;
socket_handler_t(socket_handler_t&& other) noexcept;
~socket_handler_t();
socket_handler_t& operator=(const socket_handler_t&) = delete;
socket_handler_t& operator =(socket_handler_t&&) noexcept;
void close();
2019-11-12 01:36:41 -08:00
void reset();
bool is_init() const { return sockfd >= 0; }
int fd() const { return sockfd; }
const sockaddr_in& get_addr_in() const { return addr; }
std::string get_ip() const { return net_utils::get_ip(addr); }
net_utils::socket_type get_family() const { return net_utils::get_addr_family(sockfd); }
2020-03-25 07:57:29 -07:00
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 open_socket(net_utils::addr_family ip, net_utils::socket_type socket_type, net_utils::protocol_type protocol);
int get_socket() { return sockfd; };
2019-11-12 01:36:41 -08:00
protected:
sockaddr_in addr = {};
int sockfd = -1;
2019-11-12 01:36:41 -08:00
};
namespace net_utils {
2020-03-25 07:57:29 -07:00
bool sctp_init_client(socket_handler_t* 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);
// TODO: for TCP and UDP
2020-03-25 07:57:29 -07:00
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
/****************************
* Rx multisocket handler
***************************/
2019-11-12 01:36:41 -08:00
/**
* Description - Instantiates a thread that will block waiting for IO from multiple sockets, via a select
* The user can register their own (socket fd, data handler) in this class via the
* add_socket_handler(fd, task) API or its other variants
*/
2019-11-12 01:36:41 -08:00
class rx_multisocket_handler final : public thread
{
public:
// polymorphic callback to handle the socket recv
class recv_task
{
public:
virtual ~recv_task() = default;
virtual bool operator()(int fd) = 0; // returns false, if socket needs to be removed
};
2019-11-15 10:52:17 -08:00
using task_callback_t = std::unique_ptr<recv_task>;
using recvfrom_callback_t = std::function<void(srslte::unique_byte_buffer_t, const sockaddr_in&)>;
using sctp_recv_callback_t =
std::function<void(srslte::unique_byte_buffer_t, const sockaddr_in&, const sctp_sndrcvinfo&, int)>;
2019-11-12 01:36:41 -08:00
rx_multisocket_handler(std::string name_, srslog::basic_logger& logger, int thread_prio = 65);
2019-11-12 01:36:41 -08:00
rx_multisocket_handler(rx_multisocket_handler&&) = delete;
rx_multisocket_handler(const rx_multisocket_handler&) = delete;
rx_multisocket_handler& operator=(const rx_multisocket_handler&) = delete;
rx_multisocket_handler& operator=(rx_multisocket_handler&&) = delete;
2019-11-12 01:36:41 -08:00
~rx_multisocket_handler();
void stop();
bool remove_socket(int fd);
bool add_socket_handler(int fd, task_callback_t handler);
// convenience methods for recv using buffer pool
2019-11-15 10:52:17 -08:00
bool add_socket_pdu_handler(int fd, recvfrom_callback_t pdu_task);
2019-11-15 03:52:08 -08:00
bool add_socket_sctp_pdu_handler(int fd, sctp_recv_callback_t task);
2019-11-12 01:36:41 -08:00
void run_thread() override;
private:
// used to unlock select
struct ctrl_cmd_t {
enum class cmd_id_t { EXIT, NEW_FD, RM_FD };
2019-11-12 01:36:41 -08:00
cmd_id_t cmd = cmd_id_t::EXIT;
int new_fd = -1;
};
bool remove_socket_unprotected(int fd, fd_set* total_fd_set, int* max_fd);
2019-11-12 01:36:41 -08:00
// args
std::string name;
srslog::basic_logger& logger;
2019-11-12 01:36:41 -08:00
// state
std::mutex socket_mutex;
std::map<int, task_callback_t> active_sockets;
bool running = false;
int pipefd[2] = {};
2019-11-12 01:36:41 -08:00
};
} // namespace srslte
#endif // SRSLTE_RX_SOCKET_HANDLER_H