aboutsummaryrefslogtreecommitdiff
path: root/net/src/endpoint.rs
blob: dff703dfb8ba9f34ea5041de53f4c773bec58d5f (plain)
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
use std::{
    net::{IpAddr, SocketAddr},
    path::PathBuf,
    str::FromStr,
};

use std::os::unix::net::SocketAddr as UnixSocketAddr;

use bincode::{Decode, Encode};
use url::Url;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use crate::{Error, Result};

/// Port defined as a u16.
pub type Port = u16;

/// Endpoint defines generic network endpoints for karyon.
///
/// # Example
///
/// ```
/// use std::net::SocketAddr;
///
/// use karyon_net::Endpoint;
///
/// let endpoint: Endpoint = "tcp://127.0.0.1:3000".parse().unwrap();
///
/// let socketaddr: SocketAddr = "127.0.0.1:3000".parse().unwrap();
/// let endpoint =  Endpoint::new_udp_addr(socketaddr);
///
/// ```
///
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(into = "String"))]
pub enum Endpoint {
    Udp(Addr, Port),
    Tcp(Addr, Port),
    Tls(Addr, Port),
    Ws(Addr, Port),
    Wss(Addr, Port),
    Unix(PathBuf),
}

impl Endpoint {
    /// Creates a new TCP endpoint from a `SocketAddr`.
    pub fn new_tcp_addr(addr: SocketAddr) -> Endpoint {
        Endpoint::Tcp(Addr::Ip(addr.ip()), addr.port())
    }

    /// Creates a new UDP endpoint from a `SocketAddr`.
    pub fn new_udp_addr(addr: SocketAddr) -> Endpoint {
        Endpoint::Udp(Addr::Ip(addr.ip()), addr.port())
    }

    /// Creates a new TLS endpoint from a `SocketAddr`.
    pub fn new_tls_addr(addr: SocketAddr) -> Endpoint {
        Endpoint::Tls(Addr::Ip(addr.ip()), addr.port())
    }

    /// Creates a new WS endpoint from a `SocketAddr`.
    pub fn new_ws_addr(addr: SocketAddr) -> Endpoint {
        Endpoint::Ws(Addr::Ip(addr.ip()), addr.port())
    }

    /// Creates a new WSS endpoint from a `SocketAddr`.
    pub fn new_wss_addr(addr: SocketAddr) -> Endpoint {
        Endpoint::Wss(Addr::Ip(addr.ip()), addr.port())
    }

    /// Creates a new Unix endpoint from a `UnixSocketAddr`.
    pub fn new_unix_addr(addr: &std::path::Path) -> Endpoint {
        Endpoint::Unix(addr.to_path_buf())
    }

    #[inline]
    /// Checks if the `Endpoint` is of type `Tcp`.
    pub fn is_tcp(&self) -> bool {
        matches!(self, Endpoint::Tcp(..))
    }

    #[inline]
    /// Checks if the `Endpoint` is of type `Tls`.
    pub fn is_tls(&self) -> bool {
        matches!(self, Endpoint::Tls(..))
    }

    #[inline]
    /// Checks if the `Endpoint` is of type `Ws`.
    pub fn is_ws(&self) -> bool {
        matches!(self, Endpoint::Ws(..))
    }

    #[inline]
    /// Checks if the `Endpoint` is of type `Wss`.
    pub fn is_wss(&self) -> bool {
        matches!(self, Endpoint::Wss(..))
    }

    #[inline]
    /// Checks if the `Endpoint` is of type `Udp`.
    pub fn is_udp(&self) -> bool {
        matches!(self, Endpoint::Udp(..))
    }

    #[inline]
    /// Checks if the `Endpoint` is of type `Unix`.
    pub fn is_unix(&self) -> bool {
        matches!(self, Endpoint::Unix(..))
    }

    /// Returns the `Port` of the endpoint.
    pub fn port(&self) -> Result<&Port> {
        match self {
            Endpoint::Tcp(_, port)
            | Endpoint::Udp(_, port)
            | Endpoint::Tls(_, port)
            | Endpoint::Ws(_, port)
            | Endpoint::Wss(_, port) => Ok(port),
            _ => Err(Error::TryFromEndpoint),
        }
    }

    /// Returns the `Addr` of the endpoint.
    pub fn addr(&self) -> Result<&Addr> {
        match self {
            Endpoint::Tcp(addr, _)
            | Endpoint::Udp(addr, _)
            | Endpoint::Tls(addr, _)
            | Endpoint::Ws(addr, _)
            | Endpoint::Wss(addr, _) => Ok(addr),
            _ => Err(Error::TryFromEndpoint),
        }
    }
}

impl std::fmt::Display for Endpoint {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Endpoint::Udp(ip, port) => {
                write!(f, "udp://{}:{}", ip, port)
            }
            Endpoint::Tcp(ip, port) => {
                write!(f, "tcp://{}:{}", ip, port)
            }
            Endpoint::Tls(ip, port) => {
                write!(f, "tls://{}:{}", ip, port)
            }
            Endpoint::Ws(ip, port) => {
                write!(f, "ws://{}:{}", ip, port)
            }
            Endpoint::Wss(ip, port) => {
                write!(f, "wss://{}:{}", ip, port)
            }
            Endpoint::Unix(path) => {
                write!(f, "unix:/{}", path.to_string_lossy())
            }
        }
    }
}
impl From<Endpoint> for String {
    fn from(endpoint: Endpoint) -> String {
        endpoint.to_string()
    }
}

impl TryFrom<Endpoint> for SocketAddr {
    type Error = Error;
    fn try_from(endpoint: Endpoint) -> std::result::Result<SocketAddr, Self::Error> {
        match endpoint {
            Endpoint::Udp(ip, port)
            | Endpoint::Tcp(ip, port)
            | Endpoint::Tls(ip, port)
            | Endpoint::Ws(ip, port)
            | Endpoint::Wss(ip, port) => Ok(SocketAddr::new(ip.try_into()?, port)),
            Endpoint::Unix(_) => Err(Error::TryFromEndpoint),
        }
    }
}

impl TryFrom<Endpoint> for PathBuf {
    type Error = Error;
    fn try_from(endpoint: Endpoint) -> std::result::Result<PathBuf, Self::Error> {
        match endpoint {
            Endpoint::Unix(path) => Ok(PathBuf::from(&path)),
            _ => Err(Error::TryFromEndpoint),
        }
    }
}

impl TryFrom<Endpoint> for UnixSocketAddr {
    type Error = Error;
    fn try_from(endpoint: Endpoint) -> std::result::Result<UnixSocketAddr, Self::Error> {
        match endpoint {
            Endpoint::Unix(a) => Ok(UnixSocketAddr::from_pathname(a)?),
            _ => Err(Error::TryFromEndpoint),
        }
    }
}

impl FromStr for Endpoint {
    type Err = Error;

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let url: Url = match s.parse() {
            Ok(u) => u,
            Err(err) => return Err(Error::ParseEndpoint(err.to_string())),
        };

        if url.has_host() {
            let host = url.host_str().unwrap();

            let addr = match host.parse::<IpAddr>() {
                Ok(addr) => Addr::Ip(addr),
                Err(_) => Addr::Domain(host.to_string()),
            };

            let port = match url.port() {
                Some(p) => p,
                None => return Err(Error::ParseEndpoint(format!("port missing: {s}"))),
            };

            match url.scheme() {
                "tcp" => Ok(Endpoint::Tcp(addr, port)),
                "udp" => Ok(Endpoint::Udp(addr, port)),
                "tls" => Ok(Endpoint::Tls(addr, port)),
                "ws" => Ok(Endpoint::Ws(addr, port)),
                "wss" => Ok(Endpoint::Wss(addr, port)),
                _ => Err(Error::InvalidEndpoint(s.to_string())),
            }
        } else {
            if url.path().is_empty() {
                return Err(Error::InvalidEndpoint(s.to_string()));
            }

            match url.scheme() {
                "unix" => Ok(Endpoint::Unix(url.path().into())),
                _ => Err(Error::InvalidEndpoint(s.to_string())),
            }
        }
    }
}

/// Addr defines a type for an address, either IP or domain.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Encode, Decode)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Addr {
    Ip(IpAddr),
    Domain(String),
}

impl TryFrom<Addr> for IpAddr {
    type Error = Error;
    fn try_from(addr: Addr) -> std::result::Result<IpAddr, Self::Error> {
        match addr {
            Addr::Ip(ip) => Ok(ip),
            Addr::Domain(d) => Err(Error::InvalidAddress(d)),
        }
    }
}

impl std::fmt::Display for Addr {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            Addr::Ip(ip) => {
                write!(f, "{}", ip)
            }
            Addr::Domain(d) => {
                write!(f, "{}", d)
            }
        }
    }
}

pub trait ToEndpoint {
    fn to_endpoint(&self) -> Result<Endpoint>;
}

impl ToEndpoint for String {
    fn to_endpoint(&self) -> Result<Endpoint> {
        Endpoint::from_str(self)
    }
}

impl ToEndpoint for Endpoint {
    fn to_endpoint(&self) -> Result<Endpoint> {
        Ok(self.clone())
    }
}

impl ToEndpoint for &str {
    fn to_endpoint(&self) -> Result<Endpoint> {
        Endpoint::from_str(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::net::Ipv4Addr;
    use std::path::PathBuf;

    #[test]
    fn test_endpoint_from_str() {
        let endpoint_str: Endpoint = "tcp://127.0.0.1:3000".parse().unwrap();
        let endpoint = Endpoint::Tcp(Addr::Ip(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))), 3000);
        assert_eq!(endpoint_str, endpoint);

        let endpoint_str: Endpoint = "udp://127.0.0.1:4000".parse().unwrap();
        let endpoint = Endpoint::Udp(Addr::Ip(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1))), 4000);
        assert_eq!(endpoint_str, endpoint);

        let endpoint_str: Endpoint = "tcp://example.com:3000".parse().unwrap();
        let endpoint = Endpoint::Tcp(Addr::Domain("example.com".to_string()), 3000);
        assert_eq!(endpoint_str, endpoint);

        let endpoint_str = "unix:/home/x/s.socket".parse::<Endpoint>().unwrap();
        let endpoint = Endpoint::Unix(PathBuf::from_str("/home/x/s.socket").unwrap());
        assert_eq!(endpoint_str, endpoint);
    }
}