live
Groupsock.hh
Go to the documentation of this file.
1 /**********
2 This library is free software; you can redistribute it and/or modify it under
3 the terms of the GNU Lesser General Public License as published by the
4 Free Software Foundation; either version 3 of the License, or (at your
5 option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6 
7 This library is distributed in the hope that it will be useful, but WITHOUT
8 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10 more details.
11 
12 You should have received a copy of the GNU Lesser General Public License
13 along with this library; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 **********/
16 // "groupsock"
17 // Copyright (c) 1996-2021 Live Networks, Inc. All rights reserved.
18 // 'Group sockets'
19 // C++ header
20 
21 #ifndef _GROUPSOCK_HH
22 #define _GROUPSOCK_HH
23 
24 #ifndef _GROUPSOCK_VERSION_HH
25 #include "groupsock_version.hh"
26 #endif
27 
28 #ifndef _NET_INTERFACE_HH
29 #include "NetInterface.hh"
30 #endif
31 
32 #ifndef _GROUPEID_HH
33 #include "GroupEId.hh"
34 #endif
35 
36 // An "OutputSocket" is (by default) used only to send packets.
37 // No packets are received on it (unless a subclass arranges this)
38 
39 class OutputSocket: public Socket {
40 public:
41  OutputSocket(UsageEnvironment& env, int family);
42  virtual ~OutputSocket();
43 
44  virtual Boolean write(struct sockaddr_storage const& addressAndPort, u_int8_t ttl,
45  unsigned char* buffer, unsigned bufferSize);
46 
47 protected:
48  OutputSocket(UsageEnvironment& env, Port port, int family);
49 
51 
52 private: // redefined virtual function
53  virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
54  unsigned& bytesRead,
55  struct sockaddr_storage& fromAddressAndPort);
56 
57 private:
59  unsigned fLastSentTTL;
60 };
61 
62 class destRecord {
63 public:
64  destRecord(struct sockaddr_storage const& addr, Port const& port, u_int8_t ttl, unsigned sessionId,
65  destRecord* next);
66  virtual ~destRecord();
67 
68 public:
71  unsigned fSessionId;
72 };
73 
74 // A "Groupsock" is used to both send and receive packets.
75 // As the name suggests, it was originally designed to send/receive
76 // multicast, but it can send/receive unicast as well.
77 
78 class Groupsock: public OutputSocket {
79 public:
80  Groupsock(UsageEnvironment& env, struct sockaddr_storage const& groupAddr,
81  Port port, u_int8_t ttl);
82  // used for a 'source-independent multicast' group
83  Groupsock(UsageEnvironment& env, struct sockaddr_storage const& groupAddr,
84  struct sockaddr_storage const& sourceFilterAddr,
85  Port port);
86  // used for a 'source-specific multicast' group
87 
88  virtual ~Groupsock();
89 
90  virtual destRecord* createNewDestRecord(struct sockaddr_storage const& addr, Port const& port, u_int8_t ttl, unsigned sessionId, destRecord* next);
91  // Can be redefined by subclasses that also subclass "destRecord"
92 
93  void changeDestinationParameters(struct sockaddr_storage const& newDestAddr,
94  Port newDestPort, int newDestTTL,
95  unsigned sessionId = 0);
96  // By default, the destination address, port and ttl for
97  // outgoing packets are those that were specified in
98  // the constructor. This works OK for multicast sockets,
99  // but for unicast we usually want the destination port
100  // number, at least, to be different from the source port.
101  // (If a parameter is 0 (or ~0 for ttl), then no change is made to that parameter.)
102  // (If no existing "destRecord" exists with this "sessionId", then we add a new "destRecord".)
103  unsigned lookupSessionIdFromDestination(struct sockaddr_storage const& destAddrAndPort) const;
104  // returns 0 if not found
105 
106  // As a special case, we also allow multiple destinations (addresses & ports)
107  // (This can be used to implement multi-unicast.)
108  virtual void addDestination(struct sockaddr_storage const& addr, Port const& port,
109  unsigned sessionId);
110  virtual void removeDestination(unsigned sessionId);
111  void removeAllDestinations();
113 
114  struct sockaddr_storage const& groupAddress() const {
116  }
117  struct sockaddr_storage const& sourceFilterAddress() const {
119  }
120 
121  Boolean isSSM() const {
122  return fIncomingGroupEId.isSSM();
123  }
124 
125  u_int8_t ttl() const { return fIncomingGroupEId.ttl(); }
126 
127  void multicastSendOnly(); // send, but don't receive any multicast packets
128 
129  virtual Boolean output(UsageEnvironment& env, unsigned char* buffer, unsigned bufferSize);
130 
135 
137  struct sockaddr_storage const& fromAddressAndPort);
138 
139 public: // redefined virtual functions
140  virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
141  unsigned& bytesRead,
142  struct sockaddr_storage& fromAddressAndPort);
143 
144 protected:
145  destRecord* lookupDestRecordFromDestination(struct sockaddr_storage const& targetAddrAndPort) const;
146 
147 private:
148  void removeDestinationFrom(destRecord*& dests, unsigned sessionId);
149  // used to implement (the public) "removeDestination()", and "changeDestinationParameters()"
150 protected:
152 private:
154 };
155 
157 
158 // A data structure for looking up a 'groupsock'
159 // by (multicast address, port), or by socket number
161 public:
162  Groupsock* Fetch(UsageEnvironment& env, struct sockaddr_storage const& groupAddress,
163  Port port, u_int8_t ttl, Boolean& isNew);
164  // Creates a new Groupsock if none already exists
165  Groupsock* Fetch(UsageEnvironment& env, struct sockaddr_storage const& groupAddress,
166  struct sockaddr_storage const& sourceFilterAddr,
167  Port port, Boolean& isNew);
168  // Creates a new Groupsock if none already exists
169  Groupsock* Lookup(struct sockaddr_storage const& groupAddress, Port port);
170  // Returns NULL if none already exists
171  Groupsock* Lookup(struct sockaddr_storage const& groupAddress,
172  struct sockaddr_storage const& sourceFilterAddr,
173  Port port);
174  // Returns NULL if none already exists
175  Groupsock* Lookup(UsageEnvironment& env, int sock);
176  // Returns NULL if none already exists
177  Boolean Remove(Groupsock const* groupsock);
178 
179  // Used to iterate through the groupsocks in the table
180  class Iterator {
181  public:
182  Iterator(GroupsockLookupTable& groupsocks);
183 
184  Groupsock* next(); // NULL iff none
185 
186  private:
188  };
189 
190 private:
192  struct sockaddr_storage const& groupAddress,
193  struct sockaddr_storage const& sourceFilterAddress,
194  Port port, u_int8_t ttl);
195 
196 private:
197  friend class Iterator;
199 };
200 
201 #endif
GroupEId fGroupEId
Definition: Groupsock.hh:70
unsigned char Boolean
Definition: Boolean.hh:25
AddressPortLookupTable::Iterator fIter
Definition: Groupsock.hh:187
GroupEId fIncomingGroupEId
Definition: Groupsock.hh:153
virtual Boolean handleRead(unsigned char *buffer, unsigned bufferMaxSize, unsigned &bytesRead, struct sockaddr_storage &fromAddressAndPort)
virtual ~destRecord()
Groupsock(UsageEnvironment &env, struct sockaddr_storage const &groupAddr, Port port, u_int8_t ttl)
u_int8_t ttl() const
Definition: Groupsock.hh:125
virtual destRecord * createNewDestRecord(struct sockaddr_storage const &addr, Port const &port, u_int8_t ttl, unsigned sessionId, destRecord *next)
u_int16_t portNumBits
Definition: NetAddress.hh:102
virtual Boolean write(struct sockaddr_storage const &addressAndPort, u_int8_t ttl, unsigned char *buffer, unsigned bufferSize)
void multicastSendOnly()
NetInterfaceTrafficStats statsGroupOutgoing
Definition: Groupsock.hh:134
Groupsock * AddNew(UsageEnvironment &env, struct sockaddr_storage const &groupAddress, struct sockaddr_storage const &sourceFilterAddress, Port port, u_int8_t ttl)
struct sockaddr_storage const & sourceFilterAddress() const
Definition: Groupsock.hh:117
Boolean wasLoopedBackFromUs(UsageEnvironment &env, struct sockaddr_storage const &fromAddressAndPort)
destRecord * lookupDestRecordFromDestination(struct sockaddr_storage const &targetAddrAndPort) const
OutputSocket(UsageEnvironment &env, int family)
#define NULL
destRecord(struct sockaddr_storage const &addr, Port const &port, u_int8_t ttl, unsigned sessionId, destRecord *next)
Port fSourcePort
Definition: Groupsock.hh:58
portNumBits num() const
Definition: NetAddress.hh:108
unsigned lookupSessionIdFromDestination(struct sockaddr_storage const &destAddrAndPort) const
unsigned fLastSentTTL
Definition: Groupsock.hh:59
struct sockaddr_storage const & groupAddress() const
Definition: Groupsock.hh:114
UsageEnvironment & operator<<(UsageEnvironment &s, const Groupsock &g)
destRecord * fNext
Definition: Groupsock.hh:69
Groupsock * Fetch(UsageEnvironment &env, struct sockaddr_storage const &groupAddress, Port port, u_int8_t ttl, Boolean &isNew)
virtual Boolean handleRead(unsigned char *buffer, unsigned bufferMaxSize, unsigned &bytesRead, struct sockaddr_storage &fromAddressAndPort)
u_int8_t ttl() const
Definition: GroupEId.hh:50
NetInterfaceTrafficStats statsGroupIncoming
Definition: Groupsock.hh:133
virtual ~OutputSocket()
struct sockaddr_storage const & sourceFilterAddress() const
Definition: GroupEId.hh:44
void removeDestinationFrom(destRecord *&dests, unsigned sessionId)
destRecord * fDests
Definition: Groupsock.hh:151
void changeDestinationParameters(struct sockaddr_storage const &newDestAddr, Port newDestPort, int newDestTTL, unsigned sessionId=0)
virtual void removeDestination(unsigned sessionId)
Port port() const
Definition: NetInterface.hh:51
Boolean isSSM() const
Definition: Groupsock.hh:121
Boolean hasMultipleDestinations() const
Definition: Groupsock.hh:112
void removeAllDestinations()
static NetInterfaceTrafficStats statsIncoming
Definition: Groupsock.hh:131
unsigned fSessionId
Definition: Groupsock.hh:71
virtual Boolean output(UsageEnvironment &env, unsigned char *buffer, unsigned bufferSize)
static NetInterfaceTrafficStats statsOutgoing
Definition: Groupsock.hh:132
portNumBits sourcePortNum() const
Definition: Groupsock.hh:50
virtual ~Groupsock()
Iterator(GroupsockLookupTable &groupsocks)
Groupsock * Lookup(struct sockaddr_storage const &groupAddress, Port port)
Boolean Remove(Groupsock const *groupsock)
AddressPortLookupTable fTable
Definition: Groupsock.hh:198
virtual void addDestination(struct sockaddr_storage const &addr, Port const &port, unsigned sessionId)
UsageEnvironment & env() const
Definition: NetInterface.hh:55
Boolean isSSM() const
struct sockaddr_storage const & groupAddress() const
Definition: GroupEId.hh:43