async-opcua-server 0.18.0

OPC UA server 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
use std::{
    sync::Arc,
    time::{Duration, Instant},
};

use opcua_core::{
    comms::{
        buffer::SendBuffer,
        chunker::Chunker,
        message_chunk::{MessageChunk, MessageIsFinalType},
        message_chunk_info::ChunkInfo,
        secure_channel::SecureChannel,
        sequence_number::SequenceNumberHandle,
        tcp_codec::{Message, TcpCodec},
        tcp_types::{AcknowledgeMessage, ErrorMessage},
    },
    RequestMessage, ResponseMessage,
};
use tracing::error;
use tracing_futures::Instrument;

use crate::info::ServerInfo;
use opcua_types::{DecodingOptions, Error, ResponseHeader, ServiceFault, StatusCode};

use futures::StreamExt;
use tokio::{
    io::{AsyncWriteExt, ReadHalf, WriteHalf},
    net::TcpStream,
};
use tokio_util::{codec::FramedRead, sync::CancellationToken};

use super::connect::Connector;

/// Transport implementation for opc.tcp.
pub(crate) struct TcpTransport {
    read: FramedRead<ReadHalf<TcpStream>, TcpCodec>,
    write: WriteHalf<TcpStream>,
    send_buffer: SendBuffer,
    state: TransportState,
    pending_chunks: Vec<MessageChunk>,
    /// Client protocol version set during HELLO
    pub(crate) client_protocol_version: u32,
    /// Last decoded sequence number
    sequence_numbers: SequenceNumberHandle,
}

enum TransportState {
    Running,
    Closing,
}

#[derive(Debug, Clone)]
pub(crate) struct TransportConfig {
    pub send_buffer_size: usize,
    pub receive_buffer_size: usize,
    pub max_message_size: usize,
    pub max_chunk_count: usize,
    pub hello_timeout: Duration,
}

#[derive(Debug)]
pub(crate) struct Request {
    pub message: RequestMessage,
    pub chunk_info: ChunkInfo,
    pub request_id: u32,
}

#[derive(Debug)]
/// Result of polling a TCP transport.
pub(crate) enum TransportPollResult {
    OutgoingMessageSent,
    IncomingChunk,
    IncomingMessage(Request),
    Error(StatusCode),
    RecoverableError(StatusCode, u32, u32),
    Closed,
}

fn min_zero_infinite(server: u32, client: u32) -> u32 {
    if client == 0 {
        server
    } else if server == 0 {
        client
    } else {
        client.min(server)
    }
}

pub(crate) struct TcpConnector {
    read: FramedRead<ReadHalf<TcpStream>, TcpCodec>,
    write: WriteHalf<TcpStream>,
    deadline: Instant,
    config: TransportConfig,
    decoding_options: DecodingOptions,
}

impl TcpConnector {
    pub(crate) fn new(
        stream: TcpStream,
        config: TransportConfig,
        decoding_options: DecodingOptions,
    ) -> Self {
        let (read, write) = tokio::io::split(stream);
        let read = FramedRead::new(read, TcpCodec::new(decoding_options.clone()));
        TcpConnector {
            read,
            write,
            deadline: Instant::now() + config.hello_timeout,
            config,
            decoding_options,
        }
    }

    pub(super) fn new_split(
        read: FramedRead<ReadHalf<TcpStream>, TcpCodec>,
        write: WriteHalf<TcpStream>,
        config: TransportConfig,
        decoding_options: DecodingOptions,
    ) -> Self {
        TcpConnector {
            read,
            write,
            deadline: Instant::now() + config.hello_timeout,
            config,
            decoding_options,
        }
    }

    async fn connect_inner(&mut self, info: Arc<ServerInfo>) -> Result<SendBuffer, ErrorMessage> {
        let hello = match self.read.next().await {
            Some(Ok(Message::Hello(hello))) => Ok(hello),
            Some(Ok(bad_msg)) => Err(ErrorMessage::new(
                StatusCode::BadCommunicationError,
                &format!("Expected a hello message, got {bad_msg:?} instead"),
            )),
            Some(Err(communication_err)) => Err(ErrorMessage::new(
                StatusCode::BadCommunicationError,
                &format!(
                    "Communication error while waiting for Hello message: {communication_err}"
                ),
            )),
            None => Err(ErrorMessage::new(
                StatusCode::BadCommunicationError,
                "Stream closed",
            )),
        }?;

        let mut buffer = SendBuffer::new(
            self.config.send_buffer_size,
            self.config.max_message_size,
            self.config.max_chunk_count,
            true,
        );

        let endpoints = info.endpoints(&hello.endpoint_url, &None);

        if !endpoints.is_some_and(|e| hello.is_endpoint_url_valid(&e)) {
            return Err(ErrorMessage::new(
                StatusCode::BadTcpEndpointUrlInvalid,
                "HELLO endpoint url is invalid",
            ));
        }
        if !hello.is_valid_buffer_sizes() {
            return Err(ErrorMessage::new(
                StatusCode::BadCommunicationError,
                "HELLO buffer sizes are invalid",
            ));
        }

        let server_protocol_version = 0;
        // Validate protocol version
        if hello.protocol_version > server_protocol_version {
            return Err(ErrorMessage::new(
                StatusCode::BadProtocolVersionUnsupported,
                "Client protocol version is unsupported.",
            ));
        }

        let decoding_options = &self.decoding_options;

        // Send acknowledge
        let acknowledge = AcknowledgeMessage::new(
            server_protocol_version,
            (self.config.receive_buffer_size as u32).min(hello.send_buffer_size),
            (buffer.send_buffer_size as u32).min(hello.receive_buffer_size),
            min_zero_infinite(
                decoding_options.max_message_size as u32,
                hello.max_message_size,
            ),
            min_zero_infinite(
                decoding_options.max_chunk_count as u32,
                hello.max_chunk_count,
            ),
        );
        buffer.revise(
            acknowledge.send_buffer_size as usize,
            acknowledge.max_message_size as usize,
            acknowledge.max_chunk_count as usize,
        );

        let mut buf =
            Vec::with_capacity(opcua_types::SimpleBinaryEncodable::byte_len(&acknowledge));
        opcua_types::SimpleBinaryEncodable::encode(&acknowledge, &mut buf)
            .map_err(|e| ErrorMessage::new(e.into(), "Failed to encode ack"))?;

        self.write.write_all(&buf).await.map_err(|e| {
            ErrorMessage::new(
                StatusCode::BadCommunicationError,
                &format!("Failed to send ack: {e}"),
            )
        })?;

        Ok(buffer)
    }
}

impl Connector for TcpConnector {
    async fn connect(
        mut self,
        info: Arc<ServerInfo>,
        token: CancellationToken,
    ) -> Result<TcpTransport, StatusCode> {
        let err = tokio::select! {
            _ = tokio::time::sleep_until(self.deadline.into()) => {
                ErrorMessage::new(StatusCode::BadTimeout, "Timeout waiting for HELLO")
            }
            _ = token.cancelled() => {
                ErrorMessage::new(StatusCode::BadServerHalted, "Server closed")
            }
            r = self.connect_inner(info).instrument(tracing::info_span!("OPC-UA TCP handshake")) => {
                match r {
                    Ok(r) => return Ok(TcpTransport::new(self.read, self.write, r)),
                    Err(e) => e,
                }
            }
        };

        // We want to send an error if connection failed for whatever reason, but
        // there's a good chance the channel is closed, so just ignore any errors.
        let mut buf = Vec::with_capacity(opcua_types::SimpleBinaryEncodable::byte_len(&err));
        if opcua_types::SimpleBinaryEncodable::encode(&err, &mut buf).is_ok() {
            let _ = self.write.write_all(&buf).await;
        }

        Err(err.error)
    }
}

impl TcpTransport {
    fn new(
        read: FramedRead<ReadHalf<TcpStream>, TcpCodec>,
        write: WriteHalf<TcpStream>,
        send_buffer: SendBuffer,
    ) -> Self {
        Self {
            read,
            write,
            state: TransportState::Running,
            pending_chunks: Vec::new(),
            sequence_numbers: SequenceNumberHandle::new(true),
            client_protocol_version: 0,
            send_buffer,
        }
    }

    /// Set the transport state to closing, once the final message is sent
    /// the connection will be closed.
    pub(crate) fn set_closing(&mut self) {
        self.state = TransportState::Closing;
    }

    pub(crate) fn is_closing(&self) -> bool {
        matches!(self.state, TransportState::Closing)
    }

    pub(crate) fn enqueue_error(&mut self, message: ErrorMessage) {
        self.send_buffer.write_error(message);
    }

    pub(crate) fn enqueue_message_for_send(
        &mut self,
        channel: &mut SecureChannel,
        message: ResponseMessage,
        request_id: u32,
    ) -> Result<(), StatusCode> {
        match self.send_buffer.write(request_id, message, channel) {
            Ok(_) => Ok(()),
            Err(e) => {
                tracing::warn!("Failed to encode outgoing message: {e:?}");
                if let Some((request_id, request_handle)) = e.full_context() {
                    self.send_buffer.write(
                        request_id,
                        ResponseMessage::ServiceFault(Box::new(ServiceFault {
                            response_header: ResponseHeader::new_service_result(
                                request_handle,
                                e.into(),
                            ),
                        })),
                        channel,
                    )?;
                    Ok(())
                } else {
                    Err(e.into())
                }
            }
        }
    }

    pub(crate) async fn poll(&mut self, channel: &mut SecureChannel) -> 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() {
            if let Err(e) = self.send_buffer.encode_next_chunk(channel) {
                return TransportPollResult::Error(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;
                    }
                    TransportPollResult::OutgoingMessageSent
                }
                incoming = self.read.next() => {
                    self.handle_incoming_message(incoming, channel)
                }
            }
        } else {
            if self.is_closing() {
                return TransportPollResult::Closed;
            }
            let incoming = self.read.next().await;
            self.handle_incoming_message(incoming, channel)
        }
    }

    fn handle_incoming_message(
        &mut self,
        incoming: Option<Result<Message, std::io::Error>>,
        channel: &mut SecureChannel,
    ) -> TransportPollResult {
        let Some(incoming) = incoming else {
            return TransportPollResult::Closed;
        };
        match incoming {
            Ok(message) => match self.process_message(message, channel) {
                Ok(None) => TransportPollResult::IncomingChunk,
                Ok(Some(message)) => {
                    self.pending_chunks.clear();
                    TransportPollResult::IncomingMessage(message)
                }
                Err(e) => {
                    self.pending_chunks.clear();
                    if let Some((id, handle)) = e.full_context() {
                        TransportPollResult::RecoverableError(e.status(), id, handle)
                    } else {
                        TransportPollResult::Error(e.status())
                    }
                }
            },
            Err(err) => {
                error!("Error reading from stream {:?}", err);
                TransportPollResult::Error(StatusCode::BadConnectionClosed)
            }
        }
    }

    fn process_message(
        &mut self,
        message: Message,
        channel: &mut SecureChannel,
    ) -> Result<Option<Request>, Error> {
        match message {
            Message::Chunk(chunk) => {
                let header = chunk.message_header(&channel.decoding_options())?;

                if header.is_final == MessageIsFinalType::FinalError {
                    self.pending_chunks.clear();
                    Ok(None)
                } else {
                    let chunk = channel.verify_and_remove_security_server(chunk.data)?;

                    if self.send_buffer.max_chunk_count > 0
                        && self.pending_chunks.len() == self.send_buffer.max_chunk_count
                    {
                        return Err(Error::decoding(format!(
                            "Message has more than {} chunks, exceeding negotiated limits",
                            self.send_buffer.max_chunk_count
                        )));
                    }
                    let chunk_info = chunk.chunk_info(channel)?;
                    self.sequence_numbers
                        .validate_and_increment(chunk_info.sequence_header.sequence_number)?;
                    self.pending_chunks.push(chunk);

                    if header.is_final == MessageIsFinalType::Intermediate {
                        return Ok(None);
                    }

                    let chunk_info = self.pending_chunks[0].chunk_info(channel)?;

                    Chunker::validate_chunks(channel, &self.pending_chunks)?;

                    let request = Chunker::decode(&self.pending_chunks, channel, None)
                        .map_err(|e| e.with_request_id(chunk_info.sequence_header.request_id))?;
                    Ok(Some(Request {
                        request_id: chunk_info.sequence_header.request_id,
                        chunk_info,
                        message: request,
                    }))
                }
            }
            unexpected => Err(Error::new(
                StatusCode::BadUnexpectedError,
                format!("Received unexpected message: {unexpected:?}"),
            )),
        }
    }
}