How to Ping for IPv4 only? - c#

I know my router's IPv4 address. But when I Ping using TTL = 1 I don't get that IP. Rather, I get its IPv6 address. (I know of address.MapToIPv4() but that's only for IPv4s that were changed into IPv6s.)
So how do I ping for IPv4 only (like tracert's /4 switch)?
var reply = new Ping().Send("example.com", 10000, new byte[] { 1 }, new PingOptions(1, true));

Looking at the source code (Reference Source, GitHub), if the parameter passed to Send() is a name then Dns.GetHostAddresses() is used to resolve it and the first address returned is what's used. Thus, if that first address is an IPv6 address then that address is what will be pinged and there's no way to change that behavior.
Instead, you could call Dns.GetHostAddresses() yourself, filter the results to include or prefer IPv4 addresses, and pass that to Ping.Send():
IPAddress addressToPing = Dns.GetHostAddresses("example.com")
.First(address => address.AddressFamily == AddressFamily.InterNetwork);
using (Ping ping = new Ping())
{
PingReply reply = ping.Send(addressToPing, 10000, new byte[] { 1 }, new PingOptions(1, true));
// Do something with reply...
}

Related

Ping IPv6 Address with .Net does not work

I am trying to make a simple ping to a host in my local network:
var ipString = "::ffff:192.168.178.20";
if (IPAddress.TryParse(ipString, out var ipaddr)){
var reply = new Ping().Send(ipaddr, 5000);
return reply.Status == IPStatus.Success ? "Good":"Bad"; //<- always returning "Bad"
}
The Ip Address gets parsed, but the ping runs into the 5s timeout. The ping works when I am using the windows console ('C:\ping ::ffff:192.168.178.20').
Does anyone has an idea what could be the reason for that behaviour?

Receiving udp packet on designated network card c#

I have 3 different network cards each with their individual responsibility. Two of the cards are receiving packets from a similar device (plugged directly into each individual network card) which sends data on the same port. I need to save the packets knowing which device they came from.
Given that I am required to not specify the ip address of the devices sending me the packets, how can I listen on a given network card? I am allowed to specify a static ip address for all 3 nics if needed.
Example: nic1 = 169.254.0.27, nic2 = 169.254.0.28, nic3 = 169.254.0.29
Right now I have this receiving the data from nic1 and nic2 without knowing which device it came from.
var myClient = new UdpClient(2000) //Port is random example
var endPoint = new IPEndPoint(IPAddress.Any, 0):
while (!finished)
{
byte[] receivedBytes = myClient.Receive(ref endPoint);
doStuff(receivedBytes);
}
I can't seem to specify the static ip address of the network cards in a manner which will allow me to capture the packets from just one of the devices. How can I separate these packets with only the knowledge that they are coming in on two different network cards?
Thank you.
You're not telling the UdpClient what IP endpoint to listen on. Even if you were to replace IPAddress.Any with the endpoint of your network card, you'd still have the same problem.
If you want to tell the UdpClient to receive packets on a specific network card, you have to specify the IP address of that card in the constructor. Like so:
var listenEndpoint = new IPEndPoint(IPAddress.Parse("192.168.1.2"), 2000);
var myClient = new UdpClient(listenEndpoint);
Now, you may ask "What's the ref endPoint part for when I'm calling myClient.Receive(ref endPoint)?" That endpoint is the IP endpoint of the client. I would suggest replacing your code with something like this:
IPEndpoint clientEndpoint = null;
while (!finished)
{
var receivedBytes = myClient.Receive(ref clientEndpoint);
// clientEndpoint is no longer null - it is now populated
// with the IP address of the client that just sent you data
}
So now you have two endpoints:
listenEndpoint, passed in through the constructor, specifying the address of the network card you want to listen on.
clientEndpoint, passed in as a ref parameter to Receive(), which will be populated with the client's IP address so you know who is talking to you.
Check this out this:
foreach (NetworkInterface netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
Console.WriteLine("Name: " + netInterface.Name);
Console.WriteLine("Description: " + netInterface.Description);
Console.WriteLine("Addresses: ");
IPInterfaceProperties ipProps = netInterface.GetIPProperties();
foreach (UnicastIPAddressInformation addr in ipProps.UnicastAddresses)
{
Console.WriteLine(" " + addr.Address.ToString());
}
Console.WriteLine("");
}
Then you can choose on which address start listening.
look, if you create your IPEndPoint in the following way it must work:
IPHostEntry hostEntry = null;
// Get host related information.
hostEntry = Dns.GetHostEntry(server);
foreach(IPAddress address in hostEntry.AddressList)
{
IPEndPoint ipe = new IPEndPoint(address, port);
...
try to do not pass 0 as port but a valid port number, if you run this code and break the foreach after the first iteration you will have created only 1 IPEndPoint and you can use that one in your call to: myClient.Receive
notice that the UdpClient class has a member calledd Client which is a socket, try to explore the properties of that object as well to find out some details, I have found the code I gave you here: http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.aspx

Resolve HostName to IP

I have been through a lot of googling for this, I found a lot of examples none of which was working for me. This is a simple issue which I feel has a simple answer without defining new classes\modules etc...
My code is this :
Console.WriteLine ("Please enter an IP address or hostname");
string host = Console.ReadLine ();
***IP = resolved "host"***
Socket s = new Socket (AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
s.Connect (IP, 80);
s.close();
How do I actually resolve the IP variable?
You can simply use the DNS class to do so:
IPHostEntry hostEntry;
hostEntry= Dns.GetHostEntry(host);
//you might get more than one ip for a hostname since
//DNS supports more than one record
if (hostEntry.AddressList.Length > 0)
{
var ip = hostEntry.AddressList[0];
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.IP);
s.Connect(ip, 80);
}
string howtogeek = "www.howtogeek.com";
IPAddress[] addresslist = Dns.GetHostAddresses(howtogeek);
foreach (IPAddress theaddress in addresslist)
{
Console.WriteLine(theaddress.ToString());
}
from howtogeek
Please take the note that accepted answer can resolve to IPv6. I attempted to connect to service that does not accept IPv6 as input string.
Therefore try this snippet if you care to get IPv4:
using System.Linq;
string host = "google.com";
Dns.GetHostEntry(host).AddressList.First(addr => addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
This is the method I use to resolve a hostname to IPv4 and / or IPv6.
using System.Net:
// A host can have multiple IP addresses!
public static IPAddress[] GetIPsByName(string hostName, bool ip4Wanted, bool ip6Wanted)
{
// Check if the hostname is already an IPAddress
IPAddress outIpAddress;
if (IPAddress.TryParse(hostName, out outIpAddress) == true)
return new IPAddress[] { outIpAddress };
//<----------
IPAddress[] addresslist = Dns.GetHostAddresses(hostName);
if (addresslist == null || addresslist.Length == 0)
return new IPAddress[0];
//<----------
if (ip4Wanted && ip6Wanted)
return addresslist;
//<----------
if (ip4Wanted)
return addresslist.Where(o => o.AddressFamily == AddressFamily.InterNetwork).ToArray();
//<----------
if (ip6Wanted)
return addresslist.Where(o => o.AddressFamily == AddressFamily.InterNetworkV6).ToArray();
//<----------
return new IPAddress[0];
}
If all you want is to resolve a string that could represent either a hostname OR an IP address, you probably want to use System.Net.Dns.GetHostAddresses() rather than System.Net.Dns.GetHostEntry().
GetHostAddresses() skips the DNS lookup if the string parses to an IP address already, while GetHostEntry() will do a reverse lookup for the hostname.
https://learn.microsoft.com/en-us/dotnet/api/system.net.dns.gethostaddresses
The IpAddress has the appropriate method for parsing hostname to IpAddress.
IPAddress addr = IPAddress.Parse(hostName)

Unable to connect to the remote server when selecting IpEndPoint

I've got c# code running on a computer with multiple IP addresses, and I've got following code to select an IP address for a httpWebRequest:
class Interact
{
<data, cookies, etc>
HttpWebRequest CreateWebRequest(...)
{
.....
request.ServicePoint.BindIPEndPointDelegate = delegate(
ServicePoint servicePoint,
IPEndPoint remoteEndPoint,
int retryCount)
{
if (lastIpEndpoint!=null)
{
return lastIpEndpoint;
}
var candidates =
GetAddresses(remoteEndPoint.AddressFamily);
if (candidates==null||candidates.Count()==0)
{
throw new NotImplementedException();
}
return
lastIpEndpoint = new IPEndPoint(candidates[rnd.Next(candidates.Count())],0);
};
};
return request;
}
}
Here's the code of GetAddresses:
static IPAddress[] GetAddresses(AddressFamily af)
{
System.Net.IPHostEntry _IPHostEntry = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
return (from i in _IPHostEntry.AddressList where i.AddressFamily == af select i).ToArray();
}
This code is supposed to select a random IP from avaliable IP list, and than stick to it.
Instead, every time I send request with it, I'm getting following exception:
Unable to connect to the remote server
How do I make this work?
It looks like you are setting the port number of the end point to zero in the line:
lastIpEndpoint = new IPEndPoint(candidates[rnd.Next(candidates.Count())],0);
Unless this gets changed later on, it is unlikely that you will be able to connect to an HTTP server on port 0. You may be able to use the port contained in the remoteEndPoint, or perhaps you can hard code the port number if it is well known (eg, 80 for HTTP server running on default port).
lastIpEndpoint = new IPEndPoint(candidates[rnd.Next(candidates.Count())], remoteEndPoint.Port);

IPv4 remote address in WCF

Related to How to get the IP address of a WCF remote endpoint?
I am using this code to retrieve the remote IP address when a workflow method is invoked:
private static string GetRemoteIP()
{
var oc = OperationContext.Current;
var mp = oc.IncomingMessageProperties;
var remp = mp[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
return remp == null ? "(unknown)" : remp.Address;
}
However, the address I get back is "::1". I don't want the IPv6 address, I want the IPv4 one (127.0.0.1) - any way of forcing this?
No, I don't think so. You basically just read out a property set by the client at the time of the call. Your only option would be to instruct the client (through some config) to use IPv4 instead of IPv6 at all times (i.e. turn off IPv6 all together).
I'm not aware of any WCF setting to enforce that - you'd have to dig into the network stack and see if there's any way to make it use IPv4 addresses instead of IPv6.
You're seeing ::1 because you're connecting to the service by resolving the name "localhost" instead of addressing it as "127.0.0.1". Modern versions of Windows that have the IPv6 stack enabled will use IPv6 first.
You can disable the IPv6 stack, but that's roughly the same as making like an ostrich and sticking your head in the sand. IPv6 is here, and people are using it on their networks, so your application should be prepared to support it.
The workaround proposed by Murat will not work.
The MSDN says - if you pass the IP address to the GetHostAddresses method this address is returned in an array without querying the DNS.
To get it working you will need to query for the host name first, using GetHostEntry method. And then, using the host name, use GetHostAddresses. However, even the GetHostEntry may have the list of addresses that will be enough for you.
Here is a workaround: (You can store the values in a hashtable to avoid multiple DNS operations)
static string GetClientIP()
{
var context = OperationContext.Current;
var mp = context.IncomingMessageProperties;
var propName = RemoteEndpointMessageProperty.Name;
var prop = (RemoteEndpointMessageProperty) mp[propName];
string remoteIP = prop.Address;
if(remoteIP.IndexOf(":") > -1)
{
IPAddress[] addresses = Dns.GetHostAddresses(remoteIP);
for (int i = 0; i < addresses.Length; i++)
{
if(addresses[i].ToString().IndexOf(".")>-1)
return addresses[i].ToString();
}
return remoteIP;
}
else
{
return remoteIP;
}
}
You could use AddressFamily.InterNetwork == address.AddressFamily and AddressFamily.InterNetworkV6 as a test instead of looking for ":" or "." in the addresses strings.

Categories