INET Framework for OMNeT++/OMNEST
ansaLsa6.h
Go to the documentation of this file.
00001 //
00002 // This program is free software: you can redistribute it and/or modify
00003 // it under the terms of the GNU Lesser General Public License as published by
00004 // the Free Software Foundation, either version 3 of the License, or
00005 // (at your option) any later version.
00006 //
00007 // This program is distributed in the hope that it will be useful,
00008 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00009 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010 // GNU Lesser General Public License for more details.
00011 //
00012 // You should have received a copy of the GNU Lesser General Public License
00013 // along with this program.  If not, see http://www.gnu.org/licenses/.
00014 //
00015 
00016 #ifndef ANSALSA6_H_
00017 #define ANSALSA6_H_
00018 
00019 #include "ansaOspfCommon6.h"
00020 #include "ansaOspfPacket6_m.h"
00021 
00022 namespace AnsaOspf6 {
00023 
00024 struct NextHop {
00025    unsigned char ifIndex;
00026    IPv6Address   hopAddress;
00027    RouterID      advertisingRouter;
00028 };
00029 
00030 
00031 class RoutingInfo {
00032 private:
00033    std::vector<NextHop>  nextHops;
00034    unsigned long         distance;
00035    OspfLsa6*             parent;
00036 
00037 public:
00038             RoutingInfo  (void) : distance(0), parent(NULL) {}
00039             RoutingInfo  (const RoutingInfo& routingInfo) : nextHops(routingInfo.nextHops), distance(routingInfo.distance), parent(routingInfo.parent) {}
00040 
00041    virtual ~RoutingInfo(void) {}
00042 
00043    void            AddNextHop          (NextHop nextHop)           { nextHops.push_back(nextHop); }
00044    void            ClearNextHops       (void)                      { nextHops.clear(); }
00045    unsigned int    GetNextHopCount     (void) const                { return nextHops.size(); }
00046    NextHop         GetNextHop          (unsigned int index) const  { return nextHops[index]; }
00047    void            SetDistance         (unsigned long d)           { distance = d; }
00048    unsigned long   GetDistance         (void) const                { return distance; }
00049    void            SetParent           (OspfLsa6* p)               { parent = p; }
00050    OspfLsa6*       GetParent           (void) const                { return parent; }
00051 };
00052 
00053 
00054 class LsaTrackingInfo {
00055 public:
00056    enum InstallSource {
00057       Originated = 0,
00058       Flooded = 1
00059    };
00060 
00061 private:
00062    InstallSource   source;
00063    unsigned long   installTime;
00064 
00065 public:
00066             LsaTrackingInfo(void) : source(Flooded), installTime(0) {}
00067             LsaTrackingInfo(const LsaTrackingInfo& info) : source(info.source), installTime(info.installTime) {}
00068 
00069    void            SetSource               (InstallSource installSource)   { source = installSource; }
00070    InstallSource   GetSource               (void) const                    { return source; }
00071    void            IncrementInstallTime    (void)                          { installTime++; }
00072    void            ResetInstallTime        (void)                          { installTime = 0; }
00073    unsigned long   GetInstallTime          (void) const                    { return installTime; }
00074 };
00075 
00076 
00077 class RouterLsa : public OspfRouterLsa6,
00078                   public RoutingInfo,
00079                   public LsaTrackingInfo
00080 {
00081 public:
00082             RouterLsa (void) : OspfRouterLsa6(), RoutingInfo(), LsaTrackingInfo() {}
00083             RouterLsa (const OspfRouterLsa6& lsa) : OspfRouterLsa6(lsa), RoutingInfo(), LsaTrackingInfo() {}
00084             RouterLsa (const RouterLsa& lsa) : OspfRouterLsa6(lsa), RoutingInfo(lsa), LsaTrackingInfo(lsa) {}
00085    virtual ~RouterLsa (void) {}
00086 
00087    bool     Update       (const OspfRouterLsa6* lsa);
00088    bool     DiffersFrom  (const OspfRouterLsa6* routerLsa) const;
00089 };
00090 
00091 
00092 class NetworkLsa : public OspfNetworkLsa6,
00093                    public RoutingInfo,
00094                    public LsaTrackingInfo
00095 {
00096 public:
00097             NetworkLsa (void) : OspfNetworkLsa6(), RoutingInfo(), LsaTrackingInfo() {}
00098             NetworkLsa (const OspfNetworkLsa6& lsa) : OspfNetworkLsa6(lsa), RoutingInfo(), LsaTrackingInfo() {}
00099             NetworkLsa (const NetworkLsa& lsa) : OspfNetworkLsa6(lsa), RoutingInfo(lsa), LsaTrackingInfo(lsa) {}
00100    virtual ~NetworkLsa (void) {}
00101 
00102    bool     Update      (const OspfNetworkLsa6* lsa);
00103    bool     DiffersFrom (const OspfNetworkLsa6* networkLsa) const;
00104 };
00105 
00106 
00107 class InterAreaPrefixLsa : public OspfInterAreaPrefixLsa6,
00108                            public RoutingInfo,
00109                            public LsaTrackingInfo
00110 {
00111 public:
00112             InterAreaPrefixLsa (void) : OspfInterAreaPrefixLsa6(), RoutingInfo(), LsaTrackingInfo() {}
00113             InterAreaPrefixLsa (const OspfInterAreaPrefixLsa6& lsa) : OspfInterAreaPrefixLsa6(lsa), RoutingInfo(), LsaTrackingInfo() {}
00114             InterAreaPrefixLsa (const InterAreaPrefixLsa& lsa) : OspfInterAreaPrefixLsa6(lsa), RoutingInfo(lsa), LsaTrackingInfo(lsa) {}
00115    virtual ~InterAreaPrefixLsa (void) {}
00116 
00117    bool     Update      (const OspfInterAreaPrefixLsa6* lsa);
00118    bool     DiffersFrom (const OspfInterAreaPrefixLsa6* interAreaPrefixLsa) const;
00119 };
00120 
00121 
00122 class InterAreaRouterLsa : public OspfInterAreaRouterLsa6,
00123                            public RoutingInfo,
00124                            public LsaTrackingInfo
00125 {
00126 public:
00127             InterAreaRouterLsa (void) : OspfInterAreaRouterLsa6(), RoutingInfo(), LsaTrackingInfo() {}
00128             InterAreaRouterLsa (const OspfInterAreaRouterLsa6& lsa) : OspfInterAreaRouterLsa6(lsa), RoutingInfo(), LsaTrackingInfo() {}
00129             InterAreaRouterLsa (const InterAreaRouterLsa& lsa) : OspfInterAreaRouterLsa6(lsa), RoutingInfo(lsa), LsaTrackingInfo(lsa) {}
00130    virtual ~InterAreaRouterLsa (void) {}
00131 
00132    bool     Update      (const OspfInterAreaRouterLsa6* lsa);
00133    bool     DiffersFrom (const OspfInterAreaRouterLsa6* interAreaRouterLsa) const;
00134 };
00135 
00136 
00137 class AsExternalLsa : public OspfAsExternalLsa6,
00138                       public RoutingInfo,
00139                       public LsaTrackingInfo
00140 {
00141 public:
00142             AsExternalLsa (void) : OspfAsExternalLsa6(), RoutingInfo(), LsaTrackingInfo() {}
00143             AsExternalLsa (const OspfAsExternalLsa6& lsa) : OspfAsExternalLsa6(lsa), RoutingInfo(), LsaTrackingInfo() {}
00144             AsExternalLsa (const AsExternalLsa& lsa) : OspfAsExternalLsa6(lsa), RoutingInfo(lsa), LsaTrackingInfo(lsa) {}
00145    virtual ~AsExternalLsa (void) {}
00146 
00147    bool     Update       (const OspfAsExternalLsa6* lsa);
00148    bool     DiffersFrom  (const OspfAsExternalLsa6* asExternalLsa) const;
00149 };
00150 
00151 
00152 class LinkLsa : public OspfLinkLsa6,
00153                 public RoutingInfo,
00154                 public LsaTrackingInfo
00155 {
00156 public:
00157             LinkLsa (void) : OspfLinkLsa6(), RoutingInfo(), LsaTrackingInfo() {}
00158             LinkLsa (const OspfLinkLsa6& lsa) : OspfLinkLsa6(lsa), RoutingInfo(), LsaTrackingInfo() {}
00159             LinkLsa (const LinkLsa& lsa) : OspfLinkLsa6(lsa), RoutingInfo(lsa), LsaTrackingInfo(lsa) {}
00160    virtual ~LinkLsa (void) {}
00161 
00162    bool     Update       (const OspfLinkLsa6* lsa);
00163    bool     DiffersFrom  (const OspfLinkLsa6* linkLsa) const;
00164 };
00165 
00166 
00167 class IntraAreaPrefixLsa : public OspfIntraAreaPrefixLsa6,
00168                            public RoutingInfo,
00169                            public LsaTrackingInfo
00170 {
00171 public:
00172             IntraAreaPrefixLsa (void) : OspfIntraAreaPrefixLsa6(), RoutingInfo(), LsaTrackingInfo() {}
00173             IntraAreaPrefixLsa (const OspfIntraAreaPrefixLsa6& lsa) : OspfIntraAreaPrefixLsa6(lsa), RoutingInfo(), LsaTrackingInfo() {}
00174             IntraAreaPrefixLsa (const IntraAreaPrefixLsa& lsa) : OspfIntraAreaPrefixLsa6(lsa), RoutingInfo(lsa), LsaTrackingInfo(lsa) {}
00175    virtual ~IntraAreaPrefixLsa (void) {}
00176 
00177    bool     Update       (const OspfIntraAreaPrefixLsa6* lsa);
00178    bool     DiffersFrom  (const OspfIntraAreaPrefixLsa6* intraAreaPrefixLsa) const;
00179 };
00180 
00181 }
00182 
00186 inline bool operator<(const OspfLsaHeader6& leftLSA, const OspfLsaHeader6& rightLSA) {
00187    long leftSequenceNumber = leftLSA.getLsSequenceNumber();
00188    long rightSequenceNumber = rightLSA.getLsSequenceNumber();
00189 
00190    if (leftSequenceNumber < rightSequenceNumber){
00191       return true;
00192    }
00193    if (leftSequenceNumber == rightSequenceNumber){
00194       unsigned short leftAge = leftLSA.getLsAge();
00195       unsigned short rightAge = rightLSA.getLsAge();
00196 
00197       if ((leftAge != MAX_AGE) && (rightAge == MAX_AGE)){
00198          return true;
00199       }
00200       if ((abs(leftAge - rightAge) > MAX_AGE_DIFF) && (leftAge > rightAge)){
00201          return true;
00202       }
00203    }
00204    return false;
00205 }
00206 
00210 inline bool operator==(const OspfLsaHeader6& leftLSA, const OspfLsaHeader6& rightLSA) {
00211    long leftSequenceNumber = leftLSA.getLsSequenceNumber();
00212    long rightSequenceNumber = rightLSA.getLsSequenceNumber();
00213    unsigned short leftAge = leftLSA.getLsAge();
00214    unsigned short rightAge = rightLSA.getLsAge();
00215 
00216    if (  (leftSequenceNumber == rightSequenceNumber)
00217       && (
00218             (
00219                   (leftAge == MAX_AGE) && (rightAge == MAX_AGE)
00220             ) || (((leftAge != MAX_AGE) && (rightAge != MAX_AGE))
00221                   && (abs(leftAge - rightAge) <= MAX_AGE_DIFF)
00222             )
00223       )
00224    ){
00225       return true;
00226    }else{
00227       return false;
00228    }
00229 }
00230 
00231 inline bool operator==(const OspfOptions6& leftOptions, const OspfOptions6& rightOptions) {
00232    return ((leftOptions.V6_IPv6Routing == rightOptions.V6_IPv6Routing)
00233          && (leftOptions.E_ExternalRoutingCapability == rightOptions.E_ExternalRoutingCapability)
00234          && (leftOptions.N_NSSA == rightOptions.N_NSSA)
00235          && (leftOptions.R_Router == rightOptions.R_Router)
00236          && (leftOptions.DC_DemandCircuits == rightOptions.DC_DemandCircuits));
00237 }
00238 
00239 inline bool operator!=(const OspfOptions6& leftOptions, const OspfOptions6& rightOptions) {
00240    return (!(leftOptions == rightOptions));
00241 }
00242 
00243 inline bool operator==(const OspfPrefixOptions6& left, const OspfPrefixOptions6& right) {
00244    return ( (left.NU_NoUnicast == right.NU_NoUnicast)
00245          && (left.LA_LocalAddress == right.LA_LocalAddress));
00246 }
00247 
00248 inline bool operator!=(const OspfPrefixOptions6& left, const OspfPrefixOptions6& right) {
00249    return (!(left == right));
00250 }
00251 
00252 inline bool operator==(const OspfPrefix6& left, const OspfPrefix6& right) {
00253    return ( (left.getPrefixLenght() == right.getPrefixLenght())
00254          && (left.getPrefixOptions() == right.getPrefixOptions())
00255          && (left.getMetric() == right.getMetric())
00256          && (left.getAddressPrefix() == right.getAddressPrefix()));
00257 }
00258 
00259 inline bool operator!=(const OspfPrefix6& left, const OspfPrefix6& right) {
00260    return (!(left == right));
00261 }
00262 
00263 inline bool operator==(const Link6& left, const Link6& right) {
00264    return ( (left.getType() == right.getType())
00265          && (left.getMetric() == right.getMetric())
00266          && (left.getInterfaceID() == right.getInterfaceID())
00267          && (left.getNeighborInterfaceID() == right.getNeighborInterfaceID())
00268          && (left.getNeighborRouterID() == right.getNeighborRouterID()));
00269 }
00270 
00271 inline bool operator!=(const Link6& left, const Link6& right) {
00272    return (!(left == right));
00273 }
00274 
00275 inline bool operator==(const AnsaOspf6::NextHop& leftHop, const AnsaOspf6::NextHop& rightHop) {
00276    return ( (leftHop.ifIndex           == rightHop.ifIndex) &&
00277             (leftHop.hopAddress        == rightHop.hopAddress) &&
00278             (leftHop.advertisingRouter == rightHop.advertisingRouter));
00279 }
00280 
00281 inline bool operator!=(const AnsaOspf6::NextHop& leftHop, const AnsaOspf6::NextHop& rightHop) {
00282    return (!(leftHop == rightHop));
00283 }
00284 
00285 inline void PrintLsaHeader6(const OspfLsaHeader6& lsaHeader, std::ostream& o) {
00286    o << "LsaHeader: age=" << lsaHeader.getLsAge() << ", type=";
00287    switch (lsaHeader.getLsType()){
00288       case RouterLsaType:
00289          o << "RouterLsa";
00290          break;
00291       case NetworkLsaType:
00292          o << "NetworkLsa";
00293          break;
00294       case InterAreaPrefixLsaType:
00295          o << "InterAreaPrefixLsa";
00296          break;
00297       case InterAreaRouterLsaType:
00298          o << "InterAreaRouterLsa";
00299          break;
00300       case AsExternalLsaType:
00301          o << "AsExternalLsa";
00302          break;
00303       case LinkLsaType:
00304          o << "LinkLsa";
00305          break;
00306       case IntraAreaPrefixLsaType:
00307          o << "IntraAreaPrefixLsa";
00308          break;
00309       default:
00310          o << "UnknownLsa";
00311          break;
00312    }
00313    o << ", LSID="
00314      << lsaHeader.getLinkStateID();
00315    o << ", advertisingRouter="
00316      << IPAddress(lsaHeader.getAdvertisingRouter())
00317      << ", seqNumber=" << lsaHeader.getLsSequenceNumber();
00318    o << endl;
00319 }
00320 
00321 inline void printLsaLink(const Link6& link, std::ostream& o) {
00322    o << "   " << link.getMetric();
00323    o << ", neighbor=" << IPAddress(link.getNeighborRouterID());
00324    o << ", neighbor iface=" << link.getNeighborInterfaceID();
00325    o << ", type=";
00326    switch (link.getType()) {
00327       case PointToPointLink:  o << "PointToPoint";   break;
00328       case TransitLink:       o << "Transit";        break;
00329       case VirtualLink:       o << "Virtual";        break;
00330       default:                o << "Unknown";        break;
00331    }
00332 
00333    o << ", iface=" << link.getInterfaceID() << endl;;
00334 }
00335 
00336 inline void PrintRouterLsa(const OspfRouterLsa6& lsa, std::ostream& o) {
00337    o << "bits=" << ((lsa.getV_VirtualLinkEndpoint()) ? "V " : "_ ")
00338                 << ((lsa.getE_AsBoundaryRouter()) ? "E " : "_ ")
00339                 << ((lsa.getB_AreaBorderRouter()) ? "B" : "_") << endl;
00340 
00341    o << "links:" << endl;
00342    unsigned int linkCount = lsa.getLinksArraySize();
00343    for (unsigned int j = 0; j < linkCount; j++){
00344       const Link6& link = lsa.getLinks(j);
00345          printLsaLink(link, o);
00346    }
00347    // TODO: format + options?
00348 }
00349 
00350 inline void PrintNetworkLsa(const OspfNetworkLsa6& lsa, std::ostream& o) {
00351    unsigned int routerCount = lsa.getAttachedRoutersArraySize();
00352    for (unsigned int i = 0; i < routerCount; i++) {
00353       o << "   " << IPAddress(lsa.getAttachedRouters(i)) << endl;
00354    }
00355    // TODO: options?
00356 }
00357 
00358 inline void PrintInterAreaPrefixLsa(const OspfInterAreaPrefixLsa6& lsa, std::ostream& o) {
00359    o << "   " << lsa.getMetric();
00360    o << ", " << lsa.getAddressPrefix() << "/" << lsa.getPrefixLenght() << endl;
00361    // TODO: prefixOptions?
00362 }
00363 
00364 inline void PrintInterAreaRouterLsa(const OspfInterAreaRouterLsa6& lsa, std::ostream& o) {
00365    o << "   " << lsa.getMetric();
00366    o << ", " << IPAddress(lsa.getDestinationRouterID()) << endl;
00367    // TODO: options?
00368 }
00369 
00370 inline void PrintAsExternalLsa(const OspfAsExternalLsa6& lsa, std::ostream& o) {
00371    o << "   " << lsa.getMetric();
00372    o << ", bits=" << ((lsa.getE_ExternalMetricType()) ? "E " : "_ ")
00373                   << ((lsa.getF_ForwardingAddress()) ? "F " : "_ ")
00374                   << ((lsa.getT_ExternalRouteTag()) ? "T" : "_") << endl;
00375    o << ", " << lsa.getAddressPrefix() << "/" << lsa.getPrefixLenght();
00376    if (lsa.getF_ForwardingAddress()){
00377       o << ", forward=" << lsa.getForwardingAddress();
00378    }
00379    if (lsa.getT_ExternalRouteTag()){
00380       o << ", routeTag=" << lsa.getExternalRouteTag();
00381    }
00382    o << endl;
00383 }
00384 
00385 inline void PrintLinkLsa(const OspfLinkLsa6& lsa, std::ostream& o) {
00386    o << "   " << lsa.getRtrPriority();
00387    o << ", " << lsa.getLinkLocalAddress() << " prefixes:" << endl;
00388    // TODO: options?
00389    unsigned int prefixCount = lsa.getPrefixesArraySize();
00390    for (unsigned int i = 0; i < prefixCount; i++) {
00391       OspfPrefix6 prefix = lsa.getPrefixes(i);
00392       o << "   " << prefix.getMetric() << ", "
00393         << prefix.getAddressPrefix() << "/" << prefix.getPrefixLenght() << endl;
00394    }
00395 }
00396 
00397 inline void PrintIntraAreaPrefixLsa(const OspfIntraAreaPrefixLsa6& lsa, std::ostream& o) {
00398    o << "   refType=" << lsa.getReferencedLsType();
00399    o << ", refLinkId=" << lsa.getReferencedLinkStateID();
00400    o << ", refRouter=" << IPAddress(lsa.getReferencedAdvertisingRouter()) << " prefixes:"<< endl;
00401    unsigned int prefixCount = lsa.getPrefixesArraySize();
00402    for (unsigned int i = 0; i < prefixCount; i++) {
00403       OspfPrefix6 prefix = lsa.getPrefixes(i);
00404       o << "   " << prefix.getMetric() << ", "
00405         << prefix.getAddressPrefix() << "/" << prefix.getPrefixLenght() << endl;
00406    }
00407 }
00408 
00409 inline std::ostream& operator<<(std::ostream& ostr, OspfLsa6& lsa) {
00410    PrintLsaHeader6(lsa.getHeader(), ostr);
00411    return ostr;
00412 }
00413 
00414 #endif /* ANSALSA6_H_ */