[ospfd] Bug #134, ospfd should be more robust to backward time change

2006-08-25 Paul Jakma <paul.jakma@sun.com>

	* (general) Bug #134. Be more robust to backward time changes,
	  use the newly added libzebra time functions.
	  In most cases: recent_time -> recent_relative_time()
	  gettimeofday -> quagga_gettime (QUAGGA_CLK_MONOTONIC, ..)
	  time -> quagga_time.
	  (ospf_make_md5_digest) time() call deliberately not changed.
	  (ospf_external_lsa_refresh) remove useless gettimeofday, LSA
	  tv_orig time was already set in ospf_lsa_new, called via
	  ospf_external_lsa_new.
diff --git a/ospfd/ChangeLog b/ospfd/ChangeLog
index 05f96ac..1d7a4a1 100644
--- a/ospfd/ChangeLog
+++ b/ospfd/ChangeLog
@@ -1,3 +1,15 @@
+2006-08-25 Paul Jakma <paul.jakma@sun.com>
+
+	* (general) Bug #134. Be more robust to backward time changes,
+	  use the newly added libzebra time functions.
+	  In most cases: recent_time -> recent_relative_time()
+	  gettimeofday -> quagga_gettime (QUAGGA_CLK_MONOTONIC, ..)
+	  time -> quagga_time.
+	  (ospf_make_md5_digest) time() call deliberately not changed.
+	  (ospf_external_lsa_refresh) remove useless gettimeofday, LSA
+	  tv_orig time was already set in ospf_lsa_new, called via
+	  ospf_external_lsa_new.
+
 2006-08-04 Paul Jakma <paul.jakma@sun.com>
 
 	* ospf_lsdb.c: (ospf_lsdb_delete_entry) new function, consolidate
diff --git a/ospfd/ospf_dump.c b/ospfd/ospf_dump.c
index b8dc795..2fcbfe6 100644
--- a/ospfd/ospf_dump.c
+++ b/ospfd/ospf_dump.c
@@ -300,7 +300,7 @@
   if (!t)
     return "inactive";
   
-  result = tv_sub (t->u.sands, recent_time);
+  result = tv_sub (t->u.sands, recent_relative_time());
   return ospf_timeval_dump (&result, buf, size);
 }
 
diff --git a/ospfd/ospf_flood.c b/ospfd/ospf_flood.c
index 91cbbf3..0f485fe 100644
--- a/ospfd/ospf_flood.c
+++ b/ospfd/ospf_flood.c
@@ -268,7 +268,7 @@
 		       "while local one is initial instance.");
           ; /* Accept this LSA for quick LSDB resynchronization. */
         }
-      else if (tv_cmp (tv_sub (recent_time, current->tv_recv),
+      else if (tv_cmp (tv_sub (recent_relative_time (), current->tv_recv),
 	               int2tv (OSPF_MIN_LS_ARRIVAL)) < 0)
         {
           if (IS_DEBUG_OSPF_EVENT)
diff --git a/ospfd/ospf_lsa.c b/ospfd/ospf_lsa.c
index b99b931..05eed35 100644
--- a/ospfd/ospf_lsa.c
+++ b/ospfd/ospf_lsa.c
@@ -141,7 +141,7 @@
   struct timeval delta, now;
   int delay = 0;
 
-  gettimeofday (&now, NULL);
+  quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
   delta = tv_sub (now, lsa->tv_orig);
 
   if (tv_cmp (delta, int2tv (OSPF_MIN_LS_INTERVAL)) < 0)
@@ -163,10 +163,9 @@
 get_age (struct ospf_lsa *lsa)
 {
   int age;
-  struct timeval now;
 
-  gettimeofday (&now, NULL);
-  age = ntohs (lsa->data->ls_age) + tv_floor (tv_sub (now, lsa->tv_recv));
+  age = ntohs (lsa->data->ls_age) 
+        + tv_floor (tv_sub (recent_relative_time (), lsa->tv_recv));
 
   return age;
 }
@@ -229,7 +228,7 @@
   new->flags = 0;
   new->lock = 1;
   new->retransmit_counter = 0;
-  gettimeofday (&new->tv_recv, NULL);
+  new->tv_recv = recent_relative_time ();
   new->tv_orig = new->tv_recv;
   new->refresh_list = -1;
   
@@ -2460,9 +2459,6 @@
   
   new->data->ls_seqnum = lsa_seqnum_increment (lsa);
 
-  /* Record timestamp. */
-  gettimeofday (&new->tv_orig, NULL);
-
   /* Re-calculate checksum. */
   ospf_lsa_checksum (new->data);
 
@@ -3770,7 +3766,7 @@
 	delay = 0;
 
       current_index = ospf->lsa_refresh_queue.index +
-	(time (NULL) - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
+	(quagga_time (NULL) - ospf->lsa_refresher_started)/OSPF_LSA_REFRESHER_GRANULARITY;
       
       index = (current_index + delay/OSPF_LSA_REFRESHER_GRANULARITY)
 	% (OSPF_LSA_REFRESHER_SLOTS);
@@ -3829,7 +3825,7 @@
      modulus. */
   ospf->lsa_refresh_queue.index =
    ((unsigned long)(ospf->lsa_refresh_queue.index +
-		    (time (NULL) - ospf->lsa_refresher_started) /
+		    (quagga_time (NULL) - ospf->lsa_refresher_started) /
 		    OSPF_LSA_REFRESHER_GRANULARITY)) % OSPF_LSA_REFRESHER_SLOTS;
 
   if (IS_DEBUG_OSPF (lsa, LSA_REFRESH))
@@ -3867,7 +3863,7 @@
 
   ospf->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
 					   ospf, ospf->lsa_refresh_interval);
-  ospf->lsa_refresher_started = time (NULL);
+  ospf->lsa_refresher_started = quagga_time (NULL);
 
   for (ALL_LIST_ELEMENTS (lsa_to_refresh, node, nnode, lsa))
     ospf_lsa_refresh (ospf, lsa);
diff --git a/ospfd/ospf_nsm.c b/ospfd/ospf_nsm.c
index e3517cd..69bd48c 100644
--- a/ospfd/ospf_nsm.c
+++ b/ospfd/ospf_nsm.c
@@ -617,10 +617,10 @@
 
   /* Advance in NSM */
   if (next_state > nbr->state)
-    nbr->ts_last_progress = recent_time;
+    nbr->ts_last_progress = recent_relative_time ();
   else /* regression in NSM */
     {
-      nbr->ts_last_regress = recent_time;
+      nbr->ts_last_regress = recent_relative_time ();
       nbr->last_regress_str = ospf_nsm_event_str [event];
     }
 
@@ -747,7 +747,7 @@
   if (state == NSM_ExStart)
     {
       if (nbr->dd_seqnum == 0)
-	nbr->dd_seqnum = time (NULL);
+	nbr->dd_seqnum = quagga_time (NULL);
       else
 	nbr->dd_seqnum++;
 
diff --git a/ospfd/ospf_packet.c b/ospfd/ospf_packet.c
index 6449e63..d7a3564 100644
--- a/ospfd/ospf_packet.c
+++ b/ospfd/ospf_packet.c
@@ -337,7 +337,9 @@
     return 0;
 
   /* We do this here so when we dup a packet, we don't have to
-     waste CPU rewriting other headers. */
+     waste CPU rewriting other headers.
+     
+     Note that quagga_time /deliberately/ is not used here */
   t = (time(NULL) & 0xFFFFFFFF);
   if (t > oi->crypt_seqnum)
     oi->crypt_seqnum = t;
@@ -444,7 +446,7 @@
 		  fired.  This is a small tweak to what is in the RFC,
 		  but it will cut out out a lot of retransmit traffic
 		  - MAG */
-		if (tv_cmp (tv_sub (recent_time, lsa->tv_recv), 
+		if (tv_cmp (tv_sub (recent_relative_time (), lsa->tv_recv), 
 			    int2tv (retransmit_interval)) >= 0)
 		  listnode_add (update, rn->info);
 	    }
@@ -1363,7 +1365,7 @@
 	  else
 	    {
 	      struct timeval t, now;
-	      gettimeofday (&now, NULL);
+	      quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
 	      t = tv_sub (now, nbr->last_send_ts);
 	      if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
 		{
@@ -1948,7 +1950,7 @@
 	    {
 	      struct timeval now;
 	      
-	      gettimeofday (&now, NULL);
+	      quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
 	      
 	      if (tv_cmp (tv_sub (now, current->tv_orig), 
 			  int2tv (OSPF_MIN_LS_ARRIVAL)) > 0)
@@ -3158,7 +3160,7 @@
   if (nbr->last_send)
     ospf_packet_free (nbr->last_send);
   nbr->last_send = ospf_packet_dup (op);
-  gettimeofday (&nbr->last_send_ts, NULL);
+  quagga_gettime (QUAGGA_CLK_MONOTONIC, &nbr->last_send_ts);
 }
 
 /* Re-send Database Description. */
diff --git a/ospfd/ospf_route.c b/ospfd/ospf_route.c
index e0f2565..3a1fa99 100644
--- a/ospfd/ospf_route.c
+++ b/ospfd/ospf_route.c
@@ -47,7 +47,7 @@
 
   new = XCALLOC (MTYPE_OSPF_ROUTE, sizeof (struct ospf_route));
 
-  new->ctime = time (NULL);
+  new->ctime = quagga_time (NULL);
   new->mtime = new->ctime;
   new->paths = list_new ();
   new->paths->del = (void (*) (void *))ospf_path_free;
diff --git a/ospfd/ospf_spf.c b/ospfd/ospf_spf.c
index 7228d2d..a133d5f 100644
--- a/ospfd/ospf_spf.c
+++ b/ospfd/ospf_spf.c
@@ -1136,7 +1136,7 @@
   /* Increment SPF Calculation Counter. */
   area->spf_calculation++;
 
-  gettimeofday (&area->ospf->ts_spf, NULL);
+  quagga_gettime (QUAGGA_CLK_MONOTONIC, &area->ospf->ts_spf);
 
   if (IS_DEBUG_OSPF_EVENT)
     zlog_debug ("ospf_spf_calculate: Stop. %ld vertices",
@@ -1243,7 +1243,7 @@
     }
   
   /* XXX Monotic timers: we only care about relative time here. */
-  result = tv_sub (recent_time, ospf->ts_spf);
+  result = tv_sub (recent_relative_time (), ospf->ts_spf);
   
   elapsed = (result.tv_sec * 1000) + (result.tv_usec / 1000);
   ht = ospf->spf_holdtime * ospf->spf_hold_multiplier;
diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c
index 912f1d0..04e1df4 100644
--- a/ospfd/ospf_vty.c
+++ b/ospfd/ospf_vty.c
@@ -2691,7 +2691,7 @@
   vty_out (vty, " SPF algorithm ");
   if (ospf->ts_spf.tv_sec || ospf->ts_spf.tv_usec)
     {
-      result = tv_sub (recent_time, ospf->ts_spf);
+      result = tv_sub (recent_relative_time (), ospf->ts_spf);
       vty_out (vty, "last executed %s ago%s",
                ospf_timeval_dump (&result, timebuf, sizeof (timebuf)),
                VTY_NEWLINE);
@@ -3157,7 +3157,8 @@
   vty_out (vty, " %d state changes%s", nbr->state_change, VTY_NEWLINE);
   if (nbr->ts_last_progress.tv_sec || nbr->ts_last_progress.tv_usec)
     {
-      struct timeval res = tv_sub (recent_time, nbr->ts_last_progress);
+      struct timeval res
+        = tv_sub (recent_relative_time (), nbr->ts_last_progress);
       vty_out (vty, "    Most recent state change statistics:%s",
                VTY_NEWLINE);
       vty_out (vty, "      Progressive change %s ago%s",
@@ -3166,7 +3167,8 @@
     }
   if (nbr->ts_last_regress.tv_sec || nbr->ts_last_regress.tv_usec)
     {
-      struct timeval res = tv_sub (recent_time, nbr->ts_last_regress);
+      struct timeval res
+        = tv_sub (recent_relative_time (), nbr->ts_last_regress);
       vty_out (vty, "      Regressive change %s ago, due to %s%s",
                ospf_timeval_dump (&res, timebuf, sizeof(timebuf)),
                (nbr->last_regress_str ? nbr->last_regress_str : "??"),
diff --git a/ospfd/ospfd.c b/ospfd/ospfd.c
index 95615e4..a1f0f01 100644
--- a/ospfd/ospfd.c
+++ b/ospfd/ospfd.c
@@ -202,7 +202,7 @@
   new->lsa_refresh_interval = OSPF_LSA_REFRESH_INTERVAL_DEFAULT;
   new->t_lsa_refresher = thread_add_timer (master, ospf_lsa_refresh_walker,
 					   new, new->lsa_refresh_interval);
-  new->lsa_refresher_started = time (NULL);
+  new->lsa_refresher_started = quagga_time (NULL);
 
   if ((new->fd = ospf_sock_init()) < 0)
     {
@@ -1317,7 +1317,7 @@
     return 1;
 
   time_left = ospf->lsa_refresh_interval -
-    (time (NULL) - ospf->lsa_refresher_started);
+    (quagga_time (NULL) - ospf->lsa_refresher_started);
   
   if (time_left > interval)
     {
@@ -1336,7 +1336,7 @@
   int time_left;
 
   time_left = ospf->lsa_refresh_interval -
-    (time (NULL) - ospf->lsa_refresher_started);
+    (quagga_time (NULL) - ospf->lsa_refresher_started);
 
   if (time_left > OSPF_LSA_REFRESH_INTERVAL_DEFAULT)
     {
@@ -1657,5 +1657,5 @@
   om = &ospf_master;
   om->ospf = list_new ();
   om->master = thread_master_create ();
-  om->start_time = time (NULL);
+  om->start_time = quagga_time (NULL);
 }