chiark / gitweb /
server/peer.c: Always send `BYE' packets on peer death.
[tripe] / server / peer.c
index a8099e41adb7776a026f283b10bf625d810eb65c..dab3bfcf08e1a409be0e7f7dd1198caa6cd635bb 100644 (file)
 
 /*----- Global state ------------------------------------------------------*/
 
-sel_file udpsock[NADDRFAM];
+udpsocket udpsock[NADDRFAM];
 
 /*----- Static variables --------------------------------------------------*/
 
 static sym_table byname;
 static addrmap byaddr;
 static unsigned nmobile;
-
-/*----- Tunnel table ------------------------------------------------------*/
-
-const tunnel_ops *tunnels[] = {
-#ifdef TUN_LINUX
-  &tun_linux,
-#endif
-#ifdef TUN_BSD
-  &tun_bsd,
-#endif
-#ifdef TUN_UNET
-  &tun_unet,
-#endif
-  &tun_slip,
-  0
-}, *tun_default;
+static struct tunnel_node {
+  struct tunnel_node *next;
+  const tunnel_ops *tops;
+} *tunnels, **tunnels_tail = &tunnels;
+const tunnel_ops *dflttun;
 
 /*----- Main code ---------------------------------------------------------*/
 
@@ -527,9 +516,14 @@ buf *p_txstart(peer *p, unsigned msg)
 int p_txaddr(const addr *a, const void *p, size_t sz)
 {
   socklen_t sasz = addrsz(a);
+  int i;
 
+  if ((i = afix(a->sa.sa_family)) < 0) {
+    a_warn("PEER", "?ADDR", a, "disabled-address-family", A_END);
+    return (-1);
+  }
   IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet", p, sz); )
-  if (sendto(sock.fd, p, sz, 0, &a->sa, sasz) < 0) {
+  if (sendto(udpsock[i].sf.fd, p, sz, 0, &a->sa, sasz) < 0) {
     a_warn("PEER", "?ADDR", a, "socket-write-error", "?ERRNO", A_END);
     return (-1);
   }
@@ -557,7 +551,7 @@ static int p_dotxend(peer *p)
   }
   IF_TRACING(T_PEER, trace_block(T_PACKET, "peer: sending packet",
                                 BBASE(&p->b), BLEN(&p->b)); )
-  if (sendto(udpsock[p->afix].fd, BBASE(&p->b), BLEN(&p->b),
+  if (sendto(udpsock[p->afix].sf.fd, BBASE(&p->b), BLEN(&p->b),
             0, &p->spec.sa.sa, sasz) < 0) {
     a_warn("PEER", "?PEER", p, "socket-write-error", "?ERRNO", A_END);
     return (0);
@@ -811,18 +805,18 @@ void p_setifname(peer *p, const char *name)
 
 const addr *p_addr(peer *p) { return (&p->spec.sa); }
 
-/* --- @p_init@ --- *
+/* --- @p_bind@ --- *
  *
  * Arguments:  @struct addrinfo *ailist@ = addresses to bind to
  *
- * Returns:    ---
+ * Returns:    Zero on success, @-1@ on failure.
  *
- * Use:                Initializes the peer system; creates the socket.
+ * Use:                Binds to the main UDP sockets.
  */
 
-void p_init(struct addrinfo *ailist)
+int p_bind(struct addrinfo *ailist)
 {
-  int fd;
+  int fd = -1;
   int len = PKBUFSZ;
   int yes = 1;
   int i;
@@ -831,11 +825,11 @@ void p_init(struct addrinfo *ailist)
   addr a;
   socklen_t sz;
 
-  for (i = 0; i < NADDRFAM; i++) udpsock[i].fd = -1;
+  for (i = 0; i < NADDRFAM; i++) udpsock[i].sf.fd = -1;
 
   for (ai = ailist; ai; ai = ai->ai_next) {
     if ((i = afix(ai->ai_family)) < 0) continue;
-    if (udpsock[i].fd != -1) continue;
+    if (udpsock[i].sf.fd != -1) continue;
 
     /* --- Note on socket buffer sizes --- *
      *
@@ -845,56 +839,192 @@ void p_init(struct addrinfo *ailist)
      * than second-guessing me.
      */
 
-    if ((fd = socket(ai->ai_family, SOCK_DGRAM, 0)) < 0)
-      die(EXIT_FAILURE, "socket creation failed: %s", strerror(errno));
+    if ((fd = socket(ai->ai_family, SOCK_DGRAM, 0)) < 0) {
+      a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+            "create-failed", "?ERRNO", A_END);
+      goto fail;
+    }
     if (i == AFIX_INET6 &&
        setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &yes, sizeof(yes))) {
-      die(EXIT_FAILURE, "failed to set IPv6-only state: %s",
-         strerror(errno));
+      a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+            "set-v6only-failed", "?ERRNO", A_END);
+      goto fail;
     }
     assert(ai->ai_addrlen <= sizeof(a));
     memcpy(&a, ai->ai_addr, ai->ai_addrlen);
     if ((port = getport(&a)) == 0 && lastport) setport(&a, lastport);
-    if (bind(fd, &a.sa, addrsz(&a)))
-      die(EXIT_FAILURE, "bind failed: %s", strerror(errno));
+    if (bind(fd, &a.sa, addrsz(&a))) {
+      a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+            "bind-failed", "?ERRNO", A_END);
+      goto fail;
+    }
     if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)) ||
        setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &len, sizeof(len))) {
-      die(EXIT_FAILURE, "failed to set socket buffer sizes: %s",
-         strerror(errno));
+      a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+            "set-buffers-failed", "?ERRNO", A_END);
+      goto fail;
     }
     fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
-    sel_initfile(&sel, &udpsock[i], fd, SEL_READ, p_read, 0);
-    sel_addfile(&udpsock[i]);
-    T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
-    if (!port) {
+    if (port)
+      udpsock[i].port = port;
+    else {
       sz = sizeof(a);
       if (getsockname(fd, &a.sa, &sz)) {
-       die(EXIT_FAILURE, "failed to read local socket address: %s",
-           strerror(errno));
+       a_warn("PEER", "-", "udp-socket", "%s", aftab[i].name,
+              "read-local-address-failed", "?ERRNO", A_END);
+       goto fail;
       }
-      lastport = getport(&a);
+      udpsock[i].port = lastport = getport(&a);
     }
+    T( trace(T_PEER, "peer: created %s socket", aftab[i].name); )
+    sel_initfile(&sel, &udpsock[i].sf, fd, SEL_READ, p_read, 0);
+    sel_addfile(&udpsock[i].sf);
+    fd = -1;
   }
 
+  return (0);
+
+fail:
+  if (fd != -1) close(fd);
+  p_unbind();
+  return (-1);
+}
+
+/* --- @p_unbind@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Unbinds the UDP sockets.  There must not be any active peers,
+ *             and none can be created until the sockets are rebound.
+ */
+
+void p_unbind(void)
+{
+  int i;
+
+#ifndef NDEBUG
+  { peer_iter it; p_mkiter(&it); assert(!p_next(&it)); }
+#endif
+
+  for (i = 0; i < NADDRFAM; i++) {
+    if (udpsock[i].sf.fd == -1) continue;
+    sel_rmfile(&udpsock[i].sf);
+    close(udpsock[i].sf.fd);
+    udpsock[i].sf.fd = -1;
+  }
+}
+
+/* --- @p_init@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes the peer system.
+ */
+
+void p_init(void)
+{
   sym_create(&byname);
   am_create(&byaddr);
 }
 
-/* --- @p_port@ --- *
+/* --- @p_addtun@ --- *
+ *
+ * Arguments:  @const tunnel_ops *tops@ = tunnel ops to add
+ *
+ * Returns:    Zero on success, @-1@ on failure.
+ *
+ * Use:                Adds a tunnel class to the list of known classes, if it
+ *             initializes properly.  If there is no current default tunnel,
+ *             then this one is made the default.
+ *
+ *             Does nothing if the tunnel class is already known.  So adding
+ *             a bunch of tunnels takes quadratic time, but there will be
+ *             too few to care about.
+ */
+
+int p_addtun(const tunnel_ops *tops)
+{
+  struct tunnel_node *tn;
+
+  for (tn = tunnels; tn; tn = tn->next)
+    if (tn->tops == tops) return (0);
+  if (tops->init()) return (-1);
+  tn = CREATE(struct tunnel_node);
+  tn->next = 0; tn->tops = tops;
+  *tunnels_tail = tn; tunnels_tail = &tn->next;
+  if (!dflttun) dflttun = tops;
+  return (0);
+}
+
+/* --- @p_setdflttun@ --- *
+ *
+ * Arguments:  @const tunnel_ops *tops@ = tunnel ops to set
+ *
+ * Returns:    ---
+ *
+ * Use:                Sets the default tunnel.  It must already be registered.  The
+ *             old default is forgotten.
+ */
+
+void p_setdflttun(const tunnel_ops *tops)
+  { dflttun = tops; }
+
+/* --- @p_dflttun@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    A pointer to the current default tunnel operations, or null
+ *             if no tunnels are defined.
+ */
+
+const tunnel_ops *p_dflttun(void) { return (dflttun); }
+
+/* --- @p_findtun@ --- *
  *
- * Arguments:  @int i@ = address family index to retrieve
+ * Arguments:  @const char *name@ = tunnel name
  *
- * Returns:    Port number used for socket.
+ * Returns:    Pointer to the tunnel operations, or null.
+ *
+ * Use:                Finds the operations for a named tunnel class.
  */
 
-unsigned p_port(int i)
+const tunnel_ops *p_findtun(const char *name)
 {
-  addr a;
-  socklen_t sz = sizeof(addr);
+  const struct tunnel_node *tn;
 
-  if (getsockname(udpsock[i].fd, &a.sa, &sz))
-    die(EXIT_FAILURE, "couldn't read port number: %s", strerror(errno));
-  return (getport(&a));
+  for (tn = tunnels; tn; tn = tn->next)
+    if (mystrieq(tn->tops->name, name) == 0) return (tn->tops);
+  return (0);
+}
+
+/* --- @p_mktuniter@ --- *
+ *
+ * Arguments:  @tuniter *i@ = pointer to iterator to initialize
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes a tunnel iterator.
+ */
+
+void p_mktuniter(tun_iter *i) { i->next = tunnels; }
+
+/* --- @p_nexttun@ --- *
+ *
+ * Arguments:  @tuniter *i@ = pointer to iterator
+ *
+ * Returns:    Pointer to the next tunnel's operations, or null.
+ */
+
+const tunnel_ops *p_nexttun(tun_iter *i)
+{
+  const struct tunnel_node *tn = i->next;
+
+  if (!tn) return (0);
+  else { i->next = tn->next; return (tn->tops); }
 }
 
 /* --- @p_keepalive@ --- *
@@ -980,6 +1110,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);
+  iv_addreason();
   if (kx_setup(&p->kx, p, &p->ks, p->spec.f & PSF_KXMASK))
     goto tidy_4;
   a_notify("ADD",
@@ -998,6 +1129,7 @@ tidy_4:
   if (spec->t_ka) sel_rmtimer(&p->tka);
   xfree(p->ifname);
   p->t->ops->destroy(p->t);
+  iv_rmreason();
 tidy_3:
   if (fd >= 0) close(fd);
 tidy_2:
@@ -1105,7 +1237,7 @@ void p_destroy(peer *p, int bye)
 
   T( trace(T_PEER, "peer: destroying peer `%s'", p->spec.name); )
 
-  if (bye && (p->spec.f&PSF_EPHEM)) {
+  if (bye) {
     b = p_txstart(p, MSG_MISC | MISC_BYE);
     buf_init(&bb, buf_t, sizeof(buf_t));
     assert(BOK(&bb)); buf_flip(&bb);
@@ -1129,9 +1261,21 @@ void p_destroy(peer *p, int bye)
   }
   sym_remove(&byname, p->byname);
   am_remove(&byaddr, p->byaddr);
+  iv_rmreason();
   DESTROY(p);
 }
 
+/* --- @p_destroyall@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Destroys all of the peers, saying goodbye.
+ */
+
+void p_destroyall(void) { FOREACH_PEER(p, { p_destroy(p, 1); }); }
+
 /* --- @p_mkiter@ --- *
  *
  * Arguments:  @peer_iter *i@ = pointer to an iterator