-doxygen
[oweals/gnunet.git] / src / dht / gnunet-service-xdht_routing.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 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  * @file dht/gnunet-service-xdht_routing.c
23  * @brief GNUnet DHT tracking of requests for routing replies
24  * @author Supriti Singh
25  */
26 #include "platform.h"
27 #include "gnunet-service-xdht_neighbours.h"
28 #include "gnunet-service-xdht_routing.h"
29 #include "gnunet-service-xdht.h"
30
31
32 /**
33  * Number of requests we track at most (for routing replies).
34  */
35 #define DHT_MAX_RECENT (1024 * 16)
36
37
38 /**
39  * FIXME: Do we need a field prev_hop
40  * Routing table entry .
41  */
42 struct RoutingTrail
43 {
44   /**
45    * Source peer .
46    */
47   struct GNUNET_PeerIdentity source;
48
49   /**
50    * Destination peer.
51    */
52   struct GNUNET_PeerIdentity destination;
53
54   /**
55    * The peer to which this request should be passed to.
56    */
57   struct GNUNET_PeerIdentity next_hop;
58   
59 };
60
61
62 /**
63  * Routing table of the peer
64  */
65 static struct GNUNET_CONTAINER_MultiPeerMap *routing_table;
66
67
68 /**
69  * FIXME: Change the name of variable. 
70  * Ensure that everywhere in this file you are using destination as the key.
71  * Do we need prev field in routing table?
72  * Add a new entry to our routing table.
73  * @param source peer
74  * @param destintation
75  * @param next_hop
76  */
77 void
78 GDS_ROUTING_add (struct GNUNET_PeerIdentity *source,
79                  struct GNUNET_PeerIdentity *dest,
80                  struct GNUNET_PeerIdentity *next_hop)
81 {
82   struct RoutingTrail *new_routing_entry;
83     
84   /* If dest is already present in the routing table, then exit.*/
85   if (GNUNET_YES ==
86       GNUNET_CONTAINER_multipeermap_contains (routing_table, dest))
87   {
88     GNUNET_break (0);
89     return;
90   }
91   
92   new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail));
93   memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity));
94   memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity));
95   memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity));
96   
97   GNUNET_assert (GNUNET_OK ==
98     GNUNET_CONTAINER_multipeermap_put (routing_table,
99                                        dest, new_routing_entry,
100                                        GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
101 }
102
103
104 /**FIXME: Test if its correct or not.
105  * Find the next hop to send packet to .
106  * @return next hop peer id
107  */
108 struct GNUNET_PeerIdentity *
109 GDS_ROUTING_search(struct GNUNET_PeerIdentity *source_peer,
110                    struct GNUNET_PeerIdentity *destination_peer)
111 {
112   struct RoutingTrail *trail;
113   trail = (struct RoutingTrail *)(GNUNET_CONTAINER_multipeermap_get(routing_table,destination_peer));
114     
115   if(trail == NULL)
116       return NULL;
117     
118   return &(trail->next_hop);
119 }
120
121
122 /**FIXME: Old implementation just to remove error
123  * Handle a reply (route to origin).  Only forwards the reply back to
124  * other peers waiting for it.  Does not do local caching or
125  * forwarding to local clients.  Essentially calls
126  * GDS_NEIGHBOURS_handle_reply for all peers that sent us a matching
127  * request recently.
128  *
129  * @param type type of the block
130  * @param expiration_time when does the content expire
131  * @param key key for the content
132  * @param put_path_length number of entries in put_path
133  * @param put_path peers the original PUT traversed (if tracked)
134  * @param get_path_length number of entries in get_path
135  * @param get_path peers this reply has traversed so far (if tracked)
136  * @param data payload of the reply
137  * @param data_size number of bytes in data
138  */
139 void
140 GDS_ROUTING_process (enum GNUNET_BLOCK_Type type,
141                      struct GNUNET_TIME_Absolute expiration_time,
142                      const struct GNUNET_HashCode * key, unsigned int put_path_length,
143                      const struct GNUNET_PeerIdentity *put_path,
144                      unsigned int get_path_length,
145                      const struct GNUNET_PeerIdentity *get_path,
146                      const void *data, size_t data_size)
147 {
148   return;
149 }
150
151
152 /**
153  * Initialize routing subsystem.
154  */
155 void
156 GDS_ROUTING_init ()
157
158   routing_table = GNUNET_CONTAINER_multipeermap_create (DHT_MAX_RECENT * 4 / 3, GNUNET_NO);
159 }
160
161
162 /**
163  * Shutdown routing subsystem.
164  */
165 void
166 GDS_ROUTING_done ()
167 {
168   GNUNET_assert (0 == GNUNET_CONTAINER_multipeermap_size (routing_table));
169   GNUNET_CONTAINER_multipeermap_destroy (routing_table);
170 }
171
172 /* end of gnunet-service-xdht_routing.c */