Receiving multicast on multiple machines with the same IP address - c#

Building a bit of code that does auto configuration of IP addresses, when they start they all have the same IP, this software uses multicast to find the other devices on the network and uses that information to figure it's own IP configuration.
However if they have the same IP they stop receiving multicast packets, but if I force them to have different IPs multicast starts working again. Is multicast tied to IP address or can multiple devices with the same IP receive multicast.
We cannot use APIPA/Link-Local addressing.
Thanks,
Dan

Multicast does not mean broadcast. Multicast means to send data only to devices which have previously requested this multicast group.
You cannot use multicast to reach any previously unconfigured machines. For this you have to, use broadcast (i.e. 255.255.255.255).
Multicast relies on the IGMP protocol, which in turn relies on a working IP unicast setup, which in turn requires that all devices can be uniquely identified by their IP addresses. IGMP messages contain the unicast IP address of the sending device.
In your setup all the nifty optimization algorithms in all the network components along the path (routers, switches, also the devices themselves) will fail since they cannot identify the targets which request the multicast group. They may fail in any random way. It is completely undefined how multiple devices with the same IP address behave.
In your case some network component decides that there is no need to distribute the multicast packets to the involved ports, as there is no unique receiver connected to the port. (I am just speculating here. Any other answer will be correct, too. Just an example to explain how this could behave the way you see it.)

Related

Can udpclient be both multicast and unicast

I am trying to build a client server socket library that will abstract the complexities of sending and receiving data. I was wondering if you have a UDP server setup to have a multicast group. Can it still send and receive unicast requests to specific clients if needed? Or do you have to have to open two sockets to handle either type?
You can do multicast and unicast at the same time using the same socket.
You can send and receive any multicast and/or unicast UDP packets interleaved in anyway, so even if you want to talk to many different peers, some multicast, some unicast, you can use a single socket for this, and it fact it is often useful to do so in terms of complexity of the application.
Some things to consider:
On Linux you will want to bind() your UDP socket to your desired port and to the IP address 0.0.0.0 (INADDR_ANY). This will enable you to receive UDP packets targeted at any multicast address and any unicast address. Specifying a concrete IP address for bind() with UDP has a filtering effect and will only let UDP packets in targeted at that specific UDP address, which is rarely useful in practice, as all multicast servers usually also want to allow the same traffic to arrive as unicast, transparently.
On Windows binding to a fixed nonzero IP address has different semantics: It binds the UDP socket to a specific interface (the one with the given IP address). Sending and receiving UDP traffic for multicast/unicast should work on Windows even without binding to 0.0.0.0.

UDP multicasting with IPAddress.Any

Good day all
I am going through the MSDN website try to familiarize mystelf with Multicasting and how it works. I'm wondering: when does one use the
IPAddress.Any
it refers to a commented out line from this link
//IPAddress localIP = IPAddress.Any;
I have seen it on multiple occasions, but what does IPAddress.Any really mean?
UPDATE
A quick googling reveals:
What's IPAddress.Any - MSDN Forums:
IPAddress.Any will listen on any IP Addresses assigned to the PC. For
example, if I am connected to the network via wireless and wired,
there would be two IP Addresses assigned. This means that I would
listen for requests on both IP Addresses. If I would take the IP of
say the wired then I could only receive requests from that NIC.
As you can see, this is for specifying an address to listen on (effectively, an equivalent to IPAddress.Parse("0.0.0.0")) and thus is not specific to multicasting. See How to do a UDP multicast across the local network in c#? for an example of the latter's implementation.

Find and assign a unique IP address to a device

I need to assign a unique IP address (local) for a device we will be connecting into our system via. USB. The actually assigning I will do over RS232 (don't ask!) by telling the device what it's IP is.
I would like to somehow find a usable address relative to the local PC in order to tell the device which to use. Is there a way of enumerating or finding a usable address in C#? I don't want to simply use a fixed one in case of possible clashes with other devices.
I've done a search here and there's lots of people wanting to find the current IP, or the IP of an already existing device, but I can't find much about generating a usable, unique one.
Thanks for any assistance you can give me.
Assigning static IP addresses (even if you ping them) is a bad idea.
What happens if the IP address you've assigned is to a device that happens to be offline right now?
Use DHCP if you can, and consider prompting the user if you can not retrieve an IP address from DHCP as to what it should be assigned to.
I am not ware of a native C# library for DHCP client's, but using pinvoke will get you what you're after. Example code (not tested) here, http://www.ianatkinson.net/computing/dhcpcsharp.htm
From http://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol
The Dynamic Host Configuration Protocol (DHCP) is a network
configuration protocol for hosts on Internet Protocol (IP) networks.
Computers that are connected to IP networks must be configured before
they can communicate with other hosts. The most essential information
needed is an IP address, and a default route and routing prefix. DHCP
eliminates the manual task by a network administrator. It also
provides a central database of devices that are connected to the
network and eliminates duplicate resource assignments. In addition to
IP addresses, DHCP also provides other configuration information,
particularly the IP addresses of local Domain Name Server (DNS),
network boot servers, or other service hosts. DHCP is used for IPv4 as
well as IPv6. While both versions serve much the same purpose, the
details of the protocol for IPv4 and IPv6 are sufficiently different
that they may be considered separate protocols.[1] Hosts that do not
use DHCP for address configuration may still use it to obtain other
configuration information. Alternatively, IPv6 hosts may use stateless
address autoconfiguration. IPv4 hosts may use link-local addressing to
achieve limited local connectivity.
Ok, not really sophisticated or anything, but why don't you just try to ping the ips, starting by the lowest, and pick the first one which doesn't respond?

why cant I see UDP traffic generated by c# application in wireshark?

I am using the UDPClient class to send and receive messages on my loopback address. The executables are also interacting with each other. But why doesnt the traffic appear in wireshark?
BTW I am running windows inside parallels on OSX and can select only 1 interface that is my intel pro net network card in wireshark.
Here's the key phrase:
on my loopback address
The loopback address is a complete additional interface, not just an address. Wireshark is configured to listen on a specific interface, and I'm guessing the loopback interface is not it.
The loopback traffic is not captured by the Net Packet Filter driver.
One workaround is to send it to the IP address of your Intel Pro NIC. In my experience, this is enough to hit the NPF capture driver and show up in Wireshark. (Well, to be accurate, my experience in that matter doesn't involve a VM so YMMV).
Of course, the listener should be bound to that NIC IP to receive the packets (and not only to localhost).

TcpListener: Listen on every address, including GPRS IP address

We have a simple piece of legacy software with which we need to
communicate using TCP/IP over port 15001. We need to listen on port 15001
for the legacy software to make a connection and then read whatever it sends us.
We have tested this solution accross the internet and it works just fine.
If however we test the same solution across a GPRS TCP/IP network it does not.
All the basics have been checked, we can ping other devices in the GPRS network
and port 15001 is not blocked by any firewall.
So maybe there is something wrong with my TcpListener?
It is initialized like this:
tcpServer = new TcpListener(IPAddress.Any, TCP_PORT);
I'm assuming it listens on every available IPv4 enabled interface on the system,
because I used IPAddress.Any ?
Does anybody have any idea what the difference might be between the two networks? (Even though there shouldn't be any difference) and if there is something I need to change to my TcpListener?
You need to specify the IP address on which you want to listen, instead of IPAddress.Any. See here. When you use IPAddress.Any, it will automatically choose the network interface for you. To listen on a certain interface (in your case, GPRS) you have to use the correct IP in the constructor.
This post has more information on getting the IP address for each nic.
Also, if you're looking to listen on every IP address at once, you'll need a TcpListener for each nic.

Categories