How to Bind to the same UDP port on multiple interfaces - c++

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.

Related

UDP socket receives datagrams for unintended IPv6 multicast group

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?

Remote address of active UDP connections in Windows using IP Helper

The function GetUdpTable() in IP Helper returns a table of MIB_UDPROW.
MIB_UDPROW struct does not contain any information about the remote address of the UDP connection, the extended variants of GetUdpTable() only adds the pid to the return struct.
Is it possible to get the remote address for an active UDP connection using IP Helper (or any other winapi)?
No, it is not possible to get the remote port of the UDP connection unless you capture traffic and inspect the packets since UDP is a connectionless protocol.
See: Get Destination Ip/Port of active udp Connection?

How to stop behaviour: C++ Socket sendto changes interface

I run Ubuntu 12.04 and I am currently writing C++ code to create a UDP socket, which sends packets to various destinations with sendto. Now it happens that my laptop has both a wlan0 and a eth0 interface. If I bind it to either one of these, using the IP address, and the SO_BINDTODEVICE option, depending on the destination address, sendto will still decide to use the other interface if it suits him.
Specifically, if I bind a UDP socket to the eth0 interface, with its ip address and some port, and I send a packet to another laptop (locally, with only wifi access), it will decide to use my wlan0 interface.
I understand that this behaviour has pros, but I would like to be able to turn it off, i.e. I want to be able to say to the socket that it should only use the one interface I assigned it.
Suggestions?
EDIT:
struct sockaddr_storage sa = address;
fd = socket(address.get_family(), SOCK_DGRAM, 0);
char *devname = "wlan0";
setsockopt(fd, SOL_SOCKET, SO_BINDTODEVICE, devname, strlen(devname));
bind(fd, (sockaddr*)&sa, len);
Binding a socket to an interface with SO_BINDTODEVICE or bind defines a filter for received packets. When a packet has not been received using the specified interface it is not passed to the socket's receive queue. See: http://linux.die.net/man/7/socket
But binding a socket to an interface does not affect the normal IP routing process. When you send a packet it's the responsibility of the network IP stack to find the best route and to send the IP packet over a hardware adapter. This can be an Ethernet adpter but it's not controller with and bind operation.
When you want to send a packet at a specific interface you need raw sockets. You construct the complete packet content including IP and hardware layer (probably Ethernet) and send it using the raw socket.

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.