UDP socket receives datagrams for unintended IPv6 multicast group - c++

I am having a boost::asio::ip::udp::endpoint on boost::asio::ip::address_v6::any() (INADDR_ANY) and some port (let's say 20000).
Two UDP sockets now get bound to that endpoint with each subsequently joining a distingct IPv6 multicast group (let's say ff02::a and ff02::b) on all external network interfaces by setting the option boost::asio::ip::multicast::join_group multiple times for the respective multicast IPv6 and interface. Also the option boost::asio::ip::udp::socket::reuse_address(true) is getting set for both sockets.
It now appears that each of the two sockets receives UDP datagrams for both of the two multicast groups. The intended behavior is, however, that socket A only receives datagrams targeted at ff02::a and socket B only datagrams targeted at ff02::a.
My first try was to create two different endpoints with IPv6 addresses matching those of the respective IPv6 multicast group of the socket later to be bound to the respective endpoints. However, this leads to both sockets not receiving any datagrams at all.
How do achieve that socket A ony receives datagrams for ff02::a and B only for ff02::b? And why does my approach do not work?

Related

How do i receive from Two udp multicast client?

As per question, how do I recieve from two multicast client (both joining to same group of multicast, say 224.1.0.0 but each to a different port, clientA sending to port 4000 and clientB sending to port 5000) and is non-blocking
Does my server have to create two sockets and bind them to respective port (4000 and 5000) and then use select to select the port in use?
Or could i cycle through the sockets in a for loop... say i have a list of sockets and use an iterator to choose the sockets which i am to use?
Thaks

How to Bind to the same UDP port on multiple interfaces

I have two network interfaces and am trying to bind to the same UDP port on both of them but I get an error when I try to bind to the second one, EADDRINUSE. When I bind to the sockets I pass a sockaddr* where I've setup the port and the unique IP address to use.
Do I have to use the socket option SO_REUSEADDR? Will this allow messages to be received on either socket or will they go to the socket that matches the IP address their bound to?
You can bind(2) one socket to INADDR_ANY for IPv4 or to in6addr_any for IPv6 (you don't have to, but that's the usual approach). That will make that single socket able to accept packets from all network interfaces on the box.
Then SO_REUSEADDR socket option will allow you to bind other sockets to more specific addresses, i.e. to individual interfaces, and same port.
Packets will be received on the socket that is bound to the address best matching the destination IP address of a given packet.

Cpp server, UDP socket for each client

When I'm trying to bind a UDP socket on a specific ip (other than 127.0.0.1/INADDR_LOOPBACK or 0.0.0.0/INADDR_ANY) it fails.
I need to have a dedicated UDP socket for each client (point to point connection).
If I don't bind the socket and use sendto and recvfrom function, the data never arrives.
Any obvious solution ?
You can't bind UDP sockets to nonlocal addresses -- binding a UDP socket to an address means that you want packets that are directed to that address, not ones that are being sent from that address. You'll need to figure out some way to share a single socket across all clients.

udp socket with c++ and windows API

I'm writting an UDP server for a game.
Do you know if is possible to have multisocket in UDP on one port ? or I must use dynamic port (one socket = one port) ?
thanks
It make no large sense to create multiple socket on one UDP port. UDP is not point 2 point protocol like TCP so using one "server" socket bind to specific port you can handle hundreds of clients.
See Using SO_REUSEADDR...:
Using SO_REUSEADDR
The SO_REUSEADDR socket option allows a socket to forcibly bind to a
port in use by another socket. The second socket calls setsockopt with
the optname parameter set to SO_REUSEADDR and the optval parameter set
to a boolean value of TRUE before calling bind on the same port as the
original socket. Once the second socket has successfully bound, the
behavior for all sockets bound to that port is indeterminate. For
example, if all of the sockets on the same port provide TCP service,
any incoming TCP connection requests over the port cannot be
guaranteed to be handled by the correct socket — the behavior is
non-deterministic. A malicious program can use SO_REUSEADDR to
forcibly bind sockets already in use for standard network protocol
services in order to deny access to those service. No special
privileges are required to use this option.
Bonus reading: What exactly does SO_REUSEADDR do? .
Sure you can have multiple UDP sockets on one port if SO_REUSEADDR is specified via setsockopt.
However, I doubt that what you really need is using one UDP socket to communicate with multiple clients, which is also feasible. UDP is not connection-oriented, UDP APIs like sendto and recvfrom could distinguish different peers on one socket.

Connecting to a multicast server using actual IP and port of the server

I have a multicast server which multicast on an IP 233.1.2.8 on some ports
I need to connect to that server directly using its actual IP do not want to use the broadcast. I want to get actual data it sends on a specific port let say
Multicast Ip 233.1.2.8 port 32334 and 35245
Actual IP 198.122.55.191 port 35366
so i want to read the data of 35366
I can see that data in CommView.exe but can not read it if i try to connect it connects but no data
Thanks
You may want to rethink your question. IP packets have a source and destination address and port, so lets summarise:
You are looking for multicast packets with source port 35366.
Multicast address 233.1.2.8.
Destination port 32334 or 32335.
You will have to open two sockets, one listening to port 32334 and one to 32335, for each socket you will have to the multicast address 233.1.2.8. That gets you a stream of packets from any host, now you have a choice:
Use connect() to restrict to packets from 198.122.55.191.
Use recvmsg() or recvfrom() to read each packet with the source IP address and filter the stream within your application.
Use Source-Specific-Multicast (SSM) and subscribe to multicast address 233.1.2.8 from 198.122.55.191.
The last option usually requires IGMPv3 enabled network and is limited to 232.0.0.0/8 addresses.
Multicast by definition is one-way street: the server sends data to a group, and you subscribe to it.
Connecting to a server cannot be done through UDP, it requires a TCP server actually being there and listening for incoming connections on that particular port. The fact that that port is used for sending out multicast packets doesn't suggest that there's anyone listening on it as well.
If you want to read the data sent from a particular port - then you need to subscribe to all the groups, and analyze the UDP packets (and the IP frame if you have several servers transmitting, to know the origin IP) to see where they're coming from, and filter out the ones that come from the origin you fancy.
You can have a look at this site. It has an example also
http://tack.ch/multicast/