//=========================================================================== // $Name: arts++-1-1-a13 $ // $Id: ArtsBgp4AsPathSegment.cc,v 1.2 2004/04/21 23:51:31 kkeys Exp $ //=========================================================================== // Copyright Notice // // By accessing this software, arts++, you are duly informed // of and agree to be bound by the conditions described below in this // notice: // // This software product, arts++, is developed by Daniel W. McRobb, and // copyrighted(C) 1998 by the University of California, San Diego // (UCSD), with all rights reserved. UCSD administers the CAIDA grant, // NCR-9711092, under which part of this code was developed. // // There is no charge for arts++ software. You can redistribute it // and/or modify it under the terms of the GNU Lesser General Public // License, Version 2.1, February 1999, which is incorporated by // reference herein. // // arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use // of it will not infringe on any third party's intellectual // property rights. // // You should have received a copy of the GNU Lesser General Public // License along with arts++. Copies can also be obtained from: // // http://www.gnu.org/copyleft/lesser.html // // or by writing to: // // Free Software Foundation, Inc. // 59 Temple Place, Suite 330 // Boston, MA 02111-1307 // USA // // Or contact: // // info@caida.org //=========================================================================== extern "C" { #include "artslocal.h" } #include #if defined(HAVE_SSTREAM) #include #elif defined(HAVE_STRSTREAM) #include #else #include #endif #include #include "ArtsPrimitive.hh" #include "ArtsBgp4AsPathSegment.hh" using namespace std; static const std::string rcsid = "@(#) $Name: arts++-1-1-a13 $ $Id: ArtsBgp4AsPathSegment.cc,v 1.2 2004/04/21 23:51:31 kkeys Exp $"; //---------------------------------------------------------------------------- // ArtsBgp4AsPathSegment::ArtsBgp4AsPathSegment() //............................................................................ // //---------------------------------------------------------------------------- ArtsBgp4AsPathSegment::ArtsBgp4AsPathSegment() { this->_type = 0; #ifndef NDEBUG ++_numObjects; #endif } //---------------------------------------------------------------------------- // ArtsBgp4AsPathSegment:: // ArtsBgp4AsPathSegment(const ArtsBgp4AsPathSegment & asPathSegment) //............................................................................ // //---------------------------------------------------------------------------- ArtsBgp4AsPathSegment:: ArtsBgp4AsPathSegment(const ArtsBgp4AsPathSegment & asPathSegment) { this->_type = asPathSegment.Type(); this->_AS = asPathSegment.AS(); } //---------------------------------------------------------------------------- // ArtsBgp4AsPathSegment::~ArtsBgp4AsPathSegment() //............................................................................ // //---------------------------------------------------------------------------- ArtsBgp4AsPathSegment::~ArtsBgp4AsPathSegment() { #ifndef NDEBUG if (_numObjects > 0) --_numObjects; #endif if (! (*this)._AS.empty()) (*this)._AS.erase((*this)._AS.begin(),(*this)._AS.end()); } //------------------------------------------------------------------------- // uint8_t ArtsBgp4AsPathSegment::Type() const //......................................................................... // //------------------------------------------------------------------------- uint8_t ArtsBgp4AsPathSegment::Type() const { return(this->_type); } //------------------------------------------------------------------------- // uint8_t ArtsBgp4AsPathSegment::Type(uint8_t type) //......................................................................... // //------------------------------------------------------------------------- uint8_t ArtsBgp4AsPathSegment::Type(uint8_t type) { this->_type = type; return(this->_type); } //------------------------------------------------------------------------- // vector & ArtsBgp4AsPathSegment::AS() const //......................................................................... // //------------------------------------------------------------------------- vector & ArtsBgp4AsPathSegment::AS() const { return(this->_AS); } //---------------------------------------------------------------------------- // istream & ArtsBgp4AsPathSegment::read(istream & is, uint8_t version) //............................................................................ // //---------------------------------------------------------------------------- istream & ArtsBgp4AsPathSegment::read(istream & is, uint8_t version) { uint8_t numAses; uint16_t as; is.read((char*)&this->_type,sizeof(this->_type)); is.read((char*)&numAses,sizeof(numAses)); if (numAses > 0) { this->_AS.reserve(numAses); for (int asNum = 0; asNum < numAses; asNum++) { g_ArtsLibInternal_Primitive.ReadUint16(is,as,sizeof(as)); this->_AS.push_back(as); } } return(is); } //---------------------------------------------------------------------------- // int ArtsBgp4AsPathSegment::read(int fd, uint8_t version) //............................................................................ // //---------------------------------------------------------------------------- int ArtsBgp4AsPathSegment::read(int fd, uint8_t version) { int rc; uint8_t numAses; uint16_t as; int bytesRead = 0; rc = g_ArtsLibInternal_Primitive.FdRead(fd,&this->_type,sizeof(this->_type)); if (rc < sizeof(this->_type)) return(-1); bytesRead += rc; rc = g_ArtsLibInternal_Primitive.FdRead(fd,&numAses,sizeof(numAses)); if (rc < sizeof(numAses)) return(-1); bytesRead += rc; if (numAses > 0) { this->_AS.reserve(numAses); for (int asNum = 0; asNum < numAses; asNum++) { rc = g_ArtsLibInternal_Primitive.ReadUint16(fd,as,sizeof(as)); if (rc < sizeof(as)) return(-1); bytesRead += rc; this->_AS.push_back(as); } } return(bytesRead); } //---------------------------------------------------------------------------- // ostream & ArtsBgp4AsPathSegment::write(ostream & os, // uint8_t version) const //............................................................................ // //---------------------------------------------------------------------------- ostream & ArtsBgp4AsPathSegment::write(ostream & os, uint8_t version) const { os.write((char*)&this->_type,sizeof(this->_type)); uint8_t numAses = this->_AS.size(); os.write((char*)&numAses,sizeof(numAses)); for (int asNum = 0; asNum < numAses; asNum++) { g_ArtsLibInternal_Primitive.WriteUint16(os,this->_AS[asNum], sizeof(uint16_t)); } return(os); } //---------------------------------------------------------------------------- // int ArtsBgp4AsPathSegment::write(int fd, uint8_t version) const //............................................................................ // //---------------------------------------------------------------------------- int ArtsBgp4AsPathSegment::write(int fd, uint8_t version) const { int rc; uint8_t numAses; int bytesWritten = 0; rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&this->_type, sizeof(this->_type)); if (rc < sizeof(this->_type)) return(-1); bytesWritten += rc; numAses = this->_AS.size(); rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&numAses,sizeof(numAses)); if (rc < sizeof(numAses)) return(-1); bytesWritten += rc; for (int asNum = 0; asNum < numAses; asNum++) { rc = g_ArtsLibInternal_Primitive.WriteUint16(fd,this->_AS[asNum], sizeof(uint16_t)); if (rc < sizeof(uint16_t)) return(-1); bytesWritten += rc; } return(rc); } //---------------------------------------------------------------------------- // uint32_t ArtsBgp4AsPathSegment::Length(uint8_t version) const //............................................................................ // //---------------------------------------------------------------------------- uint32_t ArtsBgp4AsPathSegment::Length(uint8_t version) const { return(sizeof(this->_type) + sizeof(uint8_t) + (sizeof(uint16_t) * this->_AS.size())); } //---------------------------------------------------------------------------- // void ArtsBgp4AsPathSegment::AddAs(uint16_t as) //............................................................................ // //---------------------------------------------------------------------------- void ArtsBgp4AsPathSegment::AddAs(uint16_t as) { this->_AS.push_back(as); return; } //---------------------------------------------------------------------------- // void ArtsBgp4AsPathSegment::Unique() //............................................................................ // //---------------------------------------------------------------------------- void ArtsBgp4AsPathSegment::Unique() { // don't muck with AS_SET segments if (this->_type == k_segmentTypeSet) return; // remove adjacent duplicate AS numbers in AS path segment vector::iterator newSegmentEnd; newSegmentEnd = unique(this->_AS.begin(),this->_AS.end()); this->_AS.erase(newSegmentEnd,this->_AS.end()); return; } //---------------------------------------------------------------------------- // ostream & // operator << (ostream & os, // const ArtsBgp4AsPathSegment & bgp4AsPathSegment) //............................................................................ // //---------------------------------------------------------------------------- ostream & operator << (ostream & os, const ArtsBgp4AsPathSegment & bgp4AsPathSegment) { if (bgp4AsPathSegment.AS().size() < 1) return(os); vector::iterator asIter; asIter = bgp4AsPathSegment.AS().begin(); if (bgp4AsPathSegment.Type() == ArtsBgp4AsPathSegment::k_segmentTypeSet) { os << "[" << (*asIter); } else { os << (*asIter); } for (asIter++; asIter != bgp4AsPathSegment.AS().end(); asIter++) { os << " " << (*asIter); } if (bgp4AsPathSegment.Type() == ArtsBgp4AsPathSegment::k_segmentTypeSet) { os << "]"; } return(os); } //---------------------------------------------------------------------------- // ArtsBgp4AsPathSegment & // ArtsBgp4AsPathSegment::operator = // (const ArtsBgp4AsPathSegment & asPathSegment) //............................................................................ // //---------------------------------------------------------------------------- ArtsBgp4AsPathSegment & ArtsBgp4AsPathSegment::operator = (const ArtsBgp4AsPathSegment & asPathSegment) { this->_type = asPathSegment.Type(); this->_AS.reserve(asPathSegment.AS().size()); this->_AS = asPathSegment.AS(); return(*this); } uint32_t ArtsBgp4AsPathSegment::_numObjects = 0;