chiark / gitweb /
Correctly tokenize output to admin clients.
[tripe] / servutil.c
index c3465e79b93c2398b5735882b583e3088743c1eb..92a5f775f03b456a0068317508db70294bedcb77 100644 (file)
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: servutil.c,v 1.4 2004/04/03 12:35:13 mdw Exp $
+ * $Id$
  *
  * Various handy server-only utilities
  *
  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: servutil.c,v $
- * Revision 1.4  2004/04/03 12:35:13  mdw
- * Support elliptic curve key exchange.
- *
- * Revision 1.3  2001/06/19 22:08:11  mdw
- * Use magic number for packet size.
- *
- * Revision 1.2  2001/02/16 21:41:06  mdw
- * Use new spare buffer for building MP textual representations.  Add a
- * function for making human-readable time strings.
- *
- * Revision 1.1  2001/02/03 20:26:37  mdw
- * Initial checkin.
- *
- */
-
 /*----- Header files ------------------------------------------------------*/
 
 #include "tripe.h"
@@ -109,4 +91,53 @@ const char *timestr(time_t t)
   return ((const char *)buf_t);
 }
 
+/* --- @seq_reset@ --- *
+ *
+ * Arguments:  @seqwin *s@ = sequence-checking window
+ *
+ * Returns:    ---
+ *
+ * Use:                Resets a sequence number window.
+ */
+
+void seq_reset(seqwin *s) { s->seq = 0; s->win = 0; }
+
+/* --- @seq_check@ --- *
+ *
+ * Arguments:  @seqwin *s@ = sequence-checking window
+ *             @uint32 q@ = sequence number to check
+ *             @const char *service@ = service to report message from
+ *
+ * Returns:    Zero on success, nonzero if the sequence number was bad.
+ *
+ * Use:                Checks a sequence number against the window, updating things
+ *             as necessary.
+ */
+
+int seq_check(seqwin *s, uint32 q, const char *service)
+{
+  uint32 qbit;
+  uint32 n;
+
+  if (q < s->seq) {
+    a_warn(service, "replay", "old-sequence", A_END);
+    return (-1);
+  }
+  if (q >= s->seq + SEQ_WINSZ) {
+    n = q - (s->seq + SEQ_WINSZ - 1);
+    if (n < SEQ_WINSZ)
+      s->win >>= n;
+    else
+      s->win = 0;
+    s->seq += n;
+  }
+  qbit = 1 << (q - s->seq);
+  if (s->win & qbit) {
+    a_warn(service, "replay", "duplicated-sequence", A_END);
+    return (-1);
+  }
+  s->win |= qbit;
+  return (0);
+}
+
 /*----- That's all, folks -------------------------------------------------*/