SatCat5
igmp_client.h
Go to the documentation of this file.
1 // Copyright 2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
29 
30 #pragma once
31 
32 #include <satcat5/ip_core.h>
33 #include <satcat5/list.h>
34 #include <satcat5/net_protocol.h>
35 #include <satcat5/polling.h>
36 
37 // Enable support for IPv4 multicast and IGMP?
38 // For memory-constrained projects that do not need to receive multicast
39 // packets, dropping IGMP support can reduce code size by about 3 kB.
40 // For GCC/G++: compiler flag "-DSATCAT5_IGMP_ENABLE=0" to disable.
41 #ifndef SATCAT5_IGMP_ENABLE
42 #define SATCAT5_IGMP_ENABLE 1
43 #endif
44 
45 namespace satcat5 {
46  namespace igmp {
47  // Destination addresses for IGMP broadcasts:
48  constexpr satcat5::ip::Addr
49  DST_ALL_SYSTEMS(224, 0, 0, 1),
50  DST_ALL_ROUTERS(224, 0, 0, 2),
51  DST_IGMPV3_REPORT(224, 0, 0, 22);
52 
55  constexpr unsigned
62 
63  // Protocol constants:
64  constexpr u16
65  MASK_TYPE = 0xFF00,
66  MASK_MAXLEN = 0x00FF,
67  TYPE_QUERY = 0x1100,
68  TYPE_REPORT_V1 = 0x1200,
69  TYPE_REPORT_V2 = 0x1600,
70  TYPE_LEAVE_V2 = 0x1700,
71  TYPE_REPORT_V3 = 0x2200,
72  RECORD_MODE_INCL = 0x0100,
73  RECORD_MODE_EXCL = 0x0200,
74  RECORD_CHANGE_INCL = 0x0300,
75  RECORD_CHANGE_EXCL = 0x0400,
76  RECORD_ALLOW_NEW = 0x0500,
77  RECORD_BLOCK_OLD = 0x0600;
78 
81  bool igmp_send(
82  satcat5::ip::Dispatch* iface,
83  const satcat5::ip::Addr& dst,
84  const unsigned wcount, u16* data);
85 
89  u32 decode_maxdly(u16 hdr, bool v3);
90 
103  class Client final
104  #if SATCAT5_IGMP_ENABLE
105  : protected satcat5::net::Protocol
106  , protected satcat5::poll::Timer
107  #endif
108  {
109  public:
111  explicit Client(satcat5::ip::Dispatch* iface);
112  ~Client();
113 
119  void join(satcat5::igmp::Address& obj, const satcat5::ip::Addr& addr);
120 
123  void leave(satcat5::igmp::Address& obj);
124 
125  protected:
126  #if SATCAT5_IGMP_ENABLE
127  void frame_rcvd(satcat5::io::LimitedRead& src) override;
128  void timer_event() override;
129  satcat5::igmp::Address* find_match(const satcat5::ip::Addr& group) const;
130  void send_leave(const satcat5::ip::Addr& group);
131  void send_report(const satcat5::ip::Addr& group);
132  void set_timer(u32& timer, u32 max_msec);
133 
134  satcat5::ip::Dispatch* const m_iface;
136  u32 m_reply_timer;
137  u8 m_version;
138  #endif
139  };
140 
145  struct Address {
146  public:
150  constexpr Address()
151  #if SATCAT5_IGMP_ENABLE
152  : m_igmp_group(0), m_igmp_timer(0), m_next(nullptr)
153  #endif
154  {} // Empty stub if IGMP is disabled.
155 
162  inline void join(satcat5::igmp::Client* igmp, const satcat5::ip::Addr& addr)
163  { igmp->join(*this, addr); }
164 
168  inline void leave(satcat5::igmp::Client* igmp)
169  { igmp->leave(*this); }
170 
171  protected:
172  #if SATCAT5_IGMP_ENABLE
173  // Required state for the IGMP protocol.
174  friend satcat5::igmp::Client;
175  satcat5::ip::Addr m_igmp_group;
176  u32 m_igmp_timer;
177 
178  // Linked list to the next IgmpAddress object.
180  satcat5::igmp::Address* m_next;
181  #endif
182  };
183  }
184 }
Host/Client for the Internet Group Management Protocol (IGMP).
Definition: igmp_client.h:108
Client(satcat5::ip::Dispatch *iface)
Link this handler to an IPv4 network interface.
Definition: igmp_client.cc:71
void join(satcat5::igmp::Address &obj, const satcat5::ip::Addr &addr)
Join a multicast group.
Definition: igmp_client.cc:87
void timer_event() override
Child class MUST override this method.
Definition: igmp_client.cc:166
void frame_rcvd(satcat5::io::LimitedRead &src) override
Dispatch calls frame_rcvd(...) for each incoming frame with with a matching net::Type value.
Definition: igmp_client.cc:112
void leave(satcat5::igmp::Address &obj)
Leave a multicast group.
Definition: igmp_client.cc:102
Limited read of next N bytes.
Definition: io_readable.h:255
Protocol handler and dispatch unit for Internet Protocol v4 (IPv4).
Definition: ip_dispatch.h:43
A net::Protocol is the counterpart to net::Dispatch that handles a particular data stream,...
Definition: net_protocol.h:28
Timer objects are polled after a fixed delay or at a regular interval.
Definition: polling.h:213
Helper functions for manipulating singly-linked lists.
Definition: list.h:52
constexpr unsigned MIN_LEN_SHORTS
Set upper and lower bounds for the length of an IGMP message.
Definition: igmp_client.h:56
constexpr unsigned MAX_LEN_SHORTS
Set upper and lower bounds for the length of an IGMP message.
Definition: igmp_client.h:58
constexpr u16 TYPE_REPORT_V3
Reply to IGMPv3 query.
Definition: igmp_client.h:71
constexpr u16 RECORD_ALLOW_NEW
Record type: Append to allow list.
Definition: igmp_client.h:76
constexpr unsigned MIN_LEN_BYTES
Set upper and lower bounds for the length of an IGMP message.
Definition: igmp_client.h:57
constexpr u16 RECORD_BLOCK_OLD
Record type: Append to block list.
Definition: igmp_client.h:77
constexpr satcat5::ip::Addr DST_ALL_ROUTERS(224, 0, 0, 2)
< All endpoints on subnet.
constexpr unsigned MAX_V3_RECORDS
Set upper and lower bounds for the length of an IGMP message.
Definition: igmp_client.h:60
constexpr u16 RECORD_CHANGE_INCL
Record type: Change to INCL mode.
Definition: igmp_client.h:74
constexpr unsigned MAX_LEN_BYTES
Set upper and lower bounds for the length of an IGMP message.
Definition: igmp_client.h:59
constexpr u16 TYPE_REPORT_V1
Reply to IGMPv1 query.
Definition: igmp_client.h:68
constexpr u16 RECORD_MODE_INCL
Record type: Include mode.
Definition: igmp_client.h:72
constexpr u16 MASK_MAXLEN
Bit-mask "length" from first header word.
Definition: igmp_client.h:66
constexpr satcat5::ip::Addr DST_IGMPV3_REPORT(224, 0, 0, 22)
< All routers on subnet.
constexpr u16 RECORD_MODE_EXCL
Record type: Exclude mode.
Definition: igmp_client.h:73
u32 decode_maxdly(u16 hdr, bool v3)
Given the first word of the IGMP query header, decode max delay.
Definition: igmp_client.cc:60
constexpr u16 TYPE_REPORT_V2
Reply to IGMPv2 query.
Definition: igmp_client.h:69
constexpr u16 RECORD_CHANGE_EXCL
Record type: Change to EXCL mode.
Definition: igmp_client.h:75
constexpr u16 TYPE_LEAVE_V2
Leaving group (v2 / v3)
Definition: igmp_client.h:70
bool igmp_send(satcat5::ip::Dispatch *iface, const satcat5::ip::Addr &dst, const unsigned wcount, u16 *data)
Given IGMP message contents, write Eth/IP headers and send packet.
Definition: igmp_client.cc:26
constexpr u16 TYPE_QUERY
IGMP query (all versions)
Definition: igmp_client.h:67
constexpr u16 MASK_TYPE
Bit-mask "type" from first header word.
Definition: igmp_client.h:65
Templated functions for manipulating singly-linked lists.
Generic network Protocol API.
Core event-processing loop for SatCat5 software.
Client state for an IGMP multicast subscription.
Definition: igmp_client.h:145
void join(satcat5::igmp::Client *igmp, const satcat5::ip::Addr &addr)
Join a multicast group.
Definition: igmp_client.h:162
void leave(satcat5::igmp::Client *igmp)
Leave a multicast group.
Definition: igmp_client.h:168
constexpr Address()
Constructor for an address placeholder.
Definition: igmp_client.h:150
IPv4 address is a 32-bit unsigned integer.
Definition: ip_core.h:15