-remove async ecc key generation, not needed
[oweals/gnunet.git] / src / util / server_nc.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 util/server_nc.c
23  * @brief convenience functions for transmission of
24  *        a notification stream
25  * @author Christian Grothoff
26  */
27
28 #include "platform.h"
29 #include "gnunet_common.h"
30 #include "gnunet_connection_lib.h"
31 #include "gnunet_container_lib.h"
32 #include "gnunet_scheduler_lib.h"
33 #include "gnunet_server_lib.h"
34 #include "gnunet_time_lib.h"
35
36 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
37
38
39 /**
40  * Entry in list of messages pending to be transmitted.
41  */
42 struct PendingMessageList
43 {
44
45   /**
46    * This is a doubly-linked list.
47    */
48   struct PendingMessageList *next;
49
50   /**
51    * This is a doubly-linked list.
52    */
53   struct PendingMessageList *prev;
54
55   /**
56    * Message to transmit (allocated at the end of this
57    * struct, do not free)
58    */
59   const struct GNUNET_MessageHeader *msg;
60
61   /**
62    * Can this message be dropped?
63    */
64   int can_drop;
65
66 };
67
68
69 /**
70  * Lists of clients we manage for notifications.
71  */
72 struct ClientList
73 {
74
75   /**
76    * This is a doubly linked list.
77    */
78   struct ClientList *next;
79
80   /**
81    * This is a doubly linked list.
82    */
83   struct ClientList *prev;
84
85   /**
86    * Overall context this client belongs to.
87    */
88   struct GNUNET_SERVER_NotificationContext *nc;
89
90   /**
91    * Handle to the client.
92    */
93   struct GNUNET_SERVER_Client *client;
94
95   /**
96    * Handle for pending transmission request to the client (or NULL).
97    */
98   struct GNUNET_SERVER_TransmitHandle *th;
99
100   /**
101    * Head of linked list of requests queued for transmission.
102    */
103   struct PendingMessageList *pending_head;
104
105   /**
106    * Tail of linked list of requests queued for transmission.
107    */
108   struct PendingMessageList *pending_tail;
109
110   /**
111    * Number of messages currently in the list.
112    */
113   unsigned int num_pending;
114
115 };
116
117
118 /**
119  * The notification context is the key datastructure for a convenience
120  * API used for transmission of notifications to the client until the
121  * client disconnects (or the notification context is destroyed, in
122  * which case we disconnect these clients).  Essentially, all
123  * (notification) messages are queued up until the client is able to
124  * read them.
125  */
126 struct GNUNET_SERVER_NotificationContext
127 {
128
129   /**
130    * Server we do notifications for.
131    */
132   struct GNUNET_SERVER_Handle *server;
133
134   /**
135    * Head of list of clients receiving notifications.
136    */
137   struct ClientList *clients_head;
138
139   /**
140    * Tail of list of clients receiving notifications.
141    */
142   struct ClientList *clients_tail;
143
144   /**
145    * Maximum number of optional messages to queue per client.
146    */
147   unsigned int queue_length;
148
149 };
150
151
152 /**
153  * Client has disconnected, clean up.
154  *
155  * @param cls our 'struct GNUNET_SERVER_NotificationContext *'
156  * @param client handle of client that disconnected
157  */
158 static void
159 handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client)
160 {
161   struct GNUNET_SERVER_NotificationContext *nc = cls;
162   struct ClientList *pos;
163   struct PendingMessageList *pml;
164
165   if (NULL == client)
166   {
167     nc->server = NULL;
168     return;
169   }
170   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
171     if (pos->client == client)
172       break;
173   if (NULL == pos)
174     return;
175   LOG (GNUNET_ERROR_TYPE_DEBUG,
176        "Client disconnected, cleaning up %u messages in NC queue\n",
177        pos->num_pending);
178   GNUNET_CONTAINER_DLL_remove (nc->clients_head,
179                                nc->clients_tail,
180                                pos);
181   while (NULL != (pml = pos->pending_head))
182   {
183     GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
184     GNUNET_free (pml);
185     pos->num_pending--;
186   }
187   if (NULL != pos->th)
188   {
189     GNUNET_SERVER_notify_transmit_ready_cancel (pos->th);
190     pos->th = NULL;
191   }
192   GNUNET_SERVER_client_drop (client);
193   GNUNET_assert (0 == pos->num_pending);
194   GNUNET_free (pos);
195 }
196
197
198 /**
199  * Create a new notification context.
200  *
201  * @param server server for which this function creates the context
202  * @param queue_length maximum number of messages to keep in
203  *        the notification queue; optional messages are dropped
204  *        if the queue gets longer than this number of messages
205  * @return handle to the notification context
206  */
207 struct GNUNET_SERVER_NotificationContext *
208 GNUNET_SERVER_notification_context_create (struct GNUNET_SERVER_Handle *server,
209                                            unsigned int queue_length)
210 {
211   struct GNUNET_SERVER_NotificationContext *ret;
212
213   ret = GNUNET_malloc (sizeof (struct GNUNET_SERVER_NotificationContext));
214   ret->server = server;
215   ret->queue_length = queue_length;
216   GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, ret);
217   return ret;
218 }
219
220
221 /**
222  * Destroy the context, force disconnect for all clients.
223  *
224  * @param nc context to destroy.
225  */
226 void
227 GNUNET_SERVER_notification_context_destroy (struct
228                                             GNUNET_SERVER_NotificationContext
229                                             *nc)
230 {
231   struct ClientList *pos;
232   struct PendingMessageList *pml;
233
234   while (NULL != (pos = nc->clients_head))
235   {
236     GNUNET_CONTAINER_DLL_remove (nc->clients_head,
237                                  nc->clients_tail,
238                                  pos);
239     if (NULL != pos->th)
240     {
241       GNUNET_SERVER_notify_transmit_ready_cancel(pos->th);
242       pos->th = NULL;
243     }
244     GNUNET_SERVER_client_drop (pos->client);
245     while (NULL != (pml = pos->pending_head))
246     {
247       GNUNET_CONTAINER_DLL_remove (pos->pending_head, pos->pending_tail, pml);
248       GNUNET_free (pml);
249       pos->num_pending--;
250     }
251     GNUNET_assert (0 == pos->num_pending);
252     GNUNET_free (pos);
253   }
254   if (NULL != nc->server)
255     GNUNET_SERVER_disconnect_notify_cancel (nc->server,
256                                             &handle_client_disconnect, nc);
257   GNUNET_free (nc);
258 }
259
260
261 /**
262  * Add a client to the notification context.
263  *
264  * @param nc context to modify
265  * @param client client to add
266  */
267 void
268 GNUNET_SERVER_notification_context_add (struct GNUNET_SERVER_NotificationContext
269                                         *nc,
270                                         struct GNUNET_SERVER_Client *client)
271 {
272   struct ClientList *cl;
273
274   for (cl = nc->clients_head; NULL != cl; cl = cl->next)
275     if (cl->client == client)
276       return; /* already present */    
277   cl = GNUNET_malloc (sizeof (struct ClientList));
278   GNUNET_CONTAINER_DLL_insert (nc->clients_head,
279                                nc->clients_tail,
280                                cl);
281   cl->nc = nc;
282   cl->client = client;
283   GNUNET_SERVER_client_keep (client);
284 }
285
286
287 /**
288  * Function called to notify a client about the socket begin ready to
289  * queue more data.  "buf" will be NULL and "size" zero if the socket
290  * was closed for writing in the meantime.
291  *
292  * @param cls the 'struct ClientList *'
293  * @param size number of bytes available in buf
294  * @param buf where the callee should write the message
295  * @return number of bytes written to buf
296  */
297 static size_t
298 transmit_message (void *cls, size_t size, void *buf)
299 {
300   struct ClientList *cl = cls;
301   char *cbuf = buf;
302   struct PendingMessageList *pml;
303   uint16_t msize;
304   size_t ret;
305
306   cl->th = NULL;
307   if (NULL == buf)
308   {
309     /* 'cl' should be freed via disconnect notification shortly */
310     LOG (GNUNET_ERROR_TYPE_DEBUG,
311          "Failed to transmit message from NC queue to client\n");
312     return 0;
313   }
314   ret = 0;
315   while (NULL != (pml = cl->pending_head))
316   {
317     msize = ntohs (pml->msg->size);
318     if (size < msize)
319       break;
320     GNUNET_CONTAINER_DLL_remove (cl->pending_head, cl->pending_tail, pml);
321     LOG (GNUNET_ERROR_TYPE_DEBUG,
322          "Copying message of type %u and size %u from pending queue to transmission buffer\n",
323          ntohs (pml->msg->type), msize);
324     memcpy (&cbuf[ret], pml->msg, msize);
325     ret += msize;
326     size -= msize;
327     GNUNET_free (pml);
328     cl->num_pending--;
329   }
330   if (NULL != pml)
331   {
332     LOG (GNUNET_ERROR_TYPE_DEBUG,
333          "Have %u messages left in NC queue, will try transmission again\n",
334          cl->num_pending);
335     cl->th =
336         GNUNET_SERVER_notify_transmit_ready (cl->client, ntohs (pml->msg->size),
337                                              GNUNET_TIME_UNIT_FOREVER_REL,
338                                              &transmit_message, cl);
339   }
340   else
341   {
342     GNUNET_assert (0 == cl->num_pending);
343   }
344   return ret;
345 }
346
347
348 /**
349  * Send a message to a particular client.
350  *
351  * @param nc context to modify
352  * @param client client to transmit to
353  * @param msg message to send
354  * @param can_drop can this message be dropped due to queue length limitations
355  */
356 static void
357 do_unicast (struct GNUNET_SERVER_NotificationContext *nc,
358             struct ClientList *client, const struct GNUNET_MessageHeader *msg,
359             int can_drop)
360 {
361   struct PendingMessageList *pml;
362   uint16_t size;
363
364   if ((client->num_pending > nc->queue_length) && (GNUNET_YES == can_drop))
365   {
366     LOG (GNUNET_ERROR_TYPE_INFO,
367          "Dropping message of type %u and size %u due to full queue (%u entries)\n",
368          ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
369     return;                     /* drop! */
370   }
371   if (client->num_pending > nc->queue_length)
372   {
373     /* FIXME: consider checking for other messages in the
374      * queue that are 'droppable' */
375   }
376   client->num_pending++;
377   size = ntohs (msg->size);
378   pml = GNUNET_malloc (sizeof (struct PendingMessageList) + size);
379   pml->msg = (const struct GNUNET_MessageHeader *) &pml[1];
380   pml->can_drop = can_drop;
381   LOG (GNUNET_ERROR_TYPE_DEBUG,
382        "Adding message of type %u and size %u to pending queue (which has %u entries)\n",
383        ntohs (msg->type), ntohs (msg->size), (unsigned int) nc->queue_length);
384   memcpy (&pml[1], msg, size);
385   /* append */
386   GNUNET_CONTAINER_DLL_insert_tail (client->pending_head, client->pending_tail,
387                                     pml);
388   if (client->th == NULL)
389     client->th =
390         GNUNET_SERVER_notify_transmit_ready (client->client,
391                                              ntohs (client->pending_head->
392                                                     msg->size),
393                                              GNUNET_TIME_UNIT_FOREVER_REL,
394                                              &transmit_message, client);
395 }
396
397
398 /**
399  * Send a message to a particular client; must have
400  * already been added to the notification context.
401  *
402  * @param nc context to modify
403  * @param client client to transmit to
404  * @param msg message to send
405  * @param can_drop can this message be dropped due to queue length limitations
406  */
407 void
408 GNUNET_SERVER_notification_context_unicast (struct
409                                             GNUNET_SERVER_NotificationContext
410                                             *nc,
411                                             struct GNUNET_SERVER_Client *client,
412                                             const struct GNUNET_MessageHeader
413                                             *msg, int can_drop)
414 {
415   struct ClientList *pos;
416
417   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
418     if (pos->client == client)
419       break;
420   GNUNET_assert (NULL != pos);
421   do_unicast (nc, pos, msg, can_drop);
422 }
423
424
425 /**
426  * Send a message to all clients of this context.
427  *
428  * @param nc context to modify
429  * @param msg message to send
430  * @param can_drop can this message be dropped due to queue length limitations
431  */
432 void
433 GNUNET_SERVER_notification_context_broadcast (struct
434                                               GNUNET_SERVER_NotificationContext
435                                               *nc,
436                                               const struct GNUNET_MessageHeader
437                                               *msg, int can_drop)
438 {
439   struct ClientList *pos;
440
441   for (pos = nc->clients_head; NULL != pos; pos = pos->next)
442     do_unicast (nc, pos, msg, can_drop);
443 }
444
445
446 /* end of server_nc.c */