00001 /*************************************************************************** 00002 ** 00003 ** This file is part of GpCoreTools. 00004 ** 00005 ** This file may be distributed and/or modified under the terms of the 00006 ** GNU General Public License version 2 or 3 as published by the Free 00007 ** Software Foundation and appearing in the file LICENSE.GPL included 00008 ** in the packaging of this file. 00009 ** 00010 ** This file is distributed in the hope that it will be useful, but WITHOUT 00011 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00012 ** FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00013 ** more details. 00014 ** 00015 ** You should have received a copy of the GNU General Public License 00016 ** along with this program. If not, see <http://www.gnu.org/licenses/>. 00017 ** 00018 ** See http://www.geopsy.org for more information. 00019 ** 00020 ** Created: 2009-08-25 00021 ** Authors: 00022 ** Marc Wathelet 00023 ** Marc Wathelet (LGIT, Grenoble, France) 00024 ** 00025 ***************************************************************************/ 00026 00027 #ifndef ADDRESS_H 00028 #define ADDRESS_H 00029 00030 #include <string> 00031 #include <arpa/inet.h> 00032 00033 namespace GpCoreTools { 00034 00035 class Address 00036 { 00037 public: 00038 Address() {_value=0;} 00039 Address(in_addr_t a) {_value=a;} 00040 Address(int index, const Address& subnet); 00041 Address(const Address& o) {_value=o._value;} 00042 00043 Address operator=(const Address& o) {_value=o._value; return *this;} 00044 Address operator&(const Address& o) const {return _value & o._value;} 00045 bool operator==(const Address& o) const {return _value==o._value;} 00046 bool operator!=(const Address& o) const {return _value!=o._value;} 00047 bool operator<(const Address& o) const {return _value<o._value;} 00048 bool operator>(const Address& o) const {return _value>o._value;} 00049 00050 bool isValid() const {return _value!=0;} 00051 Address subnet() const {return operator&(_mask);} 00052 in_addr_t value() const {return _value;} 00053 inline std::string toString() const; 00054 void fromString(const char * a) {_value=inet_addr(a);} 00055 int index() const; 00056 00057 static const Address& me() {return _me;} 00058 static void identifyMe(const char * interface); 00059 static void setMaskSize(int m); 00060 static const Address& mask() {return _mask;} 00061 static int maskSize() {return _maskSize;} 00062 private: 00063 in_addr_t _value; 00064 static Address _mask; 00065 static int _maskSize; 00066 static Address _me; 00067 }; 00068 00069 inline std::string Address::toString() const 00070 { 00071 in_addr a; 00072 a.s_addr=_value; 00073 return inet_ntoa(a); 00074 } 00075 00076 } // namespace GpCoreTools 00077 00078 #endif // ADDRESS_H