|
2 | 2 |
|
3 | 3 | use super::super::c;
|
4 | 4 | #[cfg(not(windows))]
|
5 |
| -use crate::ffi::{ZStr, ZString}; |
| 5 | +use super::offsetof_sun_path; |
| 6 | +#[cfg(not(windows))] |
| 7 | +use crate::ffi::ZStr; |
6 | 8 | #[cfg(not(windows))]
|
7 | 9 | use crate::io;
|
8 | 10 | #[cfg(not(windows))]
|
9 | 11 | use crate::path;
|
| 12 | +use core::convert::TryInto; |
10 | 13 | #[cfg(not(windows))]
|
11 | 14 | use core::fmt;
|
| 15 | +#[cfg(not(windows))] |
| 16 | +use core::mem::transmute; |
12 | 17 |
|
13 | 18 | /// `struct sockaddr_un`
|
14 | 19 | #[cfg(not(windows))]
|
15 |
| -#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)] |
| 20 | +#[derive(Clone)] |
16 | 21 | #[doc(alias = "sockaddr_un")]
|
17 | 22 | pub struct SocketAddrUnix {
|
18 |
| - path: ZString, |
| 23 | + pub(crate) unix: libc::sockaddr_un, |
| 24 | + #[cfg(not(any( |
| 25 | + target_os = "dragonfly", |
| 26 | + target_os = "freebsd", |
| 27 | + target_os = "ios", |
| 28 | + target_os = "macos", |
| 29 | + target_os = "netbsd", |
| 30 | + target_os = "openbsd" |
| 31 | + )))] |
| 32 | + len: libc::socklen_t, |
19 | 33 | }
|
20 | 34 |
|
21 | 35 | #[cfg(not(windows))]
|
22 | 36 | impl SocketAddrUnix {
|
23 |
| - /// Construct a new Unix-domain address from a byte slice. |
24 |
| - /// filesystem path. |
| 37 | + /// Construct a new Unix-domain address from a filesystem path. |
25 | 38 | #[inline]
|
26 | 39 | pub fn new<P: path::Arg>(path: P) -> io::Result<Self> {
|
27 |
| - let path = path.into_z_str()?.into_owned(); |
28 |
| - Self::_new(path) |
| 40 | + path.into_with_z_str(|path| Self::_new(path)) |
29 | 41 | }
|
30 | 42 |
|
31 | 43 | #[inline]
|
32 |
| - fn _new(path: ZString) -> io::Result<Self> { |
33 |
| - let bytes = path.as_bytes(); |
| 44 | + fn _new(path: &ZStr) -> io::Result<Self> { |
| 45 | + let mut unix = Self::init(); |
| 46 | + let bytes = path.to_bytes_with_nul(); |
| 47 | + if bytes.len() > unix.sun_path.len() { |
| 48 | + return Err(io::Error::NAMETOOLONG); |
| 49 | + } |
| 50 | + for (i, b) in bytes.iter().enumerate() { |
| 51 | + unix.sun_path[i] = *b as c::c_char; |
| 52 | + } |
34 | 53 |
|
35 |
| - let z = c::sockaddr_un { |
36 |
| - #[cfg(any( |
| 54 | + #[cfg(any( |
| 55 | + target_os = "dragonfly", |
| 56 | + target_os = "freebsd", |
| 57 | + target_os = "ios", |
| 58 | + target_os = "macos", |
| 59 | + target_os = "netbsd", |
| 60 | + target_os = "openbsd" |
| 61 | + ))] |
| 62 | + { |
| 63 | + unix.sun_len = (offsetof_sun_path() + bytes.len()).try_into().unwrap(); |
| 64 | + } |
| 65 | + |
| 66 | + Ok(Self { |
| 67 | + unix, |
| 68 | + #[cfg(not(any( |
| 69 | + target_os = "dragonfly", |
| 70 | + target_os = "freebsd", |
| 71 | + target_os = "ios", |
| 72 | + target_os = "macos", |
| 73 | + target_os = "netbsd", |
| 74 | + target_os = "openbsd" |
| 75 | + )))] |
| 76 | + len: (offsetof_sun_path() + bytes.len()).try_into().unwrap(), |
| 77 | + }) |
| 78 | + } |
| 79 | + |
| 80 | + /// Construct a new abstract Unix-domain address from a byte slice. |
| 81 | + #[cfg(any(target_os = "android", target_os = "linux"))] |
| 82 | + #[inline] |
| 83 | + pub fn new_abstract_name(name: &[u8]) -> io::Result<Self> { |
| 84 | + let mut unix = Self::init(); |
| 85 | + if 1 + name.len() > unix.sun_path.len() { |
| 86 | + return Err(io::Error::NAMETOOLONG); |
| 87 | + } |
| 88 | + unix.sun_path[0] = b'\0' as c::c_char; |
| 89 | + for (i, b) in name.iter().enumerate() { |
| 90 | + unix.sun_path[1 + i] = *b as c::c_char; |
| 91 | + } |
| 92 | + let len = offsetof_sun_path() + 1 + name.len(); |
| 93 | + let len = len.try_into().unwrap(); |
| 94 | + Ok(Self { |
| 95 | + unix, |
| 96 | + #[cfg(not(any( |
37 | 97 | target_os = "dragonfly",
|
| 98 | + target_os = "freebsd", |
38 | 99 | target_os = "ios",
|
| 100 | + target_os = "macos", |
| 101 | + target_os = "netbsd", |
| 102 | + target_os = "openbsd" |
| 103 | + )))] |
| 104 | + len, |
| 105 | + }) |
| 106 | + } |
| 107 | + |
| 108 | + fn init() -> c::sockaddr_un { |
| 109 | + c::sockaddr_un { |
| 110 | + #[cfg(any( |
| 111 | + target_os = "dragonfly", |
39 | 112 | target_os = "freebsd",
|
| 113 | + target_os = "ios", |
40 | 114 | target_os = "macos",
|
41 | 115 | target_os = "netbsd",
|
42 | 116 | target_os = "openbsd"
|
43 | 117 | ))]
|
44 | 118 | sun_len: 0,
|
45 |
| - sun_family: 0, |
| 119 | + sun_family: c::AF_UNIX as _, |
46 | 120 | #[cfg(any(
|
47 | 121 | target_os = "dragonfly",
|
48 |
| - target_os = "ios", |
49 | 122 | target_os = "freebsd",
|
| 123 | + target_os = "ios", |
50 | 124 | target_os = "macos",
|
51 | 125 | target_os = "netbsd",
|
52 | 126 | target_os = "openbsd"
|
53 | 127 | ))]
|
54 | 128 | sun_path: [0; 104],
|
55 | 129 | #[cfg(not(any(
|
56 | 130 | target_os = "dragonfly",
|
57 |
| - target_os = "ios", |
58 | 131 | target_os = "freebsd",
|
| 132 | + target_os = "ios", |
59 | 133 | target_os = "macos",
|
60 | 134 | target_os = "netbsd",
|
61 | 135 | target_os = "openbsd"
|
62 | 136 | )))]
|
63 | 137 | sun_path: [0; 108],
|
64 |
| - }; |
65 |
| - if bytes.len() + 1 > z.sun_path.len() { |
66 |
| - return Err(io::Error::NAMETOOLONG); |
67 | 138 | }
|
68 |
| - Ok(Self { path }) |
69 | 139 | }
|
70 | 140 |
|
71 |
| - /// Returns a reference to the contained path. |
| 141 | + /// For a filesystem path address, return the path. |
72 | 142 | #[inline]
|
73 |
| - pub fn path(&self) -> &ZStr { |
74 |
| - &self.path |
| 143 | + pub fn path(&self) -> Option<&ZStr> { |
| 144 | + let len = self.len(); |
| 145 | + if len != 0 && self.unix.sun_path[0] != b'\0' as c::c_char { |
| 146 | + let end = len as usize - offsetof_sun_path(); |
| 147 | + // Safety: Transmuting between `&[c_char]` and `&[u8]`. |
| 148 | + unsafe { |
| 149 | + Some(ZStr::from_bytes_with_nul(transmute(&self.unix.sun_path[..end])).unwrap()) |
| 150 | + } |
| 151 | + } else { |
| 152 | + None |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + /// For an abstract address, return the identifier. |
| 157 | + #[inline] |
| 158 | + #[cfg(any(target_os = "android", target_os = "linux"))] |
| 159 | + pub fn abstract_name(&self) -> Option<&[u8]> { |
| 160 | + let len = self.len(); |
| 161 | + if len != 0 && self.unix.sun_path[0] == b'\0' as c::c_char { |
| 162 | + let end = len as usize - offsetof_sun_path(); |
| 163 | + // Safety: Transmuting between `&[c_char]` and `&[u8]`. |
| 164 | + unsafe { Some(transmute(&self.unix.sun_path[1..end])) } |
| 165 | + } else { |
| 166 | + None |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + #[inline] |
| 171 | + pub(crate) fn addr_len(&self) -> c::socklen_t { |
| 172 | + #[cfg(not(any( |
| 173 | + target_os = "dragonfly", |
| 174 | + target_os = "freebsd", |
| 175 | + target_os = "ios", |
| 176 | + target_os = "macos", |
| 177 | + target_os = "netbsd", |
| 178 | + target_os = "openbsd" |
| 179 | + )))] |
| 180 | + { |
| 181 | + self.len |
| 182 | + } |
| 183 | + #[cfg(any( |
| 184 | + target_os = "dragonfly", |
| 185 | + target_os = "freebsd", |
| 186 | + target_os = "ios", |
| 187 | + target_os = "macos", |
| 188 | + target_os = "netbsd", |
| 189 | + target_os = "openbsd" |
| 190 | + ))] |
| 191 | + { |
| 192 | + c::socklen_t::from(self.unix.sun_len) |
| 193 | + } |
| 194 | + } |
| 195 | + |
| 196 | + #[inline] |
| 197 | + pub(crate) fn len(&self) -> usize { |
| 198 | + self.addr_len() as usize |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +#[cfg(not(windows))] |
| 203 | +impl PartialEq for SocketAddrUnix { |
| 204 | + #[inline] |
| 205 | + fn eq(&self, other: &Self) -> bool { |
| 206 | + let self_len = self.len() - offsetof_sun_path(); |
| 207 | + let other_len = other.len() - offsetof_sun_path(); |
| 208 | + self.unix.sun_path[..self_len].eq(&other.unix.sun_path[..other_len]) |
| 209 | + } |
| 210 | +} |
| 211 | + |
| 212 | +#[cfg(not(windows))] |
| 213 | +impl Eq for SocketAddrUnix {} |
| 214 | + |
| 215 | +#[cfg(not(windows))] |
| 216 | +impl PartialOrd for SocketAddrUnix { |
| 217 | + #[inline] |
| 218 | + fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> { |
| 219 | + let self_len = self.len() - offsetof_sun_path(); |
| 220 | + let other_len = other.len() - offsetof_sun_path(); |
| 221 | + self.unix.sun_path[..self_len].partial_cmp(&other.unix.sun_path[..other_len]) |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +#[cfg(not(windows))] |
| 226 | +impl Ord for SocketAddrUnix { |
| 227 | + #[inline] |
| 228 | + fn cmp(&self, other: &Self) -> core::cmp::Ordering { |
| 229 | + let self_len = self.len() - offsetof_sun_path(); |
| 230 | + let other_len = other.len() - offsetof_sun_path(); |
| 231 | + self.unix.sun_path[..self_len].cmp(&other.unix.sun_path[..other_len]) |
| 232 | + } |
| 233 | +} |
| 234 | + |
| 235 | +#[cfg(not(windows))] |
| 236 | +impl core::hash::Hash for SocketAddrUnix { |
| 237 | + #[inline] |
| 238 | + fn hash<H: core::hash::Hasher>(&self, state: &mut H) { |
| 239 | + let self_len = self.len() - offsetof_sun_path(); |
| 240 | + self.unix.sun_path[..self_len].hash(state) |
75 | 241 | }
|
76 | 242 | }
|
77 | 243 |
|
78 | 244 | #[cfg(not(windows))]
|
79 | 245 | impl fmt::Debug for SocketAddrUnix {
|
80 | 246 | fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
81 |
| - self.path.fmt(fmt) |
| 247 | + if let Some(path) = self.path() { |
| 248 | + path.fmt(fmt) |
| 249 | + } else { |
| 250 | + #[cfg(any(target_os = "android", target_os = "linux"))] |
| 251 | + if let Some(name) = self.abstract_name() { |
| 252 | + return name.fmt(fmt); |
| 253 | + } |
| 254 | + |
| 255 | + "(unnamed)".fmt(fmt) |
| 256 | + } |
82 | 257 | }
|
83 | 258 | }
|
84 | 259 |
|
|
0 commit comments