Reputation: 1552
For a project I am writing a simple dig-like tool for sending DNS requests and processing the responses. I am using C# UdpClient to send the packet. Right now I can send to any IPv4 server just fine but when I try to specify an IPv6 DNS (2001:4860:4860:0:0:0:0:8888 or 2001:4860:4860:0:0:0:0:8844) I get errors.
My code is:
try {
var ip = "[dns ip]";
var DNSServer = IPAddress.Parse(ip);
var endpoint = new IPEndPoint(DNSServer, 53);
byte[] packet = new byte[UDP_LENGTH];
var index = createRequest(type, hostname, packet);
client.Send(packet, index, endpoint);
var result = client.Receive(ref endpoint);
ParseResponse(result);
}
catch(Exception e){
Console.WriteLine(e.ToString());
Environment.Exit(0);
}
If ip == 8.8.8.8
or any other IPv4 address it all runs as intended. If ip == 2001:4860:4860::8844
or any other IPv6 I get:
System.Net.Sockets.SocketException (10051): A socket operation was attempted to an unreachable network
at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException(SocketError error, String callerName)
at System.Net.Sockets.Socket.SendTo(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, EndPoint remoteEP)
at System.Net.Sockets.UdpClient.Send(Byte[] dgram, Int32 bytes, IPEndPoint endPoint)
at Program.queryDNS(IPAddress DNSServer, DnsType type, String hostname) in C:\...\Program.cs:line 66
Upvotes: 2
Views: 677
Reputation: 123375
A socket operation was attempted to an unreachable network
Likely you don't have IPv6 connectivity to the network you are trying to reach. This might be because you don't have IPv6 inside your local network, that you have no IPv6 gateway, that you have no public IPv6 address on your router ... . Other tools like dig should show the same behavior when called on the same machine, i.e. it is not related to your code.
Upvotes: 1