namespace ZB.MOM.WW.Auth.Ldap.Internal;
/// A single LDAP server endpoint resolved from configuration.
internal sealed record LdapEndpoint(string Host, int Port)
{
/// Renders the endpoint as host:port — the form used in failover log messages.
public override string ToString() => $"{Host}:{Port}";
}
///
/// Parses FallbackServers entries: "host" (adopts the caller's default port)
/// or "host:port". IPv6 literals are not supported — use a DNS name.
///
internal static class LdapEndpointParser
{
///
/// Attempts to parse a single FallbackServers entry.
///
/// The configured entry, "host" or "host:port". Surrounding whitespace is tolerated.
/// Port adopted when the entry carries no explicit one.
/// The parsed endpoint on success; undefined otherwise.
/// A human-readable reason on failure; empty on success.
/// when the entry parsed.
public static bool TryParse(string entry, int defaultPort, out LdapEndpoint endpoint, out string error)
{
endpoint = null!;
error = "";
if (string.IsNullOrWhiteSpace(entry))
{
error = "entry is empty or whitespace";
return false;
}
var parts = entry.Trim().Split(':');
if (parts.Length > 2)
{
error = $"entry \"{entry.Trim()}\" contains more than one ':' " +
"(IPv6 literals are not supported — use a DNS name)";
return false;
}
var host = parts[0].Trim();
if (host.Length == 0)
{
error = $"entry \"{entry.Trim()}\" has an empty host";
return false;
}
var port = defaultPort;
if (parts.Length == 2 && (!int.TryParse(parts[1].Trim(), out port) || port is < 1 or > 65535))
{
error = $"entry \"{entry.Trim()}\" has an invalid port (must be 1–65535)";
return false;
}
endpoint = new LdapEndpoint(host, port);
return true;
}
}