diff options
Diffstat (limited to 'src/dht/gnunet-service-xdht_routing.c')
-rw-r--r-- | src/dht/gnunet-service-xdht_routing.c | 99 |
1 files changed, 71 insertions, 28 deletions
diff --git a/src/dht/gnunet-service-xdht_routing.c b/src/dht/gnunet-service-xdht_routing.c index 55ef1f4b2..f7e56c115 100644 --- a/src/dht/gnunet-service-xdht_routing.c +++ b/src/dht/gnunet-service-xdht_routing.c | |||
@@ -34,9 +34,12 @@ | |||
34 | */ | 34 | */ |
35 | #define DHT_MAX_RECENT (1024 * 16) | 35 | #define DHT_MAX_RECENT (1024 * 16) |
36 | 36 | ||
37 | /** | ||
38 | * Maximum number of entries in routing table. | ||
39 | */ | ||
40 | #define ROUTING_TABLE_THRESHOLD 64 | ||
37 | 41 | ||
38 | /** | 42 | /** |
39 | * FIXME: Do we need a field prev_hop | ||
40 | * Routing table entry . | 43 | * Routing table entry . |
41 | */ | 44 | */ |
42 | struct RoutingTrail | 45 | struct RoutingTrail |
@@ -56,6 +59,11 @@ struct RoutingTrail | |||
56 | */ | 59 | */ |
57 | struct GNUNET_PeerIdentity next_hop; | 60 | struct GNUNET_PeerIdentity next_hop; |
58 | 61 | ||
62 | /** | ||
63 | * Peer just before next hop in the trail. | ||
64 | */ | ||
65 | struct GNUNET_PeerIdentity prev_hop; | ||
66 | |||
59 | }; | 67 | }; |
60 | 68 | ||
61 | 69 | ||
@@ -66,56 +74,91 @@ static struct GNUNET_CONTAINER_MultiPeerMap *routing_table; | |||
66 | 74 | ||
67 | 75 | ||
68 | /** | 76 | /** |
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. | 77 | * Add a new entry to our routing table. |
73 | * @param source peer | 78 | * @param source peer Source of the trail. |
74 | * @param destintation | 79 | * @param destintation Destination of the trail. |
75 | * @param next_hop | 80 | * @param next_hop Next peer to forward the message to reach the destination. |
81 | * @return GNUNET_YES | ||
82 | * GNUNET_SYSERR If the number of routing entries crossed thershold. | ||
76 | */ | 83 | */ |
77 | void | 84 | int |
78 | GDS_ROUTING_add (struct GNUNET_PeerIdentity *source, | 85 | GDS_ROUTING_add (struct GNUNET_PeerIdentity *source, |
79 | struct GNUNET_PeerIdentity *dest, | 86 | struct GNUNET_PeerIdentity *dest, |
80 | struct GNUNET_PeerIdentity *next_hop) | 87 | struct GNUNET_PeerIdentity *next_hop, |
88 | const struct GNUNET_PeerIdentity *prev_hop) | ||
81 | { | 89 | { |
82 | struct RoutingTrail *new_routing_entry; | 90 | struct RoutingTrail *new_routing_entry; |
83 | 91 | ||
84 | /* If dest is already present in the routing table, then exit.*/ | 92 | if (GNUNET_CONTAINER_multipeermap_size(routing_table) > ROUTING_TABLE_THRESHOLD) |
85 | if (GNUNET_YES == | 93 | return GNUNET_SYSERR; |
86 | GNUNET_CONTAINER_multipeermap_contains (routing_table, dest)) | 94 | //FPRINTF (stderr,_("\nSUPU ROUTING ADD %s, %s, %d"),__FILE__, __func__,__LINE__); |
87 | { | ||
88 | GNUNET_break (0); | ||
89 | return; | ||
90 | } | ||
91 | |||
92 | new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail)); | 95 | new_routing_entry = GNUNET_malloc (sizeof (struct RoutingTrail)); |
93 | memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity)); | 96 | memcpy (&(new_routing_entry->source) , source, sizeof (struct GNUNET_PeerIdentity)); |
94 | memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity)); | 97 | memcpy (&(new_routing_entry->next_hop), next_hop, sizeof (struct GNUNET_PeerIdentity)); |
95 | memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity)); | 98 | memcpy (&(new_routing_entry->destination), dest, sizeof (struct GNUNET_PeerIdentity)); |
99 | memcpy (&(new_routing_entry->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity)); | ||
96 | 100 | ||
97 | GNUNET_assert (GNUNET_OK == | 101 | GNUNET_assert (GNUNET_OK == |
98 | GNUNET_CONTAINER_multipeermap_put (routing_table, | 102 | GNUNET_CONTAINER_multipeermap_put (routing_table, |
99 | dest, new_routing_entry, | 103 | dest, new_routing_entry, |
100 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); | 104 | GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE)); |
105 | return GNUNET_YES; | ||
101 | } | 106 | } |
102 | 107 | ||
103 | 108 | ||
104 | /**FIXME: Test if its correct or not. | 109 | /** |
105 | * Find the next hop to send packet to . | 110 | * Iterate over multiple entries for same destinational value and get |
106 | * @return next hop peer id | 111 | * the correct next hop. |
112 | * @param cls struct RoutingTrail | ||
113 | * @param key Destination identity | ||
114 | * @param value struct RoutingTrail | ||
115 | * @return #GNUNET_YES to continue looking, #GNUNET_NO if we found the next hop | ||
116 | */ | ||
117 | int | ||
118 | get_next_hop (void *cls, const struct GNUNET_PeerIdentity *key, void *value) | ||
119 | { | ||
120 | /* Here you should match if source, prev hop matches if yes then send | ||
121 | GNUNET_NO as you don't need to check more entries. */ | ||
122 | struct RoutingTrail *request = cls; | ||
123 | struct RoutingTrail *existing_entry = (struct RoutingTrail *)value; | ||
124 | |||
125 | if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->source), &(existing_entry->source))) | ||
126 | { | ||
127 | if (0 == GNUNET_CRYPTO_cmp_peer_identity (&(request->prev_hop), &(existing_entry->prev_hop))) | ||
128 | { | ||
129 | memcpy (&(request->next_hop), &(existing_entry->next_hop), sizeof (struct GNUNET_PeerIdentity)); | ||
130 | return GNUNET_YES; | ||
131 | } | ||
132 | } | ||
133 | return GNUNET_NO; | ||
134 | } | ||
135 | |||
136 | |||
137 | /** | ||
138 | * Find the next hop to send packet to. | ||
139 | * @param source_peer Source of the trail. | ||
140 | * @param destination_peer Destination of the trail. | ||
141 | * @param prev_hop Previous hop in the trail. | ||
142 | * @return Next hop in the trail from source to destination. | ||
107 | */ | 143 | */ |
108 | struct GNUNET_PeerIdentity * | 144 | struct GNUNET_PeerIdentity * |
109 | GDS_ROUTING_search(struct GNUNET_PeerIdentity *source_peer, | 145 | GDS_ROUTING_search(struct GNUNET_PeerIdentity *source_peer, |
110 | struct GNUNET_PeerIdentity *destination_peer) | 146 | struct GNUNET_PeerIdentity *destination_peer, |
147 | const struct GNUNET_PeerIdentity *prev_hop) | ||
111 | { | 148 | { |
112 | struct RoutingTrail *trail; | 149 | struct RoutingTrail *trail; |
113 | trail = (struct RoutingTrail *)(GNUNET_CONTAINER_multipeermap_get(routing_table,destination_peer)); | 150 | trail = GNUNET_malloc (sizeof (struct RoutingTrail)); |
114 | 151 | memcpy (&(trail->destination), destination_peer, sizeof (struct GNUNET_PeerIdentity)); | |
115 | if(trail == NULL) | 152 | memcpy (&(trail->source), source_peer, sizeof (struct GNUNET_PeerIdentity)); |
116 | return NULL; | 153 | memcpy (&(trail->prev_hop), prev_hop, sizeof (struct GNUNET_PeerIdentity)); |
117 | 154 | //trail->next_hop = NULL; | |
118 | return &(trail->next_hop); | 155 | //FPRINTF (stderr,_("\nSUPU ROUTING SEARCH %s, %s, %d"),__FILE__, __func__,__LINE__); |
156 | GNUNET_CONTAINER_multipeermap_get_multiple (routing_table, destination_peer, | ||
157 | get_next_hop, trail); | ||
158 | if(trail != NULL) | ||
159 | return &(trail->next_hop); | ||
160 | else | ||
161 | return NULL; | ||
119 | } | 162 | } |
120 | 163 | ||
121 | 164 | ||