chiark / gitweb /
server/keyexch.c: Use high-resolution `struct timeval' timers.
[tripe] / server / keyexch.c
index c2f035d47e082b3c2a77b93e62347a2df2cca43b..c27d69fd7cff61df15db5c9c9ef7d58afb8551d0 100644 (file)
  *     Switch received.  Committed; send data; move to @KXS_SWITCH@.
  */
 
-/*----- Tunable parameters ------------------------------------------------*/
-
-#define T_VALID MIN(2)                 /* Challenge validity period */
-#define T_RETRY SEC(10)                        /* Challenge retransmit interval */
-
-#define VALIDP(kx, now) ((now) < (kx)->t_valid)
-
 /*----- Static tables -----------------------------------------------------*/
 
 static const char *const pkname[] = {
@@ -90,6 +83,17 @@ static const char *const pkname[] = {
 
 /*----- Various utilities -------------------------------------------------*/
 
+/* --- @VALIDP@ --- *
+ *
+ * Arguments:  @const keyexch *kx@ = key exchange state
+ *             @time_t now@ = current time in seconds
+ *
+ * Returns:    Whether the challenge in the key-exchange state is still
+ *             valid or should be regenerated.
+ */
+
+#define VALIDP(kx, now) ((now) < (kx)->t_valid)
+
 /* --- @hashge@ --- *
  *
  * Arguments:  @ghash *h@ = pointer to hash context
@@ -261,21 +265,17 @@ static void timer(struct timeval *tv, void *v)
 /* --- @settimer@ --- *
  *
  * Arguments:  @keyexch *kx@ = pointer to key exchange context
- *             @time_t t@ = when to set the timer for
+ *             @struct timeval *tv@ = when to set the timer for
  *
  * Returns:    ---
  *
  * Use:                Sets the timer for the next key exchange attempt.
  */
 
-static void settimer(keyexch *kx, time_t t)
+static void settimer(keyexch *kx, struct timeval *tv)
 {
-  struct timeval tv;
-  if (kx->f & KXF_TIMER)
-    sel_rmtimer(&kx->t);
-  tv.tv_sec = t;
-  tv.tv_usec = 0;
-  sel_addtimer(&sel, &kx->t, &tv, timer, kx);
+  if (kx->f & KXF_TIMER) sel_rmtimer(&kx->t);
+  sel_addtimer(&sel, &kx->t, tv, timer, kx);
   kx->f |= KXF_TIMER;
 }
 
@@ -724,6 +724,7 @@ static void resend(keyexch *kx)
   kxchal *kxc;
   buf bb;
   stats *st = p_stats(kx->p);
+  struct timeval tv;
   buf *b;
 
   switch (kx->s) {
@@ -766,8 +767,11 @@ static void resend(keyexch *kx)
     p_txend(kx->p);
   }
 
-  if (kx->s < KXS_SWITCH)
-    settimer(kx, time(0) + T_RETRY);
+  if (kx->s < KXS_SWITCH) {
+    gettimeofday(&tv, 0);
+    tv.tv_sec += T_RETRY;
+    settimer(kx, &tv);
+  }
 }
 
 /* --- @decryptrest@ --- *
@@ -792,6 +796,7 @@ static int decryptrest(keyexch *kx, kxchal *kxc, unsigned msg, buf *b)
     a_warn("KX", "?PEER", kx->p, "decrypt-failed", "%s", pkname[msg], A_END);
     return (-1);
   }
+  if (!BOK(&bb)) return (-1);
   buf_init(b, BBASE(&bb), BLEN(&bb));
   return (0);
 }
@@ -907,8 +912,12 @@ bad:
 static void kxfinish(keyexch *kx)
 {
   kxchal *kxc = kx->r[0];
+  struct timeval tv;
+
   ks_activate(kxc->ks);
-  settimer(kx, ks_tregen(kxc->ks));
+  gettimeofday(&tv, 0);
+  tv.tv_sec += T_REGEN;
+  settimer(kx, &tv);
   kx->s = KXS_SWITCH;
   a_notify("KXDONE", "?PEER", kx->p, A_END);
   p_stats(kx->p)->t_kx = time(0);
@@ -1155,23 +1164,25 @@ void kx_start(keyexch *kx, int forcep)
 
 void kx_message(keyexch *kx, unsigned msg, buf *b)
 {
-  time_t now = time(0);
+  struct timeval now, tv;
   stats *st = p_stats(kx->p);
   size_t sz = BSZ(b);
   int rc;
 
+  gettimeofday(&now, 0);
   if (kx->f & KXF_CORK) {
-    start(kx, now);
-    settimer(kx, now + T_RETRY);
+    start(kx, now.tv_sec);
+    TV_ADDL(&tv, &now, T_RETRY, 0);
+    settimer(kx, &tv);
     a_notify("KXSTART", A_END);
   }
 
   if (checkpub(kx))
     return;
 
-  if (!VALIDP(kx, now)) {
+  if (!VALIDP(kx, now.tv_sec)) {
     stop(kx);
-    start(kx, now);
+    start(kx, now.tv_sec);
   }
   T( trace(T_KEYEXCH, "keyexch: processing %s packet from `%s'",
           msg < KX_NMSG ? pkname[msg] : "unknown", p_name(kx->p)); )
@@ -1235,7 +1246,7 @@ void kx_free(keyexch *kx)
 
 void kx_newkeys(keyexch *kx)
 {
-  if (km_getpubkey(p_name(kx->p), kx->kpub, &kx->texp_kpub))
+  if (km_getpubkey(p_tag(kx->p), kx->kpub, &kx->texp_kpub))
     return;
   kx->f |= KXF_PUBKEY;
   if ((kx->f & KXF_DEAD) || kx->s != KXS_SWITCH) {
@@ -1266,7 +1277,7 @@ int kx_init(keyexch *kx, peer *p, keyset **ks, unsigned f)
   kx->ks = ks;
   kx->p = p;
   kx->kpub = G_CREATE(gg);
-  if (km_getpubkey(p_name(p), kx->kpub, &kx->texp_kpub)) {
+  if (km_getpubkey(p_tag(p), kx->kpub, &kx->texp_kpub)) {
     G_DESTROY(gg, kx->kpub);
     return (-1);
   }