chiark / gitweb /
Fix segfault on failure to add peer specified by IP address.
[tripe] / peer.c
diff --git a/peer.c b/peer.c
index 5fdfc1bca4ec14354039b72a461d34e95985ccf4..402d08b556e120d7668d159184798236d747aa9b 100644 (file)
--- a/peer.c
+++ b/peer.c
 static peer *peers = 0;
 static sel_file sock;
 
+/*----- 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;
+
 /*----- Main code ---------------------------------------------------------*/
 
 /* --- @p_read@ --- *
@@ -80,8 +96,10 @@ static void p_read(int fd, unsigned mode, void *v)
   return;
 
 found:
-  T( trace(T_PEER, "peer: packet received from `%s'", p->name);
-     trace_block(T_PACKET, "peer: packet contents", buf_i, n); )
+  IF_TRACING(T_PEER, {
+    trace(T_PEER, "peer: packet received from `%s'", p->name);
+    trace_block(T_PACKET, "peer: packet contents", buf_i, n);
+  })
 
   /* --- Pick the packet apart --- */
 
@@ -109,7 +127,7 @@ found:
       if (BOK(&bb)) {
        p->st.n_ipin++;
        p->st.sz_ipin += BSZ(&b);
-       tun_inject(&p->t, &bb);
+       p->t->ops->inject(p->t, &bb);
       } else {
        p->st.n_reject++;
        a_warn("PEER %s packet-build-failed", p->name);
@@ -232,7 +250,7 @@ stats *p_stats(peer *p) { return (&p->st); }
  * Returns:    A pointer to the peer's interface name.
  */
 
-const char *p_ifname(peer *p) { return (tun_ifname(&p->t)); }
+const char *p_ifname(peer *p) { return (p->t->ops->ifname(p->t)); }
 
 /* --- @p_addr@ --- *
  *
@@ -307,6 +325,7 @@ unsigned p_port(void)
 /* --- @p_create@ --- *
  *
  * Arguments:  @const char *name@ = name for this peer
+ *             @const tunnel_ops *tops@ = tunnel to use
  *             @struct sockaddr *sa@ = socket address of peer
  *             @size_t sz@ = size of socket address
  *
@@ -316,9 +335,11 @@ unsigned p_port(void)
  *             by this point.
  */
 
-peer *p_create(const char *name, struct sockaddr *sa, size_t sz)
+peer *p_create(const char *name, const tunnel_ops *tops,
+              struct sockaddr *sa, size_t sz)
 {
   peer *p = CREATE(peer);
+
   T( trace(T_PEER, "peer: creating new peer `%s'", name); )
   p->name = xstrdup(name);
   p->ks = 0;
@@ -329,7 +350,7 @@ peer *p_create(const char *name, struct sockaddr *sa, size_t sz)
   p->st.t_start = time(0);
   if (kx_init(&p->kx, p, &p->ks))
     goto tidy_0;
-  if (tun_create(&p->t, p))
+  if ((p->t = tops->create(p)) == 0)
     goto tidy_1;
   p->next = peers;
   if (peers)
@@ -337,13 +358,14 @@ peer *p_create(const char *name, struct sockaddr *sa, size_t sz)
   peers = p;
   switch (p->peer.sa.sa_family) {
     case AF_INET:
-      a_notify("ADD %s INET %s %u",
+      a_notify("ADD %s %s INET %s %u",
               name,
+              p->t->ops->ifname(p->t),
               inet_ntoa(p->peer.sin.sin_addr),
               (unsigned)ntohs(p->peer.sin.sin_port));
       break;
     default:
-      a_notify("ADD %s UNKNOWN", name);
+      a_notify("ADD %s %s UNKNOWN", name, p->t->ops->ifname(p->t));
       break;
   }
   a_notify("KXSTART %s", name);                /* Couldn't tell anyone before */
@@ -400,7 +422,7 @@ void p_destroy(peer *p)
   a_notify("KILL %s", p->name);
   ksl_free(&p->ks);
   kx_free(&p->kx);
-  tun_destroy(&p->t);
+  p->t->ops->destroy(p->t);
   xfree(p->name);
   if (p->next)
     p->next->prev = p->prev;