- more refactoring
[oweals/gnunet.git] / src / mesh / gnunet-service-mesh_peer.c
1 /*
2      This file is part of GNUnet.
3      (C) 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      option) any later version.
9
10      GNUnet is distributed in the hope that it will be useful, but
11      WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20
21
22 #include "platform.h"
23 #include "gnunet_util_lib.h"
24
25 #include "gnunet-service-mesh_peer.h"
26
27 /******************************************************************************/
28 /********************************   STRUCTS  **********************************/
29 /******************************************************************************/
30
31 /**
32  * Struct containing all information regarding a given peer
33  */
34 struct MeshPeer
35 {
36     /**
37      * ID of the peer
38      */
39   GNUNET_PEER_Id id;
40
41     /**
42      * Last time we heard from this peer
43      */
44   struct GNUNET_TIME_Absolute last_contact;
45
46     /**
47      * Paths to reach the peer, ordered by ascending hop count
48      */
49   struct MeshPeerPath *path_head;
50
51     /**
52      * Paths to reach the peer, ordered by ascending hop count
53      */
54   struct MeshPeerPath *path_tail;
55
56     /**
57      * Handle to stop the DHT search for paths to this peer
58      */
59   struct GNUNET_DHT_GetHandle *dhtget;
60
61     /**
62      * Tunnel to this peer, if any.
63      */
64   struct MeshTunnel2 *tunnel;
65
66     /**
67      * Connections that go through this peer, indexed by tid;
68      */
69   struct GNUNET_CONTAINER_MultiHashMap *connections;
70
71     /**
72      * Handle for queued transmissions
73      */
74   struct GNUNET_CORE_TransmitHandle *core_transmit;
75
76   /**
77    * Transmission queue to core DLL head
78    */
79   struct MeshPeerQueue *queue_head;
80   
81   /**
82    * Transmission queue to core DLL tail
83    */
84   struct MeshPeerQueue *queue_tail;
85
86   /**
87    * How many messages are in the queue to this peer.
88    */
89   unsigned int queue_n;
90 };
91
92
93 /******************************************************************************/
94 /*******************************   GLOBALS  ***********************************/
95 /******************************************************************************/
96
97 /**
98  * Peers known, indexed by PeerIdentity (MeshPeer).
99  */
100 static struct GNUNET_CONTAINER_MultiPeerMap *peers;
101
102 /**
103  * How many peers do we want to remember?
104  */
105 static unsigned long long max_peers;
106
107
108 /******************************************************************************/
109 /********************************   STATIC  ***********************************/
110 /******************************************************************************/
111
112 /**
113  * Iterator over tunnel hash map entries to destroy the tunnel during shutdown.
114  *
115  * @param cls closure
116  * @param key current key code
117  * @param value value in the hash map
118  * @return #GNUNET_YES if we should continue to iterate,
119  *         #GNUNET_NO if not.
120  */
121 static int
122 shutdown_tunnel (void *cls, 
123                  const struct GNUNET_PeerIdentity *key, 
124                  void *value)
125 {
126   struct MeshPeer *p = value;
127   struct MeshTunnel2 *t = p->tunnel;
128
129   if (NULL != t)
130     GMT_destroy (t);
131   return GNUNET_YES;
132 }
133
134
135 /******************************************************************************/
136 /********************************    API    ***********************************/
137 /******************************************************************************/
138
139 /**
140  * Initialize the peer subsystem.
141  *
142  * @param c Configuration.
143  */
144 void
145 GMP_init (const struct GNUNET_CONFIGURATION_Handle *c)
146 {
147   peers = GNUNET_CONTAINER_multipeermap_create (128, GNUNET_NO);
148   if (GNUNET_OK !=
149       GNUNET_CONFIGURATION_get_value_number (c, "MESH", "MAX_PEERS",
150                                              &max_peers))
151   {
152     GNUNET_log_config_invalid (GNUNET_ERROR_TYPE_WARNING,
153                                "MESH", "MAX_PEERS", "USING DEFAULT");
154     max_peers = 1000;
155   }
156 }
157
158 /**
159  * Shut down the peer subsystem.
160  */
161 void
162 GMP_shutdown (void)
163 {
164   GNUNET_CONTAINER_multipeermap_iterate (peers, &shutdown_tunnel, NULL);
165 }
166