-preparations for replacement of try_connect call
[oweals/gnunet.git] / src / ats / ats_api_scheduling.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2010-2015 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., 51 Franklin Street, Fifth Floor,
18      Boston, MA 02110-1301, USA.
19 */
20 /**
21  * @file ats/ats_api_scheduling.c
22  * @brief automatic transport selection and outbound bandwidth determination
23  * @author Christian Grothoff
24  * @author Matthias Wachs
25  *
26  * TODO:
27  * - we could avoid a linear scan over the
28  *   active addresses in some cases, so if
29  *   there is need, we can still optimize here
30  * - we might want to split off the logic to
31  *   determine LAN vs. WAN, as it has nothing
32  *   to do with accessing the ATS service.
33  */
34 #include "platform.h"
35 #include "gnunet_ats_service.h"
36 #include "ats.h"
37
38 /**
39  * How frequently do we scan the interfaces for changes to the addresses?
40  */
41 #define INTERFACE_PROCESSING_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MINUTES, 2)
42
43 #define LOG(kind,...) GNUNET_log_from(kind, "ats-scheduling-api", __VA_ARGS__)
44
45 /**
46  * Session ID we use if there is no session / slot.
47  */
48 #define NOT_FOUND 0
49
50
51 /**
52  * Information we track per address, incoming or outgoing.  It also
53  * doesn't matter if we have a session, any address that ATS is
54  * allowed to suggest right now should be tracked.
55  */
56 struct GNUNET_ATS_AddressRecord
57 {
58
59   /**
60    * Scheduling handle this address record belongs to.
61    */
62   struct GNUNET_ATS_SchedulingHandle *sh;
63
64   /**
65    * Address data.
66    */
67   struct GNUNET_HELLO_Address *address;
68
69   /**
70    * Session handle.  NULL if we have an address but no
71    * active session for this address.
72    */
73   struct GNUNET_ATS_Session *session;
74
75   /**
76    * Performance data about the address.
77    */
78   struct GNUNET_ATS_PropertiesNBO properties;
79
80   /**
81    * Which slot (index) in the session array does
82    * this record correspond to?
83    * FIXME: a linear search on this is really crappy!
84    * Maybe switch to a 64-bit global counter and be
85    * done with it?  Or does that then cause too much
86    * trouble on the ATS-service side?
87    */
88   uint32_t slot;
89
90   /**
91    * We're about to destroy this address record, just ATS does
92    * not know this yet.  Once ATS confirms its destruction,
93    * we can clean up.
94    */
95   int in_destroy;
96 };
97
98
99 /**
100  * Handle to the ATS subsystem for bandwidth/transport scheduling information.
101  */
102 struct GNUNET_ATS_SchedulingHandle
103 {
104
105   /**
106    * Our configuration.
107    */
108   const struct GNUNET_CONFIGURATION_Handle *cfg;
109
110   /**
111    * Callback to invoke on suggestions.
112    */
113   GNUNET_ATS_AddressSuggestionCallback suggest_cb;
114
115   /**
116    * Closure for @e suggest_cb.
117    */
118   void *suggest_cb_cls;
119
120   /**
121    * Connection to ATS service.
122    */
123   struct GNUNET_CLIENT_Connection *client;
124
125   /**
126    * Message queue for sending requests to the ATS service.
127    */
128   struct GNUNET_MQ_Handle *mq;
129
130   /**
131    * Array of session objects (we need to translate them to numbers and back
132    * for the protocol; the offset in the array is the session number on the
133    * network).  Index 0 is always NULL and reserved to represent the NULL pointer.
134    * Unused entries are also NULL.
135    */
136   struct GNUNET_ATS_AddressRecord **session_array;
137
138   /**
139    * Task to trigger reconnect.
140    */
141   struct GNUNET_SCHEDULER_Task *task;
142
143   /**
144    * Reconnect backoff delay.
145    */
146   struct GNUNET_TIME_Relative backoff;
147
148   /**
149    * Size of the @e session_array.
150    */
151   unsigned int session_array_size;
152
153 };
154
155
156 /**
157  * Re-establish the connection to the ATS service.
158  *
159  * @param sh handle to use to re-connect.
160  */
161 static void
162 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
163
164
165 /**
166  * Re-establish the connection to the ATS service.
167  *
168  * @param cls handle to use to re-connect.
169  * @param tc scheduler context
170  */
171 static void
172 reconnect_task (void *cls,
173                 const struct GNUNET_SCHEDULER_TaskContext *tc)
174 {
175   struct GNUNET_ATS_SchedulingHandle *sh = cls;
176
177   sh->task = NULL;
178   reconnect (sh);
179 }
180
181
182 /**
183  * Disconnect from ATS and then reconnect.
184  *
185  * @param sh our handle
186  */
187 static void
188 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
189 {
190   if (NULL != sh->mq)
191   {
192     GNUNET_MQ_destroy (sh->mq);
193     sh->mq = NULL;
194   }
195   if (NULL != sh->client)
196   {
197     GNUNET_CLIENT_disconnect (sh->client);
198     sh->client = NULL;
199   }
200   sh->suggest_cb (sh->suggest_cb_cls,
201                   NULL, NULL, NULL,
202                   GNUNET_BANDWIDTH_ZERO,
203                   GNUNET_BANDWIDTH_ZERO);
204   sh->backoff = GNUNET_TIME_STD_BACKOFF (sh->backoff);
205   sh->task = GNUNET_SCHEDULER_add_delayed (sh->backoff,
206                                            &reconnect_task,
207                                            sh);
208 }
209
210
211 /**
212  * Find the session object corresponding to the given session ID.
213  *
214  * @param sh our handle
215  * @param session_id current session ID
216  * @param peer peer the session belongs to
217  * @return the session object (or NULL)
218  */
219 static struct GNUNET_ATS_AddressRecord *
220 find_session (struct GNUNET_ATS_SchedulingHandle *sh,
221               uint32_t session_id,
222               const struct GNUNET_PeerIdentity *peer)
223 {
224   struct GNUNET_ATS_AddressRecord *ar;
225
226   if (session_id >= sh->session_array_size)
227   {
228     GNUNET_break (0);
229     return NULL;
230   }
231   if (0 == session_id)
232     return NULL;
233   ar = sh->session_array[session_id];
234   if (NULL == ar)
235   {
236     GNUNET_break (0);
237     return NULL;
238   }
239   if (NULL == ar->address)
240   {
241     /* address was destroyed in the meantime, this can happen
242        as we communicate asynchronously with the ATS service. */
243     return NULL;
244   }
245   if (0 != memcmp (peer,
246                    &ar->address->peer,
247                    sizeof (struct GNUNET_PeerIdentity)))
248   {
249     GNUNET_break (0);
250     return NULL;
251   }
252   return ar;
253 }
254
255
256 /**
257  * Get an available session ID.
258  *
259  * @param sh our handle
260  * @return an unused slot, but never NOT_FOUND (0)
261  */
262 static uint32_t
263 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh)
264 {
265   static uint32_t off;
266   uint32_t i;
267
268   i = 0;
269   while ( ( (NOT_FOUND == off) ||
270             (NULL != sh->session_array[off % sh->session_array_size]) ) &&
271           (i < sh->session_array_size) )
272   {
273     off++;
274     i++;
275   }
276   if ( (NOT_FOUND != off % sh->session_array_size) &&
277        (NULL == sh->session_array[off % sh->session_array_size]) )
278     return off;
279   i = sh->session_array_size;
280   GNUNET_array_grow (sh->session_array,
281                      sh->session_array_size,
282                      sh->session_array_size * 2);
283   return i;
284 }
285
286
287 /**
288  * Get the ID for the given session object.
289  *
290  * @param sh our handle
291  * @param session session object
292  * @param address the address we are looking for
293  * @return the session id or NOT_FOUND for error
294  */
295 static uint32_t
296 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh,
297                  struct GNUNET_ATS_Session *session,
298                  const struct GNUNET_HELLO_Address *address)
299 {
300   uint32_t i;
301
302   if (NULL == address)
303   {
304     GNUNET_break (0);
305     return NOT_FOUND;
306   }
307   for (i = 1; i < sh->session_array_size; i++)
308     if ( (NULL != sh->session_array[i]) &&
309          (GNUNET_NO == sh->session_array[i]->in_destroy) &&
310          ( (session == sh->session_array[i]->session) ||
311            (NULL == sh->session_array[i]->session) ) &&
312          (0 == memcmp (&address->peer,
313                        &sh->session_array[i]->address->peer,
314                        sizeof (struct GNUNET_PeerIdentity))) &&
315          (0 == GNUNET_HELLO_address_cmp (address,
316                                          sh->session_array[i]->address)) )
317       return i;
318   return NOT_FOUND;
319 }
320
321
322 /**
323  * Release the session slot from the session table (ATS service is
324  * also done using it).
325  *
326  * @param sh our handle
327  * @param session_id identifies session that is no longer valid
328  */
329 static void
330 release_session (struct GNUNET_ATS_SchedulingHandle *sh,
331                  uint32_t session_id)
332 {
333   struct GNUNET_ATS_AddressRecord *ar;
334
335   if (NOT_FOUND == session_id)
336     return;
337   if (session_id >= sh->session_array_size)
338   {
339     GNUNET_break (0);
340     force_reconnect (sh);
341     return;
342   }
343   /* this slot should have been removed from remove_session before */
344   ar = sh->session_array[session_id];
345   if (NULL != ar->session)
346   {
347     GNUNET_break (0);
348     force_reconnect (sh);
349     return;
350   }
351   GNUNET_HELLO_address_free (ar->address);
352   GNUNET_free (ar);
353   sh->session_array[session_id] = NULL;
354 }
355
356
357 /**
358  * Type of a function to call when we receive a session release
359  * message from the service.
360  *
361  * @param cls the `struct GNUNET_ATS_SchedulingHandle`
362  * @param msg message received, NULL on timeout or fatal error
363  */
364 static void
365 process_ats_session_release_message (void *cls,
366                                      const struct GNUNET_MessageHeader *msg)
367 {
368   struct GNUNET_ATS_SchedulingHandle *sh = cls;
369   const struct GNUNET_ATS_SessionReleaseMessage *srm;
370
371   srm = (const struct GNUNET_ATS_SessionReleaseMessage *) msg;
372   /* Note: peer field in srm not necessary right now,
373      but might be good to have in the future */
374   release_session (sh,
375                    ntohl (srm->session_id));
376 }
377
378
379 /**
380  * Type of a function to call when we receive a address suggestion
381  * message from the service.
382  *
383  * @param cls the `struct GNUNET_ATS_SchedulingHandle`
384  * @param msg message received, NULL on timeout or fatal error
385  */
386 static void
387 process_ats_address_suggestion_message (void *cls,
388                                         const struct GNUNET_MessageHeader *msg)
389 {
390   struct GNUNET_ATS_SchedulingHandle *sh = cls;
391   const struct AddressSuggestionMessage *m;
392   struct GNUNET_ATS_AddressRecord *ar;
393   uint32_t session_id;
394
395   m = (const struct AddressSuggestionMessage *) msg;
396   session_id = ntohl (m->session_id);
397   if (0 == session_id)
398   {
399     GNUNET_break (0);
400     force_reconnect (sh);
401     return;
402   }
403   ar = find_session (sh,
404                      session_id,
405                      &m->peer);
406   if (NULL == ar)
407   {
408     GNUNET_break (0);
409     force_reconnect (sh);
410     return;
411   }
412   if (NULL == sh->suggest_cb)
413     return;
414   if (GNUNET_YES == ar->in_destroy)
415   {
416     /* ignore suggestion, as this address is dying, unless BW is 0,
417        in that case signal 'disconnect' via BW 0 */
418     if ( (0 == ntohl (m->bandwidth_out.value__)) &&
419          (0 == ntohl (m->bandwidth_in.value__)) )
420     {
421       LOG (GNUNET_ERROR_TYPE_DEBUG,
422            "ATS suggests disconnect from peer `%s' with BW %u/%u\n",
423            GNUNET_i2s (&ar->address->peer),
424            (unsigned int) ntohl (m->bandwidth_out.value__),
425            (unsigned int) ntohl (m->bandwidth_in.value__));
426       sh->suggest_cb (sh->suggest_cb_cls,
427                       &m->peer,
428                       NULL,
429                       NULL,
430                       m->bandwidth_out,
431                       m->bandwidth_in);
432     }
433     return;
434   }
435   if ( (NULL == ar->session) &&
436        (GNUNET_HELLO_address_check_option (ar->address,
437                                            GNUNET_HELLO_ADDRESS_INFO_INBOUND)) )
438   {
439     GNUNET_break (0);
440     return;
441   }
442   sh->backoff = GNUNET_TIME_UNIT_ZERO;
443   LOG (GNUNET_ERROR_TYPE_DEBUG,
444        "ATS suggests address slot %u for peer `%s' using plugin %s\n",
445        ar->slot,
446        GNUNET_i2s (&ar->address->peer),
447        ar->address->transport_name);
448   sh->suggest_cb (sh->suggest_cb_cls,
449                   &m->peer,
450                   ar->address,
451                   ar->session,
452                   m->bandwidth_out,
453                   m->bandwidth_in);
454 }
455
456
457 /**
458  * We encountered an error handling the MQ to the
459  * ATS service.  Reconnect.
460  *
461  * @param cls the `struct GNUNET_ATS_SchedulingHandle`
462  * @param error details about the error
463  */
464 static void
465 error_handler (void *cls,
466                enum GNUNET_MQ_Error error)
467 {
468   struct GNUNET_ATS_SchedulingHandle *sh = cls;
469
470   LOG (GNUNET_ERROR_TYPE_ERROR,
471        "ATS connection died (code %d), reconnecting\n",
472        (int) error);
473   force_reconnect (sh);
474 }
475
476
477 /**
478  * Generate and transmit the `struct AddressAddMessage` for the given
479  * address record.
480  *
481  * @param sh the scheduling handle to use for transmission
482  * @param ar the address to inform the ATS service about
483  */
484 static void
485 send_add_address_message (struct GNUNET_ATS_SchedulingHandle *sh,
486                           const struct GNUNET_ATS_AddressRecord *ar)
487 {
488   struct GNUNET_MQ_Envelope *ev;
489   struct AddressAddMessage *m;
490   char *pm;
491   size_t namelen;
492   size_t msize;
493
494   if (NULL == sh->mq)
495     return; /* disconnected, skip for now */
496   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != ar->properties.scope);
497   namelen = strlen (ar->address->transport_name) + 1;
498   msize = ar->address->address_length + namelen;
499   ev = GNUNET_MQ_msg_extra (m, msize, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
500   m->peer = ar->address->peer;
501   m->address_length = htons (ar->address->address_length);
502   m->address_local_info = htonl ((uint32_t) ar->address->local_info);
503   m->plugin_name_length = htons (namelen);
504   m->session_id = htonl (ar->slot);
505   m->properties = ar->properties;
506
507   LOG (GNUNET_ERROR_TYPE_DEBUG,
508        "Adding address for peer `%s', plugin `%s', session %p slot %u\n",
509        GNUNET_i2s (&ar->address->peer),
510        ar->address->transport_name,
511        ar->session,
512        ar->slot);
513   pm = (char *) &m[1];
514   memcpy (pm,
515           ar->address->address,
516           ar->address->address_length);
517   if (NULL != ar->address->transport_name)
518     memcpy (&pm[ar->address->address_length],
519             ar->address->transport_name,
520             namelen);
521   GNUNET_MQ_send (sh->mq, ev);
522 }
523
524
525 /**
526  * Re-establish the connection to the ATS service.
527  *
528  * @param sh handle to use to re-connect.
529  */
530 static void
531 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
532 {
533   static const struct GNUNET_MQ_MessageHandler handlers[] =
534     { { &process_ats_session_release_message,
535         GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE,
536         sizeof (struct GNUNET_ATS_SessionReleaseMessage) },
537       { &process_ats_address_suggestion_message,
538         GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION,
539         sizeof (struct AddressSuggestionMessage) },
540       { NULL, 0, 0 } };
541   struct GNUNET_MQ_Envelope *ev;
542   struct ClientStartMessage *init;
543   unsigned int i;
544   struct GNUNET_ATS_AddressRecord *ar;
545
546   GNUNET_assert (NULL == sh->client);
547   sh->client = GNUNET_CLIENT_connect ("ats",
548                                       sh->cfg);
549   if (NULL == sh->client)
550   {
551     GNUNET_break (0);
552     force_reconnect (sh);
553     return;
554   }
555   sh->mq = GNUNET_MQ_queue_for_connection_client (sh->client,
556                                                   handlers,
557                                                   &error_handler,
558                                                   sh);
559   ev = GNUNET_MQ_msg (init,
560                       GNUNET_MESSAGE_TYPE_ATS_START);
561   init->start_flag = htonl (START_FLAG_SCHEDULING);
562   GNUNET_MQ_send (sh->mq, ev);
563   if (NULL == sh->mq)
564     return;
565   for (i=0;i<sh->session_array_size;i++)
566   {
567     ar = sh->session_array[i];
568     if (NULL == ar)
569       continue;
570     send_add_address_message (sh, ar);
571     if (NULL == sh->mq)
572       return;
573   }
574 }
575
576
577 /**
578  * Initialize the ATS subsystem.
579  *
580  * @param cfg configuration to use
581  * @param suggest_cb notification to call whenever the suggestation changed
582  * @param suggest_cb_cls closure for @a suggest_cb
583  * @return ats context
584  */
585 struct GNUNET_ATS_SchedulingHandle *
586 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
587                             GNUNET_ATS_AddressSuggestionCallback suggest_cb,
588                             void *suggest_cb_cls)
589 {
590   struct GNUNET_ATS_SchedulingHandle *sh;
591
592   sh = GNUNET_new (struct GNUNET_ATS_SchedulingHandle);
593   sh->cfg = cfg;
594   sh->suggest_cb = suggest_cb;
595   sh->suggest_cb_cls = suggest_cb_cls;
596   GNUNET_array_grow (sh->session_array,
597                      sh->session_array_size,
598                      4);
599   reconnect (sh);
600   return sh;
601 }
602
603
604 /**
605  * Client is done with ATS scheduling, release resources.
606  *
607  * @param sh handle to release
608  */
609 void
610 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
611 {
612   struct GNUNET_ATS_AddressRecord *ar;
613   unsigned int i;
614
615   if (NULL != sh->mq)
616   {
617     GNUNET_MQ_destroy (sh->mq);
618     sh->mq = NULL;
619   }
620   if (NULL != sh->client)
621   {
622     GNUNET_CLIENT_disconnect (sh->client);
623     sh->client = NULL;
624   }
625   if (NULL != sh->task)
626   {
627     GNUNET_SCHEDULER_cancel (sh->task);
628     sh->task = NULL;
629   }
630   for (i=0;i<sh->session_array_size;i++)
631   {
632     if (NULL != (ar = sh->session_array[i]))
633     {
634       GNUNET_HELLO_address_free (ar->address);
635       GNUNET_free (ar);
636       sh->session_array[i] = NULL;
637     }
638   }
639   GNUNET_array_grow (sh->session_array,
640                      sh->session_array_size,
641                      0);
642   GNUNET_free (sh);
643 }
644
645
646 /**
647  * We have a new address ATS should know. Addresses have to be added
648  * with this function before they can be: updated, set in use and
649  * destroyed.
650  *
651  * @param sh handle
652  * @param address the address
653  * @param session session handle, can be NULL
654  * @param prop performance data for the address
655  * @return handle to the address representation inside ATS, NULL
656  *         on error (i.e. ATS knows this exact address already)
657  */
658 struct GNUNET_ATS_AddressRecord *
659 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
660                         const struct GNUNET_HELLO_Address *address,
661                         struct GNUNET_ATS_Session *session,
662                         const struct GNUNET_ATS_Properties *prop)
663 {
664   struct GNUNET_ATS_AddressRecord *ar;
665   size_t namelen;
666   size_t msize;
667   uint32_t s;
668
669   if (NULL == address)
670   {
671     /* we need a valid address */
672     GNUNET_break (0);
673     return NULL;
674   }
675   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
676   namelen = strlen (address->transport_name) + 1;
677   msize = address->address_length + namelen;
678   if ((msize + sizeof (struct AddressUpdateMessage) >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
679       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
680       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) )
681   {
682     /* address too large for us, this should not happen */
683     GNUNET_break (0);
684     return NULL;
685   }
686
687   if (NOT_FOUND !=
688       find_session_id (sh,
689                        session,
690                        address))
691   {
692     /* Already existing, nothing todo, but this should not happen */
693     GNUNET_break (0);
694     return NULL;
695   }
696   s = find_empty_session_slot (sh);
697   ar = GNUNET_new (struct GNUNET_ATS_AddressRecord);
698   ar->sh = sh;
699   ar->slot = s;
700   ar->session = session;
701   ar->address = GNUNET_HELLO_address_copy (address);
702   GNUNET_ATS_properties_hton (&ar->properties,
703                               prop);
704   sh->session_array[s] = ar;
705   send_add_address_message (sh, ar);
706   return ar;
707 }
708
709
710 /**
711  * An address was used to initiate a session.
712  *
713  * @param ar address record to update information for
714  * @param session session handle
715  */
716 void
717 GNUNET_ATS_address_add_session (struct GNUNET_ATS_AddressRecord *ar,
718                                 struct GNUNET_ATS_Session *session)
719 {
720   GNUNET_break (NULL == ar->session);
721   ar->session = session;
722 }
723
724
725 /**
726  * A session was destroyed, disassociate it from the
727  * given address record.  If this was an incoming
728  * addess, destroy the address as well.
729  *
730  * @param ar address record to update information for
731  * @param session session handle
732  * @return #GNUNET_YES if the @a ar was destroyed because
733  *                     it was an incoming address,
734  *         #GNUNET_NO if the @ar was kept because we can
735  *                    use it still to establish a new session
736  */
737 int
738 GNUNET_ATS_address_del_session (struct GNUNET_ATS_AddressRecord *ar,
739                                 struct GNUNET_ATS_Session *session)
740 {
741   GNUNET_assert (session == ar->session);
742   ar->session = NULL;
743   if (GNUNET_HELLO_address_check_option (ar->address,
744                                          GNUNET_HELLO_ADDRESS_INFO_INBOUND))
745   {
746     GNUNET_ATS_address_destroy (ar);
747     return GNUNET_YES;
748   }
749   return GNUNET_NO;
750 }
751
752
753 /**
754  * We have updated performance statistics for a given address.  Note
755  * that this function can be called for addresses that are currently
756  * in use as well as addresses that are valid but not actively in use.
757  * Furthermore, the peer may not even be connected to us right now (in
758  * which case the call may be ignored or the information may be stored
759  * for later use).  Update bandwidth assignments.
760  *
761  * @param ar address record to update information for
762  * @param prop performance data for the address
763  */
764 void
765 GNUNET_ATS_address_update (struct GNUNET_ATS_AddressRecord *ar,
766                            const struct GNUNET_ATS_Properties *prop)
767 {
768   struct GNUNET_ATS_SchedulingHandle *sh = ar->sh;
769   struct GNUNET_MQ_Envelope *ev;
770   struct AddressUpdateMessage *m;
771
772   LOG (GNUNET_ERROR_TYPE_DEBUG,
773        "Updating address for peer `%s', plugin `%s', session %p slot %u\n",
774        GNUNET_i2s (&ar->address->peer),
775        ar->address->transport_name,
776        ar->session,
777        ar->slot);
778   GNUNET_break (GNUNET_ATS_NET_UNSPECIFIED != prop->scope);
779   GNUNET_ATS_properties_hton (&ar->properties,
780                               prop);
781   if (NULL == sh->mq)
782     return; /* disconnected, skip for now */
783   ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
784   m->session_id = htonl (ar->slot);
785   m->peer = ar->address->peer;
786   m->properties = ar->properties;
787   GNUNET_MQ_send (sh->mq,
788                   ev);
789 }
790
791
792 /**
793  * An address got destroyed, stop using it as a valid address.
794  *
795  * @param ar address to destroy
796  */
797 void
798 GNUNET_ATS_address_destroy (struct GNUNET_ATS_AddressRecord *ar)
799 {
800   struct GNUNET_ATS_SchedulingHandle *sh = ar->sh;
801   struct GNUNET_MQ_Envelope *ev;
802   struct AddressDestroyedMessage *m;
803
804   LOG (GNUNET_ERROR_TYPE_DEBUG,
805        "Deleting address for peer `%s', plugin `%s', slot %u session %p\n",
806        GNUNET_i2s (&ar->address->peer),
807        ar->address->transport_name,
808        ar->slot,
809        ar->session);
810   GNUNET_break (NULL == ar->session);
811   ar->session = NULL;
812   ar->in_destroy = GNUNET_YES;
813   if (NULL == sh->mq)
814     return;
815   ev = GNUNET_MQ_msg (m, GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
816   m->session_id = htonl (ar->slot);
817   m->peer = ar->address->peer;
818   GNUNET_MQ_send (sh->mq, ev);
819 }
820
821
822 /* end of ats_api_scheduling.c */