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
/* This file is part of DarkFi (https://dark.fi)
 *
 * Copyright (C) 2020-2024 Dyne.org foundation
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use std::{
    io::ErrorKind,
    sync::{
        atomic::{AtomicUsize, Ordering::SeqCst},
        Arc,
    },
};

use log::{error, info, warn};
use smol::Executor;
use url::Url;

use super::{
    channel::{Channel, ChannelPtr},
    hosts::HostColor,
    session::SessionWeakPtr,
    transport::{Listener, PtListener},
};
use crate::{
    system::{CondVar, Publisher, PublisherPtr, StoppableTask, StoppableTaskPtr, Subscription},
    Error, Result,
};

/// Atomic pointer to Acceptor
pub type AcceptorPtr = Arc<Acceptor>;

/// Create inbound socket connections
pub struct Acceptor {
    channel_publisher: PublisherPtr<Result<ChannelPtr>>,
    task: StoppableTaskPtr,
    session: SessionWeakPtr,
    conn_count: AtomicUsize,
}

impl Acceptor {
    /// Create new Acceptor object.
    pub fn new(session: SessionWeakPtr) -> AcceptorPtr {
        Arc::new(Self {
            channel_publisher: Publisher::new(),
            task: StoppableTask::new(),
            session,
            conn_count: AtomicUsize::new(0),
        })
    }

    /// Start accepting inbound socket connections
    pub async fn start(self: Arc<Self>, endpoint: Url, ex: Arc<Executor<'_>>) -> Result<()> {
        let datastore =
            self.session.upgrade().unwrap().p2p().settings().read().await.datastore.clone();

        // Initialize listener
        let listener = Listener::new(endpoint.clone(), datastore).await?;

        // Open socket
        let ptlistener = listener.listen().await?;

        #[cfg(feature = "p2p-tor")]
        if endpoint.scheme() == "tor" {
            let onion_addr = listener.endpoint().await;
            info!("[P2P] Adding {} to external_addrs", onion_addr);
            self.session
                .upgrade()
                .unwrap()
                .p2p()
                .settings()
                .write()
                .await
                .external_addrs
                .push(onion_addr);
        }

        self.accept(ptlistener, ex);
        Ok(())
    }

    /// Stop accepting inbound socket connections
    pub async fn stop(&self) {
        // Send stop signal
        self.task.stop().await;
    }

    /// Start receiving network messages.
    pub async fn subscribe(self: Arc<Self>) -> Subscription<Result<ChannelPtr>> {
        self.channel_publisher.clone().subscribe().await
    }

    /// Run the accept loop in a new thread and error if a connection problem occurs
    fn accept(self: Arc<Self>, listener: Box<dyn PtListener>, ex: Arc<Executor<'_>>) {
        let self_ = self.clone();
        self.task.clone().start(
            self.run_accept_loop(listener, ex.clone()),
            |result| self_.handle_stop(result),
            Error::NetworkServiceStopped,
            ex,
        );
    }

    /// Run the accept loop.
    async fn run_accept_loop(
        self: Arc<Self>,
        listener: Box<dyn PtListener>,
        ex: Arc<Executor<'_>>,
    ) -> Result<()> {
        // CondVar used to notify the loop to recheck if new connections can
        // be accepted by the listener.
        let cv = Arc::new(CondVar::new());
        let hosts = self.session.upgrade().unwrap().p2p().hosts();

        loop {
            // Refuse new connections if we're up to the connection limit
            let limit =
                self.session.upgrade().unwrap().p2p().settings().read().await.inbound_connections;

            if self.clone().conn_count.load(SeqCst) >= limit {
                // This will get notified every time an inbound channel is stopped.
                // These channels are the channels spawned below on listener.next().is_ok().
                // After the notification, we reset the condvar and retry this loop to see
                // if we can accept more connections, and if not - we'll be back here.
                warn!(target: "net::acceptor::run_accept_loop()", "Reached incoming conn limit, waiting...");
                cv.wait().await;
                cv.reset();
                continue
            }

            // Now we wait for a new connection.
            match listener.next().await {
                Ok((stream, url)) => {
                    // Check if we reject this peer
                    if hosts.container.contains(HostColor::Black as usize, &url) ||
                        hosts.block_all_ports(url.host_str().unwrap().to_string())
                    {
                        warn!(target: "net::acceptor::run_accept_loop()", "Peer {} is blacklisted", url);
                        continue
                    }

                    // Create the new Channel.
                    let session = self.session.clone();
                    let channel = Channel::new(stream, None, url, session).await;

                    // Increment the connection counter
                    self.conn_count.fetch_add(1, SeqCst);

                    // This task will subscribe on the new channel and decrement
                    // the connection counter. Along with that, it will notify
                    // the CondVar that might be waiting to allow new connections.
                    let self_ = self.clone();
                    let channel_ = channel.clone();
                    let cv_ = cv.clone();
                    ex.spawn(async move {
                        let stop_sub = channel_.subscribe_stop().await?;
                        stop_sub.receive().await;
                        self_.conn_count.fetch_sub(1, SeqCst);
                        cv_.notify();
                        Ok::<(), crate::Error>(())
                    })
                    .detach();

                    // Finally, notify any publishers about the new channel.
                    self.channel_publisher.notify(Ok(channel)).await;
                }

                // As per accept(2) recommendation:
                Err(e) if e.raw_os_error().is_some() => match e.raw_os_error().unwrap() {
                    libc::EAGAIN | libc::ECONNABORTED | libc::EPROTO | libc::EINTR => continue,
                    libc::ECONNRESET => {
                        warn!(
                            target: "net::acceptor::run_accept_loop()",
                            "[P2P] Connection reset by peer in accept_loop"
                        );
                        continue
                    }
                    libc::ETIMEDOUT => {
                        warn!(
                            target: "net::acceptor::run_accept_loop()",
                            "[P2P] Connection timed out in accept_loop"
                        );
                        continue
                    }
                    x => {
                        error!(
                            target: "net::acceptor::run_accept_loop()",
                            "[P2P] Acceptor failed listening: {} ({})", e, x,
                        );
                        error!(
                            target: "net::acceptor::run_accept_loop()",
                            "[P2P] Closing listener loop"
                        );
                        return Err(e.into())
                    }
                },

                // In case a TLS handshake fails, we'll get this:
                Err(e) if e.kind() == ErrorKind::UnexpectedEof => continue,

                // Handle ErrorKind::Other
                Err(e) if e.kind() == ErrorKind::Other => {
                    if let Some(inner) = std::error::Error::source(&e) {
                        if let Some(inner) = inner.downcast_ref::<futures_rustls::rustls::Error>() {
                            error!(
                                target: "net::acceptor::run_accept_loop()",
                                "[P2P] rustls listener error: {:?}", inner,
                            );
                            continue
                        }
                    }

                    error!(
                        target: "net::acceptor::run_accept_loop()",
                        "[P2P] Unhandled ErrorKind::Other error: {:?}", e,
                    );
                    return Err(e.into())
                }

                // Errors we didn't handle above:
                Err(e) => {
                    error!(
                        target: "net::acceptor::run_accept_loop()",
                        "[P2P] Unhandled listener.next() error: {}", e,
                    );
                    /*
                    error!(
                        target: "net::acceptor::run_accept_loop()",
                        "[P2P] Closing listener loop"
                    );
                    return Err(e.into())
                    */
                    continue
                }
            }
        }
    }

    /// Handles network errors. Panics if errors pass silently, otherwise broadcasts it
    /// to all channel publishers.
    async fn handle_stop(self: Arc<Self>, result: Result<()>) {
        match result {
            Ok(()) => panic!("Acceptor task should never complete without error status"),
            Err(err) => self.channel_publisher.notify(Err(err)).await,
        }
    }
}