Greetings and challenges.
[tripe] / keyset.c
index cdd0c99..9b804ea 100644 (file)
--- a/keyset.c
+++ b/keyset.c
@@ -265,27 +265,12 @@ static int dodecrypt(keyset *ks, unsigned ty, buf *b, buf *bb, uint32 *seq)
 
 static int dosequence(keyset *ks, uint32 seq)
 {
-  uint32 seqbit;
-  uint32 n;
-
-  if (seq < ks->iseq) {
-    a_warn("SYMM replay old-sequence");
-    return (-1);
-  }
-  if (seq >= ks->iseq + KS_SEQWINSZ) {
-    n = seq - (ks->iseq + KS_SEQWINSZ - 1);
-    if (n < KS_SEQWINSZ)
-      ks->iwin >>= n;
-    else
-      ks->iwin = 0;
-    ks->iseq += n;
-  }
-  seqbit = 1 << (seq - ks->iseq);
-  if (ks->iwin & seqbit) {
-    a_warn("SYMM replay duplicated-sequence");
-    return (-1);
+  switch (seq_check(&ks->iseq, seq)) {
+    case SEQ_OK: break;
+    case SEQ_OLD: a_warn("SYMM replay old-sequence"); return (-1);
+    case SEQ_REPLAY: a_warn("SYMM replay duplicated-sequence"); return (-1);
+    default: abort();
   }
-  ks->iwin |= seqbit;
   return (0);
 }
 
@@ -397,8 +382,8 @@ keyset *ks_gen(const void *k, size_t x, size_t y, size_t z, peer *p)
   ks->ref = 1;
   ks->t_exp = now + T_EXP;
   ks->sz_exp = SZ_EXP;
-  ks->oseq = ks->iseq = 0;
-  ks->iwin = 0;
+  ks->oseq = 0;
+  seq_reset(&ks->iseq);
   ks->next = 0;
   ks->p = p;
   ks->f = KSF_LISTEN;