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 * Client responsible for this entry.
48 struct GNUNET_SERVER_Client *client;
54 * Information kept for each client registered to perform
60 * This is a linked list.
62 struct Blacklisters *next;
65 * This is a linked list.
67 struct Blacklisters *prev;
70 * Client responsible for this entry.
72 struct GNUNET_SERVER_Client *client;
78 * State of blacklist check to be performed for each
87 * Identity of the peer being checked.
89 struct GNUNET_PeerIdentity peer;
92 * Clients we still need to ask.
94 struct GNUNET_SERVER_Client *pending;
101 * Map of blacklisted peers (maps from peer identities
102 * to 'struct BlacklistEntry*' values).
104 static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
107 * Head of DLL of blacklisting clients.
109 static struct Blacklisters *bl_head;
112 * Tail of DLL of blacklisting clients.
114 static struct Blacklisters *bl_tail;
118 * Free the entries in the blacklist hash map.
120 * @param cls closure, unused
121 * @param key current key code
122 * @param value value in the hash map
123 * @return GNUNET_YES (continue to iterate)
126 free_blacklist_entry (void *cls,
127 const GNUNET_HashCode *key,
130 struct BlacklistEntry *be = value;
138 * Task run when we are shutting down. Cleans up.
140 * @param cls closure (unused)
141 * @param tc scheduler context (unused)
144 shutdown_task (void *cls,
145 const struct GNUNET_SCHEDULER_TaskContext *tc)
147 GNUNET_CONTAINER_multihashmap_iterate (blacklist,
148 &free_blacklist_entry,
150 GNUNET_CONTAINER_multihashmap_destroy (blacklist);
156 * Handle a request to start a blacklist.
158 * @param cls closure (always NULL)
159 * @param client identification of the client
160 * @param message the actual message
163 GNUNET_TRANSPORT_handle_blacklist_init (void *cls,
164 struct GNUNET_SERVER_Client *client,
165 const struct GNUNET_MessageHeader *message)
167 struct Blacklisters *bl;
169 bl = GNUNET_malloc (sizeof (struct Blacklisters));
171 GNUNET_SERVER_client_keep (client);
172 GNUNET_CONTAINER_DLL_insert (bl_head, bl_tail, bl);
173 /* FIXME: confirm that all existing connections are OK! */
178 * Handle a request to blacklist a peer.
180 * @param cls closure (always NULL)
181 * @param client identification of the client
182 * @param message the actual message
185 GNUNET_TRANSPORT_handle_blacklist_reply (void *cls,
186 struct GNUNET_SERVER_Client *client,
187 const struct GNUNET_MessageHeader *message)
189 struct Blacklisters *bl;
190 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
193 while ( (bl != NULL) &&
194 (bl->client != client) )
198 GNUNET_SERVER_client_done (client, GNUNET_SYSERR);
201 if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
203 be = GNUNET_malloc (sizeof (struct BlacklistEntry));
204 be->peer = msg->peer;
206 GNUNET_CONTAINER_multihashmap_put (blacklist,
207 &msg->peer.hashPubKey,
209 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
211 /* FIXME: trigger continuation... */
216 * Notify the given client about all entries in the blacklist.
218 * @param cls closure, refers to the 'struct GNUNET_SERVER_Client' to notify
219 * @param key current key code (peer identity, not used)
220 * @param value value in the hash map, the 'struct BlacklistEntry*'
221 * @return GNUNET_YES (continue to iterate)
224 notify_blacklist_entry (void *cls,
225 const GNUNET_HashCode *key,
228 struct GNUNET_SERVER_Client *client = cls;
229 struct BlacklistEntry *be = value;
230 struct BlacklistMessage msg;
232 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST);
233 msg.header.size = htons (sizeof (struct BlacklistMessage));
234 msg.reserved = htonl (0);
236 msg.until = GNUNET_TIME_absolute_hton (be->until);
237 GNUNET_SERVER_notification_context_unicast (blacklist_notifiers,
246 * Handle a request for notification of blacklist changes.
248 * @param cls closure (always NULL)
249 * @param client identification of the client
250 * @param message the actual message
253 GNUNET_TRANSPORT_handle_blacklist_notify (void *cls,
254 struct GNUNET_SERVER_Client *client,
255 const struct GNUNET_MessageHeader *message)
257 GNUNET_SERVER_notification_context_add (blacklist_notifiers, client);
258 GNUNET_CONTAINER_multihashmap_iterate (blacklist,
259 ¬ify_blacklist_entry,
265 * Is the given peer currently blacklisted?
267 * @param id identity of the peer
268 * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
271 GNUNET_TRANSPORT_blacklist_check (const struct GNUNET_PeerIdentity *id)
273 if (GNUNET_CONTAINER_multihashmap_contains (blacklist, &id->hashPubKey))
280 * Initialize the blacklisting subsystem.
282 * @param server server of the transport service
283 * @param s scheduler to use
286 GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SERVER_Handle *server,
287 struct GNUNET_SCHEDULER_Handle *s)
290 blacklist = GNUNET_CONTAINER_multihashmap_create (4);
291 GNUNET_SCHEDULER_add_delayed (sched,
292 GNUNET_TIME_UNIT_FOREVER_REL,
298 /* end of gnunet-service-transport_blacklist.c */