1#[cfg(feature = "async")]
33pub use net_lattice_async::EventStream;
34pub use net_lattice_core::{Error, Id, PlatformErrorCode, Result};
35pub use net_lattice_ip::{
36 Ipv4Address, Ipv4Network, Ipv4PrefixLength, Ipv6Address, Ipv6Network, Ipv6PrefixLength,
37};
38pub use net_lattice_model::dns::{DnsConfig, NewDnsConfig};
39pub use net_lattice_model::event::{ChangeKind, Event, EventDomain, EventFilter};
40pub use net_lattice_model::ifaddr::{InterfaceAddress, InterfaceAddressId, NewInterfaceAddress};
41pub use net_lattice_model::interface::{
42 AdminState, Interface, InterfaceId, InterfaceKind, OperationalState,
43};
44pub use net_lattice_model::mac::MacAddress;
45pub use net_lattice_model::neighbor::{NeighborEntry, NeighborId, NeighborState};
46pub use net_lattice_model::route::{Route, RouteId};
47pub use net_lattice_model::{IpAddress, Network};
48#[cfg(feature = "async")]
49pub use net_lattice_platform::TokioEventProvider;
50pub use net_lattice_platform::{
51 AddressMutator, AddressProvider, Capability, CapabilityProvider, DnsMutator, DnsProvider,
52 EventProvider, EventReceiver, InterfaceProvider, NeighborProvider, RouteProvider,
53};
54
55pub mod backend {
62 pub use crate::LatticeBackend;
63 pub use net_lattice_platform::{
64 AddressMutator, AddressProvider, CapabilityProvider, DnsMutator, DnsProvider,
65 EventProvider, EventReceiver, EventSender, InterfaceProvider, NeighborProvider,
66 RouteProvider,
67 };
68 #[cfg(feature = "async")]
69 pub use net_lattice_platform::{TokioEventProvider, TokioEventReceiver, TokioEventSender};
70}
71
72pub trait LatticeBackend:
86 RouteProvider<Route = Route>
87 + InterfaceProvider<Interface = Interface>
88 + DnsMutator<NewDnsConfig = NewDnsConfig, DnsConfig = DnsConfig>
89 + NeighborProvider<NeighborEntry = NeighborEntry>
90 + AddressProvider<InterfaceAddress = InterfaceAddress>
91 + AddressMutator<NewInterfaceAddress = NewInterfaceAddress, InterfaceAddress = InterfaceAddress>
92 + EventProvider<Event = Event, EventFilter = EventFilter>
93 + CapabilityProvider
94{
95}
96
97impl<B> LatticeBackend for B where
98 B: RouteProvider<Route = Route>
99 + InterfaceProvider<Interface = Interface>
100 + DnsMutator<NewDnsConfig = NewDnsConfig, DnsConfig = DnsConfig>
101 + NeighborProvider<NeighborEntry = NeighborEntry>
102 + AddressProvider<InterfaceAddress = InterfaceAddress>
103 + AddressMutator<
104 NewInterfaceAddress = NewInterfaceAddress,
105 InterfaceAddress = InterfaceAddress,
106 > + EventProvider<Event = Event, EventFilter = EventFilter>
107 + CapabilityProvider
108{
109}
110
111pub struct Lattice<B: LatticeBackend> {
113 backend: B,
114}
115
116impl<B: LatticeBackend> Lattice<B> {
117 pub fn routes(&self) -> Result<Vec<Route>> {
118 self.backend.routes()
119 }
120
121 pub fn add_route(&self, route: Route) -> Result<()> {
122 self.backend.add_route(route)
123 }
124
125 pub fn remove_route(&self, route: Route) -> Result<()> {
126 self.backend.remove_route(route)
127 }
128
129 pub fn interfaces(&self) -> Result<Vec<Interface>> {
130 self.backend.interfaces()
131 }
132
133 pub fn dns_config(&self) -> Result<DnsConfig> {
134 self.backend.dns_config()
135 }
136
137 pub fn set_dns_config(&self, config: NewDnsConfig) -> Result<DnsConfig> {
140 self.backend.set_dns_config(config)
141 }
142
143 pub fn neighbors(&self) -> Result<Vec<NeighborEntry>> {
144 self.backend.neighbors()
145 }
146
147 pub fn addresses(&self) -> Result<Vec<InterfaceAddress>> {
148 self.backend.addresses()
149 }
150
151 pub fn add_address(&self, address: NewInterfaceAddress) -> Result<InterfaceAddress> {
154 self.backend.add_address(address)
155 }
156
157 pub fn remove_address(&self, address: InterfaceAddress) -> Result<()> {
159 self.backend.remove_address(address)
160 }
161
162 pub fn capabilities(&self) -> Capability {
165 self.backend.capabilities()
166 }
167
168 pub fn supports(&self, capability: Capability) -> bool {
171 self.backend.capabilities().contains(capability)
172 }
173
174 pub fn watch(&self) -> Result<EventReceiver<Event>> {
191 self.ensure_monitoring()?;
192 self.backend.watch()
193 }
194
195 #[cfg(feature = "async")]
214 pub fn watch_async(&self, filter: EventFilter) -> Result<EventStream<Event>>
215 where
216 B: TokioEventProvider<Event = Event, EventFilter = EventFilter>,
217 {
218 self.ensure_monitoring()?;
219 Ok(net_lattice_async::from_tokio_receiver(
220 self.backend.watch_tokio(filter)?,
221 ))
222 }
223
224 pub fn watch_filtered(&self, filter: EventFilter) -> Result<EventReceiver<Event>> {
226 self.ensure_monitoring()?;
227 self.backend.watch_filtered(filter)
228 }
229
230 fn ensure_monitoring(&self) -> Result<()> {
231 if self.supports(Capability::MONITORING) {
232 Ok(())
233 } else {
234 Err(Error::Unsupported)
235 }
236 }
237}
238
239#[cfg(target_os = "linux")]
240impl Lattice<net_lattice_backend_linux::LinuxBackend> {
241 pub fn connect() -> Result<Self> {
243 Ok(Self {
244 backend: net_lattice_backend_linux::LinuxBackend::new()?,
245 })
246 }
247}
248
249#[cfg(target_os = "windows")]
250impl Lattice<net_lattice_backend_windows::WindowsBackend> {
251 pub fn connect() -> Result<Self> {
253 Ok(Self {
254 backend: net_lattice_backend_windows::WindowsBackend::new()?,
255 })
256 }
257}
258
259#[cfg(target_os = "macos")]
260impl Lattice<net_lattice_backend_darwin::DarwinBackend> {
261 pub fn connect() -> Result<Self> {
263 Ok(Self {
264 backend: net_lattice_backend_darwin::DarwinBackend::new()?,
265 })
266 }
267}