Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions Source.Engine/Net.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,22 +141,6 @@ public static void OutOfBandPrint(NetSocketType sock, NetAddress adr, ReadOnlySp
// todo
}

public bool StringToAdr(ReadOnlySpan<char> host, int port, [NotNullWhen(true)] out IPEndPoint? ep) {
ep = null;
if (host.IsEmpty) return false;

if (host == "localhost") {
ep = new IPEndPoint(IPAddress.Loopback, port);
return true;
}
if (IPAddress.TryParse(host, out IPAddress? ip)) {
ep = new IPEndPoint(ip, port < 0 ? 0 : port);
return true;
}

return false;
}

public bool StringToAdr(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPEndPoint? ep) {
host = host.SliceNullTerminatedString();
ep = null;
Expand All @@ -167,8 +151,30 @@ public bool StringToAdr(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPEndPo
if (strncmp(address, "localhost", 10) == 0 || strncmp(address, "localhost:", 10) == 0)
memcpy(address, "127.0.0.1");

if (!IPEndPoint.TryParse(address.SliceNullTerminatedString(), out ep))
return false;
address = address.SliceNullTerminatedString();
if (!IPEndPoint.TryParse(address, out ep)) {
bool hasPort = false;
string hostname = address.ToString();
string port = "";
if (address.IndexOf(':') > -1) {
hasPort = true;

string[] split = hostname.Split(':');
hostname = split[0];
port = split[1];
}

IPAddress[] ips = Dns.GetHostAddresses(hostname);
if (ips.Length == 0)
return false;

string addr = ips[0].ToString();
if (hasPort)
addr += $":{port}";

if(!IPEndPoint.TryParse(addr, out ep))
return false;
}

if (ep.Port == 0)
ep.Port = Net.PORT_SERVER;
Expand Down
Loading