- new test
[oweals/gnunet.git] / src / ats / ats_api_scheduling.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010,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  * @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 #include "platform.h"
27 #include "gnunet_ats_service.h"
28 #include "ats.h"
29
30
31 #define INTERFACE_PROCESSING_INTERVALL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
32
33 #define NOT_FOUND 0
34
35 /**
36  * Message in linked list we should send to the ATS service.  The
37  * actual binary message follows this struct.
38  */
39 struct PendingMessage
40 {
41
42   /**
43    * Kept in a DLL.
44    */
45   struct PendingMessage *next;
46
47   /**
48    * Kept in a DLL.
49    */
50   struct PendingMessage *prev;
51
52   /**
53    * Size of the message.
54    */
55   size_t size;
56
57   /**
58    * Is this the 'ATS_START' message?
59    */
60   int is_init;
61 };
62
63
64 /**
65  * Information we track per session.
66  */
67 struct SessionRecord
68 {
69   /**
70    * Identity of the peer (just needed for error checking).
71    */
72   struct GNUNET_PeerIdentity peer;
73
74   /**
75    * Session handle.
76    */
77   struct Session *session;
78
79   /**
80    * Set to GNUNET_YES if the slot is used.
81    */
82   int slot_used;
83 };
84
85
86 struct ATS_Network
87 {
88   struct ATS_Network * next;
89
90   struct ATS_Network * prev;
91
92   struct sockaddr *network;
93   struct sockaddr *netmask;
94   socklen_t length;
95 };
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 '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    * Head of list of messages for the ATS service.
127    */
128   struct PendingMessage *pending_head;
129
130   /**
131    * Tail of list of messages for the ATS service
132    */
133   struct PendingMessage *pending_tail;
134
135   /**
136    * Current request for transmission to ATS.
137    */
138   struct GNUNET_CLIENT_TransmitHandle *th;
139
140   /**
141    * Head of network list
142    */
143   struct ATS_Network * net_head;
144
145   /**
146    * Tail of network list
147    */
148   struct ATS_Network * net_tail;
149
150   /**
151    * Array of session objects (we need to translate them to numbers and back
152    * for the protocol; the offset in the array is the session number on the
153    * network).  Index 0 is always NULL and reserved to represent the NULL pointer.
154    * Unused entries are also NULL.
155    */
156   struct SessionRecord *session_array;
157
158   /**
159    * Task to trigger reconnect.
160    */
161   GNUNET_SCHEDULER_TaskIdentifier task;
162
163   /**
164    * Task retrieving interfaces from the system
165    */
166   GNUNET_SCHEDULER_TaskIdentifier interface_task;
167
168
169   /**
170    * Size of the session array.
171    */
172   unsigned int session_array_size;
173
174   /**
175    * Should we reconnect to ATS due to some serious error?
176    */
177   int reconnect;
178 };
179
180
181 /**
182  * Re-establish the connection to the ATS service.
183  *
184  * @param sh handle to use to re-connect.
185  */
186 static void
187 reconnect (struct GNUNET_ATS_SchedulingHandle *sh);
188
189
190 /**
191  * Re-establish the connection to the ATS service.
192  *
193  * @param cls handle to use to re-connect.
194  * @param tc scheduler context
195  */
196 static void
197 reconnect_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
198 {
199   struct GNUNET_ATS_SchedulingHandle *sh = cls;
200
201   sh->task = GNUNET_SCHEDULER_NO_TASK;
202   reconnect (sh);
203 }
204
205
206 /**
207  * Disconnect from ATS and then reconnect.
208  *
209  * @param sh our handle
210  */
211 static void
212 force_reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
213 {
214   sh->reconnect = GNUNET_NO;
215   GNUNET_CLIENT_disconnect (sh->client);
216   sh->client = NULL;
217   sh->task =
218       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &reconnect_task,
219                                     sh);
220 }
221
222
223 /**
224  * Transmit messages from the message queue to the service
225  * (if there are any, and if we are not already trying).
226  *
227  * @param sh handle to use
228  */
229 static void
230 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh);
231
232
233 /**
234  * Type of a function to call when we receive a message
235  * from the service.
236  *
237  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
238  * @param msg message received, NULL on timeout or fatal error
239  */
240 static void
241 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg);
242
243
244 /**
245  * We can now transmit a message to ATS. Do it.
246  *
247  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
248  * @param size number of bytes we can transmit to ATS
249  * @param buf where to copy the messages
250  * @return number of bytes copied into buf
251  */
252 static size_t
253 transmit_message_to_ats (void *cls, size_t size, void *buf)
254 {
255   struct GNUNET_ATS_SchedulingHandle *sh = cls;
256   struct PendingMessage *p;
257   size_t ret;
258   char *cbuf;
259
260   sh->th = NULL;
261   if ((size == 0) || (buf == NULL))
262   {
263     force_reconnect (sh);
264     return 0;
265   }
266   ret = 0;
267   cbuf = buf;
268   while ((NULL != (p = sh->pending_head)) && (p->size <= size))
269   {
270     memcpy (&cbuf[ret], &p[1], p->size);
271     ret += p->size;
272     size -= p->size;
273     GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
274     if (GNUNET_YES == p->is_init)
275       GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
276                              GNUNET_TIME_UNIT_FOREVER_REL);
277     GNUNET_free (p);
278   }
279   do_transmit (sh);
280   return ret;
281 }
282
283
284 /**
285  * Transmit messages from the message queue to the service
286  * (if there are any, and if we are not already trying).
287  *
288  * @param sh handle to use
289  */
290 static void
291 do_transmit (struct GNUNET_ATS_SchedulingHandle *sh)
292 {
293   struct PendingMessage *p;
294
295   if (NULL != sh->th)
296     return;
297   if (NULL == (p = sh->pending_head))
298     return;
299   if (NULL == sh->client)
300     return;                     /* currently reconnecting */
301   sh->th =
302       GNUNET_CLIENT_notify_transmit_ready (sh->client, p->size,
303                                            GNUNET_TIME_UNIT_FOREVER_REL,
304                                            GNUNET_NO, &transmit_message_to_ats,
305                                            sh);
306 }
307
308
309 /**
310  * Find the session object corresponding to the given session ID.
311  *
312  * @param sh our handle
313  * @param session_id current session ID
314  * @param peer peer the session belongs to
315  * @return the session object (or NULL)
316  */
317 static struct Session *
318 find_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
319               const struct GNUNET_PeerIdentity *peer)
320 {
321
322   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
323               "Find session %u from peer %s in %p\n",
324               (unsigned int) session_id, GNUNET_i2s (peer), sh);
325
326   if (session_id >= sh->session_array_size)
327   {
328     GNUNET_break (0);
329     return NULL;
330   }
331   if (0 == session_id)
332     return NULL;
333   if (sh->session_array[session_id].session == NULL)
334   {
335     GNUNET_break (0 ==
336                   memcmp (peer, &sh->session_array[session_id].peer,
337                           sizeof (struct GNUNET_PeerIdentity)));
338     return NULL;
339   }
340
341   if (0 !=
342       memcmp (peer, &sh->session_array[session_id].peer,
343               sizeof (struct GNUNET_PeerIdentity)))
344   {
345     GNUNET_break (0);
346     sh->reconnect = GNUNET_YES;
347     return NULL;
348   }
349   /* This check exploits the fact that first field of a session object
350    * is peer identity.
351    */
352   if (0 !=
353       memcmp (peer, sh->session_array[session_id].session,
354               sizeof (struct GNUNET_PeerIdentity)))
355   {
356     GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
357               "Session %p belongs to peer `%s'\n",
358               sh->session_array[session_id].session, GNUNET_i2s_full ((struct GNUNET_PeerIdentity *) &sh->session_array[session_id].peer));
359 /*
360     GNUNET_break (0);
361     sh->reconnect = GNUNET_YES;
362     return NULL;
363 */
364   }
365   return sh->session_array[session_id].session;
366 }
367
368
369 /**
370  * Get an available session ID for the given session object.
371  *
372  * @param sh our handle
373  * @param session session object
374  * @param peer peer the session belongs to
375  * @return the session id
376  */
377 static uint32_t
378 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
379                 const struct GNUNET_PeerIdentity *peer)
380 {
381   unsigned int i;
382   unsigned int f;
383
384   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
385               "Get session ID for session %p from peer %s in %p\n", session,
386               GNUNET_i2s (peer), sh);
387
388   if (NULL == session)
389     return NOT_FOUND;
390   f = 0;
391   for (i = 1; i < sh->session_array_size; i++)
392   {
393     if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
394       f = i;
395   }
396   if (f == 0)
397   {
398     f = sh->session_array_size;
399     GNUNET_array_grow (sh->session_array, sh->session_array_size,
400                        sh->session_array_size * 2);
401   }
402   GNUNET_assert (f > 0);
403   sh->session_array[f].session = session;
404   sh->session_array[f].peer = *peer;
405   sh->session_array[f].slot_used = GNUNET_YES;
406
407   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
408               "Assigning session ID %u for session %p of peer %s in %p\n", f,
409               session, GNUNET_i2s (peer), sh);
410
411   return f;
412 }
413
414
415 /**
416  * Get the ID for the given session object.
417  *
418  * @param sh our handle
419  * @param session session object
420  * @param peer peer the session belongs to
421  * @return the session id or NOT_FOUND for error
422  */
423 static uint32_t
424 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
425                 const struct GNUNET_PeerIdentity *peer)
426 {
427   unsigned int i;
428   char * p2;
429
430   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
431               "Get session ID for session %p from peer %s in %p\n", session,
432               GNUNET_i2s (peer), sh);
433
434   if (NULL == session)
435     return NOT_FOUND;
436   for (i = 1; i < sh->session_array_size; i++)
437   {
438     if (session == sh->session_array[i].session)
439     {
440       if (0 != memcmp (peer, &sh->session_array[i].peer,
441                        sizeof (struct GNUNET_PeerIdentity)))
442       {
443         p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
444         GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
445                     "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
446                     session, GNUNET_i2s (peer), p2);
447         GNUNET_free (p2);
448         return NOT_FOUND;
449       }
450       return i;
451     }
452   }
453   return NOT_FOUND;
454 }
455
456
457 /**
458  * Remove the session of the given session ID from the session
459  * table (it is no longer valid).
460  *
461  * @param sh our handle
462  * @param session_id identifies session that is no longer valid
463  * @param peer peer the session belongs to
464  */
465 static void
466 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
467                 const struct GNUNET_PeerIdentity *peer)
468 {
469   GNUNET_assert (peer != NULL);
470   GNUNET_assert (sh != NULL);
471
472   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
473               "Release sessionID %u from peer %s in %p\n",
474               (unsigned int) session_id, GNUNET_i2s (peer), sh);
475
476   if (0 == session_id)
477     return;
478
479   GNUNET_assert (session_id < sh->session_array_size);
480   GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
481   GNUNET_assert (0 == memcmp (peer,
482                               &sh->session_array[session_id].peer,
483                               sizeof (struct GNUNET_PeerIdentity)));
484   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
485               "Session %p for peer `%s' removed from slot %u \n",
486               sh->session_array[session_id].session,
487               GNUNET_i2s (peer),
488               session_id);
489   sh->session_array[session_id].session = NULL;
490
491 }
492
493
494 /**
495  * Release the session slot from the session table (ATS service is
496  * also done using it).
497  *
498  * @param sh our handle
499  * @param session_id identifies session that is no longer valid
500  * @param peer peer the session belongs to
501  */
502 static void
503 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
504                  const struct GNUNET_PeerIdentity *peer)
505 {
506
507   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
508               "Release sessionID %u from peer %s in %p\n",
509               (unsigned int) session_id, GNUNET_i2s (peer), sh);
510
511   if (session_id >= sh->session_array_size)
512   {
513     GNUNET_break (0);
514     sh->reconnect = GNUNET_YES;
515     return;
516   }
517
518   /* this slot should have been removed from remove_session before */
519   GNUNET_assert (sh->session_array[session_id].session == NULL);
520
521   if (0 !=
522       memcmp (peer, &sh->session_array[session_id].peer,
523               sizeof (struct GNUNET_PeerIdentity)))
524   {
525     GNUNET_break (0);
526     sh->reconnect = GNUNET_YES;
527     return;
528   }
529   sh->session_array[session_id].slot_used = GNUNET_NO;
530   memset (&sh->session_array[session_id].peer, 0,
531           sizeof (struct GNUNET_PeerIdentity));
532 }
533
534
535 static void
536 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
537                          const struct SessionReleaseMessage *srm)
538 {
539   release_session (sh, ntohl (srm->session_id), &srm->peer);
540 }
541
542
543 /**
544  * Type of a function to call when we receive a message
545  * from the service.
546  *
547  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
548  * @param msg message received, NULL on timeout or fatal error
549  */
550 static void
551 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
552 {
553   struct GNUNET_ATS_SchedulingHandle *sh = cls;
554   const struct AddressSuggestionMessage *m;
555   const struct GNUNET_ATS_Information *atsi;
556   const char *plugin_address;
557   const char *plugin_name;
558   uint16_t plugin_address_length;
559   uint16_t plugin_name_length;
560   uint32_t ats_count;
561   struct GNUNET_HELLO_Address address;
562   struct Session *s;
563
564   if (NULL == msg)
565   {
566     force_reconnect (sh);
567     return;
568   }
569   if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
570       (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
571   {
572     process_release_message (sh, (const struct SessionReleaseMessage *) msg);
573     GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
574                            GNUNET_TIME_UNIT_FOREVER_REL);
575     if (GNUNET_YES == sh->reconnect)
576       force_reconnect (sh);
577     return;
578   }
579   if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
580       (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
581   {
582     GNUNET_break (0);
583     force_reconnect (sh);
584     return;
585   }
586   m = (const struct AddressSuggestionMessage *) msg;
587   ats_count = ntohl (m->ats_count);
588   plugin_address_length = ntohs (m->address_length);
589   atsi = (const struct GNUNET_ATS_Information *) &m[1];
590   plugin_address = (const char *) &atsi[ats_count];
591   plugin_name = &plugin_address[plugin_address_length];
592   plugin_name_length = ntohs (m->plugin_name_length);
593   if ((plugin_address_length + plugin_name_length +
594        ats_count * sizeof (struct GNUNET_ATS_Information) +
595        sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
596       (ats_count >
597        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
598       || (plugin_name[plugin_name_length - 1] != '\0'))
599   {
600     GNUNET_break (0);
601     force_reconnect (sh);
602     return;
603   }
604   uint32_t session_id = ntohl (m->session_id);
605
606   if (session_id == 0)
607     s = NULL;
608   else
609   {
610     s = find_session (sh, session_id, &m->peer);
611     if (s == NULL)
612     {
613
614       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
615                   "ATS tries to use outdated session `%s'\n",
616                   GNUNET_i2s (&m->peer));
617       GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
618                              GNUNET_TIME_UNIT_FOREVER_REL);
619       return;
620     }
621   }
622   address.peer = m->peer;
623   address.address = plugin_address;
624   address.address_length = plugin_address_length;
625   address.transport_name = plugin_name;
626
627   if ((s == NULL) && (0 == address.address_length))
628   {
629     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
630                 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
631                 GNUNET_i2s (&address.peer), address.transport_name,
632                 plugin_address_length, session_id);
633     GNUNET_break_op (0);
634     GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
635                            GNUNET_TIME_UNIT_FOREVER_REL);
636     return;
637   }
638
639   sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
640                   m->bandwidth_in, atsi, ats_count);
641
642   GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
643                          GNUNET_TIME_UNIT_FOREVER_REL);
644   if (GNUNET_YES == sh->reconnect)
645     force_reconnect (sh);
646 }
647
648
649 /**
650  * Re-establish the connection to the ATS service.
651  *
652  * @param sh handle to use to re-connect.
653  */
654 static void
655 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
656 {
657   struct PendingMessage *p;
658   struct ClientStartMessage *init;
659
660   GNUNET_assert (NULL == sh->client);
661   sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
662   GNUNET_assert (NULL != sh->client);
663   if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
664   {
665     p = GNUNET_malloc (sizeof (struct PendingMessage) +
666                        sizeof (struct ClientStartMessage));
667     p->size = sizeof (struct ClientStartMessage);
668     p->is_init = GNUNET_YES;
669     init = (struct ClientStartMessage *) &p[1];
670     init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
671     init->header.size = htons (sizeof (struct ClientStartMessage));
672     init->start_flag = htonl (START_FLAG_SCHEDULING);
673     GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
674   }
675   do_transmit (sh);
676 }
677
678
679 /**
680  * delete the current network list
681  */
682 static void
683 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
684 {
685   struct ATS_Network * cur = sh->net_head;
686   while (cur != NULL)
687   {
688     GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
689     GNUNET_free (cur);
690     cur = sh->net_head;
691   }
692 }
693
694
695 static int
696 interface_proc (void *cls, const char *name,
697                 int isDefault,
698                 const struct sockaddr *
699                 addr,
700                 const struct sockaddr *
701                 broadcast_addr,
702                 const struct sockaddr *
703                 netmask, socklen_t addrlen)
704 {
705   struct GNUNET_ATS_SchedulingHandle * sh = cls;
706   /* Calculate network */
707   struct ATS_Network *net = NULL;
708
709   /* Skipping IPv4 loopback addresses since we have special check  */
710   if  (addr->sa_family == AF_INET)
711   {
712     struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
713
714     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
715        return GNUNET_OK;
716   }
717   /* Skipping IPv6 loopback addresses since we have special check  */
718   if  (addr->sa_family == AF_INET6)
719   {
720     struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
721     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
722       return GNUNET_OK;
723   }
724
725   if (addr->sa_family == AF_INET)
726   {
727     struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
728     struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
729     struct sockaddr_in *tmp = NULL;
730     struct sockaddr_in network4;
731
732     net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
733     tmp = (struct sockaddr_in *) &net[1];
734     net->network = (struct sockaddr *) &tmp[0];
735     net->netmask = (struct sockaddr *) &tmp[1];
736     net->length = addrlen;
737
738     memset (&network4, 0, sizeof (network4));
739     network4.sin_family = AF_INET;
740 #if HAVE_SOCKADDR_IN_SIN_LEN
741     network4.sin_len = sizeof (network4);
742 #endif
743     network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
744
745     memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
746     memcpy (net->network, &network4, sizeof (struct sockaddr_in));
747   }
748
749   if (addr->sa_family == AF_INET6)
750   {
751     struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
752     struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
753     struct sockaddr_in6 * tmp = NULL;
754     struct sockaddr_in6 network6;
755
756     net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
757     tmp = (struct sockaddr_in6 *) &net[1];
758     net->network = (struct sockaddr *) &tmp[0];
759     net->netmask = (struct sockaddr *) &tmp[1];
760     net->length = addrlen;
761
762     memset (&network6, 0, sizeof (network6));
763     network6.sin6_family = AF_INET6;
764 #if HAVE_SOCKADDR_IN_SIN_LEN
765     network6.sin6_len = sizeof (network6);
766 #endif
767     int c = 0;
768     uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
769     uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
770     uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
771     for (c = 0; c < 4; c++)
772       net_elem[c] = addr_elem[c] & mask_elem[c];
773
774     memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
775     memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
776   }
777
778   /* Store in list */
779   if (net != NULL)
780   {
781 #if VERBOSE_ATS
782     char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
783     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
784         GNUNET_a2s((struct sockaddr *) net->network, addrlen),
785         netmask);
786     GNUNET_free (netmask);
787 # endif
788     GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
789   }
790   return GNUNET_OK;
791 }
792
793
794 /**
795  * Periodically get list of addresses
796  * @param cls closure
797  * @param tc Task context
798  */
799 static void
800 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
801 {
802   struct GNUNET_ATS_SchedulingHandle * sh = cls;
803   sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
804   delete_networks (sh);
805   GNUNET_OS_network_interfaces_list(interface_proc, sh);
806   sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
807                                                      get_addresses,
808                                                      sh);
809 }
810
811 /**
812  * Convert a GNUNET_ATS_NetworkType to a string
813  *
814  * @param net the network type
815  * @return a string or NULL if invalid
816  */
817 const char *
818 GNUNET_ATS_print_network_type (uint32_t net)
819 {
820   char *networks[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
821   if (net < GNUNET_ATS_NetworkTypeCount)
822     return networks[net];
823   return NULL;
824 }
825
826
827 /**
828  * Returns where the address is located: LAN or WAN or ...
829  *
830  * @param sh the scheduling handle
831  * @param addr address
832  * @param addrlen address length
833  * @return location as GNUNET_ATS_Information
834  */
835 struct GNUNET_ATS_Information
836 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
837 {
838   GNUNET_assert (sh != NULL);
839   struct ATS_Network * cur = sh->net_head;
840
841   int type = GNUNET_ATS_NET_UNSPECIFIED;
842   struct GNUNET_ATS_Information ats;
843
844   if  (addr->sa_family == AF_UNIX)
845   {
846     type = GNUNET_ATS_NET_LOOPBACK;
847   }
848
849   /* IPv4 loopback check */
850   if  (addr->sa_family == AF_INET)
851   {
852     struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
853
854     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
855       type = GNUNET_ATS_NET_LOOPBACK;
856   }
857   /* IPv6 loopback check */
858   if  (addr->sa_family == AF_INET6)
859   {
860     struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
861     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
862       type = GNUNET_ATS_NET_LOOPBACK;
863   }
864
865   /* Check local networks */
866   while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
867   {
868     if (addrlen != cur->length)
869     {
870       cur = cur->next;
871       continue;
872     }
873
874     if (addr->sa_family == AF_INET)
875     {
876       struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
877       struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
878       struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
879
880       if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
881         type = GNUNET_ATS_NET_LAN;
882     }
883     if (addr->sa_family == AF_INET6)
884     {
885       struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
886       struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
887       struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
888
889       int res = GNUNET_YES;
890       int c = 0;
891       uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
892       uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
893       uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
894       for (c = 0; c < 4; c++)
895         if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
896           res = GNUNET_NO;
897
898       if (res == GNUNET_YES)
899         type = GNUNET_ATS_NET_LAN;
900     }
901     cur = cur->next;
902   }
903
904   /* no local network found for this address, default: WAN */
905   if (type == GNUNET_ATS_NET_UNSPECIFIED)
906     type = GNUNET_ATS_NET_WAN;
907   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
908   ats.value = htonl (type);
909
910   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
911                    "`%s' is in network `%s'\n",
912                    GNUNET_a2s ((const struct sockaddr *) addr, addrlen),
913                    GNUNET_ATS_print_network_type(type));
914   return ats;
915 }
916
917
918 /**
919  * Initialize the ATS subsystem.
920  *
921  * @param cfg configuration to use
922  * @param suggest_cb notification to call whenever the suggestation changed
923  * @param suggest_cb_cls closure for 'suggest_cb'
924  * @return ats context
925  */
926 struct GNUNET_ATS_SchedulingHandle *
927 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
928                             GNUNET_ATS_AddressSuggestionCallback suggest_cb,
929                             void *suggest_cb_cls)
930 {
931   struct GNUNET_ATS_SchedulingHandle *sh;
932
933   sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
934   sh->cfg = cfg;
935   sh->suggest_cb = suggest_cb;
936   sh->suggest_cb_cls = suggest_cb_cls;
937   GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
938   GNUNET_OS_network_interfaces_list(interface_proc, sh);
939   sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
940       get_addresses,
941       sh);
942   reconnect (sh);
943   return sh;
944 }
945
946
947 /**
948  * Client is done with ATS scheduling, release resources.
949  *
950  * @param sh handle to release
951  */
952 void
953 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
954 {
955   struct PendingMessage *p;
956
957   while (NULL != (p = sh->pending_head))
958   {
959     GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
960     GNUNET_free (p);
961   }
962   if (NULL != sh->client)
963   {
964     GNUNET_CLIENT_disconnect (sh->client);
965     sh->client = NULL;
966   }
967   if (GNUNET_SCHEDULER_NO_TASK != sh->task)
968   {
969     GNUNET_SCHEDULER_cancel (sh->task);
970     sh->task = GNUNET_SCHEDULER_NO_TASK;
971   }
972
973   delete_networks (sh);
974   if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
975   {
976     GNUNET_SCHEDULER_cancel(sh->interface_task);
977     sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
978   }
979   GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
980   GNUNET_free (sh);
981   sh = NULL;
982 }
983
984 /**
985  * We would like to reset the address suggestion block time for this
986  * peer
987  *
988  * @param sh handle
989  * @param peer identity of the peer we want to reset
990  */
991 void
992 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
993                           const struct GNUNET_PeerIdentity *peer)
994 {
995   struct PendingMessage *p;
996   struct ResetBackoffMessage *m;
997
998   p = GNUNET_malloc (sizeof (struct PendingMessage) +
999                      sizeof (struct ResetBackoffMessage));
1000   p->size = sizeof (struct ResetBackoffMessage);
1001   p->is_init = GNUNET_NO;
1002   m = (struct ResetBackoffMessage *) &p[1];
1003   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
1004   m->header.size = htons (sizeof (struct ResetBackoffMessage));
1005   m->reserved = htonl (0);
1006   m->peer = *peer;
1007   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1008   do_transmit (sh);
1009 }
1010
1011 /**
1012  * We would like to establish a new connection with a peer.  ATS
1013  * should suggest a good address to begin with.
1014  *
1015  * @param sh handle
1016  * @param peer identity of the peer we need an address for
1017  */
1018 void
1019 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1020                             const struct GNUNET_PeerIdentity *peer)
1021 {
1022   struct PendingMessage *p;
1023   struct RequestAddressMessage *m;
1024
1025   // FIXME: ATS needs to remember this in case of
1026   // a disconnect!
1027   p = GNUNET_malloc (sizeof (struct PendingMessage) +
1028                      sizeof (struct RequestAddressMessage));
1029   p->size = sizeof (struct RequestAddressMessage);
1030   p->is_init = GNUNET_NO;
1031   m = (struct RequestAddressMessage *) &p[1];
1032   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1033   m->header.size = htons (sizeof (struct RequestAddressMessage));
1034   m->reserved = htonl (0);
1035   m->peer = *peer;
1036   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1037   do_transmit (sh);
1038 }
1039
1040
1041 /**
1042  * We would like to stop receiving address updates for this peer
1043  *
1044  * @param sh handle
1045  * @param peer identity of the peer
1046  */
1047 void
1048 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1049                                    const struct GNUNET_PeerIdentity *peer)
1050 {
1051   struct PendingMessage *p;
1052   struct RequestAddressMessage *m;
1053
1054   p = GNUNET_malloc (sizeof (struct PendingMessage) +
1055                      sizeof (struct RequestAddressMessage));
1056   p->size = sizeof (struct RequestAddressMessage);
1057   p->is_init = GNUNET_NO;
1058   m = (struct RequestAddressMessage *) &p[1];
1059   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1060   m->header.size = htons (sizeof (struct RequestAddressMessage));
1061   m->reserved = htonl (0);
1062   m->peer = *peer;
1063   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1064   do_transmit (sh);
1065 }
1066
1067
1068 /**
1069  * We have a new address ATS should know. Addresses have to be added with this
1070  * function before they can be: updated, set in use and destroyed
1071  *
1072  * @param sh handle
1073  * @param address the address
1074  * @param session session handle, can be NULL
1075  * @param ats performance data for the address
1076  * @param ats_count number of performance records in 'ats'
1077  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1078  */
1079 int
1080 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1081                         const struct GNUNET_HELLO_Address *address,
1082                         struct Session *session,
1083                         const struct GNUNET_ATS_Information *ats,
1084                         uint32_t ats_count)
1085 {
1086
1087   struct PendingMessage *p;
1088   struct AddressUpdateMessage *m;
1089   struct GNUNET_ATS_Information *am;
1090   char *pm;
1091   size_t namelen;
1092   size_t msize;
1093   uint32_t s = 0;
1094
1095   if (address == NULL)
1096   {
1097     GNUNET_break (0);
1098     return GNUNET_SYSERR;
1099   }
1100
1101   namelen =
1102       (address->transport_name ==
1103        NULL) ? 0 : strlen (address->transport_name) + 1;
1104   msize =
1105       sizeof (struct AddressUpdateMessage) + address->address_length +
1106       ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1107   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1108       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1109       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1110       (ats_count >=
1111        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1112   {
1113     GNUNET_break (0);
1114     return GNUNET_SYSERR;
1115   }
1116
1117   if (NULL != session)
1118   {
1119     s = find_session_id (sh, session, &address->peer);
1120     if (NOT_FOUND != s)
1121     {
1122       /* Already existing, nothing todo */
1123       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1124                   "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1125                   GNUNET_i2s (&address->peer),
1126                   address->transport_name, session, s);
1127       return GNUNET_SYSERR;
1128     }
1129     s = find_empty_session_slot (sh, session, &address->peer);
1130     GNUNET_break (NOT_FOUND != s);
1131   }
1132
1133   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1134   p->size = msize;
1135   p->is_init = GNUNET_NO;
1136   m = (struct AddressUpdateMessage *) &p[1];
1137   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1138   m->header.size = htons (msize);
1139   m->ats_count = htonl (ats_count);
1140   m->peer = address->peer;
1141   m->address_length = htons (address->address_length);
1142   m->plugin_name_length = htons (namelen);
1143   m->session_id = htonl (s);
1144
1145   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1146               "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1147               GNUNET_i2s (&address->peer),
1148               address->transport_name, session, s);
1149
1150   am = (struct GNUNET_ATS_Information *) &m[1];
1151   memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1152   pm = (char *) &am[ats_count];
1153   memcpy (pm, address->address, address->address_length);
1154   if (NULL != address->transport_name)
1155         memcpy (&pm[address->address_length], address->transport_name, namelen);
1156   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1157   do_transmit (sh);
1158   return GNUNET_OK;
1159
1160 }
1161
1162
1163 /**
1164  * We have updated performance statistics for a given address.  Note
1165  * that this function can be called for addresses that are currently
1166  * in use as well as addresses that are valid but not actively in use.
1167  * Furthermore, the peer may not even be connected to us right now (in
1168  * which case the call may be ignored or the information may be stored
1169  * for later use).  Update bandwidth assignments.
1170  *
1171  * @param sh handle
1172  * @param address the address
1173  * @param session session handle, can be NULL
1174  * @param ats performance data for the address
1175  * @param ats_count number of performance records in 'ats'
1176  */
1177 void
1178 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1179                            const struct GNUNET_HELLO_Address *address,
1180                            struct Session *session,
1181                            const struct GNUNET_ATS_Information *ats,
1182                            uint32_t ats_count)
1183 {
1184   struct PendingMessage *p;
1185   struct AddressUpdateMessage *m;
1186   struct GNUNET_ATS_Information *am;
1187   char *pm;
1188   size_t namelen;
1189   size_t msize;
1190   uint32_t s = 0;
1191
1192   if (address == NULL)
1193   {
1194     GNUNET_break (0);
1195     return;
1196   }
1197
1198   namelen =
1199       (address->transport_name ==
1200        NULL) ? 0 : strlen (address->transport_name) + 1;
1201   msize =
1202       sizeof (struct AddressUpdateMessage) + address->address_length +
1203       ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1204   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1205       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1206       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1207       (ats_count >=
1208        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1209   {
1210     GNUNET_break (0);
1211     return;
1212   }
1213
1214   if (NULL != session)
1215   {
1216     s = find_session_id (sh, session, &address->peer);
1217     if (NOT_FOUND == s)
1218     {
1219       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1220                   "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1221                   GNUNET_i2s (&address->peer),
1222                   address->transport_name, session, s);
1223
1224       GNUNET_break (0);
1225       return;
1226     }
1227   }
1228
1229   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1230   p->size = msize;
1231   p->is_init = GNUNET_NO;
1232   m = (struct AddressUpdateMessage *) &p[1];
1233   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1234   m->header.size = htons (msize);
1235   m->ats_count = htonl (ats_count);
1236   m->peer = address->peer;
1237   m->address_length = htons (address->address_length);
1238   m->plugin_name_length = htons (namelen);
1239
1240   m->session_id = htonl (s);
1241
1242   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1243               "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1244               GNUNET_i2s (&address->peer),
1245               address->transport_name, session, s);
1246
1247   am = (struct GNUNET_ATS_Information *) &m[1];
1248   memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1249   pm = (char *) &am[ats_count];
1250   memcpy (pm, address->address, address->address_length);
1251   memcpy (&pm[address->address_length], address->transport_name, namelen);
1252   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1253   do_transmit (sh);
1254   return;
1255 }
1256
1257
1258 /**
1259  * An address is now in use or not used any more.
1260  *
1261  * @param sh handle
1262  * @param address the address
1263  * @param session session handle, can be NULL
1264  * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1265  * if address is not used any more
1266  */
1267 void
1268 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1269                            const struct GNUNET_HELLO_Address *address,
1270                            struct Session *session, int in_use)
1271 {
1272   struct PendingMessage *p;
1273   struct AddressUseMessage *m;
1274   char *pm;
1275   size_t namelen;
1276   size_t msize;
1277   uint32_t s = 0;
1278
1279   GNUNET_assert (NULL != address);
1280   namelen =
1281       (address->transport_name ==
1282        NULL) ? 0 : strlen (address->transport_name) + 1;
1283   msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1284   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1285       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1286       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1287   {
1288     GNUNET_break (0);
1289     return;
1290   }
1291
1292   if (session != NULL)
1293   {
1294     s = find_session_id (sh, session, &address->peer);
1295     if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1296     {
1297       /* trying to set unknown address to NO */
1298       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1299                   "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1300                   GNUNET_i2s (&address->peer), address->transport_name, session);
1301       GNUNET_break (0);
1302       return;
1303     }
1304     if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1305     {
1306       /* trying to set new address to YES */
1307       s = find_empty_session_slot (sh, session, &address->peer);
1308       GNUNET_assert (NOT_FOUND != s);
1309     }
1310   }
1311
1312   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1313   p->size = msize;
1314   p->is_init = GNUNET_NO;
1315   m = (struct AddressUseMessage *) &p[1];
1316   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1317   m->header.size = htons (msize);
1318   m->peer = address->peer;
1319   m->in_use = htons (in_use);
1320   m->address_length = htons (address->address_length);
1321   m->plugin_name_length = htons (namelen);
1322
1323   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324               "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1325               (GNUNET_YES == in_use) ? "YES" : "NO",
1326               GNUNET_i2s (&address->peer), address->transport_name, session);
1327
1328   m->session_id = htonl (s);
1329   pm = (char *) &m[1];
1330   memcpy (pm, address->address, address->address_length);
1331   memcpy (&pm[address->address_length], address->transport_name, namelen);
1332   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1333   do_transmit (sh);
1334   return;
1335 }
1336
1337
1338 /**
1339  * A session got destroyed, stop including it as a valid address.
1340  *
1341  * @param sh handle
1342  * @param address the address
1343  * @param session session handle that is no longer valid, can be NULL
1344  */
1345 void
1346 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1347                               const struct GNUNET_HELLO_Address *address,
1348                               struct Session *session)
1349 {
1350   struct PendingMessage *p;
1351   struct AddressDestroyedMessage *m;
1352   char *pm;
1353   size_t namelen;
1354   size_t msize;
1355   uint32_t s = 0;
1356
1357   if (address == NULL)
1358   {
1359     GNUNET_break (0);
1360     return;
1361   }
1362
1363   GNUNET_assert (address->transport_name != NULL);
1364   namelen = strlen (address->transport_name) + 1;
1365   GNUNET_assert (namelen > 1);
1366   msize =
1367       sizeof (struct AddressDestroyedMessage) + address->address_length +
1368       namelen;
1369   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1370       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1371       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1372   {
1373     GNUNET_break (0);
1374     return;
1375   }
1376
1377   s = find_session_id (sh, session, &address->peer);
1378   if ((NULL != session) && (NOT_FOUND == s))
1379   {
1380     /* trying to delete unknown address */
1381     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382                 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1383                 GNUNET_i2s (&address->peer), address->transport_name, session);
1384     return;
1385   }
1386
1387   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1388   p->size = msize;
1389   p->is_init = GNUNET_NO;
1390   m = (struct AddressDestroyedMessage *) &p[1];
1391   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1392   m->header.size = htons (msize);
1393   m->reserved = htonl (0);
1394   m->peer = address->peer;
1395   m->address_length = htons (address->address_length);
1396   m->plugin_name_length = htons (namelen);
1397
1398   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1399               "Deleting address for peer `%s', plugin `%s', session %p\n",
1400               GNUNET_i2s (&address->peer), address->transport_name, session);
1401
1402   m->session_id = htonl (s);
1403   pm = (char *) &m[1];
1404   memcpy (pm, address->address, address->address_length);
1405   memcpy (&pm[address->address_length], address->transport_name, namelen);
1406   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1407   do_transmit (sh);
1408   remove_session (sh, s, &address->peer);
1409 }
1410
1411 /* end of ats_api_scheduling.c */