SdpConnection.cxx
上传用户:sy_wanhua
上传日期:2013-07-25
资源大小:3048k
文件大小:9k
- /* ====================================================================
- * The Vovida Software License, Version 1.0
- *
- * Copyright (c) 2000 Vovida Networks, Inc. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in
- * the documentation and/or other materials provided with the
- * distribution.
- *
- * 3. The names "VOCAL", "Vovida Open Communication Application Library",
- * and "Vovida Open Communication Application Library (VOCAL)" must
- * not be used to endorse or promote products derived from this
- * software without prior written permission. For written
- * permission, please contact vocal@vovida.org.
- *
- * 4. Products derived from this software may not be called "VOCAL", nor
- * may "VOCAL" appear in their name, without prior written
- * permission of Vovida Networks, Inc.
- *
- * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED
- * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
- * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, TITLE AND
- * NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT SHALL VOVIDA
- * NETWORKS, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT DAMAGES
- * IN EXCESS OF $1,000, NOR FOR ANY INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
- * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
- * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
- * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
- * DAMAGE.
- *
- * ====================================================================
- *
- * This software consists of voluntary contributions made by Vovida
- * Networks, Inc. and many individuals on behalf of Vovida Networks,
- * Inc. For more information on Vovida Networks, Inc., please see
- * <http://www.vovida.org/>.
- *
- */
- static const char* const SdpConnection_cxx_Version =
- "$Id: SdpConnection.cxx,v 1.16 2001/06/14 16:28:24 chok Exp $";
- #include <string>
- #include "support.hxx"
- #include "SdpExceptions.hxx"
- #include "SdpConnection.hxx"
- /************************ SdpConnection class methods *************************/
- ///
- SdpConnection::SdpConnection ()
- {
- addresstype = AddressTypeIPV4;
- networktype = NetworkTypeInternet;
- strcpy (address, "0.0.0.0");
- multicast = 0;
- } // SdpConnection::SdpConnection
- ///
- SdpConnection::SdpConnection (string& str)
- {
- //initialize multicast.
- multicast = 0;
- //str.erase(0,2);
- //split based on '/'
- // use split instead of sub_split.
- // -ctam
- split_t connectionList(split(str, "/"));
- if (connectionList.size() < 1)
- {
- // not enough parameters
- cpLog(LOG_ERR, "SdpConnection: parameter count < 1 on connection list");
- throw SdpExceptions(PARAM_NUMERR);
- }
- else // >=1
- {
- if (connectionList.size() > 1)
- {
- cpLog( LOG_DEBUG_STACK, "connectionList size is %d", connectionList.size() );
- cpLog( LOG_DEBUG_STACK, "Multi-cast address in %s", str.c_str() );
- //set the multicast specific parts.
- //create the multicast details.
- multicast = new SdpMulticast;
- assert(multicast);
- multicast->setTtl(atoi(connectionList[1].c_str()) );
- }
- if (connectionList.size() > 2)
- {
- assert(multicast);
- multicast->setnumAddr(atoi(connectionList[2].c_str() ));
- }
- //sub parms: get the address specific.
- //split again.
- //const char* str = connectionList[0].c_str();
- string str2 = connectionList[0].c_str();
- string addressSpec(str2);
- // take out sub_split. This may be causing corruption in the sdp.
- // -ctam
- split_t addrSpec(split(addressSpec, " "));
- // Use nTypeStr to make a copy of the addrSpec.
- string nTypeStr = addrSpec[0].c_str();
- if (strcmp(nTypeStr.c_str(), SdpNetworkTypeIN) == 0)
- {
- networktype = NetworkTypeInternet;
- }
- else
- {
- cpLog(LOG_ERR, "addressSpec: {%s}", addressSpec.c_str());
- // bad!
- // -ctam
- printf("*** nTypeStr.c_str()= <%s>, SdpNetworkTypeIN = <%s> ***n",
- nTypeStr.c_str(), SdpNetworkTypeIN );
- cpLog( LOG_ERR, "SdpConnection: unknown transport type");
- throw SdpExceptions(UNKNOWN_NETTYPE);
- }
- if (addrSpec[1] == SdpAddressTypeIP4)
- {
- addresstype = AddressTypeIPV4;
- }
- else if (addrSpec[1] == SdpAddressTypeIP6)
- {
- addresstype = AddressTypeIPV6;
- }
- else
- {
- cpLog(LOG_ERR, "SdpConnection: unknown address type");
- throw SdpExceptions(UNKNOWN_ADDRTYPE);
- }
- if (multicast)
- {
- multicast->setAddress(addrSpec[2].c_str());
- }
- else
- {
- strcpy(address, addrSpec[2].c_str());
- }
- }
- }
- ///
- SdpConnection&
- SdpConnection::operator=(const SdpConnection& connection)
- {
- cpLog( LOG_DEBUG_STACK, "SdpConnection operator=" );
- networktype = connection.getNetworkType();
- addresstype = connection.getAddressType();
- SdpMulticast* mult = connection.getMulticast();
- if (mult) //multicast address present.
- {
- cpLog( LOG_DEBUG_STACK, "Multi-cast address" );
- if (!multicast)
- {
- multicast = new SdpMulticast;
- assert(multicast);
- }
- (*multicast) = (*mult);
- }
- else
- {
- //if previously multicast is defined, delete that.
- if (multicast)
- {
- cpLog( LOG_DEBUG_STACK, "Delete multi-cast address" );
- delete multicast;
- }
- multicast = 0;
- //assign the unicast address.
- strcpy(address, connection.getUnicast().c_str());
- }
- return *(this);
- }
- /// Copy constructor
- SdpConnection::SdpConnection( const SdpConnection& connection )
- {
- cpLog( LOG_DEBUG_STACK, "SdpConnection copy constructor" );
- networktype = connection.getNetworkType();
- addresstype = connection.getAddressType();
- strcpy(address, connection.getUnicast().c_str());
- SdpMulticast* mult = connection.getMulticast();
- if ( mult )
- {
- cpLog( LOG_DEBUG_STACK, "Multi-cast address" );
- multicast = new SdpMulticast;
- assert(multicast);
- *multicast = *mult;
- }
- else
- {
- multicast = 0;
- }
- }
- ///
- string
- SdpConnection::networkTypeString ()
- {
- string s;
- switch (networktype)
- {
- case NetworkTypeInternet:
- {
- s = SdpNetworkTypeIN;
- break;
- }
- default:
- {
- //TODO Throw some exception
- break;
- }
- }
- return s;
- } // SdpConnection::networkTypeString
- ///
- string
- SdpConnection::addressTypeString ()
- {
- string s;
- switch (addresstype)
- {
- case AddressTypeIPV4:
- {
- s = SdpAddressTypeIP4;
- break;
- }
- case AddressTypeIPV6:
- {
- s = SdpAddressTypeIP6;
- break;
- }
- default:
- {
- //TODO Throw some exception
- break;
- }
- }
- return s;
- } // SdpConnection::addressTypeString
- ///
- void
- SdpConnection::encode (ostrstream& s)
- {
- s << "c=" << networkTypeString()
- << ' '
- << addressTypeString()
- << ' ';
- if (multicast)
- {
- multicast->encode (s);
- }
- else
- {
- s << address;
- }
- s << "rn";
- } // SdpConnection::encode
- ///
- void
- SdpConnection::setHold ()
- {
- if ( !multicast )
- {
- // We are copying a fixed string, hence no need for
- // those init buffer & copy with strncpy(..) overhead
- strcpy(address, "0.0.0.0");
- }
- else
- {
- multicast->setAddress("0.0.0.0");
- }
- }
- ///
- bool
- SdpConnection::isHold ()
- {
- string addr;
- if ( !multicast )
- {
- addr = getUnicast();
- }
- else
- {
- addr = multicast->getAddress();
- }
- return (addr == "0.0.0.0") ? true : false;
- }
- /************************** SdpMulticast class methods *************************/
- SdpMulticast::SdpMulticast() :
- numAddress( 0 )
- {}
- ///
- void
- SdpMulticast::encode (ostrstream& s)
- {
- s << address << '/' << ttl;
- if (numAddress > 0)
- {
- s << '/' << numAddress;
- }
- } // SdpMulticast::encode