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