|
|
|
@ -34,8 +34,6 @@ namespace srslte {
|
|
|
|
|
|
|
|
|
|
namespace net_utils {
|
|
|
|
|
|
|
|
|
|
srslte::log_ref net_log{"COMMON"};
|
|
|
|
|
|
|
|
|
|
//! Set IP:port for ipv4
|
|
|
|
|
bool set_sockaddr(sockaddr_in* addr, const char* ip_str, int port)
|
|
|
|
|
{
|
|
|
|
@ -113,7 +111,7 @@ int open_socket(net_utils::addr_family ip_type, net_utils::socket_type socket_ty
|
|
|
|
|
{
|
|
|
|
|
int fd = socket((int)ip_type, (int)socket_type, (int)protocol);
|
|
|
|
|
if (fd == -1) {
|
|
|
|
|
net_log->error("Failed to open %s socket.\n", net_utils::protocol_to_string(protocol));
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to open %s socket.\n", net_utils::protocol_to_string(protocol));
|
|
|
|
|
perror("Could not create socket\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -124,7 +122,7 @@ int open_socket(net_utils::addr_family ip_type, net_utils::socket_type socket_ty
|
|
|
|
|
evnts.sctp_data_io_event = 1;
|
|
|
|
|
evnts.sctp_shutdown_event = 1;
|
|
|
|
|
if (setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof(evnts)) != 0) {
|
|
|
|
|
net_log->error("Failed to subscribe to SCTP_SHUTDOWN event: %s\n", strerror(errno));
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to subscribe to SCTP_SHUTDOWN event: %s\n", strerror(errno));
|
|
|
|
|
perror("setsockopt");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -135,12 +133,13 @@ int open_socket(net_utils::addr_family ip_type, net_utils::socket_type socket_ty
|
|
|
|
|
bool bind_addr(int fd, const sockaddr_in& addr_in)
|
|
|
|
|
{
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
net_log->error("Trying to bind to a closed socket\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Trying to bind to a closed socket\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (bind(fd, (struct sockaddr*)&addr_in, sizeof(addr_in)) != 0) {
|
|
|
|
|
net_log->error("Failed to bind on address %s: %s errno %d\n", get_ip(addr_in).c_str(), strerror(errno), errno);
|
|
|
|
|
srslte::logmap::get("COMMON")->error(
|
|
|
|
|
"Failed to bind on address %s: %s errno %d\n", get_ip(addr_in).c_str(), strerror(errno), errno);
|
|
|
|
|
perror("bind()");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -151,7 +150,7 @@ bool bind_addr(int fd, const char* bind_addr_str, int port, sockaddr_in* addr_re
|
|
|
|
|
{
|
|
|
|
|
sockaddr_in addr_tmp{};
|
|
|
|
|
if (not net_utils::set_sockaddr(&addr_tmp, bind_addr_str, port)) {
|
|
|
|
|
net_log->error("Failed to convert IP address (%s) to sockaddr_in struct\n", bind_addr_str);
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to convert IP address (%s) to sockaddr_in struct\n", bind_addr_str);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
bind_addr(fd, addr_tmp);
|
|
|
|
@ -164,19 +163,19 @@ bool bind_addr(int fd, const char* bind_addr_str, int port, sockaddr_in* addr_re
|
|
|
|
|
bool connect_to(int fd, const char* dest_addr_str, int dest_port, sockaddr_in* dest_sockaddr)
|
|
|
|
|
{
|
|
|
|
|
if (fd < 0) {
|
|
|
|
|
net_log->error("tried to connect to remote address with an invalid socket.\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("tried to connect to remote address with an invalid socket.\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sockaddr_in sockaddr_tmp{};
|
|
|
|
|
if (not net_utils::set_sockaddr(&sockaddr_tmp, dest_addr_str, dest_port)) {
|
|
|
|
|
net_log->error("Error converting IP address (%s) to sockaddr_in structure\n", dest_addr_str);
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Error converting IP address (%s) to sockaddr_in structure\n", dest_addr_str);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
if (dest_sockaddr != nullptr) {
|
|
|
|
|
*dest_sockaddr = sockaddr_tmp;
|
|
|
|
|
}
|
|
|
|
|
if (connect(fd, (const struct sockaddr*)&sockaddr_tmp, sizeof(sockaddr_tmp)) == -1) {
|
|
|
|
|
net_log->error("Failed to establish socket connection to %s\n", dest_addr_str);
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to establish socket connection to %s\n", dest_addr_str);
|
|
|
|
|
perror("connect()");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -241,7 +240,7 @@ bool socket_handler_t::open_socket(net_utils::addr_family ip_type,
|
|
|
|
|
net_utils::protocol_type protocol)
|
|
|
|
|
{
|
|
|
|
|
if (sockfd >= 0) {
|
|
|
|
|
net_utils::net_log->error("Socket is already open.\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Socket is already open.\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
sockfd = net_utils::open_socket(ip_type, socket_type, protocol);
|
|
|
|
@ -278,7 +277,7 @@ bool sctp_init_server(socket_handler_t* socket, net_utils::socket_type socktype,
|
|
|
|
|
}
|
|
|
|
|
// Listen for connections
|
|
|
|
|
if (listen(socket->fd(), SOMAXCONN) != 0) {
|
|
|
|
|
net_log->error("Failed to listen to incoming SCTP connections\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to listen to incoming SCTP connections\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -299,7 +298,7 @@ bool tcp_make_server(socket_handler_t* socket, const char* bind_addr_str, int po
|
|
|
|
|
}
|
|
|
|
|
// Listen for connections
|
|
|
|
|
if (listen(socket->fd(), nof_connections) != 0) {
|
|
|
|
|
net_log->error("Failed to listen to incoming TCP connections\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to listen to incoming TCP connections\n");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
@ -310,7 +309,7 @@ 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) {
|
|
|
|
|
net_log->error("Failed to accept connection\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to accept connection\n");
|
|
|
|
|
perror("accept");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
@ -321,12 +320,12 @@ int tcp_read(int remotefd, void* buf, size_t nbytes)
|
|
|
|
|
{
|
|
|
|
|
int n = ::read(remotefd, buf, nbytes);
|
|
|
|
|
if (n == 0) {
|
|
|
|
|
net_log->info("TCP connection closed\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->info("TCP connection closed\n");
|
|
|
|
|
close(remotefd);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (n == -1) {
|
|
|
|
|
net_log->error("Failed to read from TCP socket.");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to read from TCP socket.");
|
|
|
|
|
perror("TCP read");
|
|
|
|
|
}
|
|
|
|
|
return n;
|
|
|
|
@ -340,7 +339,7 @@ int tcp_send(int remotefd, const void* buf, size_t nbytes)
|
|
|
|
|
while (nbytes_remaining > 0) {
|
|
|
|
|
ssize_t i = ::send(remotefd, ptr, nbytes_remaining, 0);
|
|
|
|
|
if (i < 1) {
|
|
|
|
|
net_log->error("Failed to send data to TCP socket\n");
|
|
|
|
|
srslte::logmap::get("COMMON")->error("Failed to send data to TCP socket\n");
|
|
|
|
|
perror("Error calling send()\n");
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|