ee6f2877053246b9528603d0c0dfa6e0bb25a005
[oweals/gnunet.git] / src / ats / gnunet-service-ats_addresses.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 ats/gnunet-service-ats_addresses.c
23  * @brief ats service address management
24  * @author Matthias Wachs
25  * @author Christian Grothoff
26  */
27 #include "platform.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet-service-ats.h"
30 #include "gnunet-service-ats_addresses.h"
31 #include "gnunet-service-ats_performance.h"
32 #include "gnunet-service-ats_scheduling.h"
33 #include "gnunet-service-ats_reservations.h"
34 #if HAVE_LIBGLPK
35 #include "gnunet-service-ats_addresses_mlp.h"
36 #endif
37 #include "gnunet-service-ats_addresses_simplistic.h"
38
39 /**
40  * ATS addresses : ATS address management
41  *
42  * - General
43  *
44  * This ATS addresses ("addresses") component manages the addresses known to ATS
45  * service and suggests addresses to transport service when it is interested in
46  * address suggestion for a peer. ATS addresses also instantiates the bandwidth assignment
47  * mechanism (solver), notifies it about changes to addresses and forwards changes
48  * to bandwidth assignments to transport, depending if transport is interested
49  * in this change.
50  *
51  * - Initialization
52  * During initialization a hashmap to store addresses is created. The most
53  * important step is to load the configured solver using configuration
54  * "[ats]:MODE". Current solvers are MODE_SIMPLISTIC, MODE_MLP. Interaction
55  * is done using a solver API
56  *
57  * - Solver API
58  *
59  * Solver functions:
60  * s_init: init the solver with required information
61  * s_add: add a new address
62  * s_update: update ATS values or session for an address
63  * s_get: get prefered address for a peer
64  * s_del: delete an address
65  * s_pref: change preference value for a peer
66  * s_done: shutdown solver
67  * Callbacks:
68  * addresses provides a bandwidth_changed_cb callback to the solver which is
69  * called when bandwidth assigned to peer has changed
70  *
71  * - Shutdown
72  * During shutdown all addresses are freed and the solver told to shutdown
73  *
74  * - Address management:
75  * Transport service notifies ATS about changes to the addresses known to him.
76  *
77  * -- Addresses and sessions
78  * Addresses consist of the address itself and a numerical session.
79  * When a new addresswithout a session is added it has no session, so it gets
80  * session 0 assigned. When an address with a session is added and an address
81  * object with session 0 is found, this object is updated with the
82  * session otherwise a new address object with this session assigned is created.
83  *
84  * Terminology:
85  * Addresses a1,a2 with session s1, s2 are "exact" if:
86  *  (a1 == a2) && (s1 == s2)
87  * Addresses a1,a2 with session s1, s2 are "equivalent" if:
88  *  (a1 == a2) && ((s1 == s2) || (s1 == 0) || (s2 ==0)
89  *
90  * -- Adding an address:
91
92  * When transport learns a new address it tells ATS and ATS is telling addresses
93  * about it using GAS_address_add. If not known to addresses it creates a new
94  * address object and calls solver's s_add. ATS information are deserialized
95  * and solver is notified about the session and ATS information using s_update.
96  *
97  * -- Updating an address
98  * Addresses does an lookup up for the existing address with the given session.
99  * If disassembles included ATS information and notifies the solver using
100  * s_update about the update.
101  *
102  * -- Deleting an address
103  * Addresses does an lookup for the exact address and session and if removes
104  * this address. If session != 0 the session is set to 0 and the address is
105  * kept. If session == 0, the addresses is removed.
106  *
107  * -- Requesting an address suggestion
108  * FIXME
109  *
110  * - Address suggestions:
111  *
112  * - Bandwidth assignment
113  *
114  * The addresses are used to perform resource allocation operations. ATS
115  * addresses takes care of instantiating the solver configured and notifies the
116  * respective solver about address changes and receives changes to the bandwidth
117  * assignment from the solver. The current bandwidth assignment is sent to
118  * transport.
119  *
120  *
121  *
122  * Address lifecycle:
123  *
124  * Adding addresses:
125  *
126  * - If you add a new address without a session, a new address will be added
127  * - If you add this address again now with a session a, the existing address
128  *   will be updated with this session
129  * - If you add this address again now with a session b, a new address object
130  *   with this session will be added
131
132  * Destroying addresses:
133  *
134  * - If you destroy an address without a session, the address itself and all
135  *   address instances with an session will be removed
136  * - If you destroy an address with a session, the session for this address
137  *   will be removed
138  *
139  * Conclusion
140  * Addresses without a session will be updated with a new session and if the
141  * the session is destroyed the session is removed and address itself still
142  * exists for suggestion
143  *
144  */
145
146
147 /**
148  * Available ressource assignment modes
149  */
150 enum ATS_Mode
151 {
152   /*
153    * Simplistic mode:
154    *
155    * Assign each peer an equal amount of bandwidth (bw)
156    *
157    * bw_per_peer = bw_total / #active addresses
158    */
159   MODE_SIMPLISTIC,
160
161   /*
162    * MLP mode:
163    *
164    * Solve ressource assignment as an optimization problem
165    * Uses an mixed integer programming solver
166    */
167   MODE_MLP
168 };
169
170 /**
171  * Pending Address suggestion requests
172  */
173 struct GAS_Addresses_Suggestion_Requests
174 {
175   /**
176    * Next in DLL
177    */
178   struct GAS_Addresses_Suggestion_Requests *next;
179
180   /**
181    * Previous in DLL
182    */
183   struct GAS_Addresses_Suggestion_Requests *prev;
184
185   /**
186    * Peer ID
187    */
188   struct GNUNET_PeerIdentity id;
189 };
190
191 /**
192  * Handle for ATS address component
193  */
194 struct GAS_Addresses_Handle
195 {
196   /**
197    *
198    */
199   struct GNUNET_STATISTICS_Handle *stat;
200
201   /**
202    * A multihashmap to store all addresses
203    */
204   struct GNUNET_CONTAINER_MultiHashMap *addresses;
205
206   /**
207    * Configure WAN quota in
208    */
209   unsigned long long wan_quota_in;
210
211   /**
212    * Configure WAN quota out
213    */
214   unsigned long long wan_quota_out;
215
216   /**
217    * Is ATS addresses running
218    */
219   int running;
220
221   /**
222    * Configured ATS solver
223    */
224   int ats_mode;
225
226   /**
227    *  Solver handle
228    */
229   void *solver;
230
231   /**
232    * Address suggestion requests DLL head
233    */
234   struct GAS_Addresses_Suggestion_Requests *r_head;
235
236   /**
237    * Address suggestion requests DLL tail
238    */
239   struct GAS_Addresses_Suggestion_Requests *r_tail;
240
241   /* Solver functions */
242
243   /**
244    * Initialize solver
245    */
246   GAS_solver_init s_init;
247
248   /**
249    * Add an address to the solver
250    */
251   GAS_solver_address_add s_add;
252
253   /**
254    * Update address in solver
255    */
256   GAS_solver_address_update s_update;
257
258   /**
259    * Get address from solver
260    */
261   GAS_solver_get_preferred_address s_get;
262
263   /**
264    * Delete address in solver
265    */
266   GAS_solver_address_delete s_del;
267
268   /**
269    * Change preference for quality in solver
270    */
271   GAS_solver_address_change_preference s_pref;
272
273   /**
274    * Shutdown solver
275    */
276   GAS_solver_done s_done;
277 };
278
279
280 static unsigned int
281 assemble_ats_information (const struct ATS_Address *aa,  struct GNUNET_ATS_Information **dest)
282 {
283   unsigned int ats_count = GNUNET_ATS_PropertyCount - 1;
284   struct GNUNET_ATS_Information *ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
285   (*dest) = ats;
286
287   ats[0].type = ntohl(GNUNET_ATS_UTILIZATION_UP);
288   ats[0].value = aa->atsp_utilization_out.value__;
289   ats[1].type = ntohl(GNUNET_ATS_UTILIZATION_DOWN);
290   ats[1].value = aa->atsp_utilization_in.value__;
291   ats[2].type = ntohl(GNUNET_ATS_NETWORK_TYPE);
292   ats[2].value = ntohl(aa->atsp_network_type);
293   ats[3].type = ntohl(GNUNET_ATS_QUALITY_NET_DELAY);
294   ats[3].value = ntohl(aa->atsp_latency.rel_value);
295   ats[4].type = ntohl(GNUNET_ATS_QUALITY_NET_DISTANCE);
296   ats[4].value = ntohl(aa->atsp_distance);
297   ats[5].type = ntohl(GNUNET_ATS_COST_WAN);
298   ats[5].value = ntohl (aa->atsp_cost_wan);
299   ats[6].type = ntohl(GNUNET_ATS_COST_LAN);
300   ats[6].value = ntohl (aa->atsp_cost_lan);
301   ats[7].type = ntohl(GNUNET_ATS_COST_WLAN);
302   ats[7].value = ntohl (aa->atsp_cost_wlan);
303   return ats_count;
304 }
305
306 static unsigned int
307 disassemble_ats_information (const struct GNUNET_ATS_Information *src,
308                              uint32_t ats_count,
309                              struct ATS_Address *dest)
310 {
311   int i;
312   int res = 0;
313   for (i = 0; i < ats_count; i++)
314     switch (ntohl (src[i].type))
315     {
316     case GNUNET_ATS_UTILIZATION_UP:
317       dest->atsp_utilization_out.value__ = src[i].value;
318       res ++;
319       break;
320     case GNUNET_ATS_UTILIZATION_DOWN:
321       dest->atsp_utilization_in.value__ = src[i].value;
322       res ++;
323       break;
324     case GNUNET_ATS_QUALITY_NET_DELAY:
325       dest->atsp_latency.rel_value = ntohl (src[i].value);
326       res ++;
327       break;
328     case GNUNET_ATS_QUALITY_NET_DISTANCE:
329       dest->atsp_distance = ntohl (src[i].value);
330       res ++;
331       break;
332     case GNUNET_ATS_COST_WAN:
333       dest->atsp_cost_wan = ntohl (src[i].value);
334       res ++;
335       break;
336     case GNUNET_ATS_COST_LAN:
337       dest->atsp_cost_lan = ntohl (src[i].value);
338       res ++;
339       break;
340     case GNUNET_ATS_COST_WLAN:
341       dest->atsp_cost_wlan = ntohl (src[i].value);
342       res ++;
343       break;
344     case GNUNET_ATS_NETWORK_TYPE:
345       dest->atsp_network_type = ntohl (src[i].value);
346       res ++;
347       break;
348     case GNUNET_ATS_ARRAY_TERMINATOR:
349       break;
350     default:
351       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
352                   "Received unsupported ATS type %u\n", ntohl (src[i].type));
353       GNUNET_break (0);
354       break;
355     }
356   return res;
357 }
358
359 /**
360  * Free the given address
361  * @param addr address to destroy
362  */
363 static void
364 free_address (struct ATS_Address *addr)
365 {
366   GNUNET_free (addr->plugin);
367   GNUNET_free (addr);
368 }
369
370 /**
371  * Create a ATS_address with the given information
372  * @param peer peer
373  * @param plugin_name plugin
374  * @param plugin_addr address
375  * @param plugin_addr_len address length
376  * @param session_id session
377  * @return the ATS_Address
378  */
379 static struct ATS_Address *
380 create_address (const struct GNUNET_PeerIdentity *peer,
381                 const char *plugin_name,
382                 const void *plugin_addr, size_t plugin_addr_len,
383                 uint32_t session_id)
384 {
385   struct ATS_Address *aa = NULL;
386
387   aa = GNUNET_malloc (sizeof (struct ATS_Address) + plugin_addr_len);
388   aa->peer = *peer;
389   aa->addr_len = plugin_addr_len;
390   aa->addr = &aa[1];
391   memcpy (&aa[1], plugin_addr, plugin_addr_len);
392   aa->plugin = GNUNET_strdup (plugin_name);
393   aa->session_id = session_id;
394   aa->active = GNUNET_NO;
395   aa->used = GNUNET_NO;
396   aa->solver_information = NULL;
397   aa->assigned_bw_in = GNUNET_BANDWIDTH_value_init(0);
398   aa->assigned_bw_out = GNUNET_BANDWIDTH_value_init(0);
399   return aa;
400 }
401
402
403 /**
404  * Destroy the given address.
405  *
406  * @param handle the address handle
407  * @param addr address to destroy
408  * @return GNUNET_YES if bandwidth allocations should be recalcualted
409  */
410 static int
411 destroy_address (struct GAS_Addresses_Handle *handle, struct ATS_Address *addr)
412 {
413   int ret;
414
415   ret = GNUNET_NO;
416   GNUNET_assert (GNUNET_YES ==
417                  GNUNET_CONTAINER_multihashmap_remove (handle->addresses,
418                                                        &addr->peer.hashPubKey,
419                                                        addr));
420   free_address (addr);
421   return ret;
422 }
423
424
425 struct CompareAddressContext
426 {
427   const struct ATS_Address *search;
428
429   /* exact_address != NULL if address and session is equal */
430   struct ATS_Address *exact_address;
431   /* exact_address != NULL if address and session is 0 */
432   struct ATS_Address *base_address;
433 };
434
435
436 static int
437 compare_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
438 {
439   struct CompareAddressContext *cac = cls;
440   struct ATS_Address *aa = value;
441
442   /* Find an matching exact address:
443    *
444    * Compare by:
445    * aa->addr_len == cac->search->addr_len
446    * aa->plugin == cac->search->plugin
447    * aa->addr == cac->search->addr
448    * aa->session == cac->search->session
449    *
450    * return as exact address
451    */
452   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
453   {
454       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == cac->search->session_id))
455         cac->exact_address = aa;
456   }
457
458   /* Find an matching base address:
459    *
460    * Properties:
461    *
462    * aa->session_id == 0
463    *
464    * Compare by:
465    * aa->addr_len == cac->search->addr_len
466    * aa->plugin == cac->search->plugin
467    * aa->addr == cac->search->addr
468    *
469    * return as base address
470    */
471   if ((aa->addr_len == cac->search->addr_len) && (0 == strcmp (aa->plugin, cac->search->plugin)))
472   {
473       if ((0 == memcmp (aa->addr, cac->search->addr, aa->addr_len)) && (aa->session_id == 0))
474         cac->base_address = aa;
475   }
476
477   /* Find an matching exact address based on session:
478    *
479    * Properties:
480    *
481    * cac->search->addr_len == 0
482    *
483    * Compare by:
484    * aa->plugin == cac->search->plugin
485    * aa->session_id == cac->search->session_id
486    *
487    * return as exact address
488    */
489   if (0 == cac->search->addr_len)
490   {
491       if ((0 == strcmp (aa->plugin, cac->search->plugin)) && (aa->session_id == cac->search->session_id))
492         cac->exact_address = aa;
493   }
494
495   if (cac->exact_address == NULL)
496     return GNUNET_YES; /* Continue iteration to find exact address */
497   else
498     return GNUNET_NO; /* Stop iteration since we have an exact address */
499 }
500
501
502 /**
503  * Find an existing equivalent address record.
504  * Compares by peer identity and network address OR by session ID
505  * (one of the two must match).
506  *
507  * @param handle the address handle
508  * @param peer peer to lookup addresses for
509  * @param addr existing address record
510  * @return existing address record, NULL for none
511  */
512 struct ATS_Address *
513 find_equivalent_address (struct GAS_Addresses_Handle *handle,
514                          const struct GNUNET_PeerIdentity *peer,
515                          const struct ATS_Address *addr)
516 {
517   struct CompareAddressContext cac;
518
519   cac.exact_address = NULL;
520   cac.base_address = NULL;
521   cac.search = addr;
522   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
523                                               &compare_address_it, &cac);
524
525   if (cac.exact_address == NULL)
526     return cac.base_address;
527   return cac.exact_address;
528 }
529
530
531 static struct ATS_Address *
532 lookup_address (struct GAS_Addresses_Handle *handle,
533                 const struct GNUNET_PeerIdentity *peer,
534                 const char *plugin_name,
535                 const void *plugin_addr,
536                 size_t plugin_addr_len,
537                 uint32_t session_id,
538                 const struct GNUNET_ATS_Information *atsi,
539                 uint32_t atsi_count)
540 {
541   struct ATS_Address *aa;
542   struct ATS_Address *ea;
543
544   aa = create_address (peer,
545                        plugin_name,
546                        plugin_addr, plugin_addr_len,
547                        session_id);
548
549   /* Get existing address or address with session == 0 */
550   ea = find_equivalent_address (handle, peer, aa);
551   free_address (aa);
552   if (ea == NULL)
553   {
554     return NULL;
555   }
556   else if (ea->session_id != session_id)
557   {
558     return NULL;
559   }
560   return ea;
561 }
562
563
564 void
565 GAS_addresses_add (struct GAS_Addresses_Handle *handle,
566                    const struct GNUNET_PeerIdentity *peer,
567                    const char *plugin_name, const void *plugin_addr,
568                    size_t plugin_addr_len, uint32_t session_id,
569                    const struct GNUNET_ATS_Information *atsi,
570                    uint32_t atsi_count)
571 {
572   struct ATS_Address *aa;
573   struct ATS_Address *ea;
574   unsigned int ats_res;
575
576   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
577               "Received `%s' for peer `%s'\n",
578               "ADDRESS ADD",
579               GNUNET_i2s (peer));
580
581   if (GNUNET_NO == handle->running)
582     return;
583
584   GNUNET_assert (NULL != handle->addresses);
585
586   aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len,
587                        session_id);
588   if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, aa)))
589   {
590       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591                 "While adding address: had %u ATS elements to add, could only add %u\n",
592                 atsi_count, ats_res);
593   }
594
595   /* Get existing address or address with session == 0 */
596   ea = find_equivalent_address (handle, peer, aa);
597   if (ea == NULL)
598   {
599     /* We have a new address */
600     GNUNET_assert (GNUNET_OK ==
601                    GNUNET_CONTAINER_multihashmap_put (handle->addresses,
602                                                       &peer->hashPubKey, aa,
603                                                       GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE));
604     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Added new address for peer `%s' session id %u, %p\n",
605                 GNUNET_i2s (peer), session_id, aa);
606     /* Tell solver about new address */
607     handle->s_add (handle->solver, handle->addresses, aa);
608     return;
609   }
610   GNUNET_free (aa->plugin);
611   GNUNET_free (aa);
612
613   if (ea->session_id != 0)
614   {
615       /* This address with the same session is already existing
616        * Should not happen */
617       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
618                 "Added already existing address for peer `%s' `%s' %p with new session %u\n",
619                 GNUNET_i2s (peer), plugin_name, session_id);
620       GNUNET_break (0);
621       return;
622   }
623
624   /* We have an address without an session, update this address */
625
626   /* Notify solver about update with atsi information and session */
627   handle->s_update (handle->solver, handle->addresses, ea, session_id, ea->used, atsi, atsi_count);
628
629   /* Do the update */
630   ea->session_id = session_id;
631   if (atsi_count != (ats_res = disassemble_ats_information(atsi, atsi_count, ea)))
632   {
633       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634                   "While updating address: had %u ATS elements to add, could only add %u\n",
635                   atsi_count, ats_res);
636   }
637   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
638            "Updated existing address for peer `%s' %p with new session %u\n",
639            GNUNET_i2s (peer), ea, session_id);
640 }
641
642
643 void
644 GAS_addresses_update (struct GAS_Addresses_Handle *handle,
645                       const struct GNUNET_PeerIdentity *peer,
646                       const char *plugin_name, const void *plugin_addr,
647                       size_t plugin_addr_len, uint32_t session_id,
648                       const struct GNUNET_ATS_Information *atsi,
649                       uint32_t atsi_count)
650 {
651   struct ATS_Address *aa;
652   uint32_t ats_res;
653
654   if (GNUNET_NO == handle->running)
655     return;
656
657   GNUNET_assert (NULL != handle->addresses);
658
659   /* Get existing address */
660   aa = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
661                        session_id, atsi, atsi_count);
662   if (aa == NULL)
663   {
664     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Tried to update unknown address for peer `%s' `%s' session id %u\n",
665                 GNUNET_i2s (peer), plugin_name, session_id);
666     GNUNET_break (0);
667     return;
668   }
669
670   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671                 "Received `%s' for peer `%s' address \n",
672                 "ADDRESS UPDATE",
673                 GNUNET_i2s (peer), aa);
674
675   /* Tell solver about update */
676   handle->s_update (handle->solver, handle->addresses, aa, session_id, aa->used, atsi, atsi_count);
677
678   /* Update address */
679   if (atsi_count != (ats_res = disassemble_ats_information (atsi, atsi_count, aa)))
680   {
681       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
682                 "While adding address: had %u ATS elements to add, could only add %u\n",
683                 atsi_count, ats_res);
684   }
685   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
686             "Updated %u ATS elements for address %p\n",
687             ats_res, aa);
688 }
689
690
691 struct DestroyContext
692 {
693   struct ATS_Address *aa;
694
695   struct GAS_Addresses_Handle *handle;
696
697   /**
698    * GNUNET_NO  : full address
699    * GNUNET_YES : just session
700    */
701   int result;
702 };
703
704
705 /**
706  * Delete an address
707  *
708  * If session != 0, just the session is deleted, the address itself still exists
709  * If session == 0, remove full address
710  * If session == 0 and addrlen == 0, destroy inbound address
711  *
712  * @param cls unused
713  * @param key unused
714  * @param value the 'struct ATS_Address'
715  * @return GNUNET_OK (continue to iterate)
716  */
717 static int
718 destroy_by_session_id (void *cls, const struct GNUNET_HashCode * key, void *value)
719 {
720   struct DestroyContext *dc = cls;
721   struct GAS_Addresses_Handle *handle = dc->handle;
722   const struct ATS_Address *des = dc->aa;
723   struct ATS_Address *aa = value;
724
725   GNUNET_assert (0 == memcmp (&aa->peer, &des->peer,
726                               sizeof (struct GNUNET_PeerIdentity)));
727
728
729   if (des->session_id == 0)
730   {
731     /* Session == 0, remove full address  */
732     if ((0 == strcmp (des->plugin, aa->plugin)) &&
733         (aa->addr_len == des->addr_len) &&
734         (0 == memcmp (des->addr, aa->addr, aa->addr_len)))
735     {
736
737       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738                   "Deleting full address for peer `%s' session %u %p\n",
739                   GNUNET_i2s (&aa->peer), aa->session_id, aa);
740
741       /* Notify solver about deletion */
742       handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
743       destroy_address (handle, aa);
744       dc->result = GNUNET_NO;
745       return GNUNET_OK; /* Continue iteration */
746     }
747   }
748   else
749   {
750     /* Session != 0, just remove session */
751     if (aa->session_id != des->session_id)
752       return GNUNET_OK; /* irrelevant */
753
754     if ((aa->session_id != 0) &&
755         (0 != strcmp (des->plugin, aa->plugin)))
756     {
757         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
758                     "Different plugins during removal: `%s' vs `%s' \n",
759                     des->plugin, aa->plugin);
760         GNUNET_break (0);
761         return GNUNET_OK;
762     }
763
764     if (aa->addr_len == 0)
765     {
766         /* Inbound connection died, delete full address */
767         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
768                     "Deleting inbound address for peer `%s': `%s' session %u\n",
769                     GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
770
771         /* Notify solver about deletion */
772         handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
773         destroy_address (handle, aa);
774         dc->result = GNUNET_NO;
775         return GNUNET_OK; /* Continue iteration */
776     }
777     else
778     {
779         /* Session died */
780         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781                     "Deleting session for peer `%s': `%s' %u\n",
782                     GNUNET_i2s (&aa->peer), aa->plugin, aa->session_id);
783         /* Notify solver to delete session */
784         handle->s_del (handle->solver, handle->addresses, aa, GNUNET_YES);
785         aa->session_id = 0;
786         return GNUNET_OK;
787     }
788   }
789   return GNUNET_OK;
790 }
791
792 void
793 GAS_addresses_destroy (struct GAS_Addresses_Handle *handle,
794                        const struct GNUNET_PeerIdentity *peer,
795                        const char *plugin_name, const void *plugin_addr,
796                        size_t plugin_addr_len, uint32_t session_id)
797 {
798   struct ATS_Address *ea;
799   struct DestroyContext dc;
800
801   if (GNUNET_NO == handle->running)
802     return;
803
804   /* Get existing address */
805   ea = lookup_address (handle, peer, plugin_name, plugin_addr, plugin_addr_len,
806                        session_id, NULL, 0);
807
808   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809               "Received `%s' for peer `%s' address %p session %u\n",
810               "ADDRESS DESTROY",
811               GNUNET_i2s (peer), ea, session_id);
812
813   if (ea == NULL)
814   {
815     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Tried to destroy unknown address for peer `%s' `%s' session id %u\n",
816                 GNUNET_i2s (peer), plugin_name, session_id);
817     return;
818   }
819
820   GNUNET_break (0 < strlen (plugin_name));
821   dc.handle = handle;
822   dc.aa = create_address (peer, plugin_name, plugin_addr, plugin_addr_len, session_id);
823
824   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey,
825                                               &destroy_by_session_id, &dc);
826   free_address (dc.aa);
827 }
828
829
830 int
831 GAS_addresses_in_use (struct GAS_Addresses_Handle *handle,
832                       const struct GNUNET_PeerIdentity *peer,
833                       const char *plugin_name, const void *plugin_addr,
834                       size_t plugin_addr_len, uint32_t session_id, int in_use)
835 {
836   struct ATS_Address *ea;
837
838   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
839                 "Received `%s' for peer `%s'\n",
840                 "ADDRESS IN USE",
841                 GNUNET_i2s (peer));
842
843   if (GNUNET_NO == handle->running)
844     return GNUNET_SYSERR;
845
846   ea = lookup_address (handle, peer, plugin_name,
847                         plugin_addr, plugin_addr_len,
848                         session_id, NULL, 0);
849   if (NULL == ea)
850   {
851     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
852                 "Trying to set unknown address `%s', %s %u %s \n",
853                 GNUNET_i2s (peer),
854                 plugin_name, session_id,
855                 (GNUNET_NO == in_use) ? "NO" : "YES");
856     GNUNET_break (0);
857     return GNUNET_SYSERR;
858   }
859   if (ea->used == in_use)
860   {
861     GNUNET_break (0);
862     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
863                 "Address in use called multiple times for peer `%s': %s -> %s \n",
864                 GNUNET_i2s (peer),
865                 (GNUNET_NO == ea->used) ? "NO" : "YES",
866                 (GNUNET_NO == in_use) ? "NO" : "YES");
867     return GNUNET_SYSERR;
868   }
869
870   /* Tell solver about update */
871   handle->s_update (handle->solver, handle->addresses, ea, session_id, in_use, NULL, 0);
872   ea->used = in_use;
873
874   return GNUNET_OK;
875 }
876
877
878 /**
879  * Cancel address suggestions for a peer
880  *
881  * @param handle the address handle
882  * @param peer the respective peer
883  */
884 void
885 GAS_addresses_request_address_cancel (struct GAS_Addresses_Handle *handle,
886                                       const struct GNUNET_PeerIdentity *peer)
887 {
888   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
889
890   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
891               "Received request: `%s' for peer %s\n", "request_address_cancel", GNUNET_i2s (peer));
892
893   while (NULL != cur)
894   {
895       if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
896         break; /* found */
897       cur = cur->next;
898   }
899
900   if (NULL == cur)
901   {
902       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
903                   "No address requests pending for peer `%s', cannot remove!\n", GNUNET_i2s (peer));
904       return;
905   }
906   GAS_addresses_handle_backoff_reset (handle, peer);
907   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
908               "Removed request pending for peer `%s\n", GNUNET_i2s (peer));
909   GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
910   GNUNET_free (cur);
911 }
912
913
914 /**
915  * Add an address suggestions for a peer
916  *
917  * @param handle the address handle
918  * @param peer the respective peer
919  */
920 void
921 GAS_addresses_request_address (struct GAS_Addresses_Handle *handle,
922                                const struct GNUNET_PeerIdentity *peer)
923 {
924   struct GAS_Addresses_Suggestion_Requests *cur = handle->r_head;
925   struct ATS_Address *aa;
926   struct GNUNET_ATS_Information *ats;
927   unsigned int ats_count;
928
929   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
930               "Received `%s' for peer `%s'\n",
931               "REQUEST ADDRESS",
932               GNUNET_i2s (peer));
933
934   if (GNUNET_NO == handle->running)
935     return;
936   while (NULL != cur)
937   {
938       if (0 == memcmp (peer, &cur->id, sizeof (cur->id)))
939         break; /* already suggesting */
940       cur = cur->next;
941   }
942   if (NULL == cur)
943   {
944       cur = GNUNET_malloc (sizeof (struct GAS_Addresses_Suggestion_Requests));
945       cur->id = (*peer);
946       GNUNET_CONTAINER_DLL_insert (handle->r_head, handle->r_tail, cur);
947   }
948
949   /* Get prefered address from solver */
950   aa = (struct ATS_Address *) handle->s_get (handle->solver, handle->addresses, peer);
951   if (NULL == aa)
952   {
953     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
954                 "Cannot suggest address for peer `%s'\n", GNUNET_i2s (peer));
955     return;
956   }
957
958   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
959               "Suggesting address %p for peer `%s'\n", aa, GNUNET_i2s (peer));
960
961   ats_count = assemble_ats_information (aa, &ats);
962   GAS_scheduling_transmit_address_suggestion (peer,
963                                               aa->plugin,
964                                               aa->addr, aa->addr_len,
965                                               aa->session_id,
966                                               ats, ats_count,
967                                               aa->assigned_bw_out,
968                                               aa->assigned_bw_in);
969
970   aa->block_interval = GNUNET_TIME_relative_add (aa->block_interval, ATS_BLOCKING_DELTA);
971   aa->blocked_until = GNUNET_TIME_absolute_add (GNUNET_TIME_absolute_get(), aa->block_interval);
972
973   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974        "Address %p ready for suggestion, block interval now %llu \n",
975        aa, aa->block_interval);
976
977   GNUNET_free (ats);
978 }
979
980
981 static int
982 reset_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
983 {
984   struct ATS_Address *aa = value;
985
986   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
987               "Resetting interval for peer `%s' address %p from %llu to 0\n",
988               GNUNET_i2s (&aa->peer), aa, aa->block_interval);
989
990   aa->blocked_until = GNUNET_TIME_UNIT_ZERO_ABS;
991   aa->block_interval = GNUNET_TIME_UNIT_ZERO;
992   return GNUNET_OK;
993 }
994
995
996 void
997 GAS_addresses_handle_backoff_reset (struct GAS_Addresses_Handle *handle,
998                                     const struct GNUNET_PeerIdentity *peer)
999 {
1000   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1001               "Received `%s' for peer `%s'\n",
1002               "RESET BACKOFF",
1003               GNUNET_i2s (peer));
1004
1005   GNUNET_break (GNUNET_SYSERR != GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses,
1006                                               &peer->hashPubKey,
1007                                               &reset_address_it,
1008                                               NULL));
1009 }
1010
1011
1012 void
1013 GAS_addresses_change_preference (struct GAS_Addresses_Handle *handle,
1014                                  void *client,
1015                                  const struct GNUNET_PeerIdentity *peer,
1016                                  enum GNUNET_ATS_PreferenceKind kind,
1017                                  float score)
1018 {
1019   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1020               "Received `%s' for peer `%s' for client %p\n",
1021               "CHANGE PREFERENCE",
1022               GNUNET_i2s (peer), client);
1023
1024   if (GNUNET_NO == handle->running)
1025     return;
1026
1027   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains (handle->addresses,
1028                                                           &peer->hashPubKey))
1029   {
1030       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1031                   "Received `%s' for unknown peer `%s' from client %p\n",
1032                   "CHANGE PREFERENCE",
1033                   GNUNET_i2s (peer), client);
1034       return;
1035   }
1036
1037   /* Tell solver about update */
1038   handle->s_pref (handle->solver, client, peer, kind, score);
1039 }
1040
1041
1042 /**
1043  * Load quotas for networks from configuration
1044  *
1045  * @param cfg configuration handle
1046  * @param out_dest where to write outbound quotas
1047  * @param in_dest where to write inbound quotas
1048  * @param dest_length length of inbound and outbound arrays
1049  * @return number of networks loaded
1050  */
1051 static unsigned int
1052 load_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned long long *out_dest, unsigned long long *in_dest, int dest_length)
1053 {
1054   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
1055   char * entry_in = NULL;
1056   char * entry_out = NULL;
1057   char * quota_out_str;
1058   char * quota_in_str;
1059   int c;
1060   int res;
1061
1062   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount) && (c < dest_length); c++)
1063   {
1064     in_dest[c] = 0;
1065     out_dest[c] = 0;
1066     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
1067     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
1068
1069     /* quota out */
1070     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
1071     {
1072       res = GNUNET_NO;
1073       if (0 == strcmp(quota_out_str, BIG_M_STRING))
1074       {
1075         out_dest[c] = GNUNET_ATS_MaxBandwidth;
1076         res = GNUNET_YES;
1077       }
1078       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &out_dest[c])))
1079         res = GNUNET_YES;
1080       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_out,  &out_dest[c])))
1081          res = GNUNET_YES;
1082
1083       if (GNUNET_NO == res)
1084       {
1085           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1086               network_str[c], quota_out_str, GNUNET_ATS_DefaultBandwidth);
1087           out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1088       }
1089       else
1090       {
1091           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Outbound quota configure for network `%s' is %llu\n"),
1092               network_str[c], out_dest[c]);
1093       }
1094       GNUNET_free (quota_out_str);
1095     }
1096     else
1097     {
1098       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configured for network `%s', assigning default bandwidth %llu\n"),
1099           network_str[c], GNUNET_ATS_DefaultBandwidth);
1100       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1101     }
1102
1103     /* quota in */
1104     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
1105     {
1106       res = GNUNET_NO;
1107       if (0 == strcmp(quota_in_str, BIG_M_STRING))
1108       {
1109         in_dest[c] = GNUNET_ATS_MaxBandwidth;
1110         res = GNUNET_YES;
1111       }
1112       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &in_dest[c])))
1113         res = GNUNET_YES;
1114       if ((GNUNET_NO == res) && (GNUNET_OK == GNUNET_CONFIGURATION_get_value_number (cfg, "ats", entry_in,  &in_dest[c])))
1115          res = GNUNET_YES;
1116
1117       if (GNUNET_NO == res)
1118       {
1119           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Could not load quota for network `%s':  `%s', assigning default bandwidth %llu\n"),
1120               network_str[c], quota_in_str, GNUNET_ATS_DefaultBandwidth);
1121           in_dest[c] = GNUNET_ATS_DefaultBandwidth;
1122       }
1123       else
1124       {
1125           GNUNET_log (GNUNET_ERROR_TYPE_INFO, _("Inbound quota configured for network `%s' is %llu\n"),
1126               network_str[c], in_dest[c]);
1127       }
1128       GNUNET_free (quota_in_str);
1129     }
1130     else
1131     {
1132       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, _("No outbound quota configure for network `%s', assigning default bandwidth %llu\n"),
1133           network_str[c], GNUNET_ATS_DefaultBandwidth);
1134       out_dest[c] = GNUNET_ATS_DefaultBandwidth;
1135     }
1136     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Loaded quota for network `%s' (in/out): %llu %llu\n", network_str[c], in_dest[c], out_dest[c]);
1137     GNUNET_free (entry_out);
1138     GNUNET_free (entry_in);
1139   }
1140   return GNUNET_ATS_NetworkTypeCount;
1141 }
1142
1143
1144 static void
1145 bandwidth_changed_cb (void *cls, struct ATS_Address *address)
1146 {
1147   struct GAS_Addresses_Handle *handle = cls;
1148   struct GAS_Addresses_Suggestion_Requests *cur;
1149
1150   GNUNET_assert (handle != NULL);
1151   GNUNET_assert (address != NULL);
1152
1153   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Bandwidth assignment changed for peer %s \n", GNUNET_i2s(&address->peer));
1154   struct GNUNET_ATS_Information *ats;
1155   unsigned int ats_count;
1156
1157   cur = handle->r_head;
1158   while (NULL != cur)
1159   {
1160       if (0 == memcmp (&address->peer, &cur->id, sizeof (cur->id)))
1161         break; /* we have an address request pending*/
1162       cur = cur->next;
1163   }
1164   if (NULL == cur)
1165   {
1166       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167                   "Nobody is interested in peer `%s' :(\n",GNUNET_i2s (&address->peer));
1168       return;
1169   }
1170
1171   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172               "Sending bandwidth update for peer `%s'\n",GNUNET_i2s (&address->peer));
1173
1174   ats_count = assemble_ats_information (address, &ats);
1175   GAS_scheduling_transmit_address_suggestion (&address->peer,
1176                                               address->plugin,
1177                                               address->addr, address->addr_len,
1178                                               address->session_id,
1179                                               ats, ats_count,
1180                                               address->assigned_bw_out,
1181                                               address->assigned_bw_in);
1182   GNUNET_free (ats);
1183 }
1184
1185
1186 /**
1187  * Initialize address subsystem.
1188  *
1189  * @param cfg configuration to use
1190  * @param stats the statistics handle to use
1191  */
1192 struct GAS_Addresses_Handle *
1193 GAS_addresses_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
1194                     const struct GNUNET_STATISTICS_Handle *stats)
1195 {
1196   struct GAS_Addresses_Handle *ah;
1197   int quotas[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkType;
1198   unsigned long long  quotas_in[GNUNET_ATS_NetworkTypeCount];
1199   unsigned long long  quotas_out[GNUNET_ATS_NetworkTypeCount];
1200   int quota_count;
1201   char *mode_str;
1202   int c;
1203
1204   ah = GNUNET_malloc (sizeof (struct GAS_Addresses_Handle));
1205   ah->running = GNUNET_NO;
1206
1207   ah->stat = (struct GNUNET_STATISTICS_Handle *) stats;
1208   /* Initialize the addresses database */
1209   ah->addresses = GNUNET_CONTAINER_multihashmap_create (128, GNUNET_NO);
1210   GNUNET_assert (NULL != ah->addresses);
1211
1212   /* Figure out configured solution method */
1213   if (GNUNET_SYSERR == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", "MODE", &mode_str))
1214   {
1215       GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "No ressource assignment method configured, using simplistic approch\n");
1216       ah->ats_mode = MODE_SIMPLISTIC;
1217   }
1218   else
1219   {
1220       for (c = 0; c < strlen (mode_str); c++)
1221         mode_str[c] = toupper (mode_str[c]);
1222       if (0 == strcmp (mode_str, "SIMPLISTIC"))
1223       {
1224           ah->ats_mode = MODE_SIMPLISTIC;
1225       }
1226       else if (0 == strcmp (mode_str, "MLP"))
1227       {
1228           ah->ats_mode = MODE_MLP;
1229 #if !HAVE_LIBGLPK
1230           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Assignment method `%s' configured, but GLPK is not availabe, please install \n", mode_str);
1231           ah->ats_mode = MODE_SIMPLISTIC;
1232 #endif
1233       }
1234       else
1235       {
1236           GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Invalid ressource assignment method `%s' configured, using simplistic approch\n", mode_str);
1237           ah->ats_mode = MODE_SIMPLISTIC;
1238       }
1239       GNUNET_free (mode_str);
1240   }
1241   /* Start configured solution method */
1242   switch (ah->ats_mode)
1243   {
1244     case MODE_MLP:
1245       /* Init the MLP solver with default values */
1246 #if HAVE_LIBGLPK
1247       ah->ats_mode = MODE_MLP;
1248       ah->s_init = &GAS_mlp_init;
1249       ah->s_add = &GAS_mlp_address_add;
1250       ah->s_update = &GAS_mlp_address_update;
1251       ah->s_get = &GAS_mlp_get_preferred_address;
1252       ah->s_pref = &GAS_mlp_address_change_preference;
1253       ah->s_del =  &GAS_mlp_address_delete;
1254       ah->s_done = &GAS_mlp_done;
1255 #else
1256       GNUNET_free (ah);
1257       return NULL;
1258 #endif
1259       break;
1260     case MODE_SIMPLISTIC:
1261       /* Init the simplistic solver with default values */
1262       ah->ats_mode = MODE_SIMPLISTIC;
1263       ah->s_init = &GAS_simplistic_init;
1264       ah->s_add = &GAS_simplistic_address_add;
1265       ah->s_update = &GAS_simplistic_address_update;
1266       ah->s_get = &GAS_simplistic_get_preferred_address;
1267       ah->s_pref = &GAS_simplistic_address_change_preference;
1268       ah->s_del  = &GAS_simplistic_address_delete;
1269       ah->s_done = &GAS_simplistic_done;
1270       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "ATS started in %s mode\n", "SIMPLISTIC");
1271       break;
1272     default:
1273       return NULL;
1274       break;
1275   }
1276
1277   GNUNET_assert (NULL != ah->s_init);
1278   GNUNET_assert (NULL != ah->s_add);
1279   GNUNET_assert (NULL != ah->s_update);
1280   GNUNET_assert (NULL != ah->s_get);
1281   GNUNET_assert (NULL != ah->s_pref);
1282   GNUNET_assert (NULL != ah->s_del);
1283   GNUNET_assert (NULL != ah->s_done);
1284
1285   quota_count = load_quotas(cfg, quotas_in, quotas_out, GNUNET_ATS_NetworkTypeCount);
1286
1287   ah->solver = ah->s_init (cfg, stats, quotas, quotas_in, quotas_out, quota_count, &bandwidth_changed_cb, ah);
1288   if (NULL == ah->solver)
1289   {
1290     GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Failed to initialize solver!\n");
1291     GNUNET_free (ah);
1292     return NULL;
1293   }
1294
1295   /* up and running */
1296   ah->running = GNUNET_YES;
1297   return ah;
1298 }
1299
1300
1301 /**
1302  * Free memory of address.
1303  *
1304  * @param cls NULL
1305  * @param key peer identity (unused)
1306  * @param value the 'struct ATS_Address' to free
1307  * @return GNUNET_OK (continue to iterate)
1308  */
1309 static int
1310 free_address_it (void *cls, const struct GNUNET_HashCode * key, void *value)
1311 {
1312   struct GAS_Addresses_Handle *handle = cls;
1313   struct ATS_Address *aa = value;
1314   handle->s_del (handle->solver, handle->addresses, aa, GNUNET_NO);
1315   destroy_address (handle, aa);
1316   return GNUNET_OK;
1317 }
1318
1319
1320 void
1321 GAS_addresses_destroy_all (struct GAS_Addresses_Handle *handle)
1322 {
1323   if (GNUNET_NO == handle->running)
1324     return;
1325
1326   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327               "Received `%s'\n",
1328               "DESTROY ALL");
1329
1330   if (handle->addresses != NULL)
1331     GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &free_address_it, handle);
1332 }
1333
1334
1335 /**
1336  * Shutdown address subsystem.
1337  */
1338 void
1339 GAS_addresses_done (struct GAS_Addresses_Handle *handle)
1340 {
1341   struct GAS_Addresses_Suggestion_Requests *cur;
1342
1343   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1344               "Shutting down addresses\n");
1345   GNUNET_assert (NULL != handle);
1346   GAS_addresses_destroy_all (handle);
1347   handle->running = GNUNET_NO;
1348   GNUNET_CONTAINER_multihashmap_destroy (handle->addresses);
1349   handle->addresses = NULL;
1350   while (NULL != (cur = handle->r_head))
1351   {
1352       GNUNET_CONTAINER_DLL_remove (handle->r_head, handle->r_tail, cur);
1353       GNUNET_free (cur);
1354   }
1355   handle->s_done (handle->solver);
1356   GNUNET_free (handle);
1357   /* Stop configured solution method */
1358
1359 }
1360
1361 struct PeerIteratorContext
1362 {
1363   GNUNET_ATS_Peer_Iterator it;
1364   void *it_cls;
1365   struct GNUNET_CONTAINER_MultiHashMap *peers_returned;
1366 };
1367
1368 static int
1369 peer_it (void *cls,
1370          const struct GNUNET_HashCode * key,
1371          void *value)
1372 {
1373   struct PeerIteratorContext *ip_ctx = cls;
1374   struct GNUNET_PeerIdentity tmp;
1375
1376   if (GNUNET_NO == GNUNET_CONTAINER_multihashmap_contains(ip_ctx->peers_returned, key))
1377   {
1378       GNUNET_CONTAINER_multihashmap_put(ip_ctx->peers_returned, key, NULL, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1379       tmp.hashPubKey = (*key);
1380       ip_ctx->it (ip_ctx->it_cls, &tmp);
1381   }
1382
1383   return GNUNET_OK;
1384 }
1385
1386 /**
1387  * Return all peers currently known to ATS
1388  *
1389  * @param handle the address handle
1390  * @param p_it the iterator to call for every peer, callbach with id == NULL
1391  *        when done
1392  * @param p_it_cls the closure for the iterator
1393  */
1394 void
1395 GAS_addresses_iterate_peers (struct GAS_Addresses_Handle *handle, GNUNET_ATS_Peer_Iterator p_it, void *p_it_cls)
1396 {
1397   struct PeerIteratorContext ip_ctx;
1398   unsigned int size;
1399
1400   if (NULL == p_it)
1401       return;
1402   GNUNET_assert (NULL != handle->addresses);
1403
1404   size = GNUNET_CONTAINER_multihashmap_size(handle->addresses);
1405   if (0 != size)
1406   {
1407     ip_ctx.it = p_it;
1408     ip_ctx.it_cls = p_it_cls;
1409     ip_ctx.peers_returned = GNUNET_CONTAINER_multihashmap_create (size, GNUNET_NO);
1410     GNUNET_CONTAINER_multihashmap_iterate (handle->addresses, &peer_it, &ip_ctx);
1411     GNUNET_CONTAINER_multihashmap_destroy (ip_ctx.peers_returned);
1412   }
1413   p_it (p_it_cls, NULL);
1414 }
1415
1416 struct PeerInfoIteratorContext
1417 {
1418   GNUNET_ATS_PeerInfo_Iterator it;
1419   void *it_cls;
1420 };
1421
1422
1423 static int 
1424 peerinfo_it (void *cls,
1425              const struct GNUNET_HashCode * key,
1426              void *value)
1427 {
1428   struct PeerInfoIteratorContext *pi_ctx = cls;
1429   struct ATS_Address *addr = (struct ATS_Address *)  value;
1430   struct GNUNET_ATS_Information *ats;
1431   uint32_t ats_count;
1432
1433   if (NULL != pi_ctx->it)
1434   {
1435     ats_count = assemble_ats_information (addr, &ats);
1436
1437     pi_ctx->it (pi_ctx->it_cls,
1438                 &addr->peer,
1439                 addr->plugin,
1440                 addr->addr, addr->addr_len,
1441                 addr->active,
1442                 ats, ats_count,
1443                 addr->assigned_bw_out,
1444                 addr->assigned_bw_in);
1445     GNUNET_free (ats);
1446   }
1447   return GNUNET_YES;
1448 }
1449
1450
1451 /**
1452  * Return all peers currently known to ATS
1453  *
1454  * @param handle the address handle
1455  * @param peer the respective peer
1456  * @param pi_it the iterator to call for every peer
1457  * @param pi_it_cls the closure for the iterator
1458  */
1459 void
1460 GAS_addresses_get_peer_info (struct GAS_Addresses_Handle *handle,
1461                              const struct GNUNET_PeerIdentity *peer,
1462                              GNUNET_ATS_PeerInfo_Iterator pi_it,
1463                              void *pi_it_cls)
1464 {
1465   struct PeerInfoIteratorContext pi_ctx;
1466   struct GNUNET_BANDWIDTH_Value32NBO zero_bw;
1467   GNUNET_assert (NULL != peer);
1468   GNUNET_assert (NULL != handle->addresses);
1469   if (NULL == pi_it)
1470     return; /* does not make sense without callback */
1471
1472   zero_bw = GNUNET_BANDWIDTH_value_init (0);
1473   pi_ctx.it = pi_it;
1474   pi_ctx.it_cls = pi_it_cls;
1475
1476   GNUNET_CONTAINER_multihashmap_get_multiple (handle->addresses, &peer->hashPubKey, &peerinfo_it, &pi_ctx);
1477
1478   if (NULL != pi_it)
1479     pi_it (pi_it_cls, NULL, NULL, NULL, 0, GNUNET_NO, NULL, 0, zero_bw, zero_bw);
1480
1481 }
1482
1483
1484 /* end of gnunet-service-ats_addresses.c */