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
use std::path::{Path, PathBuf};

use snafu::ResultExt;
use tokio::net::{UnixDatagram, UnixStream};

use vector_lib::configurable::configurable_component;

use crate::{net, sinks::util::unix::UnixEither};

use super::{net_error::*, ConnectorType, NetError, NetworkConnector};

/// Unix socket modes.
#[configurable_component]
#[derive(Clone, Copy, Debug)]
pub enum UnixMode {
    /// Datagram-oriented (`SOCK_DGRAM`).
    Datagram,

    /// Stream-oriented (`SOCK_STREAM`).
    Stream,
}

/// Unix Domain Socket configuration.
#[configurable_component]
#[derive(Clone, Debug)]
pub struct UnixConnectorConfig {
    /// The Unix socket path.
    ///
    /// This should be an absolute path.
    #[configurable(metadata(docs::examples = "/path/to/socket"))]
    path: PathBuf,

    /// The Unix socket mode to use.
    #[serde(default = "default_unix_mode")]
    unix_mode: UnixMode,

    /// The size of the socket's send buffer.
    ///
    /// If set, the value of the setting is passed via the `SO_SNDBUF` option.
    #[configurable(metadata(docs::type_unit = "bytes"))]
    #[configurable(metadata(docs::examples = 65536))]
    send_buffer_size: Option<usize>,
}

const fn default_unix_mode() -> UnixMode {
    UnixMode::Stream
}

impl UnixConnectorConfig {
    pub fn from_path<P: AsRef<Path>>(path: P) -> Self {
        Self {
            path: path.as_ref().to_path_buf(),
            unix_mode: UnixMode::Stream,
            send_buffer_size: None,
        }
    }

    /// Creates a [`NetworkConnector`] from this Unix Domain Socket connector configuration.
    pub fn as_connector(&self) -> NetworkConnector {
        NetworkConnector {
            inner: ConnectorType::Unix(UnixConnector {
                path: self.path.clone(),
                mode: self.unix_mode,
                send_buffer_size: self.send_buffer_size,
            }),
        }
    }
}

#[derive(Clone)]
pub(super) struct UnixConnector {
    path: PathBuf,
    mode: UnixMode,
    send_buffer_size: Option<usize>,
}

impl UnixConnector {
    pub(super) async fn connect(&self) -> Result<(PathBuf, UnixEither), NetError> {
        let either_socket = match self.mode {
            UnixMode::Datagram => {
                UnixDatagram::unbound()
                    .context(FailedToBind)
                    .and_then(|datagram| {
                        datagram
                            .connect(&self.path)
                            .context(FailedToConnect)
                            .map(|_| UnixEither::Datagram(datagram))
                    })?
            }
            UnixMode::Stream => UnixStream::connect(&self.path)
                .await
                .context(FailedToConnect)
                .map(UnixEither::Stream)?,
        };

        if let Some(send_buffer_size) = self.send_buffer_size {
            if let Err(error) = net::set_send_buffer_size(&either_socket, send_buffer_size) {
                warn!(%error, "Failed configuring send buffer size on Unix socket.");
            }
        }

        Ok((self.path.clone(), either_socket))
    }
}