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
mod tcp;
mod udp;

#[cfg(unix)]
mod unix;

use std::{
    io,
    net::SocketAddr,
    task::{ready, Context, Poll},
    time::Duration,
};

#[cfg(unix)]
use std::path::PathBuf;

use crate::{
    internal_events::{
        SocketOutgoingConnectionError, TcpSocketConnectionEstablished, UdpSendIncompleteError,
    },
    sinks::{util::retries::ExponentialBackoff, Healthcheck},
};

#[cfg(unix)]
use crate::internal_events::{UnixSendIncompleteError, UnixSocketConnectionEstablished};

pub use self::tcp::TcpConnectorConfig;
pub use self::udp::UdpConnectorConfig;

#[cfg(unix)]
pub use self::unix::{UnixConnectorConfig, UnixMode};

use self::tcp::TcpConnector;
use self::udp::UdpConnector;
#[cfg(unix)]
use self::unix::{UnixConnector, UnixEither};

use futures_util::{future::BoxFuture, FutureExt};
use snafu::{ResultExt, Snafu};
use tokio::{
    io::AsyncWriteExt,
    net::{TcpStream, UdpSocket},
    sync::oneshot,
    time::sleep,
};
use tower::Service;
use vector_lib::configurable::configurable_component;
use vector_lib::tls::{MaybeTlsStream, TlsError};

/// Hostname and port tuple.
///
/// Both IP addresses and hostnames/fully qualified domain names (FQDNs) are accepted formats.
///
/// The address _must_ include a port.
#[configurable_component]
#[derive(Clone, Debug)]
#[serde(try_from = "String", into = "String")]
#[configurable(title = "The address to connect to.")]
#[configurable(metadata(docs::examples = "92.12.333.224:5000"))]
#[configurable(metadata(docs::examples = "somehost:5000"))]
struct HostAndPort {
    /// Hostname.
    host: String,

    /// Port.
    port: u16,
}

impl TryFrom<String> for HostAndPort {
    type Error = String;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        let uri = value.parse::<http::Uri>().map_err(|e| e.to_string())?;
        let host = uri
            .host()
            .ok_or_else(|| "missing host".to_string())?
            .to_string();
        let port = uri.port_u16().ok_or_else(|| "missing port".to_string())?;

        Ok(Self { host, port })
    }
}

impl From<HostAndPort> for String {
    fn from(value: HostAndPort) -> Self {
        format!("{}:{}", value.host, value.port)
    }
}

#[derive(Debug, Snafu)]
#[snafu(module, context(suffix(false)), visibility(pub))]
pub enum NetError {
    #[snafu(display("Address is invalid: {}", reason))]
    InvalidAddress { reason: String },

    #[snafu(display("Failed to resolve address: {}", source))]
    FailedToResolve { source: crate::dns::DnsError },

    #[snafu(display("No addresses returned."))]
    NoAddresses,

    #[snafu(display("Failed to configure socket: {}.", source))]
    FailedToConfigure { source: std::io::Error },

    #[snafu(display("Failed to configure TLS: {}.", source))]
    FailedToConfigureTLS { source: TlsError },

    #[snafu(display("Failed to bind socket: {}.", source))]
    FailedToBind { source: std::io::Error },

    #[snafu(display("Failed to send message: {}", source))]
    FailedToSend { source: std::io::Error },

    #[snafu(display("Failed to connect to endpoint: {}", source))]
    FailedToConnect { source: std::io::Error },

    #[snafu(display("Failed to connect to TLS endpoint: {}", source))]
    FailedToConnectTLS { source: TlsError },

    #[snafu(display("Failed to get socket back after send as channel closed unexpectedly."))]
    ServiceSocketChannelClosed,
}

enum NetworkServiceState {
    /// The service is currently disconnected.
    Disconnected,

    /// The service is currently attempting to connect to the endpoint.
    Connecting(BoxFuture<'static, NetworkConnection>),

    /// The service is connected and idle.
    Connected(NetworkConnection),

    /// The service has an in-flight send to the socket.
    ///
    /// If the socket experiences an unrecoverable error during the send, `None` will be returned
    /// over the channel to signal the need to establish a new connection rather than reusing the
    /// existing connection.
    Sending(oneshot::Receiver<Option<NetworkConnection>>),
}

enum NetworkConnection {
    Tcp(MaybeTlsStream<TcpStream>),
    Udp(UdpSocket),
    #[cfg(unix)]
    Unix(UnixEither),
}

impl NetworkConnection {
    fn on_partial_send(&self, data_size: usize, sent: usize) {
        match self {
            // Can't "successfully" partially send with TCP: it either all eventually sends or the
            // socket has an I/O error that kills the connection entirely.
            Self::Tcp(_) => {}
            Self::Udp(_) => {
                emit!(UdpSendIncompleteError { data_size, sent });
            }
            #[cfg(unix)]
            Self::Unix(_) => {
                emit!(UnixSendIncompleteError { data_size, sent });
            }
        }
    }

    async fn send(&mut self, buf: &[u8]) -> io::Result<usize> {
        match self {
            Self::Tcp(stream) => stream.write_all(buf).await.map(|()| buf.len()),
            Self::Udp(socket) => socket.send(buf).await,
            #[cfg(unix)]
            Self::Unix(socket) => socket.send(buf).await,
        }
    }
}

enum ConnectionMetadata {
    Tcp {
        peer_addr: SocketAddr,
    },
    #[cfg(unix)]
    Unix {
        path: PathBuf,
    },
}

#[derive(Clone)]
enum ConnectorType {
    Tcp(TcpConnector),
    Udp(UdpConnector),
    #[cfg(unix)]
    Unix(UnixConnector),
}

/// A connector for generically connecting to a remote network endpoint.
///
/// The connection can be based on TCP, UDP, or Unix Domain Sockets.
#[derive(Clone)]
pub struct NetworkConnector {
    inner: ConnectorType,
}

impl NetworkConnector {
    fn on_connected(&self, metadata: ConnectionMetadata) {
        match metadata {
            ConnectionMetadata::Tcp { peer_addr } => {
                emit!(TcpSocketConnectionEstablished {
                    peer_addr: Some(peer_addr)
                });
            }
            #[cfg(unix)]
            ConnectionMetadata::Unix { path } => {
                emit!(UnixSocketConnectionEstablished { path: &path });
            }
        }
    }

    fn on_connection_error<E: std::error::Error>(&self, error: E) {
        emit!(SocketOutgoingConnectionError { error });
    }

    async fn connect(&self) -> Result<(NetworkConnection, Option<ConnectionMetadata>), NetError> {
        match &self.inner {
            ConnectorType::Tcp(connector) => {
                let (peer_addr, stream) = connector.connect().await?;

                Ok((
                    NetworkConnection::Tcp(stream),
                    Some(ConnectionMetadata::Tcp { peer_addr }),
                ))
            }
            ConnectorType::Udp(connector) => {
                let socket = connector.connect().await?;

                Ok((NetworkConnection::Udp(socket), None))
            }
            #[cfg(unix)]
            ConnectorType::Unix(connector) => {
                let (path, socket) = connector.connect().await?;

                Ok((
                    NetworkConnection::Unix(socket),
                    Some(ConnectionMetadata::Unix { path }),
                ))
            }
        }
    }

    async fn connect_backoff(&self) -> NetworkConnection {
        // TODO: Make this configurable.
        let mut backoff = ExponentialBackoff::from_millis(2)
            .factor(250)
            .max_delay(Duration::from_secs(60));

        loop {
            match self.connect().await {
                Ok((connection, maybe_metadata)) => {
                    if let Some(metadata) = maybe_metadata {
                        self.on_connected(metadata);
                    }

                    return connection;
                }
                Err(error) => {
                    self.on_connection_error(error);
                    sleep(backoff.next().unwrap()).await;
                }
            }
        }
    }

    /// Gets a `Healthcheck` based on the configured destination of this connector.
    pub fn healthcheck(&self) -> Healthcheck {
        let connector = self.clone();
        Box::pin(async move { connector.connect().await.map(|_| ()).map_err(Into::into) })
    }

    /// Gets a `Service` suitable for sending data to the configured destination of this connector.
    pub fn service(&self) -> NetworkService {
        NetworkService::new(self.clone())
    }
}

/// A `Service` implementation for generically sending bytes to a remote peer over a network connection.
///
/// The connection can be based on TCP, UDP, or Unix Domain Sockets.
pub struct NetworkService {
    connector: NetworkConnector,
    state: NetworkServiceState,
}

impl NetworkService {
    const fn new(connector: NetworkConnector) -> Self {
        Self {
            connector,
            state: NetworkServiceState::Disconnected,
        }
    }
}

impl Service<Vec<u8>> for NetworkService {
    type Response = usize;
    type Error = NetError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        loop {
            self.state = match &mut self.state {
                NetworkServiceState::Disconnected => {
                    let connector = self.connector.clone();
                    NetworkServiceState::Connecting(Box::pin(async move {
                        connector.connect_backoff().await
                    }))
                }
                NetworkServiceState::Connecting(fut) => {
                    let socket = ready!(fut.poll_unpin(cx));
                    NetworkServiceState::Connected(socket)
                }
                NetworkServiceState::Connected(_) => break,
                NetworkServiceState::Sending(fut) => {
                    match ready!(fut.poll_unpin(cx)) {
                        // When a send concludes, and there's an error, the request future sends
                        // back `None`. Otherwise, it'll send back `Some(...)` with the socket.
                        Ok(maybe_socket) => match maybe_socket {
                            Some(socket) => NetworkServiceState::Connected(socket),
                            None => NetworkServiceState::Disconnected,
                        },
                        Err(_) => return Poll::Ready(Err(NetError::ServiceSocketChannelClosed)),
                    }
                }
            };
        }
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, buf: Vec<u8>) -> Self::Future {
        let (tx, rx) = oneshot::channel();

        let mut socket = match std::mem::replace(&mut self.state, NetworkServiceState::Sending(rx))
        {
            NetworkServiceState::Connected(socket) => socket,
            _ => panic!("poll_ready must be called first"),
        };

        Box::pin(async move {
            match socket.send(&buf).await.context(net_error::FailedToSend) {
                Ok(sent) => {
                    // Emit an error if we weren't able to send the entire buffer.
                    if sent != buf.len() {
                        socket.on_partial_send(buf.len(), sent);
                    }

                    // Send the socket back to the service, since theoretically it's still valid to
                    // reuse given that we may have simply overrun the OS socket buffers, etc.
                    let _ = tx.send(Some(socket));

                    Ok(sent)
                }
                Err(e) => {
                    // We need to signal back to the service that it needs to create a fresh socket
                    // since this one could be tainted.
                    let _ = tx.send(None);

                    Err(e)
                }
            }
        })
    }
}