chiark / gitweb /
www-cgi/: Move `xrealloc' to `ucgicommon'.
[userv-utils.git] / ipif / mech-timestamp.c
index e250c084d89f87fe26472618c7eae06afd2feffd..793ea6c4aeae7a021b6b3da629dcc8b515743085 100644 (file)
@@ -1,8 +1,10 @@
 /*
- * Timestamp mechanism
+ * Timestamp mechanism for udp tunnel
  *
+ * mechanism: timestamp
  * arguments: <max-skew> <max-age>
  *
+ * restrictions: none
  * encoding: prepend 4 bytes of UNIX time in network byte order
  *
  * <max-age> is maximum age in seconds we will accept a packet (or 0
  * accept a packet (or 0 for any future age).
  *
  */
+/*
+ * Copyright (C) 2000,2003 Ian Jackson
+ * This file is part of ipif, part of userv-utils
+ *
+ * This 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 userv-utils; if not, write to the Free Software
+ * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
 
 #include <stdint.h>
 #include <netinet/in.h>
 
 #include "forwarder.h"
 
+#define WARN_EVERY 30
+
 struct mechdata {
-  uint32_t max_skew, max_age;
+  time_t max_skew, max_age;
+  time_t next_warn;
 };
 
 static void mds_timestamp(struct mechdata **md_r) {
@@ -27,6 +50,7 @@ static void mds_timestamp(struct mechdata **md_r) {
   
   md->max_skew= getarg_ulong();
   md->max_age= getarg_ulong();
+  md->next_warn= now();
   *md_r= md;
 }
 
@@ -42,27 +66,29 @@ static void menc_timestamp(struct mechdata *md, struct buffer *buf) {
 static const char *mdec_timestamp(struct mechdata *md, struct buffer *buf) {
   static char cbuf[40];
   
-  uint32_t *tp, timestamp, tnow;
+  uint32_t *tp, timestamp;
+  time_t tnow;
   long age;
 
   BUF_UNPREPEND(tp,buf,4);
   timestamp= ntohl(*tp);
 
   tnow= now();
-  age= timestamp - tnow;
+  age= timestamp - (uint32_t)tnow;
   if (age > 0) {
-    if (md->max_age && age > md->max_age) {
-      sprintf(cbuf,"packet too old (%lds)",age);
-      return cbuf;
-    }
+    if (!md->max_age || age <= md->max_age) return 0;
+    sprintf(cbuf,"packet too old (%lds)",age);
   } else if (age < 0) {
-    if (md->max_skew && age > md->max_skew) {
-      sprintf(cbuf,"too much skew (%lds)",-age);
-      return cbuf;
-    }
+    if (!md->max_skew || age >= -md->max_skew) return 0;
+    sprintf(cbuf,"too much skew (%lds)",-age);
+  } else {
+    return 0;
   }
 
-  return 0;
+  if (tnow < md->next_warn) return "";
+
+  md->next_warn= tnow+WARN_EVERY;
+  return cbuf;
 }
 
 STANDARD_MECHANISMLIST("timestamp",timestamp);