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