From 3d30c8499ae37ca0ff837e9deaad359de0297765 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Wed, 18 Feb 2026 23:23:44 +0100 Subject: Rename all identifiers to strict Linux snake_case MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Types PascalCase→snake_case, functions camelCase→snake_case, static functions get _s_ prefix, globals get g_ prefix, struct members and locals to snake_case. --- src/client.c | 132 ++++++++++++++-------------- src/common.c | 237 ++++++++++++++++++++++++++------------------------ src/common.h | 129 +++++++++++++-------------- src/config.c | 55 ++++++------ src/config.h | 33 +++---- src/server_lds.c | 86 +++++++++--------- src/server_register.c | 157 ++++++++++++++++----------------- 7 files changed, 422 insertions(+), 407 deletions(-) (limited to 'src') diff --git a/src/client.c b/src/client.c index 97a9289..35a3e6f 100644 --- a/src/client.c +++ b/src/client.c @@ -35,10 +35,10 @@ typedef enum OP_READ_TIME, OP_DOWNLOAD_CERT, OP_INVALID -} Operation; +} operation; -static Operation -parseOperation (const char *name) +static operation +_s_parse_operation (const char *name) { if (strcmp (name, "find-servers") == 0) return OP_FIND_SERVERS; @@ -61,13 +61,13 @@ parseOperation (const char *name) * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. */ static int -opFindServers (UA_Client *client, const char *url) +_s_op_find_servers (UA_Client *client, const char *url) { - size_t arraySize = 0; + size_t array_size = 0; UA_ApplicationDescription *array = NULL; UA_StatusCode retval = UA_Client_findServers (client, url, 0, NULL, 0, NULL, - &arraySize, &array); + &array_size, &array); if (retval != UA_STATUSCODE_GOOD) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, @@ -75,10 +75,10 @@ opFindServers (UA_Client *client, const char *url) return EXIT_FAILURE; } - for (size_t i = 0; i < arraySize; i++) - printApplicationDescription (&array[i], i); + for (size_t i = 0; i < array_size; i++) + print_application_description (&array[i], i); - UA_Array_delete (array, arraySize, + UA_Array_delete (array, array_size, &UA_TYPES[UA_TYPES_APPLICATIONDESCRIPTION]); return EXIT_SUCCESS; } @@ -89,13 +89,13 @@ opFindServers (UA_Client *client, const char *url) * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. */ static int -opGetEndpoints (UA_Client *client, const char *url) +_s_op_get_endpoints (UA_Client *client, const char *url) { - size_t arraySize = 0; + size_t array_size = 0; UA_EndpointDescription *array = NULL; UA_StatusCode retval - = UA_Client_getEndpoints (client, url, &arraySize, &array); + = UA_Client_getEndpoints (client, url, &array_size, &array); if (retval != UA_STATUSCODE_GOOD) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, @@ -103,10 +103,10 @@ opGetEndpoints (UA_Client *client, const char *url) return EXIT_FAILURE; } - for (size_t i = 0; i < arraySize; i++) - printEndpoint (&array[i], i); + for (size_t i = 0; i < array_size; i++) + print_endpoint (&array[i], i); - UA_Array_delete (array, arraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); + UA_Array_delete (array, array_size, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); return EXIT_SUCCESS; } @@ -119,7 +119,7 @@ opGetEndpoints (UA_Client *client, const char *url) * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. */ static int -opReadTime (UA_Client *client, const char *url) +_s_op_read_time (UA_Client *client, const char *url) { UA_StatusCode retval = UA_Client_connect (client, url); @@ -167,13 +167,14 @@ opReadTime (UA_Client *client, const char *url) * @return EXIT_SUCCESS on success, EXIT_FAILURE otherwise. */ static int -opDownloadCert (UA_Client *client, const char *url, const char *outputPath) +_s_op_download_cert (UA_Client *client, const char *url, + const char *output_path) { - size_t arraySize = 0; + size_t array_size = 0; UA_EndpointDescription *array = NULL; UA_StatusCode retval - = UA_Client_getEndpoints (client, url, &arraySize, &array); + = UA_Client_getEndpoints (client, url, &array_size, &array); if (retval != UA_STATUSCODE_GOOD) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, @@ -182,7 +183,7 @@ opDownloadCert (UA_Client *client, const char *url, const char *outputPath) } UA_ByteString *cert = NULL; - for (size_t i = 0; i < arraySize; i++) + for (size_t i = 0; i < array_size; i++) { if (array[i].serverCertificate.length > 0) { @@ -195,17 +196,17 @@ opDownloadCert (UA_Client *client, const char *url, const char *outputPath) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, "No endpoint returned a server certificate"); - UA_Array_delete (array, arraySize, + UA_Array_delete (array, array_size, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); return EXIT_FAILURE; } - FILE *fp = fopen (outputPath, "wb"); + FILE *fp = fopen (output_path, "wb"); if (!fp) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, - "Could not open output file: %s", outputPath); - UA_Array_delete (array, arraySize, + "Could not open output file: %s", output_path); + UA_Array_delete (array, array_size, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); return EXIT_FAILURE; } @@ -224,12 +225,12 @@ opDownloadCert (UA_Client *client, const char *url, const char *outputPath) else { UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_CLIENT, - "Certificate saved to %s (%zu bytes)", outputPath, + "Certificate saved to %s (%zu bytes)", output_path, cert->length); rc = EXIT_SUCCESS; } - UA_Array_delete (array, arraySize, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); + UA_Array_delete (array, array_size, &UA_TYPES[UA_TYPES_ENDPOINTDESCRIPTION]); return rc; } @@ -253,7 +254,7 @@ main (int argc, char **argv) return EXIT_FAILURE; } - Operation op = parseOperation (argv[2]); + operation op = _s_parse_operation (argv[2]); if (op == OP_INVALID) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, @@ -263,9 +264,9 @@ main (int argc, char **argv) return EXIT_FAILURE; } - const char *endpointUrl = argv[3]; - const char *outputPath = NULL; - const char *logLevelStr = "info"; + const char *endpoint_url = argv[3]; + const char *output_path = NULL; + const char *log_level_str = "info"; if (op == OP_DOWNLOAD_CERT) { @@ -277,8 +278,8 @@ main (int argc, char **argv) argv[0]); return EXIT_FAILURE; } - outputPath = argv[4]; - logLevelStr = (argc == 6) ? argv[5] : "info"; + output_path = argv[4]; + log_level_str = (argc == 6) ? argv[5] : "info"; } else { @@ -290,47 +291,47 @@ main (int argc, char **argv) argv[0]); return EXIT_FAILURE; } - logLevelStr = (argc == 5) ? argv[4] : "info"; + log_level_str = (argc == 5) ? argv[4] : "info"; } - int logLevel = parseLogLevel (logLevelStr); - if (logLevel < 0) + int log_level = parse_log_level (log_level_str); + if (log_level < 0) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Unknown log level: %s " "(expected trace, debug, info, warning, error, fatal)", - logLevelStr); + log_level_str); return EXIT_FAILURE; } - Config cfg; - if (configLoad (argv[1], &cfg) != 0) + config cfg; + if (config_load (argv[1], &cfg) != 0) return EXIT_FAILURE; /* ---- Common config keys ---- */ - const char *applicationUri - = configRequire (&cfg, "applicationUri", "Client"); - if (!applicationUri) + const char *application_uri + = config_require (&cfg, "applicationUri", "Client"); + if (!application_uri) { - configFree (&cfg); + config_free (&cfg); return EXIT_FAILURE; } - SecurityConfig sec; - if (parseSecurityConfig (&cfg, "Client", true, &sec) != 0) + security_config sec; + if (parse_security_config (&cfg, "Client", true, &sec) != 0) { - configFree (&cfg); + config_free (&cfg); return EXIT_FAILURE; } /* ---- Auth config (read-time only) ---- */ - AuthConfig auth = { .mode = AUTH_ANONYMOUS }; + auth_config auth = { .mode = AUTH_ANONYMOUS }; - if (op == OP_READ_TIME && parseAuthConfig (&cfg, "Client", &auth) != 0) + if (op == OP_READ_TIME && parse_auth_config (&cfg, "Client", &auth) != 0) { - freeTrustStore (sec.trustPaths, sec.trustSize); - configFree (&cfg); + free_trust_store (sec.trust_paths, sec.trust_size); + config_free (&cfg); return EXIT_FAILURE; } @@ -340,24 +341,25 @@ main (int argc, char **argv) UA_StatusCode retval; if (op == OP_DOWNLOAD_CERT) - retval = createUnsecureClientConfig (UA_Client_getConfig (client), - applicationUri, NULL); - else if (sec.certPath) - retval = createSecureClientConfig (UA_Client_getConfig (client), - applicationUri, &sec, &auth); + retval = create_unsecure_client_config (UA_Client_getConfig (client), + application_uri, NULL); + else if (sec.cert_path) + retval = create_secure_client_config (UA_Client_getConfig (client), + application_uri, &sec, &auth); else - retval = createUnsecureClientConfig (UA_Client_getConfig (client), - applicationUri, &auth); + retval = create_unsecure_client_config (UA_Client_getConfig (client), + application_uri, &auth); if (retval != UA_STATUSCODE_GOOD) { UA_Client_delete (client); - freeTrustStore (sec.trustPaths, sec.trustSize); - configFree (&cfg); + free_trust_store (sec.trust_paths, sec.trust_size); + config_free (&cfg); return EXIT_FAILURE; } - UA_Client_getConfig (client)->logging->context = (void *)(uintptr_t)logLevel; + UA_Client_getConfig (client)->logging->context + = (void *)(uintptr_t)log_level; /* ---- Dispatch operation ---- */ @@ -365,16 +367,16 @@ main (int argc, char **argv) switch (op) { case OP_FIND_SERVERS: - rc = opFindServers (client, endpointUrl); + rc = _s_op_find_servers (client, endpoint_url); break; case OP_GET_ENDPOINTS: - rc = opGetEndpoints (client, endpointUrl); + rc = _s_op_get_endpoints (client, endpoint_url); break; case OP_READ_TIME: - rc = opReadTime (client, endpointUrl); + rc = _s_op_read_time (client, endpoint_url); break; case OP_DOWNLOAD_CERT: - rc = opDownloadCert (client, endpointUrl, outputPath); + rc = _s_op_download_cert (client, endpoint_url, output_path); break; default: rc = EXIT_FAILURE; @@ -384,8 +386,8 @@ main (int argc, char **argv) /* ---- Cleanup ---- */ UA_Client_delete (client); - freeTrustStore (sec.trustPaths, sec.trustSize); - configFree (&cfg); + free_trust_store (sec.trust_paths, sec.trust_size); + config_free (&cfg); return rc; } diff --git a/src/common.c b/src/common.c index 9401392..234f925 100644 --- a/src/common.c +++ b/src/common.c @@ -21,40 +21,40 @@ * ======================================================================== */ UA_ByteString -loadFile (const char *const path) +load_file (const char *const path) { - UA_ByteString fileContents = UA_STRING_NULL; + UA_ByteString file_contents = UA_STRING_NULL; FILE *fp = fopen (path, "rb"); if (!fp) { - /* fopen sets errno on failure. Callers like createServer use - loadFile for optional trustlist entries where a missing file is not + /* fopen sets errno on failure. Callers like create_server use + load_file for optional trustlist entries where a missing file is not an error. Clear errno so open62541's logging does not pick up a stale value and emit misleading error messages. */ errno = 0; - return fileContents; + return file_contents; } fseek (fp, 0, SEEK_END); - fileContents.length = (size_t)ftell (fp); - fileContents.data - = (UA_Byte *)UA_malloc (fileContents.length * sizeof (UA_Byte)); - if (fileContents.data) + file_contents.length = (size_t)ftell (fp); + file_contents.data + = (UA_Byte *)UA_malloc (file_contents.length * sizeof (UA_Byte)); + if (file_contents.data) { fseek (fp, 0, SEEK_SET); - size_t read = fread (fileContents.data, sizeof (UA_Byte), - fileContents.length, fp); - if (read != fileContents.length) - UA_ByteString_clear (&fileContents); + size_t read = fread (file_contents.data, sizeof (UA_Byte), + file_contents.length, fp); + if (read != file_contents.length) + UA_ByteString_clear (&file_contents); } else { - fileContents.length = 0; + file_contents.length = 0; } fclose (fp); - return fileContents; + return file_contents; } /* ======================================================================== @@ -62,16 +62,16 @@ loadFile (const char *const path) * ======================================================================== */ int -loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) +load_trust_store (const char *dir_path, char ***out_paths, size_t *out_size) { - *outPaths = NULL; - *outSize = 0; + *out_paths = NULL; + *out_size = 0; - DIR *dir = opendir (dirPath); + DIR *dir = opendir (dir_path); if (!dir) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Cannot open trust store directory '%s'", dirPath); + "Cannot open trust store directory '%s'", dir_path); return -1; } @@ -82,7 +82,7 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) if (!paths) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "loadTrustStore: out of memory"); + "load_trust_store: out of memory"); goto cleanup; } @@ -90,9 +90,9 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) while ((entry = readdir (dir)) != NULL) { const char *name = entry->d_name; - size_t nameLen = strlen (name); + size_t name_len = strlen (name); /* Skip entries that are not *.der files. 5 = strlen("x.der"). */ - if (nameLen < 5 || strcmp (name + nameLen - 4, ".der") != 0) + if (name_len < 5 || strcmp (name + name_len - 4, ".der") != 0) continue; if (count == capacity) @@ -102,23 +102,23 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) if (!tmp) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "loadTrustStore: out of memory"); + "load_trust_store: out of memory"); goto cleanup; } paths = tmp; } - /* Build full path: dirPath/name */ - size_t dirLen = strlen (dirPath); - size_t fullLen = dirLen + 1 + nameLen + 1; - char *full = malloc (fullLen); + /* Build full path: dir_path/name */ + size_t dir_len = strlen (dir_path); + size_t full_len = dir_len + 1 + name_len + 1; + char *full = malloc (full_len); if (!full) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "loadTrustStore: out of memory"); + "load_trust_store: out of memory"); goto cleanup; } - snprintf (full, fullLen, "%s/%s", dirPath, name); + snprintf (full, full_len, "%s/%s", dir_path, name); paths[count++] = full; } @@ -126,8 +126,8 @@ loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize) rc = 0; if (count > 0) { - *outPaths = paths; - *outSize = count; + *out_paths = paths; + *out_size = count; paths = NULL; count = 0; } @@ -141,7 +141,7 @@ cleanup: } void -freeTrustStore (char **paths, size_t size) +free_trust_store (char **paths, size_t size) { for (size_t i = 0; i < size; i++) free (paths[i]); @@ -153,7 +153,7 @@ freeTrustStore (char **paths, size_t size) * ======================================================================== */ int -parseLogLevel (const char *name) +parse_log_level (const char *name) { static const struct { @@ -173,31 +173,31 @@ parseLogLevel (const char *name) } int -parseAuthConfig (const Config *cfg, const char *program, AuthConfig *auth) +parse_auth_config (const config *cfg, const char *program, auth_config *auth) { - const char *authMode = configRequire (cfg, "authMode", program); - if (!authMode) + const char *auth_mode_str = config_require (cfg, "authMode", program); + if (!auth_mode_str) return -1; memset (auth, 0, sizeof (*auth)); - if (strcmp (authMode, "anonymous") == 0) + if (strcmp (auth_mode_str, "anonymous") == 0) { auth->mode = AUTH_ANONYMOUS; return 0; } - if (strcmp (authMode, "user") == 0) + if (strcmp (auth_mode_str, "user") == 0) { auth->mode = AUTH_USER; - auth->user.username = configRequire (cfg, "username", program); - auth->user.password = configRequire (cfg, "password", program); + auth->user.username = config_require (cfg, "username", program); + auth->user.password = config_require (cfg, "password", program); if (!auth->user.username || !auth->user.password) return -1; return 0; } - if (strcmp (authMode, "cert") == 0) + if (strcmp (auth_mode_str, "cert") == 0) { auth->mode = AUTH_CERT; return 0; @@ -206,26 +206,26 @@ parseAuthConfig (const Config *cfg, const char *program, AuthConfig *auth) UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "%s: unknown auth mode '%s' " "(expected 'anonymous', 'user', or 'cert')", - program, authMode); + program, auth_mode_str); return -1; } int -parseSecurityConfig (const Config *cfg, const char *program, - UA_Boolean needsModePolicy, SecurityConfig *sec) +parse_security_config (const config *cfg, const char *program, + UA_Boolean needs_mode_policy, security_config *sec) { memset (sec, 0, sizeof (*sec)); - const char *certPath = configGet (cfg, "certificate"); - const char *keyPath = configGet (cfg, "privateKey"); - const char *trustStore = configGet (cfg, "trustStore"); + const char *cert_path = config_get (cfg, "certificate"); + const char *key_path = config_get (cfg, "privateKey"); + const char *trust_store = config_get (cfg, "trustStore"); UA_Boolean secure - = (certPath != NULL || keyPath != NULL || trustStore != NULL); + = (cert_path != NULL || key_path != NULL || trust_store != NULL); if (!secure) return 0; - if (!certPath || !keyPath || !trustStore) + if (!cert_path || !key_path || !trust_store) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "%s: incomplete security config: certificate, privateKey, " @@ -234,41 +234,44 @@ parseSecurityConfig (const Config *cfg, const char *program, return -1; } - sec->certPath = certPath; - sec->keyPath = keyPath; + sec->cert_path = cert_path; + sec->key_path = key_path; - if (needsModePolicy) + if (needs_mode_policy) { - const char *secModeStr = configRequire (cfg, "securityMode", program); - const char *secPolStr = configRequire (cfg, "securityPolicy", program); - if (!secModeStr || !secPolStr) + const char *sec_mode_str = config_require (cfg, "securityMode", program); + const char *sec_pol_str + = config_require (cfg, "securityPolicy", program); + if (!sec_mode_str || !sec_pol_str) return -1; - sec->securityMode = parseSecurityMode (secModeStr); - if (sec->securityMode == UA_MESSAGESECURITYMODE_INVALID) + sec->security_mode = parse_security_mode (sec_mode_str); + if (sec->security_mode == UA_MESSAGESECURITYMODE_INVALID) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "%s: unknown security mode: %s", program, secModeStr); + "%s: unknown security mode: %s", program, + sec_mode_str); return -1; } - sec->securityPolicyUri = resolveSecurityPolicyUri (secPolStr); - if (!sec->securityPolicyUri) + sec->security_policy_uri = resolve_security_policy_uri (sec_pol_str); + if (!sec->security_policy_uri) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "%s: unknown security policy: %s", program, secPolStr); + "%s: unknown security policy: %s", program, + sec_pol_str); return -1; } } - if (loadTrustStore (trustStore, &sec->trustPaths, &sec->trustSize) != 0) + if (load_trust_store (trust_store, &sec->trust_paths, &sec->trust_size) != 0) return -1; return 0; } UA_MessageSecurityMode -parseSecurityMode (const char *name) +parse_security_mode (const char *name) { if (strcmp (name, "None") == 0) return UA_MESSAGESECURITYMODE_NONE; @@ -280,7 +283,7 @@ parseSecurityMode (const char *name) } const char * -resolveSecurityPolicyUri (const char *shortName) +resolve_security_policy_uri (const char *short_name) { static const struct { @@ -299,7 +302,7 @@ resolveSecurityPolicyUri (const char *shortName) }; for (size_t i = 0; i < sizeof (policies) / sizeof (policies[0]); i++) { - if (strcmp (shortName, policies[i].name) == 0) + if (strcmp (short_name, policies[i].name) == 0) return policies[i].uri; } return NULL; @@ -310,21 +313,21 @@ resolveSecurityPolicyUri (const char *shortName) * ======================================================================== */ UA_StatusCode -configureAccessControl (UA_ServerConfig *config, const AuthConfig *auth) +configure_access_control (UA_ServerConfig *srv_config, const auth_config *auth) { switch (auth->mode) { case AUTH_ANONYMOUS: - return UA_AccessControl_default (config, true, NULL, 0, NULL); + return UA_AccessControl_default (srv_config, true, NULL, 0, NULL); case AUTH_USER: { UA_UsernamePasswordLogin logins[1]; logins[0].username = UA_STRING ((char *)auth->user.username); logins[0].password = UA_STRING ((char *)auth->user.password); - return UA_AccessControl_default (config, false, NULL, 1, logins); + return UA_AccessControl_default (srv_config, false, NULL, 1, logins); } case AUTH_CERT: - return UA_AccessControl_default (config, false, NULL, 0, NULL); + return UA_AccessControl_default (srv_config, false, NULL, 0, NULL); } return UA_STATUSCODE_BADINTERNALERROR; } @@ -334,8 +337,8 @@ configureAccessControl (UA_ServerConfig *config, const AuthConfig *auth) * ======================================================================== */ void -printApplicationDescription (const UA_ApplicationDescription *description, - size_t index) +print_application_description (const UA_ApplicationDescription *description, + size_t index) { const char *type = "Unknown"; switch (description->applicationType) @@ -380,7 +383,7 @@ printApplicationDescription (const UA_ApplicationDescription *description, } void -printEndpoint (const UA_EndpointDescription *endpoint, size_t index) +print_endpoint (const UA_EndpointDescription *endpoint, size_t index) { const char *mode = "Unknown"; switch (endpoint->securityMode) @@ -400,13 +403,13 @@ printEndpoint (const UA_EndpointDescription *endpoint, size_t index) /* Extract policy name after the '#' */ const char *policy = (const char *)endpoint->securityPolicyUri.data; - size_t policyLen = endpoint->securityPolicyUri.length; + size_t policy_len = endpoint->securityPolicyUri.length; for (size_t k = 0; k < endpoint->securityPolicyUri.length; k++) { if (endpoint->securityPolicyUri.data[k] == '#') { policy = (const char *)&endpoint->securityPolicyUri.data[k + 1]; - policyLen = endpoint->securityPolicyUri.length - k - 1; + policy_len = endpoint->securityPolicyUri.length - k - 1; break; } } @@ -415,7 +418,7 @@ printEndpoint (const UA_EndpointDescription *endpoint, size_t index) " [%4lu] %.*s | Level: %3d | %-14s | %.*s", (unsigned long)index, (int)endpoint->endpointUrl.length, endpoint->endpointUrl.data, endpoint->securityLevel, mode, - (int)policyLen, policy); + (int)policy_len, policy); } /* ======================================================================== @@ -423,26 +426,26 @@ printEndpoint (const UA_EndpointDescription *endpoint, size_t index) * ======================================================================== */ UA_Server * -createServer (UA_UInt16 port, const char *applicationUri, - const SecurityConfig *sec, UA_Boolean discovery, - UA_StatusCode *retval) +create_server (UA_UInt16 port, const char *application_uri, + const security_config *sec, UA_Boolean discovery, + UA_StatusCode *retval) { UA_Server *server = UA_Server_new (); - UA_ServerConfig *config = UA_Server_getConfig (server); + UA_ServerConfig *srv_config = UA_Server_getConfig (server); - if (sec && sec->certPath) + if (sec && sec->cert_path) { - UA_ByteString certificate = loadFile (sec->certPath); - UA_ByteString privateKey = loadFile (sec->keyPath); + UA_ByteString certificate = load_file (sec->cert_path); + UA_ByteString private_key = load_file (sec->key_path); /* +1: UA_STACKARRAY requires a strictly positive size for VLA. */ - UA_STACKARRAY (UA_ByteString, trustList, sec->trustSize + 1); - for (size_t i = 0; i < sec->trustSize; i++) - trustList[i] = loadFile (sec->trustPaths[i]); + UA_STACKARRAY (UA_ByteString, trust_list, sec->trust_size + 1); + for (size_t i = 0; i < sec->trust_size; i++) + trust_list[i] = load_file (sec->trust_paths[i]); *retval = UA_ServerConfig_setDefaultWithSecureSecurityPolicies ( - config, port, &certificate, &privateKey, trustList, sec->trustSize, - NULL, 0, NULL, 0); + srv_config, port, &certificate, &private_key, trust_list, + sec->trust_size, NULL, 0, NULL, 0); /* When discovery is true (LDS) add SecurityPolicy#None restricted to discovery services so that unencrypted clients @@ -454,20 +457,20 @@ createServer (UA_UInt16 port, const char *applicationUri, None security policy, no None endpoint. */ if (*retval == UA_STATUSCODE_GOOD && discovery) { - UA_ServerConfig_addSecurityPolicyNone (config, &certificate); - UA_ServerConfig_addEndpoint (config, UA_SECURITY_POLICY_NONE_URI, + UA_ServerConfig_addSecurityPolicyNone (srv_config, &certificate); + UA_ServerConfig_addEndpoint (srv_config, UA_SECURITY_POLICY_NONE_URI, UA_MESSAGESECURITYMODE_NONE); - config->securityPolicyNoneDiscoveryOnly = true; + srv_config->securityPolicyNoneDiscoveryOnly = true; } UA_ByteString_clear (&certificate); - UA_ByteString_clear (&privateKey); - for (size_t i = 0; i < sec->trustSize; i++) - UA_ByteString_clear (&trustList[i]); + UA_ByteString_clear (&private_key); + for (size_t i = 0; i < sec->trust_size; i++) + UA_ByteString_clear (&trust_list[i]); } else { - *retval = UA_ServerConfig_setMinimal (config, port, NULL); + *retval = UA_ServerConfig_setMinimal (srv_config, port, NULL); } if (*retval != UA_STATUSCODE_GOOD) @@ -476,16 +479,17 @@ createServer (UA_UInt16 port, const char *applicationUri, return NULL; } - UA_String_clear (&config->applicationDescription.applicationUri); - config->applicationDescription.applicationUri - = UA_String_fromChars (applicationUri); + UA_String_clear (&srv_config->applicationDescription.applicationUri); + srv_config->applicationDescription.applicationUri + = UA_String_fromChars (application_uri); return server; } UA_StatusCode -createUnsecureClientConfig (UA_ClientConfig *cc, const char *applicationUri, - const AuthConfig *auth) +create_unsecure_client_config (UA_ClientConfig *cc, + const char *application_uri, + const auth_config *auth) { if (auth && auth->mode == AUTH_CERT) { @@ -499,7 +503,7 @@ createUnsecureClientConfig (UA_ClientConfig *cc, const char *applicationUri, return retval; UA_String_clear (&cc->clientDescription.applicationUri); - cc->clientDescription.applicationUri = UA_String_fromChars (applicationUri); + cc->clientDescription.applicationUri = UA_String_fromChars (application_uri); cc->securityMode = UA_MESSAGESECURITYMODE_NONE; UA_String_clear (&cc->securityPolicyUri); @@ -514,19 +518,20 @@ createUnsecureClientConfig (UA_ClientConfig *cc, const char *applicationUri, } UA_StatusCode -createSecureClientConfig (UA_ClientConfig *cc, const char *applicationUri, - const SecurityConfig *sec, const AuthConfig *auth) +create_secure_client_config (UA_ClientConfig *cc, const char *application_uri, + const security_config *sec, + const auth_config *auth) { - UA_ByteString certificate = loadFile (sec->certPath); - UA_ByteString privateKey = loadFile (sec->keyPath); + UA_ByteString certificate = load_file (sec->cert_path); + UA_ByteString private_key = load_file (sec->key_path); /* +1: UA_STACKARRAY requires a strictly positive size for VLA. */ - UA_STACKARRAY (UA_ByteString, trustList, sec->trustSize + 1); - for (size_t i = 0; i < sec->trustSize; i++) - trustList[i] = loadFile (sec->trustPaths[i]); + UA_STACKARRAY (UA_ByteString, trust_list, sec->trust_size + 1); + for (size_t i = 0; i < sec->trust_size; i++) + trust_list[i] = load_file (sec->trust_paths[i]); UA_StatusCode retval = UA_ClientConfig_setDefaultEncryption ( - cc, certificate, privateKey, trustList, sec->trustSize, NULL, 0); + cc, certificate, private_key, trust_list, sec->trust_size, NULL, 0); /* X509 identity token: reuse the application certificate. open62541 requires that the identity cert matches the SecureChannel cert, so @@ -534,12 +539,12 @@ createSecureClientConfig (UA_ClientConfig *cc, const char *applicationUri, buffers since setAuthenticationCert makes its own copy. */ if (retval == UA_STATUSCODE_GOOD && auth && auth->mode == AUTH_CERT) retval - = UA_ClientConfig_setAuthenticationCert (cc, certificate, privateKey); + = UA_ClientConfig_setAuthenticationCert (cc, certificate, private_key); UA_ByteString_clear (&certificate); - UA_ByteString_clear (&privateKey); - for (size_t i = 0; i < sec->trustSize; i++) - UA_ByteString_clear (&trustList[i]); + UA_ByteString_clear (&private_key); + for (size_t i = 0; i < sec->trust_size; i++) + UA_ByteString_clear (&trust_list[i]); if (retval != UA_STATUSCODE_GOOD) { @@ -550,10 +555,10 @@ createSecureClientConfig (UA_ClientConfig *cc, const char *applicationUri, } UA_String_clear (&cc->clientDescription.applicationUri); - cc->clientDescription.applicationUri = UA_String_fromChars (applicationUri); + cc->clientDescription.applicationUri = UA_String_fromChars (application_uri); - cc->securityMode = sec->securityMode; - cc->securityPolicyUri = UA_String_fromChars (sec->securityPolicyUri); + cc->securityMode = sec->security_mode; + cc->securityPolicyUri = UA_String_fromChars (sec->security_policy_uri); if (auth && auth->mode == AUTH_USER) UA_ClientConfig_setAuthenticationUsername (cc, auth->user.username, diff --git a/src/common.h b/src/common.h index 63fa683..0196e06 100644 --- a/src/common.h +++ b/src/common.h @@ -29,19 +29,19 @@ typedef enum AUTH_ANONYMOUS, AUTH_USER, AUTH_CERT -} AuthMode; +} auth_mode; /** * @brief Session-level authentication configuration (tagged union). * * AUTH_ANONYMOUS carries no payload. AUTH_USER carries borrowed pointers - * to username/password strings (owned by the Config that was parsed). + * to username/password strings (owned by the config that was parsed). * AUTH_CERT carries no payload — the application certificate is reused * as the X509 identity token. */ typedef struct { - AuthMode mode; + auth_mode mode; union { struct @@ -50,28 +50,28 @@ typedef struct const char *password; } user; }; -} AuthConfig; +} auth_config; /** * @brief Transport-level security configuration. * * Groups the certificate, private key, trust list, security mode, and - * security policy URI. All pointers are borrowed (owned by Config or - * returned by loadTrustStore / resolveSecurityPolicyUri). The caller - * must free trustPaths with freeTrustStore() when done. + * security policy URI. All pointers are borrowed (owned by config or + * returned by load_trust_store / resolve_security_policy_uri). The caller + * must free trust_paths with free_trust_store() when done. * - * securityMode and securityPolicyUri are only meaningful for client + * security_mode and security_policy_uri are only meaningful for client * configs; server-side callers may leave them zeroed. */ typedef struct { - const char *certPath; - const char *keyPath; - char **trustPaths; - size_t trustSize; - UA_MessageSecurityMode securityMode; - const char *securityPolicyUri; -} SecurityConfig; + const char *cert_path; + const char *key_path; + char **trust_paths; + size_t trust_size; + UA_MessageSecurityMode security_mode; + const char *security_policy_uri; +} security_config; /* ======================================================================== * File Loading @@ -83,30 +83,31 @@ typedef struct * @param path File path to read. * @return The file contents, or UA_BYTESTRING_NULL on error. */ -UA_ByteString loadFile (const char *const path); +UA_ByteString load_file (const char *const path); /** * @brief Collects all *.der file paths from a trust store directory. * * Opens the directory, finds every file ending in ".der", and builds - * heap-allocated full paths (dirPath/filename). The caller must free - * the result with freeTrustStore(). + * heap-allocated full paths (dir_path/filename). The caller must free + * the result with free_trust_store(). * - * @param dirPath Path to the trust store directory. - * @param outPaths Output: heap-allocated array of heap-allocated strings. + * @param dir_path Path to the trust store directory. + * @param out_paths Output: heap-allocated array of heap-allocated strings. * Set to NULL when the directory is empty. - * @param outSize Output: number of entries in outPaths. + * @param out_size Output: number of entries in out_paths. * @return 0 on success, -1 on error (logged via UA_LOG_ERROR). */ -int loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize); +int load_trust_store (const char *dir_path, char ***out_paths, + size_t *out_size); /** - * @brief Frees the array returned by loadTrustStore(). + * @brief Frees the array returned by load_trust_store(). * * @param paths The array of strings (may be NULL). * @param size Number of entries. */ -void freeTrustStore (char **paths, size_t size); +void free_trust_store (char **paths, size_t size); /* ======================================================================== * Parsing Helpers @@ -121,42 +122,43 @@ void freeTrustStore (char **paths, size_t size); * @param name Log-level name string. * @return The matching UA_LogLevel, or -1 if the name is not recognized. */ -int parseLogLevel (const char *name); +int parse_log_level (const char *name); /** * @brief Parses the authMode key from a configuration file. * - * Populates an AuthConfig struct. When authMode is "anonymous", sets + * Populates an auth_config struct. When authMode is "anonymous", sets * mode to AUTH_ANONYMOUS. When "user", sets mode to AUTH_USER and reads * the username/password keys. When "cert", sets mode to AUTH_CERT. * Logs errors internally. * * @param cfg Parsed configuration. * @param program Program name (for error messages). - * @param auth Output: populated AuthConfig. + * @param auth Output: populated auth_config. * @return 0 on success, -1 on error. */ -int parseAuthConfig (const Config *cfg, const char *program, AuthConfig *auth); +int parse_auth_config (const config *cfg, const char *program, + auth_config *auth); /** * @brief Parses security configuration from a config file. * * Reads certificate, privateKey, and trustStore keys. When all three * are omitted, zeroes @p sec and returns 0 (unsecure). When any of the - * three is present, all three are required. When @p needsModePolicy is + * three is present, all three are required. When @p needs_mode_policy is * true, also reads and resolves securityMode and securityPolicy keys. - * Calls loadTrustStore() internally; the caller must free - * sec->trustPaths with freeTrustStore(). + * Calls load_trust_store() internally; the caller must free + * sec->trust_paths with free_trust_store(). * - * @param cfg Parsed configuration. - * @param program Program name (for error messages). - * @param needsModePolicy When true, require securityMode and - * securityPolicy keys (client configs). - * @param sec Output: populated SecurityConfig. + * @param cfg Parsed configuration. + * @param program Program name (for error messages). + * @param needs_mode_policy When true, require securityMode and + * securityPolicy keys (client configs). + * @param sec Output: populated security_config. * @return 0 on success, -1 on error. */ -int parseSecurityConfig (const Config *cfg, const char *program, - UA_Boolean needsModePolicy, SecurityConfig *sec); +int parse_security_config (const config *cfg, const char *program, + UA_Boolean needs_mode_policy, security_config *sec); /** * @brief Parses a security mode name into the corresponding enum value. @@ -167,7 +169,7 @@ int parseSecurityConfig (const Config *cfg, const char *program, * @return The matching UA_MessageSecurityMode, or * UA_MESSAGESECURITYMODE_INVALID if the name is not recognized. */ -UA_MessageSecurityMode parseSecurityMode (const char *name); +UA_MessageSecurityMode parse_security_mode (const char *name); /** * @brief Maps a short security policy name to its full OPC UA URI. @@ -175,10 +177,10 @@ UA_MessageSecurityMode parseSecurityMode (const char *name); * Accepted names: "None", "Basic256Sha256", "Aes256_Sha256_RsaPss", * "Aes128_Sha256_RsaOaep", "ECC_nistP256". * - * @param shortName Short policy name. + * @param short_name Short policy name. * @return The full URI string, or NULL if the name is not recognized. */ -const char *resolveSecurityPolicyUri (const char *shortName); +const char *resolve_security_policy_uri (const char *short_name); /* ======================================================================== * Factory Functions @@ -197,16 +199,16 @@ const char *resolveSecurityPolicyUri (const char *shortName); * (discovery is ignored). The applicationUri is set in both cases. * * @param port Server port number. - * @param applicationUri OPC UA application URI. + * @param application_uri OPC UA application URI. * @param sec Security configuration, or NULL for unsecure. * @param discovery When true and sec is non-NULL, add a None * endpoint restricted to discovery services. * @param retval Output parameter set to the status code on failure. * @return A configured UA_Server, or NULL on error. */ -UA_Server *createServer (UA_UInt16 port, const char *applicationUri, - const SecurityConfig *sec, UA_Boolean discovery, - UA_StatusCode *retval); +UA_Server *create_server (UA_UInt16 port, const char *application_uri, + const security_config *sec, UA_Boolean discovery, + UA_StatusCode *retval); /** * @brief Initializes a UA_ClientConfig without encryption. @@ -218,13 +220,13 @@ UA_Server *createServer (UA_UInt16 port, const char *applicationUri, * authentication. AUTH_CERT returns an error (requires encryption). * * @param cc Pointer to a zero-initialized UA_ClientConfig. - * @param applicationUri OPC UA application URI. + * @param application_uri OPC UA application URI. * @param auth Authentication config, or NULL for anonymous. * @return UA_STATUSCODE_GOOD on success, error code otherwise. */ -UA_StatusCode createUnsecureClientConfig (UA_ClientConfig *cc, - const char *applicationUri, - const AuthConfig *auth); +UA_StatusCode create_unsecure_client_config (UA_ClientConfig *cc, + const char *application_uri, + const auth_config *auth); /** * @brief Initializes a UA_ClientConfig with encryption. @@ -237,32 +239,32 @@ UA_StatusCode createUnsecureClientConfig (UA_ClientConfig *cc, * username/password authentication. Both are mutually exclusive. * * @param cc Pointer to a zero-initialized UA_ClientConfig. - * @param applicationUri OPC UA application URI. + * @param application_uri OPC UA application URI. * @param sec Security configuration (cert, key, trust, mode, policy). * @param auth Authentication config, or NULL for anonymous. * @return UA_STATUSCODE_GOOD on success, error code otherwise. */ -UA_StatusCode createSecureClientConfig (UA_ClientConfig *cc, - const char *applicationUri, - const SecurityConfig *sec, - const AuthConfig *auth); +UA_StatusCode create_secure_client_config (UA_ClientConfig *cc, + const char *application_uri, + const security_config *sec, + const auth_config *auth); /** - * @brief Configures server access control from an AuthConfig. + * @brief Configures server access control from an auth_config. * * UA_ServerConfig_setDefaultWithSecureSecurityPolicies installs * certificate-only authentication by default. This function * overrides that with the desired policy: anonymous, username/password, * or X509 certificate. For AUTH_CERT the sessionPKI verifier set by - * createServer is preserved, so UA_AccessControl_default automatically + * create_server is preserved, so UA_AccessControl_default automatically * advertises the X509 certificate token policy. * - * @param config Server configuration to modify. - * @param auth Authentication configuration. + * @param srv_config Server configuration to modify. + * @param auth Authentication configuration. * @return UA_STATUSCODE_GOOD on success, error code otherwise. */ -UA_StatusCode configureAccessControl (UA_ServerConfig *config, - const AuthConfig *auth); +UA_StatusCode configure_access_control (UA_ServerConfig *srv_config, + const auth_config *auth); /* ======================================================================== * Output Formatting @@ -277,8 +279,9 @@ UA_StatusCode configureAccessControl (UA_ServerConfig *config, * @param description The application description to print. * @param index Display index (e.g. position in the FindServers result array). */ -void printApplicationDescription (const UA_ApplicationDescription *description, - size_t index); +void +print_application_description (const UA_ApplicationDescription *description, + size_t index); /** * @brief Logs a UA_EndpointDescription in a compact one-line format. @@ -289,6 +292,6 @@ void printApplicationDescription (const UA_ApplicationDescription *description, * @param endpoint The endpoint description to print. * @param index Display index (e.g. position in the GetEndpoints result array). */ -void printEndpoint (const UA_EndpointDescription *endpoint, size_t index); +void print_endpoint (const UA_EndpointDescription *endpoint, size_t index); #endif /* DISCOVERY_COMMON_H */ diff --git a/src/config.c b/src/config.c index 258d167..5f4d67a 100644 --- a/src/config.c +++ b/src/config.c @@ -29,7 +29,7 @@ * whitespace. */ static char * -trim (char *s) +_s_trim (char *s) { while (*s == ' ' || *s == '\t') s++; @@ -48,12 +48,13 @@ trim (char *s) * @return 0 on success, -1 on allocation failure. */ static int -configAppend (Config *cfg, const char *key, const char *value) +_s_config_append (config *cfg, const char *key, const char *value) { if (cfg->count == cfg->capacity) { - size_t newCap = cfg->capacity * 2; - ConfigEntry *tmp = realloc (cfg->entries, newCap * sizeof (ConfigEntry)); + size_t new_cap = cfg->capacity * 2; + config_entry *tmp + = realloc (cfg->entries, new_cap * sizeof (config_entry)); if (!tmp) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, @@ -61,7 +62,7 @@ configAppend (Config *cfg, const char *key, const char *value) return -1; } cfg->entries = tmp; - cfg->capacity = newCap; + cfg->capacity = new_cap; } cfg->entries[cfg->count].key = strdup (key); @@ -83,11 +84,11 @@ configAppend (Config *cfg, const char *key, const char *value) * ======================================================================== */ int -configLoad (const char *path, Config *cfg) +config_load (const char *path, config *cfg) { - memset (cfg, 0, sizeof (Config)); + memset (cfg, 0, sizeof (config)); - cfg->entries = malloc (CONFIG_INITIAL_CAPACITY * sizeof (ConfigEntry)); + cfg->entries = malloc (CONFIG_INITIAL_CAPACITY * sizeof (config_entry)); if (!cfg->entries) { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, @@ -102,16 +103,16 @@ configLoad (const char *path, Config *cfg) UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Config: cannot open '%s'", path); free (cfg->entries); - memset (cfg, 0, sizeof (Config)); + memset (cfg, 0, sizeof (config)); return -1; } char line[CONFIG_LINE_MAX]; - int lineNum = 0; + int line_num = 0; while (fgets (line, sizeof (line), fp)) { - lineNum++; + line_num++; /* Strip trailing newline / carriage return. */ size_t len = strlen (line); @@ -119,7 +120,7 @@ configLoad (const char *path, Config *cfg) line[--len] = '\0'; /* Skip blank lines and comments. */ - char *trimmed = trim (line); + char *trimmed = _s_trim (line); if (*trimmed == '\0' || *trimmed == '#') continue; @@ -130,29 +131,29 @@ configLoad (const char *path, Config *cfg) UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Config: syntax error at %s:%d " "(missing '=')", - path, lineNum); + path, line_num); fclose (fp); - configFree (cfg); + config_free (cfg); return -1; } *eq = '\0'; - char *key = trim (trimmed); - char *value = trim (eq + 1); + char *key = _s_trim (trimmed); + char *value = _s_trim (eq + 1); if (*key == '\0') { UA_LOG_ERROR (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, - "Config: empty key at %s:%d", path, lineNum); + "Config: empty key at %s:%d", path, line_num); fclose (fp); - configFree (cfg); + config_free (cfg); return -1; } - if (configAppend (cfg, key, value) != 0) + if (_s_config_append (cfg, key, value) != 0) { fclose (fp); - configFree (cfg); + config_free (cfg); return -1; } } @@ -162,7 +163,7 @@ configLoad (const char *path, Config *cfg) } const char * -configGet (const Config *cfg, const char *key) +config_get (const config *cfg, const char *key) { for (size_t i = 0; i < cfg->count; i++) { @@ -173,9 +174,9 @@ configGet (const Config *cfg, const char *key) } const char * -configRequire (const Config *cfg, const char *key, const char *program) +config_require (const config *cfg, const char *key, const char *program) { - const char *val = configGet (cfg, key); + const char *val = config_get (cfg, key); if (!val) UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "%s: missing required config key '%s'", program, key); @@ -183,9 +184,9 @@ configRequire (const Config *cfg, const char *key, const char *program) } int -configRequireInt (const Config *cfg, const char *key, const char *program) +config_require_int (const config *cfg, const char *key, const char *program) { - const char *val = configRequire (cfg, key, program); + const char *val = config_require (cfg, key, program); if (!val) return -1; @@ -202,7 +203,7 @@ configRequireInt (const Config *cfg, const char *key, const char *program) } void -configFree (Config *cfg) +config_free (config *cfg) { for (size_t i = 0; i < cfg->count; i++) { @@ -210,5 +211,5 @@ configFree (Config *cfg) free (cfg->entries[i].value); } free (cfg->entries); - memset (cfg, 0, sizeof (Config)); + memset (cfg, 0, sizeof (config)); } diff --git a/src/config.h b/src/config.h index 2c5e364..c35dcd8 100644 --- a/src/config.h +++ b/src/config.h @@ -21,32 +21,32 @@ typedef struct { char *key; char *value; -} ConfigEntry; +} config_entry; /** * @brief A parsed configuration file. * - * Holds a dynamic array of ConfigEntry items. + * Holds a dynamic array of config_entry items. */ typedef struct { - ConfigEntry *entries; + config_entry *entries; size_t count; size_t capacity; -} Config; +} config; /** - * @brief Parses a configuration file into a Config structure. + * @brief Parses a configuration file into a config structure. * * Each non-blank, non-comment line must contain '=' separating key * and value. Whitespace is trimmed around both key and value. * * @param path Path to the configuration file. * @param cfg Output: the parsed configuration. Must be freed - * with configFree() when no longer needed. + * with config_free() when no longer needed. * @return 0 on success, -1 on error (logged via UA_LOG_ERROR). */ -int configLoad (const char *path, Config *cfg); +int config_load (const char *path, config *cfg); /** * @brief Looks up a single-valued key. @@ -58,12 +58,12 @@ int configLoad (const char *path, Config *cfg); * @param key The key to search for. * @return The value string (owned by cfg), or NULL. */ -const char *configGet (const Config *cfg, const char *key); +const char *config_get (const config *cfg, const char *key); /** * @brief Looks up a required single-valued key. * - * Like configGet(), but logs a FATAL error and returns NULL when + * Like config_get(), but logs a FATAL error and returns NULL when * the key is missing. * * @param cfg The parsed configuration. @@ -71,8 +71,8 @@ const char *configGet (const Config *cfg, const char *key); * @param program Program name (for error messages). * @return The value string (owned by cfg), or NULL if missing. */ -const char *configRequire (const Config *cfg, const char *key, - const char *program); +const char *config_require (const config *cfg, const char *key, + const char *program); /** * @brief Looks up a required integer-valued key. @@ -86,16 +86,17 @@ const char *configRequire (const Config *cfg, const char *key, * @param program Program name (for error messages). * @return The parsed integer, or -1 on error. */ -int configRequireInt (const Config *cfg, const char *key, const char *program); +int config_require_int (const config *cfg, const char *key, + const char *program); /** - * @brief Frees all memory owned by a Config structure. + * @brief Frees all memory owned by a config structure. * - * After this call the Config is zeroed and must not be used - * unless configLoad() is called again. + * After this call the config is zeroed and must not be used + * unless config_load() is called again. * * @param cfg The configuration to free. */ -void configFree (Config *cfg); +void config_free (config *cfg); #endif /* DISCOVERY_CONFIG_H */ diff --git a/src/server_lds.c b/src/server_lds.c index 8d34acc..34672ea 100644 --- a/src/server_lds.c +++ b/src/server_lds.c @@ -19,19 +19,19 @@ #include #include -volatile UA_Boolean running = true; +volatile UA_Boolean g_running = true; static void -stopHandler (int sig) +_s_stop_handler (int sig) { - running = false; + g_running = false; } int main (int argc, char *argv[]) { - signal (SIGINT, stopHandler); - signal (SIGTERM, stopHandler); + signal (SIGINT, _s_stop_handler); + signal (SIGTERM, _s_stop_handler); if (argc < 2 || argc > 3) { @@ -40,97 +40,99 @@ main (int argc, char *argv[]) return EXIT_FAILURE; } - const char *logLevelStr = (argc == 3) ? argv[2] : "info"; - int logLevel = parseLogLevel (logLevelStr); - if (logLevel < 0) + const char *log_level_str = (argc == 3) ? argv[2] : "info"; + int log_level = parse_log_level (log_level_str); + if (log_level < 0) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Unknown log level: %s " "(expected trace, debug, info, warning, error, fatal)", - logLevelStr); + log_level_str); return EXIT_FAILURE; } - Config cfg; - if (configLoad (argv[1], &cfg) != 0) + config cfg; + if (config_load (argv[1], &cfg) != 0) return EXIT_FAILURE; - int port = configRequireInt (&cfg, "port", "ServerLDS"); - const char *applicationUri - = configRequire (&cfg, "applicationUri", "ServerLDS"); - int cleanupTimeout = configRequireInt (&cfg, "cleanupTimeout", "ServerLDS"); + int port = config_require_int (&cfg, "port", "ServerLDS"); + const char *application_uri + = config_require (&cfg, "applicationUri", "ServerLDS"); + int cleanup_timeout + = config_require_int (&cfg, "cleanupTimeout", "ServerLDS"); - if (!applicationUri || port < 0 || cleanupTimeout < 0) + if (!application_uri || port < 0 || cleanup_timeout < 0) { - configFree (&cfg); + config_free (&cfg); return EXIT_FAILURE; } /* The OPC UA specification requires the cleanup timeout to exceed the register-server interval. open62541 enforces a floor of 10 seconds. */ - if (cleanupTimeout <= 10) + if (cleanup_timeout <= 10) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Cleanup timeout must be > 10 seconds (got %d)", - cleanupTimeout); - configFree (&cfg); + cleanup_timeout); + config_free (&cfg); return EXIT_FAILURE; } - SecurityConfig sec; - if (parseSecurityConfig (&cfg, "ServerLDS", false, &sec) != 0) + security_config sec; + if (parse_security_config (&cfg, "ServerLDS", false, &sec) != 0) { - configFree (&cfg); + config_free (&cfg); return EXIT_FAILURE; } - AuthConfig auth; - if (parseAuthConfig (&cfg, "ServerLDS", &auth) != 0) + auth_config auth; + if (parse_auth_config (&cfg, "ServerLDS", &auth) != 0) { - freeTrustStore (sec.trustPaths, sec.trustSize); - configFree (&cfg); + free_trust_store (sec.trust_paths, sec.trust_size); + config_free (&cfg); return EXIT_FAILURE; } UA_StatusCode retval; - UA_Server *server = createServer ((UA_UInt16)port, applicationUri, - sec.certPath ? &sec : NULL, true, &retval); + UA_Server *server + = create_server ((UA_UInt16)port, application_uri, + sec.cert_path ? &sec : NULL, true, &retval); if (!server) { - freeTrustStore (sec.trustPaths, sec.trustSize); - configFree (&cfg); + free_trust_store (sec.trust_paths, sec.trust_size); + config_free (&cfg); return EXIT_FAILURE; } - UA_ServerConfig *serverConfig = UA_Server_getConfig (server); - serverConfig->logging->context = (void *)(uintptr_t)logLevel; + UA_ServerConfig *server_config = UA_Server_getConfig (server); + server_config->logging->context = (void *)(uintptr_t)log_level; /* Some OPC UA stacks omit the timestamp in the request header. The default behaviour rejects these requests with BadInvalidTimestamp. Downgrade to a warning so third-party servers can still register. */ - serverConfig->verifyRequestTimestamp = UA_RULEHANDLING_WARN; + server_config->verifyRequestTimestamp = UA_RULEHANDLING_WARN; - retval = configureAccessControl (serverConfig, &auth); + retval = configure_access_control (server_config, &auth); if (retval != UA_STATUSCODE_GOOD) { UA_Server_delete (server); - freeTrustStore (sec.trustPaths, sec.trustSize); - configFree (&cfg); + free_trust_store (sec.trust_paths, sec.trust_size); + config_free (&cfg); return EXIT_FAILURE; } /* Mark this server as a Discovery Server so clients can identify it. */ - serverConfig->applicationDescription.applicationType + server_config->applicationDescription.applicationType = UA_APPLICATIONTYPE_DISCOVERYSERVER; /* Time (seconds) after which stale registrations are removed. Must exceed the registering server's re-register interval. */ - serverConfig->discoveryCleanupTimeout = cleanupTimeout; + server_config->discoveryCleanupTimeout = cleanup_timeout; - retval = UA_Server_run (server, &running); + retval = UA_Server_run (server, &g_running); UA_Server_delete (server); - freeTrustStore (sec.trustPaths, sec.trustSize); - configFree (&cfg); + free_trust_store (sec.trust_paths, sec.trust_size); + config_free (&cfg); return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE; } diff --git a/src/server_register.c b/src/server_register.c index 1514c92..de333a5 100644 --- a/src/server_register.c +++ b/src/server_register.c @@ -21,12 +21,12 @@ #include #include -volatile UA_Boolean running = true; +volatile UA_Boolean g_running = true; static void -stopHandler (int sign) +_s_stop_handler (int sign) { - running = false; + g_running = false; } /* ======================================================================== @@ -39,30 +39,30 @@ stopHandler (int sign) */ typedef struct { - const char *appUri; - SecurityConfig sec; - AuthConfig auth; - int logLevel; -} LdsClientParams; + const char *app_uri; + security_config sec; + auth_config auth; + int log_level; +} lds_client_params; /** - * Builds a fresh UA_ClientConfig from LdsClientParams. + * Builds a fresh UA_ClientConfig from lds_client_params. * * UA_Server_registerDiscovery / deregisterDiscovery consume (clear) the * client config, so a new one must be created before every call. */ static UA_StatusCode -makeLdsClientConfig (UA_ClientConfig *cc, const LdsClientParams *p) +_s_make_lds_client_config (UA_ClientConfig *cc, const lds_client_params *p) { memset (cc, 0, sizeof (UA_ClientConfig)); UA_StatusCode rv; - if (p->sec.certPath) - rv = createSecureClientConfig (cc, p->appUri, &p->sec, &p->auth); + if (p->sec.cert_path) + rv = create_secure_client_config (cc, p->app_uri, &p->sec, &p->auth); else - rv = createUnsecureClientConfig (cc, p->appUri, &p->auth); + rv = create_unsecure_client_config (cc, p->app_uri, &p->auth); if (rv != UA_STATUSCODE_GOOD) return rv; - cc->logging->context = (void *)(uintptr_t)p->logLevel; + cc->logging->context = (void *)(uintptr_t)p->log_level; return UA_STATUSCODE_GOOD; } @@ -73,8 +73,8 @@ makeLdsClientConfig (UA_ClientConfig *cc, const LdsClientParams *p) int main (int argc, char **argv) { - signal (SIGINT, stopHandler); - signal (SIGTERM, stopHandler); + signal (SIGINT, _s_stop_handler); + signal (SIGTERM, _s_stop_handler); if (argc < 4 || argc > 5) { @@ -85,90 +85,90 @@ main (int argc, char **argv) return EXIT_FAILURE; } - const char *discoveryEndpoint = argv[3]; + const char *discovery_endpoint = argv[3]; - const char *logLevelStr = (argc == 5) ? argv[4] : "info"; - int logLevel = parseLogLevel (logLevelStr); - if (logLevel < 0) + const char *log_level_str = (argc == 5) ? argv[4] : "info"; + int log_level = parse_log_level (log_level_str); + if (log_level < 0) { UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND, "Unknown log level: %s " "(expected trace, debug, info, warning, error, fatal)", - logLevelStr); + log_level_str); return EXIT_FAILURE; } /* ── Load server config ─────────────────────────────────────── */ int rc = EXIT_FAILURE; - Config serverCfg = { 0 }; - Config clientCfg = { 0 }; - SecurityConfig serverSec = { 0 }; - SecurityConfig clientSec = { 0 }; + config server_cfg = { 0 }; + config client_cfg = { 0 }; + security_config server_sec = { 0 }; + security_config client_sec = { 0 }; UA_Server *server = NULL; - if (configLoad (argv[1], &serverCfg) != 0) + if (config_load (argv[1], &server_cfg) != 0) goto cleanup; - int port = configRequireInt (&serverCfg, "port", "ServerRegister"); - const char *applicationUri - = configRequire (&serverCfg, "applicationUri", "ServerRegister"); - int registerInterval - = configRequireInt (&serverCfg, "registerInterval", "ServerRegister"); + int port = config_require_int (&server_cfg, "port", "ServerRegister"); + const char *application_uri + = config_require (&server_cfg, "applicationUri", "ServerRegister"); + int register_interval + = config_require_int (&server_cfg, "registerInterval", "ServerRegister"); - if (!applicationUri || port < 0 || registerInterval < 0) + if (!application_uri || port < 0 || register_interval < 0) goto cleanup; - if (parseSecurityConfig (&serverCfg, "ServerRegister", false, &serverSec) + if (parse_security_config (&server_cfg, "ServerRegister", false, &server_sec) != 0) goto cleanup; - AuthConfig serverAuth; - if (parseAuthConfig (&serverCfg, "ServerRegister", &serverAuth) != 0) + auth_config server_auth; + if (parse_auth_config (&server_cfg, "ServerRegister", &server_auth) != 0) goto cleanup; /* ── Load client config ─────────────────────────────────────── */ - if (configLoad (argv[2], &clientCfg) != 0) + if (config_load (argv[2], &client_cfg) != 0) goto cleanup; - const char *clientAppUri - = configRequire (&clientCfg, "applicationUri", "ServerRegister"); - if (!clientAppUri) + const char *client_app_uri + = config_require (&client_cfg, "applicationUri", "ServerRegister"); + if (!client_app_uri) goto cleanup; - if (parseSecurityConfig (&clientCfg, "ServerRegister", true, &clientSec) + if (parse_security_config (&client_cfg, "ServerRegister", true, &client_sec) != 0) goto cleanup; - AuthConfig clientAuth; - if (parseAuthConfig (&clientCfg, "ServerRegister", &clientAuth) != 0) + auth_config client_auth; + if (parse_auth_config (&client_cfg, "ServerRegister", &client_auth) != 0) goto cleanup; /* ── Create and configure server ────────────────────────────── */ UA_StatusCode retval; - server - = createServer ((UA_UInt16)port, applicationUri, - serverSec.certPath ? &serverSec : NULL, true, &retval); + server = create_server ((UA_UInt16)port, application_uri, + server_sec.cert_path ? &server_sec : NULL, true, + &retval); if (!server) goto cleanup; - UA_ServerConfig *serverConfig = UA_Server_getConfig (server); - serverConfig->logging->context = (void *)(uintptr_t)logLevel; + UA_ServerConfig *server_config = UA_Server_getConfig (server); + server_config->logging->context = (void *)(uintptr_t)log_level; - retval = configureAccessControl (serverConfig, &serverAuth); + retval = configure_access_control (server_config, &server_auth); if (retval != UA_STATUSCODE_GOOD) goto cleanup; - serverConfig->applicationDescription.applicationType + server_config->applicationDescription.applicationType = UA_APPLICATIONTYPE_SERVER; - LdsClientParams ldsParams = { - .appUri = clientAppUri, - .sec = clientSec, - .auth = clientAuth, - .logLevel = logLevel, + lds_client_params lds_params = { + .app_uri = client_app_uri, + .sec = client_sec, + .auth = client_auth, + .log_level = log_level, }; /* Use run_startup + manual event loop (instead of UA_Server_run) so we @@ -176,59 +176,60 @@ main (int argc, char **argv) UA_Server_run_startup (server); /* UA_Server_registerDiscovery consumes (clears) the client config, - so makeLdsClientConfig builds a fresh one for every call. */ - UA_ClientConfig clientConfig; - retval = makeLdsClientConfig (&clientConfig, &ldsParams); + 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 discoveryUrl = UA_STRING_ALLOC (discoveryEndpoint); - retval = UA_Server_registerDiscovery (server, &clientConfig, discoveryUrl, + UA_String discovery_url = UA_STRING_ALLOC (discovery_endpoint); + retval = UA_Server_registerDiscovery (server, &client_config, discovery_url, UA_STRING_NULL); - UA_String_clear (&discoveryUrl); + UA_String_clear (&discovery_url); if (retval != UA_STATUSCODE_GOOD) UA_LOG_WARNING (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "Initial register failed: %s", UA_StatusCode_name (retval)); /* Periodic re-registration loop. Re-register with the LDS every - registerInterval seconds so the LDS does not purge us. */ - time_t lastRegister = time (NULL); + register_interval seconds so the LDS does not purge us. */ + time_t last_register = time (NULL); - while (running) + while (g_running) { UA_Server_run_iterate (server, true); time_t now = time (NULL); - if (now - lastRegister >= registerInterval) + if (now - last_register >= register_interval) { - retval = makeLdsClientConfig (&clientConfig, &ldsParams); + retval = _s_make_lds_client_config (&client_config, &lds_params); if (retval == UA_STATUSCODE_GOOD) { - UA_String reregUrl = UA_STRING_ALLOC (discoveryEndpoint); - retval = UA_Server_registerDiscovery (server, &clientConfig, - reregUrl, UA_STRING_NULL); - UA_String_clear (&reregUrl); + 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)); } - lastRegister = now; + 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 = makeLdsClientConfig (&clientConfig, &ldsParams); + retval = _s_make_lds_client_config (&client_config, &lds_params); if (retval == UA_STATUSCODE_GOOD) { - UA_String deregUrl = UA_STRING_ALLOC (discoveryEndpoint); - retval = UA_Server_deregisterDiscovery (server, &clientConfig, deregUrl); - UA_String_clear (&deregUrl); + 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", @@ -241,9 +242,9 @@ main (int argc, char **argv) cleanup: if (server) UA_Server_delete (server); - freeTrustStore (clientSec.trustPaths, clientSec.trustSize); - freeTrustStore (serverSec.trustPaths, serverSec.trustSize); - configFree (&clientCfg); - configFree (&serverCfg); + free_trust_store (client_sec.trust_paths, client_sec.trust_size); + free_trust_store (server_sec.trust_paths, server_sec.trust_size); + config_free (&client_cfg); + config_free (&server_cfg); return rc; } -- cgit v1.2.3