Skip to content

Commit

Permalink
Set SO_REUSEADDR on the server socket, to allow the agent to bind to …
Browse files Browse the repository at this point in the history
…the listening port after being restarted.
  • Loading branch information
sp193 committed Nov 13, 2019
1 parent f031c8d commit 49a8eff
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 112 deletions.
125 changes: 69 additions & 56 deletions src/cpp/transport/tcp/TCPServerLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,81 +78,94 @@ bool TCPv4Agent::init()

if (-1 != listener_poll_.fd)
{
/* IP and Port setup. */
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(transport_address_.medium_locator().port());
address.sin_addr.s_addr = INADDR_ANY;
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
if (-1 != bind(listener_poll_.fd, (struct sockaddr*)&address, sizeof(address)))
/* Set reuse port. */
int reuse = 1;
if (-1 != setsockopt(listener_poll_.fd, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse)))
{
/* Log. */
UXR_AGENT_LOG_DEBUG(
UXR_DECORATE_GREEN("port opened"),
"port: {}",
transport_address_.medium_locator().port());
/* IP and Port setup. */
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(transport_address_.medium_locator().port());
address.sin_addr.s_addr = INADDR_ANY;
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
if (-1 != bind(listener_poll_.fd, (struct sockaddr*)&address, sizeof(address)))
{
/* Log. */
UXR_AGENT_LOG_DEBUG(
UXR_DECORATE_GREEN("port opened"),
"port: {}",
transport_address_.medium_locator().port());

/* Setup listener poll. */
listener_poll_.events = POLLIN;
/* Setup listener poll. */
listener_poll_.events = POLLIN;

/* Setup connections. */
for (size_t i = 0; i < poll_fds_.size(); ++i)
{
poll_fds_[i].fd = -1;
poll_fds_[i].events = POLLIN;
connections_[i].poll_fd = &poll_fds_[i];
connections_[i].id = uint32_t(i);
connections_[i].active = false;
init_input_buffer(connections_[i].input_buffer);
free_connections_.push_back(connections_[i].id);
}
/* Setup connections. */
for (size_t i = 0; i < poll_fds_.size(); ++i)
{
poll_fds_[i].fd = -1;
poll_fds_[i].events = POLLIN;
connections_[i].poll_fd = &poll_fds_[i];
connections_[i].id = uint32_t(i);
connections_[i].active = false;
init_input_buffer(connections_[i].input_buffer);
free_connections_.push_back(connections_[i].id);
}

/* Init listener. */
if (-1 != listen(listener_poll_.fd, TCP_MAX_BACKLOG_CONNECTIONS))
{
running_cond_ = true;
listener_thread_ = std::thread(&TCPv4Agent::listener_loop, this);

/* Get local address. */
int fd = socket(PF_INET, SOCK_DGRAM, 0);
struct sockaddr_in temp_addr;
temp_addr.sin_family = AF_INET;
temp_addr.sin_port = htons(80);
temp_addr.sin_addr.s_addr = inet_addr("1.2.3.4");
int connected = connect(fd, (struct sockaddr *)&temp_addr, sizeof(temp_addr));
if (0 == connected)
/* Init listener. */
if (-1 != listen(listener_poll_.fd, TCP_MAX_BACKLOG_CONNECTIONS))
{
struct sockaddr local_addr;
socklen_t local_addr_len = sizeof(local_addr);
if (-1 != getsockname(fd, &local_addr, &local_addr_len))
running_cond_ = true;
listener_thread_ = std::thread(&TCPv4Agent::listener_loop, this);

/* Get local address. */
int fd = socket(PF_INET, SOCK_DGRAM, 0);
struct sockaddr_in temp_addr;
temp_addr.sin_family = AF_INET;
temp_addr.sin_port = htons(80);
temp_addr.sin_addr.s_addr = inet_addr("1.2.3.4");
int connected = connect(fd, (struct sockaddr *)&temp_addr, sizeof(temp_addr));
if (0 == connected)
{
transport_address_.medium_locator().address({uint8_t(local_addr.sa_data[2]),
uint8_t(local_addr.sa_data[3]),
uint8_t(local_addr.sa_data[4]),
uint8_t(local_addr.sa_data[5])});
rv = true;
UXR_AGENT_LOG_INFO(
UXR_DECORATE_GREEN("running..."),
"port: {}",
transport_address_.medium_locator().port());
struct sockaddr local_addr;
socklen_t local_addr_len = sizeof(local_addr);
if (-1 != getsockname(fd, &local_addr, &local_addr_len))
{
transport_address_.medium_locator().address({uint8_t(local_addr.sa_data[2]),
uint8_t(local_addr.sa_data[3]),
uint8_t(local_addr.sa_data[4]),
uint8_t(local_addr.sa_data[5])});
rv = true;
UXR_AGENT_LOG_INFO(
UXR_DECORATE_GREEN("running..."),
"port: {}",
transport_address_.medium_locator().port());
}
::close(fd);
}
::close(fd);
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("listen error"),
"port: {}",
transport_address_.medium_locator().port());
}
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("listen error"),
UXR_DECORATE_RED("bind error"),
"port: {}",
transport_address_.medium_locator().port());
}
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("bind error"),
"port: {}",
transport_address_.medium_locator().port());
UXR_DECORATE_RED("socket opt error"),
"Port: {}",
listener_poll_.fd);
return false;
}
}
else
Expand Down
125 changes: 69 additions & 56 deletions src/cpp/transport/tcp/TCPServerWindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,81 +68,94 @@ bool TCPv4Agent::init()

if (INVALID_SOCKET != listener_poll_.fd)
{
/* IP and Port setup. */
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(transport_address_.medium_locator().port());
address.sin_addr.s_addr = INADDR_ANY;
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
if (SOCKET_ERROR != bind(listener_poll_.fd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)))
/* Set reuse port. */
int reuse = 1;
if (-1 != setsockopt(listener_poll_.fd, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse, sizeof(reuse)))
{
/* Log. */
UXR_AGENT_LOG_DEBUG(
UXR_DECORATE_GREEN("port opened"),
"port: {}",
transport_address_.medium_locator().port());
/* IP and Port setup. */
struct sockaddr_in address;
address.sin_family = AF_INET;
address.sin_port = htons(transport_address_.medium_locator().port());
address.sin_addr.s_addr = INADDR_ANY;
memset(address.sin_zero, '\0', sizeof(address.sin_zero));
if (SOCKET_ERROR != bind(listener_poll_.fd, reinterpret_cast<struct sockaddr*>(&address), sizeof(address)))
{
/* Log. */
UXR_AGENT_LOG_DEBUG(
UXR_DECORATE_GREEN("port opened"),
"port: {}",
transport_address_.medium_locator().port());

/* Setup listener poll. */
listener_poll_.events = POLLIN;
/* Setup listener poll. */
listener_poll_.events = POLLIN;

/* Setup connections. */
for (size_t i = 0; i < poll_fds_.size(); ++i)
{
poll_fds_[i].fd = INVALID_SOCKET;
poll_fds_[i].events = POLLIN;
connections_[i].poll_fd = &poll_fds_[i];
connections_[i].id = uint32_t(i);
connections_[i].active = false;
init_input_buffer(connections_[i].input_buffer);
free_connections_.push_back(connections_[i].id);
}
/* Setup connections. */
for (size_t i = 0; i < poll_fds_.size(); ++i)
{
poll_fds_[i].fd = INVALID_SOCKET;
poll_fds_[i].events = POLLIN;
connections_[i].poll_fd = &poll_fds_[i];
connections_[i].id = uint32_t(i);
connections_[i].active = false;
init_input_buffer(connections_[i].input_buffer);
free_connections_.push_back(connections_[i].id);
}

/* Init listener. */
if (SOCKET_ERROR != listen(listener_poll_.fd, TCP_MAX_BACKLOG_CONNECTIONS))
{
running_cond_ = true;
listener_thread_ = std::thread(&TCPv4Agent::listener_loop, this);

/* Get local address. */
SOCKET fd = socket(PF_INET, SOCK_DGRAM, 0);
struct sockaddr_in temp_addr;
temp_addr.sin_family = AF_INET;
temp_addr.sin_port = htons(80);
temp_addr.sin_addr.s_addr = inet_addr("1.2.3.4");
int connected = connect(fd, (struct sockaddr *)&temp_addr, sizeof(temp_addr));
if (0 == connected)
/* Init listener. */
if (SOCKET_ERROR != listen(listener_poll_.fd, TCP_MAX_BACKLOG_CONNECTIONS))
{
struct sockaddr local_addr;
int local_addr_len = sizeof(local_addr);
if (SOCKET_ERROR != getsockname(fd, &local_addr, &local_addr_len))
running_cond_ = true;
listener_thread_ = std::thread(&TCPv4Agent::listener_loop, this);

/* Get local address. */
SOCKET fd = socket(PF_INET, SOCK_DGRAM, 0);
struct sockaddr_in temp_addr;
temp_addr.sin_family = AF_INET;
temp_addr.sin_port = htons(80);
temp_addr.sin_addr.s_addr = inet_addr("1.2.3.4");
int connected = connect(fd, (struct sockaddr *)&temp_addr, sizeof(temp_addr));
if (0 == connected)
{
transport_address_.medium_locator().address({uint8_t(local_addr.sa_data[2]),
uint8_t(local_addr.sa_data[3]),
uint8_t(local_addr.sa_data[4]),
uint8_t(local_addr.sa_data[5])});
rv = true;
UXR_AGENT_LOG_INFO(
UXR_DECORATE_GREEN("running..."),
"port: {}",
transport_address_.medium_locator().port());
struct sockaddr local_addr;
int local_addr_len = sizeof(local_addr);
if (SOCKET_ERROR != getsockname(fd, &local_addr, &local_addr_len))
{
transport_address_.medium_locator().address({uint8_t(local_addr.sa_data[2]),
uint8_t(local_addr.sa_data[3]),
uint8_t(local_addr.sa_data[4]),
uint8_t(local_addr.sa_data[5])});
rv = true;
UXR_AGENT_LOG_INFO(
UXR_DECORATE_GREEN("running..."),
"port: {}",
transport_address_.medium_locator().port());
}
closesocket(fd);
}
closesocket(fd);
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("listen error"),
"port: {}",
transport_address_.medium_locator().port());
}
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("listen error"),
UXR_DECORATE_RED("bind error"),
"port: {}",
transport_address_.medium_locator().port());
}
}
else
{
UXR_AGENT_LOG_ERROR(
UXR_DECORATE_RED("bind error"),
"port: {}",
transport_address_.medium_locator().port());
UXR_DECORATE_RED("socket opt error"),
"Port: {}",
listener_poll_.fd);
return false;
}
}
else
Expand Down

0 comments on commit 49a8eff

Please sign in to comment.