08ed2c9d2174e842fd40d4c70296c317068dcde9
[oweals/gnunet.git] / src / transport / gnunet-service-transport_blacklist.c
1 /*
2      This file is part of GNUnet.
3      (C) 2010 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 2, 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 low-level P2P messaging
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
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"
32
33
34 /**
35  * Information kept for each blacklisted peer.
36  */
37 struct BlacklistEntry
38 {
39   /**
40    * Identity of the peer being blacklisted by this entry.
41    * (also equivalent to the key)  
42    */
43   struct GNUNET_PeerIdentity peer;
44
45   /**
46    * Client responsible for this entry.
47    */
48   struct GNUNET_SERVER_Client *client;
49
50 };
51
52
53 /**
54  * Information kept for each client registered to perform
55  * blacklisting.
56  */
57 struct Blacklisters
58 {
59   /**
60    * This is a linked list.
61    */
62   struct Blacklisters *next;
63
64   /**
65    * This is a linked list.
66    */
67   struct Blacklisters *prev;
68
69   /**
70    * Client responsible for this entry.
71    */
72   struct GNUNET_SERVER_Client *client;
73
74 };
75
76
77 /**
78  * State of blacklist check to be performed for each
79  * connecting peer.
80  */
81 struct BlacklistCheck
82 {
83
84   
85
86   /**
87    * Identity of the peer being checked.
88    */
89   struct GNUNET_PeerIdentity peer;
90
91   /**
92    * Clients we still need to ask.
93    */
94   struct GNUNET_SERVER_Client *pending;
95
96 };
97
98
99
100 /**
101  * Map of blacklisted peers (maps from peer identities
102  * to 'struct BlacklistEntry*' values).
103  */
104 static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
105
106 /**
107  * Head of DLL of blacklisting clients.
108  */
109 static struct Blacklisters *bl_head;
110
111 /**
112  * Tail of DLL of blacklisting clients.
113  */
114 static struct Blacklisters *bl_tail;
115
116
117 /**
118  * Free the entries in the blacklist hash map.
119  *
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)
124  */
125 static int
126 free_blacklist_entry (void *cls,
127                       const GNUNET_HashCode *key,
128                       void *value)
129 {
130   struct BlacklistEntry *be = value;
131
132   GNUNET_free (be);
133   return GNUNET_YES;
134 }
135
136
137 /**
138  * Task run when we are shutting down.  Cleans up.
139  *
140  * @param cls closure (unused)
141  * @param tc scheduler context (unused)
142  */
143 static void 
144 shutdown_task (void *cls,
145                const struct GNUNET_SCHEDULER_TaskContext *tc)
146 {
147   GNUNET_CONTAINER_multihashmap_iterate (blacklist,
148                                          &free_blacklist_entry,
149                                          NULL);
150   GNUNET_CONTAINER_multihashmap_destroy (blacklist);
151   blacklist = NULL;
152 }
153
154
155 /**
156  * Handle a request to start a blacklist.
157  *
158  * @param cls closure (always NULL)
159  * @param client identification of the client
160  * @param message the actual message
161  */
162 void
163 GNUNET_TRANSPORT_handle_blacklist_init (void *cls,
164                                         struct GNUNET_SERVER_Client *client,
165                                         const struct GNUNET_MessageHeader *message)
166 {
167   struct Blacklisters *bl;
168
169   bl = GNUNET_malloc (sizeof (struct Blacklisters));
170   bl->client = client;
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! */
174 }
175
176
177 /**
178  * Handle a request to blacklist a peer.
179  *
180  * @param cls closure (always NULL)
181  * @param client identification of the client
182  * @param message the actual message
183  */
184 void
185 GNUNET_TRANSPORT_handle_blacklist_reply (void *cls,
186                                          struct GNUNET_SERVER_Client *client,
187                                          const struct GNUNET_MessageHeader *message)
188 {
189   struct Blacklisters *bl;
190   const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
191
192   bl = bl_head;
193   while ( (bl != NULL) &&
194           (bl->client != client) )
195     bl = bl->next;
196   if (bl == NULL)
197     {
198       GNUNET_SERVER_client_done (client, GNUNET_SYSERR);
199       return;
200     }
201   if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
202     {    
203       be = GNUNET_malloc (sizeof (struct BlacklistEntry));
204       be->peer = msg->peer;
205       be->client = client;
206       GNUNET_CONTAINER_multihashmap_put (blacklist,
207                                          &msg->peer.hashPubKey,
208                                          be,
209                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
210     }
211   /* FIXME: trigger continuation... */
212 }
213
214
215 /**
216  * Notify the given client about all entries in the blacklist.
217  *
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)
222  */
223 static int
224 notify_blacklist_entry (void *cls,
225                         const GNUNET_HashCode *key,
226                         void *value)
227 {
228   struct GNUNET_SERVER_Client *client = cls;
229   struct BlacklistEntry *be = value;
230   struct BlacklistMessage msg;
231
232   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST);
233   msg.header.size = htons (sizeof (struct BlacklistMessage));
234   msg.reserved = htonl (0);
235   msg.peer = be->peer;
236   msg.until = GNUNET_TIME_absolute_hton (be->until);
237   GNUNET_SERVER_notification_context_unicast (blacklist_notifiers,
238                                               client,
239                                               &msg.header,
240                                               GNUNET_NO);
241   return GNUNET_YES;
242 }
243
244
245 /**
246  * Handle a request for notification of blacklist changes.
247  *
248  * @param cls closure (always NULL)
249  * @param client identification of the client
250  * @param message the actual message
251  */
252 void
253 GNUNET_TRANSPORT_handle_blacklist_notify (void *cls,
254                                           struct GNUNET_SERVER_Client *client,
255                                           const struct GNUNET_MessageHeader *message)
256 {
257   GNUNET_SERVER_notification_context_add (blacklist_notifiers, client);
258   GNUNET_CONTAINER_multihashmap_iterate (blacklist,
259                                          &notify_blacklist_entry,
260                                          client);
261 }
262
263
264 /**
265  * Is the given peer currently blacklisted?
266  *
267  * @param id identity of the peer
268  * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
269  */
270 int
271 GNUNET_TRANSPORT_blacklist_check (const struct GNUNET_PeerIdentity *id)
272 {
273   if (GNUNET_CONTAINER_multihashmap_contains (blacklist, &id->hashPubKey))    
274     return GNUNET_YES;
275   
276 }
277
278
279 /**
280  * Initialize the blacklisting subsystem.
281  *
282  * @param server server of the transport service
283  * @param s scheduler to use
284  */
285 void 
286 GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SERVER_Handle *server,
287                                  struct GNUNET_SCHEDULER_Handle *s)
288 {
289   sched = s;
290   blacklist = GNUNET_CONTAINER_multihashmap_create (4);
291   GNUNET_SCHEDULER_add_delayed (sched,
292                                 GNUNET_TIME_UNIT_FOREVER_REL,
293                                 &shutdown_task,
294                                 NULL);
295 }
296
297
298 /* end of gnunet-service-transport_blacklist.c */