reSIProcate/stack  9694
Token.cxx
Go to the documentation of this file.
00001 #if defined(HAVE_CONFIG_H)
00002 #include "config.h"
00003 #endif
00004 
00005 #include "resip/stack/Token.hxx"
00006 #include "rutil/Data.hxx"
00007 #include "rutil/DnsUtil.hxx"
00008 #include "rutil/Logger.hxx"
00009 #include "rutil/ParseBuffer.hxx"
00010 //#include "rutil/WinLeakCheck.hxx"  // not compatible with placement new used below
00011 
00012 using namespace resip;
00013 using namespace std;
00014 
00015 #define RESIPROCATE_SUBSYSTEM Subsystem::SIP
00016 
00017 
00018 //====================
00019 // Token
00020 //===================
00021 Token::Token() 
00022    : ParserCategory(), 
00023      mValue() 
00024 {}
00025   
00026 Token::Token(const Data& d) 
00027    : ParserCategory(),
00028      mValue(d) 
00029 {}
00030 
00031 Token::Token(const HeaderFieldValue& hfv, Headers::Type type, PoolBase* pool) 
00032    : ParserCategory(hfv, type, pool), 
00033      mValue() 
00034 {}
00035 
00036 Token::Token(const Token& rhs, PoolBase* pool)
00037    : ParserCategory(rhs, pool),
00038      mValue(rhs.mValue)
00039 {}
00040 
00041 Token&
00042 Token::operator=(const Token& rhs)
00043 {
00044    if (this != &rhs)
00045    {
00046       ParserCategory::operator=(rhs);
00047       mValue = rhs.mValue;
00048    }
00049    return *this;
00050 }
00051 
00052 bool
00053 Token::isEqual(const Token& rhs) const
00054 {
00055    return (value() == rhs.value());
00056 }
00057 
00058 bool
00059 Token::operator==(const Token& rhs) const
00060 {
00061    return (value() == rhs.value());
00062 }
00063 
00064 bool
00065 Token::operator!=(const Token& rhs) const
00066 {
00067    return (value() != rhs.value());
00068 }
00069 
00070 bool
00071 Token::operator<(const Token& rhs) const
00072 {
00073    return (value() < rhs.value());
00074 }
00075 
00076 const Data& 
00077 Token::value() const 
00078 {
00079    checkParsed(); 
00080    return mValue;
00081 }
00082 
00083 Data& 
00084 Token::value()
00085 {
00086    checkParsed(); 
00087    return mValue;
00088 }
00089 
00090 void
00091 Token::parse(ParseBuffer& pb)
00092 {
00093    const char* startMark = pb.skipWhitespace();
00094    pb.skipToOneOf(ParseBuffer::Whitespace, Symbols::SEMI_COLON);
00095    pb.data(mValue, startMark);
00096    pb.skipToChar(Symbols::SEMI_COLON[0]);
00097    parseParameters(pb);
00098 }
00099 
00100 ParserCategory* 
00101 Token::clone() const
00102 {
00103    return new Token(*this);
00104 }
00105 
00106 ParserCategory* 
00107 Token::clone(void* location) const
00108 {
00109    return new (location) Token(*this);
00110 }
00111 
00112 ParserCategory* 
00113 Token::clone(PoolBase* pool) const
00114 {
00115    return new (pool) Token(*this, pool);
00116 }
00117 
00118 EncodeStream& 
00119 Token::encodeParsed(EncodeStream& str) const
00120 {
00121    str << mValue;
00122    encodeParameters(str);
00123    return str;
00124 }
00125 
00126 ParameterTypes::Factory Token::ParameterFactories[ParameterTypes::MAX_PARAMETER]={0};
00127 
00128 Parameter* 
00129 Token::createParam(ParameterTypes::Type type, ParseBuffer& pb, const std::bitset<256>& terminators, PoolBase* pool)
00130 {
00131    if(type > ParameterTypes::UNKNOWN && type < ParameterTypes::MAX_PARAMETER && ParameterFactories[type])
00132    {
00133       return ParameterFactories[type](type, pb, terminators, pool);
00134    }
00135    return 0;
00136 }
00137 
00138 bool 
00139 Token::exists(const Param<Token>& paramType) const
00140 {
00141     checkParsed();
00142     bool ret = getParameterByEnum(paramType.getTypeNum()) != NULL;
00143     return ret;
00144 }
00145 
00146 void 
00147 Token::remove(const Param<Token>& paramType)
00148 {
00149     checkParsed();
00150     removeParameterByEnum(paramType.getTypeNum());
00151 }
00152 
00153 #define defineParam(_enum, _name, _type, _RFC_ref_ignored)                                                      \
00154 _enum##_Param::DType&                                                                                           \
00155 Token::param(const _enum##_Param& paramType)                                                           \
00156 {                                                                                                               \
00157    checkParsed();                                                                                               \
00158    _enum##_Param::Type* p =                                                                                     \
00159       static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum()));                            \
00160    if (!p)                                                                                                      \
00161    {                                                                                                            \
00162       p = new _enum##_Param::Type(paramType.getTypeNum());                                                      \
00163       mParameters.push_back(p);                                                                                 \
00164    }                                                                                                            \
00165    return p->value();                                                                                           \
00166 }                                                                                                               \
00167                                                                                                                 \
00168 const _enum##_Param::DType&                                                                                     \
00169 Token::param(const _enum##_Param& paramType) const                                                     \
00170 {                                                                                                               \
00171    checkParsed();                                                                                               \
00172    _enum##_Param::Type* p =                                                                                     \
00173       static_cast<_enum##_Param::Type*>(getParameterByEnum(paramType.getTypeNum()));                            \
00174    if (!p)                                                                                                      \
00175    {                                                                                                            \
00176       InfoLog(<< "Missing parameter " _name " " << ParameterTypes::ParameterNames[paramType.getTypeNum()]);     \
00177       DebugLog(<< *this);                                                                                       \
00178       throw Exception("Missing parameter " _name, __FILE__, __LINE__);                                          \
00179    }                                                                                                            \
00180    return p->value();                                                                                           \
00181 }
00182 
00183 defineParam(text, "text", ExistsOrDataParameter, "RFC 3840");
00184 defineParam(dAlg, "d-alg", DataParameter, "RFC 3329");
00185 defineParam(dQop, "d-qop", DataParameter, "RFC 3329");
00186 defineParam(dVer, "d-ver", QuotedDataParameter, "RFC 3329");
00187 defineParam(expires, "expires", UInt32Parameter, "RFC 3261");
00188 defineParam(filename, "filename", DataParameter, "RFC 2183");
00189 defineParam(fromTag, "from-tag", DataParameter, "RFC 4235");
00190 defineParam(handling, "handling", DataParameter, "RFC 3261");
00191 defineParam(id, "id", DataParameter, "RFC 3265");
00192 defineParam(q, "q", QValueParameter, "RFC 3261");
00193 defineParam(reason, "reason", DataParameter, "RFC 3265");
00194 defineParam(retryAfter, "retry-after", UInt32Parameter, "RFC 3265");
00195 defineParam(toTag, "to-tag", DataParameter, "RFC 4235");
00196 defineParam(extension, "ext", DataParameter, "RFC 3966"); // Token is used when ext is a user-parameter
00197 defineParam(profileType, "profile-type", DataParameter, "RFC 6080");
00198 defineParam(vendor, "vendor", QuotedDataParameter, "RFC 6080");
00199 defineParam(model, "model", QuotedDataParameter, "RFC 6080");
00200 defineParam(version, "version", QuotedDataParameter, "RFC 6080");
00201 defineParam(effectiveBy, "effective-by", UInt32Parameter, "RFC 6080");
00202 defineParam(document, "document", DataParameter, "draft-ietf-sipping-config-framework-07 (removed in 08)");
00203 defineParam(appId, "app-id", DataParameter, "draft-ietf-sipping-config-framework-05 (renamed to auid in 06, which was then removed in 08)");
00204 defineParam(networkUser, "network-user", DataParameter, "draft-ietf-sipping-config-framework-11 (removed in 12)");
00205 defineParam(require, "require", DataParameter, "RFC 5373");
00206 
00207 #undef defineParam
00208 
00209 /* ====================================================================
00210  * The Vovida Software License, Version 1.0 
00211  * 
00212  * Copyright (c) 2000 Vovida Networks, Inc.  All rights reserved.
00213  * 
00214  * Redistribution and use in source and binary forms, with or without
00215  * modification, are permitted provided that the following conditions
00216  * are met:
00217  * 
00218  * 1. Redistributions of source code must retain the above copyright
00219  *    notice, this list of conditions and the following disclaimer.
00220  * 
00221  * 2. Redistributions in binary form must reproduce the above copyright
00222  *    notice, this list of conditions and the following disclaimer in
00223  *    the documentation and/or other materials provided with the
00224  *    distribution.
00225  * 
00226  * 3. The names "VOCAL", "Vovida Open Communication Application Library",
00227  *    and "Vovida Open Communication Application Library (VOCAL)" must
00228  *    not be used to endorse or promote products derived from this
00229  *    software without prior written permission. For written
00230  *    permission, please contact vocal@vovida.org.
00231  *
00232  * 4. Products derived from this software may not be called "VOCAL", nor
00233  *    may "VOCAL" appear in their name, without prior written
00234  *    permission of Vovida Networks, Inc.
00235  * 
00236  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
00237  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00238  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
00239  * NON-INFRINGEMENT ARE DISCLAIMED.  IN NO EVENT SHALL VOVIDA
00240  * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
00241  * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
00242  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00243  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00244  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
00245  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00246  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
00247  * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
00248  * DAMAGE.
00249  * 
00250  * ====================================================================
00251  * 
00252  * This software consists of voluntary contributions made by Vovida
00253  * Networks, Inc. and many individuals on behalf of Vovida Networks,
00254  * Inc.  For more information on Vovida Networks, Inc., please see
00255  * <http://www.vovida.org/>.
00256  *
00257  */