diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-06-21 19:44:57 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-06-21 19:44:57 +0900 |
| commit | 4575b55e7769e14d2d88d79ec97e9939c978ed9a (patch) | |
| tree | 19cc05fcac6939e210bacdc7548eaeb92070098a /crates/shirabe-php-shim/src/net.rs | |
| parent | 26bd490e6f9dc8476bc2cc9902f18692d8715df4 (diff) | |
| download | php-shirabe-4575b55e7769e14d2d88d79ec97e9939c978ed9a.tar.gz php-shirabe-4575b55e7769e14d2d88d79ec97e9939c978ed9a.tar.zst php-shirabe-4575b55e7769e14d2d88d79ec97e9939c978ed9a.zip | |
feat(php-shim): implement gethostbyname() and inet_pton()
Diffstat (limited to 'crates/shirabe-php-shim/src/net.rs')
| -rw-r--r-- | crates/shirabe-php-shim/src/net.rs | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/crates/shirabe-php-shim/src/net.rs b/crates/shirabe-php-shim/src/net.rs index e7ecb18..0ecf5cb 100644 --- a/crates/shirabe-php-shim/src/net.rs +++ b/crates/shirabe-php-shim/src/net.rs @@ -1,13 +1,31 @@ +use std::net::{IpAddr, SocketAddr, ToSocketAddrs}; + pub fn gethostname() -> String { todo!() } -pub fn gethostbyname(_hostname: &str) -> String { - todo!() +// Resolves the first IPv4 address for the host name, mirroring PHP's gethostbyname +// which only ever yields an IPv4 record and returns the unmodified host name on +// failure. +pub fn gethostbyname(hostname: &str) -> String { + match (hostname, 0u16).to_socket_addrs() { + Ok(addrs) => addrs + .filter_map(|addr| match addr { + SocketAddr::V4(v4) => Some(v4.ip().to_string()), + SocketAddr::V6(_) => None, + }) + .next() + .unwrap_or_else(|| hostname.to_string()), + Err(_) => hostname.to_string(), + } } -pub fn inet_pton(_host: &str) -> Option<Vec<u8>> { - todo!() +pub fn inet_pton(host: &str) -> Option<Vec<u8>> { + match host.parse::<IpAddr>() { + Ok(IpAddr::V4(v4)) => Some(v4.octets().to_vec()), + Ok(IpAddr::V6(v6)) => Some(v6.octets().to_vec()), + Err(_) => None, + } } pub fn http_get_last_response_headers() -> Option<Vec<String>> { |
