xorp

olsr_types.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 // vim:set sts=4 ts=8 sw=4:
00003 
00004 // Copyright (c) 2001-2009 XORP, Inc.
00005 //
00006 // This program is free software; you can redistribute it and/or modify
00007 // it under the terms of the GNU General Public License, Version 2, June
00008 // 1991 as published by the Free Software Foundation. Redistribution
00009 // and/or modification of this program under the terms of any other
00010 // version of the GNU General Public License is not permitted.
00011 // 
00012 // This program is distributed in the hope that it will be useful, but
00013 // WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00015 // see the GNU General Public License, Version 2, a copy of which can be
00016 // found in the XORP LICENSE.gpl file.
00017 // 
00018 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00019 // http://xorp.net
00020 
00021 // $XORP: xorp/contrib/olsr/olsr_types.hh,v 1.5 2008/10/02 21:56:35 bms Exp $
00022 
00023 #ifndef __OLSR_OLSR_TYPES_HH__
00024 #define __OLSR_OLSR_TYPES_HH__
00025 
00032 struct OlsrTypes {
00036     static const int DEFAULT_STATIC_FACE_COST = 0;
00037 
00041     static const int MAX_TTL = 255;
00042 
00043     /*
00044      * Constants from Section 18.2.
00045      */
00046 
00050     static const int DEFAULT_HELLO_INTERVAL = 2;
00051 
00056     static const int DEFAULT_REFRESH_INTERVAL = 2;
00057 
00061     static const int DEFAULT_TC_INTERVAL = 5;
00062 
00067     static const int DEFAULT_DUP_HOLD_TIME = 30;
00068 
00072     static const int DEFAULT_MID_INTERVAL = DEFAULT_TC_INTERVAL;
00073 
00077     static const int DEFAULT_HNA_INTERVAL = DEFAULT_TC_INTERVAL;
00078 
00082     typedef uint32_t ExternalID;
00083 
00084     static const ExternalID UNUSED_EXTERNAL_ID = 0;
00085 
00089     typedef uint32_t FaceID;
00090 
00091     static const FaceID UNUSED_FACE_ID = 0;
00092 
00093     /*
00094      * Defaults controlling link hysteresis defined in link.cc.
00095      * Used to derive link quality when RFC-compliant
00096      * link hysteresis is enabled.
00097      */
00098 
00102     static double DEFAULT_HYST_THRESHOLD_HIGH;
00103 
00107     static double DEFAULT_HYST_THRESHOLD_LOW;
00108 
00112     static double DEFAULT_HYST_SCALING;
00113 
00117     typedef uint8_t LinkType;
00118 
00119     enum LinkTypes {
00120     UNSPEC_LINK = 0,
00121     ASYM_LINK = 1,
00122     SYM_LINK = 2,
00123     LOST_LINK = 3,
00124     // May be used as a vector or array index.
00125     LINKTYPE_START = UNSPEC_LINK,
00126     LINKTYPE_END = LOST_LINK
00127     };
00128     static const int NUM_LINKTYPE =
00129     LINKTYPE_END - LINKTYPE_START + 1;
00130 
00134     typedef uint32_t LogicalLinkID;
00135 
00136     static const LogicalLinkID UNUSED_LINK_ID = 0;
00137 
00141     typedef uint8_t MessageType;
00142 
00143     enum MessageTypes {
00144     HELLO_MESSAGE = 1,
00145     TC_MESSAGE = 2,
00146     MID_MESSAGE = 3,
00147     HNA_MESSAGE = 4,
00148     LQ_HELLO_MESSAGE = 201,     // HELLO with ETX measurements.
00149     LQ_TC_MESSAGE = 202     // TC with ETX measurements.
00150     };
00151 
00155     typedef uint32_t MidEntryID;
00156 
00157     static const MidEntryID UNUSED_MID_ID = 0;
00158 
00162     typedef uint32_t NeighborID;
00163 
00164     static const NeighborID UNUSED_NEIGHBOR_ID = 0;
00165 
00169     typedef uint8_t NeighborType;
00170 
00171     enum NeighborTypes {
00172     NOT_NEIGH = 0,
00173     SYM_NEIGH = 1,
00174     MPR_NEIGH = 2,
00175     // May be used as a vector or array index.
00176     NEIGHBORTYPE_START = NOT_NEIGH,
00177     NEIGHBORTYPE_END = MPR_NEIGH,
00178     };
00179     static const int NUM_NEIGHBORTYPE =
00180     NEIGHBORTYPE_END - NEIGHBORTYPE_START + 1;
00181 
00185     static const uint16_t DEFAULT_OLSR_PORT = 698;
00186 
00190     typedef uint8_t WillType;
00191 
00192     enum Willingness {
00193     WILL_NEVER = 0,
00194     WILL_LOW = 1,
00195     WILL_DEFAULT = 3,
00196     WILL_HIGH = 6,
00197     WILL_ALWAYS = 7,
00198     // limits
00199     WILL_MIN = WILL_LOW,
00200     WILL_MAX = WILL_ALWAYS,
00201     // bounds
00202     WILL_START = WILL_NEVER,
00203     WILL_END = WILL_ALWAYS
00204     };
00205 
00210     typedef uint8_t TcRedundancyType;
00211 
00212     enum TcRedundancyMode {
00213     TCR_MPRS_IN = 0,    // MPR selectors only
00214     TCR_MPRS_INOUT = 1, // MPR selectors and MPRs
00215     TCR_ALL = 2,        // The full neighbor set.
00216     // bounds
00217     TCR_START = TCR_MPRS_IN,
00218     TCR_END = TCR_ALL
00219     };
00220 
00224     static const uint8_t DEFAULT_TC_REDUNDANCY = TCR_MPRS_IN;
00225 
00230     static const uint8_t DEFAULT_MPR_COVERAGE = 1;
00231 
00235     typedef uint32_t TopologyID;
00236 
00237     static const TopologyID UNUSED_TOPOLOGY_ID = 0;
00238 
00242     typedef uint32_t TwoHopNodeID;
00243 
00244     static const TwoHopNodeID UNUSED_TWOHOP_NODE_ID = 0;
00245 
00249     typedef uint32_t TwoHopLinkID;
00250 
00251     static const TwoHopLinkID UNUSED_TWOHOP_LINK_ID = 0;
00252 
00260     enum VertexType {
00261     VT_ORIGINATOR = 0,  // iff origin() is true
00262     VT_UNKNOWN = 0,
00263     VT_NEIGHBOR = 1,    // Nodes at radius 1.
00264     VT_TWOHOP = 2,      // Nodes at radius 2.
00265     VT_TOPOLOGY = 3,
00266     VT_MID = 4,     // Not a vertex type; used by routing table
00267     VT_HNA = 5,     // do.
00268     // bounds
00269     VT_START = VT_ORIGINATOR,
00270     VT_END = VT_HNA
00271     };
00272 
00276     enum TlvType {
00277     TLV_VERSION = 1,    // Version number (u32)
00278     TLV_SYSTEM_INFO = 2,    // Where created (string)
00279     TLV_OLSR_VERSION = 3,   // Version of OLSR in use (u32)
00280     TLV_OLSR_FAMILY = 4,    // Protocol family (u32)
00281     TLV_OLSR_N1 = 5,    // One-hop neighbor
00282     TLV_OLSR_L1 = 6,    // One-hop link
00283     TLV_OLSR_N2 = 7,    // Two-hop neighbor
00284     TLV_OLSR_L2 = 8,    // Two-hop link
00285     TLV_OLSR_MID = 9,   // MID entry
00286     TLV_OLSR_TC = 10,   // Topology Control entry
00287     TLV_OLSR_HNA = 11,  // HNA entry
00288     };
00289 };
00290 
00295 inline bool
00296 is_node_vertex(const OlsrTypes::VertexType vt)
00297 {
00298     if (vt == OlsrTypes::VT_NEIGHBOR || vt == OlsrTypes::VT_TWOHOP ||
00299     vt == OlsrTypes::VT_TOPOLOGY || vt == OlsrTypes::VT_MID)
00300     return true;
00301     return false;
00302 }
00303 
00308 class EightBitTime {
00309 public:
00314     static inline TimeVal to_timeval(const uint8_t byte) {
00315         unsigned int mant = byte >> 4;
00316         unsigned int exp = byte & 0x0F;
00317         double sec = ((16 + mant) << exp) * _scaling_factor / 16.0;
00318         return (TimeVal(sec));
00319     }
00320 
00325     static inline uint8_t from_timeval(const TimeVal& tv) {
00326         double sec = tv.get_double();
00327         int isec = static_cast<int>(sec / _scaling_factor);
00328         int mant = 0;
00329         int exp = 0;
00330         while (isec >= (1 << exp))
00331             exp++;
00332         if (exp == 0) {
00333             mant = 1;
00334         } else {
00335             exp--;
00336             if (mant > (_mod - 1)) {
00337                 // ceiling
00338                 mant = exp = (_mod - 1);
00339             } else {
00340                 mant = static_cast<int>(_mod * sec /
00341                     _scaling_factor / (1 << exp) - _mod);
00342                 exp += mant >> 4;
00343                 mant &= 0x0F;
00344             }
00345         }
00346         return (static_cast<uint8_t>(mant << 4 | (exp & 0x0F)));
00347     }
00348 
00349 private:
00350     static const int    _mod = 16;
00351     static const double _scaling_factor;
00352 };
00353 
00362 inline bool
00363 is_seq_newer(const uint16_t seq1, const uint16_t seq2)
00364 {
00365     // UINT16_MAX is defined in C99 stdint.h, however this is not
00366     // part of the C++ Standard yet.
00367     static const uint16_t uint16_max = 65535;
00368     return  (seq1 > seq2 && seq1 - seq2 <= uint16_max/2) ||
00369         (seq2 > seq1 && seq2 - seq1 > uint16_max/2);
00370 }
00371 
00375 const char* tcr_to_str(const OlsrTypes::TcRedundancyType t);
00376 
00380 const char* will_to_str(const OlsrTypes::WillType t);
00381 
00385 const char* vt_to_str(const OlsrTypes::VertexType vt);
00386 
00387 #endif // __OLSR_OLSR_TYPES_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations