adding blacklisting code, untested
[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  * Handle a request to start a blacklist.
35  *
36  * @param cls closure (always NULL)
37  * @param client identification of the client
38  * @param message the actual message
39  */
40 void
41 GNUNET_TRANSPORT_handle_blacklist_init (void *cls,
42                                         struct GNUNET_SERVER_Client *client,
43                                         const struct GNUNET_MessageHeader *message)
44 {
45   struct Blacklisters *bl;
46
47   bl = GNUNET_malloc (sizeof (struct Blacklisters));
48   bl->client = client;
49   GNUNET_SERVER_client_keep (client);
50   GNUNET_CONTAINER_DLL_insert (bl_head, bl_tail, bl);
51   /* FIXME: confirm that all existing connections are OK! */
52 }
53
54
55 /**
56  * Handle a request to blacklist a peer.
57  *
58  * @param cls closure (always NULL)
59  * @param client identification of the client
60  * @param message the actual message
61  */
62 void
63 GNUNET_TRANSPORT_handle_blacklist_reply (void *cls,
64                                          struct GNUNET_SERVER_Client *client,
65                                          const struct GNUNET_MessageHeader *message)
66 {
67   struct Blacklisters *bl;
68   const struct BlacklistMessage *msg = (const struct BlacklistMessage*) message;
69
70   bl = bl_head;
71   while ( (bl != NULL) &&
72           (bl->client != client) )
73     bl = bl->next;
74   if (bl == NULL)
75     {
76       GNUNET_SERVER_client_done (client, GNUNET_SYSERR);
77       return;
78     }
79   if (ntohl (msg->is_allowed) == GNUNET_SYSERR)
80     {    
81       be = GNUNET_malloc (sizeof (struct BlacklistEntry));
82       be->peer = msg->peer;
83       be->client = client;
84       GNUNET_CONTAINER_multihashmap_put (blacklist,
85                                          &msg->peer.hashPubKey,
86                                          be,
87                                          GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
88     }
89   /* FIXME: trigger continuation... */
90 }
91
92
93 /**
94  * Notify the given client about all entries in the blacklist.
95  *
96  * @param cls closure, refers to the 'struct GNUNET_SERVER_Client' to notify
97  * @param key current key code (peer identity, not used)
98  * @param value value in the hash map, the 'struct BlacklistEntry*'
99  * @return GNUNET_YES (continue to iterate)
100  */
101 static int
102 notify_blacklist_entry (void *cls,
103                         const GNUNET_HashCode *key,
104                         void *value)
105 {
106   struct GNUNET_SERVER_Client *client = cls;
107   struct BlacklistEntry *be = value;
108   struct BlacklistMessage msg;
109
110   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_BLACKLIST);
111   msg.header.size = htons (sizeof (struct BlacklistMessage));
112   msg.reserved = htonl (0);
113   msg.peer = be->peer;
114   msg.until = GNUNET_TIME_absolute_hton (be->until);
115   GNUNET_SERVER_notification_context_unicast (blacklist_notifiers,
116                                               client,
117                                               &msg.header,
118                                               GNUNET_NO);
119   return GNUNET_YES;
120 }
121
122
123 /**
124  * Handle a request for notification of blacklist changes.
125  *
126  * @param cls closure (always NULL)
127  * @param client identification of the client
128  * @param message the actual message
129  */
130 void
131 GNUNET_TRANSPORT_handle_blacklist_notify (void *cls,
132                                           struct GNUNET_SERVER_Client *client,
133                                           const struct GNUNET_MessageHeader *message)
134 {
135   GNUNET_SERVER_notification_context_add (blacklist_notifiers, client);
136   GNUNET_CONTAINER_multihashmap_iterate (blacklist,
137                                          &notify_blacklist_entry,
138                                          client);
139 }
140
141
142 /**
143  * Is the given peer currently blacklisted?
144  *
145  * @param id identity of the peer
146  * @return GNUNET_YES if the peer is blacklisted, GNUNET_NO if not
147  */
148 int
149 GNUNET_TRANSPORT_blacklist_check (const struct GNUNET_PeerIdentity *id)
150 {
151   if (GNUNET_CONTAINER_multihashmap_contains (blacklist, &id->hashPubKey))    
152     return GNUNET_YES;
153   
154 }
155
156
157 /**
158  * Initialize the blacklisting subsystem.
159  *
160  * @param server server of the transport service
161  * @param s scheduler to use
162  */
163 void 
164 GNUNET_TRANSPORT_blacklist_init (struct GNUNET_SERVER_Handle *server,
165                                  struct GNUNET_SCHEDULER_Handle *s)
166 {
167   sched = s;
168   blacklist = GNUNET_CONTAINER_multihashmap_create (4);
169   GNUNET_SCHEDULER_add_delayed (sched,
170                                 GNUNET_TIME_UNIT_FOREVER_REL,
171                                 &shutdown_task,
172                                 NULL);
173 }
174
175
176 /* end of gnunet-service-transport_blacklist.c */