/***************************************************************************
* Copyright (C) 2004 by Francisco J. Ros *
* fjrm@dif.um.es *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
///
/// \file OLSR.h
/// \brief Header file for OLSR agent and related classes.
///
/// Here are defined all timers used by OLSR, including those for managing internal
/// state and those for sending messages. Class OLSR is also defined, therefore this
/// file has signatures for the most important methods. Lots of constants are also
/// defined.
///
#ifndef __OLSR_h__
#define __OLSR_h__
#include <olsr/OLSR_pkt.h>
#include <olsr/OLSR_state.h>
#include <olsr/OLSR_rtable.h>
#include <olsr/OLSR_repositories.h>
#include <trace.h>
#include <classifier-port.h>
#include <agent.h>
#include <packet.h>
#include <timer-handler.h>
#include <random.h>
#include <vector>
/********** Useful macros **********/
/// Returns maximum of two numbers.
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
/// Returns minimum of two numbers.
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
/// Defines NULL as zero, used for pointers.
#ifndef NULL
#define NULL 0
#endif
/// Gets current time from the scheduler.
#define CURRENT_TIME Scheduler::instance().clock()
///
/// \brief Gets the delay between a given time and the current time.
///
/// If given time is previous to the current one, then this macro returns
/// a number close to 0. This is used for scheduling events at a certain moment.
///
#define DELAY(time) (((time) < (CURRENT_TIME)) ? (0.000001) : \
(time - CURRENT_TIME + 0.000001))
/// Scaling factor used in RFC 3626.
#define OLSR_C 0.0625
/********** Intervals **********/
/// HELLO messages emission interval.
#define OLSR_HELLO_INTERVAL 2
/// TC messages emission interval.
#define OLSR_TC_INTERVAL 5
/// MID messages emission interval.
#define OLSR_MID_INTERVAL OLSR_TC_INTERVAL
///
/// \brief Period at which a node must cite every link and every neighbor.
///
/// We only use this value in order to define OLSR_NEIGHB_HOLD_TIME.
///
#define OLSR_REFRESH_INTERVAL 2
/********** Holding times **********/
/// Neighbor holding time.
#define OLSR_NEIGHB_HOLD_TIME 3*OLSR_REFRESH_INTERVAL
/// Top holding time.
#define OLSR_TOP_HOLD_TIME 3*OLSR_TC_INTERVAL
/// Dup holding time.
#define OLSR_DUP_HOLD_TIME 30
/// MID holding time.
#define OLSR_MID_HOLD_TIME 3*OLSR_MID_INTERVAL
/********** Link types **********/
/// Unspecified link type.
#define OLSR_UNSPEC_LINK 0
/// Asymmetric link type.
#define OLSR_ASYM_LINK 1
/// Symmetric link type.
#define OLSR_SYM_LINK 2
/// Lost link type.
#define OLSR_LOST_LINK 3
/********** Neighbor types **********/
/// Not neighbor type.
#define OLSR_NOT_NEIGH 0
/// Symmetric neighbor type.
#define OLSR_SYM_NEIGH 1
/// Asymmetric neighbor type.
#define OLSR_MPR_NEIGH 2
/********** Willingness **********/
/// Willingness for forwarding packets from other nodes: never.
#define OLSR_WILL_NEVER 0
/// Willingness for forwarding packets from other nodes: low.
#define OLSR_WILL_LOW 1
/// Willingness for forwarding packets from other nodes: medium.
#define OLSR_WILL_DEFAULT 3
/// Willingness for forwarding packets from other nodes: high.
#define OLSR_WILL_HIGH 6
/// Willingness for forwarding packets from other nodes: always.
#define OLSR_WILL_ALWAYS 7
/********** Miscellaneous constants **********/
/// Maximum allowed jitter.
#define OLSR_MAXJITTER OLSR_HELLO_INTERVAL/4
/// Maximum allowed sequence number.
#define OLSR_MAX_SEQ_NUM 65535
/// Used to set status of an OLSR_nb_tuple as "not symmetric".
#define OLSR_STATUS_NOT_SYM 0
/// Used to set status of an OLSR_nb_tuple as "symmetric".
#define OLSR_STATUS_SYM 1
/// Random number between [0-OLSR_MAXJITTER] used to jitter OLSR packet transmission.
#define JITTER (Random::uniform()*OLSR_MAXJITTER)
class OLSR; // forward declaration
/********** Timers **********/
/// Timer for sending an enqued message.
class OLSR_MsgTimer : public TimerHandler {
public:
OLSR_MsgTimer(OLSR* agent) : TimerHandler() {
agent_ = agent;
}
protected:
OLSR* agent_; ///< OLSR agent which created the timer.
virtual void expire(Event* e);
};
/// Timer for sending HELLO messages.
class OLSR_HelloTimer : public TimerHandler {
public:
OLSR_HelloTimer(OLSR* agent) : TimerHandler() { agent_ = agent; }
protected:
OLSR* agent_; ///< OLSR agent which created the timer.
virtual void expire(Event* e);
};
/// Timer for sending TC messages.
class OLSR_TcTimer : public TimerHandler {
public:
OLSR_TcTimer(OLSR* agent) : TimerHandler() { agent_ = agent; }
protected:
OLSR* agent_; ///< OLSR agent which created the timer.
virtual void expire(Event* e);
};
/// Timer for sending MID messages.
class OLSR_MidTimer : public TimerHandler {
public:
OLSR_MidTimer(OLSR* agent) : TimerHandler() { agent_ = agent; }
protected:
OLSR* agent_; ///< OLSR agent which created the timer.
virtual void expire(Event* e);
};
/// Timer for removing duplicate tuples: OLSR_dup_tuple.
class OLSR_DupTupleTimer : public TimerHandler {
public:
OLSR_DupTupleTimer(OLSR* agent, OLSR_dup_tuple* tuple) : TimerHandler() {
agent_ = agent;
tuple_ = tuple;
}
protected:
OLSR* agent_; ///< OLSR agent which created the timer.
OLSR_dup_tuple* tuple_; ///< OLSR_dup_tuple which must be removed.
virtual void expire(Event* e);
};
/// Timer for removing link tuples: OLSR_link_tuple.
class OLSR_LinkTupleTimer : public TimerHandler {
///
/// \brief A flag which tells if the timer has expired (at least) once or not.
///
/// When a link tuple has been just created, its sym_time is expired but this
/// does not mean a neighbor loss. Thus, we use this flag in order to be able
/// to distinguish this situation.
///
bool first_time_;
public:
OLSR_LinkTupleTimer(OLSR* agent, OLSR_link_tuple* tuple) : TimerHandler() {
agent_ = agent;
tuple_ = tuple;
first_time_ = true;
}
protected:
OLSR* agent_; ///< OLSR agent which created the timer.
OLSR_link_tuple* tuple_; ///< OLSR_link_tuple which must be removed.
virtual void expire(Event* e);
};
/// Timer for removing nb2hop tuples: OLSR_nb2hop_tuple.
class OLSR_Nb2hopTupleTimer : public TimerHandler {
public:
OLSR_Nb2hopTupleTimer(OLSR* agent, OLSR_nb2hop_tuple* tuple) : TimerHandler() {
agent_ = agent;
tuple_ = tuple;
}
protected:
OLSR* agent_; ///< OLSR agent which created the timer.
OLSR_nb2hop_tuple* tuple_; ///< OLSR_nb2hop_tuple which must be removed.
virtual void expire(Event* e);
};
/// Timer for removing MPR selector tuples: OLSR_mprsel_tuple.
class OLSR_MprSelTupleTimer : public TimerHandler {
public:
OLSR_MprSelTupleTimer(OLSR* agent, OLSR_mprsel_tuple* tuple) :