reSIProcate
SIP stack, Dialog Usage Manager and related libraries
Loading...
Searching...
No Matches
Via.cxx
Go to the documentation of this file.
1#if defined(HAVE_CONFIG_H)
2#include "config.h"
3#endif
4
5#include "resip/stack/Via.hxx"
7#include "rutil/DnsUtil.hxx"
8#include "rutil/Logger.hxx"
10//#include "rutil/WinLeakCheck.hxx" // not compatible with placement new used below
11
12using namespace resip;
13using namespace std;
14
15#define RESIPROCATE_SUBSYSTEM Subsystem::SIP
16
17
18//====================
19// Via:
20//====================
22 : ParserCategory(),
23 mProtocolName(Data::Share,Symbols::ProtocolName),
24 mProtocolVersion(Data::Share,Symbols::ProtocolVersion),
25 mTransport(),
26 mSentHost(),
27 mSentPort(0)
28{
29 // insert a branch in all Vias (default constructor)
30 this->param(p_branch);
32 {
33 this->param(p_rport); // add the rport parameter by default as per rfc 3581
34 }
35}
36
38 Headers::Type type,
39 PoolBase* pool)
40 : ParserCategory(hfv, type, pool),
41 mProtocolName(Data::Share,Symbols::ProtocolName),
42 mProtocolVersion(Data::Share,Symbols::ProtocolVersion),
43 mTransport(Data::Share,Symbols::UDP), // !jf!
44 mSentHost(),
45 mSentPort(-1)
46{}
47
48Via::Via(const Via& rhs,
49 PoolBase* pool)
50 : ParserCategory(rhs, pool),
51 mProtocolName(rhs.mProtocolName),
52 mProtocolVersion(rhs.mProtocolVersion),
53 mTransport(rhs.mTransport),
54 mSentHost(rhs.mSentHost),
55 mSentPort(rhs.mSentPort)
56{
57}
58
59Via&
61{
62 if (this != &rhs)
63 {
65 mProtocolName = rhs.mProtocolName;
66 mProtocolVersion = rhs.mProtocolVersion;
67 mTransport = rhs.mTransport;
68 mSentHost = rhs.mSentHost;
69 mSentPort = rhs.mSentPort;
70 }
71 return *this;
72}
73
76{
77 return new Via(*this);
78}
79
81Via::clone(void* location) const
82{
83 return new (location) Via(*this);
84}
85
87Via::clone(PoolBase* pool) const
88{
89 return new (pool) Via(*this, pool);
90}
91
92Data&
94{
95 checkParsed();
96 return mProtocolName;
97}
98
99const Data&
101{
102 checkParsed();
103 return mProtocolName;
104}
105
106Data&
108{
109 checkParsed();
110 return mProtocolVersion;
111}
112
113const Data&
115{
116 checkParsed();
117 return mProtocolVersion;
118}
119
121transport()
122{
123 checkParsed();
124 return mTransport;
125}
126
128transport() const
129{
130 checkParsed();
131 return mTransport;
132}
133
134Data&
136{
137 checkParsed();
138 return mSentHost;
139}
140
141const Data&
143{
144 checkParsed();
145 return mSentHost;
146}
147
148int&
150{
151 checkParsed();
152 return mSentPort;
153}
154
155int
157{
158 checkParsed();
159 return mSentPort;
160}
161
162void
164{
165 const char* startMark;
166 startMark = pb.skipWhitespace();
167 static std::bitset<256> wos=Data::toBitset("\r\n\t /");
168 pb.skipToOneOf(wos);
169 pb.data(mProtocolName, startMark);
170 pb.skipToChar('/');
171 pb.skipChar();
172 startMark = pb.skipWhitespace();
173 pb.skipToOneOf(wos);
174 pb.data(mProtocolVersion, startMark);
175
176 pb.skipToChar('/');
177 pb.skipChar();
178 startMark = pb.skipWhitespace();
179
180 // !jf! this should really be skipTokenChar() since for instance, if the
181 // protocol token is missing it will read the rest of the Via into this field
182 pb.skipNonWhitespace();
183 pb.data(mTransport, startMark);
184
185 startMark = pb.skipWhitespace();
186 pb.assertNotEof();
187 if (*startMark == '[')
188 {
189 startMark = pb.skipChar();
190 pb.skipToChar(']');
191 pb.data(mSentHost, startMark);
192 // .bwc. We do not save this canonicalization, since we weren't doing so
193 // before. This may change soon.
194 Data canonicalizedHost=DnsUtil::canonicalizeIpV6Address(mSentHost);
195 if(canonicalizedHost.empty())
196 {
197 // .bwc. So the V6 addy is garbage.
198 throw ParseException("Unparsable V6 address (note, this might"
199 " be unparsable because IPV6 support is not"
200 " enabled)", "Via",
201 __FILE__,
202 __LINE__);
203 }
204 pb.skipChar();
205 }
206 else
207 {
208 // .bwc. If we hit whitespace, we have the host.
209 static std::bitset<256> delimiter=Data::toBitset(";: \t\r\n");
210 pb.skipToOneOf(delimiter);
211 pb.data(mSentHost, startMark);
212 }
213
214 pb.skipToOneOf(";:");
215
216 if (!pb.eof() && *pb.position() == ':')
217 {
218 startMark = pb.skipChar(':');
219 mSentPort = pb.integer();
220 static std::bitset<256> delimiter=Data::toBitset("; \t\r\n");
221 pb.skipToOneOf(delimiter);
222 }
223 else
224 {
225 mSentPort = 0;
226 }
227 parseParameters(pb);
228}
229
231Via::encodeParsed(EncodeStream& str) const
232{
233 str << mProtocolName << Symbols::SLASH << mProtocolVersion << Symbols::SLASH << mTransport
235
236 if (DnsUtil::isIpV6Address(mSentHost))
237 {
238 str << '[' << mSentHost << ']';
239 }
240 else
241 {
242 str << mSentHost;
243 }
244
245 if (mSentPort != 0)
246 {
247 str << Symbols::COLON << mSentPort;
248 }
249 encodeParameters(str);
250 return str;
251}
252
253ParameterTypes::Factory Via::ParameterFactories[ParameterTypes::MAX_PARAMETER]={0};
254
255Parameter*
256Via::createParam(ParameterTypes::Type type, ParseBuffer& pb, const std::bitset<256>& terminators, PoolBase* pool)
257{
258 if(type > ParameterTypes::UNKNOWN && type < ParameterTypes::MAX_PARAMETER && ParameterFactories[type])
259 {
260 return ParameterFactories[type](type, pb, terminators, pool);
261 }
262 return 0;
263}
264
265bool
266Via::exists(const Param<Via>& paramType) const
267{
268 checkParsed();
269 bool ret = getParameterByEnum(paramType.getTypeNum()) != NULL;
270 return ret;
271}
272
273void
274Via::remove(const Param<Via>& paramType)
275{
276 checkParsed();
278}
279
280#define defineParam(_enum, _name, _type, _RFC_ref_ignored) \
281_enum##_Param::DType& \
282Via::param(const _enum##_Param& paramType) \
283{ \
284 checkParsed(); \
285 _enum##_Param::Type* p = \
286 static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum())); \
287 if (!p) \
288 { \
289 p = new _enum##_Param::Type(paramType.getTypeNum()); \
290 mParameters.push_back(p); \
291 } \
292 return p->value(); \
293} \
294 \
295const _enum##_Param::DType& \
296Via::param(const _enum##_Param& paramType) const \
297{ \
298 checkParsed(); \
299 _enum##_Param::Type* p = \
300 static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum())); \
301 if (!p) \
302 { \
303 InfoLog(<< "Missing parameter " _name " " << ParameterTypes::ParameterNames[paramType.getTypeNum()]); \
304 DebugLog(<< *this); \
305 throw Exception("Missing parameter " _name, __FILE__, __LINE__); \
306 } \
307 return p->value(); \
308}
309
310defineParam(branch, "branch", BranchParameter, "RFC 3261");
311defineParam(comp, "comp", DataParameter, "RFC 3486");
312defineParam(received, "received", ReceivedParameter, "RFC 3261");
313defineParam(rport, "rport", RportParameter, "RFC 3581");
314defineParam(ttl, "ttl", UInt32Parameter, "RFC 3261");
315defineParam(sigcompId, "sigcomp-id", QuotedDataParameter, "RFC 5049");
316defineParam(maddr, "maddr", DataParameter, "RFC 3261");
317
318#undef defineParam
319
320/* ====================================================================
321 * The Vovida Software License, Version 1.0
322 *
323 * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
324 *
325 * Redistribution and use in source and binary forms, with or without
326 * modification, are permitted provided that the following conditions
327 * are met:
328 *
329 * 1. Redistributions of source code must retain the above copyright
330 * notice, this list of conditions and the following disclaimer.
331 *
332 * 2. Redistributions in binary form must reproduce the above copyright
333 * notice, this list of conditions and the following disclaimer in
334 * the documentation and/or other materials provided with the
335 * distribution.
336 *
337 * 3. The names "VOCAL", "Vovida Open Communication Application Library",
338 * and "Vovida Open Communication Application Library (VOCAL)" must
339 * not be used to endorse or promote products derived from this
340 * software without prior written permission. For written
341 * permission, please contact vocal@vovida.org.
342 *
343 * 4. Products derived from this software may not be called "VOCAL", nor
344 * may "VOCAL" appear in their name, without prior written
345 * permission of Vovida Networks, Inc.
346 *
347 * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
348 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
349 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
350 * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
351 * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
352 * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
353 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
354 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
355 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
356 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
357 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
358 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
359 * DAMAGE.
360 *
361 * ====================================================================
362 *
363 * This software consists of voluntary contributions made by Vovida
364 * Networks, Inc. and many individuals on behalf of Vovida Networks,
365 * Inc. For more information on Vovida Networks, Inc., please see
366 * <http://www.vovida.org/>.
367 *
368 */
#define defineParam(_enum, _name, _type, _RFC_ref_ignored)
Definition Auth.cxx:239
Represents the "via-branch" parameter of the RFC 3261 grammar.
Generically represents miscellaneous parameter data.
An alternative to std::string, encapsulates an arbitrary buffer of bytes.
Definition Data.hxx:90
bool empty() const noexcept
Checks whether the Data is empty.
Definition Data.hxx:591
static std::bitset< 256 > toBitset(const resip::Data &chars)
Creates a bitset reflecting the contents of this data (as a set) ie.
Definition Data.cxx:2477
static bool isIpV6Address(const Data &ipAddress)
Definition DnsUtil.cxx:386
static Data canonicalizeIpV6Address(const Data &ipV6Address)
Converts the printable form of an IPv6 address into consistent format so that string comparisons will...
Definition DnsUtil.cxx:427
static bool getRportEnabled()
void checkParsed() const
virtual ParameterTypes::Type getTypeNum() const =0
Parameter *(* Factory)(ParameterTypes::Type, ParseBuffer &, const std::bitset< 256 > &terminators, PoolBase *pool)
Provides string-parsing functionality with protection from buffer overflow.
CurrentPosition skipNonWhitespace()
CurrentPosition skipWhitespace()
CurrentPosition skipToChar(char c)
CurrentPosition position() const
void assertNotEof() const
CurrentPosition skipToOneOf(const char *cs)
CurrentPosition skipChar()
void data(Data &data, const char *start) const
make the passed in data share memory with the buffer (uses Data::Share)
BaseException used to indicate parse failures.
Base class for all SIP grammar elements that can have parameters.
Parameter * getParameterByEnum(ParameterTypes::Type type) const
ParserCategory & operator=(const ParserCategory &rhs)
void removeParameterByEnum(ParameterTypes::Type type)
void parseParameters(ParseBuffer &pb)
EncodeStream & encodeParameters(EncodeStream &str) const
Encodes parameters as they should appear on the wire.
Generically represents miscellaneous parameter data that is encoded using double quotes.
Represents the "via-received" element in the RFC 3261 grammar.
Represents the "response-port" element of the SIP grammar (as extended in RFC 3581).
static const char * SPACE
Definition Symbols.hxx:34
static const char * COLON
Definition Symbols.hxx:15
static const char * SLASH
Definition Symbols.hxx:32
Generically represents unsigned 32-bit integer values conveyed by SIP parameters.
Represents the "via-parm" element in the RFC 3261 grammar.
Definition Via.hxx:18
void remove(const Param< Via > &paramType)
Definition Via.cxx:274
Data & transport()
Definition Via.cxx:121
virtual void parse(ParseBuffer &pb)
Definition Via.cxx:163
virtual ParserCategory * clone() const
Definition Via.cxx:75
Data & protocolName()
Definition Via.cxx:93
Via & operator=(const Via &)
Definition Via.cxx:60
bool exists(const Param< Via > &paramType) const
Definition Via.cxx:266
Data & protocolVersion()
Definition Via.cxx:107
virtual Parameter * createParam(ParameterTypes::Type type, ParseBuffer &pb, const std::bitset< 256 > &terminators, PoolBase *pool)
Definition Via.cxx:256
virtual EncodeStream & encodeParsed(EncodeStream &str) const
Definition Via.cxx:231
int & sentPort()
Definition Via.cxx:149
Data & sentHost()
Definition Via.cxx:135
const Data & param(const ExtensionParameter &param) const
Const accessor for non-natively-supported parameter types.
dcm! – add UnusedChecking(_enum) below;
#define EncodeStream