Skip to content

Commit

Permalink
Parse UDS with prefix
Browse files Browse the repository at this point in the history
Now UDS can be parsed from string with prefix "unix:". The raw path
support will be deprecated.

From #141

Co-authored-by: blackanger <blackanger.z@gmail.com>
  • Loading branch information
eaufavor and ZhangHanDong committed Jun 24, 2024
1 parent a977d40 commit 199b84d
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .bleep
Original file line number Diff line number Diff line change
@@ -1 +1 @@
cab68d4c24c82270ceffef312efc99a3bbc8aaa9
e8c45299beef05e26f59b620d998b98d31139aac
30 changes: 23 additions & 7 deletions pingora-core/src/protocols/l4/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
//! Generic socket type
use crate::{Error, OrErr};
use log::warn;
use nix::sys::socket::{getpeername, getsockname, SockaddrStorage};
use std::cmp::Ordering;
use std::hash::{Hash, Hasher};
Expand Down Expand Up @@ -174,14 +175,23 @@ impl std::str::FromStr for SocketAddr {
type Err = Box<Error>;

// This is very basic parsing logic, it might treat invalid IP:PORT str as UDS path
// TODO: require UDS to have some prefix
fn from_str(s: &str) -> Result<Self, Self::Err> {
match StdSockAddr::from_str(s) {
Ok(addr) => Ok(SocketAddr::Inet(addr)),
Err(_) => {
let uds_socket = StdUnixSockAddr::from_pathname(s)
.or_err(crate::BindError, "invalid UDS path")?;
Ok(SocketAddr::Unix(uds_socket))
if s.starts_with("unix:") {
// format unix:/tmp/server.socket
let path = s.trim_start_matches("unix:");
let uds_socket = StdUnixSockAddr::from_pathname(path)
.or_err(crate::BindError, "invalid UDS path")?;
Ok(SocketAddr::Unix(uds_socket))
} else {
match StdSockAddr::from_str(s) {
Ok(addr) => Ok(SocketAddr::Inet(addr)),
Err(_) => {
// Try to parse as UDS for backward compatibility
let uds_socket = StdUnixSockAddr::from_pathname(s)
.or_err(crate::BindError, "invalid UDS path")?;
warn!("Raw Unix domain socket path support will be deprecated, add 'unix:' prefix instead");
Ok(SocketAddr::Unix(uds_socket))
}
}
}
}
Expand Down Expand Up @@ -246,4 +256,10 @@ mod test {
let uds: SocketAddr = "/tmp/my.sock".parse().unwrap();
assert!(uds.as_unix().is_some());
}

#[test]
fn parse_uds_with_prefix() {
let uds: SocketAddr = "unix:/tmp/my.sock".parse().unwrap();
assert!(uds.as_unix().is_some());
}
}

0 comments on commit 199b84d

Please sign in to comment.