Check if IP is in LAN (behind firewalls and routers) - c#

I've been crawling in the web for about 5 hours now and couldn't find a solution for my problem:
My company is developing an educational game and I'm writing an autoupdater for it using Monotorrent. The game will be used in schools, but because most schools only have very weak internet connections there should only be one computer in the network that downloads from a httpseeder, and the others should leech from the one computer that is downloading from the httpseed.
So I get loads of IP-addresses from the tracker and need to filter out only the ones that are in the LAN.
Of course schools are sometimes quite strict with firewalls and there will be loads of routers and switches between some computers in a school.
I've already tried most solutions, things like
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (NetworkInterface iface in interfaces)
{
IPInterfaceProperties properties = iface.GetIPProperties();
foreach (UnicastIPAddressInformation address in properties.UnicastAddresses)
{
Console.WriteLine(
"{0} (Mask: {1})",
address.Address,
address.IPv4Mask
);
}
}
Or similar techniques only deliver the information of the router/switch/whatever.
So in a nutshell, what I want to do is check if a given IP is accessible via LAN.
I'd really appreciate any help because this feature is the last one remaining :)

You could take advantage of TTL. With a TTL of 1 the packet won't be able to make it to the internet:
private static bool IsLanIP(IPAddress address)
{
var ping = new Ping();
var rep = ping.Send(address, 100, new byte[] { 1 }, new PingOptions()
{
DontFragment = true,
Ttl = 1
});
return rep.Status != IPStatus.TtlExpired && rep.Status != IPStatus.TimedOut && rep.Status != IPStatus.TimeExceeded;
}
However, remember that it is called an IPv4 mask for a reason - you can use it as one (so here is your algorithmic solution):
private static bool IsLanIP(IPAddress address)
{
var interfaces = NetworkInterface.GetAllNetworkInterfaces();
foreach (var iface in interfaces)
{
var properties = iface.GetIPProperties();
foreach (var ifAddr in properties.UnicastAddresses)
{
if (ifAddr.IPv4Mask != null &&
ifAddr.Address.AddressFamily == AddressFamily.InterNetwork &&
CheckMask(ifAddr.Address, ifAddr.IPv4Mask, address))
return true;
}
}
return false;
}
private static bool CheckMask(IPAddress address, IPAddress mask, IPAddress target)
{
if (mask == null)
return false;
var ba = address.GetAddressBytes();
var bm = mask.GetAddressBytes();
var bb = target.GetAddressBytes();
if (ba.Length != bm.Length || bm.Length != bb.Length)
return false;
for (var i = 0; i < ba.Length; i++)
{
int m = bm[i];
int a = ba[i] & m;
int b = bb[i] & m;
if (a != b)
return false;
}
return true;
}

Typically any IPs like 10.x.x.x (Class A) or 192.x.x.x (Class C) can be safely assumed to be inside a private local area network. IP Classications

One thing that you could possibly use is to try and communicate between clients using multicast. Most firewalls and routers would block multicast traffic (and ISPs most definitely), meaning that you wouldn't be able to join a multicast group if no other client is on the lan. A dumb switch would pass on the traffic, a layer 3-switch might block it, or could allow it depending on configuration. Either way, if the layer 3 switch block it, you are probably on different subnets altogether anyway so all other options would fail as well.
One technology that comes to mind is SSDP ( http://en.wikipedia.org/wiki/Simple_Service_Discovery_Protocol ) which would serve your purpose pretty good I believe. That way you don't really need to figure out if you are on a LAN or not, just search for another node that is actively downloading, if you can't find one, start downloading yourself.
Since SSDP is a standard used in uPnP you would probably be able to find decent implementations you could work with.

Related

How to Connect to a DNS Server using ARSoft.Tools.Net.Core DNSClient, IPAddress and port number

I have 2 DNS Servers(domainserverwithport.com:5356 and domainserverwithoutport.com) I am connecting to for a lookup request. One has a port number but the other one doesn't have.
The one without the port number connects and gets answer from the request. But the second returns nothing.
And while using ARSoft.Tools.Net.Core, I could not see option to instantiate a new DNSClient with IP Address and Port Number. Please is there a way around this?
I did the following for the DNS Server that has no port number.
var ip = Dns.GetHostEntry("domainserverwithoutport.com");
Console.WriteLine(ip.AddressList[0]);
var newIp = ip.AddressList[0];
IPAddress iPAddress = IPAddress.Parse(newIp.ToString());
var result = EnumDnsQuery(iPAddress.ToString(), "8.7.4.3.9.5.3.1.0.9.4.3.2.e164.arpa.4.couretech.0ceed", RecordType.Naptr, 0, 5000);
And the EnumDnsQuery Method
public static EnumDnsResponse EnumDnsQuery(string serverIp, string domain,
RecordType recordType, int maximumRetries = 0, int queryTimeout = QUERY_TIMEOUT_MILLISECONDS)
{
int retriesToUse = maximumRetries > 0 ? maximumRetries : 0;
DnsMessage dnsMessage = null;
TimeSpan duration = TimeSpan.Zero;
IPAddress ipAddress = IPAddress.Parse(serverIp);
DomainName domainName = DomainName.Parse(domain);
var stopwatch = new Stopwatch();
DnsClient client = new DnsClient(ipAddress, queryTimeout);
int attempts = 0;
stopwatch.Start();
while (dnsMessage == null && attempts <= retriesToUse)
{
attempts++;
dnsMessage = client.Resolve(domainName, recordType);
}
stopwatch.Stop();
string rawMessage = string.Empty;
if (dnsMessage != null && recordType == RecordType.Naptr)
{
var naptrRecords = dnsMessage.AnswerRecords.Where(x => x.GetType() == typeof(NaptrRecord))
.Select(x => (NaptrRecord)x)
.Where(x => x.Services.Equals("E2U+pstn:tel"))
.OrderByDescending(x => x.Order);
var naptrRecord = naptrRecords.FirstOrDefault();
if (naptrRecord != null)
rawMessage = naptrRecord.RegExp.Trim('!');
}
else if (dnsMessage != null && recordType == RecordType.Txt)
{
var txtRecords = dnsMessage.AnswerRecords.Where(x => x.GetType() == typeof(TxtRecord))
.Select(x => (TxtRecord)x);
var txtRecord = txtRecords.FirstOrDefault();
if (txtRecord == null)
rawMessage = txtRecord.TextData.Trim('!');
}
return new EnumDnsResponse(
dnsMessage: dnsMessage,
rawMessage: rawMessage,
totalDuration: stopwatch.Elapsed,
attempts: attempts);
}
The code above returns a DnsMessage.
But when I do same thing for another domain server that expects a port number, DnsMessage becomes null.Please is there a way around this?
Reading the code at https://github.com/mccj/ARSoft.Tools.Net.Core/blob/master/ARSoft.Tools.Net.Core/Dns/DnsClientBase.cs you can see there is an internal constructor that takes a port number...
And here https://github.com/mccj/ARSoft.Tools.Net.Core/blob/master/ARSoft.Tools.Net.Core/Dns/DnsClient.cs on line 81 we can see an example of an accessible constructor that accesses the internal one, passing in 53 as a hard coded value (i.e. you cant set the port number in your code, if you're using the provided DnsClient)
There doesn't appear to be a way to change the port number either upon instantiation of a DnsClient or afterwards (eg via a property) and you can't subclass DnsClientBase yourself because the relevant constructor you want is internal to the other assembly and not accessible. If you're desperate to do this, you might have to clone the ARSoft.Tools.Net.Core repo yourself and add a constructor to DnsClient
It's worth noting that the port setting is client-wide; there isn't the notion of having server A on port 53 and server B on port 5353, and both servers known to the same client. The port is fixed at instantiate time and applies to all the servers in the known list for that DnsClientBase
You may also get some mileage out of writing to the package author and asking for more info on why the code is arranged thus, as it seems to make it deliberately hard to use a custom port externally but internally all the necessary guts are there to make it a variable setting

C# WMI : Adding/Removing multiple IP addresses to NIC

My application is C# .net Framework 3.5.
The main functionality of the application is:
let the user choose a network interface card (NIC)
assign to the user selected NIC an IP address (and subnet mask) - I use WMI - EnableStatic method of Win32_NetworkAdapterConfiguration class.
start through a Process a 3rd party C++ exe component, behaving like a server, which will be listening to the given IP address - the binding functionality is implemented by the server, so on Process start up I just give pass the proper IP address and it starts listening on that one.
Operation 2 and 3 can be repeated an unlimited number of times, thus it's possible to assign to the very same NIC several IP addresses and have multiple servers, each one listening to the its own IP address.
To assign the IP address to the given NIC I use WMI, in particular this code, where adapterGUID is the GUID of the user selected NIC and newSettings it's a class holding a list of IPs and subnet masks:
public static bool ChangeNetworkInterfaceIPs(string adapterGUID, IpSettings newSettings)
{
try
{
if (String.IsNullOrEmpty(adapterGUID))
throw new ArgumentException("adapterGUID");
ManagementBaseObject inPar = null;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
ManagementObject moTarget = null;
//Look for the correct network interface
foreach (ManagementObject mo in moc)
{
//find the target management object
if ((string) mo["SettingID"] == adapterGUID)
{
moTarget = mo;
break;
}
}
if (moTarget == null)
{
mc = null;
return false;
}
//we found the correct NIC. Save the current gateways, dns and wins
object winsSecondary = moTarget.GetPropertyValue("WINSSecondaryServer");
object gateways = moTarget.GetPropertyValue("DefaultIPGateway");
object dnsDomain = moTarget.GetPropertyValue("DNSDomain");
object dnsServers = moTarget.GetPropertyValue("DNSServerSearchOrder");
object winsPrimary = moTarget.GetPropertyValue("WINSPrimaryServer");
if (newSettings.DHCP)
{
inPar = moTarget.GetMethodParameters("EnableDHCP");
moTarget.InvokeMethod("EnableDHCP", inPar, null);
}
else
{
inPar = moTarget.GetMethodParameters("EnableStatic");
inPar["IPAddress"] = newSettings.Ips;
inPar["SubnetMask"] = newSettings.Netmasks;
moTarget.InvokeMethod("EnableStatic", inPar, null);
}
//restore the gateways, dns and wins
if (gateways != null && !newSettings.DHCP)
{
inPar = moTarget.GetMethodParameters("SetGateways");
inPar["DefaultIPGateway"] = gateways;
outPar = moTarget.InvokeMethod("SetGateways", inPar, null);
}
if (dnsDomain != null && !newSettings.DHCP)
{
inPar = moTarget.GetMethodParameters("SetDNSDomain");
inPar["DNSDomain"] = dnsDomain;
outPar = moTarget.InvokeMethod("SetDNSDomain", inPar, null);
}
if (dnsServers != null && !newSettings.DHCP)
{
//Do not restore DNS Servers in case of DHCP. Will be retrieved from DHCP Server
inPar = moTarget.GetMethodParameters("SetDNSServerSearchOrder");
inPar["DNSServerSearchOrder"] = dnsServers;
outPar = moTarget.InvokeMethod("SetDNSServerSearchOrder", inPar, null);
}
if (winsPrimary != null && !newSettings.DHCP)
{
inPar = moTarget.GetMethodParameters("SetWINSServer");
inPar["WINSPrimaryServer"] = winsPrimary;
if (winsSecondary != null)
{
inPar["WINSSecondaryServer"] = winsSecondary;
}
outPar = moTarget.InvokeMethod("SetWINSServer", inPar, null);
}
return true;
}
catch
{
return false;
}
}
Now, my problem comes when the user wants to kill one the active servers.
On server closure I have to remove from the NIC the IP address the server was listening to.
Killing the process it's not a problem, but when I call my ChangeNetworkInterfaceIPs to update the IP assigned to NIC (removing the one of the server no longer in use) using a new list of IP address (namely: the old list without the ip address of the killed server) something very strange happens: randomly some of other running servers get a SOCKET_ERROR and their connection it's closed.
Any idea on what's happening?
Why the running servers are randomly getting SOCKET_ERRORs when I remove an unused IP address from the NIC ?
Additionally, I know that probably setting a whole list of IP addresses just to remove one it's not really a best practice: is there a way to remove just a given IP address?
I hope the question is clear enough.
Thank you for your time.
I am not answering all the questions asked in the post, but posting this might be helpful for starters like me.
Question: Is there a way to remove just a given IP address?
Answer: run netsh command as a process in c#
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "netsh";
p.StartInfo.Arguments = "netsh interface ipv4 delete address \"my NIC Name\" addr=192.168.1.1";
p.Start();
string response = p.StandardOutput.ReadToEnd();
p.Close();
Note: this command has to be ran in administrator.

Get closest Domain Controller in current AD site without hard coding information

For instances when Active Directory takes too long to replicate data between sites, I need to ensure that the local AD replica contains the most up to date information.
How can I get a list of DomainControllers for the current site?
I haven't found anything on Codeproject or on StackOverflow
Going to all this trouble is probably wasted effort. Unless you are experiencing issues with the built in logic for finding a domain controller you should just go with the built in method that returns one. According to Microsoft it automatically tries to find the closes one: http://technet.microsoft.com/en-us/library/cc978016.aspx.
Just use the static DomainController.FindOne method and pass in your directorycontext.
Update
Alright, try the code below, let me know how it works for you. It pings each, returns the roundtrip time, if -1 (no connection) it skips it. Flags PDC status if present. Orders by PDC status, followed by ping round trip.
static void Main(string[] args)
{
var dcsInOrder = (from DomainController c in Domain.GetCurrentDomain().DomainControllers
let responseTime = Pinger(c.Name)
where responseTime >=0
let pdcStatus = c.Roles.Contains(ActiveDirectoryRole.PdcRole)
orderby pdcStatus, responseTime
select new {DC = c, ResponseTime = responseTime}
).ToList();
foreach (var dc in dcsInOrder)
{
System.Console.WriteLine(dc.DC.Name + " - " + dc.ResponseTime);
}
System.Console.ReadLine();
}
private static int Pinger(string address)
{
Ping p = new Ping();
try
{
PingReply reply = p.Send(address, 3000);
if (reply.Status == IPStatus.Success) return (int)reply.RoundtripTime;
}
catch { }
return -1;
}
First, I'll answer the question that you actually asked:
System.DirectoryServices.ActiveDirectory.ActiveDirectorySite.GetComputerSite().Servers
But it seems like you're asking how to make sure that you're talking to the closest domain controller possible. Windows doesn't exactly provide this functionality, the best it will do is give you a domain controller in the same site that the code is running from.
I think the first thing to check is that you have your sites and subnets configured correctly. Run Active Directory Sites and Services, and make sure that subnets and domain controllers are assigned to the correct sites.
This MSDN page (and the Technet article in Peter's answer) says that you must be searching by the DNS name for the DC Locator to attempt to find a DC in the current site. I don't know if the Name property of the Domain class is the DNS domain name.
I have to assume that DomainController.FindOne is a wrapper for DsGetDcName. At that link, you can find how to turn on tracing for that function. You can use this if you still have problems, or maybe you should just PInvoke this function.
Here is a code sample that has no hard coding of DCs. Comments and criticism are welcome.
/// <summary>
/// For best results ensure all hosts are pingable, and turned on.
/// </summary>
/// <returns>An ordered list of DCs with the PDCE first</returns>
static LinkedList<DomainController> GetNearbyDCs()
{
LinkedList<DomainController> preferredDCs = new LinkedList<DomainController>();
List<string> TestedDCs = new List<string>();
using (var mysite = ActiveDirectorySite.GetComputerSite())
{
using (var currentDomain = Domain.GetCurrentDomain())
{
DirectoryContext dctx = new DirectoryContext(DirectoryContextType.Domain, currentDomain.Name);
var listOfDCs = DomainController.FindAll(dctx, mysite.Name);
foreach (DomainController item in listOfDCs)
{
Console.WriteLine(item.Name );
if (IsConnected(item.IPAddress))
{
// Enumerating "Roles" will cause the object to bind to the server
ActiveDirectoryRoleCollection rollColl = item.Roles;
if (rollColl.Count > 0)
{
foreach (ActiveDirectoryRole roleItem in rollColl)
{
if (!TestedDCs.Contains(item.Name))
{
TestedDCs.Add(item.Name);
if (roleItem == ActiveDirectoryRole.PdcRole)
{
preferredDCs.AddFirst(item);
break;
}
else
{
if (preferredDCs.Count > 0)
{
var tmp = preferredDCs.First;
preferredDCs.AddBefore(tmp, item);
}
else
{
preferredDCs.AddFirst(item);
}
break;
}
}
}
}
else
{
// The DC exists but has no roles
TestedDCs.Add(item.Name);
if (preferredDCs.Count > 0)
{
var tmp = preferredDCs.First;
preferredDCs.AddBefore(tmp, item);
}
else
{
preferredDCs.AddFirst(item);
}
}
}
else
{
preferredDCs.AddLast(item);
}
}
}
}
return preferredDCs;
}
static bool IsConnected(string hostToPing)
{
string pingurl = string.Format("{0}", hostToPing);
string host = pingurl;
bool result = false;
Ping p = new Ping();
try
{
PingReply reply = p.Send(host, 3000);
if (reply.Status == IPStatus.Success)
return true;
}
catch { }
return result;
}
Here's my approach using powershell but I'm sure it's a simple implementation in c#, etc. If DHCP is setup correctly, the Primary DNS server in your subnet should be the closest Domain Controller. So the following code should grab the first DNS IP and resolve it to the hostname of the closest DC. This doesn't require RSAT or credentials and contains no specific properties of the current domain.
$NetItems = #(Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = 'True'" -ComputerName $env:COMPUTERNAME)
foreach ($objItem in $NetItems)
{
if ($objItem.{DNSServerSearchOrder}.Count -ge 1)
{
$PrimaryDNS = $objItem.DNSServerSearchOrder[0]
$domain = $objItem.DNSDomain
break
}
}
[System.Net.Dns]::GetHostbyAddress($PrimaryDNS).hostname -replace ".$($domain)",""

How do you determine which adapter is used?

I have a need to figure out which adapter is used when a connection is created. In other words, if I have multiple NIC cards (i.e. wireless, lan, etc) on my machine, which card is being used for the connection?
If anyone can point me in the right direction...
In C#
foreach(var nic in NetworkInterface.GetAllNetworkInterfaces.Where(n => n.OperationalStatus == OperationStatus.UP)
{
if(nic.GetIsNetworkAvailable())
{
//nic is attached to some form of network
}
}
VB .NET
ForEach nic in NetworkInterface.GetAllNetworkInterfaces.Where(Function(n) n.OperationalStatus = OperationStatus.UP)
If nic.GetIsNetworkAvailable() Then
//nic is attached to some form of network
End If
Next
This will only test active working Network Interfaces that are connected to an active network.
Why don't you use the MAC address?
Maybe you could map it by the MAC Address:
var nics = NetworkInterface.GetAllNetworkInterfaces();
foreach (var nic in nics)
{
if (nic.OperationalStatus == OperationalStatus.Up)
{
var mac = nic.GetPhysicalAddress().ToString();
if (mac == "your:connections:mac:address")
{
/* ... */
}
}
}
The "your:connections:mac:address" part you can figure out following this method, using the IP address of the LocalEndPoint.
How do I obtain the physical (MAC) address of an IP address using C#?
It's not beautiful, but it could work.

Wake on LAN using C#

What's the best way going forward to implement Wake on LAN using C#?
The functionality is needed for machines in a LAN environment (and not over the internet). The method needs to be robust enough to take care of firewalls and other such issues. Also, for systems not supporting this functionality, or having it disabled, is there an alternative?
The primary objective - wake up machines (from shutdown/hibernate state) over the LAN - this is to be programmed using C#.
Please guide.
PS: I've come across the following:
http://blog.memos.cz/index.php/team/2008/06/12/wake-on-lan-in-csharp
http://community.bartdesmet.net/blogs/bart/archive/2006/04/02/3858.aspx
http://www.codeproject.com/KB/IP/cswol.aspx
However, I'm new to this and hence couldn't figure if the solutions were comprehensive enough. If someone could recommend following either of the above articles, that'd help.
Very old question, I know, but still valid. Since I didn't see any C# in the accepted answer, I wrote my own 'Wake On Lan' code.
My goal was to make a universal and easy Wake On Lan class that:
works with ipv4, ipv6 and dual-stack.
works with one or multiple network cards (NICS) connected to different networks (both computers).
works with macaddress in any standard hex format.
works using multicast (broadcast is buggy in Windows when using multiple NICs and is not supported when using ipv6).
How to use:
All you need, is the MAC address of the wired nic on the computer you wish to wake up. Any standard hex representation will do. Then call the code like this:
string mac = "01-02-03-04-05-06";
await WOL.WakeOnLan(mac);
Here's the class:
using System;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public static class WOL
{
public static async Task WakeOnLan(string macAddress)
{
byte[] magicPacket = BuildMagicPacket(macAddress);
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces().Where((n) =>
n.NetworkInterfaceType != NetworkInterfaceType.Loopback && n.OperationalStatus == OperationalStatus.Up))
{
IPInterfaceProperties iPInterfaceProperties = networkInterface.GetIPProperties();
foreach (MulticastIPAddressInformation multicastIPAddressInformation in iPInterfaceProperties.MulticastAddresses)
{
IPAddress multicastIpAddress = multicastIPAddressInformation.Address;
if (multicastIpAddress.ToString().StartsWith("ff02::1%", StringComparison.OrdinalIgnoreCase)) // Ipv6: All hosts on LAN (with zone index)
{
UnicastIPAddressInformation unicastIPAddressInformation = iPInterfaceProperties.UnicastAddresses.Where((u) =>
u.Address.AddressFamily == AddressFamily.InterNetworkV6 && !u.Address.IsIPv6LinkLocal).FirstOrDefault();
if (unicastIPAddressInformation != null)
{
await SendWakeOnLan(unicastIPAddressInformation.Address, multicastIpAddress, magicPacket);
break;
}
}
else if (multicastIpAddress.ToString().Equals("224.0.0.1")) // Ipv4: All hosts on LAN
{
UnicastIPAddressInformation unicastIPAddressInformation = iPInterfaceProperties.UnicastAddresses.Where((u) =>
u.Address.AddressFamily == AddressFamily.InterNetwork && !iPInterfaceProperties.GetIPv4Properties().IsAutomaticPrivateAddressingActive).FirstOrDefault();
if (unicastIPAddressInformation != null)
{
await SendWakeOnLan(unicastIPAddressInformation.Address, multicastIpAddress, magicPacket);
break;
}
}
}
}
}
static byte[] BuildMagicPacket(string macAddress) // MacAddress in any standard HEX format
{
macAddress = Regex.Replace(macAddress, "[: -]", "");
byte[] macBytes = new byte[6];
for (int i = 0; i < 6; i++)
{
macBytes[i] = Convert.ToByte(macAddress.Substring(i * 2, 2), 16);
}
using (MemoryStream ms = new MemoryStream())
{
using (BinaryWriter bw = new BinaryWriter(ms))
{
for (int i = 0; i < 6; i++) //First 6 times 0xff
{
bw.Write((byte)0xff);
}
for (int i = 0; i < 16; i++) // then 16 times MacAddress
{
bw.Write(macBytes);
}
}
return ms.ToArray(); // 102 bytes magic packet
}
}
static async Task SendWakeOnLan(IPAddress localIpAddress, IPAddress multicastIpAddress, byte[] magicPacket)
{
using (UdpClient client = new UdpClient(new IPEndPoint(localIpAddress, 0)))
{
await client.SendAsync(magicPacket, magicPacket.Length, multicastIpAddress.ToString(), 9);
}
}
}
How it works:
The code works by enumerating all network cards that are 'up' and connected to your network (that's usually just one). It will send out the 'magic packet' to all your connected networks using multicast, which works with both ipv4 and ipv6 (don't worry about flooding your network, it's only 102 bytes).
To work, the computer, you want to wake up, must have a wired connection (wireless computers can't be woken up, since they aren't connected to any network, when they are off). The computer, that sends the packet, can be wireless connected.
Firewalls are usually no problem, since the computer is off and hence the firewall is not active.
You must make sure that 'Wake on lan' is enabled in the computer's BIOS and on the network card.
Update for .Net 6 (and a bug fix):
Fixed a bug, where if Ipv6 was functioning on the computer that sends the packet but not on the one, that should be awakened, then it would not try Ipv4 (this is fixed in the code above).
Here's the code that works on .Net 6 (borrowed some of #Oskar Sjôberg's code) - implicit usings turned on:
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text.RegularExpressions;
public static class WOL
{
public static async Task WakeOnLan(string macAddress)
{
byte[] magicPacket = BuildMagicPacket(macAddress);
foreach (NetworkInterface networkInterface in NetworkInterface.GetAllNetworkInterfaces().Where((n) =>
n.NetworkInterfaceType != NetworkInterfaceType.Loopback && n.OperationalStatus == OperationalStatus.Up))
{
IPInterfaceProperties iPInterfaceProperties = networkInterface.GetIPProperties();
foreach (MulticastIPAddressInformation multicastIPAddressInformation in iPInterfaceProperties.MulticastAddresses)
{
IPAddress multicastIpAddress = multicastIPAddressInformation.Address;
if (multicastIpAddress.ToString().StartsWith("ff02::1%", StringComparison.OrdinalIgnoreCase)) // Ipv6: All hosts on LAN (with zone index)
{
UnicastIPAddressInformation? unicastIPAddressInformation = iPInterfaceProperties.UnicastAddresses.Where((u) =>
u.Address.AddressFamily == AddressFamily.InterNetworkV6 && !u.Address.IsIPv6LinkLocal).FirstOrDefault();
if (unicastIPAddressInformation != null)
{
await SendWakeOnLan(unicastIPAddressInformation.Address, multicastIpAddress, magicPacket);
}
}
else if (multicastIpAddress.ToString().Equals("224.0.0.1")) // Ipv4: All hosts on LAN
{
UnicastIPAddressInformation? unicastIPAddressInformation = iPInterfaceProperties.UnicastAddresses.Where((u) =>
u.Address.AddressFamily == AddressFamily.InterNetwork && !iPInterfaceProperties.GetIPv4Properties().IsAutomaticPrivateAddressingActive).FirstOrDefault();
if (unicastIPAddressInformation != null)
{
await SendWakeOnLan(unicastIPAddressInformation.Address, multicastIpAddress, magicPacket);
}
}
}
}
}
static byte[] BuildMagicPacket(string macAddress) // MacAddress in any standard HEX format
{
macAddress = Regex.Replace(macAddress, "[: -]", "");
byte[] macBytes = Convert.FromHexString(macAddress);
IEnumerable<byte> header = Enumerable.Repeat((byte)0xff, 6); //First 6 times 0xff
IEnumerable<byte> data = Enumerable.Repeat(macBytes, 16).SelectMany(m => m); // then 16 times MacAddress
return header.Concat(data).ToArray();
}
static async Task SendWakeOnLan(IPAddress localIpAddress, IPAddress multicastIpAddress, byte[] magicPacket)
{
using UdpClient client = new(new IPEndPoint(localIpAddress, 0));
await client.SendAsync(magicPacket, magicPacket.Length, new IPEndPoint(multicastIpAddress, 9));
}
}
For the WOL problem you have to clarify three problems to get it to work:
Send a WOL over the ethernet cable
Configure your PC to listen for such
a packet and wake up
Make sure the packet will come from
sender to receiver (firewall,
gateways, etc.)
As you already found on the net there are existing several solutions for the first problem programmed in C# (and after skimming your links, I would start with the first one).
The second one is something you can only achieve by configuring your network adapter. Just open the device manager and take a look into the properties of your network adapter, if such an option exists and if you can enable it. This can't be programmed, due to the fact that every network adapter has another implementation of that function and how it can be enabled.
The third problem can't also be solved by C#. It is a pure network problem, where you have to configure your router, gateways, ids-systems, etc. to allow such a packet and let it flow from sender to receiver. Due to the fact, that a WOL packet is always a broadcast packet (dest-ip 255.255.255.255) it won't leave your local network and will always be dropped from router, gateways or any other bridge between to networks (e.g. vpns, etc.).
Last but not least, I will just remind you, that the first problem can be divided into some smaller packets but as far as I could see these problems are all capped by the links you provided.
I was trying Poul Bak´s answer but was unable to wake my target computer. After verifying that a third party application, WakeMeOnLan in fact was able to wake my target computer, I wrote this code that worked for me:
void SendWakeOnLan(PhysicalAddress target)
{
var header = Enumerable.Repeat(byte.MaxValue, 6);
var data = Enumerable.Repeat(target.GetAddressBytes(), 16).SelectMany(mac => mac);
var magicPacket = header.Concat(data).ToArray();
using var client = new UdpClient();
client.Send(magicPacket, magicPacket.Length, new IPEndPoint(IPAddress.Broadcast, 9));
}
Usage:
Simply by passing in the target computers mac address like so:
SendWakeOnLan(PhysicalAddress.Parse("0A-0B-0C-0D-0E-0F"));
I think the main difference between this answer and Poul Bak´s answer is that this code is using broadcast over IPv4 instead of multicast on IPv4/IPv6, and maybe my network equipment is not handling/setup to do multicast correctly.

Categories

Resources