open62541 0.10.1

High-level, safe bindings for the C99 library open62541, an open source and free implementation of OPC UA (OPC Unified Architecture).
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::{ffi::CString, ptr, time::Duration};

use open62541_sys::{
    UA_CertificateVerification_AcceptAll, UA_Client_connect, UA_Client_getEndpoints,
    UA_ClientConfig,
};

use crate::{DataType as _, Error, Result, ua};

/// Builder for [`Client`].
///
/// Use this to specify additional options when connecting to an OPC UA endpoint.
///
/// # Examples
///
/// ```no_run
/// use open62541::ClientBuilder;
/// use std::time::Duration;
///
/// # #[tokio::main]
/// # async fn main() -> anyhow::Result<()> {
/// #
/// let client = ClientBuilder::default()
///     .secure_channel_life_time(Duration::from_secs(60))
///     .connect("opc.tcp://opcuademo.sterfive.com:26543")?;
/// #
/// # Ok(())
/// # }
/// ```
#[derive(Debug)]
pub struct ClientBuilder(ua::ClientConfig);

impl ClientBuilder {
    /// Creates builder from default client config.
    // Method name refers to call of `UA_ClientConfig_setDefault()`.
    #[must_use]
    fn default() -> Self {
        Self(ua::ClientConfig::default(ClientContext::default()))
    }

    /// Creates builder from default client config with encryption.
    ///
    /// This requires certificate and associated private key data in [DER] or [PEM] format. Data may
    /// be read from local files or created with [`crate::create_certificate()`].
    ///
    /// ```
    /// use open62541::{Certificate, ClientBuilder, PrivateKey};
    ///
    /// const CERTIFICATE_PEM: &[u8] = include_bytes!("../examples/client_certificate.pem");
    /// const PRIVATE_KEY_PEM: &[u8] = include_bytes!("../examples/client_private_key.pem");
    ///
    /// let certificate = Certificate::from_bytes(CERTIFICATE_PEM);
    /// let private_key = PrivateKey::from_bytes(PRIVATE_KEY_PEM);
    ///
    /// # let _ = move || -> open62541::Result<()> {
    /// let client = ClientBuilder::default_encryption(&certificate, &private_key)
    ///     .expect("should create builder with encryption")
    ///     .connect("opc.tcp://localhost")?;
    /// # Ok(())
    /// # };
    /// ```
    ///
    /// # Errors
    ///
    /// This fails when the certificate is invalid or the private key cannot be decrypted (e.g. when
    /// it has been protected by a password).
    ///
    /// [DER]: https://en.wikipedia.org/wiki/X.690#DER_encoding
    /// [PEM]: https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail
    // Method name refers to call of `UA_ClientConfig_setDefaultEncryption()`.
    #[cfg(feature = "mbedtls")]
    pub fn default_encryption(
        local_certificate: &crate::Certificate,
        private_key: &crate::PrivateKey,
    ) -> Result<Self> {
        Ok(Self(ua::ClientConfig::default_encryption(
            ClientContext::default(),
            local_certificate,
            private_key,
        )?))
    }

    /// Sets (response) timeout.
    ///
    /// # Panics
    ///
    /// The given duration must be non-negative and less than 4,294,967,295 milliseconds (less than
    /// 49.7 days).
    #[must_use]
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.config_mut().timeout = u32::try_from(timeout.as_millis())
            .expect("timeout (in milliseconds) should be in range of u32");
        self
    }

    /// Sets client description.
    ///
    /// The description must be internally consistent. The application URI set in the application
    /// description must match the URI set in the certificate.
    #[must_use]
    pub fn client_description(mut self, client_description: ua::ApplicationDescription) -> Self {
        client_description.move_into_raw(&mut self.config_mut().clientDescription);
        self
    }

    /// Sets user identity token.
    #[must_use]
    pub fn user_identity_token(mut self, user_identity_token: &ua::UserIdentityToken) -> Self {
        user_identity_token
            .to_extension_object()
            .move_into_raw(&mut self.config_mut().userIdentityToken);
        self
    }

    /// Sets security mode.
    #[must_use]
    pub fn security_mode(mut self, security_mode: ua::MessageSecurityMode) -> Self {
        security_mode.move_into_raw(&mut self.config_mut().securityMode);
        self
    }

    /// Sets security policy URI.
    ///
    /// The known values are as follows:
    ///
    /// - `http://opcfoundation.org/UA/SecurityPolicy#None`
    /// - `http://opcfoundation.org/UA/SecurityPolicy#Basic128Rsa15` (deprecated)
    /// - `http://opcfoundation.org/UA/SecurityPolicy#Basic256` (deprecated)
    /// - `http://opcfoundation.org/UA/SecurityPolicy#Basic256Sha256`
    /// - `http://opcfoundation.org/UA/SecurityPolicy#Aes128_Sha256_RsaOaep`
    /// - `http://opcfoundation.org/UA/SecurityPolicy#Aes256_Sha256_RsaPss`
    #[must_use]
    pub fn security_policy_uri(mut self, security_policy_uri: ua::String) -> Self {
        security_policy_uri.move_into_raw(&mut self.config_mut().securityPolicyUri);
        self
    }

    /// Sets secure channel life time.
    ///
    /// After this life time, the channel needs to be renewed.
    ///
    /// # Panics
    ///
    /// The given duration must be non-negative and less than 4,294,967,295 milliseconds (less than
    /// 49.7 days).
    #[must_use]
    pub fn secure_channel_life_time(mut self, secure_channel_life_time: Duration) -> Self {
        self.config_mut().secureChannelLifeTime =
            u32::try_from(secure_channel_life_time.as_millis())
                .expect("secure channel life time (in milliseconds) should be in range of u32");
        self
    }

    /// Sets requested session timeout.
    ///
    /// # Panics
    ///
    /// The given duration must be non-negative and less than 4,294,967,295 milliseconds (less than
    /// 49.7 days).
    #[must_use]
    pub fn requested_session_timeout(mut self, requested_session_timeout: Duration) -> Self {
        self.config_mut().requestedSessionTimeout =
            u32::try_from(requested_session_timeout.as_millis())
                .expect("secure channel life time (in milliseconds) should be in range of u32");
        self
    }

    /// Sets connectivity check interval.
    ///
    /// Use `None` to disable background task.
    ///
    /// # Panics
    ///
    /// The given duration must be non-negative and less than 4,294,967,295 milliseconds (less than
    /// 49.7 days).
    #[must_use]
    pub fn connectivity_check_interval(
        mut self,
        connectivity_check_interval: Option<Duration>,
    ) -> Self {
        self.config_mut().connectivityCheckInterval =
            u32::try_from(connectivity_check_interval.map_or(0, |interval| interval.as_millis()))
                .expect("connectivity check interval (in milliseconds) should be in range of u32");
        self
    }

    /// Disables server certificate checks.
    ///
    /// Note that this disables all certificate verification of server communications. Use only when
    /// servers can be identified in some other way, or identity is not relevant.
    ///
    /// This is a shortcut for using [`certificate_verification()`](Self::certificate_verification)
    /// with [`ua::CertificateVerification::accept_all()`].
    #[must_use]
    pub fn accept_all(mut self) -> Self {
        let config = self.config_mut();
        unsafe {
            UA_CertificateVerification_AcceptAll(&raw mut config.certificateVerification);
        }
        self
    }

    /// Sets certificate verification.
    #[must_use]
    pub fn certificate_verification(
        mut self,
        certificate_verification: ua::CertificateVerification,
    ) -> Self {
        let config = self.config_mut();
        certificate_verification.move_into_raw(&mut config.certificateVerification);
        self
    }

    /// Sets private-key password callback.
    ///
    /// The trait [`PrivateKeyPasswordCallback`] is implemented for closures, so this call may be as
    /// simple as the following (note that hard-coding secrets like this is not secure and should be
    /// avoided):
    ///
    /// ```
    /// # use open62541::{Certificate, ClientBuilder, PrivateKey};
    /// #
    /// # let _ = |certificate: Certificate, private_key: PrivateKey| -> open62541::Result<()> {
    /// let client = ClientBuilder::default_encryption(&certificate, &private_key)
    ///     .expect("should create builder with encryption")
    ///     .private_key_password_callback(|| Ok(String::from("secret").into()))
    ///     .connect("opc.tcp://localhost")?;
    /// # Ok(())
    /// # };
    /// ```
    ///
    /// [`PrivateKeyPasswordCallback`]: crate::PrivateKeyPasswordCallback
    #[cfg(feature = "mbedtls")]
    #[must_use]
    pub fn private_key_password_callback(
        mut self,
        private_key_password_callback: impl crate::PrivateKeyPasswordCallback + 'static,
    ) -> Self {
        self.context_mut().private_key_password_callback =
            Some(Box::new(private_key_password_callback));
        self
    }

    /// Connects to OPC UA endpoint and returns [`Client`].
    ///
    /// # Errors
    ///
    /// This fails when the target server is not reachable.
    ///
    /// # Panics
    ///
    /// The endpoint URL must not contain any NUL bytes.
    pub fn connect(self, endpoint_url: &str) -> Result<Client> {
        let mut client = self.build();
        client.connect(endpoint_url)?;
        Ok(client)
    }

    /// Connects to OPC UA server and returns endpoints.
    ///
    /// # Errors
    ///
    /// This fails when the target server is not reachable.
    ///
    /// # Panics
    ///
    /// The server URL must not contain any NUL bytes.
    pub fn get_endpoints(self, server_url: &str) -> Result<ua::Array<ua::EndpointDescription>> {
        log::info!("Getting endpoints of server {server_url}");

        let server_url = CString::new(server_url).expect("server URL does not contain NUL bytes");

        let mut client = self.build();
        let endpoint_descriptions: Option<ua::Array<ua::EndpointDescription>>;

        let status_code = ua::StatusCode::new({
            let mut endpoint_descriptions_size = 0;
            let mut endpoint_descriptions_ptr = ptr::null_mut();
            let result = unsafe {
                UA_Client_getEndpoints(
                    client.0.as_mut_ptr(),
                    server_url.as_ptr(),
                    &raw mut endpoint_descriptions_size,
                    &raw mut endpoint_descriptions_ptr,
                )
            };
            // Wrap array result immediately to not leak memory when leaving function early as with
            // `?` below.
            endpoint_descriptions = ua::Array::<ua::EndpointDescription>::from_raw_parts(
                endpoint_descriptions_size,
                endpoint_descriptions_ptr,
            );
            result
        });
        Error::verify_good(&status_code)?;

        let Some(endpoint_descriptions) = endpoint_descriptions else {
            return Err(Error::internal("expected array of endpoint descriptions"));
        };

        Ok(endpoint_descriptions)
    }

    /// Builds OPC UA client.
    #[must_use]
    fn build(self) -> Client {
        Client(ua::Client::new_with_config(self.0))
    }

    /// Access client configuration.
    #[must_use]
    fn config_mut(&mut self) -> &mut UA_ClientConfig {
        // SAFETY: Ownership is not given away.
        unsafe { self.0.as_mut() }
    }

    /// Access client context.
    #[cfg_attr(not(feature = "mbedtls"), expect(dead_code, reason = "unused"))]
    #[must_use]
    fn context_mut(&mut self) -> &mut ClientContext {
        self.0.context_mut()
    }
}

impl Default for ClientBuilder {
    fn default() -> Self {
        Self::default()
    }
}

/// Custom client context.
///
/// This is stored in the client's `clientContext` attribute and manages custom callbacks as used by
/// [`ClientBuilder::private_key_password_callback()`].
#[derive(Default)]
pub(crate) struct ClientContext {
    #[cfg(feature = "mbedtls")]
    pub(crate) private_key_password_callback: Option<Box<dyn crate::PrivateKeyPasswordCallback>>,
}

/// Connected OPC UA client.
///
/// This represents an OPC UA client connected to a specific endpoint. Once a client is connected to
/// an endpoint, it is not possible to switch to another server. Create a new client for that.
///
/// Once a connection to the given endpoint is established, the client keeps the connection open and
/// reconnects if necessary.
///
/// If the connection fails unrecoverably, the client is no longer usable. In this case create a new
/// client if required.
///
/// To disconnect, prefer method [`disconnect()`](Self::disconnect) over simply dropping the client:
/// disconnection involves server communication and might take a short amount of time.
#[derive(Debug)]
pub struct Client(ua::Client);

impl Client {
    /// Creates default client connected to endpoint.
    ///
    /// If you need more control over the initialization, use [`ClientBuilder`] instead, and turn it
    /// into [`Client`] by calling [`connect()`](ClientBuilder::connect).
    ///
    /// # Errors
    ///
    /// See [`ClientBuilder::connect()`].
    ///
    /// # Panics
    ///
    /// See [`ClientBuilder::connect()`].
    pub fn new(endpoint_url: &str) -> Result<Self> {
        ClientBuilder::default().connect(endpoint_url)
    }

    /// Turns client into [`AsyncClient`].
    ///
    /// The [`AsyncClient`] can be used to access methods in an asynchronous way.
    ///
    /// [`AsyncClient`]: crate::AsyncClient
    #[must_use]
    pub fn into_async(self) -> crate::AsyncClient {
        crate::AsyncClient::from_sync(self.0)
    }

    /// Gets current channel and session state, and connect status.
    #[must_use]
    pub fn state(&self) -> ua::ClientState {
        self.0.state()
    }

    /// Connects to endpoint.
    ///
    /// This method is always called internally before passing new [`Client`] instances to the user:
    /// our contract states that a `Client` should always be connected.
    fn connect(&mut self, endpoint_url: &str) -> Result<()> {
        log::info!("Connecting to endpoint {endpoint_url}");

        let endpoint_url =
            CString::new(endpoint_url).expect("endpoint URL does not contain NUL bytes");

        let status_code = ua::StatusCode::new(unsafe {
            // SAFETY: The method does not take ownership of `client`.
            UA_Client_connect(self.0.as_mut_ptr(), endpoint_url.as_ptr())
        });
        Error::verify_good(&status_code)
    }

    /// Disconnects from endpoint.
    ///
    /// This consumes the client and handles the graceful shutdown of the connection. This should be
    /// preferred over simply dropping the instance to give the server a chance to clean up and also
    /// to avoid blocking unexpectedly when the client is being dropped without calling this method.
    #[expect(clippy::semicolon_if_nothing_returned, reason = "future fail-safe")]
    // Forward any result as-is to detect mismatching method signatures at compile time if the
    // return type of the inner method should ever change.
    pub fn disconnect(self) {
        self.0.disconnect()
    }
}