live
Groupsock.hh
Go to the documentation of this file.
1/**********
2This library is free software; you can redistribute it and/or modify it under
3the terms of the GNU Lesser General Public License as published by the
4Free Software Foundation; either version 3 of the License, or (at your
5option) any later version. (See <http://www.gnu.org/copyleft/lesser.html>.)
6
7This library is distributed in the hope that it will be useful, but WITHOUT
8ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
9FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
10more details.
11
12You should have received a copy of the GNU Lesser General Public License
13along with this library; if not, write to the Free Software Foundation, Inc.,
1451 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15**********/
16// "groupsock"
17// Copyright (c) 1996-2024 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
39class OutputSocket: public Socket {
40public:
42 virtual ~OutputSocket();
43
44 virtual Boolean write(struct sockaddr_storage const& addressAndPort, u_int8_t ttl,
45 unsigned char* buffer, unsigned bufferSize);
46
47protected:
49
51
52private: // redefined virtual function
53 virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
54 unsigned& bytesRead,
55 struct sockaddr_storage& fromAddressAndPort);
56
57private:
59 unsigned fLastSentTTL;
60};
61
63public:
64 destRecord(struct sockaddr_storage const& addr, Port const& port, u_int8_t ttl, unsigned sessionId,
65 destRecord* next);
66 virtual ~destRecord();
67
68public:
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
78class Groupsock: public OutputSocket {
79public:
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);
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
139public: // redefined virtual functions
140 virtual Boolean handleRead(unsigned char* buffer, unsigned bufferMaxSize,
141 unsigned& bytesRead,
142 struct sockaddr_storage& fromAddressAndPort);
143
144protected:
145 destRecord* lookupDestRecordFromDestination(struct sockaddr_storage const& targetAddrAndPort) const;
146
147private:
148 void removeDestinationFrom(destRecord*& dests, unsigned sessionId);
149 // used to implement (the public) "removeDestination()", and "changeDestinationParameters()"
150protected:
152private:
154};
155
157
158// A data structure for looking up a 'groupsock'
159// by (multicast address, port), or by socket number
161public:
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
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:
183
184 Groupsock* next(); // NULL iff none
185
186 private:
188 };
189
190private:
192 struct sockaddr_storage const& groupAddress,
193 struct sockaddr_storage const& sourceFilterAddress,
194 Port port, u_int8_t ttl);
195
196private:
197 friend class Iterator;
199};
200
201#endif
unsigned char Boolean
Definition: Boolean.hh:25
UsageEnvironment & operator<<(UsageEnvironment &s, const Groupsock &g)
u_int16_t portNumBits
Definition: NetAddress.hh:102
#define NULL
struct sockaddr_storage const & sourceFilterAddress() const
Definition: GroupEId.hh:44
struct sockaddr_storage const & groupAddress() const
Definition: GroupEId.hh:43
Boolean isSSM() const
u_int8_t ttl() const
Definition: GroupEId.hh:50
Iterator(GroupsockLookupTable &groupsocks)
AddressPortLookupTable::Iterator fIter
Definition: Groupsock.hh:187
Groupsock * Lookup(UsageEnvironment &env, int sock)
Groupsock * Fetch(UsageEnvironment &env, struct sockaddr_storage const &groupAddress, Port port, u_int8_t ttl, Boolean &isNew)
Groupsock * Fetch(UsageEnvironment &env, struct sockaddr_storage const &groupAddress, struct sockaddr_storage const &sourceFilterAddr, Port port, Boolean &isNew)
Boolean Remove(Groupsock const *groupsock)
Groupsock * Lookup(struct sockaddr_storage const &groupAddress, struct sockaddr_storage const &sourceFilterAddr, Port port)
Groupsock * AddNew(UsageEnvironment &env, struct sockaddr_storage const &groupAddress, struct sockaddr_storage const &sourceFilterAddress, Port port, u_int8_t ttl)
AddressPortLookupTable fTable
Definition: Groupsock.hh:198
Groupsock * Lookup(struct sockaddr_storage const &groupAddress, Port port)
NetInterfaceTrafficStats statsGroupOutgoing
Definition: Groupsock.hh:134
Boolean wasLoopedBackFromUs(UsageEnvironment &env, struct sockaddr_storage const &fromAddressAndPort)
destRecord * fDests
Definition: Groupsock.hh:151
static NetInterfaceTrafficStats statsIncoming
Definition: Groupsock.hh:131
unsigned lookupSessionIdFromDestination(struct sockaddr_storage const &destAddrAndPort) const
Groupsock(UsageEnvironment &env, struct sockaddr_storage const &groupAddr, struct sockaddr_storage const &sourceFilterAddr, Port port)
struct sockaddr_storage const & groupAddress() const
Definition: Groupsock.hh:114
struct sockaddr_storage const & sourceFilterAddress() const
Definition: Groupsock.hh:117
void removeDestinationFrom(destRecord *&dests, unsigned sessionId)
virtual void removeDestination(unsigned sessionId)
static NetInterfaceTrafficStats statsOutgoing
Definition: Groupsock.hh:132
destRecord * lookupDestRecordFromDestination(struct sockaddr_storage const &targetAddrAndPort) const
virtual void addDestination(struct sockaddr_storage const &addr, Port const &port, unsigned sessionId)
void removeAllDestinations()
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)
GroupEId fIncomingGroupEId
Definition: Groupsock.hh:153
Boolean hasMultipleDestinations() const
Definition: Groupsock.hh:112
virtual ~Groupsock()
Boolean isSSM() const
Definition: Groupsock.hh:121
void changeDestinationParameters(struct sockaddr_storage const &newDestAddr, Port newDestPort, int newDestTTL, unsigned sessionId=0)
virtual Boolean output(UsageEnvironment &env, unsigned char *buffer, unsigned bufferSize)
void multicastSendOnly()
virtual Boolean handleRead(unsigned char *buffer, unsigned bufferMaxSize, unsigned &bytesRead, struct sockaddr_storage &fromAddressAndPort)
NetInterfaceTrafficStats statsGroupIncoming
Definition: Groupsock.hh:133
Groupsock(UsageEnvironment &env, struct sockaddr_storage const &groupAddr, Port port, u_int8_t ttl)
portNumBits sourcePortNum() const
Definition: Groupsock.hh:50
OutputSocket(UsageEnvironment &env, Port port, int family)
Port fSourcePort
Definition: Groupsock.hh:58
virtual Boolean write(struct sockaddr_storage const &addressAndPort, u_int8_t ttl, unsigned char *buffer, unsigned bufferSize)
virtual Boolean handleRead(unsigned char *buffer, unsigned bufferMaxSize, unsigned &bytesRead, struct sockaddr_storage &fromAddressAndPort)
OutputSocket(UsageEnvironment &env, int family)
virtual ~OutputSocket()
unsigned fLastSentTTL
Definition: Groupsock.hh:59
portNumBits num() const
Definition: NetAddress.hh:108
Port port() const
Definition: NetInterface.hh:51
UsageEnvironment & env() const
Definition: NetInterface.hh:55
virtual ~destRecord()
destRecord * fNext
Definition: Groupsock.hh:69
GroupEId fGroupEId
Definition: Groupsock.hh:70
destRecord(struct sockaddr_storage const &addr, Port const &port, u_int8_t ttl, unsigned sessionId, destRecord *next)
unsigned fSessionId
Definition: Groupsock.hh:71