使用boost的源特定组播

时间:2011-12-26 15:27:34

标签: c++ boost udp boost-asio multicast

如何使用boost加入特定于源的多播(ssm)组?

我使用

成功加入任意源组播组

set_option( ip::multicast::join_group( mcAddr.to_v4(), ifAddr.to_v4( )));

但我不知道如何设置ssm(IP_ADD_SOURCE_MEMBERSHIP)。

我该怎么办?谢谢。

2 个答案:

答案 0 :(得分:1)

看起来你必须实现他们的SettableSocketOption概念。检查现有多播选项的完成方式(svn link)可能有所帮助。

答案 1 :(得分:0)

是肯定的。我用这个

//---------------------------------------------------------------------------------------------------------
// class add_source_membership_v4
// is for Windows Sockets 2 IPv4
// to set IP_ADD_SOURCE_MEMBERSHIP option.
//---------------------------------------------------------------------------------------------------------
class add_source_membership_v4
{
private:
    ip_mreq_source ipv4_value_{};

public:
    // Default constructor.
    // Construct with multicast address and IPv4 address specifying an interface.
    add_source_membership_v4(
        const boost::asio::ip::address_v4& multicast_address,
        const boost::asio::ip::address_v4& source_address,
        const boost::asio::ip::address_v4& network_interface = boost::asio::ip::address_v4::any())
    {
        ipv4_value_.imr_multiaddr.s_addr =
            boost::asio::detail::socket_ops::host_to_network_long(
                multicast_address.to_uint());
        ipv4_value_.imr_sourceaddr.s_addr =
            boost::asio::detail::socket_ops::host_to_network_long(
                source_address.to_uint());
        ipv4_value_.imr_interface.s_addr =
            boost::asio::detail::socket_ops::host_to_network_long(
                network_interface.to_uint());
    }

    // Returns a value suitable for passing as the level argument to POSIX setsockopt() (or equivalent).
    template <typename Protocol>
    int level(const Protocol&) const noexcept
    {
        return BOOST_ASIO_OS_DEF(IPPROTO_IP);
    }

    // Returns a value suitable for passing as the option_name argument to POSIX setsockopt() (or equivalent).
    template <typename Protocol>
    int name(const Protocol&) const noexcept
    {
        return IP_ADD_SOURCE_MEMBERSHIP;
    }

    // Returns a pointer suitable for passing as the option_value argument to POSIX setsockopt() (or equivalent).
    template <typename Protocol>
    const void* data(const Protocol&) const noexcept
    {
        return &ipv4_value_;
    }

    // Returns a value suitable for passing as the option_len argument to POSIX setsockopt() (or equivalent),
    // after appropriate integer conversion has been performed.
    template <typename Protocol>
    std::size_t size(const Protocol&) const noexcept
    {
        return sizeof(ipv4_value_);
    }
};