Skip to content

Instantly share code, notes, and snippets.

@jstedfast
Created September 21, 2022 14:49
Show Gist options
  • Save jstedfast/3367b5a7199f266fe80866b698708ecf to your computer and use it in GitHub Desktop.
Save jstedfast/3367b5a7199f266fe80866b698708ecf to your computer and use it in GitHub Desktop.
using System;
using System.Net;
using System.Net.Sockets;
using System.Net.NetworkInformation;
namespace GetFQDN
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World! Join me on an adventure to try and find the FQDN of your current machine.");
Console.WriteLine("Dns.GetHostName: {0}", Dns.GetHostName());
Console.WriteLine("Dns.GetHostEntry: {0}", GetFQDNViaDnsGetHostEntry());
Console.WriteLine("IPGlobalProperties: {0}", GetFQDNViaIPGlobalProperties());
using (var socket = new Socket(/*AddressFamily.InterNetwork,*/ SocketType.Stream, ProtocolType.Tcp)) {
socket.Connect("www.google.com", 443);
Console.WriteLine("Socket.LocalEndPoint: {0}", GetFQDNViaSocketLocalEndPoint(socket));
Console.WriteLine("NetworkInterface: {0}", GetFQDNViaNetworkInterface(socket));
socket.Disconnect(false);
}
}
static string GetFQDNViaIPGlobalProperties()
{
var properties = IPGlobalProperties.GetIPGlobalProperties();
if (!string.IsNullOrEmpty(properties.DomainName))
return properties.HostName + "." + properties.DomainName;
return properties.HostName;
}
static string GetFQDNViaDnsGetHostEntry()
{
var entry = Dns.GetHostEntry(Dns.GetHostName());
var aliases = string.Join(", ", entry.Aliases);
return $"HostName: {entry.HostName}; Aliases={aliases}";
}
static string GetFQDNViaSocketLocalEndPoint(Socket socket)
{
if (socket.LocalEndPoint is IPEndPoint ipEndPoint) {
var ipAddress = ipEndPoint.Address;
if (ipAddress.IsIPv4MappedToIPv6)
ipAddress = ipAddress.MapToIPv4();
var entry = Dns.GetHostEntry(ipAddress);
var aliases = string.Join(", ", entry.Aliases);
return $"IP: {ipAddress}; HostName: {entry.HostName}; Aliases={aliases}";
} else if (socket.LocalEndPoint is DnsEndPoint dnsEndPoint) {
return dnsEndPoint.Host;
} else {
return "Nope.";
}
}
static bool IPAddressesEqual(IPAddress ipAddress1, IPAddress ipAddress2)
{
if (ipAddress1.AddressFamily != ipAddress2.AddressFamily)
return false;
var addr1 = ipAddress1.GetAddressBytes();
var addr2 = ipAddress2.GetAddressBytes();
for (int i = 0; i < addr1.Length; i++) {
if (addr1[i] != addr2[i])
return false;
}
return true;
}
static string GetFQDNViaNetworkInterface(Socket socket)
{
var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
if (socket.LocalEndPoint is not IPEndPoint ipEndPoint)
return "Nope.";
var ipAddress = ipEndPoint.Address;
if (ipAddress.IsIPv4MappedToIPv6)
ipAddress = ipAddress.MapToIPv4();
foreach (var networkInterface in networkInterfaces) {
var properties = networkInterface.GetIPProperties();
var unicast = properties.UnicastAddresses;
var found = unicast.FirstOrDefault(x => IPAddressesEqual(x.Address, ipAddress));
if (found == null)
continue;
return Dns.GetHostName() + "." + properties.DnsSuffix;
}
return "Nope.";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment