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
|
use std::{
net::{IpAddr, SocketAddr},
os::unix::net::SocketAddr as UnixSocketAddress,
path::PathBuf,
str::FromStr,
};
use bincode::{Decode, Encode};
use url::Url;
use crate::{Error, Result};
/// Port defined as a u16.
pub type Port = u16;
/// Endpoint defines generic network endpoints for karyons.
///
/// # Example
///
/// ```
/// use std::net::SocketAddr;
///
/// use karyons_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)]
pub enum Endpoint {
Udp(Addr, Port),
Tcp(Addr, Port),
Tls(Addr, Port),
Unix(String),
}
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::Unix(path) => {
if path.is_empty() {
write!(f, "unix:/UNNAMED")
} else {
write!(f, "unix:/{}", path)
}
}
}
}
}
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) => {
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 UnixSocketAddress {
type Error = Error;
fn try_from(endpoint: Endpoint) -> std::result::Result<UnixSocketAddress, Self::Error> {
match endpoint {
Endpoint::Unix(a) => Ok(UnixSocketAddress::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)),
_ => 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().to_string())),
_ => Err(Error::InvalidEndpoint(s.to_string())),
}
}
}
}
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 TLS endpoint from a `SocketAddr`.
pub fn new_tls_addr(addr: &SocketAddr) -> Endpoint {
Endpoint::Tls(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 Unix endpoint from a `UnixSocketAddress`.
pub fn new_unix_addr(addr: &UnixSocketAddress) -> Endpoint {
Endpoint::Unix(
addr.as_pathname()
.and_then(|a| a.to_str())
.unwrap_or("")
.to_string(),
)
}
/// Returns the `Port` of the endpoint.
pub fn port(&self) -> Result<&Port> {
match self {
Endpoint::Udp(_, port) | Endpoint::Tcp(_, port) | Endpoint::Tls(_, port) => Ok(port),
_ => Err(Error::TryFromEndpoint),
}
}
/// Returns the `Addr` of the endpoint.
pub fn addr(&self) -> Result<&Addr> {
match self {
Endpoint::Udp(addr, _) | Endpoint::Tcp(addr, _) | Endpoint::Tls(addr, _) => Ok(addr),
_ => Err(Error::TryFromEndpoint),
}
}
}
/// Addr defines a type for an address, either IP or domain.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Encode, Decode)]
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)
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::net::Ipv4Addr;
#[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("/home/x/s.socket".to_string());
assert_eq!(endpoint_str, endpoint);
}
}
|