User Manual, Developers Guide and API Documentation

Address.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_SERVICE_NL_ADDRESS_HPP
00029 #define WNS_SERVICE_NL_ADDRESS_HPP
00030 
00031 #include <WNS/Assure.hpp>
00032 
00033 #include <ostream>
00034 #include <sstream>
00035 #include <string>
00036 
00037 namespace wns { namespace service { namespace nl {
00038 
00042     enum protocolNumber {
00043         IP = 4,
00044         TCP = 6,
00045         UDP = 17,
00046         IPv6 = 41,
00047         // Numbers from 138-252 are not assigned yet
00048         Constanze = 138,
00049         PubSub = 139,
00050         Overlay = 140
00051         // Numbers 253-254 are for experiments and tests
00052         // Number 255 is reserved
00053     };
00054 
00055     typedef std::string FQDN;
00056 
00057     class Address
00058     {
00059     public:
00060         Address() :
00061             address(0)
00062         {}
00063 
00064         explicit
00065         Address(const std::string& addr)
00066         {
00067             std::stringstream tmp(addr);
00068             char c;
00069             unsigned long int n1;
00070             unsigned long int n2;
00071             unsigned long int n3;
00072             unsigned long int n4;
00073             tmp >> n1;
00074             tmp >> c;
00075             assure(c=='.', "Malformed IPAddress given!");
00076             tmp >> n2;
00077             tmp >> c;
00078             assure(c=='.', "Malformed IPAddress given!");
00079             tmp >> n3;
00080             tmp >> c;
00081             assure(c=='.', "Malformed IPAddress given!");
00082             tmp >> n4;
00083             assure(n1<=255, "Malformed IPAddress given!");
00084             assure(n2<=255, "Malformed IPAddress given!");
00085             assure(n3<=255, "Malformed IPAddress given!");
00086             assure(n4<=255, "Malformed IPAddress given!");
00087 
00088             address = (n1 << 24) + (n2 << 16) + (n3 << 8) + n4;
00089             }
00090 
00091         explicit
00092         Address(unsigned long int a)
00093         {
00094             address=a;
00095         }
00096 
00097         bool
00098         operator==(const Address& right) const
00099         {
00100             return address == right.address;
00101         }
00102 
00103         bool
00104         operator!=(const Address& right) const
00105         {
00106             return !( (*this)==right);
00107         }
00108 
00109         void
00110         operator=(const Address& src)
00111         {
00112             address = src.address;
00113         }
00114 
00115         bool
00116         operator<(const Address& src) const
00117         {
00118             return address < src.address;
00119         }
00120 
00121         bool
00122         operator>(const Address& src) const
00123         {
00124             return address > src.address;
00125         }
00126 
00127         bool
00128         operator<=(const Address& src) const
00129         {
00130             return (((*this) < src) || ((*this) == src));
00131         }
00132 
00133         bool
00134         operator>=(const Address& src) const
00135         {
00136             return (((*this) > src) || ((*this) == src));
00137         }
00138 
00139         const Address
00140         operator++()
00141         {
00142             ++address;
00143             return *this;
00144         }
00145 
00146         const Address
00147         operator++(int)
00148         {
00149             Address tmp(*this);
00150             ++address;
00151             return tmp;
00152         }
00153 
00154         const Address
00155         operator--()
00156         {
00157             --address;
00158             return *this;
00159         }
00160 
00161         const Address
00162         operator--(int)
00163         {
00164             Address tmp(*this);
00165             --address;
00166             return tmp;
00167         }
00168 
00169         Address
00170         operator&(Address mask) const
00171         {
00172             return Address(address&mask.address);
00173         }
00174 
00175         Address
00176         operator|(Address mask) const
00177         {
00178             return Address(address|mask.address);
00179         }
00180 
00181         friend
00182         std::ostream&
00183         operator <<(std::ostream& str, const Address& a)
00184         {
00185             std::stringstream tmp;
00186             unsigned long int n1 = (a.address&(255<<24))>>24;
00187             unsigned long int n2 = (a.address&(255<<16))>>16;
00188             unsigned long int n3 = (a.address&(255<<8))>>8;
00189             unsigned long int n4 = a.address&255;
00190 
00191             // Write to tempory and then write to output
00192             // You need this when someone outside uses manipulators on the stream
00193 
00194             tmp << n1 << "." << n2 << "." << n3 << "." << n4;
00195             str << tmp.str();
00196             return str;
00197         }
00198 
00199         unsigned long int
00200         getInteger() const { return this->address; }
00201 
00202     private:
00203         unsigned long int address;
00204     };
00205 
00206 } // nl
00207 } // service
00208 } // wns
00209 
00210 #endif //_WNS_SERVICE_NL_ADDRESS_HPP
00211 
00212 /*
00213   Local Variables:
00214   mode: c++
00215   fill-column: 80
00216   c-basic-offset: 8
00217   c-comment-only-line-offset: 0
00218   c-tab-always-indent: t
00219   indent-tabs-mode: t
00220   tab-width: 8
00221   End:
00222 */

Generated on Sun May 27 03:31:54 2012 for openWNS by  doxygen 1.5.5