Skip to content

Commit

Permalink
Merge pull request #109 from ssahani/misc
Browse files Browse the repository at this point in the history
Split out set TCP socket options
  • Loading branch information
ssahani authored May 24, 2024
2 parents 36c2ca1 + 30146ce commit fc47937
Showing 1 changed file with 49 additions and 35 deletions.
84 changes: 49 additions & 35 deletions src/netlog/netlog-network.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,51 @@ int manager_network_connect_socket(Manager *m) {
return 0;
}

static int apply_tcp_socket_options(Manager *m){
int r;

assert(m);
assert(m->socket >= 0);

if (m->no_delay) {
r = setsockopt_int(m->socket, IPPROTO_TCP, TCP_NODELAY, true);
if (r < 0)
log_debug_errno(r, "Failed to enable TCP_NODELAY mode, ignoring: %m");
}

if (m->send_buffer > 0) {
r = fd_set_sndbuf(m->socket, m->send_buffer, false);
if (r < 0)
log_debug_errno(r, "TCP: SO_SNDBUF/SO_SNDBUFFORCE failed: %m");
}

if (m->keep_alive) {
r = setsockopt_int(m->socket, SOL_SOCKET, SO_KEEPALIVE, true);
if (r < 0)
log_debug_errno(r, "Failed to enable SO_KEEPALIVE: %m");
}

if (timestamp_is_set(m->keep_alive_time)) {
r = setsockopt_int(m->socket, SOL_TCP, TCP_KEEPIDLE, m->keep_alive_time / USEC_PER_SEC);
if (r < 0)
log_debug_errno(r, "TCP_KEEPIDLE failed: %m");
}

if (m->keep_alive_interval > 0) {
r = setsockopt_int(m->socket, SOL_TCP, TCP_KEEPINTVL, m->keep_alive_interval / USEC_PER_SEC);
if (r < 0)
log_debug_errno(r, "TCP_KEEPINTVL failed: %m");
}

if (m->keep_alive_cnt > 0) {
r = setsockopt_int(m->socket, SOL_TCP, TCP_KEEPCNT, m->keep_alive_cnt);
if (r < 0)
log_debug_errno(r, "TCP_KEEPCNT failed: %m");
}

return 0;
}

int manager_open_network_socket(Manager *m) {
int r;

Expand Down Expand Up @@ -218,41 +263,10 @@ int manager_open_network_socket(Manager *m) {

break;
case SYSLOG_TRANSMISSION_PROTOCOL_TCP: {
if (m->no_delay) {
r = setsockopt_int(m->socket, IPPROTO_TCP, TCP_NODELAY, true);
if (r < 0)
log_debug_errno(r, "Failed to enable TCP_NODELAY mode, ignoring: %m");
}

if (m->send_buffer > 0) {
r = fd_set_sndbuf(m->socket, m->send_buffer, false);
if (r < 0)
log_debug_errno(r, "TCP: SO_SNDBUF/SO_SNDBUFFORCE failed: %m");
}

if (m->keep_alive) {
r = setsockopt_int(m->socket, SOL_SOCKET, SO_KEEPALIVE, true);
if (r < 0)
log_debug_errno(r, "Failed to enable SO_KEEPALIVE: %m");
}

if (timestamp_is_set(m->keep_alive_time)) {
r = setsockopt_int(m->socket, SOL_TCP, TCP_KEEPIDLE, m->keep_alive_time / USEC_PER_SEC);
if (r < 0)
log_debug_errno(r, "TCP_KEEPIDLE failed: %m");
}

if (m->keep_alive_interval > 0) {
r = setsockopt_int(m->socket, SOL_TCP, TCP_KEEPINTVL, m->keep_alive_interval / USEC_PER_SEC);
if (r < 0)
log_debug_errno(r, "TCP_KEEPINTVL failed: %m");
}

if (m->keep_alive_cnt > 0) {
r = setsockopt_int(m->socket, SOL_TCP, TCP_KEEPCNT, m->keep_alive_cnt);
if (r < 0)
log_debug_errno(r, "TCP_KEEPCNT failed: %m");
}}
r = apply_tcp_socket_options(m);
if (r < 0)
return r;
}
break;
default:
break;
Expand Down

0 comments on commit fc47937

Please sign in to comment.