Skip to main content

Configuration and Creation (C)

The C SDK uses structures + functions in place of C#'s overloaded constructors. Process:

  1. Statement DarraUa_SessionConfig cfg;
  2. Adjust DarraUa_SessionConfig_Init(&cfg) Set Default Values
  3. Overwrite the fields that need to be modified
  4. Function: DarraUa_Session_Create(&cfg, &handle) Create a handle
  5. After use, adjust and release DarraUa_Session_Close(handle)
Prerequisite Reading

DarraUa_SessionConfig Field Details​

typedef struct {
const char* endpoint_url;
DarraUa_MessageSecurityMode security_mode;
const char* security_policy_uri;
DarraUa_UserTokenType user_token_type;
const char* username;
const char* password;
const char* client_cert_path;
const char* client_key_path;
const char* server_cert_path;
uint32_t session_timeout_ms;
uint32_t connect_timeout_ms;
uint32_t request_timeout_ms;
uint32_t keepalive_interval_ms;
uint32_t auto_reconnect;
uint32_t reconnect_max_retries;
uint32_t reconnect_delay_ms;
} DarraUa_SessionConfig;
CategoryPropertyTypeAccessDescription
Requiredendpoint_urlconst char*inUTF-8, opc.tcp://host:port (port can be omitted; default is 4840). `_Create` internally makes a deep copy; the memory can be released after the call.
Securitysecurity_modeDarraUa_MessageSecurityModeinDefault: NONE
security_policy_uriconst char*inDefault: #None; Recommended: #Basic256Sha256
client_cert_pathconst char*inClient PFX (Sign / SignAndEncrypt required)
client_key_pathconst char*inPassword for the client PFX (PKCS#12) — the whole file. The field name is `client_key_path`; the value is the password, not a path. Leaving it NULL only applies to a passwordless PFX; a password-protected PFX left NULL cannot be unlocked, and `_Connect` returns `UA_STATUS_BAD_CERTIFICATE_INVALID` immediately — **no** silent downgrade, the client identity is never quietly lost
server_cert_pathconst char*inPath to the server certificate PFX (PKCS#12, passwordless) — not DER; passing a .der file will always fail to load. Not providing it = **do not verify the server's identity** (use the "None" channel to obtain the server's self-reported certificate; no TOFU, no disk storage, no fingerprint storage, no comparison). If loading fails, only a WARN is logged and it silently falls back to auto-discovery; no exceptions, and no "Did loading succeed?" query interface
Loginuser_token_typeDarraUa_UserTokenTypeinDefault: ANONYMOUS
usernameconst char*inRequired when user_token_type=USERNAME
passwordconst char*inUsed with username
Timeoutsession_timeout_msuint32_tinServer-side session timeout duration; default is 60,000 (60 seconds)
connect_timeout_msuint32_tinConnection establishment TCP timeout; default is 10,000 (10 seconds)
request_timeout_msuint32_tinSingle RPC timeout, default 5000 (5 seconds)
keepalive_interval_msuint32_tinKeep-alive interval, default 10000, 0 = disabled
Reconnectauto_reconnectuint32_tin1 = Enabled (default), 0 = Do not reconnect
reconnect_max_retriesuint32_tinDefault: 3
reconnect_delay_msuint32_tinDefault: 2000

DarraUa_MessageSecurityMode​

typedef enum {
UA_MSG_SECURITY_MODE_INVALID = 0,
UA_MSG_SECURITY_MODE_NONE = 1,
UA_MSG_SECURITY_MODE_SIGN = 2,
UA_MSG_SECURITY_MODE_SIGN_ENCRYPT = 3
} DarraUa_MessageSecurityMode;

DarraUa_UserTokenType​

typedef enum {
UA_USER_TOKEN_ANONYMOUS = 0,
UA_USER_TOKEN_USERNAME = 1,
UA_USER_TOKEN_CERTIFICATE = 2,
UA_USER_TOKEN_ISSUED = 3
} DarraUa_UserTokenType;

SecurityPolicy URI​

The current implementation only supports two entries; any additional entries you enter URI will not take effect:

  • Unencrypted — http://opcfoundation.org/UA/SecurityPolicy#None (Available)
  • Basic256Sha256 (Recommended) — http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256 (Available)
  • Aes128_Sha256_RsaOaep — http://opcfoundation.org/UA/SecurityPolicy#Aes128_Sha256_RsaOaep (Not implemented)
  • Aes256_Sha256_RsaPss — http://opcfoundation.org/UA/SecurityPolicy#Aes256_Sha256_RsaPss (Not implemented)

DarraUa_Session_Create​

OPCUA_API DarraUa_Status OPCUA_CALL DarraUa_Session_Create(
const DarraUa_SessionConfig* cfg,
DarraUa_SessionHandle* out_handle);
ParametersNote
cfgConfiguration has been initialized and overwritten
out_handleExit the function; if successful, write the valid handle; if unsuccessful, set it to 0.
ReturnMeaning
UA_STATUS_GOODSuccess
UA_STATUS_INVALID_ARGUMENTcfg=NULL or endpoint_url=NULL
UA_STATUS_NOT_INITIALIZEDDarraUa_Initialize() was not called first
UA_STATUS_OUT_OF_MEMORYInternal allocation failed

_Create just creates a local context, does not initiate any network requests. The actual network connection occurs in _Connect. And that is why URL Format errors are not reported here. — endpoint_url The analysis takes place at _Connect, and it is only at that step that it returns UA_STATUS_BAD_TCP_ENDPOINT_URL_INVALID.

When you fail, out_handle will not be written; the caller must first initialize the handle variable to UA_INVALID_SESSION_HANDLE.

Three Examples of Typical Configurations​

#include <opcua/opcua.h>
#include <stdio.h>

/* 1. Anonymous + Plaintext (Development Phase) */
static int demo_anonymous_no_security(const char* endpoint)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = endpoint;
cfg.security_mode = UA_MSG_SECURITY_MODE_NONE;
cfg.security_policy_uri = "http://opcfoundation.org/UA/SecurityPolicy#None";
cfg.user_token_type = UA_USER_TOKEN_ANONYMOUS;

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
DarraUa_Status st = DarraUa_Session_Create(&cfg, &h);
if (!UA_STATUS_IS_GOOD(st)) {
fprintf(stderr, "Create failed: 0x%08X (%s)\n",
(unsigned)st, DarraUa_StatusName(st));
return -1;
}
DarraUa_Session_Close(h);
return 0;
}

/* 2. Anonymous + Sign (Internal Network Production) */
static int demo_anonymous_sign(const char* endpoint, const char* pfx)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = endpoint;
cfg.security_mode = UA_MSG_SECURITY_MODE_SIGN;
cfg.security_policy_uri = "http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256";
cfg.user_token_type = UA_USER_TOKEN_ANONYMOUS;
cfg.client_cert_path = pfx;
cfg.client_key_path = "123456";

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
if (!UA_STATUS_IS_GOOD(DarraUa_Session_Create(&cfg, &h))) return -1;
DarraUa_Session_Close(h);
return 0;
}

/* 3. Username + SignAndEncrypt (Typical Production) */
static int demo_username_sign_encrypt(const char* endpoint, const char* pfx,
const char* user, const char* pass)
{
DarraUa_SessionConfig cfg;
DarraUa_SessionConfig_Init(&cfg);
cfg.endpoint_url = endpoint;
cfg.security_mode = UA_MSG_SECURITY_MODE_SIGN_ENCRYPT;
cfg.security_policy_uri = "http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256";
cfg.user_token_type = UA_USER_TOKEN_USERNAME;
cfg.username = user;
cfg.password = pass;
cfg.client_cert_path = pfx;
cfg.client_key_path = "123456";
cfg.session_timeout_ms = 1800000; /* 30 minutes */
cfg.keepalive_interval_ms = 5000; /* 5 seconds, more sensitive disconnection detection */

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
if (!UA_STATUS_IS_GOOD(DarraUa_Session_Create(&cfg, &h))) return -1;
DarraUa_Session_Close(h);
return 0;
}

Common Pitfalls​

  • Not adjusted DarraUa_SessionConfig_Init: If all fields contain undefined values on the stack, reading them endpoint_url results in undefined behavior. If the value endpoint_url happens to be NULL, it returns _Create UA_STATUS_INVALID_ARGUMENT; in all other cases, the behavior is undefined—you must call Init first.
  • Mistakenly assumed that the strings would be kept around indefinitely: Not necessary. _Create deep-copies all seven strings (endpoint_url / security_policy_uri / username / password / client_cert_path / client_key_path / server_cert_path) into the internal context, and they can be released after the function returns.
  • The client certificate path contains Chinese characters or spaces: Always use the following path: ASCII
  • The reconnection parameters are too aggressive: reconnect_max_retries Too large + network jitter will cause infinite reconnections; recommended value: ≤ 5

Resource Management​

Handles must be closed DarraUa_Session_Close(h); otherwise, a native context leak will occur:

DarraUa_SessionHandle h = UA_INVALID_SESSION_HANDLE;
DarraUa_Status st = DarraUa_Session_Create(&cfg, &h);
if (!UA_STATUS_IS_GOOD(st)) goto cleanup;

st = DarraUa_Session_Connect(h);
/* ... Business ... */

DarraUa_Session_Disconnect(h); /* Optional; "Close" first performs a "Disconnect" internally. */

cleanup:
if (h != UA_INVALID_SESSION_HANDLE) {
DarraUa_Session_Close(h);
}

Next Step​