<commit_before>/*
 * The olsr.org Optimized Link-State Routing daemon(olsrd)
 * Copyright (c) 2004, <NAME>(<EMAIL>)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 *
 * * Redistributions of source code must retain the above copyright 
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright 
 *   notice, this list of conditions and the following disclaimer in 
 *   the documentation and/or other materials provided with the 
 *   distribution.
 * * Neither the name of olsr.org, olsrd nor the names of its 
 *   contributors may be used to endorse or promote products derived 
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Visit http://www.olsr.org for more information.
 *
 * If you find this software useful feel free to make a donation
 * to the project. For more information see the website or contact
 * the copyright holders.
 *
 * $Id: olsr.c,v 1.30 2004/11/21 14:22:33 kattemat Exp $
 */

/**
 * All these functions are global
 */

#include "defs.h"
#include "olsr.h"
#include "link_set.h"
#include "two_hop_neighbor_table.h"
#include "tc_set.h"
#include "duplicate_set.h"
#include "mpr_selector_set.h"
#include "mid_set.h"
#include "mpr.h"
#if defined USE_LINK_QUALITY
#include "lq_mpr.h"
#include "lq_route.h"
#endif
#include "scheduler.h"
#include "generate_msg.h"
#include "apm.h"
#include "misc.h"

#include <stdarg.h>
#include <signal.h>


/**
 *Checks if a timer has timed out.
 */


/**
 *Initiates a "timer", wich is a timeval structure,
 *with the value given in time_value.
 *@param time_value the value to initialize the timer with
 *@param hold_timer the timer itself
 *@return nada
 */
inline void
olsr_init_timer(olsr_u32_t time_value, struct timeval *hold_timer)
{ 
  olsr_u16_t  time_value_sec;
  olsr_u16_t  time_value_msec;

  time_value_sec = time_value/1000;
  time_value_msec = time_value-(time_value_sec*1000);

  hold_timer->tv_sec = time_value_sec;
  hold_timer->tv_usec = time_value_msec*1000;   
}





/**
 *Generaties a timestamp a certain number of milliseconds
 *into the future.
 *
 *@param time_value how many milliseconds from now
 *@param hold_timer the timer itself
 *@return nada
 */
inline void
olsr_get_timestamp(olsr_u32_t delay, struct timeval *hold_timer)
{ 
  hold_timer->tv_sec = now.tv_sec + delay / 1000;
  hold_timer->tv_usec = now.tv_usec + (delay % 1000) * 1000;
  
  if (hold_timer->tv_usec > 1000000)
    {
      hold_timer->tv_sec++;
      hold_timer->tv_usec -= 1000000;
    }
}



/**
 *Initialize the message sequence number as a random value
 */
void
init_msg_seqno()
{
  message_seqno = random() & 0xFFFF;
}

/**
 * Get and increment the message sequence number
 *
 *@return the seqno
 */
inline olsr_u16_t
get_msg_seqno()
{
  return message_seqno++;
}


void
register_pcf(int (*f)(int, int, int))
{
  struct pcf *new_pcf;

  olsr_printf(1, "Registering pcf function\n");

  new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");

  new_pcf->function = f;
  new_pcf->next = pcf_list;
  pcf_list = new_pcf;

}


/**
 *Process changes in neighborhood or/and topology.
 *Re-calculates the neighborhooh/topology if there
 *are any updates - then calls the right functions to
 *update the routing table.
 *@return 0
 */
inline void
olsr_process_changes()
{

  struct pcf *tmp_pc_list;

#ifdef DEBUG
  if(changes_neighborhood)
    olsr_printf(3, "CHANGES IN NEIGHBORHOOD\n");
  if(changes_topology)
    olsr_printf(3, "CHANGES IN TOPOLOGY\n");
  if(changes_hna)
    olsr_printf(3, "CHANGES IN HNA\n");  
#endif
  
  if(!changes_neighborhood &&
     !changes_topology &&
     !changes_hna)
    return;

  if (olsr_cnf->debug_level > 0 && olsr_cnf->clear_screen && isatty(1))
  {
      clear_console();
      printf("<<<< %s (%s) >>>>\n", SOFTWARE_VERSION, __DATE__);
  }

  if (changes_neighborhood)
    {
      /* Calculate new mprs, HNA and routing table */
#if defined USE_LINK_QUALITY
      if (olsr_cnf->lq_level < 1)
        {
#endif
          olsr_calculate_mpr();
#if defined USE_LINK_QUALITY
        }

      else
        {
          olsr_calculate_lq_mpr();
        }


      if (olsr_cnf->lq_level < 2)
        {
#endif
          olsr_calculate_routing_table();
#if defined USE_LINK_QUALITY
        }

      else
        {
          olsr_calculate_lq_routing_table();<commit_msg>Center title in debug output when ClearScreen is enabled.<commit_after>/*
 * The olsr.org Optimized Link-State Routing daemon(olsrd)
 * Copyright (c) 2004, <NAME>(<EMAIL>)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions 
 * are met:
 *
 * * Redistributions of source code must retain the above copyright 
 *   notice, this list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright 
 *   notice, this list of conditions and the following disclaimer in 
 *   the documentation and/or other materials provided with the 
 *   distribution.
 * * Neither the name of olsr.org, olsrd nor the names of its 
 *   contributors may be used to endorse or promote products derived 
 *   from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Visit http://www.olsr.org for more information.
 *
 * If you find this software useful feel free to make a donation
 * to the project. For more information see the website or contact
 * the copyright holders.
 *
 * $Id: olsr.c,v 1.31 2004/11/29 20:47:01 tlopatic Exp $
 */

/**
 * All these functions are global
 */

#include "defs.h"
#include "olsr.h"
#include "link_set.h"
#include "two_hop_neighbor_table.h"
#include "tc_set.h"
#include "duplicate_set.h"
#include "mpr_selector_set.h"
#include "mid_set.h"
#include "mpr.h"
#if defined USE_LINK_QUALITY
#include "lq_mpr.h"
#include "lq_route.h"
#endif
#include "scheduler.h"
#include "generate_msg.h"
#include "apm.h"
#include "misc.h"

#include <stdarg.h>
#include <signal.h>


/**
 *Checks if a timer has timed out.
 */


/**
 *Initiates a "timer", wich is a timeval structure,
 *with the value given in time_value.
 *@param time_value the value to initialize the timer with
 *@param hold_timer the timer itself
 *@return nada
 */
inline void
olsr_init_timer(olsr_u32_t time_value, struct timeval *hold_timer)
{ 
  olsr_u16_t  time_value_sec;
  olsr_u16_t  time_value_msec;

  time_value_sec = time_value/1000;
  time_value_msec = time_value-(time_value_sec*1000);

  hold_timer->tv_sec = time_value_sec;
  hold_timer->tv_usec = time_value_msec*1000;   
}





/**
 *Generaties a timestamp a certain number of milliseconds
 *into the future.
 *
 *@param time_value how many milliseconds from now
 *@param hold_timer the timer itself
 *@return nada
 */
inline void
olsr_get_timestamp(olsr_u32_t delay, struct timeval *hold_timer)
{ 
  hold_timer->tv_sec = now.tv_sec + delay / 1000;
  hold_timer->tv_usec = now.tv_usec + (delay % 1000) * 1000;
  
  if (hold_timer->tv_usec > 1000000)
    {
      hold_timer->tv_sec++;
      hold_timer->tv_usec -= 1000000;
    }
}



/**
 *Initialize the message sequence number as a random value
 */
void
init_msg_seqno()
{
  message_seqno = random() & 0xFFFF;
}

/**
 * Get and increment the message sequence number
 *
 *@return the seqno
 */
inline olsr_u16_t
get_msg_seqno()
{
  return message_seqno++;
}


void
register_pcf(int (*f)(int, int, int))
{
  struct pcf *new_pcf;

  olsr_printf(1, "Registering pcf function\n");

  new_pcf = olsr_malloc(sizeof(struct pcf), "New PCF");

  new_pcf->function = f;
  new_pcf->next = pcf_list;
  pcf_list = new_pcf;

}


/**
 *Process changes in neighborhood or/and topology.
 *Re-calculates the neighborhooh/topology if there
 *are any updates - then calls the right functions to
 *update the routing table.
 *@return 0
 */
inline void
olsr_process_changes()
{

  struct pcf *tmp_pc_list;
  static char title[100] = "";
  char tmp_title[100];
  int len, i;

#ifdef DEBUG
  if(changes_neighborhood)
    olsr_printf(3, "CHANGES IN NEIGHBORHOOD\n");
  if(changes_topology)
    olsr_printf(3, "CHANGES IN TOPOLOGY\n");
  if(changes_hna)
    olsr_printf(3, "CHANGES IN HNA\n");  
#endif
  
  if(!changes_neighborhood &&
     !changes_topology &&
     !changes_hna)
    return;

  if (olsr_cnf->debug_level > 0 && olsr_cnf->clear_screen && isatty(1))
  {
      clear_console();

      if (title[0] == 0)
      {
        len = sprintf(tmp_title, "*** %s (%s) ***\n",
                      SOFTWARE_VERSION, __DATE__);

        len = (74 - len) / 2;
        
        for (i = 0; i < len; i++)
          title[i] = ' ';

        strcpy(title + i, tmp_title);
      }

      printf(title);
  }

  if (changes_neighborhood)
    {
      /* Calculate new mprs, HNA and routing table */
#if defined USE_LINK_QUALITY
      if (olsr_cnf->lq_level < 1)
        {
#endif
          olsr_calculate_mpr();
#if defined USE_LINK_QUALITY
        }

      else
        {
          olsr_calculate_lq_mpr();
        }


      if (olsr_cnf->lq_level < 2)
        {
#endif
          olsr_calculate_routing_table();
#if defined USE_LINK_QUALITY
        }

      else
        {
          olsr_calculate_lq_routing_table();
{'fraction_non_alphanumeric': 0.09754315462599324, 'fraction_numerical': 0.015526532103388438, 'mean_word_length': 3.4621026894865525, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 45, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5119372', 'n_tokens_mistral': 3951, 'n_tokens_neox': 3579, 'n_words': 1154}