3

I'd like to know, how to determine IP address of machine, which sends multicast packets to the multicast group, using asio library. Now my source is like:

std::string listen_addr4 = lpd_config.config.lpd_listen_host4();
std::string multicast_addr4 = lpd_config.config.lpd_multicast_host4();
short multicast_port = lpd_config.config.lpd_multicast_port();

asio::ip::udp::endpoint lpd_listen_endpoint4(asio::ip::address::from_string(listen_addr4), multicast_port); //TODO ipv6 support

asio::ip::udp::socket lpd_listen_socket4(lpd_io_service, lpd_listen_endpoint4.protocol());  //TODO ipv6 support

lpd_listen_socket4.set_option(asio::ip::udp::socket::reuse_address(true));
lpd_listen_socket4.bind(lpd_listen_endpoint4);  //TODO ipv6 support

lpd_listen_socket4.set_option(asio::ip::multicast::join_group(asio::ip::address::from_string(multicast_addr4)));    //TODO ipv6 support

What to do next?

Sam Miller
  • 23,808
  • 4
  • 67
  • 87
Alexander Shishenko
  • 940
  • 1
  • 11
  • 28

1 Answers1

3

As any machine could be broadcasting to the multicast group, what you need to do is to start listening to messages, using the receive_from or the async_receive_from methods on the upd::socket object.

These methods will populate an endpoint object with the remote peer details, which you can then resolve to an address.

small_duck
  • 3,038
  • 20
  • 28