From fcad9e1c47e3c958ec39cc45a48fb11b4c51dd17 Mon Sep 17 00:00:00 2001 From: Joel Anderson Date: Sun, 11 Feb 2024 21:25:51 -0500 Subject: [PATCH] add closed connection error handling --- include/private/config/have_sys_socket.h | 2 +- include/private/config/locale/bg-bg.h | 4 ++++ include/private/config/locale/bn-in.h | 4 ++++ include/private/config/locale/cz-cz.h | 4 ++++ include/private/config/locale/da-dk.h | 4 ++++ include/private/config/locale/de-de.h | 4 ++++ include/private/config/locale/el-gr.h | 4 ++++ include/private/config/locale/en-us.h | 5 ++++- include/private/config/locale/es-es.h | 4 ++++ include/private/config/locale/fr-fr.h | 4 ++++ include/private/config/locale/he-il.h | 4 ++++ include/private/config/locale/hi-in.h | 4 ++++ include/private/config/locale/hu-hu.h | 4 ++++ include/private/config/locale/it-it.h | 4 ++++ include/private/config/locale/pl-pl.h | 4 ++++ include/private/config/locale/pt-br.h | 4 ++++ include/private/config/locale/sk-sk.h | 4 ++++ include/private/config/locale/sq-al.h | 4 ++++ include/private/config/locale/sv-se.h | 4 ++++ include/private/config/locale/sw-ke.h | 4 ++++ include/private/config/locale/tr-tr.h | 4 ++++ include/private/config/locale/zh-cn.h | 4 ++++ include/private/error.h | 22 +++++++++++++++++++++- include/stumpless/error.h | 10 ++++++++-- src/config/have_sys_socket.c | 17 +++++++++++------ src/error.c | 5 +++++ src/target/network.c | 2 +- tools/check_headers/stumpless.yml | 1 + tools/check_headers/stumpless_private.yml | 1 + 29 files changed, 133 insertions(+), 12 deletions(-) diff --git a/include/private/config/have_sys_socket.h b/include/private/config/have_sys_socket.h index e1c0b77c0..ea6c2bd0a 100644 --- a/include/private/config/have_sys_socket.h +++ b/include/private/config/have_sys_socket.h @@ -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 ); diff --git a/include/private/config/locale/bg-bg.h b/include/private/config/locale/bg-bg.h index 2df039e49..ddab339e3 100644 --- a/include/private/config/locale/bg-bg.h +++ b/include/private/config/locale/bg-bg.h @@ -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" diff --git a/include/private/config/locale/bn-in.h b/include/private/config/locale/bn-in.h index c3228c41e..80fdc134b 100644 --- a/include/private/config/locale/bn-in.h +++ b/include/private/config/locale/bn-in.h @@ -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 \ "নির্বাচিত নেটওয়ার্ক প্রোটোকল অসমর্থিত" diff --git a/include/private/config/locale/cz-cz.h b/include/private/config/locale/cz-cz.h index 344ab095d..b6e3d1445 100644 --- a/include/private/config/locale/cz-cz.h +++ b/include/private/config/locale/cz-cz.h @@ -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" diff --git a/include/private/config/locale/da-dk.h b/include/private/config/locale/da-dk.h index ce77bc3fe..9d10011d6 100644 --- a/include/private/config/locale/da-dk.h +++ b/include/private/config/locale/da-dk.h @@ -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" diff --git a/include/private/config/locale/de-de.h b/include/private/config/locale/de-de.h index 1ffdd95da..13a453867 100644 --- a/include/private/config/locale/de-de.h +++ b/include/private/config/locale/de-de.h @@ -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" diff --git a/include/private/config/locale/el-gr.h b/include/private/config/locale/el-gr.h index e7662e2d6..c0709e0a9 100644 --- a/include/private/config/locale/el-gr.h +++ b/include/private/config/locale/el-gr.h @@ -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 \ "το πρωτόκολλο του δικτύου που επιλέχθηκε δεν υποστηρίζεται" diff --git a/include/private/config/locale/en-us.h b/include/private/config/locale/en-us.h index 2a4c15905..60dd38a33 100644 --- a/include/private/config/locale/en-us.h +++ b/include/private/config/locale/en-us.h @@ -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. @@ -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" diff --git a/include/private/config/locale/es-es.h b/include/private/config/locale/es-es.h index 18f10db3e..4371903ba 100644 --- a/include/private/config/locale/es-es.h +++ b/include/private/config/locale/es-es.h @@ -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" diff --git a/include/private/config/locale/fr-fr.h b/include/private/config/locale/fr-fr.h index b61ba32e0..8bdd7fe79 100644 --- a/include/private/config/locale/fr-fr.h +++ b/include/private/config/locale/fr-fr.h @@ -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é" diff --git a/include/private/config/locale/he-il.h b/include/private/config/locale/he-il.h index e441dd7a8..fcbce3ca2 100644 --- a/include/private/config/locale/he-il.h +++ b/include/private/config/locale/he-il.h @@ -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 \ "פרוטוקול הרשת שנבחר אינו נתמך" diff --git a/include/private/config/locale/hi-in.h b/include/private/config/locale/hi-in.h index d42639220..346f362da 100644 --- a/include/private/config/locale/hi-in.h +++ b/include/private/config/locale/hi-in.h @@ -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 \ "चुना गया नेटवर्क प्रोटोकॉल असमर्थित है" diff --git a/include/private/config/locale/hu-hu.h b/include/private/config/locale/hu-hu.h index 404836f1b..a7eb94378 100644 --- a/include/private/config/locale/hu-hu.h +++ b/include/private/config/locale/hu-hu.h @@ -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" diff --git a/include/private/config/locale/it-it.h b/include/private/config/locale/it-it.h index 32d1e8817..37c68a5a8 100644 --- a/include/private/config/locale/it-it.h +++ b/include/private/config/locale/it-it.h @@ -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" diff --git a/include/private/config/locale/pl-pl.h b/include/private/config/locale/pl-pl.h index bce94b7d9..f698265db 100644 --- a/include/private/config/locale/pl-pl.h +++ b/include/private/config/locale/pl-pl.h @@ -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" diff --git a/include/private/config/locale/pt-br.h b/include/private/config/locale/pt-br.h index 2b2865499..34954e1d0 100644 --- a/include/private/config/locale/pt-br.h +++ b/include/private/config/locale/pt-br.h @@ -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" diff --git a/include/private/config/locale/sk-sk.h b/include/private/config/locale/sk-sk.h index 8b948d200..2a6381096 100644 --- a/include/private/config/locale/sk-sk.h +++ b/include/private/config/locale/sk-sk.h @@ -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" diff --git a/include/private/config/locale/sq-al.h b/include/private/config/locale/sq-al.h index d6d04dc88..718682148 100644 --- a/include/private/config/locale/sq-al.h +++ b/include/private/config/locale/sq-al.h @@ -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" diff --git a/include/private/config/locale/sv-se.h b/include/private/config/locale/sv-se.h index d11115ac2..24014eb65 100644 --- a/include/private/config/locale/sv-se.h +++ b/include/private/config/locale/sv-se.h @@ -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" diff --git a/include/private/config/locale/sw-ke.h b/include/private/config/locale/sw-ke.h index c40f7a7cc..e2b1641ae 100644 --- a/include/private/config/locale/sw-ke.h +++ b/include/private/config/locale/sw-ke.h @@ -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" diff --git a/include/private/config/locale/tr-tr.h b/include/private/config/locale/tr-tr.h index 0a394215f..b5841999c 100644 --- a/include/private/config/locale/tr-tr.h +++ b/include/private/config/locale/tr-tr.h @@ -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" diff --git a/include/private/config/locale/zh-cn.h b/include/private/config/locale/zh-cn.h index 4889c84b1..55ad2797a 100644 --- a/include/private/config/locale/zh-cn.h +++ b/include/private/config/locale/zh-cn.h @@ -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 \ "所选网络协议不受支持" diff --git a/include/private/error.h b/include/private/error.h index 595d02102..9a17c5175 100644 --- a/include/private/error.h +++ b/include/private/error.h @@ -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. @@ -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 ); diff --git a/include/stumpless/error.h b/include/stumpless/error.h index dab644c62..c5ce9b08c 100644 --- a/include/stumpless/error.h +++ b/include/stumpless/error.h @@ -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. @@ -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. diff --git a/src/config/have_sys_socket.c b/src/config/have_sys_socket.c index d726aa9a0..207f16247 100644 --- a/src/config/have_sys_socket.c +++ b/src/config/have_sys_socket.c @@ -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 ); @@ -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; } send_result = send( target->handle, @@ -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 diff --git a/src/error.c b/src/error.c index 698db7242..96a3d993e 100644 --- a/src/error.c +++ b/src/error.c @@ -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 ); +} + void raise_network_protocol_unsupported( void ) { raise_error( STUMPLESS_NETWORK_PROTOCOL_UNSUPPORTED, diff --git a/src/target/network.c b/src/target/network.c index 37d03f01b..e6f3e3e8a 100644 --- a/src/target/network.c +++ b/src/target/network.c @@ -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; diff --git a/tools/check_headers/stumpless.yml b/tools/check_headers/stumpless.yml index a609cfb4c..029b1e8e0 100644 --- a/tools/check_headers/stumpless.yml +++ b/tools/check_headers/stumpless.yml @@ -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" diff --git a/tools/check_headers/stumpless_private.yml b/tools/check_headers/stumpless_private.yml index 91149c91c..e0291ad93 100644 --- a/tools/check_headers/stumpless_private.yml +++ b/tools/check_headers/stumpless_private.yml @@ -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"