-makefile for new test_stream_local (commented)
[oweals/gnunet.git] / src / transport / gnunet-service-transport_blacklist.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 /**
22  * @file transport/gnunet-service-transport_blacklist.c
23  * @brief blacklisting implementation
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet-service-transport.h"
28 #include "gnunet-service-transport_blacklist.h"
29 #include "gnunet-service-transport_neighbours.h"
30 #include "transport.h"
31
32
33 /**
34  * Size of the blacklist hash map.
35  */
36 #define TRANSPORT_BLACKLIST_HT_SIZE 64
37
38
39 /**
40  * Context we use when performing a blacklist check.
41  */
42 struct GST_BlacklistCheck;
43
44
45 /**
46  * Information kept for each client registered to perform
47  * blacklisting.
48  */
49 struct Blacklisters
50 {
51   /**
52    * This is a linked list.
53    */
54   struct Blacklisters *next;
55
56   /**
57    * This is a linked list.
58    */
59   struct Blacklisters *prev;
60
61   /**
62    * Client responsible for this entry.
63    */
64   struct GNUNET_SERVER_Client *client;
65
66   /**
67    * Blacklist check that we're currently performing (or NULL
68    * if we're performing one that has been cancelled).
69    */
70   struct GST_BlacklistCheck *bc;
71
72   /**
73    * Set to GNUNET_YES if we're currently waiting for a reply.
74    */
75   int waiting_for_reply;
76
77 };
78
79
80
81 /**
82  * Context we use when performing a blacklist check.
83  */
84 struct GST_BlacklistCheck
85 {
86
87   /**
88    * This is a linked list.
89    */
90   struct GST_BlacklistCheck *next;
91
92   /**
93    * This is a linked list.
94    */
95   struct GST_BlacklistCheck *prev;
96
97   /**
98    * Peer being checked.
99    */
100   struct GNUNET_PeerIdentity peer;
101
102   /**
103    * Continuation to call with the result.
104    */
105   GST_BlacklistTestContinuation cont;
106
107   /**
108    * Closure for cont.
109    */
110   void *cont_cls;
111
112   /**
113    * Current transmission request handle for this client, or NULL if no
114    * request is pending.
115    */
116   struct GNUNET_CONNECTION_TransmitHandle *th;
117
118   /**
119    * Our current position in the blacklisters list.
120    */
121   struct Blacklisters *bl_pos;
122
123   /**
124    * Current task performing the check.
125    */
126   GNUNET_SCHEDULER_TaskIdentifier task;
127
128 };
129
130
131 /**
132  * Head of DLL of active blacklisting queries.
133  */
134 static struct GST_BlacklistCheck *bc_head;
135
136 /**
137  * Tail of DLL of active blacklisting queries.
138  */
139 static struct GST_BlacklistCheck *bc_tail;
140
141 /**
142  * Head of DLL of blacklisting clients.
143  */
144 static struct Blacklisters *bl_head;
145
146 /**
147  * Tail of DLL of blacklisting clients.
148  */
149 static struct Blacklisters *bl_tail;
150
151 /**
152  * Hashmap of blacklisted peers.  Values are of type 'char *' (transport names),
153  * can be NULL if we have no static blacklist.
154  */
155 static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
156
157
158 /**
159  * Perform next action in the blacklist check.
160  *
161  * @param cls the 'struct BlacklistCheck*'
162  * @param tc unused
163  */
164 static void
165 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
166
167
168 /**
169  * Called whenever a client is disconnected.  Frees our
170  * resources associated with that client.
171  *
172  * @param cls closure (unused)
173  * @param client identification of the client
174  */
175 static void
176 client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client)
177 {
178   struct Blacklisters *bl;
179   struct GST_BlacklistCheck *bc;
180
181   if (client == NULL)
182     return;
183   for (bl = bl_head; bl != NULL; bl = bl->next)
184   {
185     if (bl->client != client)
186       continue;
187     for (bc = bc_head; bc != NULL; bc = bc->next)
188     {
189       if (bc->bl_pos != bl)
190         continue;
191       bc->bl_pos = bl->next;
192       if (bc->th != NULL)
193       {
194         GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
195         bc->th = NULL;
196       }
197       if (bc->task == GNUNET_SCHEDULER_NO_TASK)
198         bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
199       break;
200     }
201     GNUNET_CONTAINER_DLL_remove (bl_head, bl_tail, bl);
202     GNUNET_SERVER_client_drop (bl->client);
203     GNUNET_free (bl);
204     break;
205   }
206 }
207
208
209 /**
210  * Read the blacklist file, containing transport:peer entries.
211  * Provided the transport is loaded, set up hashmap with these
212  * entries to blacklist peers by transport.
213  *
214  */
215 static void
216 read_blacklist_file ()
217 {
218   char *fn;
219   char *data;
220   size_t pos;
221   size_t colon_pos;
222   int tsize;
223   struct GNUNET_PeerIdentity pid;
224   uint64_t fsize;
225   struct GNUNET_CRYPTO_HashAsciiEncoded enc;
226   unsigned int entries_found;
227   char *transport_name;
228
229   if (GNUNET_OK !=
230       GNUNET_CONFIGURATION_get_value_filename (GST_cfg, "TRANSPORT",
231                                                "BLACKLIST_FILE", &fn))
232   {
233 #if DEBUG_TRANSPORT
234     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
235                 "Option `%s' in section `%s' not specified!\n",
236                 "BLACKLIST_FILE", "TRANSPORT");
237 #endif
238     return;
239   }
240   if (GNUNET_OK != GNUNET_DISK_file_test (fn))
241     GNUNET_DISK_fn_write (fn, NULL, 0,
242                           GNUNET_DISK_PERM_USER_READ |
243                           GNUNET_DISK_PERM_USER_WRITE);
244   if (GNUNET_OK != GNUNET_DISK_file_size (fn,
245       &fsize, GNUNET_NO, GNUNET_YES))
246   {
247     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
248                 _("Could not read blacklist file `%s'\n"), fn);
249     GNUNET_free (fn);
250     return;
251   }
252   if (fsize == 0)
253   {
254 #if DEBUG_TRANSPORT
255     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Blacklist file `%s' is empty.\n"),
256                 fn);
257 #endif
258     GNUNET_free (fn);
259     return;
260   }
261   /* FIXME: use mmap */
262   data = GNUNET_malloc_large (fsize);
263   GNUNET_assert (data != NULL);
264   if (fsize != GNUNET_DISK_fn_read (fn, data, fsize))
265   {
266     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
267                 _("Failed to read blacklist from `%s'\n"), fn);
268     GNUNET_free (fn);
269     GNUNET_free (data);
270     return;
271   }
272   entries_found = 0;
273   pos = 0;
274   while ((pos < fsize) && isspace ((unsigned char) data[pos]))
275     pos++;
276   while ((fsize >= sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)) &&
277          (pos <=
278           fsize - sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded)))
279   {
280     colon_pos = pos;
281     while ((colon_pos < fsize) && (data[colon_pos] != ':') &&
282            (!isspace ((unsigned char) data[colon_pos])))
283       colon_pos++;
284     if (colon_pos >= fsize)
285     {
286       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
287                   _
288                   ("Syntax error in blacklist file at offset %llu, giving up!\n"),
289                   (unsigned long long) colon_pos);
290       GNUNET_free (fn);
291       GNUNET_free (data);
292       return;
293     }
294
295     if (isspace ((unsigned char) data[colon_pos]))
296     {
297       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
298                   _
299                   ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
300                   (unsigned long long) colon_pos);
301       pos = colon_pos;
302       while ((pos < fsize) && isspace ((unsigned char) data[pos]))
303         pos++;
304       continue;
305     }
306     tsize = colon_pos - pos;
307     if ((pos >= fsize) || (pos + tsize >= fsize) ||
308         (tsize == 0))
309     {
310       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
311                   _
312                   ("Syntax error in blacklist file at offset %llu, giving up!\n"),
313                   (unsigned long long) colon_pos);
314       GNUNET_free (fn);
315       GNUNET_free (data);
316       return;
317     }
318
319     if (tsize < 1)
320       continue;
321
322     transport_name = GNUNET_malloc (tsize + 1);
323     memcpy (transport_name, &data[pos], tsize);
324     pos = colon_pos + 1;
325 #if DEBUG_TRANSPORT
326     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
327                 "Read transport name `%s' in blacklist file.\n",
328                 transport_name);
329 #endif
330     memcpy (&enc, &data[pos], sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded));
331     if (!isspace
332         ((unsigned char)
333          enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1]))
334     {
335       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
336                   _
337                   ("Syntax error in blacklist file at offset %llu, skipping bytes.\n"),
338                   (unsigned long long) pos);
339       pos++;
340       while ((pos < fsize) && (!isspace ((unsigned char) data[pos])))
341         pos++;
342       GNUNET_free_non_null (transport_name);
343       continue;
344     }
345     enc.encoding[sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded) - 1] = '\0';
346     if (GNUNET_OK !=
347         GNUNET_CRYPTO_hash_from_string ((char *) &enc, &pid.hashPubKey))
348     {
349       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
350                   _
351                   ("Syntax error in blacklist file at offset %llu, skipping bytes `%s'.\n"),
352                   (unsigned long long) pos, &enc);
353     }
354     else
355     {
356       if (0 !=
357           memcmp (&pid, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
358       {
359         entries_found++;
360         GST_blacklist_add_peer (&pid, transport_name);
361       }
362       else
363       {
364         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
365                     _("Found myself `%s' in blacklist (useless, ignored)\n"),
366                     GNUNET_i2s (&pid));
367       }
368     }
369     pos = pos + sizeof (struct GNUNET_CRYPTO_HashAsciiEncoded);
370     GNUNET_free_non_null (transport_name);
371     while ((pos < fsize) && isspace ((unsigned char) data[pos]))
372       pos++;
373   }
374   GNUNET_STATISTICS_update (GST_stats, "# Transport entries blacklisted",
375                             entries_found, GNUNET_NO);
376   GNUNET_free (data);
377   GNUNET_free (fn);
378 }
379
380
381 /**
382  * Start blacklist subsystem.
383  *
384  * @param server server used to accept clients from
385  */
386 void
387 GST_blacklist_start (struct GNUNET_SERVER_Handle *server)
388 {
389   read_blacklist_file ();
390   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification,
391                                    NULL);
392 }
393
394
395 /**
396  * Free the given entry in the blacklist.
397  *
398  * @param cls unused
399  * @param key host identity (unused)
400  * @param value the blacklist entry
401  * @return GNUNET_OK (continue to iterate)
402  */
403 static int
404 free_blacklist_entry (void *cls, const GNUNET_HashCode * key, void *value)
405 {
406   char *be = value;
407
408   GNUNET_free (be);
409   return GNUNET_OK;
410 }
411
412
413 /**
414  * Stop blacklist subsystem.
415  */
416 void
417 GST_blacklist_stop ()
418 {
419   if (NULL != blacklist)
420   {
421     GNUNET_CONTAINER_multihashmap_iterate (blacklist, &free_blacklist_entry,
422                                            NULL);
423     GNUNET_CONTAINER_multihashmap_destroy (blacklist);
424     blacklist = NULL;
425   }
426 }
427
428
429 /**
430  * Transmit blacklist query to the client.
431  *
432  * @param cls the 'struct GST_BlacklistCheck'
433  * @param size number of bytes allowed
434  * @param buf where to copy the message
435  * @return number of bytes copied to buf
436  */
437 static size_t
438 transmit_blacklist_message (void *cls, size_t size, void *buf)
439 {
440   struct GST_BlacklistCheck *bc = cls;
441   struct Blacklisters *bl;
442   struct BlacklistMessage bm;
443
444   bc->th = NULL;
445   if (size == 0)
446   {
447     GNUNET_assert (bc->task == GNUNET_SCHEDULER_NO_TASK);
448     bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
449     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
450                 "Failed to send blacklist test for peer `%s' to client\n",
451                 GNUNET_i2s (&bc->peer));
452     return 0;
453   }
454 #if DEBUG_TRANSPORT
455   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
456               "Sending blacklist test for peer `%s' to client\n",
457               GNUNET_i2s (&bc->peer));
458 #endif
459   bl = bc->bl_pos;
460   bm.header.size = htons (sizeof (struct BlacklistMessage));
461   bm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST_QUERY);
462   bm.is_allowed = htonl (0);
463   bm.peer = bc->peer;
464   memcpy (buf, &bm, sizeof (bm));
465   GNUNET_SERVER_receive_done (bl->client, GNUNET_OK);
466   bl->waiting_for_reply = GNUNET_YES;
467   return sizeof (bm);
468 }
469
470
471 /**
472  * Perform next action in the blacklist check.
473  *
474  * @param cls the 'struct GST_BlacklistCheck*'
475  * @param tc unused
476  */
477 static void
478 do_blacklist_check (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
479 {
480   struct GST_BlacklistCheck *bc = cls;
481   struct Blacklisters *bl;
482
483   bc->task = GNUNET_SCHEDULER_NO_TASK;
484   bl = bc->bl_pos;
485   if (bl == NULL)
486   {
487 #if DEBUG_TRANSPORT
488     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
489                 "No other blacklist clients active, will allow neighbour `%s'\n",
490                 GNUNET_i2s (&bc->peer));
491 #endif
492     bc->cont (bc->cont_cls, &bc->peer, GNUNET_OK);
493     GNUNET_CONTAINER_DLL_remove(bc_head, bc_tail, bc);
494     GNUNET_free (bc);
495     return;
496   }
497   if ((bl->bc != NULL) || (bl->waiting_for_reply != GNUNET_NO))
498     return;                     /* someone else busy with this client */
499   bl->bc = bc;
500   bc->th =
501       GNUNET_SERVER_notify_transmit_ready (bl->client,
502                                            sizeof (struct BlacklistMessage),
503                                            GNUNET_TIME_UNIT_FOREVER_REL,
504                                            &transmit_blacklist_message, bc);
505 }
506
507
508 /**
509  * Got the result about an existing connection from a new blacklister.
510  * Shutdown the neighbour if necessary.
511  *
512  * @param cls unused
513  * @param peer the neighbour that was investigated
514  * @param allowed GNUNET_OK if we can keep it,
515  *                GNUNET_NO if we must shutdown the connection
516  */
517 static void
518 confirm_or_drop_neighbour (void *cls, const struct GNUNET_PeerIdentity *peer,
519                            int allowed)
520 {
521   if (GNUNET_OK == allowed)
522     return;                     /* we're done */
523   GNUNET_STATISTICS_update (GST_stats,
524                             gettext_noop ("# disconnects due to blacklist"), 1,
525                             GNUNET_NO);
526   GST_neighbours_force_disconnect (peer);
527 }
528
529
530 /**
531  * Closure for 'test_connection_ok'.
532  */
533 struct TestConnectionContext
534 {
535   /**
536    * Is this the first neighbour we're checking?
537    */
538   int first;
539
540   /**
541    * Handle to the blacklisting client we need to ask.
542    */
543   struct Blacklisters *bl;
544 };
545
546
547 /**
548  * Test if an existing connection is still acceptable given a new
549  * blacklisting client.
550  *
551  * @param cls the 'struct TestConnectionContest'
552  * @param neighbour neighbour's identity
553  * @param ats performance data
554  * @param ats_count number of entries in ats (excluding 0-termination)
555  * @param address the address
556  */
557 static void
558 test_connection_ok (void *cls, const struct GNUNET_PeerIdentity *neighbour,
559                     const struct GNUNET_ATS_Information *ats,
560                     uint32_t ats_count,
561                     const struct GNUNET_HELLO_Address *address)
562 {
563   struct TestConnectionContext *tcc = cls;
564   struct GST_BlacklistCheck *bc;
565
566   bc = GNUNET_malloc (sizeof (struct GST_BlacklistCheck));
567   GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
568   bc->peer = *neighbour;
569   bc->cont = &confirm_or_drop_neighbour;
570   bc->cont_cls = NULL;
571   bc->bl_pos = tcc->bl;
572   if (GNUNET_YES == tcc->first)
573   {
574     /* all would wait for the same client, no need to
575      * create more than just the first task right now */
576     bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
577     tcc->first = GNUNET_NO;
578   }
579 }
580
581
582
583 /**
584  * Initialize a blacklisting client.  We got a blacklist-init
585  * message from this client, add him to the list of clients
586  * to query for blacklisting.
587  *
588  * @param cls unused
589  * @param client the client
590  * @param message the blacklist-init message that was sent
591  */
592 void
593 GST_blacklist_handle_init (void *cls, struct GNUNET_SERVER_Client *client,
594                            const struct GNUNET_MessageHeader *message)
595 {
596   struct Blacklisters *bl;
597   struct TestConnectionContext tcc;
598
599   bl = bl_head;
600   while (bl != NULL)
601   {
602     if (bl->client == client)
603     {
604       GNUNET_break (0);
605       GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
606       return;
607     }
608     bl = bl->next;
609   }
610   bl = GNUNET_malloc (sizeof (struct Blacklisters));
611   bl->client = client;
612   GNUNET_SERVER_client_keep (client);
613   GNUNET_CONTAINER_DLL_insert_after (bl_head, bl_tail, bl_tail, bl);
614
615   /* confirm that all existing connections are OK! */
616   tcc.bl = bl;
617   tcc.first = GNUNET_YES;
618   GST_neighbours_iterate (&test_connection_ok, &tcc);
619 }
620
621
622 /**
623  * A blacklisting client has sent us reply. Process it.
624  *
625  * @param cls unused
626  * @param client the client
627  * @param message the blacklist-init message that was sent
628  */
629 void
630 GST_blacklist_handle_reply (void *cls, struct GNUNET_SERVER_Client *client,
631                             const struct GNUNET_MessageHeader *message)
632 {
633   const struct BlacklistMessage *msg =
634       (const struct BlacklistMessage *) message;
635   struct Blacklisters *bl;
636   struct GST_BlacklistCheck *bc;
637
638   bl = bl_head;
639   while ((bl != NULL) && (bl->client != client))
640     bl = bl->next;
641   if (bl == NULL)
642   {
643 #if DEBUG_TRANSPORT
644     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Blacklist client disconnected\n");
645 #endif
646     /* FIXME: other error handling here!? */
647     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
648     return;
649   }
650   bc = bl->bc;
651   bl->bc = NULL;
652   bl->waiting_for_reply = GNUNET_NO;
653   if (NULL != bc)
654   {
655     /* only run this if the blacklist check has not been
656      * cancelled in the meantime... */
657     if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
658     {
659 #if DEBUG_TRANSPORT
660       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
661                   "Blacklist check failed, peer not allowed\n");
662 #endif
663       bc->cont (bc->cont_cls, &bc->peer, GNUNET_NO);
664       GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
665       GNUNET_free (bc);
666     }
667     else
668     {
669 #if DEBUG_TRANSPORT
670       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
671                   "Blacklist check succeeded, continuing with checks\n");
672 #endif
673       bc->bl_pos = bc->bl_pos->next;
674       bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
675     }
676   }
677   /* check if any other bc's are waiting for this blacklister */
678   bc = bc_head;
679   for (bc = bc_head; bc != NULL; bc = bc->next)
680     if ((bc->bl_pos == bl) && (GNUNET_SCHEDULER_NO_TASK == bc->task))
681     {
682       bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
683       break;
684     }
685 }
686
687
688 /**
689  * Add the given peer to the blacklist (for the given transport).
690  *
691  * @param peer peer to blacklist
692  * @param transport_name transport to blacklist for this peer, NULL for all
693  */
694 void
695 GST_blacklist_add_peer (const struct GNUNET_PeerIdentity *peer,
696                         const char *transport_name)
697 {
698 #if DEBUG_TRANSPORT
699   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700               "Adding peer `%s' with plugin `%s' to blacklist\n",
701               GNUNET_i2s (peer), transport_name);
702 #endif
703   if (blacklist == NULL)
704     blacklist =
705         GNUNET_CONTAINER_multihashmap_create (TRANSPORT_BLACKLIST_HT_SIZE);
706   GNUNET_CONTAINER_multihashmap_put (blacklist, &peer->hashPubKey,
707                                      GNUNET_strdup (transport_name),
708                                      GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
709 }
710
711
712 /**
713  * Test if the given blacklist entry matches.  If so,
714  * abort the iteration.
715  *
716  * @param cls the transport name to match (const char*)
717  * @param key the key (unused)
718  * @param value the 'char *' (name of a blacklisted transport)
719  * @return GNUNET_OK if the entry does not match, GNUNET_NO if it matches
720  */
721 static int
722 test_blacklisted (void *cls, const GNUNET_HashCode * key, void *value)
723 {
724   const char *transport_name = cls;
725   char *be = value;
726
727   /* blacklist check for specific no specific transport*/
728   if (transport_name == NULL)
729     return GNUNET_NO;
730
731   /* blacklist check for specific transport */
732   if (0 == strcmp (transport_name, be))
733     return GNUNET_NO;           /* abort iteration! */
734   return GNUNET_OK;
735 }
736
737
738 /**
739  * Test if a peer/transport combination is blacklisted.
740  *
741  * @param peer the identity of the peer to test
742  * @param transport_name name of the transport to test, never NULL
743  * @param cont function to call with result
744  * @param cont_cls closure for 'cont'
745  * @return handle to the blacklist check, NULL if the decision
746  *        was made instantly and 'cont' was already called
747  */
748 struct GST_BlacklistCheck *
749 GST_blacklist_test_allowed (const struct GNUNET_PeerIdentity *peer,
750                             const char *transport_name,
751                             GST_BlacklistTestContinuation cont, void *cont_cls)
752 {
753   struct GST_BlacklistCheck *bc;
754
755   GNUNET_assert (peer != NULL);
756
757   if ((blacklist != NULL) &&
758       (GNUNET_SYSERR ==
759        GNUNET_CONTAINER_multihashmap_get_multiple (blacklist, &peer->hashPubKey,
760                                                    &test_blacklisted,
761                                                    (void *) transport_name)))
762   {
763     /* disallowed by config, disapprove instantly */
764     GNUNET_STATISTICS_update (GST_stats,
765                               gettext_noop ("# disconnects due to blacklist"),
766                               1, GNUNET_NO);
767     if (cont != NULL)
768       cont (cont_cls, peer, GNUNET_NO);
769     return NULL;
770   }
771
772   if (bl_head == NULL)
773   {
774     /* no blacklist clients, approve instantly */
775     if (cont != NULL)
776       cont (cont_cls, peer, GNUNET_OK);
777     return NULL;
778   }
779
780   /* need to query blacklist clients */
781   bc = GNUNET_malloc (sizeof (struct GST_BlacklistCheck));
782   GNUNET_CONTAINER_DLL_insert (bc_head, bc_tail, bc);
783   bc->peer = *peer;
784   bc->cont = cont;
785   bc->cont_cls = cont_cls;
786   bc->bl_pos = bl_head;
787   bc->task = GNUNET_SCHEDULER_add_now (&do_blacklist_check, bc);
788   return bc;
789 }
790
791
792 /**
793  * Cancel a blacklist check.
794  *
795  * @param bc check to cancel
796  */
797 void
798 GST_blacklist_test_cancel (struct GST_BlacklistCheck *bc)
799 {
800   GNUNET_CONTAINER_DLL_remove (bc_head, bc_tail, bc);
801   if (bc->bl_pos != NULL)
802   {
803     if (bc->bl_pos->bc == bc)
804     {
805       /* we're at the head of the queue, remove us! */
806       bc->bl_pos->bc = NULL;
807     }
808   }
809   if (GNUNET_SCHEDULER_NO_TASK != bc->task)
810   {
811     GNUNET_SCHEDULER_cancel (bc->task);
812     bc->task = GNUNET_SCHEDULER_NO_TASK;
813   }
814   if (NULL != bc->th)
815   {
816     GNUNET_CONNECTION_notify_transmit_ready_cancel (bc->th);
817     bc->th = NULL;
818   }
819   GNUNET_free (bc);
820 }
821
822
823 /* end of file gnunet-service-transport_blacklist.c */