- cleanup
[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   return sh->session_array[session_id].session;
350 }
351
352
353 /**
354  * Get an available session ID for the given session object.
355  *
356  * @param sh our handle
357  * @param session session object
358  * @param peer peer the session belongs to
359  * @return the session id
360  */
361 static uint32_t
362 find_empty_session_slot (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
363                 const struct GNUNET_PeerIdentity *peer)
364 {
365   unsigned int i;
366   unsigned int f;
367
368   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
369               "Get session ID for session %p from peer %s in %p\n", session,
370               GNUNET_i2s (peer), sh);
371
372   if (NULL == session)
373     return NOT_FOUND;
374   f = 0;
375   for (i = 1; i < sh->session_array_size; i++)
376   {
377     if ((f == 0) && (sh->session_array[i].slot_used == GNUNET_NO))
378       f = i;
379   }
380   if (f == 0)
381   {
382     f = sh->session_array_size;
383     GNUNET_array_grow (sh->session_array, sh->session_array_size,
384                        sh->session_array_size * 2);
385   }
386   GNUNET_assert (f > 0);
387   sh->session_array[f].session = session;
388   sh->session_array[f].peer = *peer;
389   sh->session_array[f].slot_used = GNUNET_YES;
390
391   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
392               "Assigning session ID %u for session %p of peer %s in %p\n", f,
393               session, GNUNET_i2s (peer), sh);
394
395   return f;
396 }
397
398
399 /**
400  * Get the ID for the given session object.
401  *
402  * @param sh our handle
403  * @param session session object
404  * @param peer peer the session belongs to
405  * @return the session id or NOT_FOUND for error
406  */
407 static uint32_t
408 find_session_id (struct GNUNET_ATS_SchedulingHandle *sh, struct Session *session,
409                 const struct GNUNET_PeerIdentity *peer)
410 {
411   unsigned int i;
412   unsigned int f;
413   char * p2;
414
415   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
416               "Get session ID for session %p from peer %s in %p\n", session,
417               GNUNET_i2s (peer), sh);
418
419   if (NULL == session)
420     return NOT_FOUND;
421   f = 0;
422   for (i = 1; i < sh->session_array_size; i++)
423   {
424     if (session == sh->session_array[i].session)
425     {
426       if (0 != memcmp (peer, &sh->session_array[i].peer,
427                        sizeof (struct GNUNET_PeerIdentity)))
428       {
429         p2 = strdup (GNUNET_i2s (&sh->session_array[i].peer));
430         GNUNET_log_from (GNUNET_ERROR_TYPE_ERROR, "ats-scheduling-api",
431                     "Session %p did not match: old session was for peer `%s' new session is for `%s'\n",
432                     session, GNUNET_i2s (peer), p2);
433         GNUNET_free (p2);
434         return NOT_FOUND;
435       }
436       return i;
437     }
438   }
439   return NOT_FOUND;
440 }
441
442
443 /**
444  * Remove the session of the given session ID from the session
445  * table (it is no longer valid).
446  *
447  * @param sh our handle
448  * @param session_id identifies session that is no longer valid
449  * @param peer peer the session belongs to
450  */
451 static void
452 remove_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
453                 const struct GNUNET_PeerIdentity *peer)
454 {
455   GNUNET_assert (peer != NULL);
456   GNUNET_assert (sh != NULL);
457
458   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
459               "Release sessionID %u from peer %s in %p\n",
460               (unsigned int) session_id, GNUNET_i2s (peer), sh);
461
462   if (0 == session_id)
463     return;
464
465   GNUNET_assert (session_id < sh->session_array_size);
466   GNUNET_assert (GNUNET_YES == sh->session_array[session_id].slot_used);
467   GNUNET_assert (0 == memcmp (peer,
468                               &sh->session_array[session_id].peer,
469                               sizeof (struct GNUNET_PeerIdentity)));
470   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
471               "Session %p for peer `%s' removed from slot %u \n",
472               sh->session_array[session_id].session,
473               GNUNET_i2s (peer),
474               session_id);
475   sh->session_array[session_id].session = NULL;
476
477 }
478
479
480 /**
481  * Release the session slot from the session table (ATS service is
482  * also done using it).
483  *
484  * @param sh our handle
485  * @param session_id identifies session that is no longer valid
486  * @param peer peer the session belongs to
487  */
488 static void
489 release_session (struct GNUNET_ATS_SchedulingHandle *sh, uint32_t session_id,
490                  const struct GNUNET_PeerIdentity *peer)
491 {
492
493   GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
494               "Release sessionID %u from peer %s in %p\n",
495               (unsigned int) session_id, GNUNET_i2s (peer), sh);
496
497   if (session_id >= sh->session_array_size)
498   {
499     GNUNET_break (0);
500     sh->reconnect = GNUNET_YES;
501     return;
502   }
503
504   /* this slot should have been removed from remove_session before */
505   GNUNET_assert (sh->session_array[session_id].session == NULL);
506
507   if (0 !=
508       memcmp (peer, &sh->session_array[session_id].peer,
509               sizeof (struct GNUNET_PeerIdentity)))
510   {
511     GNUNET_break (0);
512     sh->reconnect = GNUNET_YES;
513     return;
514   }
515   sh->session_array[session_id].slot_used = GNUNET_NO;
516   memset (&sh->session_array[session_id].peer, 0,
517           sizeof (struct GNUNET_PeerIdentity));
518 }
519
520
521 static void
522 process_release_message (struct GNUNET_ATS_SchedulingHandle *sh,
523                          const struct SessionReleaseMessage *srm)
524 {
525   release_session (sh, ntohl (srm->session_id), &srm->peer);
526 }
527
528
529 /**
530  * Type of a function to call when we receive a message
531  * from the service.
532  *
533  * @param cls the 'struct GNUNET_ATS_SchedulingHandle'
534  * @param msg message received, NULL on timeout or fatal error
535  */
536 static void
537 process_ats_message (void *cls, const struct GNUNET_MessageHeader *msg)
538 {
539   struct GNUNET_ATS_SchedulingHandle *sh = cls;
540   const struct AddressSuggestionMessage *m;
541   const struct GNUNET_ATS_Information *atsi;
542   const char *plugin_address;
543   const char *plugin_name;
544   uint16_t plugin_address_length;
545   uint16_t plugin_name_length;
546   uint32_t ats_count;
547   struct GNUNET_HELLO_Address address;
548   struct Session *s;
549
550   if (NULL == msg)
551   {
552     force_reconnect (sh);
553     return;
554   }
555   if ((ntohs (msg->type) == GNUNET_MESSAGE_TYPE_ATS_SESSION_RELEASE) &&
556       (ntohs (msg->size) == sizeof (struct SessionReleaseMessage)))
557   {
558     process_release_message (sh, (const struct SessionReleaseMessage *) msg);
559     GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
560                            GNUNET_TIME_UNIT_FOREVER_REL);
561     if (GNUNET_YES == sh->reconnect)
562       force_reconnect (sh);
563     return;
564   }
565   if ((ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ATS_ADDRESS_SUGGESTION) ||
566       (ntohs (msg->size) <= sizeof (struct AddressSuggestionMessage)))
567   {
568     GNUNET_break (0);
569     force_reconnect (sh);
570     return;
571   }
572   m = (const struct AddressSuggestionMessage *) msg;
573   ats_count = ntohl (m->ats_count);
574   plugin_address_length = ntohs (m->address_length);
575   atsi = (const struct GNUNET_ATS_Information *) &m[1];
576   plugin_address = (const char *) &atsi[ats_count];
577   plugin_name = &plugin_address[plugin_address_length];
578   plugin_name_length = ntohs (m->plugin_name_length);
579   if ((plugin_address_length + plugin_name_length +
580        ats_count * sizeof (struct GNUNET_ATS_Information) +
581        sizeof (struct AddressSuggestionMessage) != ntohs (msg->size)) ||
582       (ats_count >
583        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information))
584       || (plugin_name[plugin_name_length - 1] != '\0'))
585   {
586     GNUNET_break (0);
587     force_reconnect (sh);
588     return;
589   }
590   uint32_t session_id = ntohl (m->session_id);
591
592   if (session_id == 0)
593     s = NULL;
594   else
595   {
596     s = find_session (sh, session_id, &m->peer);
597     if (s == NULL)
598     {
599
600       GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
601                   "ATS tries to use outdated session `%s'\n",
602                   GNUNET_i2s (&m->peer));
603       GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
604                              GNUNET_TIME_UNIT_FOREVER_REL);
605       return;
606     }
607   }
608   address.peer = m->peer;
609   address.address = plugin_address;
610   address.address_length = plugin_address_length;
611   address.transport_name = plugin_name;
612
613   if ((s == NULL) && (0 == address.address_length))
614   {
615     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
616                 "ATS returned invalid address for peer `%s' transport `%s' address length %i, session_id %i\n",
617                 GNUNET_i2s (&address.peer), address.transport_name,
618                 plugin_address_length, session_id);
619     GNUNET_break_op (0);
620     GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
621                            GNUNET_TIME_UNIT_FOREVER_REL);
622     return;
623   }
624
625   sh->suggest_cb (sh->suggest_cb_cls, &address, s, m->bandwidth_out,
626                   m->bandwidth_in, atsi, ats_count);
627
628   GNUNET_CLIENT_receive (sh->client, &process_ats_message, sh,
629                          GNUNET_TIME_UNIT_FOREVER_REL);
630   if (GNUNET_YES == sh->reconnect)
631     force_reconnect (sh);
632 }
633
634
635 /**
636  * Re-establish the connection to the ATS service.
637  *
638  * @param sh handle to use to re-connect.
639  */
640 static void
641 reconnect (struct GNUNET_ATS_SchedulingHandle *sh)
642 {
643   struct PendingMessage *p;
644   struct ClientStartMessage *init;
645
646   GNUNET_assert (NULL == sh->client);
647   sh->client = GNUNET_CLIENT_connect ("ats", sh->cfg);
648   GNUNET_assert (NULL != sh->client);
649   if ((NULL == (p = sh->pending_head)) || (GNUNET_YES != p->is_init))
650   {
651     p = GNUNET_malloc (sizeof (struct PendingMessage) +
652                        sizeof (struct ClientStartMessage));
653     p->size = sizeof (struct ClientStartMessage);
654     p->is_init = GNUNET_YES;
655     init = (struct ClientStartMessage *) &p[1];
656     init->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_START);
657     init->header.size = htons (sizeof (struct ClientStartMessage));
658     init->start_flag = htonl (START_FLAG_SCHEDULING);
659     GNUNET_CONTAINER_DLL_insert (sh->pending_head, sh->pending_tail, p);
660   }
661   do_transmit (sh);
662 }
663
664
665 /**
666  * delete the current network list
667  */
668 static void
669 delete_networks (struct GNUNET_ATS_SchedulingHandle *sh)
670 {
671   struct ATS_Network * cur = sh->net_head;
672   while (cur != NULL)
673   {
674     GNUNET_CONTAINER_DLL_remove(sh->net_head, sh->net_tail, cur);
675     GNUNET_free (cur);
676     cur = sh->net_head;
677   }
678 }
679
680
681 static int
682 interface_proc (void *cls, const char *name,
683                 int isDefault,
684                 const struct sockaddr *
685                 addr,
686                 const struct sockaddr *
687                 broadcast_addr,
688                 const struct sockaddr *
689                 netmask, socklen_t addrlen)
690 {
691   struct GNUNET_ATS_SchedulingHandle * sh = cls;
692   /* Calculate network */
693   struct ATS_Network *net = NULL;
694
695   /* Skipping IPv4 loopback addresses since we have special check  */
696   if  (addr->sa_family == AF_INET)
697   {
698     struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
699
700     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
701        return GNUNET_OK;
702   }
703   /* Skipping IPv6 loopback addresses since we have special check  */
704   if  (addr->sa_family == AF_INET6)
705   {
706     struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
707     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
708       return GNUNET_OK;
709   }
710
711   if (addr->sa_family == AF_INET)
712   {
713     struct sockaddr_in *addr4 = (struct sockaddr_in *) addr;
714     struct sockaddr_in *netmask4 = (struct sockaddr_in *) netmask;
715     struct sockaddr_in *tmp = NULL;
716     struct sockaddr_in network4;
717
718     net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in));
719     tmp = (struct sockaddr_in *) &net[1];
720     net->network = (struct sockaddr *) &tmp[0];
721     net->netmask = (struct sockaddr *) &tmp[1];
722     net->length = addrlen;
723
724     memset (&network4, 0, sizeof (network4));
725     network4.sin_family = AF_INET;
726 #if HAVE_SOCKADDR_IN_SIN_LEN
727     network4.sin_len = sizeof (network4);
728 #endif
729     network4.sin_addr.s_addr = (addr4->sin_addr.s_addr & netmask4->sin_addr.s_addr);
730
731     memcpy (net->netmask, netmask4, sizeof (struct sockaddr_in));
732     memcpy (net->network, &network4, sizeof (struct sockaddr_in));
733   }
734
735   if (addr->sa_family == AF_INET6)
736   {
737     struct sockaddr_in6 *addr6 = (struct sockaddr_in6 *) addr;
738     struct sockaddr_in6 *netmask6 = (struct sockaddr_in6 *) netmask;
739     struct sockaddr_in6 * tmp = NULL;
740     struct sockaddr_in6 network6;
741
742     net = GNUNET_malloc(sizeof (struct ATS_Network) + 2 * sizeof (struct sockaddr_in6));
743     tmp = (struct sockaddr_in6 *) &net[1];
744     net->network = (struct sockaddr *) &tmp[0];
745     net->netmask = (struct sockaddr *) &tmp[1];
746     net->length = addrlen;
747
748     memset (&network6, 0, sizeof (network6));
749     network6.sin6_family = AF_INET6;
750 #if HAVE_SOCKADDR_IN_SIN_LEN
751     network6.sin6_len = sizeof (network6);
752 #endif
753     int c = 0;
754     uint32_t *addr_elem = (uint32_t *) &addr6->sin6_addr;
755     uint32_t *mask_elem = (uint32_t *) &netmask6->sin6_addr;
756     uint32_t *net_elem = (uint32_t *) &network6.sin6_addr;
757     for (c = 0; c < 4; c++)
758       net_elem[c] = addr_elem[c] & mask_elem[c];
759
760     memcpy (net->netmask, netmask6, sizeof (struct sockaddr_in6));
761     memcpy (net->network, &network6, sizeof (struct sockaddr_in6));
762   }
763
764   /* Store in list */
765   if (net != NULL)
766   {
767 #if VERBOSE_ATS
768     char * netmask = GNUNET_strdup (GNUNET_a2s((struct sockaddr *) net->netmask, addrlen));
769     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Adding network `%s', netmask `%s'\n",
770         GNUNET_a2s((struct sockaddr *) net->network, addrlen),
771         netmask);
772     GNUNET_free (netmask);
773 # endif
774     GNUNET_CONTAINER_DLL_insert(sh->net_head, sh->net_tail, net);
775   }
776   return GNUNET_OK;
777 }
778
779
780 /**
781  * Periodically get list of addresses
782  * @param cls closure
783  * @param tc Task context
784  */
785 static void
786 get_addresses (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
787 {
788   struct GNUNET_ATS_SchedulingHandle * sh = cls;
789   sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
790   delete_networks (sh);
791   GNUNET_OS_network_interfaces_list(interface_proc, sh);
792   sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
793                                                      get_addresses,
794                                                      sh);
795 }
796
797
798 /**
799  * Returns where the address is located: LAN or WAN or ...
800  *
801  * @param sh the scheduling handle
802  * @param addr address
803  * @param addrlen address length
804  * @return location as GNUNET_ATS_Information
805  */
806 struct GNUNET_ATS_Information
807 GNUNET_ATS_address_get_type (struct GNUNET_ATS_SchedulingHandle * sh, const struct sockaddr * addr, socklen_t addrlen)
808 {
809   GNUNET_assert (sh != NULL);
810   struct GNUNET_ATS_Information ats;
811   struct ATS_Network * cur = sh->net_head;
812   int type = GNUNET_ATS_NET_UNSPECIFIED;
813
814   if  (addr->sa_family == AF_UNIX)
815   {
816     type = GNUNET_ATS_NET_LOOPBACK;
817   }
818
819   /* IPv4 loopback check */
820   if  (addr->sa_family == AF_INET)
821   {
822     struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
823
824     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
825       type = GNUNET_ATS_NET_LOOPBACK;
826   }
827   /* IPv6 loopback check */
828   if  (addr->sa_family == AF_INET6)
829   {
830     struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
831     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
832       type = GNUNET_ATS_NET_LOOPBACK;
833   }
834
835   /* Check local networks */
836   while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
837   {
838     if (addrlen != cur->length)
839     {
840       cur = cur->next;
841       continue;
842     }
843
844     if (addr->sa_family == AF_INET)
845     {
846       struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
847       struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
848       struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
849
850       if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
851       {
852         char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
853         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
854             "`%s' is in network `%s'\n",
855             GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
856             net);
857         GNUNET_free (net);
858         type = GNUNET_ATS_NET_LAN;
859       }
860     }
861     if (addr->sa_family == AF_INET6)
862     {
863       struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
864       struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
865       struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
866
867       int res = GNUNET_YES;
868       int c = 0;
869       uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
870       uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
871       uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
872       for (c = 0; c < 4; c++)
873         if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
874           res = GNUNET_NO;
875
876       if (res == GNUNET_YES)
877       {
878         char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
879         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
880               GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
881               net);
882         GNUNET_free (net);
883         type = GNUNET_ATS_NET_LAN;
884       }
885     }
886     cur = cur->next;
887   }
888
889   /* no local network found for this address, default: WAN */
890   if (type == GNUNET_ATS_NET_UNSPECIFIED)
891     type = GNUNET_ATS_NET_WAN;
892   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
893   ats.value = htonl (type);
894   return (const struct GNUNET_ATS_Information) ats;
895 }
896
897
898 /**
899  * Initialize the ATS subsystem.
900  *
901  * @param cfg configuration to use
902  * @param suggest_cb notification to call whenever the suggestation changed
903  * @param suggest_cb_cls closure for 'suggest_cb'
904  * @return ats context
905  */
906 struct GNUNET_ATS_SchedulingHandle *
907 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
908                             GNUNET_ATS_AddressSuggestionCallback suggest_cb,
909                             void *suggest_cb_cls)
910 {
911   struct GNUNET_ATS_SchedulingHandle *sh;
912
913   sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
914   sh->cfg = cfg;
915   sh->suggest_cb = suggest_cb;
916   sh->suggest_cb_cls = suggest_cb_cls;
917   GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
918   GNUNET_OS_network_interfaces_list(interface_proc, sh);
919   sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
920       get_addresses,
921       sh);
922   reconnect (sh);
923   return sh;
924 }
925
926
927 /**
928  * Client is done with ATS scheduling, release resources.
929  *
930  * @param sh handle to release
931  */
932 void
933 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
934 {
935   struct PendingMessage *p;
936
937   while (NULL != (p = sh->pending_head))
938   {
939     GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
940     GNUNET_free (p);
941   }
942   if (NULL != sh->client)
943   {
944     GNUNET_CLIENT_disconnect (sh->client);
945     sh->client = NULL;
946   }
947   if (GNUNET_SCHEDULER_NO_TASK != sh->task)
948   {
949     GNUNET_SCHEDULER_cancel (sh->task);
950     sh->task = GNUNET_SCHEDULER_NO_TASK;
951   }
952
953   delete_networks (sh);
954   if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
955   {
956     GNUNET_SCHEDULER_cancel(sh->interface_task);
957     sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
958   }
959   GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
960   GNUNET_free (sh);
961   sh = NULL;
962 }
963
964 /**
965  * We would like to reset the address suggestion block time for this
966  * peer
967  *
968  * @param sh handle
969  * @param peer identity of the peer we want to reset
970  */
971 void
972 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
973                           const struct GNUNET_PeerIdentity *peer)
974 {
975   struct PendingMessage *p;
976   struct ResetBackoffMessage *m;
977
978   p = GNUNET_malloc (sizeof (struct PendingMessage) +
979                      sizeof (struct ResetBackoffMessage));
980   p->size = sizeof (struct ResetBackoffMessage);
981   p->is_init = GNUNET_NO;
982   m = (struct ResetBackoffMessage *) &p[1];
983   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
984   m->header.size = htons (sizeof (struct ResetBackoffMessage));
985   m->reserved = htonl (0);
986   m->peer = *peer;
987   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
988   do_transmit (sh);
989 }
990
991 /**
992  * We would like to establish a new connection with a peer.  ATS
993  * should suggest a good address to begin with.
994  *
995  * @param sh handle
996  * @param peer identity of the peer we need an address for
997  */
998 void
999 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1000                             const struct GNUNET_PeerIdentity *peer)
1001 {
1002   struct PendingMessage *p;
1003   struct RequestAddressMessage *m;
1004
1005   // FIXME: ATS needs to remember this in case of
1006   // a disconnect!
1007   p = GNUNET_malloc (sizeof (struct PendingMessage) +
1008                      sizeof (struct RequestAddressMessage));
1009   p->size = sizeof (struct RequestAddressMessage);
1010   p->is_init = GNUNET_NO;
1011   m = (struct RequestAddressMessage *) &p[1];
1012   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1013   m->header.size = htons (sizeof (struct RequestAddressMessage));
1014   m->reserved = htonl (0);
1015   m->peer = *peer;
1016   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1017   do_transmit (sh);
1018 }
1019
1020
1021 /**
1022  * We would like to stop receiving address updates for this peer
1023  *
1024  * @param sh handle
1025  * @param peer identity of the peer
1026  */
1027 void
1028 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1029                                    const struct GNUNET_PeerIdentity *peer)
1030 {
1031   struct PendingMessage *p;
1032   struct RequestAddressMessage *m;
1033
1034   p = GNUNET_malloc (sizeof (struct PendingMessage) +
1035                      sizeof (struct RequestAddressMessage));
1036   p->size = sizeof (struct RequestAddressMessage);
1037   p->is_init = GNUNET_NO;
1038   m = (struct RequestAddressMessage *) &p[1];
1039   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1040   m->header.size = htons (sizeof (struct RequestAddressMessage));
1041   m->reserved = htonl (0);
1042   m->peer = *peer;
1043   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1044   do_transmit (sh);
1045 }
1046
1047
1048 /**
1049  * We have a new address ATS should know. Addresses have to be added with this
1050  * function before they can be: updated, set in use and destroyed
1051  *
1052  * @param sh handle
1053  * @param address the address
1054  * @param session session handle (if available)
1055  * @param ats performance data for the address
1056  * @param ats_count number of performance records in 'ats'
1057  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1058  */
1059 int
1060 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1061                         const struct GNUNET_HELLO_Address *address,
1062                         struct Session *session,
1063                         const struct GNUNET_ATS_Information *ats,
1064                         uint32_t ats_count)
1065 {
1066
1067   struct PendingMessage *p;
1068   struct AddressUpdateMessage *m;
1069   struct GNUNET_ATS_Information *am;
1070   char *pm;
1071   size_t namelen;
1072   size_t msize;
1073   uint32_t s = 0;
1074
1075   if (address == NULL)
1076   {
1077     GNUNET_break (0);
1078     return GNUNET_SYSERR;
1079   }
1080   if ((address == NULL) && (session == NULL))
1081   {
1082     GNUNET_break (0);
1083     return GNUNET_SYSERR;
1084   }
1085
1086   namelen =
1087       (address->transport_name ==
1088        NULL) ? 0 : strlen (address->transport_name) + 1;
1089   msize =
1090       sizeof (struct AddressUpdateMessage) + address->address_length +
1091       ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1092   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1093       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1094       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1095       (ats_count >=
1096        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1097   {
1098     GNUNET_break (0);
1099     return GNUNET_SYSERR;
1100   }
1101
1102   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1103   p->size = msize;
1104   p->is_init = GNUNET_NO;
1105   m = (struct AddressUpdateMessage *) &p[1];
1106   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1107   m->header.size = htons (msize);
1108   m->ats_count = htonl (ats_count);
1109   m->peer = address->peer;
1110   m->address_length = htons (address->address_length);
1111   m->plugin_name_length = htons (namelen);
1112   if (NULL != session)
1113   {
1114     s = find_session_id (sh, session, &address->peer);
1115     if (NOT_FOUND != s)
1116     {
1117       /* Already existing, nothing todo */
1118       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119                   "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1120                   GNUNET_i2s (&address->peer),
1121                   address->transport_name, session, s);
1122       return GNUNET_SYSERR;
1123     }
1124     s = find_empty_session_slot (sh, session, &address->peer);
1125     GNUNET_break (NOT_FOUND != s);
1126   }
1127   m->session_id = htonl (s);
1128
1129   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1130               "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1131               GNUNET_i2s (&address->peer),
1132               address->transport_name, session, s);
1133
1134   am = (struct GNUNET_ATS_Information *) &m[1];
1135   memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1136   pm = (char *) &am[ats_count];
1137   memcpy (pm, address->address, address->address_length);
1138   memcpy (&pm[address->address_length], address->transport_name, namelen);
1139   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1140   do_transmit (sh);
1141   return GNUNET_OK;
1142
1143 }
1144
1145
1146 /**
1147  * We have updated performance statistics for a given address.  Note
1148  * that this function can be called for addresses that are currently
1149  * in use as well as addresses that are valid but not actively in use.
1150  * Furthermore, the peer may not even be connected to us right now (in
1151  * which case the call may be ignored or the information may be stored
1152  * for later use).  Update bandwidth assignments.
1153  *
1154  * @param sh handle
1155  * @param address the address
1156  * @param session session handle (if available)
1157  * @param ats performance data for the address
1158  * @param ats_count number of performance records in 'ats'
1159  */
1160 void
1161 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1162                            const struct GNUNET_HELLO_Address *address,
1163                            struct Session *session,
1164                            const struct GNUNET_ATS_Information *ats,
1165                            uint32_t ats_count)
1166 {
1167   struct PendingMessage *p;
1168   struct AddressUpdateMessage *m;
1169   struct GNUNET_ATS_Information *am;
1170   char *pm;
1171   size_t namelen;
1172   size_t msize;
1173   uint32_t s = 0;
1174
1175   if (address == NULL)
1176   {
1177     GNUNET_break (0);
1178     return;
1179   }
1180   if ((address == NULL) && (session == NULL))
1181   {
1182     GNUNET_break (0);
1183     return;
1184   }
1185
1186   namelen =
1187       (address->transport_name ==
1188        NULL) ? 0 : strlen (address->transport_name) + 1;
1189   msize =
1190       sizeof (struct AddressUpdateMessage) + address->address_length +
1191       ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1192   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1193       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1194       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1195       (ats_count >=
1196        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1197   {
1198     GNUNET_break (0);
1199     return;
1200   }
1201
1202   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1203   p->size = msize;
1204   p->is_init = GNUNET_NO;
1205   m = (struct AddressUpdateMessage *) &p[1];
1206   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1207   m->header.size = htons (msize);
1208   m->ats_count = htonl (ats_count);
1209   m->peer = address->peer;
1210   m->address_length = htons (address->address_length);
1211   m->plugin_name_length = htons (namelen);
1212   if (NULL != session)
1213   {
1214     s = find_session_id (sh, session, &address->peer);
1215     if (NOT_FOUND == s)
1216     {
1217       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1218                   "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1219                   GNUNET_i2s (&address->peer),
1220                   address->transport_name, session, s);
1221
1222       GNUNET_break (0);
1223       return;
1224     }
1225   }
1226   m->session_id = htonl (s);
1227
1228   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1229               "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1230               GNUNET_i2s (&address->peer),
1231               address->transport_name, session, s);
1232
1233   am = (struct GNUNET_ATS_Information *) &m[1];
1234   memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1235   pm = (char *) &am[ats_count];
1236   memcpy (pm, address->address, address->address_length);
1237   memcpy (&pm[address->address_length], address->transport_name, namelen);
1238   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1239   do_transmit (sh);
1240   return;
1241 }
1242
1243
1244 /**
1245  * An address is now in use or not used any more.
1246  *
1247  * @param sh handle
1248  * @param address the address
1249  * @param session session handle
1250  * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1251  * if address is not used any more
1252  */
1253 void
1254 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1255                            const struct GNUNET_HELLO_Address *address,
1256                            struct Session *session, int in_use)
1257 {
1258   struct PendingMessage *p;
1259   struct AddressUseMessage *m;
1260   char *pm;
1261   size_t namelen;
1262   size_t msize;
1263   uint32_t s = 0;
1264
1265   GNUNET_assert (NULL != address);
1266   namelen =
1267       (address->transport_name ==
1268        NULL) ? 0 : strlen (address->transport_name) + 1;
1269   msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1270   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1271       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1272       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1273   {
1274     GNUNET_break (0);
1275     return;
1276   }
1277
1278   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1279   p->size = msize;
1280   p->is_init = GNUNET_NO;
1281   m = (struct AddressUseMessage *) &p[1];
1282   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1283   m->header.size = htons (msize);
1284   m->peer = address->peer;
1285   m->in_use = htons (in_use);
1286   m->address_length = htons (address->address_length);
1287   m->plugin_name_length = htons (namelen);
1288   if (session != NULL)
1289   {
1290     s = find_session_id (sh, session, &address->peer);
1291     if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1292     {
1293       /* trying to set unknown address to NO */
1294       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1295                   "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1296                   GNUNET_i2s (&address->peer), address->transport_name, session);
1297       GNUNET_break (0);
1298       return;
1299     }
1300     if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1301     {
1302       /* trying to set new address to YES */
1303       s = find_empty_session_slot (sh, session, &address->peer);
1304       GNUNET_assert (NOT_FOUND != s);
1305     }
1306   }
1307
1308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309               "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1310               (GNUNET_YES == in_use) ? "YES" : "NO",
1311               GNUNET_i2s (&address->peer), address->transport_name, session);
1312
1313   m->session_id = htonl (s);
1314   pm = (char *) &m[1];
1315   memcpy (pm, address->address, address->address_length);
1316   memcpy (&pm[address->address_length], address->transport_name, namelen);
1317   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1318   do_transmit (sh);
1319   return;
1320 }
1321
1322
1323 /**
1324  * A session got destroyed, stop including it as a valid address.
1325  *
1326  * @param sh handle
1327  * @param address the address
1328  * @param session session handle that is no longer valid
1329  */
1330 void
1331 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1332                               const struct GNUNET_HELLO_Address *address,
1333                               struct Session *session)
1334 {
1335   struct PendingMessage *p;
1336   struct AddressDestroyedMessage *m;
1337   char *pm;
1338   size_t namelen;
1339   size_t msize;
1340   uint32_t s = 0;
1341
1342   GNUNET_assert (address->transport_name != NULL);
1343   namelen = strlen (address->transport_name) + 1;
1344   GNUNET_assert (namelen > 1);
1345   msize =
1346       sizeof (struct AddressDestroyedMessage) + address->address_length +
1347       namelen;
1348   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1349       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1350       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1351   {
1352     GNUNET_break (0);
1353     return;
1354   }
1355
1356   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1357   p->size = msize;
1358   p->is_init = GNUNET_NO;
1359   m = (struct AddressDestroyedMessage *) &p[1];
1360   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1361   m->header.size = htons (msize);
1362   m->reserved = htonl (0);
1363   m->peer = address->peer;
1364   m->address_length = htons (address->address_length);
1365   m->plugin_name_length = htons (namelen);
1366
1367   s = find_session_id (sh, session, &address->peer);
1368   if ((NULL != session) && (NOT_FOUND == s))
1369   {
1370     /* trying to delete unknown address */
1371     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372                 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1373                 GNUNET_i2s (&address->peer), address->transport_name, session);
1374     return;
1375   }
1376
1377   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1378               "Deleting address for peer `%s', plugin `%s', session %p\n",
1379               GNUNET_i2s (&address->peer), address->transport_name, session);
1380
1381   m->session_id = htonl (s);
1382   pm = (char *) &m[1];
1383   memcpy (pm, address->address, address->address_length);
1384   memcpy (&pm[address->address_length], address->transport_name, namelen);
1385   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1386   do_transmit (sh);
1387   remove_session (sh, s, &address->peer);
1388 }
1389
1390 /* end of ats_api_scheduling.c */