async-opcua-client 0.18.0

OPC UA client API
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
use std::{future::Future, sync::Arc};

use futures::StreamExt;
use opcua_core::{
    comms::{
        buffer::SendBuffer,
        secure_channel::SecureChannel,
        tcp_codec::{Message, TcpCodec},
        tcp_types::{AcknowledgeMessage, HelloMessage, ReverseHelloMessage},
    },
    sync::RwLock,
    trace_read_lock, RequestMessage,
};
use opcua_crypto::SecurityPolicy;
use opcua_types::{DecodingOptions, Error, StatusCode};
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
use tokio_util::codec::FramedRead;
use tracing::{debug, error};

use crate::transport::{
    core::{TransportCloseState, TransportState},
    state::SecureChannelState,
    tcp::TransportConfiguration,
    Connector, OutgoingMessage, Transport, TransportPollResult,
};

/// Result of a stream connection attempt, containing the read and write streams, and the
/// actual endpoint URL used.
pub struct StreamConnection<R, W> {
    reader: FramedRead<R, TcpCodec>,
    writer: W,
    endpoint_url: String,
}

impl<R, W> StreamConnection<R, W> {
    /// Create a new `StreamConnection`.
    pub fn new(reader: FramedRead<R, TcpCodec>, writer: W, endpoint_url: String) -> Self {
        Self {
            reader,
            writer,
            endpoint_url,
        }
    }
}

/// Generic stream connector implementation.
/// This is the core of any TCP-based connector, and can be used to implement
/// custom connectors by providing a custom connection function.
///
/// The Connector function should take an endpoint URL and decoding options,
/// and return a future that resolves to a read stream, a write stream, and
/// the actual endpoint URL used. The endpoint URL may differ from the requested
/// one in some cases, like for reverse connections, or if the connection layer
/// does some manner of translation. It is used in the `Hello` message
/// produced by the connector.
///
/// To use, simply create a new `StreamConnector` with the desired
/// connection function and default endpoint URL, and use it directly as
/// a `Connector`. `C` does _not_ need to be cancellation safe.
///
/// For instance, the built-in TCP connector parses the endpoint URL,
/// creates a TCP stream, then splits it into read and write halves.
///
/// # Cancellation Safety
///
/// We rely on the cancellation safety of `StreamExt::next` on `AsyncRead`,
/// and `AsyncWriteExt::write` on `AsyncWrite`. Any custom implementation
/// must preserve this property, or risk weird behavior as incoming or outgoing
/// data is lost.
pub struct StreamConnector<R, W, C, F> {
    connector: C,
    default_endpoint_url: String,
    // fn() -> T is covariant in T, so this makes the struct
    // covariant in R, W, F.
    _f: std::marker::PhantomData<fn() -> F>,
    _r: std::marker::PhantomData<fn() -> R>,
    _w: std::marker::PhantomData<fn() -> W>,
}

impl<R, W, C, F> StreamConnector<R, W, C, F>
where
    R: AsyncRead + Unpin + Send + Sync + 'static,
    W: AsyncWrite + Unpin + Send + Sync + 'static,
    C: Fn(String, DecodingOptions) -> F + Send + Sync,
    F: Future<Output = Result<StreamConnection<R, W>, Error>> + Send + Sync,
{
    /// Create a new `StreamConnector` with the given connector function and default endpoint URL.
    pub fn new(connector: C, default_endpoint_url: String) -> Self {
        Self {
            connector,
            default_endpoint_url,
            _f: std::marker::PhantomData,
            _r: std::marker::PhantomData,
            _w: std::marker::PhantomData,
        }
    }

    async fn hello_exchange(
        reader: &mut FramedRead<R, TcpCodec>,
        writer: &mut W,
        endpoint_url: &str,
        config: &TransportConfiguration,
    ) -> Result<AcknowledgeMessage, Error> {
        let hello = HelloMessage::new(
            endpoint_url,
            config.send_buffer_size,
            config.recv_buffer_size,
            config.max_message_size,
            config.max_chunk_count,
        );
        tracing::trace!("Send hello message: {hello:?}");

        writer
            .write_all(&opcua_types::SimpleBinaryEncodable::encode_to_vec(&hello))
            .await
            .map_err(|err| {
                error!("Cannot send hello to server, err = {}", err);
                Error::new(
                    StatusCode::BadCommunicationError,
                    format!("Cannot send hello to server, err = {}", err),
                )
            })?;
        writer.flush().await.map_err(|err| {
            Error::new(
                StatusCode::BadCommunicationError,
                format!("Cannot send hello to server, err = {}", err),
            )
        })?;
        match reader.next().await {
            Some(Ok(Message::Acknowledge(ack))) => {
                if ack.send_buffer_size > hello.receive_buffer_size {
                    tracing::warn!("Acknowledged send buffer size is greater than receive buffer size in hello message!")
                }
                if ack.receive_buffer_size > hello.send_buffer_size {
                    tracing::warn!("Acknowledged receive buffer size is greater than send buffer size in hello message!")
                }
                tracing::trace!("Received acknowledgement: {:?}", ack);
                Ok(ack)
            }
            other => {
                error!(
                    "Unexpected error while waiting for server ACK. Expected ACK, got {:?}",
                    other
                );
                Err(Error::new(
                    StatusCode::BadConnectionClosed,
                    format!(
                        "Unexpected error while waiting for server ACK. Expected ACK, got {:?}",
                        other
                    ),
                ))
            }
        }
    }

    async fn connect_inner(
        &self,
        secure_channel: &RwLock<SecureChannel>,
        config: &TransportConfiguration,
    ) -> Result<(StreamConnection<R, W>, AcknowledgeMessage, SecurityPolicy), Error> {
        let (decoding_options, policy) = {
            let secure_channel = trace_read_lock!(secure_channel);
            (
                secure_channel.decoding_options(),
                secure_channel.security_policy(),
            )
        };
        let mut connection =
            (self.connector)(self.default_endpoint_url.clone(), decoding_options).await?;

        let ack = Self::hello_exchange(
            &mut connection.reader,
            &mut connection.writer,
            &connection.endpoint_url,
            config,
        )
        .await?;

        Ok((connection, ack, policy))
    }
}

impl<R, W, C, F> Connector for StreamConnector<R, W, C, F>
where
    R: AsyncRead + Unpin + Send + Sync + 'static,
    W: AsyncWrite + Unpin + Send + Sync + 'static,
    C: Fn(String, DecodingOptions) -> F + Send + Sync,
    F: Future<Output = Result<StreamConnection<R, W>, Error>> + Send + Sync,
{
    type Transport = StreamTransport<R, W>;

    async fn connect(
        &self,
        channel: Arc<SecureChannelState>,
        outgoing_recv: tokio::sync::mpsc::Receiver<OutgoingMessage>,
        config: TransportConfiguration,
    ) -> Result<StreamTransport<R, W>, StatusCode> {
        let (connection, ack, policy) = self
            .connect_inner(channel.secure_channel(), &config)
            .await
            .map_err(|e| e.status())?;
        let mut buffer = SendBuffer::new(
            config.send_buffer_size,
            config.max_message_size,
            config.max_chunk_count,
            policy.legacy_sequence_numbers(),
        );
        buffer.revise(
            ack.receive_buffer_size as usize,
            ack.max_message_size as usize,
            ack.max_chunk_count as usize,
        );

        Ok(StreamTransport {
            state: TransportState::new(
                channel,
                outgoing_recv,
                config.max_chunk_count,
                ack.send_buffer_size.min(config.recv_buffer_size as u32) as usize,
            ),
            read: connection.reader,
            write: connection.writer,
            send_buffer: buffer,
            should_close: false,
            closed: TransportCloseState::Open,
            connected_url: connection.endpoint_url,
        })
    }

    fn default_endpoint(&self) -> opcua_types::EndpointDescription {
        opcua_types::EndpointDescription::from(self.default_endpoint_url.as_str())
    }
}

/// Stream-based transport implementation.
/// This serves as the transport layer for OPC-UA over TCP, relying on
/// sending and receiving framed messages over a raw binary stream.
pub struct StreamTransport<R, W> {
    state: TransportState,
    read: FramedRead<R, TcpCodec>,
    write: W,
    send_buffer: SendBuffer,
    should_close: bool,
    closed: TransportCloseState,
    connected_url: String,
}

impl<R: AsyncRead + Unpin, W: AsyncWrite + Unpin> StreamTransport<R, W> {
    fn handle_incoming_message(
        &mut self,
        incoming: Option<Result<Message, std::io::Error>>,
    ) -> TransportPollResult {
        let Some(incoming) = incoming else {
            return TransportPollResult::Closed(StatusCode::BadCommunicationError);
        };
        match incoming {
            Ok(message) => {
                if let Err(e) = self.state.handle_incoming_message(message) {
                    TransportPollResult::Closed(e)
                } else {
                    TransportPollResult::IncomingMessage
                }
            }
            Err(err) => {
                error!("Error reading from stream {}", err);
                TransportPollResult::Closed(StatusCode::BadConnectionClosed)
            }
        }
    }

    async fn poll_inner(&mut self) -> TransportPollResult {
        // Either we've got something in the send buffer, which we can send,
        // or we're waiting for more outgoing messages.
        // We won't wait for outgoing messages while sending, since that
        // could cause the send buffer to fill up.

        // If there's nothing in the send buffer, but there are chunks available,
        // write them to the send buffer before proceeding.
        if self.send_buffer.should_encode_chunks() {
            let secure_channel = trace_read_lock!(self.state.channel_state.secure_channel());
            if let Err(e) = self.send_buffer.encode_next_chunk(&secure_channel) {
                return TransportPollResult::Closed(e);
            }
        }

        // If there is something in the send buffer, write to the stream.
        // If not, wait for outgoing messages.
        // Either way, listen to incoming messages while we do this.
        if self.send_buffer.can_read() {
            tokio::select! {
                r = self.send_buffer.read_into_async(&mut self.write) => {
                    if let Err(e) = r {
                        error!("write bytes task failed: {}", e);
                        return TransportPollResult::Closed(StatusCode::BadCommunicationError);
                    }
                    TransportPollResult::OutgoingMessageSent
                }
                incoming = self.read.next() => {
                    self.handle_incoming_message(incoming)
                }
            }
        } else {
            if self.should_close {
                debug!("Writer is setting the connection state to finished(good)");
                return TransportPollResult::Closed(StatusCode::Good);
            }
            tokio::select! {
                outgoing = self.state.wait_for_outgoing_message(&mut self.send_buffer) => {
                    let Some((outgoing, request_id)) = outgoing else {
                        return TransportPollResult::Closed(StatusCode::Good);
                    };
                    let close_connection =
                        matches!(outgoing, RequestMessage::CloseSecureChannel(_));
                    if close_connection {
                        self.should_close = true;
                        debug!("Writer is about to send a CloseSecureChannelRequest which means it should close in a moment");
                    }
                    let secure_channel = trace_read_lock!(self.state.channel_state.secure_channel());
                    if let Err(e) = self.send_buffer.write(request_id, outgoing, &secure_channel) {
                        drop(secure_channel);
                        if let Some((request_id, request_handle)) = e.full_context() {
                            error!("Failed to send message with request handle {}: {}", request_handle, e);
                            self.state.message_send_failed(request_id, e.status());
                            TransportPollResult::RecoverableError(e.status())
                        } else {
                            TransportPollResult::Closed(e.status())
                        }
                    } else {
                        TransportPollResult::OutgoingMessage
                    }
                }
                incoming = self.read.next() => {
                    self.handle_incoming_message(incoming)
                }
            }
        }
    }
}

impl<R, W> Transport for StreamTransport<R, W>
where
    R: AsyncRead + Unpin + Send + Sync + 'static,
    W: AsyncWrite + Unpin + Send + Sync + 'static,
{
    async fn poll(&mut self) -> TransportPollResult {
        // We want poll to be cancel safe, this means that if we stop polling
        // a future returned from poll, we do not lose data or get in an
        // inconsistent state.
        // `poll_inner` is cancel safe, because all the async methods it
        // calls are cancel safe, and it only ever finishes one future.
        // The only thing that isn't cancel safe is when we close the channel.
        // `close` can be called multiple times, and will continue where it left off,
        // so all we have to do is keep calling close until we manage to complete it,
        // and _then_ we can set the state to `closed`.
        match self.closed {
            TransportCloseState::Open => {}
            TransportCloseState::Closing(c) => {
                // Close is kind-of cancel safe, in that
                // calling it multiple times is safe.
                let r = self.state.close(c).await;
                self.closed = TransportCloseState::Closed(c);
                return TransportPollResult::Closed(r);
            }
            TransportCloseState::Closed(c) => {
                return TransportPollResult::Closed(c);
            }
        }

        let r = self.poll_inner().await;
        if let TransportPollResult::Closed(status) = &r {
            self.closed = TransportCloseState::Closing(*status);
            let r = self.state.close(*status).await;
            self.closed = TransportCloseState::Closed(r);
        }
        r
    }

    fn connected_url(&self) -> &str {
        &self.connected_url
    }
}

/// Wait for a ReverseHello message from the given stream.
pub async fn wait_for_reverse_hello<R: AsyncRead + Unpin>(
    framed_read: &mut FramedRead<R, TcpCodec>,
) -> Result<ReverseHelloMessage, Error> {
    match framed_read.next().await {
        Some(Ok(Message::ReverseHello(rev_hello))) => {
            tracing::trace!("Received ReverseHello message: {:?}", rev_hello);
            Ok(rev_hello)
        }
        Some(Ok(_)) => Err(Error::new(
            StatusCode::BadConnectionClosed,
            "Unexpected message while waiting for ReverseHello",
        )),
        Some(Err(err)) => Err(Error::new(
            StatusCode::BadConnectionClosed,
            format!("Error while waiting for ReverseHello: {}", err),
        )),
        None => Err(Error::new(
            StatusCode::BadConnectionClosed,
            "Connection closed while waiting for ReverseHello",
        )),
    }
}