0

I am trying to send a multicast packet on a network interface with multiple host ip addresses(lets say ips A and B). How can I specify the source ip field of said multicast packet to the host ip that I prefer(A). Currently, multicast packets have their source ip as B. I would prefer to do it OS(Windows 10) level. How can I set this up correctly?

I am using java for the sender program and I found that for MulticastSocket objects setInterface(InetAddress inf) method can be used to (I assume) specify the chosen source ip address but DatagramChannel only allows setting network interface and I would much rather prefer to config this in OS level.

EDIT: forgot to add: route table's first row is Dest - (0.0.0.0) | Netmask - (0.0.0.0) | Gateway - Gateway IP | Interface - IP A with the lowest metric value of all the entries in the route table. Still multicast packets have ip B as their source

EDIT2: I tried the following Scenario 1:

  • bind socket to wildcard address
  • iterate over all valid interfaces
  • set socket interface to each valid interface and send packets
  • with this, I can send packets over multiple interfaces using single socket object

Scenario 2:

  • bind socket to a specific local ip address (say IP1) on interface 1
  • iterate over valid interfaces
  • try to set socket interface to each interface and send packets
  • in this one, interface setting doesnt do anything and all packets sent during iteration leave on the interface with IP1

Shortcoming of the first one is that while I can specify the network interface, I cannot specify the local ip if there are multiple local ip addresses on that interface, second one allows one to specify the local ip address but cannot iterate over all valid interfaces with only 1 socket. It seems I will need to create n sockets bound to n local ip addresses on n different interfaces to be able to send packets on n valid interfaces.

  • 2
    This is platform dependent. On Windows, a semi usable approach was to use netsh, and branding the addresses that you do not want to use as non-source address. https://serverfault.com/questions/670294/set-default-windows-server-interface-ip-with-multiple-ips – Greg Askew Feb 21 '24 at 13:02
  • Thank you for the reply, will get back with the results. In the meantime, is there a general solution to this problem where packets to some multicast groups have ip A as a source and some other groups have ip B as their source? I would assume that route table would do the trick but like I said in the edit, route table's first entry should send all packets using IP B based on my understanding. – denvercoder Feb 21 '24 at 13:25
  • Windows is not a good platform for that. Linux and IP tables is more flexible. Or Windows Server with Routing and Remote Access enabled. – Greg Askew Feb 21 '24 at 14:05

1 Answers1

0

You can bind your sender to a specific local IP address to be used as source address.

If you use the unspecified address 0.0.0.0 most platforms use the "default" address, usually the first or the last(!) one.

Zac67
  • 11,547
  • Thank you for the reply. I am using the sender for sending multicast packets on multiple interfaces by binding it to wildcard address and iterating over valid interfaces during send. I tried binding it to a specific local ip, then no matter which network interface I try to set it to, packets will always go out of the interface with specified local ip address. Thats why I think it will not suitable for this particular case. – denvercoder Feb 21 '24 at 21:29
  • You need to iterate over the local IP addresses, sort them by their bound interface and then select your favorite IP address from each interface. You shouldn't be able to use a source address that isn't bound to an interface (sounds like you tried just that). – Zac67 Feb 21 '24 at 21:41
  • Since answer was too long to post as a comment, I have updated the question with edit2 explaining what I had tried. It seems like the only solution for this particular problem, would you agree? – denvercoder Feb 21 '24 at 21:57