b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | --- a/mcproxy/include/proxy/igmp_sender.hpp |
| 2 | +++ b/mcproxy/include/proxy/igmp_sender.hpp |
| 3 | @@ -37,9 +37,10 @@ class igmp_sender : public sender |
| 4 | { |
| 5 | private: |
| 6 | bool send_igmpv3_query(unsigned int if_index, const timers_values& tv, const addr_storage& gaddr, bool s_flag, const source_list<source>& slist) const; |
| 7 | + bool send_igmpv2_query(unsigned int if_index, const timers_values& tv, const addr_storage& gaddr ) const; |
| 8 | |
| 9 | public: |
| 10 | - igmp_sender(const std::shared_ptr<const interfaces>& interfaces); |
| 11 | + igmp_sender(const std::shared_ptr<const interfaces>& interfaces, const group_mem_protocol gmp); |
| 12 | |
| 13 | bool send_record(unsigned int if_index, mc_filter filter_mode, const addr_storage& gaddr, const source_list<source>& slist) const override; |
| 14 | |
| 15 | --- a/mcproxy/src/proxy/igmp_sender.cpp |
| 16 | +++ b/mcproxy/src/proxy/igmp_sender.cpp |
| 17 | @@ -32,7 +32,7 @@ |
| 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | -igmp_sender::igmp_sender(const std::shared_ptr<const interfaces>& interfaces): sender(interfaces, IGMPv3) |
| 22 | +igmp_sender::igmp_sender(const std::shared_ptr<const interfaces>& interfaces, const group_mem_protocol gmp): sender(interfaces, gmp) |
| 23 | { |
| 24 | HC_LOG_TRACE(""); |
| 25 | |
| 26 | @@ -119,10 +119,79 @@ bool igmp_sender::send_mc_addr_and_src_s |
| 27 | return rc; |
| 28 | } |
| 29 | |
| 30 | +bool igmp_sender::send_igmpv2_query(unsigned int if_index, const timers_values& tv, const addr_storage& gaddr ) const |
| 31 | +{ |
| 32 | + HC_LOG_TRACE(""); |
| 33 | + |
| 34 | + std::unique_ptr<unsigned char[]> packet; |
| 35 | + unsigned int size; |
| 36 | + |
| 37 | + size = sizeof(ip) + sizeof(router_alert_option) + sizeof(igmp); |
| 38 | + packet.reset(new unsigned char[size]); |
| 39 | + |
| 40 | + addr_storage dst_addr; |
| 41 | + |
| 42 | + if (gaddr == addr_storage(AF_INET)) { //is general query |
| 43 | + dst_addr = IPV4_ALL_HOST_ADDR; |
| 44 | + } else { |
| 45 | + dst_addr = gaddr; |
| 46 | + } |
| 47 | + |
| 48 | + //------------------------------------------------------------------- |
| 49 | + //fill ip header |
| 50 | + ip* ip_hdr = reinterpret_cast<ip*>(packet.get()); |
| 51 | + |
| 52 | + ip_hdr->ip_v = 4; |
| 53 | + ip_hdr->ip_hl = (sizeof(ip) + sizeof(router_alert_option)) / 4; |
| 54 | + ip_hdr->ip_tos = 0; |
| 55 | + ip_hdr->ip_len = htons(size); |
| 56 | + ip_hdr->ip_id = 0; |
| 57 | + ip_hdr->ip_off = htons(0 | IP_DF); //dont fragment flag |
| 58 | + ip_hdr->ip_ttl = 1; |
| 59 | + ip_hdr->ip_p = IPPROTO_IGMP; |
| 60 | + ip_hdr->ip_sum = 0; |
| 61 | + ip_hdr->ip_src = m_interfaces->get_saddr(interfaces::get_if_name(if_index)).get_in_addr(); |
| 62 | + ip_hdr->ip_dst = dst_addr.get_in_addr(); |
| 63 | + |
| 64 | + //------------------------------------------------------------------- |
| 65 | + //fill router_alert_option header |
| 66 | + router_alert_option* ra_hdr = reinterpret_cast<router_alert_option*>(reinterpret_cast<unsigned char*>(ip_hdr) + sizeof(ip)); |
| 67 | + *ra_hdr = router_alert_option(); |
| 68 | + |
| 69 | + ip_hdr->ip_sum = m_sock.calc_checksum(reinterpret_cast<unsigned char*>(ip_hdr), sizeof(ip) + sizeof(router_alert_option)); |
| 70 | + |
| 71 | + //------------------------------------------------------------------- |
| 72 | + //fill igmpv3 query |
| 73 | + igmp* query = reinterpret_cast<igmp*>(reinterpret_cast<unsigned char*>(ra_hdr) + sizeof(router_alert_option)); |
| 74 | + |
| 75 | + query->igmp_type = IGMP_MEMBERSHIP_QUERY; |
| 76 | + |
| 77 | + if (gaddr == addr_storage(AF_INET)) { //general query |
| 78 | + query->igmp_code = tv.maxrespi_to_maxrespc_igmpv3(tv.get_query_response_interval()); |
| 79 | + } else { |
| 80 | + query->igmp_code = tv.maxrespi_to_maxrespc_igmpv3(tv.get_last_listener_query_time()); |
| 81 | + } |
| 82 | + |
| 83 | + query->igmp_cksum = 0; |
| 84 | + query->igmp_group = gaddr.get_in_addr(); |
| 85 | + |
| 86 | + query->igmp_cksum = m_sock.calc_checksum(reinterpret_cast<unsigned char*>(query), (sizeof(igmp) )); |
| 87 | + |
| 88 | + if (!m_sock.choose_if(if_index)) { |
| 89 | + return false; |
| 90 | + } |
| 91 | + |
| 92 | + return m_sock.send_packet(dst_addr, reinterpret_cast<unsigned char*>(ip_hdr), size); |
| 93 | +} |
| 94 | + |
| 95 | bool igmp_sender::send_igmpv3_query(unsigned int if_index, const timers_values& tv, const addr_storage& gaddr, bool s_flag, const source_list<source>& slist) const |
| 96 | { |
| 97 | HC_LOG_TRACE(""); |
| 98 | |
| 99 | + if ( (m_group_mem_protocol & IGMPv3) == 0 ) { |
| 100 | + return send_igmpv2_query( if_index, tv, gaddr ); |
| 101 | + } |
| 102 | + |
| 103 | std::unique_ptr<unsigned char[]> packet; |
| 104 | unsigned int size; |
| 105 | |
| 106 | --- a/mcproxy/src/proxy/proxy_instance.cpp |
| 107 | +++ b/mcproxy/src/proxy/proxy_instance.cpp |
| 108 | @@ -119,7 +119,7 @@ bool proxy_instance::init_sender() |
| 109 | { |
| 110 | HC_LOG_TRACE(""); |
| 111 | if (is_IPv4(m_group_mem_protocol)) { |
| 112 | - m_sender = std::make_shared<igmp_sender>(m_interfaces); |
| 113 | + m_sender = std::make_shared<igmp_sender>(m_interfaces, m_group_mem_protocol ); |
| 114 | } else if (is_IPv6(m_group_mem_protocol)) { |
| 115 | m_sender = std::make_shared<mld_sender>(m_interfaces); |
| 116 | } else { |