epicars 0.1.2

Standalone, pure rust implementation of EPICS CA protocol
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
#![allow(dead_code)]

use core::str;
use pnet::datalink;
use socket2::{Domain, Protocol, Type};
use std::{
    collections::HashMap,
    io::{self, Cursor},
    net::{IpAddr, Ipv4Addr, ToSocketAddrs},
    num::NonZeroUsize,
    time::{Duration, Instant},
};
use tokio::{
    io::AsyncWriteExt,
    net::{TcpListener, TcpStream, UdpSocket},
    sync::{broadcast, mpsc},
};
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};

use crate::{
    dbr::{DBR_BASIC_STRING, Dbr, DbrType},
    messages::{
        self, AccessRights, AsBytes, CAMessage, CreateChannel, CreateChannelResponse, ECAError,
        ErrorCondition, EventAddResponse, Message, MessageError, MonitorMask, ReadNotify,
        ReadNotifyResponse, Write, parse_search_packet,
    },
    providers::Provider,
};

#[doc(hidden)]
pub fn new_reusable_udp_socket<T: ToSocketAddrs>(address: T) -> io::Result<std::net::UdpSocket> {
    let socket = socket2::Socket::new(Domain::IPV4, Type::DGRAM, Some(Protocol::UDP))?;
    socket.set_reuse_port(true)?;
    socket.set_nonblocking(true)?;
    let addr = address.to_socket_addrs()?.next().unwrap();
    socket.bind(&addr.into())?;
    Ok(socket.into())
}

/// Serve data to CA clients by managing the Circuit/Channel lifecycles and interfacing with [`Provider`].
pub struct Server<L: Provider> {
    /// Broadcast port to sent beacons
    beacon_port: u16,
    /// Port to receive search queries on
    search_port: u16,
    /// Port to receive connections on, if specified
    connection_port: Option<u16>,
    /// Time that last beacon was sent
    last_beacon: Instant,
    /// The beacon ID of the last beacon broadcast
    beacon_id: u32,
    next_circuit_id: u64,
    circuits: Vec<Circuit<L>>,
    shutdown: CancellationToken,
    library_provider: L,
}

fn get_broadcast_ips() -> Vec<Ipv4Addr> {
    datalink::interfaces()
        .into_iter()
        .filter(|i| !i.is_loopback())
        .flat_map(|i| i.ips.into_iter())
        .filter_map(|i| match i.broadcast() {
            IpAddr::V4(broadcast_ip) => Some(broadcast_ip),
            _ => None,
        })
        .collect()
}

/// Try to bind a specific port, or if not supplied the default, or a random port if that fails.
async fn try_bind_ports(
    request_port: Option<u16>,
) -> Result<tokio::net::TcpListener, std::io::Error> {
    match request_port {
        None => {
            // Try binding TCP 5064
            if let Ok(socket) = tokio::net::TcpListener::bind("0.0.0.0:5064").await {
                return Ok(socket);
            }
            tokio::net::TcpListener::bind("0.0.0.0:0").await
        }
        Some(port) => tokio::net::TcpListener::bind(format!("0.0.0.0:{port}")).await,
    }
}

impl<L: Provider> Server<L> {
    async fn listen(&mut self) -> Result<(), std::io::Error> {
        // Create the TCP listener first so we know what port to advertise
        let request_port = self.connection_port;
        let connection_socket = try_bind_ports(request_port).await?;

        // Whatever we ended up with, we want to advertise
        let listen_port = connection_socket.local_addr().unwrap().port();

        self.listen_for_searches(listen_port);
        self.handle_tcp_connections(connection_socket);
        self.broadcast_beacons(listen_port).await?;

        Ok(())
    }

    async fn broadcast_beacons(&self, connection_port: u16) -> io::Result<()> {
        let beacon_port = self.beacon_port;
        let broadcast = UdpSocket::bind("0.0.0.0:0").await?;
        tokio::spawn(async move {
            broadcast.set_broadcast(true).unwrap();
            let mut message = messages::RsrvIsUp {
                server_port: connection_port,
                beacon_id: 0,
                ..Default::default()
            };
            loop {
                let mut writer = Cursor::new(Vec::new());
                message.write(&mut writer).unwrap();
                let message_bytes = writer.into_inner();
                let broadcast_ips = get_broadcast_ips();
                for i in broadcast_ips.iter() {
                    broadcast
                        .send_to(message_bytes.as_slice(), (*i, beacon_port))
                        .await
                        .unwrap();
                }
                info!(
                    "Broadcasting beacon {} to {} interfaces: {:?}",
                    message.beacon_id,
                    broadcast_ips.len(),
                    broadcast_ips,
                );
                message.beacon_id = message.beacon_id.wrapping_add(1);
                tokio::time::sleep(Duration::from_secs(15)).await;
            }
        });
        Ok(())
    }

    fn listen_for_searches(&self, connection_port: u16) {
        let search_port = self.search_port;
        let library_provider = self.library_provider.clone();
        tokio::spawn(async move {
            let mut buf: Vec<u8> = vec![0; 0xFFFF];
            let listener = UdpSocket::from_std(
                new_reusable_udp_socket(format!("0.0.0.0:{search_port}")).unwrap(),
            )
            .unwrap();

            info!(
                "Listening for searches on {:?}",
                listener.local_addr().unwrap()
            );
            // Keep track of recent searches and source ports, to help reject duplicate
            // messages coming in on multiple network ports.
            let mut recent_searches: Vec<(Instant, u16, Vec<messages::Search>)> = Vec::new();

            loop {
                let (size, origin) = listener.recv_from(&mut buf).await.unwrap();
                let msg_buf = &buf[..size];
                if let Ok(searches) = parse_search_packet(msg_buf) {
                    // Handle rejection window:
                    // Drop all requests for identical PVs from the same source port
                    let rejection_window_start = Instant::now()
                        .checked_sub(Duration::from_micros(500))
                        .unwrap();
                    let mut is_duplicate = false;
                    recent_searches.retain(|(i, port, msgs)| {
                        let keep = i > &rejection_window_start;
                        if *port == origin.port() && msgs == &searches && keep {
                            is_duplicate = true;
                        }
                        keep
                    });
                    if is_duplicate {
                        continue;
                    }
                    // This isn't a duplicate search, record it in case it comes in again
                    recent_searches.push((Instant::now(), origin.port(), searches.to_vec()));

                    let mut replies = Vec::new();
                    {
                        for search in searches {
                            if library_provider.provides(&search.channel_name) {
                                replies.push(search.respond(None, connection_port, true));
                            }
                        }
                    }
                    if !replies.is_empty() {
                        let mut reply_buf = Cursor::new(Vec::new());
                        messages::Version::default().write(&mut reply_buf).unwrap();
                        for reply in &replies {
                            reply.write(&mut reply_buf).unwrap();
                        }
                        listener
                            .send_to(&reply_buf.into_inner(), origin)
                            .await
                            .unwrap();
                        debug!("Sending {} search results", replies.len());
                    }
                } else {
                    error!("Got unparseable search message from {origin}");
                }
            }
        });
    }

    fn handle_tcp_connections(&mut self, listener: TcpListener) {
        let library = self.library_provider.clone();
        tokio::spawn(async move {
            let mut id = 0;
            loop {
                info!(
                    "Waiting to accept TCP connections on {}",
                    listener.local_addr().unwrap()
                );
                let (connection, client) = listener.accept().await.unwrap();
                debug!("  Got new stream from {client}");
                let circuit_library = library.clone();
                tokio::spawn(async move {
                    Circuit::start(id, connection, circuit_library).await;
                });
                id += 1;
            }
        });
    }
}

struct Circuit<L: Provider> {
    id: u64,
    last_message: Instant,
    client_version: u16,
    client_host_name: Option<String>,
    client_user_name: Option<String>,
    client_events_on: bool,
    library: L,
    channels: HashMap<u32, Channel>,
    next_channel_id: u32,
    monitor_value_available: mpsc::Sender<String>,
}

#[derive(Debug)]
struct Channel {
    name: String,
    client_id: u32,
    server_id: u32,
    subscription: Option<PVSubscription>,
}

#[derive(Debug)]
struct PVSubscription {
    data_type: DbrType,
    data_count: usize,
    subscription_id: u32,
    mask: MonitorMask,
    receiver: broadcast::Receiver<Dbr>,
}

impl<L: Provider> Circuit<L> {
    async fn do_version_exchange(stream: &mut TcpStream) -> Result<u16, MessageError> {
        // Send our Version
        stream
            .write_all(messages::Version::default().as_bytes().as_ref())
            .await?;
        // Immediately receive a Version message back from the client
        Ok(match Message::read_server_message(stream).await? {
            Message::Version(v) => v.protocol_version,
            err => {
                // This is an error, we cannot receive anything until we get this
                return Err(MessageError::UnexpectedMessage(err));
            }
        })
    }
    async fn start(id: u64, mut stream: TcpStream, library: L) {
        info!("{id}: Starting circuit with {:?}", stream.peer_addr());
        let client_version = Circuit::<L>::do_version_exchange(&mut stream)
            .await
            .unwrap();
        // Client version is the bare minimum we need to establish a valid circuit
        debug!("{id}: Got client version: {client_version}");
        let (monitor_value_available, mut monitor_updates) = mpsc::channel::<String>(32);
        let mut circuit = Circuit {
            id,
            last_message: Instant::now(),
            client_version,
            client_host_name: None,
            client_user_name: None,
            client_events_on: true,
            library,
            channels: HashMap::new(),
            next_channel_id: 0,
            monitor_value_available,
        };

        // Now, everything else is based on responding to events
        loop {
            tokio::select! {
                pv_name = monitor_updates.recv() => match pv_name {
                    Some(pv_name) => match circuit.handle_monitor_update(&pv_name).await {
                        Ok(messages) => {
                            for msg in messages {
                                debug!("{id}: Writing subscription update: {msg:?}");
                                stream.write_all(&msg.as_bytes()).await.unwrap()
                            }
                        },
                        Err(msg) => {
                            error!("{id} Error: Unexpected Error message {msg:?}");
                            continue;
                        },
                    }
                    None => {
                        panic!("Update monitor got all endpoints closed");
                    }
                },
                message = Message::read_server_message(&mut stream) => {
                    let message = match message {
                        Ok(message) => message,
                        Err(MessageError::IO(io)) => {
                            if io.kind() != io::ErrorKind::UnexpectedEof {
                                error!("{id}: IO Error reading server message: {io}");
                            }
                            break;
                        }
                        Err(MessageError::UnknownCommandId(command_id)) => {
                            error!("{id}: Error: Receieved unknown command id: {command_id}");
                            continue;
                        }
                        Err(MessageError::ParsingError(msg)) => {
                            error!("{id}: Error: Incoming message parse error: {msg}");
                            continue;
                        }
                        Err(MessageError::UnexpectedMessage(msg)) => {
                            error!("{id}: Error: Got message from client that is invalid to receive on a server: {msg:?}");
                            continue;
                        }
                        Err(MessageError::IncorrectCommandId(msg, expect)) => {
                            panic!("{id}: Fatal error: Got {msg} instead of {expect}")
                        }
                        Err(MessageError::InvalidField(message)) => {
                            error!("{id}: Got invalid message field: {message}");
                            continue;
                        }
                        Err(MessageError::ErrorResponse(message)) => {
                            error!("{id}: Got reading server messages generated error response: {message}");
                            continue;
                        }
                    };
                    match circuit.handle_message(message).await {
                        Ok(messages) => {
                            for msg in messages {
                                stream.write_all(&msg.as_bytes()).await.unwrap()
                            }
                        }
                        Err(MessageError::UnexpectedMessage(msg)) => {
                            error!("{id}: Error: Unexpected message: {msg:?}");
                            continue;
                        }
                        Err(msg) => {
                            error!("{id} Error: Unexpected Error message {msg:?}");
                            continue;
                        }
                    };
                }
            }
        }

        // If out here, we are closing the channel
        info!("{id}: Closing circuit");
        let _ = stream.shutdown().await;
    }

    async fn handle_monitor_update(&mut self, pv_name: &str) -> Result<Vec<Message>, MessageError> {
        let c = self
            .channels
            .values_mut()
            .find(|v| v.name == pv_name)
            .unwrap();
        debug!(
            "{}: {}: Got update notification for PV {}",
            self.id, c.server_id, c.name
        );
        let subscription = c.subscription.as_mut().unwrap();
        let dbr = subscription.receiver.recv().await.unwrap();

        info!("Circuit got update notification: {dbr:?}");
        let (item_count, data) = dbr
            .convert_to(subscription.data_type)
            .unwrap()
            .to_bytes(NonZeroUsize::new(subscription.data_count));
        Ok(vec![Message::EventAddResponse(EventAddResponse {
            data_type: subscription.data_type,
            data_count: item_count as u32,
            subscription_id: subscription.subscription_id,
            status_code: ErrorCondition::Normal,
            data,
        })])
    }

    async fn handle_message(&mut self, message: Message) -> Result<Vec<Message>, MessageError> {
        let id = self.id;
        match message {
            Message::Echo => Ok(vec![Message::Echo]),
            Message::EventAdd(msg) => {
                debug!("{id}: {}: Got {:?}", msg.server_id, msg);
                let channel = &mut self.channels.get_mut(&msg.server_id).unwrap();

                let receiver = self
                    .library
                    .monitor_value(
                        &channel.name,
                        msg.data_type,
                        msg.data_count as usize,
                        msg.mask,
                        self.monitor_value_available.clone(),
                    )
                    .map_err(MessageError::ErrorResponse)?;

                channel.subscription = Some(PVSubscription {
                    data_type: msg.data_type,
                    data_count: msg.data_count as usize,
                    mask: msg.mask,
                    subscription_id: msg.subscription_id,
                    receiver,
                });
                Ok(Vec::new())
            }
            Message::ClientName(name) if self.client_user_name.is_none() => {
                info!("{id}: Got client username: {}", name.name);
                self.client_user_name = Some(name.name);
                Ok(Vec::default())
            }
            Message::HostName(name) if self.client_host_name.is_none() => {
                info!("{id}: Got client hostname: {}", name.name);
                self.client_host_name = Some(name.name);
                Ok(Vec::default())
            }
            Message::CreateChannel(message) => {
                info!(
                    "{id}: Got request to create channel to: {}",
                    message.channel_name
                );
                let (messages, channel) = self.create_channel(message);
                if let Ok(channel) = channel {
                    self.channels.insert(channel.server_id, channel);
                }
                Ok(messages)
            }
            Message::ClearChannel(message) => {
                info!("{id}:{}: Request to clear channel", message.server_id);
                self.channels.remove(&message.server_id);
                Ok(Vec::default())
            }
            Message::ReadNotify(msg) => {
                info!("{id}:{}: ReadNotify request: {:?}", msg.server_id, msg);
                match self.do_read(&msg) {
                    Ok(r) => {
                        debug!("Sending response: {r:?}");
                        // self.stream.write_all(&r.as_bytes()).await?
                        Ok(vec![Message::ReadNotifyResponse(r)])
                    }

                    Err(e) => {
                        // Send an error in response to the read
                        let err = ECAError::new(e, msg.client_ioid, Message::ReadNotify(msg));
                        error!("Returning error: {err:?}");
                        // self.stream.write_all(&err.as_bytes()).await?
                        Ok(vec![Message::ECAError(err)])
                    }
                }
            }
            Message::Write(msg) => {
                debug!("{id}:{}: Write request: {:?}", msg.server_id, msg);
                if !self.do_write(&msg) {
                    Ok(vec![Message::ECAError(ECAError::new(
                        ErrorCondition::PutFail,
                        msg.client_ioid,
                        Message::Write(msg),
                    ))])
                } else {
                    Ok(Vec::default())
                }
            }
            msg => Err(MessageError::UnexpectedMessage(msg)),
        }
    }

    fn do_read(&self, request: &ReadNotify) -> Result<ReadNotifyResponse, ErrorCondition> {
        let channel = &self.channels[&request.server_id];
        let pv = self
            .library
            .read_value(&channel.name, Some(request.data_type))
            .unwrap();

        // Read the data into a Vec<u8>
        let (data_count, data) = pv
            .convert_to(request.data_type)?
            .to_bytes(NonZeroUsize::new(request.data_count as usize));
        Ok(request.respond(data_count, data))
    }

    fn do_write(&mut self, request: &Write) -> bool {
        assert!(request.data_type == DBR_BASIC_STRING);
        let channel = self.channels.get(&request.server_id).unwrap();

        let Ok(dbr) = Dbr::from_bytes(
            request.data_type,
            request.data_count as usize,
            &request.data,
        ) else {
            return false;
        };
        debug!("Got write request: {dbr:?}");
        self.library.write_value(&channel.name, dbr).is_ok()
    }

    fn create_channel(&mut self, message: CreateChannel) -> (Vec<Message>, Result<Channel, ()>) {
        let Ok(pv) = self.library.read_value(&message.channel_name, None) else {
            warn!(
                "Got a request for channel to '{}', which we do not appear to have.",
                message.channel_name
            );
            return (
                vec![Message::CreateChannelFailure(message.respond_failure())],
                Err(()),
            );
        };
        let access_rights = AccessRights {
            client_id: message.client_id,
            access_rights: self.library.get_access_right(
                &message.channel_name,
                self.client_user_name.as_deref(),
                self.client_host_name.as_deref(),
            ),
        };
        let id = self.next_channel_id;
        self.next_channel_id += 1;
        let createchan = CreateChannelResponse {
            data_count: pv.value().get_count() as u32,
            data_type: pv.data_type().basic_type,
            client_id: message.client_id,
            server_id: id,
        };
        info!(
            "{}:{}: Opening {:?} channel to {}",
            self.id, id, access_rights.access_rights, message.channel_name
        );
        // We have this channel, send the initial
        (
            vec![
                Message::AccessRights(access_rights),
                Message::CreateChannelResponse(createchan),
            ],
            Ok(Channel {
                name: message.channel_name,
                server_id: id,
                client_id: message.client_id,
                subscription: None,
            }),
        )
    }
}

/// Construct a [Server] object by setting up multiple aspects before running
pub struct ServerBuilder<L: Provider> {
    beacon_port: u16,
    search_port: u16,
    connection_port: Option<u16>,
    provider: L,
}

impl<L: Provider> ServerBuilder<L> {
    pub fn new(provider: L) -> ServerBuilder<L> {
        ServerBuilder {
            beacon_port: 5065,
            search_port: 5064,
            connection_port: None,
            provider,
        }
    }
    pub fn beacon_port(mut self, port: u16) -> ServerBuilder<L> {
        self.beacon_port = port;
        self
    }
    pub fn search_port(mut self, port: u16) -> ServerBuilder<L> {
        self.search_port = port;
        self
    }
    pub fn connection_port(mut self, port: u16) -> ServerBuilder<L> {
        self.connection_port = Some(port);
        self
    }
    pub async fn start(self) -> io::Result<Server<L>> {
        let mut server = Server {
            beacon_port: self.beacon_port,
            search_port: self.search_port,
            connection_port: self.connection_port,
            library_provider: self.provider,
            last_beacon: Instant::now(),
            beacon_id: 0,
            next_circuit_id: 0,
            circuits: Vec::new(),
            shutdown: CancellationToken::new(),
        };
        server.listen().await?;
        Ok(server)
    }
}