veilid-core 0.5.3

Core library used to create a Veilid node and operate it as part of an application
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
mod discovery_context;
mod igd_manager;
mod network_state;
mod network_tcp;
mod network_udp;
mod protocol;
mod start_protocols;
mod tasks;

pub(super) use protocol::*;

use super::*;
use crate::routing_table::*;
use connection_manager::*;
use discovery_context::*;
use network_state::*;
use network_tcp::*;
use protocol::tcp::RawTcpProtocolHandler;
use protocol::udp::RawUdpProtocolHandler;
use protocol::ws::WebsocketProtocolHandler;
use start_protocols::*;

use futures_rustls::{
    pki_types::{
        pem::PemObject as _, CertificateDer, PrivateKeyDer, PrivatePkcs1KeyDer, PrivatePkcs8KeyDer,
    },
    rustls::server::ServerConfig,
    TlsAcceptor,
};
use futures_util::StreamExt;
use std::fs::File;
use std::io;
use std::io::BufReader;
use std::path::{Path, PathBuf};

/////////////////////////////////////////////////////////////////

impl_veilid_log_facility!("net");

pub const MAX_DIAL_INFO_FAILURE_COUNT: usize = 100;
pub const UPDATE_OUTBOUND_ONLY_DIAL_INFO_PERIOD_SECS: u32 = 10;
pub const UPDATE_DIAL_INFO_TASK_TICK_PERIOD_SECS: u32 = 1;
pub const NETWORK_INTERFACES_TASK_TICK_PERIOD_SECS: u32 = 1;
pub const UPNP_TASK_TICK_PERIOD_SECS: u32 = 1;
pub const HOLE_PUNCH_TTL: u32 = 3;
pub const PEEK_DETECT_LEN: usize = 64;

/////////////////////////////////////////////////////////////////

struct NetworkInner {
    /// set if the network needs to be restarted due to a low level configuration change
    /// such as dhcp release or change of address or interfaces being added or removed
    network_needs_restart: bool,
    /// the number of consecutive dial info failures per routing domain,
    /// which may indicate the network is down for that domain
    dial_info_failure_count: BTreeMap<RoutingDomain, usize>,
    /// if we need to redo the publicinternet network class
    needs_update_dial_info: bool,
    /// result of resolving 'auto'/None detect_address_changes mode
    resolved_detect_address_changes: bool,
    /// the next time we are allowed to check for better dialinfo when we are OutboundOnly
    next_outbound_only_dial_info_check: Timestamp,
    /// join handles for all the low level network background tasks
    join_handles: Vec<MustJoinHandle<()>>,
    /// stop source for shutting down the low level network background tasks
    stop_source: Option<StopSource>,
    /// Actual bound addresses per protocol
    bound_address_per_protocol: BTreeMap<ProtocolType, Vec<SocketAddr>>,
    /// mapping of protocol handlers to accept or send messages from a set of bound socket addresses
    udp_protocol_handlers: BTreeMap<SocketAddr, RawUdpProtocolHandler>,
    /// TLS handling socket controller
    tls_acceptor: Option<TlsAcceptor>,
    /// Multiplexer record for protocols on low level TCP sockets
    listener_states: BTreeMap<SocketAddr, Arc<RwLock<ListenerState>>>,
    /// Preferred local addresses for protocols/address combinations for outgoing connections
    preferred_local_addresses: BTreeMap<(ProtocolType, AddressType), SocketAddr>,
    /// set of statically configured protocols with public dialinfo
    static_public_dial_info: ProtocolTypeSet,
    /// Network state
    network_state: Option<Arc<NetworkState>>,
}

pub(super) struct NetworkUnlockedInner {
    // Startup lock
    startup_lock: StartupLock,

    // Network
    interfaces: NetworkInterfaces,

    // Background processes
    update_dial_info_task: TickTask<EyreReport>,
    network_interfaces_task: TickTask<EyreReport>,
    upnp_task: TickTask<EyreReport>,
    network_task_lock: AsyncMutex<()>,

    // Managers
    igd_manager: igd_manager::IGDManager,
}

#[derive(Clone)]
pub(super) struct Network {
    registry: VeilidComponentRegistry,
    inner: Arc<Mutex<NetworkInner>>,
    unlocked_inner: Arc<NetworkUnlockedInner>,
}

impl_veilid_component_accessors!(Network);

impl core::ops::Deref for Network {
    type Target = NetworkUnlockedInner;

    fn deref(&self) -> &Self::Target {
        &self.unlocked_inner
    }
}

impl Network {
    fn new_inner() -> NetworkInner {
        NetworkInner {
            network_needs_restart: false,
            dial_info_failure_count: BTreeMap::new(),
            needs_update_dial_info: false,
            resolved_detect_address_changes: false,
            next_outbound_only_dial_info_check: Timestamp::default(),
            join_handles: Vec::new(),
            stop_source: None,
            bound_address_per_protocol: BTreeMap::new(),
            udp_protocol_handlers: BTreeMap::new(),
            tls_acceptor: None,
            listener_states: BTreeMap::new(),
            preferred_local_addresses: BTreeMap::new(),
            static_public_dial_info: ProtocolTypeSet::new(),
            network_state: None,
        }
    }

    fn new_unlocked_inner(registry: VeilidComponentRegistry) -> NetworkUnlockedInner {
        NetworkUnlockedInner {
            startup_lock: StartupLock::new(),
            interfaces: NetworkInterfaces::new(),
            update_dial_info_task: TickTask::new(
                "update_dial_info_task",
                UPDATE_DIAL_INFO_TASK_TICK_PERIOD_SECS,
            ),
            network_interfaces_task: TickTask::new(
                "network_interfaces_task",
                NETWORK_INTERFACES_TASK_TICK_PERIOD_SECS,
            ),
            upnp_task: TickTask::new("upnp_task", UPNP_TASK_TICK_PERIOD_SECS),
            network_task_lock: AsyncMutex::new(()),
            igd_manager: igd_manager::IGDManager::new(registry),
        }
    }

    pub fn new(registry: VeilidComponentRegistry) -> Self {
        let this = Self {
            inner: Arc::new(Mutex::new(Self::new_inner())),
            unlocked_inner: Arc::new(Self::new_unlocked_inner(registry.clone())),
            registry,
        };

        this.setup_tasks();

        this
    }

    fn load_certs(path: &Path) -> io::Result<Vec<CertificateDer<'static>>> {
        let cvec = CertificateDer::<'static>::pem_reader_iter(&mut BufReader::new(
            // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path
            File::open(path)?,
        ))
        .collect::<Result<Vec<CertificateDer<'static>>, futures_rustls::pki_types::pem::Error>>()
        .map_err(io::Error::other)?;
        Ok(cvec)
    }

    fn load_keys(path: &Path) -> io::Result<Vec<PrivateKeyDer<'static>>> {
        {
            if let Ok(v) = PrivatePkcs1KeyDer::<'static>::pem_reader_iter(&mut BufReader::new(
                // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path
                File::open(path)?,
            ))
            .collect::<Result<Vec<PrivatePkcs1KeyDer<'static>>, futures_rustls::pki_types::pem::Error>>()
            {
                if !v.is_empty() {
                    return Ok(v
                        .into_iter()
                        .map(PrivateKeyDer::Pkcs1)
                        .collect::<Vec<PrivateKeyDer<'static>>>());
                }
            }
        }
        {
            if let Ok(v) = PrivatePkcs8KeyDer::<'static>::pem_reader_iter(&mut BufReader::new(
                // nosemgrep: rust.actix.path-traversal.tainted-path.tainted-path
                File::open(path)?,
            ))
            .collect::<Result<Vec<PrivatePkcs8KeyDer<'static>>,futures_rustls::pki_types::pem::Error>>()
            {
                if !v.is_empty() {
                    return Ok(v.into_iter().map(PrivateKeyDer::Pkcs8).collect());
                }
            }
        }

        Err(io::Error::new(
            io::ErrorKind::InvalidInput,
            "invalid TLS private key",
        ))
    }

    fn load_server_config(&self) -> io::Result<ServerConfig> {
        let config = self.config();
        //
        veilid_log!(self trace
            "loading certificate from {}",
            config.network.tls.certificate_path
        );
        let certs_path = PathBuf::from(&config.network.tls.certificate_path);
        let certs = Self::load_certs(&certs_path)?;
        veilid_log!(self trace "loaded {} certificates", certs.len());
        if certs.is_empty() {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, format!("Certificates at {} could not be loaded.\nEnsure it is in PEM format, beginning with '-----BEGIN CERTIFICATE-----'",config.network.tls.certificate_path)));
        }
        //
        veilid_log!(self trace
            "loading private key from {}",
            config.network.tls.private_key_path
        );
        let keys_path = PathBuf::from(&config.network.tls.private_key_path);
        let mut keys = Self::load_keys(&keys_path)?;
        veilid_log!(self trace "loaded {} keys", keys.len());
        if keys.is_empty() {
            return Err(io::Error::new(io::ErrorKind::InvalidInput, format!("Private key at {} could not be loaded.\nEnsure it is unencrypted and in RSA or PKCS8 format, beginning with '-----BEGIN RSA PRIVATE KEY-----' or '-----BEGIN PRIVATE KEY-----'",config.network.tls.private_key_path)));
        }

        let config = ServerConfig::builder()
            .with_no_client_auth()
            .with_single_cert(certs, keys.remove(0))
            .map_err(|err| io::Error::new(io::ErrorKind::InvalidInput, err))?;

        Ok(config)
    }

    fn add_to_join_handles(&self, jh: MustJoinHandle<()>) {
        let mut inner = self.inner.lock();
        inner.join_handles.push(jh);
    }

    fn translate_unspecified_address(&self, from: SocketAddr) -> Vec<SocketAddr> {
        if !from.ip().is_unspecified() {
            vec![from]
        } else {
            self.last_network_state()
                .unwrap_or_log()
                .interface_address_state
                .interface_addresses
                .iter()
                .filter_map(|a| {
                    // We create sockets that are only ipv6 or ipv6 (not dual, so only translate matching unspecified address)
                    if (a.ip().is_ipv4() && from.ip().is_ipv4())
                        || (a.ip().is_ipv6() && from.ip().is_ipv6())
                    {
                        Some(SocketAddr::new(a.ip(), from.port()))
                    } else {
                        None
                    }
                })
                .collect()
        }
    }

    pub fn get_preferred_local_address(&self, dial_info: &DialInfo) -> Option<SocketAddr> {
        let inner = self.inner.lock();
        let key = (dial_info.protocol_type(), dial_info.address_type());
        inner.preferred_local_addresses.get(&key).copied()
    }

    pub fn get_preferred_local_address_by_key(
        &self,
        pt: ProtocolType,
        at: AddressType,
    ) -> Option<SocketAddr> {
        let inner = self.inner.lock();
        let key = (pt, at);
        inner.preferred_local_addresses.get(&key).copied()
    }

    ////////////////////////////////////////////////////////////

    // Record DialInfo failures
    async fn record_dial_info_failure<T, F: Future<Output = EyreResult<NetworkResult<T>>>>(
        &self,
        dial_info: DialInfo,
        fut: F,
    ) -> EyreResult<NetworkResult<T>> {
        let opt_routing_domain = self
            .routing_table()
            .routing_domain_for_address(dial_info.address());

        let network_result = pin_future_closure!(fut).await?;
        if matches!(
            network_result,
            NetworkResult::NoConnection(_) | NetworkResult::Timeout
        ) {
            // Dial info failure
            self.network_manager()
                .address_filter()
                .set_dial_info_failed(dial_info);

            // Increment consecutive failure count for this routing domain
            if let Some(rd) = opt_routing_domain {
                let dial_info_failure_count = {
                    let mut inner = self.inner.lock();
                    *inner
                        .dial_info_failure_count
                        .entry(rd)
                        .and_modify(|x| *x += 1)
                        .or_insert(1)
                };

                if dial_info_failure_count == MAX_DIAL_INFO_FAILURE_COUNT {
                    veilid_log!(self debug "Node may be offline. Exceeded maximum dial info failure count for {:?}", rd);
                    // todo: what operations should we perform here?
                    // self.set_needs_dial_info_check(rd);
                }
            }
        } else {
            // Dial info success

            // Clear failure count for this routing domain
            if let Some(rd) = opt_routing_domain {
                let mut inner = self.inner.lock();
                inner.dial_info_failure_count.remove(&rd);
            }
        }
        Ok(network_result)
    }

    // Send data to a dial info, unbound, using a new connection from a random port
    // This creates a short-lived connection in the case of connection-oriented protocols
    // for the purpose of sending this one message.
    // This bypasses the connection table as it is not a 'node to node' connection.
    #[cfg_attr(feature = "instrument", instrument(level="trace", target="net", err, skip(self, data), fields(data.len = data.len())))]
    pub async fn send_data_unbound_to_dial_info(
        &self,
        dial_info: DialInfo,
        data: Bytes,
    ) -> EyreResult<NetworkResult<()>> {
        let _guard = self.startup_lock.enter()?;

        self.record_dial_info_failure(
            dial_info.clone(),
            async move {
                let data_len = data.len();
                let connect_timeout_ms = self.config().network.connection_initial_timeout_ms;

                if self
                    .network_manager()
                    .address_filter()
                    .is_ip_addr_punished(dial_info.address().ip_addr())
                {
                    return Ok(NetworkResult::no_connection_other("punished"));
                }

                match dial_info.protocol_type() {
                    ProtocolType::UDP => {
                        let peer_socket_addr = dial_info.to_socket_addr();
                        let h = RawUdpProtocolHandler::new_unspecified_bound_handler(
                            self.registry(),
                            &peer_socket_addr,
                        )
                        .wrap_err("create socket failure")?;
                        let _ = network_result_try!(h
                            .send_message(data, peer_socket_addr)
                            .await
                            .map(NetworkResult::Value)
                            .wrap_err("send message failure")?);
                    }
                    ProtocolType::TCP => {
                        let peer_socket_addr = dial_info.to_socket_addr();
                        let pnc = network_result_try!(RawTcpProtocolHandler::connect(
                            self.registry(),
                            None,
                            peer_socket_addr,
                            connect_timeout_ms
                        )
                        .await
                        .wrap_err("connect failure")?);
                        network_result_try!(pnc.send(data).await.wrap_err("send failure")?);
                    }
                    ProtocolType::WS => {
                        let pnc = network_result_try!(WebsocketProtocolHandler::connect(
                            self.registry(),
                            None,
                            &dial_info,
                            connect_timeout_ms
                        )
                        .await
                        .wrap_err("connect failure")?);
                        network_result_try!(pnc.send(data).await.wrap_err("send failure")?);
                    }
                    #[cfg(feature = "enable-protocol-wss")]
                    ProtocolType::WSS => {
                        let pnc = network_result_try!(WebsocketProtocolHandler::connect(
                            self.registry(),
                            None,
                            &dial_info,
                            connect_timeout_ms
                        )
                        .await
                        .wrap_err("connect failure")?);
                        network_result_try!(pnc.send(data).await.wrap_err("send failure")?);
                    }
                }
                // Network accounting
                self.network_manager()
                    .stats_packet_sent(dial_info.ip_addr(), ByteCount::new(data_len as u64));

                Ok(NetworkResult::Value(()))
            }
            .in_current_span(),
        )
        .await
    }

    // Send data to a dial info, unbound, using a new connection from a random port
    // Waits for a specified amount of time to receive a single response
    // This creates a short-lived connection in the case of connection-oriented protocols
    // for the purpose of sending this one message.
    // This bypasses the connection table as it is not a 'node to node' connection.
    #[cfg_attr(feature = "instrument", instrument(level="trace", target="net", err, skip(self, data), fields(data.len = data.len())))]
    pub async fn send_recv_data_unbound_to_dial_info(
        &self,
        dial_info: DialInfo,
        data: Bytes,
        timeout_ms: u32,
    ) -> EyreResult<NetworkResult<Bytes>> {
        let _guard = self.startup_lock.enter()?;

        self.record_dial_info_failure(
            dial_info.clone(),
            async move {
                let data_len = data.len();
                let connect_timeout_ms = self.config().network.connection_initial_timeout_ms;

                if self
                    .network_manager()
                    .address_filter()
                    .is_ip_addr_punished(dial_info.address().ip_addr())
                {
                    return Ok(NetworkResult::no_connection_other("punished"));
                }

                match dial_info.protocol_type() {
                    // Connectionless protocols
                    ProtocolType::UDP => {
                        let peer_socket_addr = dial_info.to_socket_addr();
                        let h = RawUdpProtocolHandler::new_unspecified_bound_handler(
                            self.registry(),
                            &peer_socket_addr,
                        )
                        .wrap_err("create socket failure")?;
                        network_result_try!(h
                            .send_message(data, peer_socket_addr)
                            .await
                            .wrap_err("send message failure")?);
                        self.network_manager().stats_packet_sent(
                            dial_info.ip_addr(),
                            ByteCount::new(data_len as u64),
                        );

                        // receive single response
                        let mut out = BytesMut::zeroed(MAX_MESSAGE_SIZE);
                        let (recv_len, recv_addr) = network_result_try!(timeout(
                            timeout_ms,
                            h.recv_message(&mut out).in_current_span()
                        )
                        .await
                        .into_network_result())
                        .wrap_err("recv_message failure")?;

                        let recv_socket_addr = recv_addr.remote_address().socket_addr();
                        self.network_manager().stats_packet_rcvd(
                            recv_socket_addr.ip(),
                            ByteCount::new(recv_len as u64),
                        );

                        // if the from address is not the same as the one we sent to, then drop this
                        if recv_socket_addr != peer_socket_addr {
                            bail!("wrong address");
                        }
                        out.resize(recv_len, 0u8);
                        Ok(NetworkResult::Value(out.into()))
                    }
                    // Connection-oriented protocols
                    _ => {
                        let pnc = network_result_try!(match dial_info.protocol_type() {
                            ProtocolType::UDP => unreachable!(),
                            ProtocolType::TCP => {
                                let peer_socket_addr = dial_info.to_socket_addr();
                                RawTcpProtocolHandler::connect(
                                    self.registry(),
                                    None,
                                    peer_socket_addr,
                                    connect_timeout_ms,
                                )
                                .await
                                .wrap_err("connect failure")?
                            }
                            ProtocolType::WS => {
                                WebsocketProtocolHandler::connect(
                                    self.registry(),
                                    None,
                                    &dial_info,
                                    connect_timeout_ms,
                                )
                                .await
                                .wrap_err("connect failure")?
                            }
                            #[cfg(feature = "enable-protocol-wss")]
                            ProtocolType::WSS => {
                                WebsocketProtocolHandler::connect(
                                    self.registry(),
                                    None,
                                    &dial_info,
                                    connect_timeout_ms,
                                )
                                .await
                                .wrap_err("connect failure")?
                            }
                        });

                        network_result_try!(pnc.send(data).await.wrap_err("send failure")?);
                        self.network_manager().stats_packet_sent(
                            dial_info.ip_addr(),
                            ByteCount::new(data_len as u64),
                        );

                        let out = network_result_try!(network_result_try!(timeout(
                            timeout_ms,
                            pnc.recv().in_current_span()
                        )
                        .await
                        .into_network_result())
                        .wrap_err("recv failure")?);

                        self.network_manager().stats_packet_rcvd(
                            dial_info.ip_addr(),
                            ByteCount::new(out.len() as u64),
                        );

                        Ok(NetworkResult::Value(out))
                    }
                }
            }
            .in_current_span(),
        )
        .await
    }

    #[cfg_attr(feature = "instrument", instrument(level="trace", target="net", err, skip(self, data), fields(data.len = data.len())))]
    pub async fn send_data_to_existing_flow(
        &self,
        flow: Flow,
        data: Bytes,
    ) -> EyreResult<SendDataToExistingFlowResult> {
        let _guard = self.startup_lock.enter()?;

        let data_len = data.len();

        // Handle connectionless protocol
        if flow.protocol_type() == ProtocolType::UDP {
            // send over the best udp socket we have bound since UDP is not connection oriented
            let peer_socket_addr = flow.remote().socket_addr();
            if let Some(ph) = self.find_best_udp_protocol_handler(
                &peer_socket_addr,
                &flow.local().map(|sa| sa.socket_addr()),
            ) {
                network_result_value_or_log!(self ph.clone()
                    .send_message(data.clone(), peer_socket_addr)
                    .await
                    .wrap_err("sending data to existing flow")? => [ format!(": data.len={}, flow={:?}", data.len(), flow) ]
                    { return Ok(SendDataToExistingFlowResult::NotSent(data)); } );

                // Network accounting
                self.network_manager()
                    .stats_packet_sent(peer_socket_addr.ip(), ByteCount::new(data_len as u64));

                // Data was consumed
                let unique_flow = UniqueFlow {
                    flow,
                    connection_id: None,
                };
                return Ok(SendDataToExistingFlowResult::Sent(unique_flow));
            }
        }

        // Handle connection-oriented protocols

        // Try to send to the exact existing connection if one exists
        if let Some(conn) = self
            .network_manager()
            .connection_manager()
            .get_connection(flow)
        {
            // connection exists, send over it
            match conn.send_async(data).await {
                ConnectionHandleSendResult::Sent => {
                    // Network accounting
                    self.network_manager().stats_packet_sent(
                        flow.remote().socket_addr().ip(),
                        ByteCount::new(data_len as u64),
                    );

                    // Data was consumed
                    return Ok(SendDataToExistingFlowResult::Sent(conn.unique_flow()));
                }
                ConnectionHandleSendResult::NotSent(data) => {
                    // Couldn't send
                    // Pass the data back out so we don't own it any more
                    return Ok(SendDataToExistingFlowResult::NotSent(data));
                }
            }
        }
        // Connection didn't exist
        // Pass the data back out so we don't own it any more
        Ok(SendDataToExistingFlowResult::NotSent(data))
    }

    // Send data directly to a dial info, possibly without knowing which node it is going to
    // Returns a flow for the connection used to send the data
    #[cfg_attr(feature = "instrument", instrument(level="trace", target="net", err, skip(self, data), fields(data.len = data.len())))]
    pub async fn send_data_to_dial_info(
        &self,
        dial_info: DialInfo,
        data: Bytes,
    ) -> EyreResult<NetworkResult<UniqueFlow>> {
        let _guard = self.startup_lock.enter()?;

        self.record_dial_info_failure(
            dial_info.clone(),
            async move {
                let data_len = data.len();
                let unique_flow;
                if dial_info.protocol_type() == ProtocolType::UDP {
                    // Handle connectionless protocol
                    let peer_socket_addr = dial_info.to_socket_addr();
                    let ph = match self.find_best_udp_protocol_handler(&peer_socket_addr, &None) {
                        Some(ph) => ph,
                        None => {
                            return Ok(NetworkResult::no_connection_other(
                                "no appropriate UDP protocol handler for dial_info",
                            ));
                        }
                    };
                    let flow = network_result_try!(ph
                        .send_message(data, peer_socket_addr)
                        .await
                        .wrap_err("failed to send data to dial info")?);
                    unique_flow = UniqueFlow {
                        flow,
                        connection_id: None,
                    };
                } else {
                    // Handle connection-oriented protocols
                    let connmgr = self.network_manager().connection_manager();
                    let conn = network_result_try!(
                        connmgr.get_or_create_connection(dial_info.clone()).await?
                    );

                    if let ConnectionHandleSendResult::NotSent(_) = conn.send_async(data).await {
                        return Ok(NetworkResult::NoConnection(io::Error::new(
                            io::ErrorKind::ConnectionReset,
                            "failed to send",
                        )));
                    }
                    unique_flow = conn.unique_flow();
                }

                // Network accounting
                self.network_manager()
                    .stats_packet_sent(dial_info.ip_addr(), ByteCount::new(data_len as u64));

                Ok(NetworkResult::value(unique_flow))
            }
            .in_current_span(),
        )
        .await
    }

    // Send hole punch attempt to a specific dialinfo. May not be appropriate for all protocols.
    // Returns a flow for the connection used to send the data
    #[cfg_attr(
        feature = "instrument",
        instrument(level = "trace", target = "net", err, skip(self), fields(__VEILID_LOG_KEY = self.log_key()))
    )]
    pub async fn send_hole_punch(
        &self,
        dial_info: DialInfo,
    ) -> EyreResult<NetworkResult<UniqueFlow>> {
        let _guard = self.startup_lock.enter()?;

        self.record_dial_info_failure(
            dial_info.clone(),
            async move {
                let unique_flow;
                if dial_info.protocol_type().low_level_protocol_type() == LowLevelProtocolType::UDP
                {
                    // Handle connectionless protocol
                    let peer_socket_addr = dial_info.to_socket_addr();
                    let ph = match self.find_best_udp_protocol_handler(&peer_socket_addr, &None) {
                        Some(ph) => ph,
                        None => {
                            return Ok(NetworkResult::no_connection_other(
                                "no appropriate UDP protocol handler for dial_info",
                            ));
                        }
                    };
                    let flow = network_result_try!(ph
                        .send_hole_punch(peer_socket_addr, HOLE_PUNCH_TTL)
                        .await
                        .wrap_err("failed to send hole punch to dial info")?);
                    unique_flow = UniqueFlow {
                        flow,
                        connection_id: None,
                    };
                } else {
                    return Ok(NetworkResult::ServiceUnavailable(
                        "unimplemented for this protocol".to_owned(),
                    ));
                }

                // Network accounting
                self.network_manager()
                    .stats_packet_sent(dial_info.ip_addr(), ByteCount::new(0));

                Ok(NetworkResult::value(unique_flow))
            }
            .in_current_span(),
        )
        .await
    }

    /////////////////////////////////////////////////////////////////

    pub async fn startup_internal(&self) -> EyreResult<StartupDisposition> {
        // Get the initial network state snapshot
        // Caution: this -must- happen first because we use unwrap()
        let network_state = self.refresh_network_state().await?.unwrap_or_log();

        // Resolve 'auto'/None config fo detect_address_changes
        let resolved_detect_address_changes = {
            let mut inner = self.inner.lock();

            // Create the shutdown stopper
            inner.stop_source = Some(StopSource::new());

            // Process the detect_address_changes 'auto' mode
            let detect_address_changes = self.config().network.detect_address_changes;
            if let Some(detect_address_changes) = detect_address_changes {
                inner.resolved_detect_address_changes = detect_address_changes;
                if inner.resolved_detect_address_changes {
                    veilid_log!(self info "Manually-enabled detection of address changes");
                } else {
                    veilid_log!(self info "Manually-disabled detection of address changes");
                }
            } else {
                // Check for publicly routable IPv4 and IPv6 addresses on the local interfaces
                let mut global_ipv4 = false;
                let mut global_ipv6 = false;
                for siaddr in network_state
                    .interface_address_state
                    .interface_addresses
                    .iter()
                {
                    if Address::from_ip_addr(siaddr.ip()).is_global() {
                        match siaddr {
                            IfAddr::V4(_) => {
                                global_ipv4 = true;
                            }
                            IfAddr::V6(_) => {
                                global_ipv6 = true;
                            }
                        }
                    }
                }

                // If both ipv4 and ipv6 global addresses are present, turn off detect_address_changes otherwise turn it on
                inner.resolved_detect_address_changes = !(global_ipv4 && global_ipv6);
                if inner.resolved_detect_address_changes {
                    veilid_log!(self info "Auto-enabled detection of address changes: global_ipv4={}, global_ipv6={}", global_ipv4, global_ipv6);
                } else {
                    veilid_log!(self info "Auto-disabled detection of address changes because this node has global IPv4 and IPv6 addresses");
                }
            }
            inner.resolved_detect_address_changes
        };

        // Start editing routing table
        let routing_table = self.routing_table();
        let confirmed_public_internet;

        // Guard to limit lifetime of editors
        {
            let mut editor_public_internet = routing_table.edit_public_internet_routing_domain();
            let mut editor_local_network = routing_table.edit_local_network_routing_domain();

            // Setup network
            editor_local_network.set_interface_addresses(
                network_state
                    .interface_address_state
                    .as_ref()
                    .interface_addresses
                    .clone(),
            );
            editor_local_network.setup_network(
                network_state.protocol_config.outbound,
                network_state.protocol_config.inbound,
                network_state.protocol_config.family_local,
                network_state
                    .protocol_config
                    .local_network_capabilities
                    .clone(),
                true,
            );

            confirmed_public_internet = !resolved_detect_address_changes
                || self.config().network.privacy.require_inbound_relay;
            editor_public_internet.set_interface_addresses(
                network_state
                    .interface_address_state
                    .as_ref()
                    .interface_addresses
                    .clone(),
            );
            editor_public_internet.setup_network(
                network_state.protocol_config.outbound,
                network_state.protocol_config.inbound,
                network_state.protocol_config.family_global,
                network_state
                    .protocol_config
                    .public_internet_capabilities
                    .clone(),
                confirmed_public_internet,
            );

            // Start listeners
            if network_state
                .protocol_config
                .inbound
                .contains(ProtocolType::UDP)
            {
                let res = self.bind_udp_protocol_handlers();
                if !matches!(res, Ok(StartupDisposition::Success)) {
                    return res;
                }
            }
            if network_state
                .protocol_config
                .inbound
                .contains(ProtocolType::WS)
            {
                let res = self.start_ws_listeners();
                if !matches!(res, Ok(StartupDisposition::Success)) {
                    return res;
                }
            }

            #[cfg(feature = "enable-protocol-wss")]
            if network_state
                .protocol_config
                .inbound
                .contains(ProtocolType::WSS)
            {
                let res = self.start_wss_listeners();
                if !matches!(res, Ok(StartupDisposition::Success)) {
                    return res;
                }
            }
            if network_state
                .protocol_config
                .inbound
                .contains(ProtocolType::TCP)
            {
                let res = self.start_tcp_listeners();
                if !matches!(res, Ok(StartupDisposition::Success)) {
                    return res;
                }
            }

            // Register all dialinfo
            self.register_all_dial_info(&mut editor_public_internet, &mut editor_local_network)?;

            // Commit routing domain edits
            if editor_public_internet.commit(true).await {
                editor_public_internet.publish();
            }
            if editor_local_network.commit(true).await {
                editor_local_network.publish();
            }
        }

        if !confirmed_public_internet {
            // Update public internet network class if we haven't confirmed it
            self.trigger_update_dial_info(RoutingDomain::PublicInternet);
        } else {
            // Warn if we have no public dialinfo, because we're not going to magically find some
            // with detect_address_changes turned off. Skip the warning if require_inbound_relay is
            // enabled, this option intentionally disables publishing any dialinfo.
            let pi = routing_table.get_current_peer_info(RoutingDomain::PublicInternet);
            if !pi.node_info().has_any_dial_info()
                && !self.config().network.privacy.require_inbound_relay
            {
                veilid_log!(self warn
                    "This node has no valid public dial info.\nConfigure this node with a static public IP address and correct firewall rules."
                );
            }
        }

        Ok(StartupDisposition::Success)
    }

    #[cfg_attr(feature = "instrument", instrument(level = "debug", err, skip_all, fields(__VEILID_LOG_KEY = self.log_key())))]
    pub(super) fn register_all_dial_info(
        &self,
        editor_public_internet: &mut RoutingDomainEditorPublicInternet<'_>,
        editor_local_network: &mut RoutingDomainEditorLocalNetwork<'_>,
    ) -> EyreResult<()> {
        let Some(protocol_config) = ({
            let inner = self.inner.lock();
            inner
                .network_state
                .as_ref()
                .map(|ns| ns.protocol_config.clone())
        }) else {
            bail!("can't register dial info without network state");
        };

        if protocol_config.inbound.contains(ProtocolType::UDP) {
            self.register_udp_dial_info(editor_public_internet, editor_local_network)?;
        }
        if protocol_config.inbound.contains(ProtocolType::WS) {
            self.register_ws_dial_info(editor_public_internet, editor_local_network)?;
        }
        #[cfg(feature = "enable-protocol-wss")]
        if protocol_config.inbound.contains(ProtocolType::WSS) {
            self.register_wss_dial_info(editor_public_internet, editor_local_network)?;
        }
        if protocol_config.inbound.contains(ProtocolType::TCP) {
            self.register_tcp_dial_info(editor_public_internet, editor_local_network)?;
        }

        Ok(())
    }

    #[cfg_attr(feature = "instrument", instrument(level = "debug", err, skip_all, fields(__VEILID_LOG_KEY = self.log_key())))]
    pub async fn startup(&self) -> EyreResult<StartupDisposition> {
        let guard = self.startup_lock.startup()?;

        match self.startup_internal().await {
            Ok(StartupDisposition::Success) => {
                veilid_log!(self debug "Network started");
                guard.success();
                Ok(StartupDisposition::Success)
            }
            Ok(StartupDisposition::BindRetry) => {
                debug!("network bind retry");
                self.shutdown_internal().await;
                Ok(StartupDisposition::BindRetry)
            }
            Err(e) => {
                debug!("network failed to start");
                self.shutdown_internal().await;
                Err(e)
            }
        }
    }

    pub fn needs_restart(&self) -> bool {
        self.inner.lock().network_needs_restart
    }

    pub fn is_started(&self) -> bool {
        self.startup_lock.is_started()
    }

    #[cfg_attr(feature = "instrument", instrument(level = "debug", skip_all, fields(__VEILID_LOG_KEY = self.log_key())))]
    pub fn restart_network(&self) {
        self.inner.lock().network_needs_restart = true;
    }

    #[cfg_attr(feature = "instrument", instrument(level = "debug", skip_all, fields(__VEILID_LOG_KEY = self.log_key())))]
    async fn shutdown_internal(&self) {
        let routing_table = self.routing_table();

        let mut unord = FuturesUnordered::new();
        {
            let mut inner = self.inner.lock();
            // take the join handles out
            for h in inner.join_handles.drain(..) {
                veilid_log!(self trace "joining: {:?}", h);
                unord.push(h);
            }
            // Drop the stop
            drop(inner.stop_source.take());
        }
        veilid_log!(self debug "stopping {} low level network tasks", unord.len());
        // Wait for everything to stop
        while unord.next().await.is_some() {}

        veilid_log!(self debug "clearing dial info");

        routing_table
            .edit_public_internet_routing_domain()
            .reset()
            .await;

        routing_table
            .edit_local_network_routing_domain()
            .reset()
            .await;

        // Reset state including network class
        *self.inner.lock() = Self::new_inner();
    }

    #[cfg_attr(feature = "instrument", instrument(level = "debug", skip_all, fields(__VEILID_LOG_KEY = self.log_key())))]
    pub async fn shutdown(&self) {
        veilid_log!(self debug "starting low level network shutdown");
        let Ok(guard) = self.startup_lock.shutdown().await else {
            veilid_log!(self error "low level network is already shut down");
            return;
        };

        self.shutdown_internal().await;

        guard.success();
        veilid_log!(self debug "finished low level network shutdown");
    }

    //////////////////////////////////////////

    pub fn needs_update_dial_info(&self) -> bool {
        let Ok(_guard) = self.startup_lock.enter() else {
            veilid_log!(self debug "ignoring 'needs_update_dial_info' due to not started up");
            return false;
        };

        self.inner.lock().needs_update_dial_info
    }

    pub fn resolved_detect_address_changes(&self) -> bool {
        let Ok(_guard) = self.startup_lock.enter() else {
            veilid_log!(self debug "ignoring 'resolved_detect_address_changes' due to not started up");
            return false;
        };

        self.inner.lock().resolved_detect_address_changes
    }

    pub fn trigger_update_dial_info(&self, routing_domain: RoutingDomain) {
        let Ok(_guard) = self.startup_lock.enter() else {
            veilid_log!(self debug "ignoring 'trigger_update_dial_info' due to not started up");
            return;
        };

        if !matches!(routing_domain, RoutingDomain::PublicInternet) {
            return;
        }
        self.inner.lock().needs_update_dial_info = true;
    }
}