Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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
00048 uint32 addr;
00049
00050 protected:
00051
00052
00053
00054 void keepFirstBits (unsigned int n);
00055
00056
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