-
Notifications
You must be signed in to change notification settings - Fork 4
/
DnsUtil.cs
41 lines (33 loc) · 1.41 KB
/
DnsUtil.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// The Sisk Framework source code
// Copyright (c) 2024- PROJECT PRINCIPIUM and all Sisk contributors
//
// The code below is licensed under the MIT license as
// of the date of its publication, available at
//
// File name: DnsUtil.cs
// Repository: https://github.com/sisk-http/core
using System.Net;
using System.Net.Sockets;
using Sisk.Core.Http;
namespace Sisk.Ssl;
static class DnsUtil {
public static IPEndPoint ResolveEndpoint ( ListeningPort port, bool onlyUseIPv4 = false ) {
var hostEntry = Dns.GetHostEntry ( port.Hostname );
if (hostEntry.AddressList.Length == 0)
throw new InvalidOperationException ( $"Couldn't resolve any IP addresses for {port}." );
IPAddress? resolvedAddress;
if (onlyUseIPv4) {
resolvedAddress =
// only resolves IPv4
hostEntry.AddressList.LastOrDefault ( a => a.AddressFamily == AddressFamily.InterNetwork );
}
else {
resolvedAddress =
// try to return the last IPv6 or IPv4
hostEntry.AddressList.LastOrDefault ( a => a.AddressFamily == AddressFamily.InterNetwork || a.AddressFamily == AddressFamily.InterNetworkV6 );
}
if (resolvedAddress is null)
throw new InvalidOperationException ( $"Couldn't resolve any IP addresses for {port}." );
return new IPEndPoint ( resolvedAddress, port.Port );
}
}