//=========================================================================== // @(#) $Name: $ // @(#) $Id: ArtsAttribute.hh,v 1.1.1.1 2000/03/29 22:53:56 kkeys Exp $ //=========================================================================== // CAIDA 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 General Public License, // v. 2 dated June 1991 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 GPL along with arts++. // Copies can also be obtained from: // // http://www.gnu.org/copyleft/gpl.html // // or by writing to: // // University of California, San Diego // // SDSC/CAIDA // 9500 Gilman Dr., MS-0505 // La Jolla, CA 92093 - 0505 USA // // Or contact: // // info@caida.org //=========================================================================== #ifndef _ARTSATTRIBUTE_HH_ #define _ARTSATTRIBUTE_HH_ extern "C" { #include #include #include "caida_t.h" } #include #include //--------------------------------------------------------------------------- // Object Attribute Identifiers //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // pad attribute //--------------------------------------------------------------------------- const uint32_t artsC_ATTR_PAD(); #define artsC_ATTR_PAD 0 //--------------------------------------------------------------------------- // ASCII comment string attribute //--------------------------------------------------------------------------- const uint32_t artsC_ATTR_COMMENT(); #define artsC_ATTR_COMMENT 1 //--------------------------------------------------------------------------- // Creation time attribute //--------------------------------------------------------------------------- const uint32_t artsC_ATTR_CREATION(); #define artsC_ATTR_CREATION 2 //--------------------------------------------------------------------------- // Time range attribute //--------------------------------------------------------------------------- const uint32_t artsC_ATTR_PERIOD(); #define artsC_ATTR_PERIOD 3 //--------------------------------------------------------------------------- // Host attribute (an IP address) //--------------------------------------------------------------------------- const uint32_t artsC_ATTR_HOST(); #define artsC_ATTR_HOST 4 //--------------------------------------------------------------------------- // Interface name attribute //--------------------------------------------------------------------------- const uint32_t artsC_ATTR_INTERFACE(); #define artsC_ATTR_INTERFACE 5 //--------------------------------------------------------------------------- // interface index attribute //--------------------------------------------------------------------------- const uint32_t artsC_ATTR_IFINDEX(); #define artsC_ATTR_IFINDEX 6 //--------------------------------------------------------------------------- // Object Attribute Formats //--------------------------------------------------------------------------- //--------------------------------------------------------------------------- // Unsigned 8-bit value //--------------------------------------------------------------------------- const uint8_t artsC_UBIT8(); #define artsC_UBIT8 2 //--------------------------------------------------------------------------- // Unsigned 16-bit value //--------------------------------------------------------------------------- const uint8_t artsC_UBIT16(); #define artsC_UBIT16 3 //--------------------------------------------------------------------------- // Unsigned 32-bit value //--------------------------------------------------------------------------- const uint8_t artsC_UBIT32(); #define artsC_UBIT32 4 //--------------------------------------------------------------------------- // Unsigned 64-bit value //--------------------------------------------------------------------------- const uint8_t artsC_UBIT64(); #define artsC_UBIT64 5 //--------------------------------------------------------------------------- // Signed 8-bit value //--------------------------------------------------------------------------- const uint8_t artsC_BIT8(); #define artsC_BIT8 6 //--------------------------------------------------------------------------- // Signed 16-bit value //--------------------------------------------------------------------------- const uint8_t artsC_BIT16(); #define artsC_BIT16 7 //--------------------------------------------------------------------------- // Signed 32-bit value //--------------------------------------------------------------------------- const uint8_t artsC_BIT32(); #define artsC_BIT32 8 //--------------------------------------------------------------------------- // Signed 64-bit value //--------------------------------------------------------------------------- const uint8_t artsC_BIT64(); #define artsC_BIT64 9 //--------------------------------------------------------------------------- // Null-terminated 8-bit string //--------------------------------------------------------------------------- const uint8_t artsC_STRING8(); #define artsC_STRING8 10 //--------------------------------------------------------------------------- // 8-bit character array //--------------------------------------------------------------------------- const uint8_t artsC_CHAR8(); #define artsC_CHAR8 11 //--------------------------------------------------------------------------- // IP Address //--------------------------------------------------------------------------- const uint8_t artsC_IPV4_ADDRESS(); #define artsC_IPV4_ADDRESS 12 //--------------------------------------------------------------------------- // Unix GMT date value //--------------------------------------------------------------------------- const uint8_t artsC_UNIX_DATE(); #define artsC_UNIX_DATE 13 //--------------------------------------------------------------------------- // Pair of unix GMT date values //--------------------------------------------------------------------------- const uint8_t artsC_UNIX_DATE_RANGE(); #define artsC_UNIX_DATE_RANGE 14 //--------------------------------------------------------------------------- // 32-bit floating point (XDR) //--------------------------------------------------------------------------- const uint8_t artsC_FLOAT32(); #define artsC_FLOAT32 15 //--------------------------------------------------------------------------- // 64-bit float (XDR double) //--------------------------------------------------------------------------- const uint8_t artsC_DOUBLE64(); #define artsC_DOUBLE64 16 //--------------------------------------------------------------------------- // class ArtsAttribute //--------------------------------------------------------------------------- // Class to encapsulate an ARTS attribute. //--------------------------------------------------------------------------- class ArtsAttribute { public: //------------------------------------------------------------------------- // ArtsAttribute() //......................................................................... // constructor //------------------------------------------------------------------------- ArtsAttribute(); //------------------------------------------------------------------------- // ~ArtsAttribute() //......................................................................... // destructor //------------------------------------------------------------------------- ~ArtsAttribute(); //------------------------------------------------------------------------- // ArtsAttribute & operator = (const ArtsAttribute artsAttribute) //......................................................................... // Overloaded '=' operator for deep-copying an ArtsAttribute. //------------------------------------------------------------------------- ArtsAttribute & operator = (const ArtsAttribute artsAttribute); //------------------------------------------------------------------------- // inline uint32_t Identifier() const //......................................................................... // Returns the attribute identifier. //------------------------------------------------------------------------- inline uint32_t Identifier() const { return(this->_identifier); } //------------------------------------------------------------------------- // inline uint32_t Identifier(uint32_t identifier) //......................................................................... // Sets and returns the attribute identifier. //------------------------------------------------------------------------- inline uint32_t Identifier(uint32_t identifier) { this->_identifier = identifier; return(this->_identifier); } //------------------------------------------------------------------------- // inline uint8_t Format() const //......................................................................... // Returns the attribute format. //------------------------------------------------------------------------- inline uint8_t Format() const { return(this->_format); } //------------------------------------------------------------------------- // inline uint8_t Format(uint8_t format) //......................................................................... // Sets and returns the attribute format. //------------------------------------------------------------------------- inline uint8_t Format(uint8_t format) { this->_format = format; return(this->_format); } //------------------------------------------------------------------------- // inline uint32_t Length() const //......................................................................... // Returns the attribute length. //------------------------------------------------------------------------- inline uint32_t Length() const { return(this->_length); } //------------------------------------------------------------------------- // inline uint32_t Length(uint32_t length) //......................................................................... // Sets and returns the attribute length. //------------------------------------------------------------------------- inline uint32_t Length(uint32_t length) { this->_length = length; return(this->_length); } //------------------------------------------------------------------------- // inline const string Comment() const //......................................................................... // Returns the attribute comment if the attribute really is a comment // attribute, otherwise asserts. //------------------------------------------------------------------------- inline const string Comment() const { assert(this->Identifier() == artsC_ATTR_COMMENT); return(*(this->_value.comment)); } //------------------------------------------------------------------------- // inline uint32_t Creation() const //......................................................................... // Returns the attribute creation timestamp if the attribute identifier // is artsC_ATTR_CREATION (i.e. the attirbute really is a creation // attribute), else asserts. //------------------------------------------------------------------------- inline uint32_t Creation() const { assert(this->Identifier() == artsC_ATTR_CREATION); return(this->_value.creation); } //------------------------------------------------------------------------- // inline uint32_t Creation(uint32_t creation) //......................................................................... // Sets and returns the attribute creation timestamp if the attribute // identifier is artsC_ATTR_CREATION (i.e. the attirbute really is a // creation attribute), else asserts. //------------------------------------------------------------------------- inline uint32_t Creation(uint32_t creation) { assert(this->Identifier() == artsC_ATTR_CREATION); this->_value.creation = creation; return(this->_value.creation); } //------------------------------------------------------------------------- // inline const uint32_t *Period() const //......................................................................... // Returns the attribute period if the attribute is really a period // attribute, else asserts. //------------------------------------------------------------------------- inline const uint32_t *Period() const { assert(this->Identifier() == artsC_ATTR_PERIOD); return(this->_value.period); } //------------------------------------------------------------------------- // inline uint32_t *Period(uint32_t startTime, uint32_t endTime) //......................................................................... // Sets and returns the attribute period if the attribute is really a // period attribute, else asserts. //------------------------------------------------------------------------- inline uint32_t *Period(uint32_t startTime, uint32_t endTime) { assert(this->Identifier() == artsC_ATTR_PERIOD); this->_value.period[0] = startTime; this->_value.period[1] = endTime; return(this->_value.period); } //------------------------------------------------------------------------- // inline ipv4addr_t Host() const //......................................................................... // Returns the attribute IP address if the attribute is really a host // attribute, else asserts. //------------------------------------------------------------------------- inline ipv4addr_t Host() const { assert(this->Identifier() == artsC_ATTR_HOST); return(this->_value.host); } //------------------------------------------------------------------------- // inline ipv4addr_t Host(ipv4addr_t host) //......................................................................... // Sets and returns the attribute IP address if the attribute is really a // host attribute, else asserts. //------------------------------------------------------------------------- inline ipv4addr_t Host(ipv4addr_t host) { assert(this->Identifier() == artsC_ATTR_HOST); this->_value.host = host; return(this->_value.host); } //------------------------------------------------------------------------- // inline const string Interface() const //......................................................................... // Returns the attribute interface name if the attribute is really an // interface attribute, else asserts. //------------------------------------------------------------------------- inline const string Interface() const { assert(this->Identifier() == artsC_ATTR_INTERFACE); return(*(this->_value.interface)); } //------------------------------------------------------------------------- // inline string Interface(const string & interface) //......................................................................... // Sets and returns the attribute interface name if the attribute is // really an interface attribute, else asserts. //------------------------------------------------------------------------- inline string Interface(const string & interface) { assert(this->Identifier() == artsC_ATTR_INTERFACE); if (this->_value.interface) delete(this->_value.interface); this->_value.interface = new string(interface); return(*(this->_value.interface)); } //------------------------------------------------------------------------- // inline uint16_t IfIndex() const //......................................................................... // Returns the interface index if the attribute is really an // interface index attribute, else asserts. //------------------------------------------------------------------------- inline uint16_t IfIndex() const { assert(this->Identifier() == artsC_ATTR_IFINDEX); return(this->_value.ifIndex); } //------------------------------------------------------------------------- // inline uint16_t IfIndex(uint16_t ifIndex) //......................................................................... // Sets and returns the ifIndex if the attribute is really an // interface index attribute, else asserts. //------------------------------------------------------------------------- inline uint16_t IfIndex(uint16_t ifIndex) { assert(this->Identifier() == artsC_ATTR_IFINDEX); this->_value.ifIndex = ifIndex; return(this->_value.ifIndex); } //------------------------------------------------------------------------- // inline void *Arbitrary() const //......................................................................... // Returns the value of an arbitrary-format ARTS attribute. Arbitrary // attributes can be used when a pre-defined attribute type does not // exist for the type of data stored in the attribute. For example, // an application-specific attribute. The pointer returned here is // just a pointer to a block of memory of length ArtsAttribute::Length(). // It is the responsibility of the caller to convert to an approriate // representation. //------------------------------------------------------------------------- inline void *Arbitrary() const { return(this->_value.arbitrary); } //------------------------------------------------------------------------- // inline void *Arbitrary(void *arbitrary) //......................................................................... // Sets and returns the value of an arbitrary-format ARTS attribute. // Note that the pointer passed in should be a pointer to a block of // memory of length ArtsAttribute::Length(). Internally the ARTS library // will treat the data as opaque. //------------------------------------------------------------------------- inline void *Arbitrary(void *arbitrary) { this->_value.arbitrary = arbitrary; return(this->_value.arbitrary); } //------------------------------------------------------------------------- // ostream& write(ostream& os) const //......................................................................... // Writes the attribute to an ostream and returns the ostream. //------------------------------------------------------------------------- ostream& write(ostream& os) const; //------------------------------------------------------------------------- // int write(int fd) const //......................................................................... // Writes the attribute to a file descriptor. Returns the number of // bytes written on success, -1 on failure. //------------------------------------------------------------------------- int write(int fd) const; //------------------------------------------------------------------------- // istream& read(istream& is) //......................................................................... // Reads an attribute from an istream and returns the istream. //------------------------------------------------------------------------- istream& read(istream& is); //------------------------------------------------------------------------- // int read(int fd) //......................................................................... // Reads an attribute from a file descriptor and returns the number of // bytes read. UNTESTED //------------------------------------------------------------------------- int read(int fd); // UNTESTED //------------------------------------------------------------------------- // uint32_t Bytes() const //......................................................................... // Returns the number of bytes required to store the attribute on disk. //------------------------------------------------------------------------- uint32_t Bytes() const { return(sizeof(this->_identifier) + sizeof(this->_length) + this->_length); } //------------------------------------------------------------------------- // friend ostream & operator << (ostream &os, // ArtsAttribute & artsAttribute) //......................................................................... // Overloaded '<<' operator to dump attribute to an ostream in a // himan-readable form. //------------------------------------------------------------------------- friend ostream & operator << (ostream &os, ArtsAttribute & artsAttribute); private: uint32_t _identifier; uint8_t _format; uint32_t _length; union { uint8_t pad; string *comment; uint32_t creation; uint32_t period[2]; ipv4addr_t host; uint16_t ifIndex; string *interface; void *arbitrary; } _value; // Attribute value }; #endif // _ARTSATTRIBUTE_HH_