From f3648fefe040152bb1676d651ebf7d836cb8ac9e Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Thu, 19 Feb 2026 00:01:18 +0100 Subject: Refactor: reduce duplication and tighten helpers - Remove redundant applicationUri log in print_application_description - Use UA_SECURITY_POLICY_NONE_URI macro instead of hardcoded string - Extract _s_register_with_lds / _s_deregister_from_lds helpers - Rename signal handler param 'sign' to 'sig' for consistency - Add INT_MIN/INT_MAX bounds check to config_require_int - Extract shared test helpers into tests/test_helpers.sh --- src/common.c | 7 +---- src/config.c | 3 +- src/server_register.c | 86 ++++++++++++++++++++++++++++----------------------- 3 files changed, 50 insertions(+), 46 deletions(-) (limited to 'src') diff --git a/src/common.c b/src/common.c index 234f925..8f141d7 100644 --- a/src/common.c +++ b/src/common.c @@ -365,10 +365,6 @@ print_application_description (const UA_ApplicationDescription *description, UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION, " Name: %.*s", (int)description->applicationName.text.length, description->applicationName.text.data); - UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION, - " Application URI: %.*s", - (int)description->applicationUri.length, - description->applicationUri.data); UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_APPLICATION, " Product URI: %.*s", (int)description->productUri.length, description->productUri.data); @@ -507,8 +503,7 @@ create_unsecure_client_config (UA_ClientConfig *cc, cc->securityMode = UA_MESSAGESECURITYMODE_NONE; UA_String_clear (&cc->securityPolicyUri); - cc->securityPolicyUri = UA_String_fromChars ( - "http://opcfoundation.org/UA/SecurityPolicy#None"); + UA_String_copy (&UA_SECURITY_POLICY_NONE_URI, &cc->securityPolicyUri); if (auth && auth->mode == AUTH_USER) UA_ClientConfig_setAuthenticationUsername (cc, auth->user.username, diff --git a/src/config.c b/src/config.c index 5f4d67a..2cee9d3 100644 --- a/src/config.c +++ b/src/config.c @@ -7,6 +7,7 @@ #include +#include #include #include #include @@ -192,7 +193,7 @@ config_require_int (const config *cfg, const char *key, const char *program) char *endptr; long num = strtol (val, &endptr, 10); - if (*endptr != '\0') + if (*endptr != '\0' || num < INT_MIN || num > INT_MAX) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "%s: config key '%s' is not a valid integer: '%s'", diff --git a/src/server_register.c b/src/server_register.c index de333a5..b675e2f 100644 --- a/src/server_register.c +++ b/src/server_register.c @@ -24,7 +24,7 @@ volatile UA_Boolean g_running = true; static void -_s_stop_handler (int sign) +_s_stop_handler (int sig) { g_running = false; } @@ -66,6 +66,40 @@ _s_make_lds_client_config (UA_ClientConfig *cc, const lds_client_params *p) return UA_STATUSCODE_GOOD; } +/** + * Creates a fresh client config and registers with the LDS. + */ +static UA_StatusCode +_s_register_with_lds (UA_Server *server, const lds_client_params *p, + const char *endpoint) +{ + UA_ClientConfig cc; + UA_StatusCode rv = _s_make_lds_client_config (&cc, p); + if (rv != UA_STATUSCODE_GOOD) + return rv; + UA_String url = UA_STRING_ALLOC (endpoint); + rv = UA_Server_registerDiscovery (server, &cc, url, UA_STRING_NULL); + UA_String_clear (&url); + return rv; +} + +/** + * Creates a fresh client config and deregisters from the LDS. + */ +static UA_StatusCode +_s_deregister_from_lds (UA_Server *server, const lds_client_params *p, + const char *endpoint) +{ + UA_ClientConfig cc; + UA_StatusCode rv = _s_make_lds_client_config (&cc, p); + if (rv != UA_STATUSCODE_GOOD) + return rv; + UA_String url = UA_STRING_ALLOC (endpoint); + rv = UA_Server_deregisterDiscovery (server, &cc, url); + UA_String_clear (&url); + return rv; +} + /* ======================================================================== * Main * ======================================================================== */ @@ -175,20 +209,7 @@ main (int argc, char **argv) can periodically re-register with the LDS between iterations. */ UA_Server_run_startup (server); - /* UA_Server_registerDiscovery consumes (clears) the client config, - so _s_make_lds_client_config builds a fresh one for every call. */ - UA_ClientConfig client_config; - retval = _s_make_lds_client_config (&client_config, &lds_params); - if (retval != UA_STATUSCODE_GOOD) - { - UA_Server_run_shutdown (server); - goto cleanup; - } - - UA_String discovery_url = UA_STRING_ALLOC (discovery_endpoint); - retval = UA_Server_registerDiscovery (server, &client_config, discovery_url, - UA_STRING_NULL); - UA_String_clear (&discovery_url); + retval = _s_register_with_lds (server, &lds_params, discovery_endpoint); if (retval != UA_STATUSCODE_GOOD) UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Initial register failed: %s", @@ -205,36 +226,23 @@ main (int argc, char **argv) time_t now = time (NULL); if (now - last_register >= register_interval) { - retval = _s_make_lds_client_config (&client_config, &lds_params); - if (retval == UA_STATUSCODE_GOOD) - { - UA_String rereg_url = UA_STRING_ALLOC (discovery_endpoint); - retval = UA_Server_registerDiscovery (server, &client_config, - rereg_url, UA_STRING_NULL); - UA_String_clear (&rereg_url); - if (retval != UA_STATUSCODE_GOOD) - UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, - "Re-register failed: %s", - UA_StatusCode_name (retval)); - } + retval + = _s_register_with_lds (server, &lds_params, discovery_endpoint); + if (retval != UA_STATUSCODE_GOOD) + UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, + "Re-register failed: %s", + UA_StatusCode_name (retval)); last_register = now; } } /* Deregister from the LDS before shutting down so the LDS removes our entry immediately rather than waiting for the cleanup timeout. */ - retval = _s_make_lds_client_config (&client_config, &lds_params); - if (retval == UA_STATUSCODE_GOOD) - { - UA_String dereg_url = UA_STRING_ALLOC (discovery_endpoint); - retval - = UA_Server_deregisterDiscovery (server, &client_config, dereg_url); - UA_String_clear (&dereg_url); - if (retval != UA_STATUSCODE_GOOD) - UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, - "Could not unregister from discovery server: %s", - UA_StatusCode_name (retval)); - } + retval = _s_deregister_from_lds (server, &lds_params, discovery_endpoint); + if (retval != UA_STATUSCODE_GOOD) + UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, + "Could not unregister from discovery server: %s", + UA_StatusCode_name (retval)); UA_Server_run_shutdown (server); rc = EXIT_SUCCESS; -- cgit v1.2.3