chiark / gitweb /
Add notion of `ephemeral' associations and a goodbye protocol.
[tripe] / server / peer.c
index 891888b0b56aef503798ed45dbb2bd12794a8283..606b2038d0c76f93a64af77d539566786de0fe7f 100644 (file)
@@ -166,6 +166,51 @@ static int p_encrypt(peer *p, int ty, buf *bin, buf *bout)
   return (err);
 }
 
+/* --- @p_updateaddr@ --- *
+ *
+ * Arguments:  @peer *p@ = pointer to peer block
+ *             @const addr *a@ = address to associate with this peer
+ *
+ * Returns:    Zero if the address was changed; @+1@ if it was already
+ *             right.
+ *
+ * Use:                Updates our idea of @p@'s address.
+ */
+
+int p_updateaddr(peer *p, const addr *a)
+{
+  peer *q;
+  peer_byaddr *pa, *qa;
+  unsigned f;
+
+  /* --- Figure out how to proceed --- *
+   *
+   * If this address already belongs to a different peer, then swap the
+   * addresses over.  This doesn't leave the displaced peer in an especially
+   * good state, but it ought to get sorted out soon enough.
+   */
+
+  pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f);
+  if (f && pa->p == p)
+    return (+1);
+  else if (!f) {
+    T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); )
+    am_remove(&byaddr, p->byaddr);
+    p->byaddr = pa; p->spec.sa = *a; pa->p = p;
+    a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
+    return (0);
+  } else {
+    q = pa->p; qa = p->byaddr;
+    T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'",
+            p_name(p), p_name(q)); )
+    q->byaddr = qa; qa->p = q; q->spec.sa = p->spec.sa;
+    p->byaddr = pa; pa->p = p; p->spec.sa = *a;
+    a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
+    a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END);
+    return (0);
+  }
+}
+
 /* --- @p_decrypt@ --- *
  *
  * Arguments:  @peer **pp@ = pointer to peer to decrypt message from
@@ -188,9 +233,7 @@ static int p_decrypt(peer **pp, addr *a, size_t n,
                     int ty, buf *bin, buf *bout)
 {
   peer *p, *q;
-  peer_byaddr *pa, *qa;
   int err = KSERR_DECRYPT;
-  unsigned f;
 
   /* --- If we have a match on the source address then try that first --- */
 
@@ -248,33 +291,11 @@ searched:
     return (-1);
   }
 
-  /* --- We found one that accepted, so update the peer's address --- *
-   *
-   * If we had an initial guess of which peer this packet came from -- i.e.,
-   * @q@ is not null -- then swap the addresses over.  This doesn't leave the
-   * evicted peer in an especially good state, but it ought to get sorted out
-   * soon enough.
-   */
+  /* --- We found one that accepted, so update the peer's address --- */
 
   if (!err) {
     *pp = p;
-    if (!q) {
-      T( trace(T_PEER, "peer: updating address for `%s'", p_name(p)); )
-      pa = am_find(&byaddr, a, sizeof(peer_byaddr), &f); assert(!f);
-      am_remove(&byaddr, p->byaddr);
-      p->byaddr = pa;
-      pa->p = p;
-      p->spec.sa = *a;
-      a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
-    } else {
-      T( trace(T_PEER, "peer: swapping addresses for `%s' and `%s'",
-              p_name(p), p_name(q)); )
-      pa = p->byaddr; qa = q->byaddr;
-      pa->p = q; q->byaddr = pa; q->spec.sa = p->spec.sa;
-      qa->p = p; p->byaddr = qa; p->spec.sa = *a;
-      a_notify("NEWADDR", "?PEER", p, "?ADDR", a, A_END);
-      a_notify("NEWADDR", "?PEER", q, "?ADDR", &q->spec.sa, A_END);
-    }
+    p_updateaddr(p, a);
   }
 
 match:
@@ -331,7 +352,7 @@ static void p_read(int fd, unsigned mode, void *v)
     })
     buf_init(&b, buf_i, n);
     buf_getbyte(&b);
-    if (c_check(&b) || BLEFT(&b)) {
+    if (c_check(0, 0, &b) || BLEFT(&b)) {
       a_warn("PEER", "-", "invalid-greeting", A_END);
       return;
     }
@@ -392,9 +413,8 @@ static void p_read(int fd, unsigned mode, void *v)
       }
       break;
     case MSG_KEYEXCH:
-      if (!p) goto unexp;
-      p_rxupdstats(p, n);
-      kx_message(&p->kx, ch & MSG_TYPEMASK, &b);
+      if (p) p_rxupdstats(p, n);
+      if (kx_message(p ? &p->kx : 0, &a, ch & MSG_TYPEMASK, &b)) goto unexp;
       break;
     case MSG_MISC:
       switch (ch & MSG_TYPEMASK) {
@@ -434,6 +454,16 @@ static void p_read(int fd, unsigned mode, void *v)
            p_ponged(p, MISC_EPONG, &bb);
          }
          break;
+       case MISC_BYE:
+         buf_init(&bb, buf_t, sizeof(buf_t));
+         if (p_decrypt(&p, &a, n, ch, &b, &bb)) return;
+         if (!(p->spec.f&PSF_EPHEM)) return;
+         if (BOK(&bb)) {
+           buf_flip(&bb);
+           if (BSZ(&bb)) return;
+           p_destroy(p, 0);
+         }
+         break;
       }
       break;
     default:
@@ -468,6 +498,30 @@ buf *p_txstart(peer *p, unsigned msg)
   return (&p->b);
 }
 
+/* --- @p_txaddr@ --- *
+ *
+ * Arguments:  @const addr *a@ = recipient address
+ *             @const void *p@ = pointer to packet to send
+ *             @size_t sz@ = length of packet
+ *
+ * Returns:    Zero if successful, nonzero on error.
+ *
+ * Use:                Sends a packet to an address which (possibly) isn't a current
+ *             peer.
+ */
+
+int p_txaddr(const addr *a, const void *p, size_t sz)
+{
+  socklen_t sasz = addrsz(a);
+
+  IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet", p, sz); )
+  if (sendto(sock.fd, p, sz, 0, &a->sa, sasz) < 0) {
+    a_warn("PEER", "?ADDR", a, "socket-write-error", "?ERRNO", A_END);
+    return (-1);
+  }
+  return (0);
+}
+
 /* --- @p_txend@ --- *
  *
  * Arguments:  @peer *p@ = pointer to peer block
@@ -874,6 +928,7 @@ peer *p_create(peerspec *spec)
   p->spec.name = (/*unconst*/ char *)SYM_NAME(p->byname);
   if (spec->tag) p->spec.tag = xstrdup(spec->tag);
   if (spec->privtag) p->spec.privtag = xstrdup(spec->privtag);
+  if (spec->knock) p->spec.knock = xstrdup(spec->knock);
   p->ks = 0;
   p->pings = 0;
   p->ifname = 0;
@@ -888,7 +943,7 @@ peer *p_create(peerspec *spec)
   T( trace(T_TUNNEL, "peer: attached interface %s to peer `%s'",
           p->ifname, p_name(p)); )
   p_setkatimer(p);
-  if (kx_init(&p->kx, p, &p->ks, p->spec.f & PSF_KXMASK))
+  if (kx_setup(&p->kx, p, &p->ks, p->spec.f & PSF_KXMASK))
     goto tidy_4;
   a_notify("ADD",
           "?PEER", p,
@@ -999,17 +1054,28 @@ peer *p_find(const char *name)
 /* --- @p_destroy@ --- *
  *
  * Arguments:  @peer *p@ = pointer to a peer
+ *             @int bye@ = say goodbye to the peer?
  *
  * Returns:    ---
  *
  * Use:                Destroys a peer.
  */
 
-void p_destroy(peer *p)
+void p_destroy(peer *p, int bye)
 {
   ping *pg, *ppg;
+  buf *b, bb;
 
   T( trace(T_PEER, "peer: destroying peer `%s'", p->spec.name); )
+
+  if (bye && (p->spec.f&PSF_EPHEM)) {
+    b = p_txstart(p, MSG_MISC | MISC_BYE);
+    buf_init(&bb, buf_t, sizeof(buf_t));
+    assert(BOK(&bb)); buf_flip(&bb);
+    p_encrypt(p, MSG_MISC | MISC_BYE, &bb, b);
+    p_txend(p);
+  }
+
   a_notify("KILL", "%s", p->spec.name, A_END);
   ksl_free(&p->ks);
   kx_free(&p->kx);
@@ -1017,6 +1083,7 @@ void p_destroy(peer *p)
   if (p->ifname) xfree(p->ifname);
   if (p->spec.tag) xfree(p->spec.tag);
   if (p->spec.privtag) xfree(p->spec.privtag);
+  if (p->spec.knock) xfree(p->spec.knock);
   p->t->ops->destroy(p->t);
   if (p->spec.t_ka) sel_rmtimer(&p->tka);
   for (pg = p->pings; pg; pg = ppg) {