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
#[cfg(feature = "sources-utils-net-tcp")]
mod tcp;
#[cfg(feature = "sources-utils-net-udp")]
mod udp;

use std::{fmt, net::SocketAddr};

use snafu::Snafu;
use vector_lib::configurable::configurable_component;

use crate::config::{Protocol, Resource};

#[cfg(feature = "sources-utils-net-tcp")]
pub use self::tcp::{
    request_limiter::RequestLimiter, try_bind_tcp_listener, TcpNullAcker, TcpSource, TcpSourceAck,
    TcpSourceAcker, MAX_IN_FLIGHT_EVENTS_TARGET,
};
#[cfg(feature = "sources-utils-net-udp")]
pub use self::udp::try_bind_udp_socket;

#[derive(Clone, Debug, Eq, PartialEq, Snafu)]
pub enum SocketListenAddrParseError {
    #[snafu(display("Unable to parse as socket address"))]
    SocketAddrParse,
    #[snafu(display("# after \"systemd\" must be a valid integer"))]
    UsizeParse,
    #[snafu(display("Systemd indices start from 1, found 0"))]
    OneBased,
    // last case evaluated must explain all valid formats accepted
    #[snafu(display("Must be a valid IPv4/IPv6 address with port, or start with \"systemd\""))]
    UnableToParse,
}

/// The socket address to listen for connections on, or `systemd{#N}` to use the Nth socket passed by
/// systemd socket activation.
///
/// If a socket address is used, it _must_ include a port.
//
// `SocketListenAddr` is valid for any socket based source, such as `fluent` and `logstash`.
//  Socket activation is just a way for the program to get a socket for listening on.
//  Systemd can open the port, if it is a privileged number. That way the program does not
//  need to worry about dropping ports.
//  This is particularly common in non-containerized environments.
#[configurable_component]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[serde(untagged)]
#[serde(try_from = "String", into = "String")]
#[configurable(metadata(docs::examples = "0.0.0.0:9000"))]
#[configurable(metadata(docs::examples = "systemd"))]
#[configurable(metadata(docs::examples = "systemd#3"))]
pub enum SocketListenAddr {
    /// An IPv4/IPv6 address and port.
    SocketAddr(SocketAddr),

    /// A file descriptor identifier that is given from, and managed by, the socket activation feature of `systemd`.
    SystemdFd(usize),
}

impl SocketListenAddr {
    const fn as_resource(self, protocol: Protocol) -> Resource {
        match self {
            Self::SocketAddr(addr) => match protocol {
                Protocol::Tcp => Resource::tcp(addr),
                Protocol::Udp => Resource::udp(addr),
            },
            Self::SystemdFd(fd_offset) => Resource::SystemFdOffset(fd_offset),
        }
    }

    /// Gets this listen address as a `Resource`, specifically for TCP.
    #[cfg(feature = "sources-utils-net-tcp")]
    pub const fn as_tcp_resource(self) -> Resource {
        self.as_resource(Protocol::Tcp)
    }

    /// Gets this listen address as a `Resource`, specifically for UDP.
    #[cfg(feature = "sources-utils-net-udp")]
    pub const fn as_udp_resource(self) -> Resource {
        self.as_resource(Protocol::Udp)
    }
}

impl fmt::Display for SocketListenAddr {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::SocketAddr(ref addr) => addr.fmt(f),
            Self::SystemdFd(offset) => write!(f, "systemd socket #{}", offset),
        }
    }
}

impl From<SocketAddr> for SocketListenAddr {
    fn from(addr: SocketAddr) -> Self {
        Self::SocketAddr(addr)
    }
}

impl From<usize> for SocketListenAddr {
    fn from(fd: usize) -> Self {
        Self::SystemdFd(fd)
    }
}

impl TryFrom<String> for SocketListenAddr {
    type Error = SocketListenAddrParseError;

    fn try_from(input: String) -> Result<Self, Self::Error> {
        // first attempt to parse the string into a SocketAddr directly
        match input.parse::<SocketAddr>() {
            Ok(socket_addr) => Ok(socket_addr.into()),

            // then attempt to parse a systemd file descriptor
            Err(_) => {
                let fd: usize = match input.as_str() {
                    "systemd" => Ok(0),
                    s if s.starts_with("systemd#") => s[8..]
                        .parse::<usize>()
                        .map_err(|_| Self::Error::UsizeParse)?
                        .checked_sub(1)
                        .ok_or(Self::Error::OneBased),

                    // otherwise fail
                    _ => Err(Self::Error::UnableToParse),
                }?;

                Ok(fd.into())
            }
        }
    }
}

impl From<SocketListenAddr> for String {
    fn from(addr: SocketListenAddr) -> String {
        match addr {
            SocketListenAddr::SocketAddr(addr) => addr.to_string(),
            SocketListenAddr::SystemdFd(fd) => {
                if fd == 0 {
                    "systemd".to_owned()
                } else {
                    format!("systemd#{}", fd)
                }
            }
        }
    }
}

#[cfg(test)]
mod test {
    use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};

    use serde::Deserialize;

    use super::SocketListenAddr;

    #[derive(Debug, Deserialize)]
    struct Config {
        addr: SocketListenAddr,
    }

    #[test]
    fn parse_socket_listen_addr_success() {
        let test: Config = toml::from_str(r#"addr="127.1.2.3:1234""#).unwrap();
        assert_eq!(
            test.addr,
            SocketListenAddr::SocketAddr(SocketAddr::V4(SocketAddrV4::new(
                Ipv4Addr::new(127, 1, 2, 3),
                1234,
            )))
        );
        let test: Config = toml::from_str(r#"addr="systemd""#).unwrap();
        assert_eq!(test.addr, SocketListenAddr::SystemdFd(0));
        let test: Config = toml::from_str(r#"addr="systemd#3""#).unwrap();
        assert_eq!(test.addr, SocketListenAddr::SystemdFd(2));
    }

    #[test]
    fn parse_socket_listen_addr_fail() {
        // no port specified
        let test: Result<Config, toml::de::Error> = toml::from_str(r#"addr="127.1.2.3""#);
        assert!(test.is_err());

        // systemd fd indexing should be one based not zero.
        // the user should leave off the {#N} to get the fd 0.
        let test: Result<Config, toml::de::Error> = toml::from_str(r#"addr="systemd#0""#);
        assert!(test.is_err());

        // typo
        let test: Result<Config, toml::de::Error> = toml::from_str(r#"addr="system""#);
        assert!(test.is_err());
    }
}