From 2c15b5701fcea291ff7455140d5c49e7e915505b Mon Sep 17 00:00:00 2001
From: Richard Nyberg <rnyberg@murmeldjur.se>
Date: Wed, 13 Sep 2006 08:18:33 +0000
Subject: [PATCH] No need to have the net structs on a list. Fix a test for
 finding a torrent with active net.

---
 btpd/net.c       | 27 ++++++++++-----------------
 btpd/net_types.h |  3 ---
 2 files changed, 10 insertions(+), 20 deletions(-)

diff --git a/btpd/net.c b/btpd/net.c
index 02171ed..912965c 100644
--- a/btpd/net.c
+++ b/btpd/net.c
@@ -26,9 +26,6 @@ static unsigned long m_rate_dwn;
 
 static struct event m_net_incoming;
 
-static unsigned m_ntorrents;
-static struct net_tq m_torrents = BTPDQ_HEAD_INITIALIZER(m_torrents);
-
 unsigned net_npeers;
 
 struct peer_tq net_bw_readq = BTPDQ_HEAD_INITIALIZER(net_bw_readq);
@@ -78,8 +75,6 @@ void
 net_start(struct torrent *tp)
 {
     struct net *n = tp->net;
-    BTPDQ_INSERT_HEAD(&m_torrents, n, entry);
-    m_ntorrents++;
     n->active = 1;
 }
 
@@ -88,10 +83,6 @@ net_stop(struct torrent *tp)
 {
     struct net *n = tp->net;
 
-    assert(m_ntorrents > 0);
-    m_ntorrents--;
-    BTPDQ_REMOVE(&m_torrents, n, entry);
-
     n->active = 0;
     n->rate_up = 0;
     n->rate_dwn = 0;
@@ -101,6 +92,7 @@ net_stop(struct torrent *tp)
     struct piece *pc;
     while ((pc = BTPDQ_FIRST(&n->getlst)) != NULL)
         piece_free(pc);
+    BTPDQ_INIT(&n->getlst);
 
     struct peer *p = BTPDQ_FIRST(&net_unattached);
     while (p != NULL) {
@@ -332,10 +324,10 @@ net_state(struct peer *p, const char *buf)
     case SHAKE_INFO:
         if (p->flags & PF_INCOMING) {
             struct torrent *tp = torrent_by_hash(buf);
-            if (tp == NULL || tp->net == NULL)
+            if (tp == NULL || !net_active(tp))
                 goto bad;
             p->n = tp->net;
-            peer_send(p, nb_create_shake(p->n->tp));
+            peer_send(p, nb_create_shake(tp));
         } else if (bcmp(buf, p->n->tp->tl->hash, 20) != 0)
             goto bad;
         peer_set_in_state(p, SHAKE_ID, 20);
@@ -545,9 +537,10 @@ compute_rate_sub(unsigned long rate)
 static void
 compute_rates(void) {
     unsigned long tot_up = 0, tot_dwn = 0;
-    struct net *n;
-    BTPDQ_FOREACH(n, &m_torrents, entry) {
+    struct torrent *tp;
+    BTPDQ_FOREACH(tp, torrent_get_all(), entry) {
         unsigned long tp_up = 0, tp_dwn = 0;
+        struct net *n = tp->net;
         struct peer *p;
         BTPDQ_FOREACH(p, &n->peers, p_entry) {
             if (p->count_up > 0 || peer_active_up(p)) {
@@ -611,15 +604,15 @@ net_bw_tick(void)
 static void
 run_peer_ticks(void)
 {
-    struct net *n;
+    struct torrent *tp;
     struct peer *p, *next;
 
     BTPDQ_FOREACH_MUTABLE(p, &net_unattached, p_entry, next)
         peer_on_tick(p);
 
-    BTPDQ_FOREACH(n, &m_torrents, entry)
-        BTPDQ_FOREACH_MUTABLE(p, &n->peers, p_entry, next)
-            peer_on_tick(p);
+    BTPDQ_FOREACH(tp, torrent_get_all(), entry)
+        BTPDQ_FOREACH_MUTABLE(p, &tp->net->peers, p_entry, next)
+        peer_on_tick(p);
 }
 
 void
diff --git a/btpd/net_types.h b/btpd/net_types.h
index ff5b99a..8ef61e5 100644
--- a/btpd/net_types.h
+++ b/btpd/net_types.h
@@ -1,7 +1,6 @@
 #ifndef BTPD_NET_TYPES_H
 #define BTPD_NET_TYPES_H
 
-BTPDQ_HEAD(net_tq, net);
 BTPDQ_HEAD(peer_tq, peer);
 BTPDQ_HEAD(piece_tq, piece);
 BTPDQ_HEAD(block_request_tq, block_request);
@@ -22,8 +21,6 @@ struct net {
 
     unsigned npeers;
     struct peer_tq peers;
-
-    BTPDQ_ENTRY(net) entry;
 };
 
 enum input_state {