aboutsummaryrefslogtreecommitdiff
path: root/src/transport/gnunet-service-transport_blacklist.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2010-01-22 14:47:45 +0000
committerChristian Grothoff <christian@grothoff.org>2010-01-22 14:47:45 +0000
commit3f3d51ef20ae4ef5017ca92ed1f6e153322f3a95 (patch)
treedb90b586ad9747655c379c80cfb24a565d8bfe98 /src/transport/gnunet-service-transport_blacklist.c
parent1fb0d6c5f735da2f8feb0d3f39bc27a1ff8e2723 (diff)
downloadgnunet-3f3d51ef20ae4ef5017ca92ed1f6e153322f3a95.tar.gz
gnunet-3f3d51ef20ae4ef5017ca92ed1f6e153322f3a95.zip
finishing blacklist implementation
Diffstat (limited to 'src/transport/gnunet-service-transport_blacklist.c')
-rw-r--r--src/transport/gnunet-service-transport_blacklist.c169
1 files changed, 106 insertions, 63 deletions
diff --git a/src/transport/gnunet-service-transport_blacklist.c b/src/transport/gnunet-service-transport_blacklist.c
index b51f51442..5ddf9c1bf 100644
--- a/src/transport/gnunet-service-transport_blacklist.c
+++ b/src/transport/gnunet-service-transport_blacklist.c
@@ -37,63 +37,20 @@
37struct BlacklistEntry 37struct BlacklistEntry
38{ 38{
39 /** 39 /**
40 * How long until this entry times out? 40 * Identity of the peer being blacklisted by this entry.
41 */ 41 * (also equivalent to the key)
42 struct GNUNET_TIME_Absolute until;
43
44 /**
45 * Task scheduled to run the moment the time does run out.
46 */
47 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
48};
49
50
51/**
52 * Entry in list of notifications still to transmit to
53 * a client.
54 */
55struct PendingNotificationList
56{
57
58 /**
59 * This is a linked list.
60 */
61 struct PendingNotificationList *next;
62
63 /**
64 * Identity of the peer to send notification about.
65 */ 42 */
66 struct GNUNET_PeerIdentity peer; 43 struct GNUNET_PeerIdentity peer;
67 44
68};
69
70
71/**
72 * List of clients to notify whenever the blacklist changes.
73 */
74struct BlacklistNotificationList
75{
76
77 /** 45 /**
78 * This is a linked list. 46 * How long until this entry times out?
79 */
80 struct BlacklistNotificationList *next;
81
82 /**
83 * Client to notify.
84 */ 47 */
85 struct GNUNET_SERVER_Client *client; 48 struct GNUNET_TIME_Absolute until;
86
87 /**
88 * Pending request for transmission to client, or NULL.
89 */
90 struct GNUNET_CONNECTION_TransmitHandle *req;
91 49
92 /** 50 /**
93 * Blacklist entries that still need to be submitted. 51 * Task scheduled to run the moment the time does run out.
94 */ 52 */
95 struct PendingNotificationList *pending; 53 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
96
97}; 54};
98 55
99 56
@@ -104,9 +61,9 @@ struct BlacklistNotificationList
104static struct GNUNET_CONTAINER_MultiHashMap *blacklist; 61static struct GNUNET_CONTAINER_MultiHashMap *blacklist;
105 62
106/** 63/**
107 * Linked list of clients to notify whenever the blacklist changes. 64 * Notifications for blacklisting.
108 */ 65 */
109static struct BlacklistNotificationList *blacklist_notifiers; 66static struct GNUNET_SERVER_NotificationContext *blacklist_notifiers;
110 67
111/** 68/**
112 * Our scheduler. 69 * Our scheduler.
@@ -120,9 +77,7 @@ static struct GNUNET_SCHEDULER_Handle *sched;
120 * @param cls closure, unused 77 * @param cls closure, unused
121 * @param key current key code 78 * @param key current key code
122 * @param value value in the hash map 79 * @param value value in the hash map
123 * @return GNUNET_YES if we should continue to 80 * @return GNUNET_YES (continue to iterate)
124 * iterate,
125 * GNUNET_NO if not.
126 */ 81 */
127static int 82static int
128free_blacklist_entry (void *cls, 83free_blacklist_entry (void *cls,
@@ -152,6 +107,38 @@ shutdown_task (void *cls,
152 &free_blacklist_entry, 107 &free_blacklist_entry,
153 NULL); 108 NULL);
154 GNUNET_CONTAINER_multihashmap_destroy (blacklist); 109 GNUNET_CONTAINER_multihashmap_destroy (blacklist);
110 blacklist = NULL;
111 GNUNET_SERVER_notification_context_destroy (blacklist_notifiers);
112 blacklist_notifiers = NULL;
113}
114
115
116/**
117 * Task run when a blacklist entry times out.
118 *
119 * @param cls closure (the 'struct BlacklistEntry*')
120 * @param tc scheduler context (unused)
121 */
122static void
123timeout_task (void *cls,
124 const struct GNUNET_SCHEDULER_TaskContext *tc)
125{
126 struct BlacklistEntry *be = cls;
127 struct BlacklistMessage msg;
128
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);
133 msg.peer = be->peer;
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,
137 be));
138 GNUNET_free (be);
139 GNUNET_SERVER_notification_context_broadcast (blacklist_notifiers,
140 &msg.header,
141 GNUNET_NO);
155} 142}
156 143
157 144
@@ -167,12 +154,68 @@ GNUNET_TRANSPORT_handle_blacklist (void *cls,
167 struct GNUNET_SERVER_Client *client, 154 struct GNUNET_SERVER_Client *client,
168 const struct GNUNET_MessageHeader *message) 155 const struct GNUNET_MessageHeader *message)
169{ 156{
170 /* FIXME */ 157 struct BlacklistEntry *be;
158 const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
159
160 be = GNUNET_CONTAINER_multihashmap_get (blacklist,
161 &be->peer.hashPubKey);
162 if (be != NULL)
163 {
164 GNUNET_SCHEDULER_cancel (sched,
165 be->timeout_task);
166 }
167 else
168 {
169 be = GNUNET_malloc (sizeof (struct BlacklistEntry));
170 be->peer = msg->peer;
171 GNUNET_CONTAINER_multihashmap_put (blacklist,
172 &be->peer.hashPubKey,
173 be,
174 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY);
175 }
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),
179 &timeout_task,
180 be);
181 GNUNET_SERVER_notification_context_broadcast (blacklist_notifiers,
182 &msg->header,
183 GNUNET_NO);
171 GNUNET_SERVER_receive_done (client, GNUNET_OK); 184 GNUNET_SERVER_receive_done (client, GNUNET_OK);
172} 185}
173 186
174 187
175/** 188/**
189 * Notify the given client about all entries in the blacklist.
190 *
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)
195 */
196static int
197notify_blacklist_entry (void *cls,
198 const GNUNET_HashCode *key,
199 void *value)
200{
201 struct GNUNET_SERVER_Client *client = cls;
202 struct BlacklistEntry *be = value;
203 struct BlacklistMessage msg;
204
205 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST);
206 msg.header.size = htons (sizeof (struct BlacklistMessage));
207 msg.reserved = htonl (0);
208 msg.peer = be->peer;
209 msg.until = GNUNET_TIME_absolute_hton (be->until);
210 GNUNET_SERVER_notification_context_unicast (blacklist_notifiers,
211 client,
212 &msg.header,
213 GNUNET_NO);
214 return GNUNET_YES;
215}
216
217
218/**
176 * Handle a request for notification of blacklist changes. 219 * Handle a request for notification of blacklist changes.
177 * 220 *
178 * @param cls closure (always NULL) 221 * @param cls closure (always NULL)
@@ -184,13 +227,10 @@ GNUNET_TRANSPORT_handle_blacklist_notify (void *cls,
184 struct GNUNET_SERVER_Client *client, 227 struct GNUNET_SERVER_Client *client,
185 const struct GNUNET_MessageHeader *message) 228 const struct GNUNET_MessageHeader *message)
186{ 229{
187 struct BlacklistNotificationList *bnl; 230 GNUNET_SERVER_notification_context_add (blacklist_notifiers, client);
188 231 GNUNET_CONTAINER_multihashmap_iterate (blacklist,
189 bnl = GNUNET_malloc (sizeof (struct BlacklistNotificationList)); 232 &notify_blacklist_entry,
190 bnl->next = blacklist_notifiers; 233 client);
191 blacklist_notifiers = bnl;
192 /* FIXME */
193 GNUNET_SERVER_receive_done (client, GNUNET_OK);
194} 234}
195 235
196 236
@@ -213,7 +253,8 @@ GNUNET_TRANSPORT_blacklist_check (const struct GNUNET_PeerIdentity *id)
213 * @param s scheduler to use 253 * @param s scheduler to use
214 */ 254 */
215void 255void
216GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SCHEDULER_Handle *s) 256GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SERVER_Handle *server,
257 struct GNUNET_SCHEDULER_Handle *s)
217{ 258{
218 sched = s; 259 sched = s;
219 blacklist = GNUNET_CONTAINER_multihashmap_create (4); 260 blacklist = GNUNET_CONTAINER_multihashmap_create (4);
@@ -221,6 +262,8 @@ GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SCHEDULER_Handle *s)
221 GNUNET_TIME_UNIT_FOREVER_REL, 262 GNUNET_TIME_UNIT_FOREVER_REL,
222 &shutdown_task, 263 &shutdown_task,
223 NULL); 264 NULL);
265 blacklist_notifiers = GNUNET_SERVER_notification_context_create (server, 0);
224} 266}
225 267
268
226/* end of gnunet-service-transport_blacklist.c */ 269/* end of gnunet-service-transport_blacklist.c */