//=========================================================================== // $Name: arts++-1-1-a12 $ // $Id: ArtsInterfaceMatrixEntry.cc,v 1.2 2004/04/21 23:51:32 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 //=========================================================================== #include "ArtsInterfaceMatrixEntry.hh" using namespace std; //------------------------------------------------------------------------- // ArtsInterfaceMatrixEntry::ArtsInterfaceMatrixEntry() //......................................................................... // //------------------------------------------------------------------------- ArtsInterfaceMatrixEntry::ArtsInterfaceMatrixEntry() { this->_descriptor = 0; this->_src = 0; this->_dst = 0; this->_pkts = 0; this->_bytes = 0; #ifndef NDEBUG ++_numObjects; #endif } //------------------------------------------------------------------------- // ArtsInterfaceMatrixEntry::~ArtsInterfaceMatrixEntry() //......................................................................... // //------------------------------------------------------------------------- ArtsInterfaceMatrixEntry::~ArtsInterfaceMatrixEntry() { #ifndef NDEBUG if (_numObjects > 0) --_numObjects; #endif } //------------------------------------------------------------------------- // uint8_t ArtsInterfaceMatrixEntry::Descriptor() const //......................................................................... // //------------------------------------------------------------------------- uint8_t ArtsInterfaceMatrixEntry::Descriptor() const { return(this->_descriptor); } //------------------------------------------------------------------------- // uint8_t ArtsInterfaceMatrixEntry::Descriptor(uint8_t descriptor) //......................................................................... // //------------------------------------------------------------------------- uint8_t ArtsInterfaceMatrixEntry::Descriptor(uint8_t descriptor) { this->_descriptor = descriptor; return(this->_descriptor); } //------------------------------------------------------------------------- // uint16_t ArtsInterfaceMatrixEntry::Src() const //......................................................................... // //------------------------------------------------------------------------- uint16_t ArtsInterfaceMatrixEntry::Src() const { return(this->_src); } //------------------------------------------------------------------------- // uint16_t ArtsInterfaceMatrixEntry::Src(uint16_t src) //......................................................................... // //------------------------------------------------------------------------- uint16_t ArtsInterfaceMatrixEntry::Src(uint16_t src) { this->_src = src; return(this->_src); } //------------------------------------------------------------------------- // uint16_t ArtsInterfaceMatrixEntry::Dst() const //......................................................................... // //------------------------------------------------------------------------- uint16_t ArtsInterfaceMatrixEntry::Dst() const { return(this->_dst); } //------------------------------------------------------------------------- // uint16_t ArtsInterfaceMatrixEntry::Dst(uint16_t dst) //......................................................................... // //------------------------------------------------------------------------- uint16_t ArtsInterfaceMatrixEntry::Dst(uint16_t dst) { this->_dst = dst; return(this->_dst); } //------------------------------------------------------------------------- // uint64_t ArtsInterfaceMatrixEntry::Pkts() const //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsInterfaceMatrixEntry::Pkts() const { return(this->_pkts); } //------------------------------------------------------------------------- // uint64_t ArtsInterfaceMatrixEntry::Pkts(uint64_t pkts) //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsInterfaceMatrixEntry::Pkts(uint64_t pkts) { this->_pkts = pkts; if (pkts > (uint64_t)0xffffffff) { this->_descriptor = (this->_descriptor | 0x1c); } else if (pkts > (uint64_t)0xffff) { this->_descriptor = (this->_descriptor & 0xe3) | (0x0003 << 2); } else if (pkts > (uint64_t)0xff) { this->_descriptor = (this->_descriptor & 0xe3) | (0x0001 << 2); } else { this->_descriptor = (this->_descriptor & 0xe3); } return(this->_pkts); } //------------------------------------------------------------------------- // uint64_t ArtsInterfaceMatrixEntry::Bytes() const //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsInterfaceMatrixEntry::Bytes() const { return(this->_bytes); } //------------------------------------------------------------------------- // uint64_t ArtsInterfaceMatrixEntry::Bytes(uint64_t bytes) //......................................................................... // //------------------------------------------------------------------------- uint64_t ArtsInterfaceMatrixEntry::Bytes(uint64_t bytes) { this->_bytes = bytes; if (bytes > (uint64_t)0xffffffff) { this->_descriptor = (this->_descriptor | 0xe0); } else if (bytes > (uint64_t)0xffff) { this->_descriptor = (this->_descriptor & 0x1f) | (0x0003 << 5); } else if (bytes > (uint64_t)0xff) { this->_descriptor = (this->_descriptor & 0x1f) | (0x0001 << 5); } else { this->_descriptor = (this->_descriptor & 0x1f); } return(this->_bytes); } //------------------------------------------------------------------------- // uint32_t ArtsInterfaceMatrixEntry::Length(uint8_t version) const //......................................................................... // //------------------------------------------------------------------------- uint32_t ArtsInterfaceMatrixEntry::Length(uint8_t version) const { uint32_t length; length = (sizeof(this->_descriptor) + (this->_descriptor & 0x01) + 1 + ((this->_descriptor >> 1) & 0x01) + 1 + ((this->_descriptor >> 2) & 0x07) + 1 + ((this->_descriptor >> 5) & 0x07) + 1); return(length); } //------------------------------------------------------------------------- // istream & ArtsInterfaceMatrixEntry::read(istream& is, uint8_t version) //......................................................................... // //------------------------------------------------------------------------- istream & ArtsInterfaceMatrixEntry::read(istream& is, uint8_t version) { uint8_t bytesize, pktsize, srcsize, dstsize; is.read((char*)&this->_descriptor,sizeof(this->_descriptor)); srcsize = (this->_descriptor & 0x01) + 1; dstsize = ((this->_descriptor >> 1) & 0x01) + 1; pktsize = ((this->_descriptor >> 2) & 0x07) + 1; bytesize = ((this->_descriptor >> 5) & 0x07) + 1; g_ArtsLibInternal_Primitive.ReadUint16(is,this->_src,srcsize); g_ArtsLibInternal_Primitive.ReadUint16(is,this->_dst,dstsize); g_ArtsLibInternal_Primitive.ReadUint64(is,this->_pkts,pktsize); g_ArtsLibInternal_Primitive.ReadUint64(is,this->_bytes,bytesize); return(is); } //------------------------------------------------------------------------- // int ArtsInterfaceMatrixEntry::read(int fd, uint8_t version) //......................................................................... // //------------------------------------------------------------------------- int ArtsInterfaceMatrixEntry::read(int fd, uint8_t version) { uint8_t bytesize, pktsize, srcsize, dstsize; int rc; int bytesRead = 0; rc = g_ArtsLibInternal_Primitive.FdRead(fd,&this->_descriptor, sizeof(this->_descriptor)); if (rc != sizeof(this->_descriptor)) { return(-1); } bytesRead += rc; srcsize = (this->_descriptor & 0x01) + 1; dstsize = ((this->_descriptor >> 1) & 0x01) + 1; pktsize = ((this->_descriptor >> 2) & 0x07) + 1; bytesize = ((this->_descriptor >> 5) & 0x07) + 1; rc = g_ArtsLibInternal_Primitive.ReadUint16(fd,this->_src,srcsize); if (rc != srcsize) { return(-1); } bytesRead += rc; rc = g_ArtsLibInternal_Primitive.ReadUint16(fd,this->_dst,dstsize); if (rc != dstsize) { return(-1); } bytesRead += rc; rc = g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_pkts,pktsize); if (rc != pktsize) { return(-1); } bytesRead += rc; rc = g_ArtsLibInternal_Primitive.ReadUint64(fd,this->_bytes,bytesize); if (rc != bytesize) { return(-1); } bytesRead += rc; return(bytesRead); } //------------------------------------------------------------------------- // ostream & ArtsInterfaceMatrixEntry::write(ostream& os, // uint8_t version) const //......................................................................... // //------------------------------------------------------------------------- ostream & ArtsInterfaceMatrixEntry::write(ostream& os, uint8_t version) const { uint8_t bytesize, pktsize, srcsize, dstsize; os.write((char*)&this->_descriptor,sizeof(this->_descriptor)); srcsize = (this->_descriptor & 0x01) + 1; dstsize = ((this->_descriptor >> 1) & 0x01) + 1; pktsize = ((this->_descriptor >> 2) & 0x07) + 1; bytesize = ((this->_descriptor >> 5) & 0x07) + 1; g_ArtsLibInternal_Primitive.WriteUint16(os,this->_src,srcsize); g_ArtsLibInternal_Primitive.WriteUint16(os,this->_dst,dstsize); g_ArtsLibInternal_Primitive.WriteUint64(os,this->_pkts,pktsize); g_ArtsLibInternal_Primitive.WriteUint64(os,this->_bytes,bytesize); return(os); } //------------------------------------------------------------------------- // int ArtsInterfaceMatrixEntry::write(int fd, uint8_t version) const //......................................................................... // //------------------------------------------------------------------------- int ArtsInterfaceMatrixEntry::write(int fd, uint8_t version) const { uint8_t bytesize, pktsize, srcsize, dstsize; int rc; int bytesWritten = 0; rc = g_ArtsLibInternal_Primitive.FdWrite(fd,&this->_descriptor, sizeof(this->_descriptor)); if (rc != sizeof(this->_descriptor)) { return(-1); } bytesWritten += rc; srcsize = (this->_descriptor & 0x01) + 1; dstsize = ((this->_descriptor >> 1) & 0x01) + 1; pktsize = ((this->_descriptor >> 2) & 0x07) + 1; bytesize = ((this->_descriptor >> 5) & 0x07) + 1; rc = g_ArtsLibInternal_Primitive.WriteUint16(fd,this->_src,srcsize); if (rc != srcsize) { return(-1); } bytesWritten += rc; rc = g_ArtsLibInternal_Primitive.WriteUint16(fd,this->_dst,dstsize); if (rc != dstsize) { return(-1); } bytesWritten += rc; rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_pkts,pktsize); if (rc != pktsize) { return(-1); } bytesWritten += rc; rc = g_ArtsLibInternal_Primitive.WriteUint64(fd,this->_bytes,bytesize); if (rc != bytesize) { return(-1); } bytesWritten += rc; return(bytesWritten); } //------------------------------------------------------------------------- // ostream & operator << (ostream& os, // const ArtsInterfaceMatrixEntry & ifEntry) //......................................................................... // //------------------------------------------------------------------------- ostream & operator << (ostream& os, const ArtsInterfaceMatrixEntry & ifEntry) { os << "\tINTERFACE MATRIX ENTRY" << endl; os << "\t\tdescriptor: " << (int)ifEntry.Descriptor() << endl; os << "\t\tsrc: " << ifEntry.Src() << endl; os << "\t\tdst: " << ifEntry.Dst() << endl; os << "\t\tpkts: " << ifEntry.Pkts() << endl; os << "\t\tbytes: " << ifEntry.Bytes() << endl; return(os); } //--------------------------------------------------------------------------- // bool ArtsInterfaceMatrixEntryGreaterBytes:: // operator () (const ArtsInterfaceMatrixEntry & interfaceEntry1, // const ArtsInterfaceMatrixEntry & interfaceEntry2) const //........................................................................... // //--------------------------------------------------------------------------- bool ArtsInterfaceMatrixEntryGreaterBytes:: operator () (const ArtsInterfaceMatrixEntry & interfaceEntry1, const ArtsInterfaceMatrixEntry & interfaceEntry2) const { return(interfaceEntry1.Bytes() > interfaceEntry2.Bytes()); } //--------------------------------------------------------------------------- // bool ArtsInterfaceMatrixEntryGreaterPkts:: // operator () (const ArtsInterfaceMatrixEntry & interfaceEntry1, // const ArtsInterfaceMatrixEntry & interfaceEntry2) const //........................................................................... // //--------------------------------------------------------------------------- bool ArtsInterfaceMatrixEntryGreaterPkts:: operator () (const ArtsInterfaceMatrixEntry & interfaceEntry1, const ArtsInterfaceMatrixEntry & interfaceEntry2) const { return(interfaceEntry1.Pkts() > interfaceEntry2.Pkts()); } uint32_t ArtsInterfaceMatrixEntry::_numObjects = 0;