aboutsummaryrefslogtreecommitdiff
path: root/src/topology
diff options
context:
space:
mode:
authorng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
committerng0 <ng0@n0.is>2019-09-08 12:33:09 +0000
commitd41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb (patch)
tree9efd18ea7d425652085ed0bd5e8e45604bc5f6b9 /src/topology
parenta0fce305c565c0937d917a92712f15e9c5736260 (diff)
downloadgnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.tar.gz
gnunet-d41ed82a4ea0cc8e1674b6d5d2c49fd6462610bb.zip
uncrustify as demanded.
Diffstat (limited to 'src/topology')
-rw-r--r--src/topology/friends.c263
-rw-r--r--src/topology/gnunet-daemon-topology.c854
-rw-r--r--src/topology/test_gnunet_daemon_topology.c224
3 files changed, 670 insertions, 671 deletions
diff --git a/src/topology/friends.c b/src/topology/friends.c
index f23724444..81c7d45e9 100644
--- a/src/topology/friends.c
+++ b/src/topology/friends.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file topology/friends.c 22 * @file topology/friends.c
@@ -36,9 +36,9 @@
36 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors 36 * @return #GNUNET_OK on success, #GNUNET_SYSERR on parsing errors
37 */ 37 */
38int 38int
39GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg, 39GNUNET_FRIENDS_parse(const struct GNUNET_CONFIGURATION_Handle *cfg,
40 GNUNET_FRIENDS_Callback cb, 40 GNUNET_FRIENDS_Callback cb,
41 void *cb_cls) 41 void *cb_cls)
42{ 42{
43 char *fn; 43 char *fn;
44 char *data; 44 char *data;
@@ -49,94 +49,94 @@ GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
49 ssize_t ssize; 49 ssize_t ssize;
50 50
51 if (GNUNET_OK != 51 if (GNUNET_OK !=
52 GNUNET_CONFIGURATION_get_value_filename (cfg, 52 GNUNET_CONFIGURATION_get_value_filename(cfg,
53 "TOPOLOGY", 53 "TOPOLOGY",
54 "FRIENDS", 54 "FRIENDS",
55 &fn)) 55 &fn))
56 { 56 {
57 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 57 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
58 "topology", 58 "topology",
59 "FRIENDS"); 59 "FRIENDS");
60 return GNUNET_SYSERR; 60 return GNUNET_SYSERR;
61 } 61 }
62 if (GNUNET_SYSERR == 62 if (GNUNET_SYSERR ==
63 GNUNET_DISK_directory_create_for_file (fn)) 63 GNUNET_DISK_directory_create_for_file(fn))
64 { 64 {
65 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 65 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
66 "mkdir", 66 "mkdir",
67 fn); 67 fn);
68 GNUNET_free (fn); 68 GNUNET_free(fn);
69 return GNUNET_SYSERR; 69 return GNUNET_SYSERR;
70 } 70 }
71 if ( (GNUNET_OK != 71 if ((GNUNET_OK !=
72 GNUNET_DISK_file_test (fn)) && 72 GNUNET_DISK_file_test(fn)) &&
73 (GNUNET_OK != 73 (GNUNET_OK !=
74 GNUNET_DISK_fn_write (fn, 74 GNUNET_DISK_fn_write(fn,
75 NULL, 75 NULL,
76 0, 76 0,
77 GNUNET_DISK_PERM_USER_READ | 77 GNUNET_DISK_PERM_USER_READ |
78 GNUNET_DISK_PERM_USER_WRITE | 78 GNUNET_DISK_PERM_USER_WRITE |
79 GNUNET_DISK_OPEN_CREATE)) ) 79 GNUNET_DISK_OPEN_CREATE)))
80 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, 80 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_WARNING,
81 "write", 81 "write",
82 fn); 82 fn);
83 if ( (GNUNET_OK != 83 if ((GNUNET_OK !=
84 GNUNET_DISK_file_size (fn, 84 GNUNET_DISK_file_size(fn,
85 &fsize, 85 &fsize,
86 GNUNET_NO, 86 GNUNET_NO,
87 GNUNET_YES)) || 87 GNUNET_YES)) ||
88 (0 == fsize) ) 88 (0 == fsize))
89 { 89 {
90 GNUNET_free (fn); 90 GNUNET_free(fn);
91 return GNUNET_OK; 91 return GNUNET_OK;
92 } 92 }
93 data = GNUNET_malloc_large (fsize); 93 data = GNUNET_malloc_large(fsize);
94 if (NULL == data) 94 if (NULL == data)
95 { 95 {
96 GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); 96 GNUNET_log_strerror(GNUNET_ERROR_TYPE_ERROR, "malloc");
97 GNUNET_free (fn); 97 GNUNET_free(fn);
98 return GNUNET_SYSERR; 98 return GNUNET_SYSERR;
99 } 99 }
100 ssize = GNUNET_DISK_fn_read (fn, 100 ssize = GNUNET_DISK_fn_read(fn,
101 data, 101 data,
102 fsize); 102 fsize);
103 if ( (ssize < 0) || 103 if ((ssize < 0) ||
104 (fsize != (uint64_t) ssize) ) 104 (fsize != (uint64_t)ssize))
105 { 105 {
106 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_ERROR, 106 GNUNET_log_strerror_file(GNUNET_ERROR_TYPE_ERROR,
107 "read", 107 "read",
108 "fn"); 108 "fn");
109 GNUNET_free (fn); 109 GNUNET_free(fn);
110 GNUNET_free (data); 110 GNUNET_free(data);
111 return GNUNET_SYSERR; 111 return GNUNET_SYSERR;
112 } 112 }
113 start = 0; 113 start = 0;
114 pos = 0; 114 pos = 0;
115 while (pos < fsize) 115 while (pos < fsize)
116 {
117 while ( (pos < fsize) &&
118 (! isspace ((unsigned char) data[pos])) )
119 pos++;
120 if (GNUNET_OK !=
121 GNUNET_CRYPTO_eddsa_public_key_from_string (&data[start],
122 pos - start,
123 &pid.public_key))
124 { 116 {
125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 117 while ((pos < fsize) &&
126 _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"), 118 (!isspace((unsigned char)data[pos])))
127 (unsigned long long) pos, 119 pos++;
128 (int) (pos - start), 120 if (GNUNET_OK !=
129 &data[start]); 121 GNUNET_CRYPTO_eddsa_public_key_from_string(&data[start],
122 pos - start,
123 &pid.public_key))
124 {
125 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
126 _("Syntax error in FRIENDS file at offset %llu, skipping bytes `%.*s'.\n"),
127 (unsigned long long)pos,
128 (int)(pos - start),
129 &data[start]);
130 pos++;
131 start = pos;
132 continue;
133 }
130 pos++; 134 pos++;
131 start = pos; 135 start = pos;
132 continue; 136 cb(cb_cls, &pid);
133 } 137 }
134 pos++; 138 GNUNET_free(data);
135 start = pos; 139 GNUNET_free(fn);
136 cb (cb_cls, &pid);
137 }
138 GNUNET_free (data);
139 GNUNET_free (fn);
140 return GNUNET_OK; 140 return GNUNET_OK;
141} 141}
142 142
@@ -144,8 +144,7 @@ GNUNET_FRIENDS_parse (const struct GNUNET_CONFIGURATION_Handle *cfg,
144/** 144/**
145 * Handle for writing a friends file. 145 * Handle for writing a friends file.
146 */ 146 */
147struct GNUNET_FRIENDS_Writer 147struct GNUNET_FRIENDS_Writer {
148{
149 /** 148 /**
150 * Handle to the file. 149 * Handle to the file.
151 */ 150 */
@@ -161,41 +160,41 @@ struct GNUNET_FRIENDS_Writer
161 * @return NULL on error 160 * @return NULL on error
162 */ 161 */
163struct GNUNET_FRIENDS_Writer * 162struct GNUNET_FRIENDS_Writer *
164GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg) 163GNUNET_FRIENDS_write_start(const struct GNUNET_CONFIGURATION_Handle *cfg)
165{ 164{
166 struct GNUNET_FRIENDS_Writer *w; 165 struct GNUNET_FRIENDS_Writer *w;
167 char *fn; 166 char *fn;
168 167
169 if (GNUNET_OK != 168 if (GNUNET_OK !=
170 GNUNET_CONFIGURATION_get_value_filename (cfg, "TOPOLOGY", "FRIENDS", &fn)) 169 GNUNET_CONFIGURATION_get_value_filename(cfg, "TOPOLOGY", "FRIENDS", &fn))
171 { 170 {
172 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 171 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR,
173 "topology", "FRIENDS"); 172 "topology", "FRIENDS");
174 return NULL; 173 return NULL;
175 } 174 }
176 if (GNUNET_OK != 175 if (GNUNET_OK !=
177 GNUNET_DISK_directory_create_for_file (fn)) 176 GNUNET_DISK_directory_create_for_file(fn))
178 { 177 {
179 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 178 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
180 _("Directory for file `%s' does not seem to be writable.\n"), 179 _("Directory for file `%s' does not seem to be writable.\n"),
181 fn); 180 fn);
182 GNUNET_free (fn); 181 GNUNET_free(fn);
183 return NULL; 182 return NULL;
184 } 183 }
185 if (GNUNET_OK == GNUNET_DISK_file_test (fn)) 184 if (GNUNET_OK == GNUNET_DISK_file_test(fn))
186 GNUNET_DISK_file_backup (fn); 185 GNUNET_DISK_file_backup(fn);
187 w = GNUNET_new (struct GNUNET_FRIENDS_Writer); 186 w = GNUNET_new(struct GNUNET_FRIENDS_Writer);
188 w->fh = GNUNET_DISK_file_open (fn, 187 w->fh = GNUNET_DISK_file_open(fn,
189 GNUNET_DISK_OPEN_CREATE | 188 GNUNET_DISK_OPEN_CREATE |
190 GNUNET_DISK_OPEN_WRITE | 189 GNUNET_DISK_OPEN_WRITE |
191 GNUNET_DISK_OPEN_FAILIFEXISTS, 190 GNUNET_DISK_OPEN_FAILIFEXISTS,
192 GNUNET_DISK_PERM_USER_READ); 191 GNUNET_DISK_PERM_USER_READ);
193 GNUNET_free (fn); 192 GNUNET_free(fn);
194 if (NULL == w->fh) 193 if (NULL == w->fh)
195 { 194 {
196 GNUNET_free (w); 195 GNUNET_free(w);
197 return NULL; 196 return NULL;
198 } 197 }
199 return w; 198 return w;
200} 199}
201 200
@@ -207,12 +206,12 @@ GNUNET_FRIENDS_write_start (const struct GNUNET_CONFIGURATION_Handle *cfg)
207 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 206 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
208 */ 207 */
209int 208int
210GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w) 209GNUNET_FRIENDS_write_stop(struct GNUNET_FRIENDS_Writer *w)
211{ 210{
212 int ret; 211 int ret;
213 212
214 ret = GNUNET_DISK_file_close (w->fh); 213 ret = GNUNET_DISK_file_close(w->fh);
215 GNUNET_free (w); 214 GNUNET_free(w);
216 return ret; 215 return ret;
217} 216}
218 217
@@ -225,28 +224,28 @@ GNUNET_FRIENDS_write_stop (struct GNUNET_FRIENDS_Writer *w)
225 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error 224 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error
226 */ 225 */
227int 226int
228GNUNET_FRIENDS_write (struct GNUNET_FRIENDS_Writer *w, 227GNUNET_FRIENDS_write(struct GNUNET_FRIENDS_Writer *w,
229 const struct GNUNET_PeerIdentity *friend_id) 228 const struct GNUNET_PeerIdentity *friend_id)
230{ 229{
231 char *buf; 230 char *buf;
232 char *ret; 231 char *ret;
233 size_t slen; 232 size_t slen;
234 233
235 ret = GNUNET_CRYPTO_eddsa_public_key_to_string (&friend_id->public_key); 234 ret = GNUNET_CRYPTO_eddsa_public_key_to_string(&friend_id->public_key);
236 GNUNET_asprintf (&buf, 235 GNUNET_asprintf(&buf,
237 "%s\n", 236 "%s\n",
238 ret); 237 ret);
239 GNUNET_free (ret); 238 GNUNET_free(ret);
240 slen = strlen (buf); 239 slen = strlen(buf);
241 if (slen != 240 if (slen !=
242 GNUNET_DISK_file_write (w->fh, 241 GNUNET_DISK_file_write(w->fh,
243 buf, 242 buf,
244 slen)) 243 slen))
245 { 244 {
246 GNUNET_free (buf); 245 GNUNET_free(buf);
247 return GNUNET_SYSERR; 246 return GNUNET_SYSERR;
248 } 247 }
249 GNUNET_free (buf); 248 GNUNET_free(buf);
250 return GNUNET_OK; 249 return GNUNET_OK;
251} 250}
252 251
diff --git a/src/topology/gnunet-daemon-topology.c b/src/topology/gnunet-daemon-topology.c
index 5a22ee0d2..a550286e7 100644
--- a/src/topology/gnunet-daemon-topology.c
+++ b/src/topology/gnunet-daemon-topology.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20 20
21/** 21/**
22 * @file topology/gnunet-daemon-topology.c 22 * @file topology/gnunet-daemon-topology.c
@@ -52,20 +52,19 @@
52 * At what frequency do we sent HELLOs to a peer? 52 * At what frequency do we sent HELLOs to a peer?
53 */ 53 */
54#define HELLO_ADVERTISEMENT_MIN_FREQUENCY \ 54#define HELLO_ADVERTISEMENT_MIN_FREQUENCY \
55 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 5) 55 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MINUTES, 5)
56 56
57/** 57/**
58 * After what time period do we expire the HELLO Bloom filter? 58 * After what time period do we expire the HELLO Bloom filter?
59 */ 59 */
60#define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY \ 60#define HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY \
61 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_HOURS, 4) 61 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_HOURS, 4)
62 62
63 63
64/** 64/**
65 * Record for neighbours, friends and blacklisted peers. 65 * Record for neighbours, friends and blacklisted peers.
66 */ 66 */
67struct Peer 67struct Peer {
68{
69 /** 68 /**
70 * Which peer is this entry about? 69 * Which peer is this entry about?
71 */ 70 */
@@ -217,17 +216,17 @@ static unsigned int friend_count;
217 * @return #GNUNET_OK if the connection is allowed 216 * @return #GNUNET_OK if the connection is allowed
218 */ 217 */
219static int 218static int
220blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid) 219blacklist_check(void *cls, const struct GNUNET_PeerIdentity *pid)
221{ 220{
222 struct Peer *pos; 221 struct Peer *pos;
223 222
224 pos = GNUNET_CONTAINER_multipeermap_get (peers, pid); 223 pos = GNUNET_CONTAINER_multipeermap_get(peers, pid);
225 if ((NULL != pos) && (GNUNET_YES == pos->is_friend)) 224 if ((NULL != pos) && (GNUNET_YES == pos->is_friend))
226 return GNUNET_OK; 225 return GNUNET_OK;
227 GNUNET_STATISTICS_update (stats, 226 GNUNET_STATISTICS_update(stats,
228 gettext_noop ("# peers blacklisted"), 227 gettext_noop("# peers blacklisted"),
229 1, 228 1,
230 GNUNET_NO); 229 GNUNET_NO);
231 return GNUNET_SYSERR; 230 return GNUNET_SYSERR;
232} 231}
233 232
@@ -237,13 +236,13 @@ blacklist_check (void *cls, const struct GNUNET_PeerIdentity *pid)
237 * the minimum number of friends. 236 * the minimum number of friends.
238 */ 237 */
239static void 238static void
240whitelist_peers () 239whitelist_peers()
241{ 240{
242 if (NULL != blacklist) 241 if (NULL != blacklist)
243 { 242 {
244 GNUNET_TRANSPORT_blacklist_cancel (blacklist); 243 GNUNET_TRANSPORT_blacklist_cancel(blacklist);
245 blacklist = NULL; 244 blacklist = NULL;
246 } 245 }
247} 246}
248 247
249 248
@@ -256,34 +255,34 @@ whitelist_peers ()
256 * @return #GNUNET_YES (always: continue to iterate) 255 * @return #GNUNET_YES (always: continue to iterate)
257 */ 256 */
258static int 257static int
259free_peer (void *cls, const struct GNUNET_PeerIdentity *pid, void *value) 258free_peer(void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
260{ 259{
261 struct Peer *pos = value; 260 struct Peer *pos = value;
262 261
263 GNUNET_break (NULL == pos->mq); 262 GNUNET_break(NULL == pos->mq);
264 GNUNET_break (GNUNET_OK == 263 GNUNET_break(GNUNET_OK ==
265 GNUNET_CONTAINER_multipeermap_remove (peers, pid, pos)); 264 GNUNET_CONTAINER_multipeermap_remove(peers, pid, pos));
266 if (NULL != pos->hello_delay_task) 265 if (NULL != pos->hello_delay_task)
267 { 266 {
268 GNUNET_SCHEDULER_cancel (pos->hello_delay_task); 267 GNUNET_SCHEDULER_cancel(pos->hello_delay_task);
269 pos->hello_delay_task = NULL; 268 pos->hello_delay_task = NULL;
270 } 269 }
271 if (NULL != pos->sh) 270 if (NULL != pos->sh)
272 { 271 {
273 GNUNET_ATS_connectivity_suggest_cancel (pos->sh); 272 GNUNET_ATS_connectivity_suggest_cancel(pos->sh);
274 pos->sh = NULL; 273 pos->sh = NULL;
275 } 274 }
276 if (NULL != pos->hello) 275 if (NULL != pos->hello)
277 { 276 {
278 GNUNET_free_non_null (pos->hello); 277 GNUNET_free_non_null(pos->hello);
279 pos->hello = NULL; 278 pos->hello = NULL;
280 } 279 }
281 if (NULL != pos->filter) 280 if (NULL != pos->filter)
282 { 281 {
283 GNUNET_CONTAINER_bloomfilter_free (pos->filter); 282 GNUNET_CONTAINER_bloomfilter_free(pos->filter);
284 pos->filter = NULL; 283 pos->filter = NULL;
285 } 284 }
286 GNUNET_free (pos); 285 GNUNET_free(pos);
287 return GNUNET_YES; 286 return GNUNET_YES;
288} 287}
289 288
@@ -295,23 +294,23 @@ free_peer (void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
295 * @param pos peer to consider connecting to 294 * @param pos peer to consider connecting to
296 */ 295 */
297static void 296static void
298attempt_connect (struct Peer *pos) 297attempt_connect(struct Peer *pos)
299{ 298{
300 uint32_t strength; 299 uint32_t strength;
301 300
302 if (0 == GNUNET_memcmp (&my_identity, &pos->pid)) 301 if (0 == GNUNET_memcmp(&my_identity, &pos->pid))
303 return; /* This is myself, nothing to do. */ 302 return; /* This is myself, nothing to do. */
304 if (connection_count < target_connection_count) 303 if (connection_count < target_connection_count)
305 strength = 1; 304 strength = 1;
306 else 305 else
307 strength = 0; 306 strength = 0;
308 if ((friend_count < minimum_friend_count) || (GNUNET_YES == friends_only)) 307 if ((friend_count < minimum_friend_count) || (GNUNET_YES == friends_only))
309 { 308 {
310 if (pos->is_friend) 309 if (pos->is_friend)
311 strength += 10; /* urgently needed */ 310 strength += 10; /* urgently needed */
312 else 311 else
313 strength = 0; /* disallowed */ 312 strength = 0; /* disallowed */
314 } 313 }
315 if (pos->is_friend) 314 if (pos->is_friend)
316 strength *= 2; /* friends always count more */ 315 strength *= 2; /* friends always count more */
317 if (NULL != pos->mq) 316 if (NULL != pos->mq)
@@ -319,23 +318,23 @@ attempt_connect (struct Peer *pos)
319 if (strength == pos->strength) 318 if (strength == pos->strength)
320 return; /* nothing to do */ 319 return; /* nothing to do */
321 if (NULL != pos->sh) 320 if (NULL != pos->sh)
322 { 321 {
323 GNUNET_ATS_connectivity_suggest_cancel (pos->sh); 322 GNUNET_ATS_connectivity_suggest_cancel(pos->sh);
324 pos->sh = NULL; 323 pos->sh = NULL;
325 } 324 }
326 pos->strength = strength; 325 pos->strength = strength;
327 if (0 != strength) 326 if (0 != strength)
328 { 327 {
329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 328 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
330 "Asking to connect to `%s' with strength %u\n", 329 "Asking to connect to `%s' with strength %u\n",
331 GNUNET_i2s (&pos->pid), 330 GNUNET_i2s(&pos->pid),
332 (unsigned int) strength); 331 (unsigned int)strength);
333 GNUNET_STATISTICS_update (stats, 332 GNUNET_STATISTICS_update(stats,
334 gettext_noop ("# connect requests issued to ATS"), 333 gettext_noop("# connect requests issued to ATS"),
335 1, 334 1,
336 GNUNET_NO); 335 GNUNET_NO);
337 pos->sh = GNUNET_ATS_connectivity_suggest (ats, &pos->pid, strength); 336 pos->sh = GNUNET_ATS_connectivity_suggest(ats, &pos->pid, strength);
338 } 337 }
339} 338}
340 339
341 340
@@ -348,26 +347,26 @@ attempt_connect (struct Peer *pos)
348 * @return the new entry 347 * @return the new entry
349 */ 348 */
350static struct Peer * 349static struct Peer *
351make_peer (const struct GNUNET_PeerIdentity *peer, 350make_peer(const struct GNUNET_PeerIdentity *peer,
352 const struct GNUNET_HELLO_Message *hello, 351 const struct GNUNET_HELLO_Message *hello,
353 int is_friend) 352 int is_friend)
354{ 353{
355 struct Peer *ret; 354 struct Peer *ret;
356 355
357 ret = GNUNET_new (struct Peer); 356 ret = GNUNET_new(struct Peer);
358 ret->pid = *peer; 357 ret->pid = *peer;
359 ret->is_friend = is_friend; 358 ret->is_friend = is_friend;
360 if (NULL != hello) 359 if (NULL != hello)
361 { 360 {
362 ret->hello = GNUNET_malloc (GNUNET_HELLO_size (hello)); 361 ret->hello = GNUNET_malloc(GNUNET_HELLO_size(hello));
363 GNUNET_memcpy (ret->hello, hello, GNUNET_HELLO_size (hello)); 362 GNUNET_memcpy(ret->hello, hello, GNUNET_HELLO_size(hello));
364 } 363 }
365 GNUNET_break (GNUNET_OK == 364 GNUNET_break(GNUNET_OK ==
366 GNUNET_CONTAINER_multipeermap_put ( 365 GNUNET_CONTAINER_multipeermap_put(
367 peers, 366 peers,
368 peer, 367 peer,
369 ret, 368 ret,
370 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); 369 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
371 return ret; 370 return ret;
372} 371}
373 372
@@ -378,7 +377,7 @@ make_peer (const struct GNUNET_PeerIdentity *peer,
378 * @param peer entry to initialize 377 * @param peer entry to initialize
379 */ 378 */
380static void 379static void
381setup_filter (struct Peer *peer) 380setup_filter(struct Peer *peer)
382{ 381{
383 struct GNUNET_HashCode hc; 382 struct GNUNET_HashCode hc;
384 383
@@ -389,21 +388,19 @@ setup_filter (struct Peer *peer)
389 * "useless" once a HELLO has been passed on to ~100 388 * "useless" once a HELLO has been passed on to ~100
390 * other peers, which is likely more than enough in 389 * other peers, which is likely more than enough in
391 * any case; hence 64, 5 as bloomfilter parameters. */ 390 * any case; hence 64, 5 as bloomfilter parameters. */
392 peer->filter = GNUNET_CONTAINER_bloomfilter_init (NULL, 64, 5); 391 peer->filter = GNUNET_CONTAINER_bloomfilter_init(NULL, 64, 5);
393 peer->filter_expiration = 392 peer->filter_expiration =
394 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY); 393 GNUNET_TIME_relative_to_absolute(HELLO_ADVERTISEMENT_MIN_REPEAT_FREQUENCY);
395 /* never send a peer its own HELLO */ 394 /* never send a peer its own HELLO */
396 GNUNET_CRYPTO_hash (&peer->pid, sizeof (struct GNUNET_PeerIdentity), &hc); 395 GNUNET_CRYPTO_hash(&peer->pid, sizeof(struct GNUNET_PeerIdentity), &hc);
397 GNUNET_CONTAINER_bloomfilter_add (peer->filter, &hc); 396 GNUNET_CONTAINER_bloomfilter_add(peer->filter, &hc);
398} 397}
399 398
400 399
401/** 400/**
402 * Closure for #find_advertisable_hello(). 401 * Closure for #find_advertisable_hello().
403 */ 402 */
404struct FindAdvHelloContext 403struct FindAdvHelloContext {
405{
406
407 /** 404 /**
408 * Peer we want to advertise to. 405 * Peer we want to advertise to.
409 */ 406 */
@@ -432,9 +429,9 @@ struct FindAdvHelloContext
432 * @return #GNUNET_YES (continue iteration) 429 * @return #GNUNET_YES (continue iteration)
433 */ 430 */
434static int 431static int
435find_advertisable_hello (void *cls, 432find_advertisable_hello(void *cls,
436 const struct GNUNET_PeerIdentity *pid, 433 const struct GNUNET_PeerIdentity *pid,
437 void *value) 434 void *value)
438{ 435{
439 struct FindAdvHelloContext *fah = cls; 436 struct FindAdvHelloContext *fah = cls;
440 struct Peer *pos = value; 437 struct Peer *pos = value;
@@ -446,21 +443,21 @@ find_advertisable_hello (void *cls,
446 return GNUNET_YES; 443 return GNUNET_YES;
447 if (pos->hello == NULL) 444 if (pos->hello == NULL)
448 return GNUNET_YES; 445 return GNUNET_YES;
449 rst_time = GNUNET_TIME_absolute_get_remaining (pos->filter_expiration); 446 rst_time = GNUNET_TIME_absolute_get_remaining(pos->filter_expiration);
450 if (0 == rst_time.rel_value_us) 447 if (0 == rst_time.rel_value_us)
451 { 448 {
452 /* time to discard... */ 449 /* time to discard... */
453 GNUNET_CONTAINER_bloomfilter_free (pos->filter); 450 GNUNET_CONTAINER_bloomfilter_free(pos->filter);
454 setup_filter (pos); 451 setup_filter(pos);
455 } 452 }
456 fah->next_adv = GNUNET_TIME_relative_min (rst_time, fah->next_adv); 453 fah->next_adv = GNUNET_TIME_relative_min(rst_time, fah->next_adv);
457 hs = GNUNET_HELLO_size (pos->hello); 454 hs = GNUNET_HELLO_size(pos->hello);
458 if (hs > fah->max_size) 455 if (hs > fah->max_size)
459 return GNUNET_YES; 456 return GNUNET_YES;
460 GNUNET_CRYPTO_hash (&fah->peer->pid, 457 GNUNET_CRYPTO_hash(&fah->peer->pid,
461 sizeof (struct GNUNET_PeerIdentity), 458 sizeof(struct GNUNET_PeerIdentity),
462 &hc); 459 &hc);
463 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test (pos->filter, &hc)) 460 if (GNUNET_NO == GNUNET_CONTAINER_bloomfilter_test(pos->filter, &hc))
464 fah->result = pos; 461 fah->result = pos;
465 return GNUNET_YES; 462 return GNUNET_YES;
466} 463}
@@ -473,7 +470,7 @@ find_advertisable_hello (void *cls,
473 * @param cls for which peer to schedule the HELLO 470 * @param cls for which peer to schedule the HELLO
474 */ 471 */
475static void 472static void
476schedule_next_hello (void *cls) 473schedule_next_hello(void *cls)
477{ 474{
478 struct Peer *pl = cls; 475 struct Peer *pl = cls;
479 struct FindAdvHelloContext fah; 476 struct FindAdvHelloContext fah;
@@ -483,42 +480,42 @@ schedule_next_hello (void *cls)
483 struct GNUNET_HashCode hc; 480 struct GNUNET_HashCode hc;
484 481
485 pl->hello_delay_task = NULL; 482 pl->hello_delay_task = NULL;
486 GNUNET_assert (NULL != pl->mq); 483 GNUNET_assert(NULL != pl->mq);
487 /* find applicable HELLOs */ 484 /* find applicable HELLOs */
488 fah.peer = pl; 485 fah.peer = pl;
489 fah.result = NULL; 486 fah.result = NULL;
490 fah.max_size = GNUNET_MAX_MESSAGE_SIZE - 1; 487 fah.max_size = GNUNET_MAX_MESSAGE_SIZE - 1;
491 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL; 488 fah.next_adv = GNUNET_TIME_UNIT_FOREVER_REL;
492 GNUNET_CONTAINER_multipeermap_iterate (peers, &find_advertisable_hello, &fah); 489 GNUNET_CONTAINER_multipeermap_iterate(peers, &find_advertisable_hello, &fah);
493 pl->hello_delay_task = 490 pl->hello_delay_task =
494 GNUNET_SCHEDULER_add_delayed (fah.next_adv, &schedule_next_hello, pl); 491 GNUNET_SCHEDULER_add_delayed(fah.next_adv, &schedule_next_hello, pl);
495 if (NULL == fah.result) 492 if (NULL == fah.result)
496 return; 493 return;
497 delay = GNUNET_TIME_absolute_get_remaining (pl->next_hello_allowed); 494 delay = GNUNET_TIME_absolute_get_remaining(pl->next_hello_allowed);
498 if (0 != delay.rel_value_us) 495 if (0 != delay.rel_value_us)
499 return; 496 return;
500 497
501 want = GNUNET_HELLO_size (fah.result->hello); 498 want = GNUNET_HELLO_size(fah.result->hello);
502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 499 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
503 "Sending HELLO with %u bytes", 500 "Sending HELLO with %u bytes",
504 (unsigned int) want); 501 (unsigned int)want);
505 env = GNUNET_MQ_msg_copy (&fah.result->hello->header); 502 env = GNUNET_MQ_msg_copy(&fah.result->hello->header);
506 GNUNET_MQ_send (pl->mq, env); 503 GNUNET_MQ_send(pl->mq, env);
507 504
508 /* avoid sending this one again soon */ 505 /* avoid sending this one again soon */
509 GNUNET_CRYPTO_hash (&pl->pid, sizeof (struct GNUNET_PeerIdentity), &hc); 506 GNUNET_CRYPTO_hash(&pl->pid, sizeof(struct GNUNET_PeerIdentity), &hc);
510 GNUNET_CONTAINER_bloomfilter_add (fah.result->filter, &hc); 507 GNUNET_CONTAINER_bloomfilter_add(fah.result->filter, &hc);
511 508
512 GNUNET_STATISTICS_update (stats, 509 GNUNET_STATISTICS_update(stats,
513 gettext_noop ("# HELLO messages gossipped"), 510 gettext_noop("# HELLO messages gossipped"),
514 1, 511 1,
515 GNUNET_NO); 512 GNUNET_NO);
516 /* prepare to send the next one */ 513 /* prepare to send the next one */
517 pl->next_hello_allowed = 514 pl->next_hello_allowed =
518 GNUNET_TIME_relative_to_absolute (HELLO_ADVERTISEMENT_MIN_FREQUENCY); 515 GNUNET_TIME_relative_to_absolute(HELLO_ADVERTISEMENT_MIN_FREQUENCY);
519 if (NULL != pl->hello_delay_task) 516 if (NULL != pl->hello_delay_task)
520 GNUNET_SCHEDULER_cancel (pl->hello_delay_task); 517 GNUNET_SCHEDULER_cancel(pl->hello_delay_task);
521 pl->hello_delay_task = GNUNET_SCHEDULER_add_now (&schedule_next_hello, pl); 518 pl->hello_delay_task = GNUNET_SCHEDULER_add_now(&schedule_next_hello, pl);
522} 519}
523 520
524 521
@@ -533,9 +530,9 @@ schedule_next_hello (void *cls)
533 * @return #GNUNET_YES (always) 530 * @return #GNUNET_YES (always)
534 */ 531 */
535static int 532static int
536reschedule_hellos (void *cls, 533reschedule_hellos(void *cls,
537 const struct GNUNET_PeerIdentity *pid, 534 const struct GNUNET_PeerIdentity *pid,
538 void *value) 535 void *value)
539{ 536{
540 struct Peer *peer = value; 537 struct Peer *peer = value;
541 struct Peer *skip = cls; 538 struct Peer *skip = cls;
@@ -545,12 +542,12 @@ reschedule_hellos (void *cls,
545 if (NULL == peer->mq) 542 if (NULL == peer->mq)
546 return GNUNET_YES; 543 return GNUNET_YES;
547 if (NULL != peer->hello_delay_task) 544 if (NULL != peer->hello_delay_task)
548 { 545 {
549 GNUNET_SCHEDULER_cancel (peer->hello_delay_task); 546 GNUNET_SCHEDULER_cancel(peer->hello_delay_task);
550 peer->hello_delay_task = NULL; 547 peer->hello_delay_task = NULL;
551 } 548 }
552 peer->hello_delay_task = 549 peer->hello_delay_task =
553 GNUNET_SCHEDULER_add_now (&schedule_next_hello, peer); 550 GNUNET_SCHEDULER_add_now(&schedule_next_hello, peer);
554 return GNUNET_YES; 551 return GNUNET_YES;
555} 552}
556 553
@@ -564,44 +561,44 @@ reschedule_hellos (void *cls,
564 * @return our `struct Peer` for @a peer 561 * @return our `struct Peer` for @a peer
565 */ 562 */
566static void * 563static void *
567connect_notify (void *cls, 564connect_notify(void *cls,
568 const struct GNUNET_PeerIdentity *peer, 565 const struct GNUNET_PeerIdentity *peer,
569 struct GNUNET_MQ_Handle *mq) 566 struct GNUNET_MQ_Handle *mq)
570{ 567{
571 struct Peer *pos; 568 struct Peer *pos;
572 569
573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 570 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
574 "Core told us that we are connecting to `%s'\n", 571 "Core told us that we are connecting to `%s'\n",
575 GNUNET_i2s (peer)); 572 GNUNET_i2s(peer));
576 if (0 == GNUNET_memcmp (&my_identity, peer)) 573 if (0 == GNUNET_memcmp(&my_identity, peer))
577 return NULL; 574 return NULL;
578 GNUNET_MQ_set_options (mq, GNUNET_MQ_PRIO_BEST_EFFORT); 575 GNUNET_MQ_set_options(mq, GNUNET_MQ_PRIO_BEST_EFFORT);
579 connection_count++; 576 connection_count++;
580 GNUNET_STATISTICS_set (stats, 577 GNUNET_STATISTICS_set(stats,
581 gettext_noop ("# peers connected"), 578 gettext_noop("# peers connected"),
582 connection_count, 579 connection_count,
583 GNUNET_NO); 580 GNUNET_NO);
584 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer); 581 pos = GNUNET_CONTAINER_multipeermap_get(peers, peer);
585 if (NULL == pos) 582 if (NULL == pos)
586 { 583 {
587 pos = make_peer (peer, NULL, GNUNET_NO); 584 pos = make_peer(peer, NULL, GNUNET_NO);
588 } 585 }
589 else 586 else
590 { 587 {
591 GNUNET_assert (NULL == pos->mq); 588 GNUNET_assert(NULL == pos->mq);
592 } 589 }
593 pos->mq = mq; 590 pos->mq = mq;
594 if (pos->is_friend) 591 if (pos->is_friend)
595 { 592 {
596 friend_count++; 593 friend_count++;
597 if ((friend_count == minimum_friend_count) && (GNUNET_YES != friends_only)) 594 if ((friend_count == minimum_friend_count) && (GNUNET_YES != friends_only))
598 whitelist_peers (); 595 whitelist_peers();
599 GNUNET_STATISTICS_set (stats, 596 GNUNET_STATISTICS_set(stats,
600 gettext_noop ("# friends connected"), 597 gettext_noop("# friends connected"),
601 friend_count, 598 friend_count,
602 GNUNET_NO); 599 GNUNET_NO);
603 } 600 }
604 reschedule_hellos (NULL, peer, pos); 601 reschedule_hellos(NULL, peer, pos);
605 return pos; 602 return pos;
606} 603}
607 604
@@ -615,11 +612,11 @@ connect_notify (void *cls,
615 * @return #GNUNET_YES (continue to iterate) 612 * @return #GNUNET_YES (continue to iterate)
616 */ 613 */
617static int 614static int
618try_add_peers (void *cls, const struct GNUNET_PeerIdentity *pid, void *value) 615try_add_peers(void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
619{ 616{
620 struct Peer *pos = value; 617 struct Peer *pos = value;
621 618
622 attempt_connect (pos); 619 attempt_connect(pos);
623 return GNUNET_YES; 620 return GNUNET_YES;
624} 621}
625 622
@@ -630,11 +627,11 @@ try_add_peers (void *cls, const struct GNUNET_PeerIdentity *pid, void *value)
630 * @param cls unused, NULL 627 * @param cls unused, NULL
631 */ 628 */
632static void 629static void
633add_peer_task (void *cls) 630add_peer_task(void *cls)
634{ 631{
635 add_task = NULL; 632 add_task = NULL;
636 633
637 GNUNET_CONTAINER_multipeermap_iterate (peers, &try_add_peers, NULL); 634 GNUNET_CONTAINER_multipeermap_iterate(peers, &try_add_peers, NULL);
638} 635}
639 636
640 637
@@ -646,47 +643,47 @@ add_peer_task (void *cls)
646 * @param internal_cls the `struct Peer` for this peer 643 * @param internal_cls the `struct Peer` for this peer
647 */ 644 */
648static void 645static void
649disconnect_notify (void *cls, 646disconnect_notify(void *cls,
650 const struct GNUNET_PeerIdentity *peer, 647 const struct GNUNET_PeerIdentity *peer,
651 void *internal_cls) 648 void *internal_cls)
652{ 649{
653 struct Peer *pos = internal_cls; 650 struct Peer *pos = internal_cls;
654 651
655 if (NULL == pos) 652 if (NULL == pos)
656 return; /* myself, we're shutting down */ 653 return; /* myself, we're shutting down */
657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 654 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
658 "Core told us that we disconnected from `%s'\n", 655 "Core told us that we disconnected from `%s'\n",
659 GNUNET_i2s (peer)); 656 GNUNET_i2s(peer));
660 if (NULL == pos->mq) 657 if (NULL == pos->mq)
661 { 658 {
662 GNUNET_break (0); 659 GNUNET_break(0);
663 return; 660 return;
664 } 661 }
665 pos->mq = NULL; 662 pos->mq = NULL;
666 connection_count--; 663 connection_count--;
667 if (NULL != pos->hello_delay_task) 664 if (NULL != pos->hello_delay_task)
668 { 665 {
669 GNUNET_SCHEDULER_cancel (pos->hello_delay_task); 666 GNUNET_SCHEDULER_cancel(pos->hello_delay_task);
670 pos->hello_delay_task = NULL; 667 pos->hello_delay_task = NULL;
671 } 668 }
672 GNUNET_STATISTICS_set (stats, 669 GNUNET_STATISTICS_set(stats,
673 gettext_noop ("# peers connected"), 670 gettext_noop("# peers connected"),
674 connection_count, 671 connection_count,
675 GNUNET_NO); 672 GNUNET_NO);
676 if (pos->is_friend) 673 if (pos->is_friend)
677 { 674 {
678 friend_count--; 675 friend_count--;
679 GNUNET_STATISTICS_set (stats, 676 GNUNET_STATISTICS_set(stats,
680 gettext_noop ("# friends connected"), 677 gettext_noop("# friends connected"),
681 friend_count, 678 friend_count,
682 GNUNET_NO); 679 GNUNET_NO);
683 } 680 }
684 if (((connection_count < target_connection_count) || 681 if (((connection_count < target_connection_count) ||
685 (friend_count < minimum_friend_count)) && 682 (friend_count < minimum_friend_count)) &&
686 (NULL == add_task)) 683 (NULL == add_task))
687 add_task = GNUNET_SCHEDULER_add_now (&add_peer_task, NULL); 684 add_task = GNUNET_SCHEDULER_add_now(&add_peer_task, NULL);
688 if ((friend_count < minimum_friend_count) && (NULL == blacklist)) 685 if ((friend_count < minimum_friend_count) && (NULL == blacklist))
689 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL); 686 blacklist = GNUNET_TRANSPORT_blacklist(cfg, &blacklist_check, NULL);
690} 687}
691 688
692 689
@@ -699,9 +696,9 @@ disconnect_notify (void *cls,
699 * @return #GNUNET_SYSERR always, to terminate iteration 696 * @return #GNUNET_SYSERR always, to terminate iteration
700 */ 697 */
701static int 698static int
702address_iterator (void *cls, 699address_iterator(void *cls,
703 const struct GNUNET_HELLO_Address *address, 700 const struct GNUNET_HELLO_Address *address,
704 struct GNUNET_TIME_Absolute expiration) 701 struct GNUNET_TIME_Absolute expiration)
705{ 702{
706 int *flag = cls; 703 int *flag = cls;
707 704
@@ -717,7 +714,7 @@ address_iterator (void *cls,
717 * @param hello the HELLO we got 714 * @param hello the HELLO we got
718 */ 715 */
719static void 716static void
720consider_for_advertising (const struct GNUNET_HELLO_Message *hello) 717consider_for_advertising(const struct GNUNET_HELLO_Message *hello)
721{ 718{
722 int have_address; 719 int have_address;
723 struct GNUNET_PeerIdentity pid; 720 struct GNUNET_PeerIdentity pid;
@@ -726,55 +723,55 @@ consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
726 struct Peer *peer; 723 struct Peer *peer;
727 uint16_t size; 724 uint16_t size;
728 725
729 if (GNUNET_OK != GNUNET_HELLO_get_id (hello, &pid)) 726 if (GNUNET_OK != GNUNET_HELLO_get_id(hello, &pid))
730 { 727 {
731 GNUNET_break (0); 728 GNUNET_break(0);
732 return; 729 return;
733 } 730 }
734 if (0 == GNUNET_memcmp (&pid, &my_identity)) 731 if (0 == GNUNET_memcmp(&pid, &my_identity))
735 return; /* that's me! */ 732 return; /* that's me! */
736 have_address = GNUNET_NO; 733 have_address = GNUNET_NO;
737 GNUNET_HELLO_iterate_addresses (hello, 734 GNUNET_HELLO_iterate_addresses(hello,
738 GNUNET_NO, 735 GNUNET_NO,
739 &address_iterator, 736 &address_iterator,
740 &have_address); 737 &have_address);
741 if (GNUNET_NO == have_address) 738 if (GNUNET_NO == have_address)
742 return; /* no point in advertising this one... */ 739 return; /* no point in advertising this one... */
743 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid); 740 peer = GNUNET_CONTAINER_multipeermap_get(peers, &pid);
744 if (NULL == peer) 741 if (NULL == peer)
745 { 742 {
746 peer = make_peer (&pid, hello, GNUNET_NO); 743 peer = make_peer(&pid, hello, GNUNET_NO);
747 } 744 }
748 else if (NULL != peer->hello) 745 else if (NULL != peer->hello)
749 { 746 {
750 dt = GNUNET_HELLO_equals (peer->hello, hello, GNUNET_TIME_absolute_get ()); 747 dt = GNUNET_HELLO_equals(peer->hello, hello, GNUNET_TIME_absolute_get());
751 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us) 748 if (dt.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
752 return; /* nothing new here */ 749 return; /* nothing new here */
753 } 750 }
754 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 751 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
755 "Found HELLO from peer `%s' for advertising\n", 752 "Found HELLO from peer `%s' for advertising\n",
756 GNUNET_i2s (&pid)); 753 GNUNET_i2s(&pid));
757 if (NULL != peer->hello) 754 if (NULL != peer->hello)
758 { 755 {
759 nh = GNUNET_HELLO_merge (peer->hello, hello); 756 nh = GNUNET_HELLO_merge(peer->hello, hello);
760 GNUNET_free (peer->hello); 757 GNUNET_free(peer->hello);
761 peer->hello = nh; 758 peer->hello = nh;
762 } 759 }
763 else 760 else
764 { 761 {
765 size = GNUNET_HELLO_size (hello); 762 size = GNUNET_HELLO_size(hello);
766 peer->hello = GNUNET_malloc (size); 763 peer->hello = GNUNET_malloc(size);
767 GNUNET_memcpy (peer->hello, hello, size); 764 GNUNET_memcpy(peer->hello, hello, size);
768 } 765 }
769 if (NULL != peer->filter) 766 if (NULL != peer->filter)
770 { 767 {
771 GNUNET_CONTAINER_bloomfilter_free (peer->filter); 768 GNUNET_CONTAINER_bloomfilter_free(peer->filter);
772 peer->filter = NULL; 769 peer->filter = NULL;
773 } 770 }
774 setup_filter (peer); 771 setup_filter(peer);
775 /* since we have a new HELLO to pick from, re-schedule all 772 /* since we have a new HELLO to pick from, re-schedule all
776 * HELLO requests that are not bound by the HELLO send rate! */ 773 * HELLO requests that are not bound by the HELLO send rate! */
777 GNUNET_CONTAINER_multipeermap_iterate (peers, &reschedule_hellos, peer); 774 GNUNET_CONTAINER_multipeermap_iterate(peers, &reschedule_hellos, peer);
778} 775}
779 776
780 777
@@ -788,49 +785,49 @@ consider_for_advertising (const struct GNUNET_HELLO_Message *hello)
788 * @param err_msg NULL if successful, otherwise contains error message 785 * @param err_msg NULL if successful, otherwise contains error message
789 */ 786 */
790static void 787static void
791process_peer (void *cls, 788process_peer(void *cls,
792 const struct GNUNET_PeerIdentity *peer, 789 const struct GNUNET_PeerIdentity *peer,
793 const struct GNUNET_HELLO_Message *hello, 790 const struct GNUNET_HELLO_Message *hello,
794 const char *err_msg) 791 const char *err_msg)
795{ 792{
796 struct Peer *pos; 793 struct Peer *pos;
797 794
798 if (NULL != err_msg) 795 if (NULL != err_msg)
799 { 796 {
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 797 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
801 _ ("Error in communication with PEERINFO service: %s\n"), 798 _("Error in communication with PEERINFO service: %s\n"),
802 err_msg); 799 err_msg);
803 GNUNET_PEERINFO_notify_cancel (peerinfo_notify); 800 GNUNET_PEERINFO_notify_cancel(peerinfo_notify);
804 peerinfo_notify = 801 peerinfo_notify =
805 GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_peer, NULL); 802 GNUNET_PEERINFO_notify(cfg, GNUNET_NO, &process_peer, NULL);
806 return; 803 return;
807 } 804 }
808 GNUNET_assert (NULL != peer); 805 GNUNET_assert(NULL != peer);
809 if (0 == GNUNET_memcmp (&my_identity, peer)) 806 if (0 == GNUNET_memcmp(&my_identity, peer))
810 return; /* that's me! */ 807 return; /* that's me! */
811 if (NULL == hello) 808 if (NULL == hello)
812 {
813 /* free existing HELLO, if any */
814 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
815 if (NULL != pos)
816 { 809 {
817 GNUNET_free_non_null (pos->hello); 810 /* free existing HELLO, if any */
818 pos->hello = NULL; 811 pos = GNUNET_CONTAINER_multipeermap_get(peers, peer);
819 if (NULL != pos->filter) 812 if (NULL != pos)
820 { 813 {
821 GNUNET_CONTAINER_bloomfilter_free (pos->filter); 814 GNUNET_free_non_null(pos->hello);
822 pos->filter = NULL; 815 pos->hello = NULL;
823 } 816 if (NULL != pos->filter)
824 if ((NULL == pos->mq) && (GNUNET_NO == pos->is_friend)) 817 {
825 free_peer (NULL, &pos->pid, pos); 818 GNUNET_CONTAINER_bloomfilter_free(pos->filter);
819 pos->filter = NULL;
820 }
821 if ((NULL == pos->mq) && (GNUNET_NO == pos->is_friend))
822 free_peer(NULL, &pos->pid, pos);
823 }
824 return;
826 } 825 }
827 return; 826 consider_for_advertising(hello);
828 } 827 pos = GNUNET_CONTAINER_multipeermap_get(peers, peer);
829 consider_for_advertising (hello);
830 pos = GNUNET_CONTAINER_multipeermap_get (peers, peer);
831 if (NULL == pos) 828 if (NULL == pos)
832 pos = make_peer (peer, hello, GNUNET_NO); 829 pos = make_peer(peer, hello, GNUNET_NO);
833 attempt_connect (pos); 830 attempt_connect(pos);
834} 831}
835 832
836 833
@@ -842,20 +839,20 @@ process_peer (void *cls,
842 * @param my_id ID of this peer, NULL if we failed 839 * @param my_id ID of this peer, NULL if we failed
843 */ 840 */
844static void 841static void
845core_init (void *cls, const struct GNUNET_PeerIdentity *my_id) 842core_init(void *cls, const struct GNUNET_PeerIdentity *my_id)
846{ 843{
847 if (NULL == my_id) 844 if (NULL == my_id)
848 { 845 {
849 GNUNET_log ( 846 GNUNET_log(
850 GNUNET_ERROR_TYPE_ERROR, 847 GNUNET_ERROR_TYPE_ERROR,
851 _ ("Failed to connect to core service, can not manage topology!\n")); 848 _("Failed to connect to core service, can not manage topology!\n"));
852 GNUNET_SCHEDULER_shutdown (); 849 GNUNET_SCHEDULER_shutdown();
853 return; 850 return;
854 } 851 }
855 my_identity = *my_id; 852 my_identity = *my_id;
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s (my_id)); 853 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "I am peer `%s'\n", GNUNET_i2s(my_id));
857 peerinfo_notify = 854 peerinfo_notify =
858 GNUNET_PEERINFO_notify (cfg, GNUNET_NO, &process_peer, NULL); 855 GNUNET_PEERINFO_notify(cfg, GNUNET_NO, &process_peer, NULL);
859} 856}
860 857
861 858
@@ -866,23 +863,23 @@ core_init (void *cls, const struct GNUNET_PeerIdentity *my_id)
866 * @param pid identity of the friend 863 * @param pid identity of the friend
867 */ 864 */
868static void 865static void
869handle_friend (void *cls, const struct GNUNET_PeerIdentity *pid) 866handle_friend(void *cls, const struct GNUNET_PeerIdentity *pid)
870{ 867{
871 unsigned int *entries_found = cls; 868 unsigned int *entries_found = cls;
872 struct Peer *fl; 869 struct Peer *fl;
873 870
874 if (0 == GNUNET_memcmp (pid, &my_identity)) 871 if (0 == GNUNET_memcmp(pid, &my_identity))
875 { 872 {
876 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 873 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
877 _ ("Found myself `%s' in friend list (useless, ignored)\n"), 874 _("Found myself `%s' in friend list (useless, ignored)\n"),
878 GNUNET_i2s (pid)); 875 GNUNET_i2s(pid));
879 return; 876 return;
880 } 877 }
881 (*entries_found)++; 878 (*entries_found)++;
882 fl = make_peer (pid, NULL, GNUNET_YES); 879 fl = make_peer(pid, NULL, GNUNET_YES);
883 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 880 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
884 _ ("Found friend `%s' in configuration\n"), 881 _("Found friend `%s' in configuration\n"),
885 GNUNET_i2s (&fl->pid)); 882 GNUNET_i2s(&fl->pid));
886} 883}
887 884
888 885
@@ -890,36 +887,36 @@ handle_friend (void *cls, const struct GNUNET_PeerIdentity *pid)
890 * Read the friends file. 887 * Read the friends file.
891 */ 888 */
892static void 889static void
893read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg) 890read_friends_file(const struct GNUNET_CONFIGURATION_Handle *cfg)
894{ 891{
895 unsigned int entries_found; 892 unsigned int entries_found;
896 893
897 entries_found = 0; 894 entries_found = 0;
898 if (GNUNET_OK != GNUNET_FRIENDS_parse (cfg, &handle_friend, &entries_found)) 895 if (GNUNET_OK != GNUNET_FRIENDS_parse(cfg, &handle_friend, &entries_found))
899 { 896 {
900 if ((GNUNET_YES == friends_only) || (minimum_friend_count > 0)) 897 if ((GNUNET_YES == friends_only) || (minimum_friend_count > 0))
901 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 898 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
902 _ ("Encountered errors parsing friends list!\n")); 899 _("Encountered errors parsing friends list!\n"));
903 } 900 }
904 GNUNET_STATISTICS_update (stats, 901 GNUNET_STATISTICS_update(stats,
905 gettext_noop ("# friends in configuration"), 902 gettext_noop("# friends in configuration"),
906 entries_found, 903 entries_found,
907 GNUNET_NO); 904 GNUNET_NO);
908 if ((minimum_friend_count > entries_found) && (GNUNET_NO == friends_only)) 905 if ((minimum_friend_count > entries_found) && (GNUNET_NO == friends_only))
909 { 906 {
910 GNUNET_log ( 907 GNUNET_log(
911 GNUNET_ERROR_TYPE_WARNING, 908 GNUNET_ERROR_TYPE_WARNING,
912 _ ( 909 _(
913 "Fewer friends specified than required by minimum friend count. Will only connect to friends.\n")); 910 "Fewer friends specified than required by minimum friend count. Will only connect to friends.\n"));
914 } 911 }
915 if ((minimum_friend_count > target_connection_count) && 912 if ((minimum_friend_count > target_connection_count) &&
916 (GNUNET_NO == friends_only)) 913 (GNUNET_NO == friends_only))
917 { 914 {
918 GNUNET_log ( 915 GNUNET_log(
919 GNUNET_ERROR_TYPE_WARNING, 916 GNUNET_ERROR_TYPE_WARNING,
920 _ ( 917 _(
921 "More friendly connections required than target total number of connections.\n")); 918 "More friendly connections required than target total number of connections.\n"));
922 } 919 }
923} 920}
924 921
925 922
@@ -933,15 +930,15 @@ read_friends_file (const struct GNUNET_CONFIGURATION_Handle *cfg)
933 * #GNUNET_SYSERR if @a message is invalid 930 * #GNUNET_SYSERR if @a message is invalid
934 */ 931 */
935static int 932static int
936check_hello (void *cls, const struct GNUNET_HELLO_Message *message) 933check_hello(void *cls, const struct GNUNET_HELLO_Message *message)
937{ 934{
938 struct GNUNET_PeerIdentity pid; 935 struct GNUNET_PeerIdentity pid;
939 936
940 if (GNUNET_OK != GNUNET_HELLO_get_id (message, &pid)) 937 if (GNUNET_OK != GNUNET_HELLO_get_id(message, &pid))
941 { 938 {
942 GNUNET_break_op (0); 939 GNUNET_break_op(0);
943 return GNUNET_SYSERR; 940 return GNUNET_SYSERR;
944 } 941 }
945 return GNUNET_OK; 942 return GNUNET_OK;
946} 943}
947 944
@@ -954,35 +951,35 @@ check_hello (void *cls, const struct GNUNET_HELLO_Message *message)
954 * @param message the actual HELLO message 951 * @param message the actual HELLO message
955 */ 952 */
956static void 953static void
957handle_hello (void *cls, const struct GNUNET_HELLO_Message *message) 954handle_hello(void *cls, const struct GNUNET_HELLO_Message *message)
958{ 955{
959 const struct GNUNET_PeerIdentity *other = cls; 956 const struct GNUNET_PeerIdentity *other = cls;
960 struct Peer *peer; 957 struct Peer *peer;
961 struct GNUNET_PeerIdentity pid; 958 struct GNUNET_PeerIdentity pid;
962 959
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 960 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
964 "Received encrypted HELLO from peer `%s'", 961 "Received encrypted HELLO from peer `%s'",
965 GNUNET_i2s (other)); 962 GNUNET_i2s(other));
966 GNUNET_assert (GNUNET_OK == GNUNET_HELLO_get_id (message, &pid)); 963 GNUNET_assert(GNUNET_OK == GNUNET_HELLO_get_id(message, &pid));
967 GNUNET_STATISTICS_update (stats, 964 GNUNET_STATISTICS_update(stats,
968 gettext_noop ("# HELLO messages received"), 965 gettext_noop("# HELLO messages received"),
969 1, 966 1,
970 GNUNET_NO); 967 GNUNET_NO);
971 peer = GNUNET_CONTAINER_multipeermap_get (peers, &pid); 968 peer = GNUNET_CONTAINER_multipeermap_get(peers, &pid);
972 if (NULL == peer) 969 if (NULL == peer)
973 { 970 {
974 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count)) 971 if ((GNUNET_YES == friends_only) || (friend_count < minimum_friend_count))
975 return; 972 return;
976 } 973 }
977 else 974 else
978 { 975 {
979 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only)) 976 if ((GNUNET_YES != peer->is_friend) && (GNUNET_YES == friends_only))
980 return; 977 return;
981 if ((GNUNET_YES != peer->is_friend) && 978 if ((GNUNET_YES != peer->is_friend) &&
982 (friend_count < minimum_friend_count)) 979 (friend_count < minimum_friend_count))
983 return; 980 return;
984 } 981 }
985 (void) GNUNET_PEERINFO_add_peer (pi, message, NULL, NULL); 982 (void)GNUNET_PEERINFO_add_peer(pi, message, NULL, NULL);
986} 983}
987 984
988 985
@@ -993,47 +990,47 @@ handle_hello (void *cls, const struct GNUNET_HELLO_Message *message)
993 * @param cls unused, NULL 990 * @param cls unused, NULL
994 */ 991 */
995static void 992static void
996cleaning_task (void *cls) 993cleaning_task(void *cls)
997{ 994{
998 if (NULL != peerinfo_notify) 995 if (NULL != peerinfo_notify)
999 { 996 {
1000 GNUNET_PEERINFO_notify_cancel (peerinfo_notify); 997 GNUNET_PEERINFO_notify_cancel(peerinfo_notify);
1001 peerinfo_notify = NULL; 998 peerinfo_notify = NULL;
1002 } 999 }
1003 if (NULL != handle) 1000 if (NULL != handle)
1004 { 1001 {
1005 GNUNET_CORE_disconnect (handle); 1002 GNUNET_CORE_disconnect(handle);
1006 handle = NULL; 1003 handle = NULL;
1007 } 1004 }
1008 whitelist_peers (); 1005 whitelist_peers();
1009 if (NULL != add_task) 1006 if (NULL != add_task)
1010 { 1007 {
1011 GNUNET_SCHEDULER_cancel (add_task); 1008 GNUNET_SCHEDULER_cancel(add_task);
1012 add_task = NULL; 1009 add_task = NULL;
1013 } 1010 }
1014 if (NULL != oh) 1011 if (NULL != oh)
1015 { 1012 {
1016 GNUNET_TRANSPORT_offer_hello_cancel (oh); 1013 GNUNET_TRANSPORT_offer_hello_cancel(oh);
1017 oh = NULL; 1014 oh = NULL;
1018 } 1015 }
1019 GNUNET_CONTAINER_multipeermap_iterate (peers, &free_peer, NULL); 1016 GNUNET_CONTAINER_multipeermap_iterate(peers, &free_peer, NULL);
1020 GNUNET_CONTAINER_multipeermap_destroy (peers); 1017 GNUNET_CONTAINER_multipeermap_destroy(peers);
1021 peers = NULL; 1018 peers = NULL;
1022 if (NULL != ats) 1019 if (NULL != ats)
1023 { 1020 {
1024 GNUNET_ATS_connectivity_done (ats); 1021 GNUNET_ATS_connectivity_done(ats);
1025 ats = NULL; 1022 ats = NULL;
1026 } 1023 }
1027 if (NULL != pi) 1024 if (NULL != pi)
1028 { 1025 {
1029 GNUNET_PEERINFO_disconnect (pi); 1026 GNUNET_PEERINFO_disconnect(pi);
1030 pi = NULL; 1027 pi = NULL;
1031 } 1028 }
1032 if (NULL != stats) 1029 if (NULL != stats)
1033 { 1030 {
1034 GNUNET_STATISTICS_destroy (stats, GNUNET_NO); 1031 GNUNET_STATISTICS_destroy(stats, GNUNET_NO);
1035 stats = NULL; 1032 stats = NULL;
1036 } 1033 }
1037} 1034}
1038 1035
1039 1036
@@ -1046,62 +1043,62 @@ cleaning_task (void *cls)
1046 * @param c configuration 1043 * @param c configuration
1047 */ 1044 */
1048static void 1045static void
1049run (void *cls, 1046run(void *cls,
1050 char *const *args, 1047 char *const *args,
1051 const char *cfgfile, 1048 const char *cfgfile,
1052 const struct GNUNET_CONFIGURATION_Handle *c) 1049 const struct GNUNET_CONFIGURATION_Handle *c)
1053{ 1050{
1054 struct GNUNET_MQ_MessageHandler handlers[] = 1051 struct GNUNET_MQ_MessageHandler handlers[] =
1055 {GNUNET_MQ_hd_var_size (hello, 1052 { GNUNET_MQ_hd_var_size(hello,
1056 GNUNET_MESSAGE_TYPE_HELLO, 1053 GNUNET_MESSAGE_TYPE_HELLO,
1057 struct GNUNET_HELLO_Message, 1054 struct GNUNET_HELLO_Message,
1058 NULL), 1055 NULL),
1059 GNUNET_MQ_handler_end ()}; 1056 GNUNET_MQ_handler_end() };
1060 unsigned long long opt; 1057 unsigned long long opt;
1061 1058
1062 cfg = c; 1059 cfg = c;
1063 stats = GNUNET_STATISTICS_create ("topology", cfg); 1060 stats = GNUNET_STATISTICS_create("topology", cfg);
1064 friends_only = 1061 friends_only =
1065 GNUNET_CONFIGURATION_get_value_yesno (cfg, "TOPOLOGY", "FRIENDS-ONLY"); 1062 GNUNET_CONFIGURATION_get_value_yesno(cfg, "TOPOLOGY", "FRIENDS-ONLY");
1066 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, 1063 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number(cfg,
1067 "TOPOLOGY", 1064 "TOPOLOGY",
1068 "MINIMUM-FRIENDS", 1065 "MINIMUM-FRIENDS",
1069 &opt)) 1066 &opt))
1070 opt = 0; 1067 opt = 0;
1071 minimum_friend_count = (unsigned int) opt; 1068 minimum_friend_count = (unsigned int)opt;
1072 if (GNUNET_OK != 1069 if (GNUNET_OK !=
1073 GNUNET_CONFIGURATION_get_value_number (cfg, 1070 GNUNET_CONFIGURATION_get_value_number(cfg,
1074 "TOPOLOGY", 1071 "TOPOLOGY",
1075 "TARGET-CONNECTION-COUNT", 1072 "TARGET-CONNECTION-COUNT",
1076 &opt)) 1073 &opt))
1077 opt = 16; 1074 opt = 16;
1078 target_connection_count = (unsigned int) opt; 1075 target_connection_count = (unsigned int)opt;
1079 peers = GNUNET_CONTAINER_multipeermap_create (target_connection_count * 2, 1076 peers = GNUNET_CONTAINER_multipeermap_create(target_connection_count * 2,
1080 GNUNET_NO); 1077 GNUNET_NO);
1081 read_friends_file (cfg); 1078 read_friends_file(cfg);
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1079 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1083 "Topology would like %u connections with at least %u friends\n", 1080 "Topology would like %u connections with at least %u friends\n",
1084 target_connection_count, 1081 target_connection_count,
1085 minimum_friend_count); 1082 minimum_friend_count);
1086 if ((GNUNET_YES == friends_only) || (minimum_friend_count > 0)) 1083 if ((GNUNET_YES == friends_only) || (minimum_friend_count > 0))
1087 blacklist = GNUNET_TRANSPORT_blacklist (cfg, &blacklist_check, NULL); 1084 blacklist = GNUNET_TRANSPORT_blacklist(cfg, &blacklist_check, NULL);
1088 ats = GNUNET_ATS_connectivity_init (cfg); 1085 ats = GNUNET_ATS_connectivity_init(cfg);
1089 pi = GNUNET_PEERINFO_connect (cfg); 1086 pi = GNUNET_PEERINFO_connect(cfg);
1090 handle = GNUNET_CORE_connect (cfg, 1087 handle = GNUNET_CORE_connect(cfg,
1091 NULL, 1088 NULL,
1092 &core_init, 1089 &core_init,
1093 &connect_notify, 1090 &connect_notify,
1094 &disconnect_notify, 1091 &disconnect_notify,
1095 handlers); 1092 handlers);
1096 GNUNET_SCHEDULER_add_shutdown (&cleaning_task, NULL); 1093 GNUNET_SCHEDULER_add_shutdown(&cleaning_task, NULL);
1097 if (NULL == handle) 1094 if (NULL == handle)
1098 { 1095 {
1099 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 1096 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
1100 _ ("Failed to connect to `%s' service.\n"), 1097 _("Failed to connect to `%s' service.\n"),
1101 "core"); 1098 "core");
1102 GNUNET_SCHEDULER_shutdown (); 1099 GNUNET_SCHEDULER_shutdown();
1103 return; 1100 return;
1104 } 1101 }
1105} 1102}
1106 1103
1107 1104
@@ -1113,25 +1110,26 @@ run (void *cls,
1113 * @return 0 ok, 1 on error 1110 * @return 0 ok, 1 on error
1114 */ 1111 */
1115int 1112int
1116main (int argc, char *const *argv) 1113main(int argc, char *const *argv)
1117{ 1114{
1118 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 1115 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1119 GNUNET_GETOPT_OPTION_END}; 1116 GNUNET_GETOPT_OPTION_END
1117 };
1120 int ret; 1118 int ret;
1121 1119
1122 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 1120 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv))
1123 return 2; 1121 return 2;
1124 1122
1125 ret = (GNUNET_OK == GNUNET_PROGRAM_run (argc, 1123 ret = (GNUNET_OK == GNUNET_PROGRAM_run(argc,
1126 argv, 1124 argv,
1127 "gnunet-daemon-topology", 1125 "gnunet-daemon-topology",
1128 _ ("GNUnet topology control"), 1126 _("GNUnet topology control"),
1129 options, 1127 options,
1130 &run, 1128 &run,
1131 NULL)) 1129 NULL))
1132 ? 0 1130 ? 0
1133 : 1; 1131 : 1;
1134 GNUNET_free ((void *) argv); 1132 GNUNET_free((void *)argv);
1135 return ret; 1133 return ret;
1136} 1134}
1137 1135
@@ -1142,11 +1140,11 @@ main (int argc, char *const *argv)
1142/** 1140/**
1143 * MINIMIZE heap size (way below 128k) since this process doesn't need much. 1141 * MINIMIZE heap size (way below 128k) since this process doesn't need much.
1144 */ 1142 */
1145void __attribute__ ((constructor)) GNUNET_ARM_memory_init () 1143void __attribute__ ((constructor)) GNUNET_ARM_memory_init()
1146{ 1144{
1147 mallopt (M_TRIM_THRESHOLD, 4 * 1024); 1145 mallopt(M_TRIM_THRESHOLD, 4 * 1024);
1148 mallopt (M_TOP_PAD, 1 * 1024); 1146 mallopt(M_TOP_PAD, 1 * 1024);
1149 malloc_trim (0); 1147 malloc_trim(0);
1150} 1148}
1151#endif 1149#endif
1152 1150
diff --git a/src/topology/test_gnunet_daemon_topology.c b/src/topology/test_gnunet_daemon_topology.c
index 6b2c58208..9e2db4f66 100644
--- a/src/topology/test_gnunet_daemon_topology.c
+++ b/src/topology/test_gnunet_daemon_topology.c
@@ -16,7 +16,7 @@
16 along with this program. If not, see <http://www.gnu.org/licenses/>. 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 17
18 SPDX-License-Identifier: AGPL3.0-or-later 18 SPDX-License-Identifier: AGPL3.0-or-later
19*/ 19 */
20/** 20/**
21 * @file topology/test_gnunet_daemon_topology.c 21 * @file topology/test_gnunet_daemon_topology.c
22 * @brief testcase for topology maintenance code 22 * @brief testcase for topology maintenance code
@@ -36,12 +36,12 @@
36 * too high can cause bandwidth problems for the testing peers. 36 * too high can cause bandwidth problems for the testing peers.
37 * Normal should be 5KB/s per peer. See gnunet-config -s ats. 37 * Normal should be 5KB/s per peer. See gnunet-config -s ats.
38 */ 38 */
39#define THRESHOLD NUM_PEERS/2 39#define THRESHOLD NUM_PEERS / 2
40 40
41/** 41/**
42 * How long until we give up on connecting the peers? 42 * How long until we give up on connecting the peers?
43 */ 43 */
44#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60) 44#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 60)
45 45
46/* 46/*
47 * Store manual connections. 47 * Store manual connections.
@@ -71,8 +71,7 @@ static struct GNUNET_SCHEDULER_Task *timeout_tid;
71/* 71/*
72 * Peer context for every testbed peer. 72 * Peer context for every testbed peer.
73 */ 73 */
74struct peerctx 74struct peerctx {
75{
76 int index; 75 int index;
77 struct GNUNET_STATISTICS_Handle *statistics; 76 struct GNUNET_STATISTICS_Handle *statistics;
78 int connections; 77 int connections;
@@ -81,28 +80,29 @@ struct peerctx
81 80
82 81
83static void 82static void
84shutdown_task (void *cls) 83shutdown_task(void *cls)
85{ 84{
86 unsigned int i; 85 unsigned int i;
87 86
88 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 87 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
89 "Shutting down testcase\n"); 88 "Shutting down testcase\n");
90 89
91 for (i=0;i<NUM_PEERS;i++) { 90 for (i = 0; i < NUM_PEERS; i++)
92 if (NULL != op[i]) 91 {
93 GNUNET_TESTBED_operation_done (op[i]); 92 if (NULL != op[i])
94 } 93 GNUNET_TESTBED_operation_done(op[i]);
94 }
95 95
96 if (NULL != timeout_tid) 96 if (NULL != timeout_tid)
97 GNUNET_SCHEDULER_cancel (timeout_tid); 97 GNUNET_SCHEDULER_cancel(timeout_tid);
98} 98}
99 99
100static void 100static void
101timeout_task (void *cls) 101timeout_task(void *cls)
102{ 102{
103 timeout_tid = NULL; 103 timeout_tid = NULL;
104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 104 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
105 "Testcase timeout\n"); 105 "Testcase timeout\n");
106 106
107 result = GNUNET_SYSERR; 107 result = GNUNET_SYSERR;
108 GNUNET_SCHEDULER_shutdown(); 108 GNUNET_SCHEDULER_shutdown();
@@ -113,172 +113,174 @@ timeout_task (void *cls)
113 * peers to a peer changes. 113 * peers to a peer changes.
114 */ 114 */
115int 115int
116statistics_iterator (void *cls, 116statistics_iterator(void *cls,
117 const char *subsystem, 117 const char *subsystem,
118 const char *name, 118 const char *name,
119 uint64_t value, 119 uint64_t value,
120 int is_persistent) 120 int is_persistent)
121{ 121{
122 struct peerctx *p_ctx = (struct peerctx*) cls; 122 struct peerctx *p_ctx = (struct peerctx*)cls;
123 123
124 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 124 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
125 "Peer %d: %s = %llu\n", 125 "Peer %d: %s = %llu\n",
126 p_ctx->index, 126 p_ctx->index,
127 name, 127 name,
128 (unsigned long long) value); 128 (unsigned long long)value);
129 129
130 if (p_ctx->connections < value) 130 if (p_ctx->connections < value)
131 p_ctx->connections = value; 131 p_ctx->connections = value;
132 132
133 if (THRESHOLD <= value && GNUNET_NO == p_ctx->reported) { 133 if (THRESHOLD <= value && GNUNET_NO == p_ctx->reported)
134 p_ctx->reported = GNUNET_YES; 134 {
135 checked_peers++; 135 p_ctx->reported = GNUNET_YES;
136 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 136 checked_peers++;
137 "Peer %d successfully connected to at least %d peers once.\n",
138 p_ctx->index,
139 THRESHOLD);
140
141 if (checked_peers == NUM_PEERS) {
142 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 137 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
143 "Test OK: All peers have connected to %d peers once.\n", 138 "Peer %d successfully connected to at least %d peers once.\n",
144 THRESHOLD); 139 p_ctx->index,
145 result = GNUNET_YES; 140 THRESHOLD);
146 GNUNET_SCHEDULER_shutdown(); 141
142 if (checked_peers == NUM_PEERS)
143 {
144 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
145 "Test OK: All peers have connected to %d peers once.\n",
146 THRESHOLD);
147 result = GNUNET_YES;
148 GNUNET_SCHEDULER_shutdown();
149 }
147 } 150 }
148 }
149 151
150 return GNUNET_YES; 152 return GNUNET_YES;
151} 153}
152 154
153static void * 155static void *
154ca_statistics (void *cls, 156ca_statistics(void *cls,
155 const struct GNUNET_CONFIGURATION_Handle *cfg) 157 const struct GNUNET_CONFIGURATION_Handle *cfg)
156{ 158{
157 return GNUNET_STATISTICS_create ("topology", cfg); 159 return GNUNET_STATISTICS_create("topology", cfg);
158} 160}
159 161
160 162
161void 163void
162da_statistics (void *cls, 164da_statistics(void *cls,
163 void *op_result) 165 void *op_result)
164{ 166{
165 struct peerctx *p_ctx = (struct peerctx *) cls; 167 struct peerctx *p_ctx = (struct peerctx *)cls;
166 168
167 GNUNET_break (GNUNET_OK == GNUNET_STATISTICS_watch_cancel 169 GNUNET_break(GNUNET_OK == GNUNET_STATISTICS_watch_cancel
168 (p_ctx->statistics, "topology", "# peers connected", 170 (p_ctx->statistics, "topology", "# peers connected",
169 statistics_iterator, p_ctx)); 171 statistics_iterator, p_ctx));
170 172
171 GNUNET_STATISTICS_destroy (p_ctx->statistics, GNUNET_NO); 173 GNUNET_STATISTICS_destroy(p_ctx->statistics, GNUNET_NO);
172 p_ctx->statistics = NULL; 174 p_ctx->statistics = NULL;
173 175
174 GNUNET_free (p_ctx); 176 GNUNET_free(p_ctx);
175} 177}
176 178
177 179
178static void 180static void
179service_connect_complete (void *cls, 181service_connect_complete(void *cls,
180 struct GNUNET_TESTBED_Operation *op, 182 struct GNUNET_TESTBED_Operation *op,
181 void *ca_result, 183 void *ca_result,
182 const char *emsg) 184 const char *emsg)
183{ 185{
184 int ret; 186 int ret;
185 struct peerctx *p_ctx = (struct peerctx*) cls; 187 struct peerctx *p_ctx = (struct peerctx*)cls;
186 188
187 if (NULL == ca_result) 189 if (NULL == ca_result)
188 GNUNET_SCHEDULER_shutdown(); 190 GNUNET_SCHEDULER_shutdown();
189 191
190 p_ctx->statistics = ca_result; 192 p_ctx->statistics = ca_result;
191 193
192 ret = GNUNET_STATISTICS_watch (ca_result, 194 ret = GNUNET_STATISTICS_watch(ca_result,
193 "topology", 195 "topology",
194 "# peers connected", 196 "# peers connected",
195 statistics_iterator, 197 statistics_iterator,
196 p_ctx); 198 p_ctx);
197 199
198 if (GNUNET_NO == ret) 200 if (GNUNET_NO == ret)
199 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 201 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
200 "call to GNUNET_STATISTICS_watch() failed\n"); 202 "call to GNUNET_STATISTICS_watch() failed\n");
201} 203}
202 204
203static void 205static void
204notify_connect_complete (void *cls, 206notify_connect_complete(void *cls,
205 struct GNUNET_TESTBED_Operation *op, 207 struct GNUNET_TESTBED_Operation *op,
206 const char *emsg) 208 const char *emsg)
207{ 209{
208 GNUNET_TESTBED_operation_done (op); 210 GNUNET_TESTBED_operation_done(op);
209 if (NULL != emsg) 211 if (NULL != emsg)
210 { 212 {
211 fprintf (stderr, "Failed to connect two peers: %s\n", emsg); 213 fprintf(stderr, "Failed to connect two peers: %s\n", emsg);
212 result = GNUNET_SYSERR; 214 result = GNUNET_SYSERR;
213 GNUNET_SCHEDULER_shutdown (); 215 GNUNET_SCHEDULER_shutdown();
214 return; 216 return;
215 } 217 }
216 connect_left--; 218 connect_left--;
217} 219}
218 220
219static void 221static void
220do_connect (void *cls, 222do_connect(void *cls,
221 struct GNUNET_TESTBED_RunHandle *h, 223 struct GNUNET_TESTBED_RunHandle *h,
222 unsigned int num_peers, 224 unsigned int num_peers,
223 struct GNUNET_TESTBED_Peer **peers, 225 struct GNUNET_TESTBED_Peer **peers,
224 unsigned int links_succeeded, 226 unsigned int links_succeeded,
225 unsigned int links_failed) 227 unsigned int links_failed)
226{ 228{
227 unsigned int i; 229 unsigned int i;
228 struct peerctx *p_ctx; 230 struct peerctx *p_ctx;
229 231
230 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 232 GNUNET_log(GNUNET_ERROR_TYPE_INFO,
231 "Threshold is set to %d.\n", 233 "Threshold is set to %d.\n",
232 THRESHOLD); 234 THRESHOLD);
233 235
234 GNUNET_assert (NUM_PEERS == num_peers); 236 GNUNET_assert(NUM_PEERS == num_peers);
235 237
236 for (i=0;i<NUM_PEERS;i++) 238 for (i = 0; i < NUM_PEERS; i++)
237 { 239 {
238 p_ctx = GNUNET_new (struct peerctx); 240 p_ctx = GNUNET_new(struct peerctx);
239 p_ctx->index = i; 241 p_ctx->index = i;
240 p_ctx->connections = 0; 242 p_ctx->connections = 0;
241 p_ctx->reported = GNUNET_NO; 243 p_ctx->reported = GNUNET_NO;
242 244
243 if (i<NUM_PEERS-1) { 245 if (i < NUM_PEERS - 1)
244 connect_left++; 246 {
245 GNUNET_TESTBED_overlay_connect (NULL, 247 connect_left++;
246 &notify_connect_complete, NULL, 248 GNUNET_TESTBED_overlay_connect(NULL,
247 peers[i], peers[i+1]); 249 &notify_connect_complete, NULL,
248 } 250 peers[i], peers[i + 1]);
251 }
249 252
250 op[i] = 253 op[i] =
251 GNUNET_TESTBED_service_connect (cls, 254 GNUNET_TESTBED_service_connect(cls,
252 peers[i], 255 peers[i],
253 "statistics", 256 "statistics",
254 service_connect_complete, 257 service_connect_complete,
255 p_ctx, /* cls of completion cb */ 258 p_ctx, /* cls of completion cb */
256 ca_statistics, /* connect adapter */ 259 ca_statistics, /* connect adapter */
257 da_statistics, /* disconnect adapter */ 260 da_statistics, /* disconnect adapter */
258 p_ctx); 261 p_ctx);
259
260 } 262 }
261 263
262 GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); 264 GNUNET_SCHEDULER_add_shutdown(&shutdown_task, NULL);
263 timeout_tid = 265 timeout_tid =
264 GNUNET_SCHEDULER_add_delayed (TIMEOUT, 266 GNUNET_SCHEDULER_add_delayed(TIMEOUT,
265 &timeout_task, 267 &timeout_task,
266 NULL); 268 NULL);
267} 269}
268 270
269 271
270int 272int
271main (int argc, char *argv[]) 273main(int argc, char *argv[])
272{ 274{
273 result = GNUNET_SYSERR; 275 result = GNUNET_SYSERR;
274 checked_peers = 0; 276 checked_peers = 0;
275 277
276 (void) GNUNET_TESTBED_test_run ("test-gnunet-daemon-topology", 278 (void)GNUNET_TESTBED_test_run("test-gnunet-daemon-topology",
277 "test_gnunet_daemon_topology_data.conf", 279 "test_gnunet_daemon_topology_data.conf",
278 NUM_PEERS, 280 NUM_PEERS,
279 0, NULL, NULL, 281 0, NULL, NULL,
280 &do_connect, NULL); 282 &do_connect, NULL);
281 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-topology"); 283 GNUNET_DISK_directory_remove("/tmp/test-gnunet-topology");
282 284
283 return (GNUNET_OK != result) ? 1 : 0; 285 return (GNUNET_OK != result) ? 1 : 0;
284} 286}