Mercurial > crates > systemd-socket
annotate src/lib.rs @ 20:2b78a483f84b
Update `libsystemd`
| author | Martin Habovstiak <martin.habovstiak@gmail.com> |
|---|---|
| date | Sat, 13 Jul 2024 14:19:29 +0200 |
| parents | db1dc99252e2 |
| children | f6334887e3c8 |
| rev | line source |
|---|---|
| 0 | 1 //! A convenience crate for optionally supporting systemd socket activation. |
| 2 //! | |
| 3 //! ## About | |
| 4 //! | |
| 5 //! The goal of this crate is to make socket activation with systemd in your project trivial. | |
| 6 //! It provides a replacement for `std::net::SocketAddr` that allows parsing the bind address from string just like the one from `std` | |
| 7 //! but on top of that also allows `systemd://socket_name` format that tells it to use systemd activation with given socket name. | |
| 8 //! Then it provides a method to bind the address which will return the socket from systemd if available. | |
| 9 //! | |
| 10 //! The provided type supports conversions from various types of strings and also `serde` and `parse_arg` via feature flag. | |
| 11 //! Thanks to this the change to your code should be minimal - parsing will continue to work, it'll just allow a new format. | |
| 12 //! You only need to change the code to use `SocketAddr::bind()` instead of `TcpListener::bind()` for binding. | |
| 13 //! | |
|
6
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
14 //! You also don't need to worry about conditional compilation to ensure OS compatibility. |
|
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
15 //! This crate handles that for you by disabling systemd on non-linux systems. |
|
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
16 //! |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
17 //! Further, the crate also provides methods for binding `tokio` 0.2, 0.3, and `async_std` sockets if the appropriate features are |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
18 //! activated. |
| 0 | 19 //! |
| 20 //! ## Example | |
| 21 //! | |
| 22 //! ```no_run | |
| 23 //! use systemd_socket::SocketAddr; | |
| 24 //! use std::convert::TryFrom; | |
| 25 //! use std::io::Write; | |
| 26 //! | |
| 27 //! let mut args = std::env::args_os(); | |
| 28 //! let program_name = args.next().expect("unknown program name"); | |
| 29 //! let socket_addr = args.next().expect("missing socket address"); | |
| 30 //! let socket_addr = SocketAddr::try_from(socket_addr).expect("failed to parse socket address"); | |
| 31 //! let socket = socket_addr.bind().expect("failed to bind socket"); | |
| 32 //! | |
| 33 //! loop { | |
| 34 //! let _ = socket | |
| 35 //! .accept() | |
| 36 //! .expect("failed to accept connection") | |
| 37 //! .0 | |
| 38 //! .write_all(b"Hello world!") | |
| 39 //! .map_err(|err| eprintln!("Failed to send {}", err)); | |
| 40 //! } | |
| 41 //! ``` | |
| 42 //! | |
| 43 //! ## Features | |
| 44 //! | |
|
13
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
11
diff
changeset
|
45 //! * `enable_systemd` - on by default, the existence of this feature can allow your users to turn |
|
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
11
diff
changeset
|
46 //! off systemd support if they don't need it. Note that it's already disabled on non-linux |
|
f740dadd2948
Added enable_systemd feature
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
11
diff
changeset
|
47 //! systems, so you don't need to care about that. |
| 0 | 48 //! * `serde` - implements `serde::Deserialize` for `SocketAddr` |
| 49 //! * `parse_arg` - implements `parse_arg::ParseArg` for `SocketAddr` | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
50 //! * `tokio_0_2` - adds `bind_tokio_0_2` method to `SocketAddr` |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
51 //! * `tokio_0_3` - adds `bind_tokio_0_3` method to `SocketAddr` |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
52 //! * `async_std` - adds `bind_async_std` method to `SocketAddr` |
|
3
0edcde404b02
Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
53 //! |
|
0edcde404b02
Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
54 //! ## MSRV |
|
0edcde404b02
Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
55 //! |
|
0edcde404b02
Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
56 //! This crate must always compile with the latest Rust available in the latest Debian stable. |
|
0edcde404b02
Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
57 //! That is currently Rust 1.41.1. (Debian 10 - Buster) |
|
0edcde404b02
Added information about MSRV
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
2
diff
changeset
|
58 |
| 0 | 59 |
| 60 #![deny(missing_docs)] | |
| 61 | |
| 62 pub mod error; | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
63 mod resolv_addr; |
| 0 | 64 |
| 65 use std::convert::{TryFrom, TryInto}; | |
| 66 use std::fmt; | |
| 67 use std::ffi::{OsStr, OsString}; | |
| 68 use crate::error::*; | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
69 use crate::resolv_addr::ResolvAddr; |
| 0 | 70 |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
71 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
|
6
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
72 use std::convert::Infallible as Never; |
|
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
73 |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
74 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 0 | 75 pub(crate) mod systemd_sockets { |
| 76 use std::fmt; | |
| 77 use std::sync::Mutex; | |
| 78 use libsystemd::activation::FileDescriptor; | |
|
20
2b78a483f84b
Update `libsystemd`
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
18
diff
changeset
|
79 use libsystemd::errors::SdError as LibSystemdError; |
|
2b78a483f84b
Update `libsystemd`
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
18
diff
changeset
|
80 type LibSystemdResult<T> = Result<T, LibSystemdError>; |
| 0 | 81 |
| 82 #[derive(Debug)] | |
| 83 pub(crate) struct Error(&'static Mutex<LibSystemdError>); | |
| 84 | |
| 85 impl fmt::Display for Error { | |
| 86 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| 87 fmt::Display::fmt(&*self.0.lock().expect("mutex poisoned"), f) | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 // No source we can't keep the mutex locked | |
| 92 impl std::error::Error for Error {} | |
| 93 | |
| 94 pub(crate) fn take(name: &str) -> Result<Option<FileDescriptor>, Error> { | |
| 95 match &*SYSTEMD_SOCKETS { | |
| 96 Ok(sockets) => Ok(sockets.take(name)), | |
| 97 Err(error) => Err(Error(error)) | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 struct SystemdSockets(std::sync::Mutex<std::collections::HashMap<String, FileDescriptor>>); | |
| 102 | |
| 103 impl SystemdSockets { | |
| 104 fn new() -> LibSystemdResult<Self> { | |
| 105 // MUST BE true FOR SAFETY!!! | |
| 106 let map = libsystemd::activation::receive_descriptors_with_names(/*unset env = */ true)?.into_iter().map(|(fd, name)| (name, fd)).collect(); | |
| 107 Ok(SystemdSockets(Mutex::new(map))) | |
| 108 } | |
| 109 | |
| 110 fn take(&self, name: &str) -> Option<FileDescriptor> { | |
| 111 // MUST remove THE SOCKET FOR SAFETY!!! | |
| 112 self.0.lock().expect("poisoned mutex").remove(name) | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 lazy_static::lazy_static! { | |
| 117 // We don't panic in order to let the application handle the error later | |
| 118 static ref SYSTEMD_SOCKETS: Result<SystemdSockets, Mutex<LibSystemdError>> = SystemdSockets::new().map_err(Mutex::new); | |
| 119 } | |
| 120 } | |
| 121 | |
| 122 /// Socket address that can be an ordinary address or a systemd socket | |
| 123 /// | |
| 124 /// This is the core type of this crate that abstracts possible addresses. | |
| 125 /// It can be (fallibly) converted from various types of strings or deserialized with `serde`. | |
| 126 /// After it's created, it can be bound as `TcpListener` from `std` or even `tokio` or `async_std` | |
| 127 /// if the appropriate feature is enabled. | |
| 128 /// | |
| 129 /// Optional dependencies on `parse_arg` and `serde` make it trivial to use with | |
| 130 /// [`configure_me`](https://crates.io/crates/configure_me). | |
| 131 #[derive(Debug)] | |
| 132 #[cfg_attr(feature = "serde", derive(serde_crate::Deserialize), serde(crate = "serde_crate", try_from = "serde_str_helpers::DeserBorrowStr"))] | |
| 133 pub struct SocketAddr(SocketAddrInner); | |
| 134 | |
| 135 impl SocketAddr { | |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
136 /// Creates SocketAddr from systemd name directly, without requiring `systemd://` prefix. |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
137 /// |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
138 /// Always fails with systemd unsupported error if systemd is not supported. |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
139 pub fn from_systemd_name<T: Into<String>>(name: T) -> Result<Self, ParseError> { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
140 Self::inner_from_systemd_name(name.into(), false) |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
141 } |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
142 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
143 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
144 fn inner_from_systemd_name(name: String, prefixed: bool) -> Result<Self, ParseError> { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
145 let real_systemd_name = if prefixed { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
146 &name[SYSTEMD_PREFIX.len()..] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
147 } else { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
148 &name |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
149 }; |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
150 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
151 let name_len = real_systemd_name.len(); |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
152 match real_systemd_name.chars().enumerate().find(|(_, c)| !c.is_ascii() || *c < ' ' || *c == ':') { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
153 None if name_len <= 255 && prefixed => Ok(SocketAddr(SocketAddrInner::Systemd(name))), |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
154 None if name_len <= 255 && !prefixed => Ok(SocketAddr(SocketAddrInner::SystemdNoPrefix(name))), |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
155 None => Err(ParseErrorInner::LongSocketName { string: name, len: name_len }.into()), |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
156 Some((pos, c)) => Err(ParseErrorInner::InvalidCharacter { string: name, c, pos, }.into()), |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
157 } |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
158 } |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
159 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
160 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
161 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
162 fn inner_from_systemd_name(name: String, _prefixed: bool) -> Result<Self, ParseError> { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
163 Err(ParseError(ParseErrorInner::SystemdUnsupported(name))) |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
164 } |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
165 |
| 0 | 166 /// Creates `std::net::TcpListener` |
| 167 /// | |
| 168 /// This method either `binds` the socket, if the address was provided or uses systemd socket | |
| 169 /// if the socket name was provided. | |
| 170 pub fn bind(self) -> Result<std::net::TcpListener, BindError> { | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
171 match self.0 { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
172 SocketAddrInner::Ordinary(addr) => match std::net::TcpListener::bind(addr) { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
173 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
174 Err(error) => Err(BindErrorInner::BindFailed { addr, error, }.into()), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
175 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
176 SocketAddrInner::WithHostname(addr) => match std::net::TcpListener::bind(addr.as_str()) { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
177 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
178 Err(error) => Err(BindErrorInner::BindOrResolvFailed { addr, error, }.into()), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
179 }, |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
180 SocketAddrInner::Systemd(socket_name) => Self::get_systemd(socket_name, true).map(|(socket, _)| socket), |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
181 SocketAddrInner::SystemdNoPrefix(socket_name) => Self::get_systemd(socket_name, false).map(|(socket, _)| socket), |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
182 } |
| 0 | 183 } |
| 184 | |
| 185 /// Creates `tokio::net::TcpListener` | |
| 186 /// | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
187 /// To be specific, it binds the socket or converts systemd socket to `tokio` 0.2 socket. |
| 0 | 188 /// |
| 189 /// This method either `binds` the socket, if the address was provided or uses systemd socket | |
| 190 /// if the socket name was provided. | |
| 191 #[cfg(feature = "tokio_0_2")] | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
192 pub async fn bind_tokio_0_2(self) -> Result<tokio_0_2::net::TcpListener, TokioBindError> { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
193 match self.0 { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
194 SocketAddrInner::Ordinary(addr) => match tokio_0_2::net::TcpListener::bind(addr).await { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
195 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
196 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindFailed { addr, error, }.into())), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
197 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
198 SocketAddrInner::WithHostname(addr) => match tokio_0_2::net::TcpListener::bind(addr.as_str()).await { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
199 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
200 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindOrResolvFailed { addr, error, }.into())), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
201 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
202 SocketAddrInner::Systemd(socket_name) => { |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
203 let (socket, addr) = Self::get_systemd(socket_name, true)?; |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
204 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into()) |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
205 }, |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
206 SocketAddrInner::SystemdNoPrefix(socket_name) => { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
207 let (socket, addr) = Self::get_systemd(socket_name, false)?; |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
208 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into()) |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
209 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
210 } |
| 0 | 211 } |
| 212 | |
| 213 /// Creates `tokio::net::TcpListener` | |
| 214 /// | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
215 /// To be specific, it binds the socket or converts systemd socket to `tokio` 0.3 socket. |
| 0 | 216 /// |
| 217 /// This method either `binds` the socket, if the address was provided or uses systemd socket | |
| 218 /// if the socket name was provided. | |
| 219 #[cfg(feature = "tokio_0_3")] | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
220 pub async fn bind_tokio_0_3(self) -> Result<tokio_0_3::net::TcpListener, TokioBindError> { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
221 match self.0 { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
222 SocketAddrInner::Ordinary(addr) => match tokio_0_3::net::TcpListener::bind(addr).await { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
223 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
224 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindFailed { addr, error, }.into())), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
225 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
226 SocketAddrInner::WithHostname(addr) => match tokio_0_3::net::TcpListener::bind(addr.as_str()).await { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
227 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
228 Err(error) => Err(TokioBindError::Bind(BindErrorInner::BindOrResolvFailed { addr, error, }.into())), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
229 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
230 SocketAddrInner::Systemd(socket_name) => { |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
231 let (socket, addr) = Self::get_systemd(socket_name, true)?; |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
232 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into()) |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
233 }, |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
234 SocketAddrInner::SystemdNoPrefix(socket_name) => { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
235 let (socket, addr) = Self::get_systemd(socket_name, false)?; |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
236 socket.try_into().map_err(|error| TokioConversionError { addr, error, }.into()) |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
237 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
238 } |
| 0 | 239 } |
| 240 | |
| 241 /// Creates `async_std::net::TcpListener` | |
| 242 /// | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
243 /// To be specific, it binds the socket or converts systemd socket to `async_std` socket. |
| 0 | 244 /// |
| 245 /// This method either `binds` the socket, if the address was provided or uses systemd socket | |
| 246 /// if the socket name was provided. | |
| 247 #[cfg(feature = "async-std")] | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
248 pub async fn bind_async_std(self) -> Result<async_std::net::TcpListener, BindError> { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
249 match self.0 { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
250 SocketAddrInner::Ordinary(addr) => match async_std::net::TcpListener::bind(addr).await { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
251 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
252 Err(error) => Err(BindErrorInner::BindFailed { addr, error, }.into()), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
253 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
254 SocketAddrInner::WithHostname(addr) => match async_std::net::TcpListener::bind(addr.as_str()).await { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
255 Ok(socket) => Ok(socket), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
256 Err(error) => Err(BindErrorInner::BindOrResolvFailed { addr, error, }.into()), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
257 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
258 SocketAddrInner::Systemd(socket_name) => { |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
259 let (socket, _) = Self::get_systemd(socket_name, true)?; |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
260 Ok(socket.into()) |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
261 }, |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
262 SocketAddrInner::SystemdNoPrefix(socket_name) => { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
263 let (socket, _) = Self::get_systemd(socket_name, false)?; |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
264 Ok(socket.into()) |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
265 }, |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
266 } |
| 0 | 267 } |
| 268 | |
| 269 // We can't impl<T: Deref<Target=str> + Into<String>> TryFrom<T> for SocketAddr because of orphan | |
| 270 // rules. | |
| 271 fn try_from_generic<'a, T>(string: T) -> Result<Self, ParseError> where T: 'a + std::ops::Deref<Target=str> + Into<String> { | |
| 272 if string.starts_with(SYSTEMD_PREFIX) { | |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
273 Self::inner_from_systemd_name(string.into(), true) |
| 0 | 274 } else { |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
275 match string.parse() { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
276 Ok(addr) => Ok(SocketAddr(SocketAddrInner::Ordinary(addr))), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
277 Err(_) => Ok(SocketAddr(SocketAddrInner::WithHostname(ResolvAddr::try_from_generic(string).map_err(ParseErrorInner::ResolvAddr)?))), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
278 } |
| 0 | 279 } |
| 280 } | |
| 281 | |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
282 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
283 fn get_systemd(socket_name: String, prefixed: bool) -> Result<(std::net::TcpListener, SocketAddrInner), BindError> { |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
284 use libsystemd::activation::IsType; |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
285 use std::os::unix::io::{FromRawFd, IntoRawFd}; |
| 0 | 286 |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
287 let real_systemd_name = if prefixed { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
288 &socket_name[SYSTEMD_PREFIX.len()..] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
289 } else { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
290 &socket_name |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
291 }; |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
292 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
293 let socket = systemd_sockets::take(real_systemd_name).map_err(BindErrorInner::ReceiveDescriptors)?; |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
294 // Safety: The environment variable is unset, so that no other calls can get the |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
295 // descriptors. The descriptors are taken from the map, not cloned, so they can't |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
296 // be duplicated. |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
297 unsafe { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
298 // match instead of combinators to avoid cloning socket_name |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
299 match socket { |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
300 Some(socket) if socket.is_inet() => Ok((std::net::TcpListener::from_raw_fd(socket.into_raw_fd()), SocketAddrInner::Systemd(socket_name))), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
301 Some(_) => Err(BindErrorInner::NotInetSocket(socket_name).into()), |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
302 None => Err(BindErrorInner::MissingDescriptor(socket_name).into()) |
|
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
303 } |
| 0 | 304 } |
| 305 } | |
|
6
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
306 |
|
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
307 // This approach makes the rest of the code much simpler as it doesn't require sprinkling it |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
308 // with #[cfg(all(target_os = "linux", feature = "enable_systemd"))] yet still statically guarantees it won't execute. |
|
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
309 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
310 fn get_systemd(socket_name: Never, _prefixed: bool) -> Result<(std::net::TcpListener, SocketAddrInner), BindError> { |
|
6
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
311 match socket_name {} |
|
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
312 } |
| 0 | 313 } |
| 314 | |
| 315 /// Displays the address in format that can be parsed again. | |
| 316 /// | |
| 317 /// **Important: While I don't expect this impl to change, don't rely on it!** | |
| 318 /// It should be used mostly for debugging/logging. | |
| 319 impl fmt::Display for SocketAddr { | |
| 320 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| 321 fmt::Display::fmt(&self.0, f) | |
| 322 } | |
| 323 } | |
| 324 | |
| 325 impl fmt::Display for SocketAddrInner { | |
| 326 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | |
| 327 match self { | |
| 328 SocketAddrInner::Ordinary(addr) => fmt::Display::fmt(addr, f), | |
| 329 SocketAddrInner::Systemd(addr) => fmt::Display::fmt(addr, f), | |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
330 SocketAddrInner::SystemdNoPrefix(addr) => write!(f, "{}{}", SYSTEMD_PREFIX, addr), |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
331 SocketAddrInner::WithHostname(addr) => fmt::Display::fmt(addr, f), |
| 0 | 332 } |
| 333 } | |
| 334 } | |
| 335 | |
| 336 // PartialEq for testing, I'm not convinced it should be exposed | |
| 337 #[derive(Debug, PartialEq)] | |
| 338 enum SocketAddrInner { | |
| 339 Ordinary(std::net::SocketAddr), | |
|
4
66c0e10c89fc
Support resolving hostnames
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
3
diff
changeset
|
340 WithHostname(resolv_addr::ResolvAddr), |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
341 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 0 | 342 Systemd(String), |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
343 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
|
6
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
344 #[allow(dead_code)] |
|
a7893294e9b2
Make the crate compilable on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
4
diff
changeset
|
345 Systemd(Never), |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
346 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
347 #[allow(dead_code)] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
348 SystemdNoPrefix(String), |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
349 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
350 #[allow(dead_code)] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
351 SystemdNoPrefix(Never), |
| 0 | 352 } |
| 353 | |
| 354 const SYSTEMD_PREFIX: &str = "systemd://"; | |
| 355 | |
|
11
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
356 impl<I: Into<std::net::IpAddr>> From<(I, u16)> for SocketAddr { |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
357 fn from(value: (I, u16)) -> Self { |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
358 SocketAddr(SocketAddrInner::Ordinary(value.into())) |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
359 } |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
360 } |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
361 |
|
18
db1dc99252e2
Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
17
diff
changeset
|
362 impl From<std::net::SocketAddr> for SocketAddr { |
|
db1dc99252e2
Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
17
diff
changeset
|
363 fn from(value: std::net::SocketAddr) -> Self { |
|
db1dc99252e2
Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
17
diff
changeset
|
364 SocketAddr(SocketAddrInner::Ordinary(value)) |
|
db1dc99252e2
Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
17
diff
changeset
|
365 } |
|
db1dc99252e2
Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
17
diff
changeset
|
366 } |
|
db1dc99252e2
Added From<std::net::SocketAddr>
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
17
diff
changeset
|
367 |
|
11
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
368 impl From<std::net::SocketAddrV4> for SocketAddr { |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
369 fn from(value: std::net::SocketAddrV4) -> Self { |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
370 SocketAddr(SocketAddrInner::Ordinary(value.into())) |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
371 } |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
372 } |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
373 |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
374 impl From<std::net::SocketAddrV6> for SocketAddr { |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
375 fn from(value: std::net::SocketAddrV6) -> Self { |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
376 SocketAddr(SocketAddrInner::Ordinary(value.into())) |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
377 } |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
378 } |
|
13e2a5545167
Implement From conversions
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
9
diff
changeset
|
379 |
| 0 | 380 impl std::str::FromStr for SocketAddr { |
| 381 type Err = ParseError; | |
| 382 | |
| 383 fn from_str(s: &str) -> Result<Self, Self::Err> { | |
| 384 SocketAddr::try_from_generic(s) | |
| 385 } | |
| 386 } | |
| 387 | |
| 388 impl<'a> TryFrom<&'a str> for SocketAddr { | |
| 389 type Error = ParseError; | |
| 390 | |
| 391 fn try_from(s: &'a str) -> Result<Self, Self::Error> { | |
| 392 SocketAddr::try_from_generic(s) | |
| 393 } | |
| 394 } | |
| 395 | |
| 396 impl TryFrom<String> for SocketAddr { | |
| 397 type Error = ParseError; | |
| 398 | |
| 399 fn try_from(s: String) -> Result<Self, Self::Error> { | |
| 400 SocketAddr::try_from_generic(s) | |
| 401 } | |
| 402 } | |
| 403 | |
| 404 impl<'a> TryFrom<&'a OsStr> for SocketAddr { | |
| 405 type Error = ParseOsStrError; | |
| 406 | |
| 407 fn try_from(s: &'a OsStr) -> Result<Self, Self::Error> { | |
| 408 s.to_str().ok_or(ParseOsStrError::InvalidUtf8)?.try_into().map_err(Into::into) | |
| 409 } | |
| 410 } | |
| 411 | |
| 412 impl TryFrom<OsString> for SocketAddr { | |
| 413 type Error = ParseOsStrError; | |
| 414 | |
| 415 fn try_from(s: OsString) -> Result<Self, Self::Error> { | |
| 416 s.into_string().map_err(|_| ParseOsStrError::InvalidUtf8)?.try_into().map_err(Into::into) | |
| 417 } | |
| 418 } | |
| 419 | |
| 420 #[cfg(feature = "serde")] | |
| 421 impl<'a> TryFrom<serde_str_helpers::DeserBorrowStr<'a>> for SocketAddr { | |
| 422 type Error = ParseError; | |
| 423 | |
| 424 fn try_from(s: serde_str_helpers::DeserBorrowStr<'a>) -> Result<Self, Self::Error> { | |
|
9
4fb70ca820a6
Upgrade serde_str_helpers
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
7
diff
changeset
|
425 SocketAddr::try_from_generic(s) |
| 0 | 426 } |
| 427 } | |
| 428 | |
| 429 #[cfg(feature = "parse_arg")] | |
| 430 impl parse_arg::ParseArg for SocketAddr { | |
| 431 type Error = ParseOsStrError; | |
| 432 | |
| 433 fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result { | |
| 434 std::net::SocketAddr::describe_type(&mut writer)?; | |
| 435 write!(writer, " or a systemd socket name prefixed with systemd://") | |
| 436 } | |
| 437 | |
| 438 fn parse_arg(arg: &OsStr) -> Result<Self, Self::Error> { | |
| 439 arg.try_into() | |
| 440 } | |
| 441 | |
| 442 fn parse_owned_arg(arg: OsString) -> Result<Self, Self::Error> { | |
| 443 arg.try_into() | |
| 444 } | |
| 445 } | |
| 446 | |
| 447 #[cfg(test)] | |
| 448 mod tests { | |
| 449 use super::{SocketAddr, SocketAddrInner}; | |
| 450 | |
| 451 #[test] | |
| 452 fn parse_ordinary() { | |
| 453 assert_eq!("127.0.0.1:42".parse::<SocketAddr>().unwrap().0, SocketAddrInner::Ordinary(([127, 0, 0, 1], 42).into())); | |
| 454 } | |
| 455 | |
| 456 #[test] | |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
457 #[cfg(all(target_os = "linux", feature = "enable_systemd"))] |
| 0 | 458 fn parse_systemd() { |
| 459 assert_eq!("systemd://foo".parse::<SocketAddr>().unwrap().0, SocketAddrInner::Systemd("systemd://foo".to_owned())); | |
| 460 } | |
|
2
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
461 |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
462 #[test] |
|
16
bc76507dd878
Fixed conditional compilation based on OS
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
13
diff
changeset
|
463 #[cfg(not(all(target_os = "linux", feature = "enable_systemd")))] |
|
7
9731ff589d9c
Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
464 #[should_panic] |
|
9731ff589d9c
Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
465 fn parse_systemd() { |
|
9731ff589d9c
Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
466 "systemd://foo".parse::<SocketAddr>().unwrap(); |
|
9731ff589d9c
Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
467 } |
|
9731ff589d9c
Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
468 |
|
9731ff589d9c
Fix tests on non-linux systems
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
6
diff
changeset
|
469 #[test] |
|
2
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
470 #[should_panic] |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
471 fn parse_systemd_fail_control() { |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
472 "systemd://foo\n".parse::<SocketAddr>().unwrap(); |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
473 } |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
474 |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
475 #[test] |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
476 #[should_panic] |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
477 fn parse_systemd_fail_colon() { |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
478 "systemd://foo:".parse::<SocketAddr>().unwrap(); |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
479 } |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
480 |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
481 #[test] |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
482 #[should_panic] |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
483 fn parse_systemd_fail_non_ascii() { |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
484 "systemd://fooá".parse::<SocketAddr>().unwrap(); |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
485 } |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
486 |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
487 #[test] |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
488 #[should_panic] |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
489 fn parse_systemd_fail_too_long() { |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
490 "systemd://xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".parse::<SocketAddr>().unwrap(); |
|
cabc4aafdd85
Added length check and a few tests
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
0
diff
changeset
|
491 } |
|
17
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
492 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
493 #[test] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
494 #[cfg_attr(not(all(target_os = "linux", feature = "enable_systemd")), should_panic)] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
495 fn no_prefix_parse_systemd() { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
496 SocketAddr::from_systemd_name("foo").unwrap(); |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
497 } |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
498 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
499 #[test] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
500 #[should_panic] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
501 fn no_prefix_parse_systemd_fail_non_ascii() { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
502 SocketAddr::from_systemd_name("fooá").unwrap(); |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
503 } |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
504 |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
505 #[test] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
506 #[should_panic] |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
507 fn no_prefix_parse_systemd_fail_too_long() { |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
508 SocketAddr::from_systemd_name("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx").unwrap(); |
|
dfb727367934
Allow specifying systemd name directly
Martin Habovstiak <martin.habovstiak@gmail.com>
parents:
16
diff
changeset
|
509 } |
| 0 | 510 } |
