aboutsummaryrefslogtreecommitdiffstats
path: root/src/common.h
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-02-17 23:52:06 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-02-17 23:52:06 +0100
commit7648a256d97abda40edbdc0d7bf59edd0a09fb95 (patch)
treea86d829f85f4a3d8038741c299d9409cb2272686 /src/common.h
parentb2002d96f495dcb3bd2f5a738ec1615034ca876f (diff)
downloadBobinkCOpcUa-7648a256d97abda40edbdc0d7bf59edd0a09fb95.tar.gz
BobinkCOpcUa-7648a256d97abda40edbdc0d7bf59edd0a09fb95.zip
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.
Diffstat (limited to 'src/common.h')
-rw-r--r--src/common.h46
1 files changed, 34 insertions, 12 deletions
diff --git a/src/common.h b/src/common.h
index 7290181..b4bd323 100644
--- a/src/common.h
+++ b/src/common.h
@@ -15,6 +15,8 @@
#include <stddef.h>
+#include "config.h"
+
/**
* @brief Loads a DER-encoded certificate or key file into a UA_ByteString.
*
@@ -47,26 +49,26 @@ int loadTrustStore (const char *dirPath, char ***outPaths, size_t *outSize);
void freeTrustStore (char **paths, size_t size);
/**
- * @brief Creates a UA_Server configured with security policies and encryption.
+ * @brief Creates a UA_Server, optionally configured with security policies.
*
- * The server is initialized with the specified port, certificate, private key,
- * and trustlist. The applicationUri is set in the server's application
- * description.
+ * When @p certPath is non-NULL the server is initialized with encryption
+ * (certificate, private key, trustlist). When @p certPath is NULL the server
+ * runs with SecurityPolicy#None only (keyPath and trustPaths are ignored).
+ * The applicationUri is set in both cases.
*
* @param port Server port number.
* @param applicationUri OPC UA application URI.
- * @param certPath Path to server certificate (.der).
- * @param keyPath Path to private key (.der).
- * @param trustPaths Array of trustlist file paths (may be NULL if trustSize is
- * 0).
+ * @param certPath Path to server certificate (.der), or NULL for unsecure.
+ * @param keyPath Path to private key (.der), or NULL when certPath is NULL.
+ * @param trustPaths Array of trustlist file paths (may be NULL).
* @param trustSize Number of entries in trustPaths.
* @param retval Output parameter set to the status code on failure.
* @return A configured UA_Server, or NULL on error.
*/
-UA_Server *createSecureServer (UA_UInt16 port, const char *applicationUri,
- const char *certPath, const char *keyPath,
- char **trustPaths, size_t trustSize,
- UA_StatusCode *retval);
+UA_Server *createServer (UA_UInt16 port, const char *applicationUri,
+ const char *certPath, const char *keyPath,
+ char **trustPaths, size_t trustSize,
+ UA_StatusCode *retval);
/**
* @brief Parses a log-level name into the corresponding UA_LogLevel value.
@@ -80,6 +82,26 @@ UA_Server *createSecureServer (UA_UInt16 port, const char *applicationUri,
int parseLogLevel (const char *name);
/**
+ * @brief Parses the authMode key from a configuration file.
+ *
+ * When authMode is "anonymous", sets *allowAnonymous to true and
+ * *username / *password to NULL. When authMode is "user", sets
+ * *allowAnonymous to false and loads the username/password keys.
+ * Logs errors internally.
+ *
+ * @param cfg Parsed configuration.
+ * @param program Program name (for error messages).
+ * @param allowAnonymous Output: true for anonymous, false for user.
+ * May be NULL (ignored — useful for client callers).
+ * @param username Output: username string (owned by cfg), or NULL.
+ * @param password Output: password string (owned by cfg), or NULL.
+ * @return 0 on success, -1 on error.
+ */
+int parseAuthConfig (const Config *cfg, const char *program,
+ UA_Boolean *allowAnonymous, const char **username,
+ const char **password);
+
+/**
* @brief Parses a security mode name into the corresponding enum value.
*
* Accepted names: "None", "Sign", "SignAndEncrypt".