Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resolving an IP address from DNS in C# [duplicate]

Tags:

c#

ip-address

I am trying to make a TCP socket connection to an IP address. I can do this by directly parsing an IP address like this:

IPAddress ipAddress = IPAddress.Parse("192.168.1.123");
IPEndPoint remoteEP = new IPEndPoint(ipAddress, 80);
// Create a TCP/IP  socket.
Socket sender = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);  // This works!

However, I cannot figure out how to divine this IP address from a DNS string. I've tried every combination of the following:

IPAddress ipAddress = Dns.Resolve("www.mydns.org");   // No dice
IPAddress ipAddress = Dns.GetHostEntry("www.mydns.org");  // Nada
IPAddress ipAddress = IPAddress.Parse(Dns.Resolve("www.mydns.org"));   // So many errors...
IPAddress ipAddress = IPAddress.Parse(Dns.Resolve("www.mydns.org").toString());  // WTh is this attempt anyway?

Would any of you kind souls have a tip to help me squeeze an IPAddress out of a DNS?

like image 493
Nanomurf Avatar asked Oct 06 '12 02:10

Nanomurf


People also ask

How do I resolve an IP address to a URL?

To do this in Chrome, simply open up the DevTools, navigate to the Network tab and select the site's HTML doc. You should then see the IP address associated with that URL under Headers > General > Remote Address.

How is a particular name resolved in DNS?

The resolution of the domain name is done by the DNS server. Domain resolution is also called domain pointing, server settings, domain configuration, reverse IP registration, and so on. To put it simply, the easy-to-remember domain name is resolved into IP.


2 Answers

foreach (IPAddress ip in Dns.GetHostAddresses("www.mydns.org"))
{
    Console.WriteLine(ip.ToString());
}

or simply IPAddress address = Dns.GetHostAddresses("www.mydns.org")[0]; if you want the first one only.

like image 133
coolmine Avatar answered Oct 19 '22 04:10

coolmine


I've got a very neat extension method for just that!

I takes into account that an IPV6 may be returned as the first address in the list of addresses returned by the DNS class and allows you to "favor" an IPV6 or IPV4 on the result. Here is the fully documented class (only with the pertinent method for this case for reasons of brevity):

using System;
using System.Linq;
using System.Net;
using System.Net.Sockets;

/// <summary>
/// Basic helper methods around networking objects (IPAddress, IpEndPoint, Socket, etc.)
/// </summary>
public static class NetworkingExtensions
{
    /// <summary>
    /// Converts a string representing a host name or address to its <see cref="IPAddress"/> representation, 
    /// optionally opting to return a IpV6 address (defaults to IpV4)
    /// </summary>
    /// <param name="hostNameOrAddress">Host name or address to convert into an <see cref="IPAddress"/></param>
    /// <param name="favorIpV6">When <code>true</code> will return an IpV6 address whenever available, otherwise 
    /// returns an IpV4 address instead.</param>
    /// <returns>The <see cref="IPAddress"/> represented by <paramref name="hostNameOrAddress"/> in either IpV4 or
    /// IpV6 (when available) format depending on <paramref name="favorIpV6"/></returns>
    public static IPAddress ToIPAddress(this string hostNameOrAddress, bool favorIpV6=false)
    {
        var favoredFamily = favorIpV6 ? AddressFamily.InterNetworkV6 : AddressFamily.InterNetwork;
        var addrs = Dns.GetHostAddresses(hostNameOrAddress);
        return addrs.FirstOrDefault(addr => addr.AddressFamily == favoredFamily)
               ??
               addrs.FirstOrDefault();
    }
}

Don't forget to put this class inside a namespace! :-)

Now you can simply do this:

var server = "http://simpax.com.br".ToIPAddress();
var blog = "http://simpax.codax.com.br".ToIPAddress();
var google = "google.com.br".ToIPAddress();
var ipv6Google = "google.com.br".ToIPAddress(true); // if available will be an IPV6
like image 25
Loudenvier Avatar answered Oct 19 '22 05:10

Loudenvier