//=========================================================================== // $Name: $ // $Id: ArtsPortChoice.hh,v 1.1.1.1 2000/03/29 22:54:00 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 _ARTSPORTCHOICE_HH_ #define _ARTSPORTCHOICE_HH_ extern "C" { #include "caida_t.h" } #include //--------------------------------------------------------------------------- // class ArtsPortChoice //--------------------------------------------------------------------------- // This class abstracts a port choice. A port choice can be a single // port number or a range of ports ([a,b] inclusive). Typically this // class is used only inside the ArtsPortChooser class. //--------------------------------------------------------------------------- class ArtsPortChoice { public: //------------------------------------------------------------------------- // The value we will hold is a [firstPort,lastPort], stored in // a pair. When the port choice is a range, // first will be set to firstPort and second will be set to // lastPort. When the port choice is a single port, only the // first value of the pair is valid. //------------------------------------------------------------------------- typedef pair value_type; //------------------------------------------------------------------------- // We use a single bit in _flags for a few settings: // // - whether or not the value is a range or a single port // - the length of the first port number // - the length of the second port number if the value is a range // // This isn't really relevant to the class user, since _flags is // private. But I document the use here anyway. //------------------------------------------------------------------------- //------------------------------------------------------------------------- // If the value is a range, (_flags & k_isRangeMask) == 1 //------------------------------------------------------------------------- const uint8_t k_isRangeMask = 0x01; //------------------------------------------------------------------------- // If the first port number is two bytes, // (_flags & k_firstPortLengthMask) == 0x02 // else // (_flags & k_firstPortLengthMask) == 0 //------------------------------------------------------------------------- const uint8_t k_firstPortLengthMask = 0x02; //------------------------------------------------------------------------- // If the value is a range, there will be a second port number. // If the second port number is two bytes, // (_flags & k_lastPortLengthMask) == 0x04 // else // (_flags & k_firstPortLengthMask) == 0 //------------------------------------------------------------------------- const uint8_t k_lastPortLengthMask = 0x04; //------------------------------------------------------------------------- // ArtsPortChoice() //......................................................................... // constructor //------------------------------------------------------------------------- ArtsPortChoice(); //------------------------------------------------------------------------- // ArtsPortChoice(uint16_t port) //......................................................................... // Constructor for a singple port number choice. //------------------------------------------------------------------------- ArtsPortChoice(uint16_t port); //------------------------------------------------------------------------- // ArtsPortChoice(uint16_t firstPort, uint16_t lastPort) //......................................................................... // Constructor for a port range choice. firstPort must be less than // or equal to lastPort. The range is inclusive. //------------------------------------------------------------------------- ArtsPortChoice(uint16_t firstPort, uint16_t lastPort); //------------------------------------------------------------------------- // bool IsRange() const //......................................................................... // Returns true if the port choice is a range, else returns false. //------------------------------------------------------------------------- inline bool IsRange() const { if (this->_flags & k_isRangeMask) return(true); return(false); } //------------------------------------------------------------------------- // inline bool IsRange(bool isRange) //......................................................................... // If isRange is true, sets the port choice to a range, else sets // the port choice to a single port. //------------------------------------------------------------------------- inline bool IsRange(bool isRange) { if (isRange) this->_flags |= k_isRangeMask; else this->_flags &= (~ k_isRangeMask); return(isRange); } //------------------------------------------------------------------------- // const value_type & Value() const //......................................................................... // Returns a constant reference to the value in the port choice. //------------------------------------------------------------------------- const value_type & Value() const; //------------------------------------------------------------------------- // uint16_t Value(uint16_t port) //......................................................................... // Sets the port choice to a single port with value 'port'. Returns // the single port value. //------------------------------------------------------------------------- uint16_t Value(uint16_t port); //------------------------------------------------------------------------- // const value_type & Value(uint16_t firstPort, uint16_t lastPort) //......................................................................... // Sets the port choice to a port range [firstPort,lastPort] // inclusive. Returns a refernece to the range value. //------------------------------------------------------------------------- const value_type & Value(uint16_t firstPort, uint16_t lastPort); //------------------------------------------------------------------------- // bool Matches(uint16_t port) const //......................................................................... // Returns true if port matches our value (is equal to our first // port number if we're a single port value, or is within the range // specified by our [first,second] port pair if we're a port range). // Returns false otherwise. //------------------------------------------------------------------------- bool Matches(uint16_t port) const; //------------------------------------------------------------------------- // uint32_t Length() const //......................................................................... // Returns the length (in bytes) of space required to store the // port choice on disk. //------------------------------------------------------------------------- uint32_t Length() const; //------------------------------------------------------------------------- // int read(int fd) //......................................................................... // Reads the port choice from a file descriptor. Returns the number // of bytes read on successs, -1 on failure. //------------------------------------------------------------------------- int read(int fd); //------------------------------------------------------------------------- // istream & read(istream & is) //......................................................................... // Reads the port choice from an istream. Returns the istream. //------------------------------------------------------------------------- istream & read(istream & is); //------------------------------------------------------------------------- // int write(int fd) const //......................................................................... // Writes the port choice to a file descriptor. Returns the number // of bytes written on success, -1 on failure. //------------------------------------------------------------------------- int write(int fd) const; //------------------------------------------------------------------------- // ostream & write(ostream & os) const //......................................................................... // Writes the port choice to an ostream. Returns the ostream. //------------------------------------------------------------------------- ostream & write(ostream & os) const; //------------------------------------------------------------------------- // bool operator == (const ArtsPortChoice & portChoice) const //......................................................................... // Overloaded '==' operator. //------------------------------------------------------------------------- bool operator == (const ArtsPortChoice & portChoice) const; //------------------------------------------------------------------------- // bool operator < (const ArtsPortChoice & portChoice) const //......................................................................... // Overloaded '<' operator, used for sorting a vector of ArtsPortChoice // objects so that 2 vectors of ArtsPortChoice objects may be compared. //------------------------------------------------------------------------- bool operator < (const ArtsPortChoice & portChoice) const; //------------------------------------------------------------------------- // friend ostream & operator << (ostream& os, // const ArtsPortChoice & portChoice) //......................................................................... // Overloaded ostream << operator to dump an ArtsPortChoice to // an ostream in a human-readable form. //------------------------------------------------------------------------- friend ostream & operator << (ostream& os, const ArtsPortChoice & portChoice); private: uint8_t _flags; value_type _value; }; #endif // _ARTSPORTCHOICE_HH_