IPAddress.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2001  Vincent Oberle (vincent@oberle.com)
00003 // Institute of Telematics, University of Karlsruhe, Germany.
00004 // University Comillas, Madrid, Spain.
00005 // Copyright (C) 2004 Andras Varga
00006 //
00007 // This program is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 2.1 of the License, or (at your option) any later version.
00011 //
00012 // This program is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU Lesser General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU Lesser General Public
00018 // License along with this program; if not, see <http://www.gnu.org/licenses/>.
00019 //
00020 
00021 //
00022 // Author: Vincent Oberle, Jan-March 2001
00023 // Cleanup and rewrite: Andras Varga, 2004
00024 //
00025 
00026 #ifndef __INET_IPADDRESS_H
00027 #define __INET_IPADDRESS_H
00028 
00029 #include <omnetpp.h>
00030 #include <iostream>
00031 #include <string>
00032 #include "INETDefs.h"
00033 
00034 
00038 const short PORT_UNDEF = 0;
00039 const short PORT_MAX = 0x7fff;
00040 
00044 class INET_API IPAddress
00045 {
00046   protected:
00047     // Address is encoded in a single uint32
00048     uint32 addr;
00049 
00050   protected:
00051     // Only keeps the n first bits of the address, completing it with zeros.
00052     // Typical usage is when the length of an IP prefix is done and to check
00053     // the address ends with the right number of 0.
00054     void keepFirstBits (unsigned int n);
00055 
00056     // Parses IP address into the given bytes, and returns true if syntax was OK.
00057     static bool parseIPAddress(const char *text, unsigned char tobytes[]);
00058 
00059   public:
00062     static const IPAddress UNSPECIFIED_ADDRESS; 
00063     static const IPAddress LOOPBACK_ADDRESS;    
00064     static const IPAddress LOOPBACK_NETMASK;    
00065     static const IPAddress ALLONES_ADDRESS;     
00066 
00067     static const IPAddress ALL_HOSTS_MCAST;          
00068     static const IPAddress ALL_ROUTERS_MCAST;        
00069     static const IPAddress ALL_DVMRP_ROUTERS_MCAST;  
00070     static const IPAddress ALL_OSPF_ROUTERS_MCAST;   
00071     static const IPAddress ALL_OSPF_DESIGNATED_ROUTERS_MCAST;  
00072 
00073 
00076 
00080     IPAddress() {addr = 0;}
00081 
00085     IPAddress(uint32 ip) {addr = ip;}
00086 
00090     IPAddress(int i0, int i1, int i2, int i3) {set(i0, i1, i2, i3);}
00091 
00095     IPAddress(const char *text) {set(text);}
00096 
00100     IPAddress(const IPAddress& obj) {operator=(obj);}
00101 
00102     ~IPAddress() {}
00104 
00110     void set(uint32 ip) {addr = ip;}
00111 
00115     void set(int i0, int i1, int i2, int i3);
00116 
00120     void set(const char *t);
00122 
00126     IPAddress& operator=(const IPAddress& obj) {addr = obj.addr; return *this;}
00127 
00133     bool isUnspecified() const {return addr==0;}
00134 
00138     bool equals(const IPAddress& toCmp) const {return addr == toCmp.addr;}
00139 
00143     IPAddress doAnd(const IPAddress& ip) const {return addr & ip.addr;}
00144 
00148     std::string str() const;
00149 
00153     uint32 getInt() const {return addr;}
00154 
00159     int getDByte(int i) const {return (addr >> (3-i)*8) & 0xFF;}
00160 
00165     char getIPClass() const;
00166 
00171     bool isMulticast() const {return (addr & 0xF0000000)==0xE0000000;}
00172 
00179      bool isLinkLocalMulticast() const {return (addr & 0xFFFFFF00) == 0xE0000000;}
00180 
00186     IPAddress getNetwork() const;
00187 
00192     IPAddress getNetworkMask() const;
00193 
00197     bool isNetwork(const IPAddress& toCmp) const;
00198 
00202     bool prefixMatches(const IPAddress& to_cmp, int numbits) const;
00203 
00212     int getNumMatchingPrefixBits(const IPAddress& to_cmp) const;
00213 
00217     int getNetmaskLength() const;
00218 
00223     static bool maskedAddrAreEqual(const IPAddress& addr1,
00224                                    const IPAddress& addr2,
00225                                    const IPAddress& netmask);
00226 
00230     bool operator==(const IPAddress& addr1) const {return equals(addr1);}
00231 
00235     bool operator!=(const IPAddress& addr1) const {return !equals(addr1);}
00236 
00240     bool operator<(const IPAddress& addr1) const {return getInt()<addr1.getInt();}
00241 
00250     static bool isWellFormed(const char *text);
00251 };
00252 
00253 inline std::ostream& operator<<(std::ostream& os, const IPAddress& ip)
00254 {
00255     return os << ip.str();
00256 }
00257 
00258 inline void doPacking(cCommBuffer *buf, const IPAddress& addr)
00259 {
00260     buf->pack(addr.getInt());
00261 }
00262 
00263 inline void doUnpacking(cCommBuffer *buf, IPAddress& addr)
00264 {
00265     int32 d; buf->unpack(d); addr.set(d);
00266 }
00267 
00268 #endif
00269 
00270