aboutsummaryrefslogtreecommitdiffstats
path: root/src/server_lds.c
diff options
context:
space:
mode:
authorThomas Vanbesien <tvanbesi@proton.me>2026-02-17 02:27:51 +0100
committerThomas Vanbesien <tvanbesi@proton.me>2026-02-17 02:27:51 +0100
commitc35eb35bb63a97b7c46e879819757a9cb48165b5 (patch)
treeabc7f07740fae388f4ff6776585b56f56ec558c9 /src/server_lds.c
downloadBobinkCOpcUa-c35eb35bb63a97b7c46e879819757a9cb48165b5.tar.gz
BobinkCOpcUa-c35eb35bb63a97b7c46e879819757a9cb48165b5.zip
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.
Diffstat (limited to 'src/server_lds.c')
-rw-r--r--src/server_lds.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/server_lds.c b/src/server_lds.c
new file mode 100644
index 0000000..a7794aa
--- /dev/null
+++ b/src/server_lds.c
@@ -0,0 +1,81 @@
+/**
+ * @file server_lds.c
+ * @brief Local Discovery Server implementation.
+ *
+ * This program runs an OPC UA Local Discovery Server (LDS) configured with
+ * encryption and a configurable cleanup timeout. Other OPC UA servers register
+ * with this LDS using the RegisterServer2 service. Clients can query this LDS
+ * using the FindServers service to discover registered servers.
+ */
+
+#include "common.h"
+
+#include <open62541/plugin/log_stdout.h>
+#include <open62541/server.h>
+#include <open62541/server_config_default.h>
+
+#include <signal.h>
+#include <stdlib.h>
+
+UA_Boolean running = true;
+
+static void
+stopHandler (int sig)
+{
+ UA_LOG_INFO (UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "received ctrl-c");
+ running = false;
+}
+
+int
+main (int argc, char *argv[])
+{
+ signal (SIGINT, stopHandler);
+ signal (SIGTERM, stopHandler);
+
+ if (argc < 6)
+ {
+ UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
+ "Usage: %s\n"
+ " <port> <applicationUri>\n"
+ " <server-certificate.der> <private-key.der>\n"
+ " <cleanup-timeout-seconds>\n"
+ " [<trustlist1.der>, ...]",
+ argv[0]);
+ return EXIT_FAILURE;
+ }
+
+ UA_UInt16 port = (UA_UInt16)atoi (argv[1]);
+ int cleanupTimeout = atoi (argv[5]);
+
+ /* The OPC UA specification requires the cleanup timeout to exceed the
+ register-server interval. open62541 enforces a floor of 10 seconds. */
+ if (cleanupTimeout <= 10)
+ {
+ UA_LOG_FATAL (UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,
+ "Cleanup timeout must be > 10 seconds (got %d)",
+ cleanupTimeout);
+ return EXIT_FAILURE;
+ }
+ size_t trustSize = (argc > 6) ? (size_t)argc - 6 : 0;
+
+ UA_StatusCode retval;
+ UA_Server *server = createSecureServer (port, argv[2], argv[3], argv[4],
+ argv + 6, trustSize, &retval);
+ if (!server)
+ return EXIT_FAILURE;
+
+ UA_ServerConfig *serverConfig = UA_Server_getConfig (server);
+
+ /* Mark this server as a Discovery Server so clients can identify it. */
+ serverConfig->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;
+
+ retval = UA_Server_run (server, &running);
+
+ UA_Server_delete (server);
+ return retval == UA_STATUSCODE_GOOD ? EXIT_SUCCESS : EXIT_FAILURE;
+}