From c35eb35bb63a97b7c46e879819757a9cb48165b5 Mon Sep 17 00:00:00 2001 From: Thomas Vanbesien Date: Tue, 17 Feb 2026 02:27:51 +0100 Subject: Initial commit: OPC UA discovery project CMake-based C project using open62541 for OPC UA discovery. Includes Local Discovery Server, register server, and find servers client with OpenSSL encryption support. --- src/common.h | 115 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 src/common.h (limited to 'src/common.h') diff --git a/src/common.h b/src/common.h new file mode 100644 index 0000000..e3d2f4c --- /dev/null +++ b/src/common.h @@ -0,0 +1,115 @@ +#ifndef DISCOVERY_COMMON_H +#define DISCOVERY_COMMON_H + +/** + * @file common.h + * @brief Shared helpers for the OPC UA discovery demo programs. + * + * Provides file-loading, factory, and output formatting functions used by + * the LDS, the registering server, and the FindServers client. + */ + +#include +#include +#include + +/** + * @brief Loads a DER-encoded certificate or key file into a UA_ByteString. + * + * @param path File path to read. + * @return The file contents, or UA_BYTESTRING_NULL on error. + */ +UA_ByteString loadFile (const char *const path); + +/** + * @brief Creates a UA_Server configured with security policies and encryption. + * + * The server is initialized with the specified port, certificate, private key, + * and trustlist. The applicationUri is set in the server's application + * description. + * + * @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 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); + +/** + * @brief Parses a security mode name into the corresponding enum value. + * + * Accepted names: "None", "Sign", "SignAndEncrypt". + * + * @param name Mode name string. + * @return The matching UA_MessageSecurityMode, or + * UA_MESSAGESECURITYMODE_INVALID if the name is not recognized. + */ +UA_MessageSecurityMode parseSecurityMode (const char *name); + +/** + * @brief Maps a short security policy name to its full OPC UA URI. + * + * Accepted names: "None", "Basic256Sha256", "Aes256_Sha256_RsaPss", + * "Aes128_Sha256_RsaOaep", "ECC_nistP256". + * + * @param shortName Short policy name. + * @return The full URI string, or NULL if the name is not recognized. + */ +const char *resolveSecurityPolicyUri (const char *shortName); + +/** + * @brief Initializes a UA_ClientConfig with encryption from file paths. + * + * The config must be zero-initialized by the caller before calling this + * function. Loads the certificate, private key, and trustlist, then applies + * default encryption settings. + * + * @param cc Pointer to a zero-initialized UA_ClientConfig. + * @param applicationUri OPC UA application URI. + * @param certPath Path to client certificate (.der). + * @param keyPath Path to private key (.der). + * @param trustPaths Array of trustlist file paths (may be NULL if trustSize is + * 0). + * @param trustSize Number of entries in trustPaths. + * @param securityMode Requested message security mode. + * @param securityPolicyUri Security policy URI string (e.g. + * "http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256"). + * @return UA_STATUSCODE_GOOD on success, error code otherwise. + */ +UA_StatusCode createSecureClientConfig ( + UA_ClientConfig *cc, const char *applicationUri, const char *certPath, + const char *keyPath, char **trustPaths, size_t trustSize, + UA_MessageSecurityMode securityMode, const char *securityPolicyUri); + +/** + * @brief Logs a UA_ApplicationDescription (server info from FindServers). + * + * Outputs the application URI, name, product URI, type, and discovery URLs + * via UA_LOG_INFO. + * + * @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); + +/** + * @brief Logs a UA_EndpointDescription in a compact one-line format. + * + * Outputs the endpoint URL, security level, security mode, and the short + * policy name (the part after '#') via UA_LOG_INFO. + * + * @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); + +#endif /* DISCOVERY_COMMON_H */ -- cgit v1.2.3