Sending multicast messages from specific IP address

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys!
I have several IP addresses defined on my PC (all bound to the same
interface), and i need to send multicast messages from a specific address.
Here's my code:

void SendMsg( string localAddr, int localPort, string destAddr, int
destPort, byte[] body )
{
Socket sock = new Socket( AddressFamily.InterNetwork, SocketType.Dgram,
ProtocolType.Udp );
sock.Bind( new IPEndPoint( IPAddress.Parse( localAddr ), localPort ) );
sock.SendTo( body, new IPEndPoint( IPAddress.Parse( destAddr ), destPort
) );
sock.Close();
}

The problem is - if "destAddr" contains a multicast address, than no matter
what address i pass as "localAddr" parameters, the messages are always sent
from the same address, randomly chosen from among the addresses defined on my
PC.
How can i bind my outgoing multicast messages to a particular address?
Thanks!

p.s.: i've seen suggestions to use "MulticastInterface" socket option in
order to solve this problem. However, this only affects the interface, via
which the messages are sent, but not the address (as i said, i have several
addresses defined over one interface).
 
Hello, Gal!

GS> The problem is - if "destAddr" contains a multicast address, than no
GS> matter what address i pass as "localAddr" parameters, the messages are
GS> always sent from the same address, randomly chosen from among the
GS> addresses defined on my PC.
GS> How can i bind my outgoing multicast messages to a particular address?

Are the addresses on the NIC from the same net/subnet?

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
if the subnet is the same I wonder how then network driver will differentiate with what adapter to send data?

Just a guess, try to set SocketOptionName.ExclusiveAddressUse option before calling bind?

Additionaly, you can ask on
microsoft.public.win32.programmer.networks

--
Regards, Vadym Stetsyak
www: http://vadmyst.blogspot.com
 
Thanks, Vadim.
I've tried using the ExclusiveAddressUse option, but it didn't help.
I've also performed some tests using addresses in different subnets, and it
didn't help either - the multicast messages are sent not from the address, to
which the socket is bound.
 
Hi guys!
I have several IP addresses defined on my PC (all bound to the same
interface), and i need to send multicast messages from a specific
address. Here's my code: [...]
p.s.: i've seen suggestions to use "MulticastInterface" socket option
in order to solve this problem. However, this only affects the
interface, via which the messages are sent, but not the address (as i
said, i have several addresses defined over one interface).
Works for me. There's some (apparently) dodgy information in the
archives about this, but if called how Winsock defines it, then is
works, see the definition at
http://msdn.microsoft.com/library/en-us/winsock/winsock/ipproto_ip_socket_options.asp
etc, "The input value is the 4-byte network byte order IPv4 address."

I added the following to your function, after the bind (which may itself
now be unnecessary..., but I'll leave that testing to you):

if(setMcastIf)
{
IPAddress localAddrAsIpaddr = IPAddress.Parse(localAddr);
byte []localAddrAsByteArray = localAddrAsIpaddr.GetAddressBytes();
System.Diagnostics.Debug.Assert(localAddrAsByteArray.Length==4,
"IPv4
addresses are 4 bytes long");
sock.SetSocketOption(SocketOptionLevel.IP,
SocketOptionName.MulticastInterface, localAddrAsByteArray);
}//if

My interfaces are MAC Address/IP Addresses:
xx-...-CF = 192.168.1.101, and
xx-...-B6 = { 192.0.2.5, 192.0.2.10 }, with respective route table
entries:
224.0.0.0 240.0.0.0 192.0.2.5 192.0.2.5
100
224.0.0.0 240.0.0.0 192.168.1.101 192.168.1.101
30

And I called that function with (the last parameter being the
boolean,setMcastIf, from above):
SendMsg("192.168.1.101", 1099, "239.255.255.99", 2099, hello, false);
SendMsg("192.168.1.101", 1099, "239.255.255.99", 2099, hello, true);
SendMsg("192.0.2.5", 1099, "239.255.255.99", 2099, hello, false);
SendMsg("192.0.2.10", 1099, "239.255.255.99", 2099, hello, false);
SendMsg("192.0.2.10", 1099, "239.255.255.99", 2099, hello, true);

And my pair of sniffers saw the following packets appear, with the given
source addresses:
Intf ...-CF | Intf ...-B6
-----------------------------------
192.168.1.101 | 192.168.1.101
192.168.1.101 | 192.168.1.101
(those two rows surprised me, the multicast packet is only supposed to
be sent once, however...)
-none- | 192.0.2.5
-none- | 192.0.2.10
-none- | 192.0.2.10

So, clearly that shows IP_MULTICAST_IF works from .NET, if called
correctly. :-)
 
And my pair of sniffers saw the following packets appear, with the
given source addresses:
Intf ...-CF | Intf ...-B6
-----------------------------------
192.168.1.101 | 192.168.1.101
192.168.1.101 | 192.168.1.101
(those two rows surprised me, the multicast packet is only supposed to
be sent once, however...)[/QUOTE]

I've just realised that there was a route between the two networks so
the packet would have been sent on one and received on the other... :-(
 
Back
Top