I am having an issue I can't seem to sort out and after looking through the site I didn't see my exact issue so here we go.
I have a PC with multiple NIC adapters. One is set to 192.168.6.1 and the other is set to 192.168.2.90, both have the same subnet mask of 255.255.0.0. Both NICs are plugged into the same network switch (not managed, just a 32 port switch). Residing on the same network are multiple PCs with the same subnet mask but differing IP addresses in the 192.168.6.X range (192.168.6.6 , 192.168.6.7, etc)
I have an application running on the PC with multiple NIC adapters and it is binding and joining a multicast group to receive packets. The issue I am having is that the multicast packets that are being sent from one of the PCs in the system (i.e. 192.168.6.6) are not being received by my application. I can see the multicast packets in Wireshark when viewing either NIC's traffic so I believe that each NIC is getting the packets properly.
A small snippet:
{
IPEndPoint localIPEndPoint
localIPEndPoint = new IPEndPoint(IPAddress.Parse("192.168.6.1"), 16466);
receiveUDPClient = new UdpClient(localIPEndPoint);
receiveUDPClient.JoinMulticastGroup(IPAddress.Parse("239.254.2.1"));
receiveUDPClient.MulticastLoopback = false;
receiveUDPClient.DontFragment = true;
receiveUDPClient.Client.ReceiveBufferSize = 16384;
// ....
// ....
BeginReceiveAudio();
}
public void BeginReceiveAudio()
{
receiveUDPClient.BeginReceive(ReceiveCallback, receiveUDPClient);
}
I am processing the packets in a callback which I know works fine when using a single NIC -- in fact if I simply disable the 192.168.2.90 NIC adapter my application receives the packets just fine.
private void ReceiveCallback(IAsyncResult ar)
{
byte[] receivedBytes = receiveUDPClient.EndReceive(ar, ref localIPEndPoint);
// Process Data...
BeginReceiveAudio();
}
The other interesting thing is that if I swap the IP addresses of the adapter's while both enabled, my application receives the packets just fine! I am not at all familiar with how the packets get routed to each NIC; I assumed that when you bind to an IP Address and join a multicast group, any packets transmitted to that multicast group get received by the PC are routed to each NIC that is a part of that multicast group.
Any clue whats going on here? Given that it's one common network, what would be the reason that using one NIC lets my application recieve the packets but the other doesn't?
You need to join the multicast group via each available interface, so that the IGMP messages get sent out via each, so that the hosts on all those networks know there is a multicast member. If you only join once without specifying the interface, the static IP route is used to determine where the IGMP is sent, which is only one network.
The problem is you are using the line below and assuming that it is sufficient for using the correct NIC
receiveUDPClient = new UdpClient(localIPEndPoint);
The reality of the matter is that when you use that client to join the multicast address, the OS does not force the IGMP message to go out of that Endpoint. To force the IGMP out of the endpoint you desire, you need to use the two parameter method for joining the multicast address, where the second IP address is local enpoint you are wanting to listen on. Simply use the line below instead of the single parameter JoinMulticastGroup method:
receiveUDPClient.JoinMulticastGroup(IPAddress.Parse("239.254.2.1"), IPAddress.Parse("192.168.6.1"));
I wish that this wasn't necessary, since it would make sense for the all the JoinMulticastGroup methods to use the currently bound endpoint as the default localEndpoint, but it doesn't work that way.
Related
First of all I have to mention that I don't have any dedicated server in this case. But I know about the server's IP. So the problem is if the client connects to that IP, how could I know which client IP it uses? Because a client can have more than 1 network adapter, each one is connected to a different network. Also the server does not need to be in the same LAN with the client, it may require several hops before the client's packets reaching the server. We know the server's IP, how could we know the client's IP in this case?
Because we don't have dedicated server so we cannot detect the client's IP via some server app (some kind of clients manager). Also the Ping class seems not helpful in this case, also in general the server does not have to have pinging enabled. Currently the server is just a SQL Server to which the client executes some query directly. I know we can obtain the client's IP when it executes a query to my SQL Server (using sys.dm_exec_connections) but it's just the last resort for me. I would like to find another way directly from the client (and without involving the SQL Server).
Here is the closest approach I can follow but I don't really know why it chooses the last address?
var hn = System.Net.Dns.GetHostName();
var ipEntry = System.Net.Dns.GetHostEntry(hn);
IPAddress[] addr = ipEntry.AddressList;
var ip = addr[addr.Length - 1].ToString();//why the last?
Any way there are many IP addresses (not in the same LAN directly) which are not contained in the AddressList.
My problem is some kind of finding all possible network paths between the client (starting at some available network adapter on the client) and the server (with IP known beforehand). In almost cases there should be just 1 path available (so we should just find 1 client's IP). Trying sending some request from the client may be required to find the path but I currently don't have any clue on how it can be done.
You can easily do this from the client side using any available listening port on the server side (HTTP port, SSH port, or SQL Server port). Here's a small python program:
import sys
import socket
if len(sys.argv) != 3:
print("Usage: {} ip-or-hostname port-number".format(sys.argv[0]))
sys.exit(1)
hostname = sys.argv[1]
port = int(sys.argv[2])
s = socket.socket()
s.connect((hostname, port))
print(s.getsockname()[0])
What it does is to create a connection to the destination system, then uses the getsockname system call to provide the local address used in the connection. This local address will always correspond to one of your client side interfaces.
So for example, if I run it with arguments www.google.com 80, I get:
python /tmp/disc.py www.google.com 80
192.168.0.110
If I instead give it an address and port of a machine on a different local subnet, I get:
python /tmp/disc.py 192.168.40.128 22
192.168.40.131
The two addresses returned correspond to two different interfaces on my local system:
ip -4 addr
[snipped]
2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.0.110/24 brd 192.168.0.255 scope global dynamic ens33
valid_lft 166968sec preferred_lft 166968sec
3: ens34: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
inet 192.168.40.131/24 brd 192.168.40.255 scope global ens34
valid_lft forever preferred_lft forever
I'm sure you could easily implement the same program in C# -- just not a language I'm intimately familiar with.
Looks like I missed one important already built-in class to act as a client (we have HttpClient, WebClient ... but in this case I need a TcpClient). This can try connecting to some IP (with some port provided) and if it succeeds I can obtain the resolved LocalEndPoint of the underlying webSocket. It can just be converted to string but I can parse for the client IP it used (the string format is like IP:port). Here is just a few lines of code to achieve what I want:
public string GetIPConnectingTo(string targetIP, int port = 80){
try {
using(var tc = new TcpClient(targetIP, port)){
return tc.Client.LocalEndPoint.ToString().Split(':')[0];
}
}
catch {
return null;
}
}
In my program I am getting the local machine's public IP address like this
public static IPAddress getIPAddress()
{
IPAddress[] localIPs = Dns.GetHostAddresses(Dns.GetHostName());
foreach (IPAddress addr in localIPs)
{
if (addr.AddressFamily == AddressFamily.InterNetwork)
{
return addr;
}
}
return null;
}
and it worked fine where I live.
Right now I am at a friend's house, and I am connected to internet via Wi-Fi, and this code does not give me my external IP address, it has probably something to do with the router settings, but I am not very familiar with networks...
The router is TP-LINK, and I can access its settings like this
By the way, the 8080 port is exactly the one I need, I only need to be able to access my public IP. How can I do it?
You can make a request to a site like http://icanhazip.com/ to get your external IP address
var request = WebRequest.Create("http://ipv4.icanhazip.com/");
var response = request.GetResponse();
var dataStream = response.GetResponseStream();
var reader = new StreamReader (dataStream);
string myIPAddress = reader.ReadToEnd();
More info here: https://msdn.microsoft.com/en-us/library/456dfw4f%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
There is no sure-fire way to do this that will work 100% of the time. However, there are two methods that will work most of the time.
The first is to use an external STUN server. It's relatively easy to add a configuration entry to your software to allow the user to change the STUN server if this server ever changes or goes down, they can choose another one.. there are many of them out there. A STUN server returns the users IP address back to the caller and is used by most VOIP devices.
The second is to use the built-in Universal Plug-n-Play framework (UPnP) to ask the router for its IP Address. This one, however, depends on the router supporting UPnP and it not being disabled. Additionally, UPnP may not work within a corporate network as there are several layers of routers and firewalls usually. Still, for home users this is typically a good option. There is a .NET based UPnP library here that utilizes the built-in COM based UPnP components in Windows:
http://managedupnp.codeplex.com/
I don't have any examples of how to implement this, but it supposedly has a good documentation library.
What you want is not possible. A router is a proxy of sorts, whereby all traffic must pass through it. The external IP address for all internal devices (including PCs, laptops, tablets, etc), will be the same--the internal IP address what is different. The only way for a device to know its external IP address is either to query the router (which might not be possible), or to query an external source. The port forwarding page you have in your post only shows the router where to redirect incoming traffic on that port, but it will tell the PC nothing as to what its external IP address is--because, generally, to the PC the external IP address is irrelevant.
I am developing a system that is sending UDP packets using LWIP on Nios processor. I have developed a C# application to allow visualization of the received data.
The issue that I am having is on receiving data on the C# application when sending to multicast addresses. On the com+uter running the C# app I am able to visualized the incoming packets addressed for IP 225.0.0.1(multicast address) but my C# app does not receive them.
The C# app receives data sent to a network address, for example 192.168.0.100 or when data is sent to 255.255.255.255 (in this case I can run the app in two diferrent computers and both receive the same data).
I have read several answers here on the forum and tried them all.
The code that I am using currently is:
UdpClient udpClientImage;
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 8884);
udpClientImage = new UdpClient(RemoteIpEndPoint);
udpClientImage.EnableBroadcast = true;
IPAddress m_GrpAddr;
m_GrpAddr = IPAddress.Parse("225.0.0.1");
udpClientImage.JoinMulticastGroup(m_GrpAddr);
while (true)
{
Byte[] receiveBytes = udpClientImage.Receive(ref RemoteIpEndPoint);
senderIPAddress = RemoteIpEndPoint.Address;
string returnData = Encoding.ASCII.GetString(receiveBytes);
}
Am I missing something in order to receive the multicast addresses?
Any help would be welcome,
Your UdpClient has to join the multicast group to listen. It's not automatic.
udpClientImage.JoinMulticastGroup(multicastAddress);
See MSDN for more information about this method.
The address passed into the constructor is the NIC(s) that you are listening on. The address passed into the Receive method is a filter and gets updated to reflect the source of the message. I don't think you want to reuse that one, and I don't think it should be the same as the bound NIC.
Try disabling rp_filter on the receiving system
You can check out this post:
UDP multicast client does not see UDP multicast traffic generated by tcpreplay
Writing a chat program (as so many do) and i have found that i would like to be able to get the clients to connect to the server automatically.
However, the IP address of the server would not be permanent, so i cannot just hard-core it into the program
In TCP, I'm looking for some sort of broadcast feature, that allows the client to know where the server is
Any ideas?
EDIT: should have said, this will be a LAN program only - no outside connections
If you are talking about a chat in a LAN and you can't or don't want to use DNS for some reason, you could implement, or find an implementation of, the discovery protocol used by UPnP. The SSDP is based on a UDP broadcast. It is, afaik, not possible to multicast via TCP, because TCP needs a session.
If you want to use the chat server over the internet you have no choice but to use DNS. Look for a dynamic dns provider (I use selfhost.bz). In C# you can then resolve the hostname to an IP address as described in the other answers. If you have a hostname to connect to it will probably be enough to pass that to the socket, though:
socket.Connect("myhostname.selfhost.bz", ...
Edit: Since you say you're in a LAN, a few more details on SSDP. The protocol does way more, than you actually need. If you're thinking of implementing it yourself, don't stick to it exactly. Just make your clients send a broadcast on a specified port. The server permanently listens on that port, answering with a predefined message, once it receives a message. When the client receives that answer, it will know that the sender is a valid server.
Use DNS. Resolve the hostname in your app and connect to the IP it resolves to. You'll need dynamic DNS since you say the IP isn't permanent.
Use the below process to find server IP address
public string GetIPAddress()
{
string strHostName = System.Net.Dns.GetHostName();
IPHostEntry ipHostInfo = Dns.Resolve(Dns.GetHostName());
IPAddress ipAddress = ipHostInfo.AddressList[0];
return ipAddress.ToString();
}
You can also use
Request.ServerVariables["LOCAL_ADDR"];
I had an idea: just get the server to write the IP address/port/whatever to a textfile somewhere on the (public) network, and the clients can read the text file
Obviously, if the text file is not there or empty, no server is running...
Is this such a bad idea?
I am using TcpClient to listen on a port for requests. When the requests come in from the client I want to know the client ip making the request.
I've tried:
Console.WriteLine(tcpClient.Client.RemoteEndPoint.ToString());
Console.WriteLine(tcpClient.Client.LocalEndPoint.ToString());
var networkStream = tcpClient.GetStream();
var pi = networkStream.GetType().GetProperty("Socket", BindingFlags.NonPublic | BindingFlags.Instance);
var socketIp = ((Socket)pi.GetValue(networkStream, null)).RemoteEndPoint.ToString();
Console.WriteLine(socketIp);
All of these addresses output 10.x.x.x addresses which are private addresses and are clearly not the address of the clients off my network making the requests. What can I do to get the public ip of the clients making the requests?
Edit:
We are using an Amazon EC2 Load Balancer with tcp forwarding. Is there a way to get the true client ip in this set up?
Does this work:
((IPEndPoint)tcpClient.Client.RemoteEndPoint).Address.ToString()
If the client is connecting to you via an internal network I am not sure you can get their public IP since the connection to get back to the client would not need that information.
It sounds like perhaps your server is behind a load balancer or router using NAT. In this case, the IP packet won't have the originating client's address, but the address of the NAT router. Only the NAT router knows the sender's address (on an IP level).
Depending on whatever higher-level protocol you might be using on top of TCP, you may be able to get client identification from that, although it's much easier to spoof such information at higher levels, if that may be a concern.
If you need this data only for research purposes, your NAT device may keep a log.
If it's a requirement that you get the true originating IP packet in real time, you may have to have to reconfigure your router or have your server moved to the DMZ, but that's a whole nother ball of wax. Talk to your network guys, as they would certainly know more about this than I (I'm not a network expert).
Simply use the connection socket object of Socket class which you have used to accept the client.
connectionSocket.RemoteEndPoint.toString();
AdresseIP = DirectCast(SocketClient.Client.RemoteEndPoint, IPEndPoint).Address.ToString