2 This file is part of GNUnet.
3 (C) 2010 Christian Grothoff (and other contributing authors)
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 2, or (at your
8 option) any later version.
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.
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.
22 * @file transport/gnunet-service-transport_blacklist.c
23 * @brief low-level P2P messaging
24 * @author Christian Grothoff
27 #include "gnunet_protocols.h"
28 #include "gnunet_util_lib.h"
29 #include "gnunet_service_lib.h"
30 #include "transport.h"
31 #include "gnunet-service-transport_blacklist.h"
35 * Information kept for each blacklisted peer.
40 * Identity of the peer being blacklisted by this entry.
41 * (also equivalent to the key)
43 struct GNUNET_PeerIdentity peer;
46 * How long until this entry times out?
48 struct GNUNET_TIME_Absolute until;
51 * Task scheduled to run the moment the time does run out.
53 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
58 * Map of blacklisted peers (maps from peer identities
59 * to 'struct BlacklistEntry*' values).
61 static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
64 * Notifications for blacklisting.
66 static struct GNUNET_SERVER_NotificationContext *blacklist_notifiers;
71 static struct GNUNET_SCHEDULER_Handle *sched;
75 * Free the entries in the blacklist hash map.
77 * @param cls closure, unused
78 * @param key current key code
79 * @param value value in the hash map
80 * @return GNUNET_YES (continue to iterate)
83 free_blacklist_entry (void *cls,
84 const GNUNET_HashCode *key,
87 struct BlacklistEntry *be = value;
89 GNUNET_SCHEDULER_cancel (sched,
97 * Task run when we are shutting down. Cleans up.
99 * @param cls closure (unused)
100 * @param tc scheduler context (unused)
103 shutdown_task (void *cls,
104 const struct GNUNET_SCHEDULER_TaskContext *tc)
106 GNUNET_CONTAINER_multihashmap_iterate (blacklist,
107 &free_blacklist_entry,
109 GNUNET_CONTAINER_multihashmap_destroy (blacklist);
111 GNUNET_SERVER_notification_context_destroy (blacklist_notifiers);
112 blacklist_notifiers = NULL;
117 * Task run when a blacklist entry times out.
119 * @param cls closure (the 'struct BlacklistEntry*')
120 * @param tc scheduler context (unused)
123 timeout_task (void *cls,
124 const struct GNUNET_SCHEDULER_TaskContext *tc)
126 struct BlacklistEntry *be = cls;
127 struct BlacklistMessage msg;
129 be->timeout_task = GNUNET_SCHEDULER_NO_TASK;
130 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST);
131 msg.header.size = htons (sizeof (struct BlacklistMessage));
132 msg.reserved = htonl (0);
134 msg.until = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_ZERO_ABS);
135 GNUNET_assert (GNUNET_YES == GNUNET_CONTAINER_multihashmap_remove (blacklist,
136 &be->peer.hashPubKey,
139 GNUNET_SERVER_notification_context_broadcast (blacklist_notifiers,
146 * Handle a request to blacklist a peer.
148 * @param cls closure (always NULL)
149 * @param client identification of the client
150 * @param message the actual message
153 GNUNET_TRANSPORT_handle_blacklist (void *cls,
154 struct GNUNET_SERVER_Client *client,
155 const struct GNUNET_MessageHeader *message)
157 struct BlacklistEntry *be;
158 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
160 be = GNUNET_CONTAINER_multihashmap_get (blacklist,
161 &be->peer.hashPubKey);
164 GNUNET_SCHEDULER_cancel (sched,
169 be = GNUNET_malloc (sizeof (struct BlacklistEntry));
170 be->peer = msg->peer;
171 GNUNET_CONTAINER_multihashmap_put (blacklist,
172 &be->peer.hashPubKey,
174 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
176 be->until = GNUNET_TIME_absolute_ntoh (msg->until);
177 be->timeout_task = GNUNET_SCHEDULER_add_delayed (sched,
178 GNUNET_TIME_absolute_get_remaining (be->until),
181 GNUNET_SERVER_notification_context_broadcast (blacklist_notifiers,
184 GNUNET_SERVER_receive_done (client, GNUNET_OK);
189 * Notify the given client about all entries in the blacklist.
191 * @param cls closure, refers to the 'struct GNUNET_SERVER_Client' to notify
192 * @param key current key code (peer identity, not used)
193 * @param value value in the hash map, the 'struct BlacklistEntry*'
194 * @return GNUNET_YES (continue to iterate)
197 notify_blacklist_entry (void *cls,
198 const GNUNET_HashCode *key,
201 struct GNUNET_SERVER_Client *client = cls;
202 struct BlacklistEntry *be = value;
203 struct BlacklistMessage msg;
205 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST);
206 msg.header.size = htons (sizeof (struct BlacklistMessage));
207 msg.reserved = htonl (0);
209 msg.until = GNUNET_TIME_absolute_hton (be->until);
210 GNUNET_SERVER_notification_context_unicast (blacklist_notifiers,
219 * Handle a request for notification of blacklist changes.
221 * @param cls closure (always NULL)
222 * @param client identification of the client
223 * @param message the actual message
226 GNUNET_TRANSPORT_handle_blacklist_notify (void *cls,
227 struct GNUNET_SERVER_Client *client,
228 const struct GNUNET_MessageHeader *message)
230 GNUNET_SERVER_notification_context_add (blacklist_notifiers, client);
231 GNUNET_CONTAINER_multihashmap_iterate (blacklist,
232 ¬ify_blacklist_entry,
238 * Is the given peer currently blacklisted?
240 * @param id identity of the peer
241 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
244 GNUNET_TRANSPORT_blacklist_check (const struct GNUNET_PeerIdentity *id)
246 return GNUNET_CONTAINER_multihashmap_contains (blacklist, &id->hashPubKey);
251 * Initialize the blacklisting subsystem.
253 * @param s scheduler to use
256 GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SERVER_Handle *server,
257 struct GNUNET_SCHEDULER_Handle *s)
260 blacklist = GNUNET_CONTAINER_multihashmap_create (4);
261 GNUNET_SCHEDULER_add_delayed (sched,
262 GNUNET_TIME_UNIT_FOREVER_REL,
265 blacklist_notifiers = GNUNET_SERVER_notification_context_create (server, 0);
269 /* end of gnunet-service-transport_blacklist.c */