//=========================================================================== // @(#) $Name: arts++-1-1-a12 $ // @(#) $Id: ArtsNextHopTableEntry.cc,v 1.3 2004/10/22 18:26:27 rkoga 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 #include #include #include #include #include } #include #include "ArtsPrimitive.hh" #include "ArtsNextHopTableEntry.hh" using namespace std; static const std::string rcsid = "@(#) $Name: arts++-1-1-a12 $ $Id: ArtsNextHopTableEntry.cc,v 1.3 2004/10/22 18:26:27 rkoga Exp $"; //------------------------------------------------------------------------- // ArtsNextHopTableEntry::ArtsNextHopTableEntry() //------------------------------------------------------------------------- ArtsNextHopTableEntry::ArtsNextHopTableEntry() { this->_descriptor = 0; this->_ipAddr = 0; this->_pkts = 0; this->_bytes = 0; #ifndef NDEBUG ++ArtsNextHopTableEntry::_numObjects; #endif } //---------------------------------------------------------------------------- // ArtsNextHopTableEntry:: // ArtsNextHopTableEntry(const ArtsNextHopTableEntry & nexthopEntry) //............................................................................ // //---------------------------------------------------------------------------- ArtsNextHopTableEntry:: ArtsNextHopTableEntry(const ArtsNextHopTableEntry & nexthopEntry) { this->_descriptor = nexthopEntry.Descriptor(); this->_ipAddr = nexthopEntry.IpAddr(); this->_pkts = nexthopEntry.Pkts(); this->_bytes = nexthopEntry.Bytes(); #ifndef NDEBUG ++ArtsNextHopTableEntry::_numObjects; #endif } //------------------------------------------------------------------------- // ArtsNextHopTableEntry::~ArtsNextHopTableEntry() //------------------------------------------------------------------------- ArtsNextHopTableEntry::~ArtsNextHopTableEntry() { #ifndef NDEBUG --ArtsNextHopTableEntry::_numObjects; #endif } //------------------------------------------------------------------------- // ipv4addr_t ArtsNextHopTableEntry::IpAddr() const //------------------------------------------------------------------------- ipv4addr_t ArtsNextHopTableEntry::IpAddr() const { return(this->_ipAddr); } //------------------------------------------------------------------------- // ipv4addr_t ArtsNextHopTableEntry::IpAddr(ipv4addr_t ipAddr) //------------------------------------------------------------------------- ipv4addr_t ArtsNextHopTableEntry::IpAddr(ipv4addr_t ipAddr) { this->_ipAddr = ipAddr; return(this->_ipAddr); } //------------------------------------------------------------------------- // uint64_t ArtsNextHopTableEntry::Pkts() const //------------------------------------------------------------------------- uint64_t ArtsNextHopTableEntry::Pkts() const { return(this->_pkts); } //------------------------------------------------------------------------- // uint64_t ArtsNextHopTableEntry::Pkts(uint64_t pkts) //------------------------------------------------------------------------- uint64_t ArtsNextHopTableEntry::Pkts(uint64_t pkts) { this->_pkts = pkts; if (pkts > (uint64_t)0xffffffff) { this->_descriptor = (this->_descriptor & 0xc7) | 0x38; // use 8 bytes } else if (pkts > (uint64_t)0xffff) { this->_descriptor = (this->_descriptor & 0xc7) | 0x18; // use 4 bytes } else if (pkts > (uint64_t)0xff) { this->_descriptor = (this->_descriptor & 0xc7) | 0x08; // use 2 bytes } else { this->_descriptor = (this->_descriptor & 0xc7); // use 1 byte } return(this->_pkts); } //------------------------------------------------------------------------- // uint64_t ArtsNextHopTableEntry::Bytes() const //------------------------------------------------------------------------- uint64_t ArtsNextHopTableEntry::Bytes() const { return(this->_bytes); } //------------------------------------------------------------------------- // uint64_t ArtsNextHopTableEntry::Bytes(uint64_t bytes) //------------------------------------------------------------------------- uint64_t ArtsNextHopTableEntry::Bytes(uint64_t bytes) { this->_bytes = bytes; if (bytes > (uint64_t)0xffffffff) { this->_descriptor = (this->_descriptor & 0xf8) | 0x07; // use 8 bytes } else if (bytes > (uint64_t)0xffff) { this->_descriptor = (this->_descriptor & 0xf8) | 0x03; // use 4 bytes } else if (bytes > (uint64_t)0xff) { this->_descriptor = (this->_descriptor & 0xf8) | 0x01; // use 2 bytes } else { this->_descriptor = (this->_descriptor & 0xf8); // use 1 byte } return(this->_bytes); } //------------------------------------------------------------------------- // uint8_t ArtsNextHopTableEntry::Descriptor() const //------------------------------------------------------------------------- uint8_t ArtsNextHopTableEntry::Descriptor() const { return(this->_descriptor); } //------------------------------------------------------------------------- // uint8_t ArtsNextHopTableEntry::Descriptor(uint8_t descriptor) //------------------------------------------------------------------------- uint8_t ArtsNextHopTableEntry::Descriptor(uint8_t descriptor) { this->_descriptor = descriptor; return(this->_descriptor); } //------------------------------------------------------------------------- // uint32_t ArtsNextHopTableEntry::Length(uint8_t version) const //------------------------------------------------------------------------- uint32_t ArtsNextHopTableEntry::Length(uint8_t version) const { uint32_t len = 0; len += sizeof(this->_ipAddr); len += sizeof(this->_descriptor); len += (((this->_descriptor >> 3) + 1) + ((this->_descriptor & 0x07) + 1)); return(len); } //------------------------------------------------------------------------- // istream& ArtsNextHopTableEntry::read(istream& is, uint8_t version) //------------------------------------------------------------------------- istream& ArtsNextHopTableEntry::read(istream& is, uint8_t version) { uint8_t fieldLen; // IP address is.read((char*)&this->_ipAddr,sizeof(this->_ipAddr)); // descriptor is.read((char*)&this->_descriptor,sizeof(this->_descriptor)); // pkts fieldLen = (this->_descriptor >> 3) + 1; g_ArtsLibInternal_Primitive.ReadUint64(is,this->_pkts,fieldLen); // bytes fieldLen = (this->_descriptor & 0x07) + 1; g_ArtsLibInternal_Primitive.ReadUint64(is,this->_bytes,fieldLen); return(is); } //------------------------------------------------------------------------- // int ArtsNextHopTableEntry::read(int fd, uint8_t version) //------------------------------------------------------------------------- int ArtsNextHopTableEntry::read(int fd, uint8_t version) { uint8_t fieldLen; int rc = 0; // IP address rc += g_ArtsLibInternal_Primitive.FdRead(fd,&this->_ipAddr, sizeof(this->_ipAddr)); // descriptor rc += g_ArtsLibInternal_Primitive.FdRead(fd,&this->_descriptor,1); // pkts fieldLen = (this->_descriptor >> 3) + 1; rc += g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_pkts,fieldLen); // bytes fieldLen = (this->_descriptor & 0x07) + 1; rc += g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_bytes,fieldLen); if (rc != (int)this->Length(version)) rc = -1; return(rc); } //------------------------------------------------------------------------- // ostream& ArtsNextHopTableEntry::write(ostream& os, // uint8_t version) const //------------------------------------------------------------------------- ostream& ArtsNextHopTableEntry::write(ostream& os, uint8_t version) const { uint8_t fieldLen; // IP address os.write((char*)&this->_ipAddr,sizeof(this->_ipAddr)); // descriptor os.write((char*)&this->_descriptor,1); // pkts fieldLen = (this->_descriptor >> 3) + 1; g_ArtsLibInternal_Primitive.WriteUint64(os,this->_pkts,fieldLen); // bytes fieldLen = (this->_descriptor & 0x07) + 1; g_ArtsLibInternal_Primitive.WriteUint64(os,this->_bytes,fieldLen); return(os); } //------------------------------------------------------------------------- // int ArtsNextHopTableEntry::write(int fd, uint8_t version) const //------------------------------------------------------------------------- int ArtsNextHopTableEntry::write(int fd, uint8_t version) const { uint8_t fieldLen; int rc = 0; int bytesRead = 0; // IP address rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&this->_ipAddr, sizeof(this->_ipAddr)); if (rc < sizeof(this->_ipAddr)) return(-1); bytesRead += rc; // descriptor rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&this->_descriptor,1); if (rc < 1) return(-1); bytesRead += rc; // pkts fieldLen = (this->_descriptor >> 3) + 1; rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_pkts,fieldLen); if (rc < fieldLen) return(-1); bytesRead += rc; // bytes fieldLen = (this->_descriptor & 0x07) + 1; rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_bytes,fieldLen); if (rc < fieldLen) return(-1); bytesRead += rc; if (bytesRead != (int)this->Length(version)) bytesRead = -1; return(bytesRead); } //------------------------------------------------------------------------- // ArtsNextHopTableEntry & // operator = (const ArtsNextHopTableEntry & artsNextHopTableEntry) //------------------------------------------------------------------------- ArtsNextHopTableEntry & ArtsNextHopTableEntry::operator = (const ArtsNextHopTableEntry & artsNextHopTableEntry) { this->_ipAddr = artsNextHopTableEntry.IpAddr(); this->_descriptor = artsNextHopTableEntry.Descriptor(); this->_pkts = artsNextHopTableEntry.Pkts(); this->_bytes = artsNextHopTableEntry.Bytes(); return(*this); } //------------------------------------------------------------------------- // ostream & // operator << (ostream& os, // const ArtsNextHopTableEntry & artsNextHopTableEntry) //------------------------------------------------------------------------- ostream & operator << (ostream& os, const ArtsNextHopTableEntry & artsNextHopTableEntry) { struct in_addr addrIn; os << "\tNEXTHOP TABLE ENTRY" << endl; addrIn.s_addr = artsNextHopTableEntry.IpAddr(); os << "\t\tIP address: " << inet_ntoa(addrIn) << endl; os << "\t\tdescriptor: 0x" << hex << (int)artsNextHopTableEntry.Descriptor() << dec << endl; os << "\t\tpkts: " << artsNextHopTableEntry.Pkts() << endl; os << "\t\tbytes: " << artsNextHopTableEntry.Bytes() << endl; return(os); } //--------------------------------------------------------------------------- // bool ArtsNextHopEntryGreaterBytes:: // operator()(const ArtsNextHopTableEntry & nextHopEntry1, // const ArtsNextHopTableEntry & nextHopEntry2) const //--------------------------------------------------------------------------- bool ArtsNextHopEntryGreaterBytes:: operator()(const ArtsNextHopTableEntry & nextHopEntry1, const ArtsNextHopTableEntry & nextHopEntry2) const { return(nextHopEntry1.Bytes() > nextHopEntry2.Bytes()); } //--------------------------------------------------------------------------- // bool ArtsNextHopEntryGreaterPkts:: // operator()(const ArtsNextHopTableEntry & protoEntry1, // const ArtsNextHopTableEntry & protoEntry2) const //--------------------------------------------------------------------------- bool ArtsNextHopEntryGreaterPkts:: operator()(const ArtsNextHopTableEntry & nextHopEntry1, const ArtsNextHopTableEntry & nextHopEntry2) const { return(nextHopEntry1.Pkts() > nextHopEntry2.Pkts()); } uint32_t ArtsNextHopTableEntry::_numObjects = 0;