stuff
[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_service_lib.h"
29 #include "transport.h"
30 #include "gnunet-service-transport_blacklist.h"
31
32
33 /**
34  * Information kept for each blacklisted peer.
35  */
36 struct BlacklistEntry
37 {
38   /**
39    * How long until this entry times out?
40    */
41   struct GNUNET_TIME_Absolute until;
42
43   /**
44    * Task scheduled to run the moment the time does run out.
45    */
46   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
47 };
48
49
50 /**
51  * Entry in list of notifications still to transmit to
52  * a client.
53  */
54 struct PendingNotificationList 
55 {
56
57   /**
58    * This is a linked list.
59    */
60   struct PendingNotificationList *next;
61
62   /**
63    * Identity of the peer to send notification about.
64    */
65   struct GNUNET_PeerIdentity peer;
66
67 };
68
69
70 /**
71  * List of clients to notify whenever the blacklist changes.
72  */
73 struct BlacklistNotificationList
74 {
75
76   /**
77    * This is a linked list.
78    */
79   struct BlacklistNotificationList *next;
80
81   /**
82    * Client to notify.
83    */
84   struct GNUNET_SERVER_Client *client;
85
86   /**
87    * Pending request for transmission to client, or NULL.
88    */ 
89   struct GNUNET_CONNECTION_TransmitHandle *req;
90
91   /**
92    * Blacklist entries that still need to be submitted.
93    */
94   struct PendingNotificationList *pending;
95   
96 };
97
98
99 /**
100  * Map of blacklisted peers (maps from peer identities
101  * to 'struct BlacklistEntry*' values).
102  */
103 static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
104
105 /**
106  * Linked list of clients to notify whenever the blacklist changes.
107  */
108 static struct BlacklistNotificationList *blacklist_notifiers;
109
110 /**
111  * Our scheduler.
112  */
113 static struct GNUNET_SCHEDULER_Handle *sched;
114
115
116 /**
117  * Free the entries in the blacklist hash map.
118  *
119  * @param cls closure, unused
120  * @param key current key code
121  * @param value value in the hash map
122  * @return GNUNET_YES if we should continue to
123  *         iterate,
124  *         GNUNET_NO if not.
125  */
126 static int
127 free_blacklist_entry (void *cls,
128                       const GNUNET_HashCode *key,
129                       void *value)
130 {
131   struct BlacklistEntry *be = value;
132
133   GNUNET_SCHEDULER_cancel (sched,
134                            be->timeout_task);
135   GNUNET_free (be);
136   return GNUNET_YES;
137 }
138
139
140 static void 
141 shutdown_task (void *cls,
142                const struct GNUNET_SCHEDULER_TaskContext *tc)
143 {
144   GNUNET_CONTAINER_multihashmap_iterate (blacklist,
145                                          &free_blacklist_entry,
146                                          NULL);
147   GNUNET_CONTAINER_multihashmap_destroy (blacklist);
148 }
149
150
151 /**
152  * Handle a request to blacklist a peer.
153  */
154 void
155 GNUNET_TRANSPORT_handle_blacklist (void *cls,
156                                    struct GNUNET_SERVER_Client *client,
157                                    const struct GNUNET_MessageHeader *message)
158 {
159 }
160
161
162 /**
163  * Handle a request for notification of blacklist changes.
164  */
165 void
166 GNUNET_TRANSPORT_handle_blacklist_notify (void *cls,
167                                           struct GNUNET_SERVER_Client *client,
168                                           const struct GNUNET_MessageHeader *message)
169 {
170 }
171
172
173 /**
174  * Is the given peer currently blacklisted?
175  *
176  * @param id identity of the peer
177  * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
178  */
179 int
180 GNUNET_TRANSPORT_blacklist_check (const struct GNUNET_PeerIdentity *id)
181 {
182   return GNUNET_CONTAINER_multihashmap_contains (blacklist, &id->hashPubKey);
183 }
184
185
186 /**
187  * Initialize the blacklisting subsystem.
188  *
189  * @param s scheduler to use
190  */
191 void 
192 GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SCHEDULER_Handle *s)
193 {
194   sched = s;
195   blacklist = GNUNET_CONTAINER_multihashmap_create (4);
196   GNUNET_SCHEDULER_add_delayed (sched,
197                                 GNUNET_TIME_UNIT_FOREVER_REL,
198                                 &shutdown_task,
199                                 NULL);
200 }
201
202 /* end of gnunet-service-transport_blacklist.c */