-getting rid of silly, stupid, useless, often wrong DEFAULTCONFIG setting
[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].session));
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 GNUNET_ATS_Information ats;
825   struct ATS_Network * cur = sh->net_head;
826   int type = GNUNET_ATS_NET_UNSPECIFIED;
827
828   if  (addr->sa_family == AF_UNIX)
829   {
830     type = GNUNET_ATS_NET_LOOPBACK;
831   }
832
833   /* IPv4 loopback check */
834   if  (addr->sa_family == AF_INET)
835   {
836     struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
837
838     if ((a4->sin_addr.s_addr & htonl(0xff000000)) == htonl (0x7f000000))
839       type = GNUNET_ATS_NET_LOOPBACK;
840   }
841   /* IPv6 loopback check */
842   if  (addr->sa_family == AF_INET6)
843   {
844     struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
845     if (IN6_IS_ADDR_LOOPBACK (&a6->sin6_addr))
846       type = GNUNET_ATS_NET_LOOPBACK;
847   }
848
849   /* Check local networks */
850   while ((cur != NULL) && (type == GNUNET_ATS_NET_UNSPECIFIED))
851   {
852     if (addrlen != cur->length)
853     {
854       cur = cur->next;
855       continue;
856     }
857
858     if (addr->sa_family == AF_INET)
859     {
860       struct sockaddr_in * a4 = (struct sockaddr_in *) addr;
861       struct sockaddr_in * net4 = (struct sockaddr_in *) cur->network;
862       struct sockaddr_in * mask4 = (struct sockaddr_in *) cur->netmask;
863
864       if (((a4->sin_addr.s_addr & mask4->sin_addr.s_addr)) == net4->sin_addr.s_addr)
865       {
866         char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net4, addrlen));
867         GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "ats-scheduling-api",
868             "`%s' is in network `%s'\n",
869             GNUNET_a2s ((const struct sockaddr *)a4, addrlen),
870             net);
871         GNUNET_free (net);
872         type = GNUNET_ATS_NET_LAN;
873       }
874     }
875     if (addr->sa_family == AF_INET6)
876     {
877       struct sockaddr_in6 * a6 = (struct sockaddr_in6 *) addr;
878       struct sockaddr_in6 * net6 = (struct sockaddr_in6 *) cur->network;
879       struct sockaddr_in6 * mask6 = (struct sockaddr_in6 *) cur->netmask;
880
881       int res = GNUNET_YES;
882       int c = 0;
883       uint32_t *addr_elem = (uint32_t *) &a6->sin6_addr;
884       uint32_t *mask_elem = (uint32_t *) &mask6->sin6_addr;
885       uint32_t *net_elem = (uint32_t *) &net6->sin6_addr;
886       for (c = 0; c < 4; c++)
887         if ((addr_elem[c] & mask_elem[c]) != net_elem[c])
888           res = GNUNET_NO;
889
890       if (res == GNUNET_YES)
891       {
892         char * net = GNUNET_strdup (GNUNET_a2s ((const struct sockaddr *) net6, addrlen));
893         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "`%s' is in network `%s'\n",
894               GNUNET_a2s ((const struct sockaddr *) a6, addrlen),
895               net);
896         GNUNET_free (net);
897         type = GNUNET_ATS_NET_LAN;
898       }
899     }
900     cur = cur->next;
901   }
902
903   /* no local network found for this address, default: WAN */
904   if (type == GNUNET_ATS_NET_UNSPECIFIED)
905     type = GNUNET_ATS_NET_WAN;
906   ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
907   ats.value = htonl (type);
908   return (const struct GNUNET_ATS_Information) ats;
909 }
910
911
912 /**
913  * Initialize the ATS subsystem.
914  *
915  * @param cfg configuration to use
916  * @param suggest_cb notification to call whenever the suggestation changed
917  * @param suggest_cb_cls closure for 'suggest_cb'
918  * @return ats context
919  */
920 struct GNUNET_ATS_SchedulingHandle *
921 GNUNET_ATS_scheduling_init (const struct GNUNET_CONFIGURATION_Handle *cfg,
922                             GNUNET_ATS_AddressSuggestionCallback suggest_cb,
923                             void *suggest_cb_cls)
924 {
925   struct GNUNET_ATS_SchedulingHandle *sh;
926
927   sh = GNUNET_malloc (sizeof (struct GNUNET_ATS_SchedulingHandle));
928   sh->cfg = cfg;
929   sh->suggest_cb = suggest_cb;
930   sh->suggest_cb_cls = suggest_cb_cls;
931   GNUNET_array_grow (sh->session_array, sh->session_array_size, 4);
932   GNUNET_OS_network_interfaces_list(interface_proc, sh);
933   sh->interface_task = GNUNET_SCHEDULER_add_delayed (INTERFACE_PROCESSING_INTERVALL,
934       get_addresses,
935       sh);
936   reconnect (sh);
937   return sh;
938 }
939
940
941 /**
942  * Client is done with ATS scheduling, release resources.
943  *
944  * @param sh handle to release
945  */
946 void
947 GNUNET_ATS_scheduling_done (struct GNUNET_ATS_SchedulingHandle *sh)
948 {
949   struct PendingMessage *p;
950
951   while (NULL != (p = sh->pending_head))
952   {
953     GNUNET_CONTAINER_DLL_remove (sh->pending_head, sh->pending_tail, p);
954     GNUNET_free (p);
955   }
956   if (NULL != sh->client)
957   {
958     GNUNET_CLIENT_disconnect (sh->client);
959     sh->client = NULL;
960   }
961   if (GNUNET_SCHEDULER_NO_TASK != sh->task)
962   {
963     GNUNET_SCHEDULER_cancel (sh->task);
964     sh->task = GNUNET_SCHEDULER_NO_TASK;
965   }
966
967   delete_networks (sh);
968   if (sh->interface_task != GNUNET_SCHEDULER_NO_TASK)
969   {
970     GNUNET_SCHEDULER_cancel(sh->interface_task);
971     sh->interface_task = GNUNET_SCHEDULER_NO_TASK;
972   }
973   GNUNET_array_grow (sh->session_array, sh->session_array_size, 0);
974   GNUNET_free (sh);
975   sh = NULL;
976 }
977
978 /**
979  * We would like to reset the address suggestion block time for this
980  * peer
981  *
982  * @param sh handle
983  * @param peer identity of the peer we want to reset
984  */
985 void
986 GNUNET_ATS_reset_backoff (struct GNUNET_ATS_SchedulingHandle *sh,
987                           const struct GNUNET_PeerIdentity *peer)
988 {
989   struct PendingMessage *p;
990   struct ResetBackoffMessage *m;
991
992   p = GNUNET_malloc (sizeof (struct PendingMessage) +
993                      sizeof (struct ResetBackoffMessage));
994   p->size = sizeof (struct ResetBackoffMessage);
995   p->is_init = GNUNET_NO;
996   m = (struct ResetBackoffMessage *) &p[1];
997   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_RESET_BACKOFF);
998   m->header.size = htons (sizeof (struct ResetBackoffMessage));
999   m->reserved = htonl (0);
1000   m->peer = *peer;
1001   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1002   do_transmit (sh);
1003 }
1004
1005 /**
1006  * We would like to establish a new connection with a peer.  ATS
1007  * should suggest a good address to begin with.
1008  *
1009  * @param sh handle
1010  * @param peer identity of the peer we need an address for
1011  */
1012 void
1013 GNUNET_ATS_suggest_address (struct GNUNET_ATS_SchedulingHandle *sh,
1014                             const struct GNUNET_PeerIdentity *peer)
1015 {
1016   struct PendingMessage *p;
1017   struct RequestAddressMessage *m;
1018
1019   // FIXME: ATS needs to remember this in case of
1020   // a disconnect!
1021   p = GNUNET_malloc (sizeof (struct PendingMessage) +
1022                      sizeof (struct RequestAddressMessage));
1023   p->size = sizeof (struct RequestAddressMessage);
1024   p->is_init = GNUNET_NO;
1025   m = (struct RequestAddressMessage *) &p[1];
1026   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS);
1027   m->header.size = htons (sizeof (struct RequestAddressMessage));
1028   m->reserved = htonl (0);
1029   m->peer = *peer;
1030   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1031   do_transmit (sh);
1032 }
1033
1034
1035 /**
1036  * We would like to stop receiving address updates for this peer
1037  *
1038  * @param sh handle
1039  * @param peer identity of the peer
1040  */
1041 void
1042 GNUNET_ATS_suggest_address_cancel (struct GNUNET_ATS_SchedulingHandle *sh,
1043                                    const struct GNUNET_PeerIdentity *peer)
1044 {
1045   struct PendingMessage *p;
1046   struct RequestAddressMessage *m;
1047
1048   p = GNUNET_malloc (sizeof (struct PendingMessage) +
1049                      sizeof (struct RequestAddressMessage));
1050   p->size = sizeof (struct RequestAddressMessage);
1051   p->is_init = GNUNET_NO;
1052   m = (struct RequestAddressMessage *) &p[1];
1053   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL);
1054   m->header.size = htons (sizeof (struct RequestAddressMessage));
1055   m->reserved = htonl (0);
1056   m->peer = *peer;
1057   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1058   do_transmit (sh);
1059 }
1060
1061
1062 /**
1063  * We have a new address ATS should know. Addresses have to be added with this
1064  * function before they can be: updated, set in use and destroyed
1065  *
1066  * @param sh handle
1067  * @param address the address
1068  * @param session session handle, can be NULL
1069  * @param ats performance data for the address
1070  * @param ats_count number of performance records in 'ats'
1071  * @return GNUNET_OK on success, GNUNET_SYSERR on error
1072  */
1073 int
1074 GNUNET_ATS_address_add (struct GNUNET_ATS_SchedulingHandle *sh,
1075                         const struct GNUNET_HELLO_Address *address,
1076                         struct Session *session,
1077                         const struct GNUNET_ATS_Information *ats,
1078                         uint32_t ats_count)
1079 {
1080
1081   struct PendingMessage *p;
1082   struct AddressUpdateMessage *m;
1083   struct GNUNET_ATS_Information *am;
1084   char *pm;
1085   size_t namelen;
1086   size_t msize;
1087   uint32_t s = 0;
1088
1089   if (address == NULL)
1090   {
1091     GNUNET_break (0);
1092     return GNUNET_SYSERR;
1093   }
1094
1095   namelen =
1096       (address->transport_name ==
1097        NULL) ? 0 : strlen (address->transport_name) + 1;
1098   msize =
1099       sizeof (struct AddressUpdateMessage) + address->address_length +
1100       ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1101   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1102       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1103       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1104       (ats_count >=
1105        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1106   {
1107     GNUNET_break (0);
1108     return GNUNET_SYSERR;
1109   }
1110
1111   if (NULL != session)
1112   {
1113     s = find_session_id (sh, session, &address->peer);
1114     if (NOT_FOUND != s)
1115     {
1116       /* Already existing, nothing todo */
1117       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118                   "Adding duplicate address for peer `%s', plugin `%s', session %p id %u\n",
1119                   GNUNET_i2s (&address->peer),
1120                   address->transport_name, session, s);
1121       return GNUNET_SYSERR;
1122     }
1123     s = find_empty_session_slot (sh, session, &address->peer);
1124     GNUNET_break (NOT_FOUND != s);
1125   }
1126
1127   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1128   p->size = msize;
1129   p->is_init = GNUNET_NO;
1130   m = (struct AddressUpdateMessage *) &p[1];
1131   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_ADD);
1132   m->header.size = htons (msize);
1133   m->ats_count = htonl (ats_count);
1134   m->peer = address->peer;
1135   m->address_length = htons (address->address_length);
1136   m->plugin_name_length = htons (namelen);
1137   m->session_id = htonl (s);
1138
1139   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140               "Adding address for peer `%s', plugin `%s', session %p id %u\n",
1141               GNUNET_i2s (&address->peer),
1142               address->transport_name, session, s);
1143
1144   am = (struct GNUNET_ATS_Information *) &m[1];
1145   memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1146   pm = (char *) &am[ats_count];
1147   memcpy (pm, address->address, address->address_length);
1148   if (NULL != address->transport_name)
1149         memcpy (&pm[address->address_length], address->transport_name, namelen);
1150   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1151   do_transmit (sh);
1152   return GNUNET_OK;
1153
1154 }
1155
1156
1157 /**
1158  * We have updated performance statistics for a given address.  Note
1159  * that this function can be called for addresses that are currently
1160  * in use as well as addresses that are valid but not actively in use.
1161  * Furthermore, the peer may not even be connected to us right now (in
1162  * which case the call may be ignored or the information may be stored
1163  * for later use).  Update bandwidth assignments.
1164  *
1165  * @param sh handle
1166  * @param address the address
1167  * @param session session handle, can be NULL
1168  * @param ats performance data for the address
1169  * @param ats_count number of performance records in 'ats'
1170  */
1171 void
1172 GNUNET_ATS_address_update (struct GNUNET_ATS_SchedulingHandle *sh,
1173                            const struct GNUNET_HELLO_Address *address,
1174                            struct Session *session,
1175                            const struct GNUNET_ATS_Information *ats,
1176                            uint32_t ats_count)
1177 {
1178   struct PendingMessage *p;
1179   struct AddressUpdateMessage *m;
1180   struct GNUNET_ATS_Information *am;
1181   char *pm;
1182   size_t namelen;
1183   size_t msize;
1184   uint32_t s = 0;
1185
1186   if (address == NULL)
1187   {
1188     GNUNET_break (0);
1189     return;
1190   }
1191
1192   namelen =
1193       (address->transport_name ==
1194        NULL) ? 0 : strlen (address->transport_name) + 1;
1195   msize =
1196       sizeof (struct AddressUpdateMessage) + address->address_length +
1197       ats_count * sizeof (struct GNUNET_ATS_Information) + namelen;
1198   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1199       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1200       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1201       (ats_count >=
1202        GNUNET_SERVER_MAX_MESSAGE_SIZE / sizeof (struct GNUNET_ATS_Information)))
1203   {
1204     GNUNET_break (0);
1205     return;
1206   }
1207
1208   if (NULL != session)
1209   {
1210     s = find_session_id (sh, session, &address->peer);
1211     if (NOT_FOUND == s)
1212     {
1213       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1214                   "Update for unknown address for peer `%s', plugin `%s', session %p id %u\n",
1215                   GNUNET_i2s (&address->peer),
1216                   address->transport_name, session, s);
1217
1218       GNUNET_break (0);
1219       return;
1220     }
1221   }
1222
1223   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1224   p->size = msize;
1225   p->is_init = GNUNET_NO;
1226   m = (struct AddressUpdateMessage *) &p[1];
1227   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_UPDATE);
1228   m->header.size = htons (msize);
1229   m->ats_count = htonl (ats_count);
1230   m->peer = address->peer;
1231   m->address_length = htons (address->address_length);
1232   m->plugin_name_length = htons (namelen);
1233
1234   m->session_id = htonl (s);
1235
1236   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237               "Updating address for peer `%s', plugin `%s', session %p id %u\n",
1238               GNUNET_i2s (&address->peer),
1239               address->transport_name, session, s);
1240
1241   am = (struct GNUNET_ATS_Information *) &m[1];
1242   memcpy (am, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
1243   pm = (char *) &am[ats_count];
1244   memcpy (pm, address->address, address->address_length);
1245   memcpy (&pm[address->address_length], address->transport_name, namelen);
1246   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1247   do_transmit (sh);
1248   return;
1249 }
1250
1251
1252 /**
1253  * An address is now in use or not used any more.
1254  *
1255  * @param sh handle
1256  * @param address the address
1257  * @param session session handle, can be NULL
1258  * @param in_use GNUNET_YES if this address is now used, GNUNET_NO
1259  * if address is not used any more
1260  */
1261 void
1262 GNUNET_ATS_address_in_use (struct GNUNET_ATS_SchedulingHandle *sh,
1263                            const struct GNUNET_HELLO_Address *address,
1264                            struct Session *session, int in_use)
1265 {
1266   struct PendingMessage *p;
1267   struct AddressUseMessage *m;
1268   char *pm;
1269   size_t namelen;
1270   size_t msize;
1271   uint32_t s = 0;
1272
1273   GNUNET_assert (NULL != address);
1274   namelen =
1275       (address->transport_name ==
1276        NULL) ? 0 : strlen (address->transport_name) + 1;
1277   msize = sizeof (struct AddressUseMessage) + address->address_length + namelen;
1278   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1279       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1280       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1281   {
1282     GNUNET_break (0);
1283     return;
1284   }
1285
1286   if (session != NULL)
1287   {
1288     s = find_session_id (sh, session, &address->peer);
1289     if ((s == NOT_FOUND) && (GNUNET_NO == in_use))
1290     {
1291       /* trying to set unknown address to NO */
1292       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1293                   "Trying to set unknown address to unused for peer `%s', plugin `%s', session %p\n",
1294                   GNUNET_i2s (&address->peer), address->transport_name, session);
1295       GNUNET_break (0);
1296       return;
1297     }
1298     if ((s == NOT_FOUND) && (GNUNET_YES == in_use))
1299     {
1300       /* trying to set new address to YES */
1301       s = find_empty_session_slot (sh, session, &address->peer);
1302       GNUNET_assert (NOT_FOUND != s);
1303     }
1304   }
1305
1306   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1307   p->size = msize;
1308   p->is_init = GNUNET_NO;
1309   m = (struct AddressUseMessage *) &p[1];
1310   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_IN_USE);
1311   m->header.size = htons (msize);
1312   m->peer = address->peer;
1313   m->in_use = htons (in_use);
1314   m->address_length = htons (address->address_length);
1315   m->plugin_name_length = htons (namelen);
1316
1317   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1318               "Setting address used to %s for peer `%s', plugin `%s', session %p\n",
1319               (GNUNET_YES == in_use) ? "YES" : "NO",
1320               GNUNET_i2s (&address->peer), address->transport_name, session);
1321
1322   m->session_id = htonl (s);
1323   pm = (char *) &m[1];
1324   memcpy (pm, address->address, address->address_length);
1325   memcpy (&pm[address->address_length], address->transport_name, namelen);
1326   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1327   do_transmit (sh);
1328   return;
1329 }
1330
1331
1332 /**
1333  * A session got destroyed, stop including it as a valid address.
1334  *
1335  * @param sh handle
1336  * @param address the address
1337  * @param session session handle that is no longer valid, can be NULL
1338  */
1339 void
1340 GNUNET_ATS_address_destroyed (struct GNUNET_ATS_SchedulingHandle *sh,
1341                               const struct GNUNET_HELLO_Address *address,
1342                               struct Session *session)
1343 {
1344   struct PendingMessage *p;
1345   struct AddressDestroyedMessage *m;
1346   char *pm;
1347   size_t namelen;
1348   size_t msize;
1349   uint32_t s = 0;
1350
1351   if (address == NULL)
1352   {
1353     GNUNET_break (0);
1354     return;
1355   }
1356
1357   GNUNET_assert (address->transport_name != NULL);
1358   namelen = strlen (address->transport_name) + 1;
1359   GNUNET_assert (namelen > 1);
1360   msize =
1361       sizeof (struct AddressDestroyedMessage) + address->address_length +
1362       namelen;
1363   if ((msize >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1364       (address->address_length >= GNUNET_SERVER_MAX_MESSAGE_SIZE) ||
1365       (namelen >= GNUNET_SERVER_MAX_MESSAGE_SIZE))
1366   {
1367     GNUNET_break (0);
1368     return;
1369   }
1370
1371   s = find_session_id (sh, session, &address->peer);
1372   if ((NULL != session) && (NOT_FOUND == s))
1373   {
1374     /* trying to delete unknown address */
1375     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1376                 "Trying to delete unknown address for peer `%s', plugin `%s', session %p\n",
1377                 GNUNET_i2s (&address->peer), address->transport_name, session);
1378     return;
1379   }
1380
1381   p = GNUNET_malloc (sizeof (struct PendingMessage) + msize);
1382   p->size = msize;
1383   p->is_init = GNUNET_NO;
1384   m = (struct AddressDestroyedMessage *) &p[1];
1385   m->header.type = htons (GNUNET_MESSAGE_TYPE_ATS_ADDRESS_DESTROYED);
1386   m->header.size = htons (msize);
1387   m->reserved = htonl (0);
1388   m->peer = address->peer;
1389   m->address_length = htons (address->address_length);
1390   m->plugin_name_length = htons (namelen);
1391
1392   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393               "Deleting address for peer `%s', plugin `%s', session %p\n",
1394               GNUNET_i2s (&address->peer), address->transport_name, session);
1395
1396   m->session_id = htonl (s);
1397   pm = (char *) &m[1];
1398   memcpy (pm, address->address, address->address_length);
1399   memcpy (&pm[address->address_length], address->transport_name, namelen);
1400   GNUNET_CONTAINER_DLL_insert_tail (sh->pending_head, sh->pending_tail, p);
1401   do_transmit (sh);
1402   remove_session (sh, s, &address->peer);
1403 }
1404
1405 /* end of ats_api_scheduling.c */