533fd0ef72b3d5821757984dbf0e126621a10a7a
[oweals/gnunet.git] / src / set / gnunet-service-set.h
1 /*
2       This file is part of GNUnet
3       (C) 2013 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 set/gnunet-service-set.h
23  * @brief common components for the implementation the different set operations
24  * @author Florian Dold
25  */
26
27 #ifndef GNUNET_SERVICE_SET_H_PRIVATE
28 #define GNUNET_SERVICE_SET_H_PRIVATE
29
30 #include "platform.h"
31 #include "gnunet_common.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_applications.h"
34 #include "gnunet_util_lib.h"
35 #include "gnunet_core_service.h"
36 #include "gnunet_mesh2_service.h"
37 #include "gnunet_set_service.h"
38 #include "set.h"
39
40
41 /* FIXME: cfuchs */
42 struct IntersectionState;
43
44
45 /**
46  * Extra state required for set union.
47  */
48 struct UnionState;
49
50 struct UnionEvaluateOperation;
51
52
53 /**
54  * A set that supports a specific operation
55  * with other peers.
56  */
57 struct Set
58 {
59   /**
60    * Client that owns the set.
61    * Only one client may own a set.
62    */
63   struct GNUNET_SERVER_Client *client;
64
65   /**
66    * Message queue for the client
67    */
68   struct GNUNET_MQ_Handle *client_mq;
69
70   /**
71    * Type of operation supported for this set
72    */
73   uint32_t operation; // use enum from API
74
75   /**
76    * Sets are held in a doubly linked list.
77    */
78   struct Set *next;
79
80   /**
81    * Sets are held in a doubly linked list.
82    */
83   struct Set *prev;
84
85   /**
86    * Appropriate state for each type of
87    * operation.
88    */
89   union {
90     struct IntersectionState *i;
91     struct UnionState *u;
92   } state;
93 };
94
95
96 /**
97  * A listener is inhabited by a client, and
98  * waits for evaluation requests from remote peers.
99  */
100 struct Listener
101 {
102   /**
103    * Listeners are held in a doubly linked list.
104    */
105   struct Listener *next;
106
107   /**
108    * Listeners are held in a doubly linked list.
109    */
110   struct Listener *prev;
111
112   /**
113    * Client that owns the listener.
114    * Only one client may own a listener.
115    */
116   struct GNUNET_SERVER_Client *client;
117
118   /**
119    * Message queue for the client
120    */
121   struct GNUNET_MQ_Handle *client_mq;
122
123   /**
124    * Type of operation supported for this set
125    */
126   enum GNUNET_SET_OperationType operation;
127
128   /**
129    * Application id of intereset for this listener.
130    */
131   struct GNUNET_HashCode app_id;
132 };
133
134
135 /**
136  * Peer that has connected to us, but is not yet evaluating a set operation.
137  * Once the peer has sent a request, and the client has
138  * accepted or rejected it, this information will be deleted.
139  */
140 struct Incoming
141 {
142   /**
143    * Incoming peers are held in a linked list
144    */
145   struct Incoming *next;
146
147   /**
148    * Incoming peers are held in a linked list
149    */
150   struct Incoming *prev;
151
152   /**
153    * Tunnel context, stores information about
154    * the tunnel and its peer.
155    */
156   struct TunnelContext *tc;
157
158   /**
159    * GNUNET_YES if the incoming peer has sent
160    * an operation request (and we are waiting
161    * for the client to ack/nack), GNUNET_NO otherwise.
162    */
163   int received_request;
164
165   /**
166    * App code, set once the peer has
167    * requested an operation
168    */
169   struct GNUNET_HashCode app_id;
170
171   /**
172    * Context message, set once the peer
173    * has requested an operation.
174    */
175   struct GNUNET_MessageHeader *context_msg;
176
177   /**
178    * Salt the peer has requested to use for the
179    * operation
180    */
181   uint16_t salt;
182
183   /**
184    * Operation the other peer wants to do
185    */
186   enum GNUNET_SET_OperationType operation;
187
188   /**
189    * Has the incoming request been suggested to
190    * a client listener yet?
191    */
192   int suggested;
193
194   /**
195    * Unique request id for the request from
196    * a remote peer, sent to the client, which will
197    * accept or reject the request.
198    */
199   uint32_t accept_id;
200
201   /**
202    * Timeout task, if the incoming peer has not been accepted
203    * after the timeout, it will be disconnected.
204    */
205   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
206 };
207
208
209 enum TunnelContextType {
210   CONTEXT_INCOMING,
211   CONTEXT_OPERATION_UNION,
212   CONTEXT_OPERATION_INTERSECTION,
213 };
214
215 /**
216  * Information about a tunnel we are connected to.
217  * Used as tunnel context with mesh.
218  */
219 struct TunnelContext
220 {
221   /**
222    * The mesh tunnel that has this context
223    */
224   struct GNUNET_MESH_Tunnel *tunnel;
225
226   /**
227    * The peer on the other side.
228    */
229   struct GNUNET_PeerIdentity peer;
230
231   /**
232    * Handle to the message queue for the tunnel.
233    */
234   struct GNUNET_MQ_Handle *mq;
235
236   /**
237    * Type of the tunnel.
238    */
239   enum TunnelContextType type;
240
241   /**
242    * State associated with the tunnel, dependent on
243    * tunnel type.
244    */
245   void *data;
246 };
247
248
249
250 /**
251  * Configuration of the local peer
252  */
253 extern const struct GNUNET_CONFIGURATION_Handle *configuration;
254
255 extern struct GNUNET_MESH_Handle *mesh;
256
257
258 /**
259  * Create a new set supporting the union operation
260  *
261  * @return the newly created set
262  */
263 struct Set *
264 _GSS_union_set_create (void);
265
266
267 /**
268  * Evaluate a union operation with
269  * a remote peer.
270  *
271  * @param m the evaluate request message from the client
272  * @param set the set to evaluate the operation with
273  */
274 void
275 _GSS_union_evaluate (struct GNUNET_SET_EvaluateMessage *m, struct Set *set);
276
277
278 /**
279  * Add the element from the given element message to the set.
280  *
281  * @param m message with the element
282  * @param set set to add the element to
283  */
284 void
285 _GSS_union_add (struct GNUNET_SET_ElementMessage *m, struct Set *set);
286
287
288 /**
289  * Remove the element given in the element message from the set.
290  * Only marks the element as removed, so that older set operations can still exchange it.
291  *
292  * @param m message with the element
293  * @param set set to remove the element from
294  */
295 void
296 _GSS_union_remove (struct GNUNET_SET_ElementMessage *m, struct Set *set);
297
298
299 /**
300  * Destroy a set that supports the union operation
301  *
302  * @param set the set to destroy, must be of type GNUNET_SET_OPERATION_UNION
303  */
304 void
305 _GSS_union_set_destroy (struct Set *set);
306
307
308 /**
309  * Accept an union operation request from a remote peer
310  *
311  * @param m the accept message from the client
312  * @param set the set of the client
313  * @param incoming information about the requesting remote peer
314  */
315 void
316 _GSS_union_accept (struct GNUNET_SET_AcceptRejectMessage *m, struct Set *set,
317                    struct Incoming *incoming);
318
319
320 /**
321  * Destroy a union operation, and free all resources
322  * associated with it.
323  *
324  * @param eo the union operation to destroy
325  */
326 void
327 _GSS_union_operation_destroy (struct UnionEvaluateOperation *eo);
328
329
330 /**
331  * Dispatch messages for a union operation.
332  *
333  * @param cls closure
334  * @param tunnel mesh tunnel
335  * @param tunnel_ctx tunnel context
336  * @param mh message to process
337  * @return ???
338  */
339 int
340 _GSS_union_handle_p2p_message (void *cls,
341                                struct GNUNET_MESH_Tunnel *tunnel,
342                                void **tunnel_ctx,
343                                const struct GNUNET_MessageHeader *mh);
344
345
346 #endif