Skip to content

Commit 62578bc

Browse files
authored
Merge pull request #272 from wrldspawn/fix/stringtoadr-dns
Add DNS resolution to Net.StringToAdr
2 parents ff7999b + fd87821 commit 62578bc

1 file changed

Lines changed: 24 additions & 18 deletions

File tree

‎Source.Engine/Net.cs‎

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -141,22 +141,6 @@ public static void OutOfBandPrint(NetSocketType sock, NetAddress adr, ReadOnlySp
141141
// todo
142142
}
143143

144-
public bool StringToAdr(ReadOnlySpan<char> host, int port, [NotNullWhen(true)] out IPEndPoint? ep) {
145-
ep = null;
146-
if (host.IsEmpty) return false;
147-
148-
if (host == "localhost") {
149-
ep = new IPEndPoint(IPAddress.Loopback, port);
150-
return true;
151-
}
152-
if (IPAddress.TryParse(host, out IPAddress? ip)) {
153-
ep = new IPEndPoint(ip, port < 0 ? 0 : port);
154-
return true;
155-
}
156-
157-
return false;
158-
}
159-
160144
public bool StringToAdr(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPEndPoint? ep) {
161145
host = host.SliceNullTerminatedString();
162146
ep = null;
@@ -167,8 +151,30 @@ public bool StringToAdr(ReadOnlySpan<char> host, [NotNullWhen(true)] out IPEndPo
167151
if (strncmp(address, "localhost", 10) == 0 || strncmp(address, "localhost:", 10) == 0)
168152
memcpy(address, "127.0.0.1");
169153

170-
if (!IPEndPoint.TryParse(address.SliceNullTerminatedString(), out ep))
171-
return false;
154+
address = address.SliceNullTerminatedString();
155+
if (!IPEndPoint.TryParse(address, out ep)) {
156+
bool hasPort = false;
157+
string hostname = address.ToString();
158+
string port = "";
159+
if (address.IndexOf(':') > -1) {
160+
hasPort = true;
161+
162+
string[] split = hostname.Split(':');
163+
hostname = split[0];
164+
port = split[1];
165+
}
166+
167+
IPAddress[] ips = Dns.GetHostAddresses(hostname);
168+
if (ips.Length == 0)
169+
return false;
170+
171+
string addr = ips[0].ToString();
172+
if (hasPort)
173+
addr += $":{port}";
174+
175+
if(!IPEndPoint.TryParse(addr, out ep))
176+
return false;
177+
}
172178

173179
if (ep.Port == 0)
174180
ep.Port = Net.PORT_SERVER;

0 commit comments

Comments
 (0)