I'm creating an application for Minecraft Classic to send "Heartbeats" to an external website and my application wants to send IPv6 heartbeats rather than IPv4 heartbeats if both IPv6 and IPv4 are present on the system.
Here's what I've tried (through looking through Google search):
Ipv6Element DisableIPv6 = null;
DisableIPv6.enabled = false;
The issue here is that when I add the System.Net.Configuration import, my other portions of code from a .dll won't work anymore because I've attempted to use this method of disabling IPv6.
Here's an example of the .cs file where the heartbeat is sent to the external website (Either Minecraft.net or ClassiCube.net. Since Minecraft.net only supports IPv4 for its Classic servers, the software works fine, but since ClassiCube accepts both 4 and 6, if the machine the server is running has both, it will only take 6, but my software doesn't support IPv6 yet. The .cs file is here. (the Pastebin link expires in 2 weeks)
I've been trying to disable IPv6 if this is the case, but now that I realize that IPv6 will someday replace IPv4, I know I need to support IPv6 now before it is too late
I want to be able to support IPv6 in the application. How can I fix my code to support both IPv4 and 6?
Disclaimer
This code, in fact, is Visual C#, not java. The software has been created in Visual Studio 2013. This program is used to host Minecraft Classic Servers, (Not Premium). Many people don't believe the program was writen in C#, but I'm going to state that now so there is no confusion down the road.
Under the salt initialization:
// Dns lookup, to make sure that IPv4 is preferred for heartbeats
static readonly Dictionary<string, IPAddress> TargetAddresses = new Dictionary<string, IPAddress>();
static DateTime nextDnsLookup = DateTime.MinValue;
static readonly TimeSpan DnsRefreshInterval = TimeSpan.FromMinutes(30);
static IPAddress RefreshTargetAddress([NotNull] Uri requestUri)
{
if (requestUri == null) throw new ArgumentNullException("requestUri");
string hostName = requestUri.Host.ToLowerInvariant();
IPAddress targetAddress;
if (!TargetAddresses.TryGetValue(hostName, out targetAddress) || DateTime.UtcNow >= nextDnsLookup)
{
try
{
// Perform a DNS lookup on given host. Throws SocketException if no host found.
IPAddress[] allAddresses = Dns.GetHostAddresses(requestUri.Host);
// Find a suitable IPv4 address. Throws InvalidOperationException if none found.
targetAddress = allAddresses.First(ip => ip.AddressFamily == AddressFamily.InterNetwork);
}
catch (SocketException ex)
{
Logger.Log(LogType.Error,
"Heartbeat.RefreshTargetAddress: Error looking up heartbeat server URLs: {0}",
ex);
}
catch (InvalidOperationException)
{
Logger.Log(LogType.Warning,
"Heartbeat.RefreshTargetAddress: {0} does not have an IPv4 address!", requestUri.Host);
}
TargetAddresses[hostName] = targetAddress;
nextDnsLookup = DateTime.UtcNow + DnsRefreshInterval;
}
return targetAddress;
}
And then, within static HttpWebRequest CreateRequest, under request.UserAgent, insert
if (uri.Scheme == "http")
{
request.Proxy = new WebProxy("http://" + RefreshTargetAddress(uri) + ":" + uri.Port);
}
That should work, I have no way of testing it. Hopefully you or someone else has ipv6 so they can test. All of this code was basically taken from fCraft by the way, all credit to them. http://www.fcraft.net
Related
I am trying to resolve an Ip address in Windows 7.
The DNS server im trying to reach is IPV4 only - therefore I needed to disable IPV6 in the network card and in the registry.
I am trying to use DNS.BeginGetHostEntry(hostname) because I need the ip.
when it reaches DNS.EndGetHostEntry it throws the above exception.
Weird thing is - I can see the Ip address I need in Wireshark as a response from the server - but in my code it throws the exception.
by the way - on WinXP the same code works flawlessly.
any help would be appreciated.
private IpAddress GetAddress(string name)
{
var result = Dns.BeginGetHostEntry(name,null,null);
if (!reuslt.AsyncwaitHandle.WaitOne(TimeSpan.FromSeconds(7)))
return null;
try
{
var addresses = Dns.EndGetHostEntry(result);
return addresses.AddressList[addresses.AddressList - 1];
}
catch (Exception e)
{
return null;
}
}
we have already tried all the functions in Dns like GetHostEntry,GetHostAddresses,GetHostByName,Resolve, etc...
Anyone know if it can recover the ( hostname/machine name ) with a local IP address ? I know that the resolution reverse 'hostname -> ip' works perfectly with DeviceNetworkInformation.ResolveHostNameAsync (endpoint, OnNameResolved, null).
Is it possible to do the same thing but in reverse? Given the IP of a Local Machine and retrieve its hostname? A thousand thanks
Here is my code , but it's doesn't work the host is always called "192.168.1.5" like the ip . And it's should return "computer0001"
DeviceNetworkInformation.ResolveHostNameAsync("192.168.1.5", OnNameResolved, null);
private void OnNameResolved(NameResolutionResult result)
{
IPEndPoint[] endpoints = result.IPEndPoints;
if (endpoints != null)
{
if (endpoints.Length > 0)
{
//Host always return ip adress and not the machine name
host = endpoints[0].ToString();
}
}
}
Not sure if there is specific API on Windows Phone for that purpose.
I suggest you looking into similar question 'DNS lookup from custom DNS server in c#'.
If you're okay with going only WP8, then you should study this part of MSDN - Supported Win32 APIs for Windows Phone 8.
For example, getpeername function might return you information you're looking for. Or you might create new HostName object passing IP address there and trying to read DisplayName, CanonicalName or RawName properties of it.
Make sure you test with both emulator and real device as WP8 emulator has a little bit complex network configuration.
Would be great if you can update us if you have any success with the task.
Using C# Winforms I am trying to automatically detect the local machines IP address through which it can connect to a particular remote DNS/IP address.
One senario is running over a VPN, with the remote address being 10.8.0.1 and local address being 10.8.0.6, netmask of 255.255.255.252
Iterating through the local addresses and checking if the remote and local are on the same subnet obviously fails and I am unsure of how else to do this.
Here is some sample code that should get you the information you're looking for. It creates a UDP socket and calls Connect() on it (effectively a NOOP), and then checks the local address.
static EndPoint GetLocalEndPointFor(IPAddress remote)
{
using (Socket s = new Socket(remote.AddressFamily,
SocketType.Dgram,
ProtocolType.IP))
{
// Just picked a random port, you could make this application
// specific if you want, but I don't think it really matters
s.Connect(new IPEndPoint(remote, 35353));
return s.LocalEndPoint;
}
}
static void Main(string[] args)
{
IPAddress remoteAddress = IPAddress.Parse("10.8.0.1");
IPEndPoint localEndPoint = GetLocalEndPointFor(remoteAddress) as IPEndPoint;
if (localEndPoint == null)
Console.WriteLine("Couldn't find local address");
else
Console.WriteLine(localEndPoint.Address);
Console.ReadKey();
}
Note that this is effectively an implementation of this answer, but in C#.
The routing table determines which local port to use. I don't know of a way from C# to get it other than to run the route print CLI command. If there is a network match it uses that port, otherwise it uses the default route.
http://www.csharp-examples.net/local-ip/
Give that a shot.
Hey all. I have written a program that sequentially scans certain parts of a LAN for computers (code will be provided). However, when I run this code, it only returns the DNS HostName of the computer it is running on. I looked into using WMI, but I cannot, as I will not always have priveleges to the computers being found. Is there any other way to find a local computers HostName?
using System;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
namespace CheckLocalNetwork
{
class PingCheck
{
public static string fullip;
public void CheckSequentialIP()
{
IPHostEntry IpEntry = Dns.GetHostEntry(fullip);
Ping pingSender = new Ping();
PingOptions options = new PingOptions();
options.DontFragment = true;
string data = "a";
byte[] buffer = Encoding.ASCII.GetBytes(data);
int timeout = 120;
PingReply reply = pingSender.Send(fullip, timeout, buffer, options);
if (reply.Status == IPStatus.Success)
{
Console.WriteLine("Address: {0}", reply.Address.ToString());
Console.WriteLine("Host Name: {0}", IpEntry.HostName);
Console.WriteLine("RoundTrip time: {0}", reply.RoundtripTime);
Console.WriteLine("Time to live: {0}", reply.Options.Ttl);
Console.WriteLine("Don't fragment: {0}", reply.Options.DontFragment);
Console.WriteLine("Buffer size: {0}", reply.Buffer.Length);
Console.WriteLine("");
}
}
static void Main(string[] args)
{
Console.WriteLine("Press enter to search for ip adresses that begin with 192.168.1");
Console.ReadLine();
for (int endofip = 1; endofip < 101; endofip++)
{
fullip = "192.168.1." + Convert.ToString(endofip);
PingCheck checkfullip = new PingCheck();
checkfullip.CheckSequentialIP();
}
Console.ReadLine();
}
All help is much appreciated.
Hmm - your code sample behaves as expected on my machine - i.e. it returns the hostname of the machine being scanned.
To investigate your problem deeper, have you tried using nslookup to check the ip addresses resolve?
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
C:\Users\Rob>nslookup <-- type this at a command prompt
Default Server: mydns.mydomain.co.uk <--- these two lines indicate the dns server being used to resolve your queries
Address: 192.168.0.1 <----|
> 192.168.0.5 <---- type in the ip address of one of the machines in question
Server: mydns.mydomain.co.uk
Address: 192.168.0.1
Name: myworkstation.mydomain.co.uk <---- this is the hostname, as reported by the DNS using a reverse lookup
Address: 192.168.0.5
If this doesn't return the machine name, then you may have a name resolution issue that is not related to your code.
If this all looks ok, then it might also be worth enumerating the IpEntry.Aliases collection. Are there any entries here, and do they make sense?
Finally - is the code you have above exactly the code that is going wrong for you, or is it a "distilled" example? The reason I ask is that the documentation for Dns.GetHostEntry states
"When an empty string is passed as the
host name, this method returns the
IPv4 addresses of the local host."
I also notice you're holding "fullip" in a static. If this is not the exact code that is causing the problem, especially if this runs multithreaded, is there a chance you are not initialising "fullip" before the Dns.GetHostEntry is called?
I may be way off, but I thought is was worth giving a brain dump of what occured to me as I looked at your problem :)
[EDIT:] - your comment to kdt has clarified something I misunderstood. I thought you were saying you always got back the hostname for your local machine, no matter which machine you "scanned" - which is very odd behaviour. In fact I think you are saying you just get back IP addresses for other machines (their IP address), and only get a hostname for your local. Disregard my last bit about the threading and the empty argument.
This is far more easily explained - your machine is almost certainly just not able to resolve the machine names - I expect my nslookup test I suggested will not return the machine names either.
In order to resolve these IP's to host names, your machine needs a DNS that has entries for these machines, or to have them in its local hosts file; your machine isn't actually asking the remote machine for its name when you do this call so it won;t be able to find it out without help from one of its usual name resolution paths.
It works for me, because my local DNS really does have entries for all the machines on my network, resolving their host names to ip addresses and vice-versa.
I'm trying to send a WOL package on all interfaces in order to wake up the gateway(which is the DHCP server, so the machine won't have an IP yet).
And it seems that I can only bind sockets to IP and port pairs...
So the question is: How can a create a socket(or something else) that is bound to a NIC that has no IP?
(Any languge is ok. c# is prefered)
#ctacke: I know that WOL is done by MAC address... My problem is that windows only sends UDP broadcasts on the NIC what Windows considers to be the primary NIC (which is not even the NIC with the default route on my Vista machine). And I can not seems to find a way to bind a socket to an interface which has no IP address. (like DHCP clients do this)
#Arnout: Why not? The clients know the MAC address of the gateway. I just want a send a WOL packet like a DHCP client does initially...(DHCP discover packets claim to come from 0.0.0.0) I don't mind if I have to construct the whole packet byte by byte...
It seems that I have found a solution. One can use winpcap to inject packets to any interface.
And there is good wrapper for .net: http://www.tamirgal.com/home/dev.aspx?Item=SharpPcap
(I would have prefered a solution which requires no extra libraries to be installed...)
UPDATE: Here is what I came up for sending a WOL packet on all interfaces:
//You need SharpPcap for this to work
private void WakeFunction(string MAC_ADDRESS)
{
/* Retrieve the device list */
Tamir.IPLib.PcapDeviceList devices = Tamir.IPLib.SharpPcap.GetAllDevices();
/*If no device exists, print error */
if (devices.Count < 1)
{
Console.WriteLine("No device found on this machine");
return;
}
foreach (NetworkDevice device in devices)
{
//Open the device
device.PcapOpen();
//A magic packet is a broadcast frame containing anywhere within its payload: 6 bytes of ones
//(resulting in hexadecimal FF FF FF FF FF FF), followed by sixteen repetitions
byte[] bytes = new byte[120];
int counter = 0;
for (int y = 0; y < 6; y++)
bytes[counter++] = 0xFF;
//now repeat MAC 16 times
for (int y = 0; y < 16; y++)
{
int i = 0;
for (int z = 0; z < 6; z++)
{
bytes[counter++] =
byte.Parse(MAC_ADDRESS.Substring(i, 2),
NumberStyles.HexNumber);
i += 2;
}
}
byte[] etherheader = new byte[54];//If you say so...
var myPacket = new Tamir.IPLib.Packets.UDPPacket(EthernetFields_Fields.ETH_HEADER_LEN, etherheader);
//Ethernet
myPacket.DestinationHwAddress = "FFFFFFFFFFFFF";//it's buggy if you don't have lots of "F"s... (I don't really understand it...)
try { myPacket.SourceHwAddress = device.MacAddress; }
catch { myPacket.SourceHwAddress = "0ABCDEF"; }//whatever
myPacket.EthernetProtocol = EthernetProtocols_Fields.IP;
//IP
myPacket.DestinationAddress = "255.255.255.255";
try { myPacket.SourceAddress = device.IpAddress; }
catch { myPacket.SourceAddress = "0.0.0.0"; }
myPacket.IPProtocol = IPProtocols_Fields.UDP;
myPacket.TimeToLive = 50;
myPacket.Id = 100;
myPacket.Version = 4;
myPacket.IPTotalLength = bytes.Length - EthernetFields_Fields.ETH_HEADER_LEN; //Set the correct IP length
myPacket.IPHeaderLength = IPFields_Fields.IP_HEADER_LEN;
//UDP
myPacket.SourcePort = 9;
myPacket.DestinationPort = 9;
myPacket.UDPLength = UDPFields_Fields.UDP_HEADER_LEN;
myPacket.UDPData = bytes;
myPacket.ComputeIPChecksum();
myPacket.ComputeUDPChecksum();
try
{
//Send the packet out the network device
device.PcapSendPacket(myPacket);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
device.PcapClose();
}
}
WOL is a very flexible protocol that can be implemented in multiple different ways.
The most common are:
Sending a WOL as the payload of an ethernet packet.
Sending a WOL as the payload of a UDP packet (for routing across the net).
Once it lands on the local network it's passes to all the hosts on the network using the broadcast MAC address.
For an Ethernet packet the structure is:
Destination MAC: FF:FF:FF:FF:FF:FF (Broadcast)
A Magic Packet Payload
For a UDP packet the structure is:
Destination MAC: FF:FF:FF:FF:FF:FF (Broadcast)
UDP Port: 9
A Magic Packet Payload
The Magic Payload consists of:
The Synchronization Stream: FFFFFFFFFFFF (that's 6 pairs or 6 bytes of FF)
16 copies of the MAC of the computer you're signaling to WOL
An optional passphrase of 0, 4, or 6 bytes.
To receive WOL packets from the internet (through a firewall/router):
Configure router port 9 to forward to IP 255.255.255.255 (Broadcast IP)
Set the destination IP: The external IP of the router
Note: This can only be achieved using the UDP example because Ethernet packets lack the IP layer necessary for the packet to be routed through the internet. IE, Ethernet packets are the local-network-only option. The issue with sending WOL packets over UDP is security because you have to set the router to enable IP broadcasting (255.255.255.255). Enabling broadcasting over IP is usually considered a bad idea because of the added risk of internal attack within the network (Ping flooding, cache spoofing, etc...).
For more info on the protocol including a sample capture see this site.
If you want a quick-and-dirty command line tool that generates WOL packets (and you're running on a debian, linux mint, or Ubuntu) you can install a tool that already does this.
Just install using the command line with:
sudo apt-get install wakeonlan
Update:
Here's a working example that generates a WakeOnLan packet using the current version of SharpPcap.
using System;
using System.Collections.Generic;
using System.Net.NetworkInformation;
using PacketDotNet;
using SharpPcap;
namespace SharpPcap.Test.Example9
{
public class DumpTCP
{
public static void Main(string[] args)
{
// Print SharpPcap version
string ver = SharpPcap.Version.VersionString;
Console.WriteLine("SharpPcap {0}, Example9.SendPacket.cs\n", ver);
// Retrieve the device list
var devices = CaptureDeviceList.Instance;
// If no devices were found print an error
if(devices.Count < 1)
{
Console.WriteLine("No devices were found on this machine");
return;
}
Console.WriteLine("The following devices are available on this machine:");
Console.WriteLine("----------------------------------------------------");
Console.WriteLine();
int i = 0;
// Print out the available devices
foreach(var dev in devices)
{
Console.WriteLine("{0}) {1}",i,dev.Description);
i++;
}
Console.WriteLine();
Console.Write("-- Please choose a device to send a packet on: ");
i = int.Parse( Console.ReadLine() );
var device = devices[i];
Console.Write("What MAC address are you sending the WOL packet to: ");
string response = Console.ReadLine().ToLower().Replace(":", "-");
//Open the device
device.Open();
EthernetPacket ethernet = new EthernetPacket(PhysicalAddress.Parse(
"ff-ff-ff-ff-ff-ff"), PhysicalAddress.Parse("ff-ff-ff-ff-ff-ff"),
EthernetPacketType.WakeOnLan);
ethernet.PayloadPacket = new WakeOnLanPacket(
PhysicalAddress.Parse(response));
byte[] bytes = ethernet.BytesHighPerformance.Bytes;
try
{
//Send the packet out the network device
device.SendPacket(bytes);
Console.WriteLine("-- Packet sent successfuly.");
}
catch(Exception e)
{
Console.WriteLine("-- "+ e.Message );
}
//Close the pcap device
device.Close();
Console.WriteLine("-- Device closed.");
Console.Write("Hit 'Enter' to exit...");
Console.ReadLine();
}
}
}
Note: This is a fully functional Wake-On-Lan packet sending console application built on the Example09 that can be found in the SharpPcap source.
The libraries used in this example that can't be found in the .NET framework are:
using PacketDotNet;
This library (.dll) comes packaged with SharpPcap. It is responsible for all of the packet construction and parsing within SharpPcap. This is where the WakeOnLan class resides.
Note: The packet construction/parsing code was originally bundled within SharpPcap.dll. It was migrated to its own lib because SharpPcap is meant to be a wrapper for winpcap. Many of its users deal with designing protocols and/or handling raw networking packets.
using SharpPcap;
SharpPcap contains all of the winpcap(windows)/libpcap(*nix) wrapper code. It's needed to select the interface and send the actual packets across the wire.
WOL is done by MAC, not IP. Here's an example.
.NET operates as a virtual machine (the CLR), so it abstracts away much of the underlying real machine. For example, it only provides interfaces for TCP and UDP networking, which is much higher in the network protocol stack that what you are discussing. You might be able to find a third-party component that provides access to a lower-level interface, but I would not count on it (I have looked in the past for .NET and Java).
For access to that low in the network protocol stack, you probably will need to code in C to the relevant OS system calls. You may find this easiest in Python, and you may find this functionality already implemented in Python's or third-party libraries. For example, I suggest taking a look at the Twisted networking libraries. That was one of the reasons that I switched to Python for much of my work.
Best wishes.