Configuration and Creation (C)
The C SDK uses structures + functions in place of C#'s overloaded constructors. Process:
- Statement
DarraUa_SessionConfig cfg; - Adjust
DarraUa_SessionConfig_Init(&cfg)Set Default Values - Overwrite the fields that need to be modified
- Function:
DarraUa_Session_Create(&cfg, &handle)Create a handle - After use, adjust and release
DarraUa_Session_Close(handle)
Prerequisite Reading
- For encryption parameters (SecurityMode/certificates), see Security Encryption.
- See "Server-Side Pre-Scan" Discovery.
- For instructions on what to do after connecting, see Connect / Disconnect.
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;
| Category | Property | Type | Access | Description |
|---|---|---|---|---|
| Required | endpoint_url | const char* | in | UTF-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. |
| Security | security_mode | DarraUa_MessageSecurityMode | in | Default: NONE |
security_policy_uri | const char* | in | Default: #None; Recommended: #Basic256Sha256 | |
client_cert_path | const char* | in | Client PFX (Sign / SignAndEncrypt required) | |
client_key_path | const char* | in | Password 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_path | const char* | in | Path 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 | |
| Login | user_token_type | DarraUa_UserTokenType | in | Default: ANONYMOUS |
username | const char* | in | Required when user_token_type=USERNAME | |
password | const char* | in | Used with username | |
| Timeout | session_timeout_ms | uint32_t | in | Server-side session timeout duration; default is 60,000 (60 seconds) |
connect_timeout_ms | uint32_t | in | Connection establishment TCP timeout; default is 10,000 (10 seconds) | |
request_timeout_ms | uint32_t | in | Single RPC timeout, default 5000 (5 seconds) | |
keepalive_interval_ms | uint32_t | in | Keep-alive interval, default 10000, 0 = disabled | |
| Reconnect | auto_reconnect | uint32_t | in | 1 = Enabled (default), 0 = Do not reconnect |
reconnect_max_retries | uint32_t | in | Default: 3 | |
reconnect_delay_ms | uint32_t | in | Default: 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);
| Parameters | Note |
|---|---|
| cfg | Configuration has been initialized and overwritten |
| out_handle | Exit the function; if successful, write the valid handle; if unsuccessful, set it to 0. |
| Return | Meaning |
|---|---|
| UA_STATUS_GOOD | Success |
| UA_STATUS_INVALID_ARGUMENT | cfg=NULL or endpoint_url=NULL |
| UA_STATUS_NOT_INITIALIZED | DarraUa_Initialize() was not called first |
| UA_STATUS_OUT_OF_MEMORY | Internal 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 themendpoint_urlresults in undefined behavior. If the valueendpoint_urlhappens to be NULL, it returns_CreateUA_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.
_Createdeep-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_retriesToo 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);
}