Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TCP: Reconnect to remote server after a reset/closed connection by #105

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/netlog/netlog-manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ struct Manager {

bool keep_alive;
bool no_delay;
bool connected;

unsigned keep_alive_cnt;

Expand Down
38 changes: 27 additions & 11 deletions src/netlog/netlog-network.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ static int protocol_send(Manager *m, struct iovec *iovec, unsigned n_iovec) {
return r;
}
break;
case SYSLOG_TRANSMISSION_PROTOCOL_TCP:
r = network_send(m, iovec, n_iovec);
if (r < 0 && r != -EAGAIN) {
manager_close_network_socket(m);
return r;
}
break;
default:
return network_send(m, iovec, n_iovec);
}
Expand Down Expand Up @@ -297,9 +304,6 @@ int manager_push_to_network(Manager *m,

assert(m);

if (!message)
return 0;

switch (m->protocol) {
case SYSLOG_TRANSMISSION_PROTOCOL_DTLS:
if (!m->dtls->connected) {
Expand All @@ -316,6 +320,13 @@ int manager_push_to_network(Manager *m,
return r;
}
break;
case SYSLOG_TRANSMISSION_PROTOCOL_TCP:
if (!m->connected) {
r = manager_open_network_socket(m);
if (r < 0)
return r;
}
break;
default:
break;
}
Expand All @@ -340,6 +351,7 @@ void manager_close_network_socket(Manager *m) {
log_error_errno(errno, "Failed to shutdown netlog socket: %m");
}

m->connected = false;
m->socket = safe_close(m->socket);
}

Expand Down Expand Up @@ -377,11 +389,16 @@ int manager_network_connect_socket(Manager *m) {
if (r < 0)
return r;

log_debug("Connecting to remote server: '%s'", pretty);

r = connect(m->socket, &m->address.sockaddr.sa, salen);
if (r < 0 && errno != EINPROGRESS)
return log_error_errno(errno, "Failed to connect to remote server='%s'", pretty);
return log_error_errno(errno, "Failed to connect to remote server='%s': %m", pretty);

log_debug("Connected to remote server: '%s'", pretty);
if (errno != EINPROGRESS)
log_debug("Connected to remote server: '%s'", pretty);
else
log_debug("Connection in progress to remote server: '%s'", pretty);

return 0;
}
Expand All @@ -407,6 +424,8 @@ int manager_open_network_socket(Manager *m) {
if (m->socket < 0)
return log_error_errno(errno, "Failed to create socket: %m");;

log_debug("Successfully created socket with fd='%d'", m->socket);

switch (m->protocol) {
case SYSLOG_TRANSMISSION_PROTOCOL_UDP: {
r = setsockopt_int(m->socket, IPPROTO_IP, IP_MULTICAST_LOOP, true);
Expand Down Expand Up @@ -462,17 +481,14 @@ int manager_open_network_socket(Manager *m) {
}

r = fd_nonblock(m->socket, true);
if (r < 0) {
log_error_errno(errno, "Failed to set socket nonblock: %m");
goto fail;
}
if (r < 0)
log_debug_errno(errno, "Failed to set socket='%d' nonblock: %m", m->socket);

r = manager_network_connect_socket(m);
if (r < 0)
goto fail;

log_debug("Successfully created socket with fd='%d'", m->socket);

m->connected = true;
return m->socket;

fail:
Expand Down