From 7648a256d97abda40edbdc0d7bf59edd0a09fb95 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Tue, 17 Feb 2026 23:52:06 +0100 Subject: Extract createServer and parseAuthConfig, simplify programs Rename createSecureServer to createServer and add an unsecure path (UA_ServerConfig_setMinimal) when certPath is NULL, eliminating the if/else server creation blocks in server_lds.c and server_register.c. Add parseAuthConfig() to common.c to replace four near-identical authMode parsing blocks across the three programs. Restructure server_register.c error handling with goto cleanup, removing ~20 duplicated cleanup sequences. Rename the CMake library target from DiscoveryCommon to common. --- src/server_lds.c | 70 ++++++++++++-------------------------------------------- 1 file changed, 15 insertions(+), 55 deletions(-) (limited to 'src/server_lds.c') diff --git a/src/server_lds.c b/src/server_lds.c index e3407d5..0a1bf89 100644 --- a/src/server_lds.c +++ b/src/server_lds.c @@ -16,11 +16,9 @@ #include #include #include -#include #include #include -#include UA_Boolean running = true; @@ -63,9 +61,8 @@ main (int argc, char *argv[]) const char *applicationUri = configRequire (&cfg, "applicationUri", "ServerLDS"); int cleanupTimeout = configRequireInt (&cfg, "cleanupTimeout", "ServerLDS"); - const char *authMode = configRequire (&cfg, "authMode", "ServerLDS"); - if (!applicationUri || !authMode || port < 0 || cleanupTimeout < 0) + if (!applicationUri || port < 0 || cleanupTimeout < 0) { configFree (&cfg); return EXIT_FAILURE; @@ -102,67 +99,30 @@ main (int argc, char *argv[]) UA_Boolean allowAnonymous; const char *username = NULL, *password = NULL; - - if (strcmp (authMode, "anonymous") == 0) - { - allowAnonymous = true; - } - else if (strcmp (authMode, "user") == 0) - { - allowAnonymous = false; - username = configRequire (&cfg, "username", "ServerLDS"); - password = configRequire (&cfg, "password", "ServerLDS"); - if (!username || !password) - { - configFree (&cfg); - return EXIT_FAILURE; - } - } - else + if (parseAuthConfig (&cfg, "ServerLDS", &allowAnonymous, &username, + &password) + != 0) { - UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Unknown auth mode: %s " - "(expected 'anonymous' or 'user')", - authMode); configFree (&cfg); return EXIT_FAILURE; } char **trustPaths = NULL; size_t trustSize = 0; - UA_StatusCode retval; - UA_Server *server; - - if (secure) + if (secure && loadTrustStore (trustStore, &trustPaths, &trustSize) != 0) { - if (loadTrustStore (trustStore, &trustPaths, &trustSize) != 0) - { - configFree (&cfg); - return EXIT_FAILURE; - } - server = createSecureServer ((UA_UInt16)port, applicationUri, certPath, - keyPath, trustPaths, trustSize, &retval); - if (!server) - { - freeTrustStore (trustPaths, trustSize); - configFree (&cfg); - return EXIT_FAILURE; - } + configFree (&cfg); + return EXIT_FAILURE; } - else + + UA_StatusCode retval; + UA_Server *server = createServer ((UA_UInt16)port, applicationUri, certPath, + keyPath, trustPaths, trustSize, &retval); + if (!server) { - server = UA_Server_new (); - UA_ServerConfig *config = UA_Server_getConfig (server); - retval = UA_ServerConfig_setMinimal (config, (UA_UInt16)port, NULL); - if (retval != UA_STATUSCODE_GOOD) - { - UA_Server_delete (server); - configFree (&cfg); - return EXIT_FAILURE; - } - UA_String_clear (&config->applicationDescription.applicationUri); - config->applicationDescription.applicationUri - = UA_String_fromChars (applicationUri); + freeTrustStore (trustPaths, trustSize); + configFree (&cfg); + return EXIT_FAILURE; } UA_ServerConfig *serverConfig = UA_Server_getConfig (server); -- cgit v1.2.3