Skip to content

Commit

Permalink
add closed connection error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
goatshriek committed Feb 12, 2024
1 parent 5a5943f commit fcad9e1
Show file tree
Hide file tree
Showing 29 changed files with 133 additions and 12 deletions.
2 changes: 1 addition & 1 deletion include/private/config/have_sys_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ sys_socket_reopen_udp6_target( struct network_target *target );
* error is encountered, an error code is set appropriately.
*/
int
sys_socket_sendto_tcp_target( const struct network_target *target,
sys_socket_sendto_tcp_target( struct network_target *target,
const char *msg,
size_t msg_size );

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/bg-bg.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

// todo translate
# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"chosen network protocol is unsupported"
Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/bn-in.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"নির্বাচিত নেটওয়ার্ক প্রোটোকল অসমর্থিত"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/cz-cz.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"vybraný síťový protokol není podporován"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/da-dk.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] og inet_pton fejlede i at løse navnet"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"valgte nætværks protokol er ikke støttet"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/de-de.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

// todo translate
# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"chosen network protocol is unsupported"
Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/el-gr.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"το πρωτόκολλο του δικτύου που επιλέχθηκε δεν υποστηρίζεται"

Expand Down
5 changes: 4 additions & 1 deletion include/private/config/locale/en-us.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: Apache-2.0 */

/*
* Copyright 2020-2023 Joel E. Anderson
* Copyright 2020-2024 Joel E. Anderson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -158,6 +158,9 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"chosen network protocol is unsupported"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/es-es.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] y inet_pton fallaron en resolver el nombre"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"protocolo de red elegido no soportado"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/fr-fr.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] et inet_pton échec de résolvez le nom"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"le protocol réseaux choisi n'est pas supporté"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/he-il.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"לא הצליחו לפתור שם זה gethostbyname[2] וגם inet_pton"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"פרוטוקול הרשת שנבחר אינו נתמך"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/hi-in.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"चुना गया नेटवर्क प्रोटोकॉल असमर्थित है"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/hu-hu.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] és inet_pton nem tudta feloldani a nevet"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"a kiválasztott hálózati protokoll nem támogatott"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/it-it.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] e inet_pton fallita realizzare il nome"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"il protocollo di rete non è supportato"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/pl-pl.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"wybrany protokół sieciowy nie jest obsługiwany"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/pt-br.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"o protocolo de rede escolhido não é suportado"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/sk-sk.h
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

// todo translate
# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"chosen network protocol is unsupported"
Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/sq-al.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] dhe inet_pton dështuan të zgjidhin emrin"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"protokolli i rrjetës i zgjedhur nuk përkrahet"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/sv-se.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

// todo translate
# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"chosen network protocol is unsupported"
Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/sw-ke.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] na inet_pton imeshindwa kutatua jina"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"malengo ya mtandao hayangwi mkono na ujenzi huu"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/tr-tr.h
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] ve inet_pton ismi çözümleyemedi"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"seçilen ağ protokolü desteklenmiyor"

Expand Down
4 changes: 4 additions & 0 deletions include/private/config/locale/zh-cn.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@
# define L10N_NAME_RESOLUTION_FAILED_ERROR_MESSAGE \
"gethostbyname[2] and inet_pton failed to resolve the name"

// todo translate
# define L10N_NETWORK_CLOSED_ERROR_MESSAGE \
"the network connection is closed"

# define L10N_NETWORK_PROTOCOL_UNSUPPORTED_ERROR_MESSAGE \
"所选网络协议不受支持"

Expand Down
22 changes: 21 additions & 1 deletion include/private/error.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: Apache-2.0 */

/*
* Copyright 2018-2023 Joel E. Anderson
* Copyright 2018-2024 Joel E. Anderson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -182,6 +182,26 @@ COLD_FUNCTION
void
raise_memory_allocation_failure( void );

/**
* Raises an error indicating a network connection is closed.
*
* **Thread Safety: MT-Safe**
* This function is thread safe.
*
* **Async Signal Safety: AS-Unsafe**
* This function is not safe to call from signal handlers due to the use of
* a thread-global structure to store the error.
*
* **Async Cancel Safety: AC-Unsafe**
* This function is not safe to call from threads that may be asynchronously
* cancelled, due to the use of a thread-global structure to store the error.
*
* @param message The message to assign to the error.
*/
COLD_FUNCTION
void
raise_network_closed( const char *message );

COLD_FUNCTION
void
raise_network_protocol_unsupported( void );
Expand Down
10 changes: 8 additions & 2 deletions include/stumpless/error.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* SPDX-License-Identifier: Apache-2.0 */

/*
* Copyright 2018-2023 Joel E. Anderson
* Copyright 2018-2024 Joel E. Anderson
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -150,7 +150,13 @@ extern "C" {
*
* @since release v2.2.0
*/\
ERROR( STUMPLESS_SQLITE3_FAILURE, 32 )
ERROR( STUMPLESS_SQLITE3_FAILURE, 32 ) \
/**
* The network connection requested has been closed.
*
* @since release v2.2.0
*/\
ERROR( STUMPLESS_NETWORK_CLOSED, 33 )

/**
* An (enum) identifier of the types of errors that might be encountered.
Expand Down
17 changes: 11 additions & 6 deletions src/config/have_sys_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,14 @@ sys_socket_reopen_udp6_target( struct network_target *target ) {
}

int
sys_socket_sendto_tcp_target( const struct network_target *target,
sys_socket_sendto_tcp_target( struct network_target *target,
const char *msg,
size_t msg_size ) {
ssize_t recv_result;
char recv_buffer[1];
ssize_t send_result;
size_t sent_bytes = 0;
int result = 1;

lock_network_target( target );

Expand All @@ -193,8 +194,11 @@ sys_socket_sendto_tcp_target( const struct network_target *target,
// check to see if the remote end has sent a FIN
recv_result = recv( target->handle, recv_buffer, 1, MSG_DONTWAIT );
if( recv_result == 0 ){
// TODO raise error for connection closed and close the target
return -1;
raise_network_closed( L10N_NETWORK_CLOSED_ERROR_MESSAGE );
close( target->handle );
target->handle = -1;
result = -1;
goto cleanup_and_return;

Check warning on line 201 in src/config/have_sys_socket.c

View check run for this annotation

Codecov / codecov/patch

src/config/have_sys_socket.c#L197-L201

Added lines #L197 - L201 were not covered by tests
}

send_result = send( target->handle,
Expand All @@ -203,18 +207,19 @@ sys_socket_sendto_tcp_target( const struct network_target *target,
MSG_NOSIGNAL );

if( unlikely( send_result == -1 ) ){
unlock_network_target( target );
raise_socket_send_failure( L10N_SEND_SYS_SOCKET_FAILED_ERROR_MESSAGE,
errno,
L10N_ERRNO_ERROR_CODE_TYPE );
return -1;
result = -1;
goto cleanup_and_return;
}

sent_bytes += send_result;
}

cleanup_and_return:
unlock_network_target( target );
return 1;
return result;
}

int
Expand Down
5 changes: 5 additions & 0 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ raise_memory_allocation_failure( void ) {
NULL );
}

void
raise_network_closed( const char *message ) {
raise_error( STUMPLESS_NETWORK_CLOSED, message, 0, NULL );

Check warning on line 282 in src/error.c

View check run for this annotation

Codecov / codecov/patch

src/error.c#L281-L282

Added lines #L281 - L282 were not covered by tests
}

void
raise_network_protocol_unsupported( void ) {
raise_error( STUMPLESS_NETWORK_PROTOCOL_UNSUPPORTED,
Expand Down
2 changes: 1 addition & 1 deletion src/target/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ sendto_tcp_target( struct network_target *target,

static
int
sendto_udp_target( struct network_target *target,
sendto_udp_target( const struct network_target *target,
const char *msg,
size_t msg_length ) {
size_t effective_length;
Expand Down
1 change: 1 addition & 0 deletions tools/check_headers/stumpless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,7 @@
"STUMPLESS_MAX_MSGID_LENGTH": "stumpless/entry.h"
"STUMPLESS_MAX_PARAM_NAME_LENGTH": "stumpless/param.h"
"STUMPLESS_MINOR_VERSION": "stumpless/config.h"
"STUMPLESS_NETWORK_CLOSED": "stumpless/error.h"
"stumpless_network_protocol": "stumpless/target/network.h"
"STUMPLESS_NETWORK_PROTOCOL_UNSUPPORTED": "stumpless/error.h"
"STUMPLESS_NETWORK_TARGET": "stumpless/target.h"
Expand Down
1 change: 1 addition & 0 deletions tools/check_headers/stumpless_private.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@
"raise_gethostname_failure": "private/error.h"
"raise_journald_failure": "private/error.h"
"raise_mb_conversion_failure": "private/error.h"
"raise_network_closed": "private/error.h"
"raise_resolve_hostname_failure": "private/error.h"
"raise_sqlite3_busy": "private/error.h"
"raise_sqlite3_failure": "private/error.h"
Expand Down

0 comments on commit fcad9e1

Please sign in to comment.