Reputation: 403
Maybe this question is strange. I don't know. For example, getting multicast packets in Java:
byte[] buf = new byte[1500];
MulticastSocket socket = new MulticastSocket(1234);
InetAddress group = InetAddress.getByName("233.0.0.1");
socket.joinGroup(group);
while (true) {
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
// doing something with packet
}
socket.leaveGroup(group);
socket.close();
But it works well if I know multicast group address. And what if I don't know? I need to scan the network and find all available multicast streams by port. I don't know address but I know port. Is it possible?
Suppose I have 3 streams with addresses 233.0.0.1:1234, 233.0.0.2:1234, 233.0.0.3:1234. I need to discover network by port 1234 and find this addresses. How can I do it without going through all the multicast addresses in the loop and joining every group?
Upvotes: 1
Views: 1285
Reputation: 718788
How to discover multicast addresses in Java?
AFAIK, there is no way to do this from Java (currently).
The problem is that multicast routing is essentially receiver driven. It goes something like this:
As you can see, this works without any centralized authority. Indeed, if nothing in your local routing domain is currently using a given multicast group, there may be no trace of it on the network. It is possible to snoop the IGMP traffic ... but that only tells you about groups that are in use locally, not all available groups.
For more information, the Wikipedia article on IP Multicast is a good place to start.
Normally, most of the multicast routing and associate protocols are implemented in your network switches. However, there is linux-based software that supports multicasts routing, etcetera; see http://troglobit.com/howto/multicast/. You may be able to use this to do your on IGMP snooping ... though this has limitations; see above.
Your code doesn't appear to be doing multicast address discovery at all. Rather, it is joining a specific group, and receiving multicast traffic on a the IP address.
AFAIK, you cannot monitor to "all multicast traffic" from standard Java. Java doesn't support promiscuous mode. However, it may be possible to do it using the jpcap library ... with the caveat that you will only see traffic that lands on your local network segment.
Upvotes: 2