diff options
-rw-r--r-- | src/cadet/gnunet-service-cadet_peer.c | 111 |
1 files changed, 85 insertions, 26 deletions
diff --git a/src/cadet/gnunet-service-cadet_peer.c b/src/cadet/gnunet-service-cadet_peer.c index e27ba9fde..52d9f6bf8 100644 --- a/src/cadet/gnunet-service-cadet_peer.c +++ b/src/cadet/gnunet-service-cadet_peer.c | |||
@@ -143,6 +143,11 @@ struct CadetPeer | |||
143 | */ | 143 | */ |
144 | struct GCD_search_handle *search_h; | 144 | struct GCD_search_handle *search_h; |
145 | 145 | ||
146 | /** | ||
147 | * Handle to stop the DHT search for paths to this peer | ||
148 | */ | ||
149 | struct GNUNET_SCHEDULER_Task *search_delayed; | ||
150 | |||
146 | /** | 151 | /** |
147 | * Tunnel to this peer, if any. | 152 | * Tunnel to this peer, if any. |
148 | */ | 153 | */ |
@@ -694,6 +699,43 @@ shutdown_tunnel (void *cls, | |||
694 | } | 699 | } |
695 | 700 | ||
696 | 701 | ||
702 | |||
703 | /** | ||
704 | * Check if peer is searching for a path (either active or delayed search). | ||
705 | * | ||
706 | * @param peer Peer to check | ||
707 | * | ||
708 | * @return GNUNET_YES if there is a search active. | ||
709 | * GNUNET_NO otherwise. | ||
710 | */ | ||
711 | static int | ||
712 | is_searching (const struct CadetPeer *peer) | ||
713 | { | ||
714 | return (NULL == peer->search_h && NULL == peer->search_delayed) ? | ||
715 | GNUNET_NO : GNUNET_YES; | ||
716 | } | ||
717 | |||
718 | |||
719 | /** | ||
720 | * @brief Start a search for a peer. | ||
721 | * | ||
722 | * @param cls Closure (Peer to search for). | ||
723 | * @param tc Task context. | ||
724 | */ | ||
725 | static void | ||
726 | delayed_search (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) | ||
727 | { | ||
728 | struct CadetPeer *peer = cls; | ||
729 | |||
730 | peer->search_delayed = NULL; | ||
731 | |||
732 | if (0 != (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason)) | ||
733 | return; | ||
734 | |||
735 | GCP_start_search (peer); | ||
736 | } | ||
737 | |||
738 | |||
697 | /** | 739 | /** |
698 | * Destroy the peer_info and free any allocated resources linked to it | 740 | * Destroy the peer_info and free any allocated resources linked to it |
699 | * | 741 | * |
@@ -719,10 +761,7 @@ peer_destroy (struct CadetPeer *peer) | |||
719 | GNUNET_break (0); | 761 | GNUNET_break (0); |
720 | LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n"); | 762 | LOG (GNUNET_ERROR_TYPE_WARNING, " not in peermap!!\n"); |
721 | } | 763 | } |
722 | if (NULL != peer->search_h) | 764 | GCP_stop_search (peer); |
723 | { | ||
724 | GCD_search_stop (peer->search_h); | ||
725 | } | ||
726 | p = peer->path_head; | 765 | p = peer->path_head; |
727 | while (NULL != p) | 766 | while (NULL != p) |
728 | { | 767 | { |
@@ -1802,24 +1841,19 @@ GCP_connect (struct CadetPeer *peer) | |||
1802 | } | 1841 | } |
1803 | } | 1842 | } |
1804 | 1843 | ||
1805 | if (NULL != peer->search_h && GNUNET_YES == rerun_search) | 1844 | if (GNUNET_YES == rerun_search) |
1806 | { | ||
1807 | LOG (GNUNET_ERROR_TYPE_DEBUG, " Stopping DHT GET for peer %s\n", | ||
1808 | GCP_2s (peer)); | ||
1809 | GCD_search_stop (peer->search_h); | ||
1810 | peer->search_h = NULL; | ||
1811 | } | ||
1812 | |||
1813 | if (NULL == peer->search_h) | ||
1814 | { | 1845 | { |
1815 | const struct GNUNET_PeerIdentity *id; | 1846 | struct GNUNET_TIME_Relative delay; |
1816 | 1847 | ||
1817 | id = GNUNET_PEER_resolve2 (peer->id); | 1848 | GCP_stop_search (peer); |
1818 | peer->search_h = GCD_search (id, &search_handler, peer); | 1849 | delay = GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100); |
1819 | if (CADET_TUNNEL_NEW == GCT_get_cstate (t) | 1850 | peer->search_delayed = GNUNET_SCHEDULER_add_delayed (delay, &delayed_search, |
1820 | || 0 == GCT_count_any_connections (t)) | 1851 | peer); |
1821 | GCT_change_cstate (t, CADET_TUNNEL_SEARCHING); | 1852 | return; |
1822 | } | 1853 | } |
1854 | |||
1855 | if (GNUNET_NO == is_searching (peer)) | ||
1856 | GCP_start_search (peer); | ||
1823 | } | 1857 | } |
1824 | 1858 | ||
1825 | 1859 | ||
@@ -2132,13 +2166,33 @@ GCP_remove_connection (struct CadetPeer *peer, | |||
2132 | void | 2166 | void |
2133 | GCP_start_search (struct CadetPeer *peer) | 2167 | GCP_start_search (struct CadetPeer *peer) |
2134 | { | 2168 | { |
2169 | const struct GNUNET_PeerIdentity *id; | ||
2170 | struct CadetTunnel *t = peer->tunnel; | ||
2171 | |||
2135 | if (NULL != peer->search_h) | 2172 | if (NULL != peer->search_h) |
2136 | { | 2173 | { |
2137 | GNUNET_break (0); | 2174 | GNUNET_break (0); |
2138 | return; | 2175 | return; |
2139 | } | 2176 | } |
2140 | 2177 | ||
2141 | peer->search_h = GCD_search (GCP_get_id (peer), &search_handler, peer); | 2178 | if (NULL != peer->search_delayed) |
2179 | GCP_stop_search (peer); | ||
2180 | |||
2181 | id = GNUNET_PEER_resolve2 (peer->id); | ||
2182 | peer->search_h = GCD_search (id, &search_handler, peer); | ||
2183 | |||
2184 | if (NULL == t) | ||
2185 | { | ||
2186 | /* Why would we search for a peer with no tunnel towards it? */ | ||
2187 | GNUNET_break (0); | ||
2188 | return; | ||
2189 | } | ||
2190 | |||
2191 | if (CADET_TUNNEL_NEW == GCT_get_cstate (t) | ||
2192 | || 0 == GCT_count_any_connections (t)) | ||
2193 | { | ||
2194 | GCT_change_cstate (t, CADET_TUNNEL_SEARCHING); | ||
2195 | } | ||
2142 | } | 2196 | } |
2143 | 2197 | ||
2144 | 2198 | ||
@@ -2151,13 +2205,16 @@ GCP_start_search (struct CadetPeer *peer) | |||
2151 | void | 2205 | void |
2152 | GCP_stop_search (struct CadetPeer *peer) | 2206 | GCP_stop_search (struct CadetPeer *peer) |
2153 | { | 2207 | { |
2154 | if (NULL == peer->search_h) | 2208 | if (NULL != peer->search_h) |
2155 | { | 2209 | { |
2156 | return; | 2210 | GCD_search_stop (peer->search_h); |
2211 | peer->search_h = NULL; | ||
2212 | } | ||
2213 | if (NULL != peer->search_delayed) | ||
2214 | { | ||
2215 | GNUNET_SCHEDULER_cancel (peer->search_delayed); | ||
2216 | peer->search_delayed = NULL; | ||
2157 | } | 2217 | } |
2158 | |||
2159 | GCD_search_stop (peer->search_h); | ||
2160 | peer->search_h = NULL; | ||
2161 | } | 2218 | } |
2162 | 2219 | ||
2163 | 2220 | ||
@@ -2192,6 +2249,8 @@ GCP_get_short_id (const struct CadetPeer *peer) | |||
2192 | /** | 2249 | /** |
2193 | * Set tunnel. | 2250 | * Set tunnel. |
2194 | * | 2251 | * |
2252 | * If tunnel is NULL and there was a search active, stop it, as it's useless. | ||
2253 | * | ||
2195 | * @param peer Peer. | 2254 | * @param peer Peer. |
2196 | * @param t Tunnel. | 2255 | * @param t Tunnel. |
2197 | */ | 2256 | */ |
@@ -2199,7 +2258,7 @@ void | |||
2199 | GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t) | 2258 | GCP_set_tunnel (struct CadetPeer *peer, struct CadetTunnel *t) |
2200 | { | 2259 | { |
2201 | peer->tunnel = t; | 2260 | peer->tunnel = t; |
2202 | if (NULL == t && NULL != peer->search_h) | 2261 | if (NULL == t && GNUNET_YES == is_searching (peer)) |
2203 | { | 2262 | { |
2204 | GCP_stop_search (peer); | 2263 | GCP_stop_search (peer); |
2205 | } | 2264 | } |