socket_proxy/
dns_watcher.rs

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
// Copyright 2024 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

//! Implements the fuchsia.netpol.socketproxy.DnsServerWatcher service.

use anyhow::{Context, Error};
use fidl::endpoints::{ControlHandle as _, RequestStream as _, Responder as _};
use fidl_fuchsia_netpol_socketproxy::{self as fnp_socketproxy, DnsServerList};
use fuchsia_inspect_derive::{IValue, Inspect, Unit};
use futures::channel::mpsc;
use futures::lock::Mutex;
use futures::{StreamExt, TryStreamExt};
use std::sync::Arc;
use tracing::{info, warn};

#[derive(Unit, Debug, Default)]
struct DnsServerWatcherState {
    #[inspect(skip)]
    server_list: Vec<DnsServerList>,
    #[inspect(skip)]
    last_sent: Option<Vec<DnsServerList>>,
    #[inspect(skip)]
    queued_responder: Option<fnp_socketproxy::DnsServerWatcherWatchServersResponder>,

    updates_seen: u32,
    updates_sent: u32,
}

/// A wrapper around the fuchsia.netpol.socketproxy.DnsServerWatcher service
/// that tracks when a DnsServerList update needs to be sent.
#[derive(Inspect, Debug, Clone)]
pub(crate) struct DnsServerWatcher {
    #[inspect(forward)]
    state: Arc<Mutex<IValue<DnsServerWatcherState>>>,
    dns_rx: Arc<Mutex<mpsc::Receiver<Vec<fnp_socketproxy::DnsServerList>>>>,
}

impl DnsServerWatcher {
    /// Create a new DnsServerWatcher.
    pub(crate) fn new(
        dns_rx: Arc<Mutex<mpsc::Receiver<Vec<fnp_socketproxy::DnsServerList>>>>,
    ) -> Self {
        Self { dns_rx, state: Default::default() }
    }

    /// Runs the fuchsia.netpol.socketproxy.DnsServerWatcher service.
    pub(crate) async fn run<'a>(
        &self,
        stream: fnp_socketproxy::DnsServerWatcherRequestStream,
    ) -> Result<(), Error> {
        let mut state = match self.state.try_lock() {
            Some(o) => o,
            None => {
                warn!("Only one connection to DnsServerWatcher is allowed at a time");
                stream.control_handle().shutdown_with_epitaph(fidl::Status::ACCESS_DENIED);
                return Ok(());
            }
        };
        let mut dns_rx = self.dns_rx.lock().await;
        info!("Starting fuchsia.netpol.socketproxy.DnsServerWatcher server");
        let mut stream = stream.map(|result| result.context("failed request")).fuse();

        loop {
            futures::select! {
                request = stream.try_next() => match request? {
                    Some(fnp_socketproxy::DnsServerWatcherRequest::WatchServers { responder }) => {
                        let mut state = state.as_mut();
                        if state.queued_responder.is_some() {
                            warn!("Only one call to watch server may be active at once");
                            responder
                                .control_handle()
                                .shutdown_with_epitaph(fidl::Status::ACCESS_DENIED);
                        } else {
                            state.queued_responder = Some(responder);
                            state.maybe_respond()?;
                        }
                    },
                    None => {}
                },
                dns_update = dns_rx.select_next_some() => {
                    let mut state = state.as_mut();
                    state.updates_seen += 1;
                    state.server_list = dns_update;
                    state.maybe_respond()?;
                }
            }
        }
    }
}

impl DnsServerWatcherState {
    fn maybe_respond(&mut self) -> Result<(), Error> {
        if self.last_sent.as_ref() != Some(&self.server_list) {
            if let Some(responder) = self.queued_responder.take() {
                info!("Sending DNS update to client: {}", self.server_list.len());
                responder.send(&self.server_list)?;
                self.updates_sent += 1;
                self.last_sent = Some(self.server_list.clone());
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use assert_matches::assert_matches;
    use diagnostics_assertions::assert_data_tree;
    use fuchsia_component::server::ServiceFs;
    use fuchsia_component_test::{
        Capability, ChildOptions, LocalComponentHandles, RealmBuilder, RealmInstance, Ref, Route,
    };
    use fuchsia_inspect_derive::WithInspect;
    use futures::channel::mpsc::{Receiver, Sender};
    use futures::SinkExt as _;
    use pretty_assertions::assert_eq;

    enum IncomingService {
        DnsServerWatcher(fnp_socketproxy::DnsServerWatcherRequestStream),
    }

    async fn run_registry(
        handles: LocalComponentHandles,
        dns_rx: Arc<Mutex<Receiver<Vec<fnp_socketproxy::DnsServerList>>>>,
    ) -> Result<(), Error> {
        let mut fs = ServiceFs::new();
        let _ = fs.dir("svc").add_fidl_service(IncomingService::DnsServerWatcher);
        let _ = fs.serve_connection(handles.outgoing_dir)?;

        let watcher = DnsServerWatcher::new(dns_rx)
            .with_inspect(fuchsia_inspect::component::inspector().root(), "dns_watcher")?;

        fs.for_each_concurrent(0, |IncomingService::DnsServerWatcher(stream)| {
            let watcher = watcher.clone();
            async move {
                watcher
                    .run(stream)
                    .await
                    .context("Failed to serve request stream")
                    .unwrap_or_else(|e| eprintln!("Error encountered: {e:?}"))
            }
        })
        .await;

        Ok(())
    }

    async fn setup_test(
    ) -> Result<(RealmInstance, Sender<Vec<fnp_socketproxy::DnsServerList>>), Error> {
        let builder = RealmBuilder::new().await?;
        let (dns_tx, dns_rx) = mpsc::channel(1);
        let dns_rx = Arc::new(Mutex::new(dns_rx));
        let registry = builder
            .add_local_child(
                "dns_watcher",
                {
                    let dns_rx = dns_rx.clone();
                    move |handles: LocalComponentHandles| {
                        Box::pin(run_registry(handles, dns_rx.clone()))
                    }
                },
                ChildOptions::new(),
            )
            .await?;

        builder
            .add_route(
                Route::new()
                    .capability(Capability::protocol::<fnp_socketproxy::DnsServerWatcherMarker>())
                    .from(&registry)
                    .to(Ref::parent()),
            )
            .await?;

        let realm = builder.build().await?;

        Ok((realm, dns_tx))
    }

    #[fuchsia::test]
    async fn test_normal_operation() -> Result<(), Error> {
        let (realm, mut dns_tx) = setup_test().await?;

        let dns_server_watcher = realm
            .root
            .connect_to_protocol_at_exposed_dir::<fnp_socketproxy::DnsServerWatcherMarker>()
            .context("While connecting to DnsServerWatcher")?;

        // Initial watch should return immediately
        assert_eq!(dns_server_watcher.watch_servers().await?, vec![]);

        // Send a new DNS update
        let (send_result, watch_result) = futures::future::join(
            dns_tx.send(vec![DnsServerList { source_network_id: Some(0), ..Default::default() }]),
            dns_server_watcher.watch_servers(),
        )
        .await;

        assert_matches!(send_result, Ok(()));
        assert_eq!(
            watch_result?,
            vec![DnsServerList { source_network_id: Some(0), ..Default::default() }]
        );

        assert_data_tree!(fuchsia_inspect::component::inspector(), root: {
            dns_watcher: {
                updates_seen: 1u64,
                updates_sent: 2u64,
            },
        });

        Ok(())
    }

    #[fuchsia::test]
    async fn test_duplicate_list() -> Result<(), Error> {
        let (realm, mut dns_tx) = setup_test().await?;
        let dns_server_watcher = realm
            .root
            .connect_to_protocol_at_exposed_dir::<fnp_socketproxy::DnsServerWatcherMarker>()
            .context("While connecting to DnsServerWatcher")?;

        // Initial watch should return immediately
        assert_eq!(dns_server_watcher.watch_servers().await?, vec![]);

        let server_list = vec![DnsServerList { source_network_id: Some(0), ..Default::default() }];

        let mut dns_tx2 = dns_tx.clone();
        let mut dns_tx3 = dns_tx.clone();
        let (watch_result, s1, s2, s3) = futures::join!(
            dns_server_watcher.watch_servers(),
            dns_tx.send(server_list.clone()),
            dns_tx2.send(server_list.clone()),
            dns_tx3.send(server_list.clone()),
        );

        assert_matches!(s1, Ok(()));
        assert_matches!(s2, Ok(()));
        assert_matches!(s3, Ok(()));
        assert_eq!(watch_result?, server_list);

        // Send a new (distinct) DNS update
        let (send_result, watch_result) = futures::future::join(
            dns_tx.send(vec![DnsServerList { source_network_id: Some(1), ..Default::default() }]),
            dns_server_watcher.watch_servers(),
        )
        .await;
        assert_matches!(send_result, Ok(()));

        // We expect that this watch should get the new server list, not one of
        // the old duplicate ones.
        assert_eq!(
            watch_result?,
            vec![DnsServerList { source_network_id: Some(1), ..Default::default() }]
        );

        assert_data_tree!(fuchsia_inspect::component::inspector(), root: {
            dns_watcher: {
                updates_seen: 4u64,
                updates_sent: 3u64,
            },
        });

        Ok(())
    }

    #[fuchsia::test]
    async fn test_duplicate_watch() -> Result<(), Error> {
        let (realm, _dns_tx) = setup_test().await?;

        let dns_server_watcher = realm
            .root
            .connect_to_protocol_at_exposed_dir::<fnp_socketproxy::DnsServerWatcherMarker>()
            .context("While connecting to DnsServerWatcher")?;

        // Initial watch should return immediately
        assert_eq!(dns_server_watcher.watch_servers().await?, vec![]);

        let watch1 = dns_server_watcher.watch_servers();
        let watch2 = dns_server_watcher.watch_servers();

        // Two simultaneous calls to watch_servers is invalid and will cause the
        // watcher channel to be closed.
        assert_matches!(
            futures::future::join(watch1, watch2).await,
            (
                Err(fidl::Error::ClientChannelClosed { status: fidl::Status::ACCESS_DENIED, .. }),
                Err(fidl::Error::ClientChannelClosed { status: fidl::Status::ACCESS_DENIED, .. })
            )
        );

        assert_data_tree!(fuchsia_inspect::component::inspector(), root: {
            dns_watcher: {
                updates_seen: 0u64,
                updates_sent: 1u64,
            },
        });

        Ok(())
    }
}