Debian Codegraph @

srtcore/packetfilter.h

raw
1 /*
2 * SRT - Secure, Reliable, Transport
3 * Copyright (c) 2019 Haivision Systems Inc.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 */
10
11 #ifndef INC_SRT_PACKETFILTER_H
12 #define INC_SRT_PACKETFILTER_H
13
14 #include <cstdlib>
15 #include <map>
16 #include <string>
17
18 #include "packet.h"
19 #include "utilities.h"
20 #include "packetfilter_api.h"
21
22 namespace srt {
23
24 class CUnitQueue;
25 struct CUnit;
26 class CUDT;
27
28 class PacketFilter
29 {
30 friend class SrtPacketFilterBase;
31
32 public:
33 typedef std::vector< std::pair<int32_t, int32_t> > loss_seqs_t;
34
35 typedef SrtPacketFilterBase* filter_create_t(const SrtFilterInitializer& init, std::vector<SrtPacket>&, const std::string& config);
36
37 class Factory
38 {
39 public:
40 virtual SrtPacketFilterBase* Create(const SrtFilterInitializer& init, std::vector<SrtPacket>& provided, const std::string& confstr) = 0;
41
42 // Characteristic data
43 virtual size_t ExtraSize() const = 0;
44
45 // Represent default parameters. This is for completing and comparing
46 // filter configurations from both parties. Possible values to return:
47 // - an empty string (all parameters are mandatory)
48 // - a form of: "<filter-name>,<param1>:<value1>,..."
49 virtual std::string defaultConfig() const = 0;
50 virtual bool verifyConfig(const SrtFilterConfig& config, std::string& w_errormsg) const = 0;
51 virtual ~Factory();
52 };
53 private:
54
55 template <class Target>
56 class Creator: public Factory
57 {
58 virtual SrtPacketFilterBase* Create(const SrtFilterInitializer& init,
59 std::vector<SrtPacket>& provided,
60 const std::string& confstr) ATR_OVERRIDE
61 { return new Target(init, provided, confstr); }
62
63 // Import the extra size data
64 virtual size_t ExtraSize() const ATR_OVERRIDE { return Target::EXTRA_SIZE; }
65 virtual std::string defaultConfig() const ATR_OVERRIDE { return Target::defaultConfig; }
66 virtual bool verifyConfig(const SrtFilterConfig& config, std::string& w_errormsg) const ATR_OVERRIDE
67 {
68 return Target::verifyConfig(config, (w_errormsg));
69 }
70
71 public:
72 Creator() {}
73 virtual ~Creator() {}
74 };
75
76 // We need a private wrapper for the auto-pointer, can't use
77 // std::unique_ptr here due to no C++11.
78 struct ManagedPtr
79 {
80 Factory* f;
81 mutable bool owns;
82
83 // Accept whatever
84 ManagedPtr(Factory* ff): f(ff), owns(true) {}
85 ManagedPtr(): f(NULL), owns(false) {}
86 ~ManagedPtr()
87 {
88 if (owns)
89 delete f;
90 }
91
92 void copy_internal(const ManagedPtr& other)
93 {
94 other.owns = false;
95 f = other.f;
96 owns = true;
97 }
98
99 ManagedPtr(const ManagedPtr& other)
100 {
101 copy_internal(other);
102 }
103
104 void operator=(const ManagedPtr& other)
105 {
106 if (owns)
107 delete f;
108 copy_internal(other);
109 }
110
111 Factory* operator->() { return f; }
112 Factory* get() { return f; }
113 };
114
115 // This is a filter container.
116 SrtPacketFilterBase* m_filter;
117 void Check()
118 {
119 #if ENABLE_DEBUG
120 if (!m_filter)
121 abort();
122 #endif
123 // Don't do any check for now.
124 }
125
126 friend class Internal;
127
128 public:
129 class Internal
130 {
131 public:
132
133 // The list of builtin names that are reserved.
134 std::set<std::string> m_builtin_filters;
135
136 // Temporarily changed to linear searching, until this is exposed
137 // for a user-defined filter.
138 typedef std::map<std::string, ManagedPtr> filters_map_t;
139 filters_map_t m_filters;
140
141
142 public:
143
144 bool IsBuiltin(const std::string& s)
145 {
146 return m_builtin_filters.count(s);
147 }
148
149 template <class NewFilter>
150 bool add(const std::string& name)
151 {
152 if (IsBuiltin(name))
153 return false;
154
155 m_filters[name] = new Creator<NewFilter>;
156 return true;
157 }
158
159 Factory* find(const std::string& type)
160 {
161 filters_map_t::iterator i = m_filters.find(type);
162 if (i == m_filters.end())
163 return NULL; // No matter what to return - this is "undefined behavior" to be prevented
164 return i->second.get();
165 }
166 bool ParseConfig(const std::string& s, SrtFilterConfig& out, PacketFilter::Factory** ppf = NULL);
167 bool CheckFilterCompat(srt::SrtFilterConfig& w_agent, const srt::SrtFilterConfig& peer);
168 public:
169 Internal();
170 };
171 static Internal& internal(); // Singleton accessor
172
173 // Filter is optional, so this check should be done always
174 // manually.
175 bool installed() const { return m_filter; }
176 operator bool() const { return installed(); }
177
178 SrtPacketFilterBase* operator->() { Check(); return m_filter; }
179
180 // In the beginning it's initialized as first, builtin default.
181 // Still, it will be created only when requested.
182 PacketFilter(): m_filter(), m_parent(), m_sndctlpkt(0), m_unitq() {}
183
184 // Copy constructor - important when listener-spawning
185 // Things being done:
186 // 1. The filter is individual, so don't copy it. Set NULL.
187 // 2. This will be configured anyway basing on possibly a new rule set.
188 PacketFilter(const PacketFilter& source SRT_ATR_UNUSED): m_filter(), m_parent(), m_sndctlpkt(0), m_unitq() {}
189
190 // This function will be called by the parent CUDT
191 // in appropriate time. It should select appropriate
192 // filter basing on the value in selector, then
193 // pin oneself in into CUDT for receiving event signals.
194 bool configure(CUDT* parent, CUnitQueue* uq, const std::string& confstr);
195
196 static bool correctConfig(const SrtFilterConfig& c);
197
198 // Will delete the pinned in filter object.
199 // This must be defined in *.cpp file due to virtual
200 // destruction.
201 ~PacketFilter();
202
203 // Simple wrappers
204 void feedSource(CPacket& w_packet) { SRT_ASSERT(m_filter); return m_filter->feedSource((w_packet)); }
205 SRT_ARQLevel arqLevel() { SRT_ASSERT(m_filter); return m_filter->arqLevel(); }
206 bool packControlPacket(int32_t seq, int kflg, CPacket& w_packet);
207 void receive(CUnit* unit, std::vector<CUnit*>& w_incoming, loss_seqs_t& w_loss_seqs);
208
209 protected:
210 PacketFilter& operator=(const PacketFilter& p);
211 void InsertRebuilt(std::vector<CUnit*>& incoming, CUnitQueue* uq);
212
213 CUDT* m_parent;
214
215 // Sender part
216 SrtPacket m_sndctlpkt;
217
218 // Receiver part
219 CUnitQueue* m_unitq;
220 std::vector<SrtPacket> m_provided;
221 };
222
223 inline bool CheckFilterCompat(SrtFilterConfig& w_agent, const SrtFilterConfig& peer)
224 {
225 return PacketFilter::internal().CheckFilterCompat((w_agent), peer);
226 }
227
228 } // namespace srt
229
230 #endif