- test for external iterator
[oweals/gnunet.git] / src / util / server.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009-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 3, 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.c
23  * @brief library for building GNUnet network servers
24  * @author Christian Grothoff
25  */
26
27 #include "platform.h"
28 #include "gnunet_common.h"
29 #include "gnunet_util_lib.h"
30 #include "gnunet_protocols.h"
31
32 #define LOG(kind,...) GNUNET_log_from (kind, "util", __VA_ARGS__)
33
34 #define LOG_STRERROR(kind,syscall) GNUNET_log_from_strerror (kind, "util", syscall)
35
36 #define LOG_STRERROR_FILE(kind,syscall,filename) GNUNET_log_from_strerror_file (kind, "util", syscall, filename)
37
38
39 /**
40  * List of arrays of message handlers.
41  */
42 struct HandlerList
43 {
44   /**
45    * This is a linked list.
46    */
47   struct HandlerList *next;
48
49   /**
50    * NULL-terminated array of handlers.
51    */
52   const struct GNUNET_SERVER_MessageHandler *handlers;
53 };
54
55
56 /**
57  * List of arrays of message handlers.
58  */
59 struct NotifyList
60 {
61   /**
62    * This is a doubly linked list.
63    */
64   struct NotifyList *next;
65
66   /**
67    * This is a doubly linked list.
68    */
69   struct NotifyList *prev;
70
71   /**
72    * Function to call.
73    */
74   GNUNET_SERVER_DisconnectCallback callback;
75
76   /**
77    * Closure for callback.
78    */
79   void *callback_cls;
80 };
81
82
83 /**
84  * @brief handle for a server
85  */
86 struct GNUNET_SERVER_Handle
87 {
88   /**
89    * List of handlers for incoming messages.
90    */
91   struct HandlerList *handlers;
92
93   /**
94    * Head of list of our current clients.
95    */
96   struct GNUNET_SERVER_Client *clients_head;
97
98   /**
99    * Head of list of our current clients.
100    */
101   struct GNUNET_SERVER_Client *clients_tail;
102
103   /**
104    * Head of linked list of functions to call on disconnects by clients.
105    */
106   struct NotifyList *disconnect_notify_list_head;
107
108   /**
109    * Tail of linked list of functions to call on disconnects by clients.
110    */
111   struct NotifyList *disconnect_notify_list_tail;
112
113   /**
114    * Head of linked list of functions to call on connects by clients.
115    */
116   struct NotifyList *connect_notify_list_head;
117
118   /**
119    * Tail of linked list of functions to call on connects by clients.
120    */
121   struct NotifyList *connect_notify_list_tail;
122
123   /**
124    * Function to call for access control.
125    */
126   GNUNET_CONNECTION_AccessCheck access;
127
128   /**
129    * Closure for access.
130    */
131   void *access_cls;
132
133   /**
134    * NULL-terminated array of sockets used to listen for new
135    * connections.
136    */
137   struct GNUNET_NETWORK_Handle **listen_sockets;
138
139   /**
140    * After how long should an idle connection time
141    * out (on write).
142    */
143   struct GNUNET_TIME_Relative idle_timeout;
144
145   /**
146    * Task scheduled to do the listening.
147    */
148   GNUNET_SCHEDULER_TaskIdentifier listen_task;
149
150   /**
151    * Alternative function to create a MST instance.
152    */
153   GNUNET_SERVER_MstCreateCallback mst_create;
154
155   /**
156    * Alternative function to destroy a MST instance.
157    */
158   GNUNET_SERVER_MstDestroyCallback mst_destroy;
159
160   /**
161    * Alternative function to give data to a MST instance.
162    */
163   GNUNET_SERVER_MstReceiveCallback mst_receive;
164
165   /**
166    * Closure for 'mst_'-callbacks.
167    */
168   void *mst_cls;
169
170   /**
171    * Do we ignore messages of types that we do not understand or do we
172    * require that a handler is found (and if not kill the connection)?
173    */
174   int require_found;
175
176   /**
177    * Set to GNUNET_YES once we are in 'soft' shutdown where we wait for
178    * all non-monitor clients to disconnect before we call
179    * GNUNET_SERVER_destroy.  See 'test_monitor_clients'.  Set to
180    * GNUNET_SYSERR once the final destroy task has been scheduled
181    * (we cannot run it in the same task).
182    */
183   int in_soft_shutdown;
184 };
185
186
187 /**
188  * Handle server returns for aborting transmission to a client.
189  */
190 struct GNUNET_SERVER_TransmitHandle
191 {
192   /**
193    * Function to call to get the message.
194    */
195   GNUNET_CONNECTION_TransmitReadyNotify callback;
196
197   /**
198    * Closure for 'callback'
199    */
200   void *callback_cls;
201
202   /**
203    * Active connection transmission handle.
204    */
205   struct GNUNET_CONNECTION_TransmitHandle *cth;
206
207 };
208
209
210 /**
211  * @brief handle for a client of the server
212  */
213 struct GNUNET_SERVER_Client
214 {
215
216   /**
217    * This is a doubly linked list.
218    */
219   struct GNUNET_SERVER_Client *next;
220
221   /**
222    * This is a doubly linked list.
223    */
224   struct GNUNET_SERVER_Client *prev;
225
226   /**
227    * Processing of incoming data.
228    */
229   void *mst;
230
231   /**
232    * Server that this client belongs to.
233    */
234   struct GNUNET_SERVER_Handle *server;
235
236   /**
237    * Client closure for callbacks.
238    */
239   struct GNUNET_CONNECTION_Handle *connection;
240
241   /**
242    * User context value, manipulated using
243    * 'GNUNET_SERVER_client_{get/set}_user_context' functions.
244    */
245   void *user_context;
246
247   /**
248    * ID of task used to restart processing.
249    */
250   GNUNET_SCHEDULER_TaskIdentifier restart_task;
251
252   /**
253    * Task that warns about missing calls to 'GNUNET_SERVER_receive_done'.
254    */
255   GNUNET_SCHEDULER_TaskIdentifier warn_task;
256
257   /**
258    * Time when the warn task was started.
259    */
260   struct GNUNET_TIME_Absolute warn_start;
261
262   /**
263    * Last activity on this socket (used to time it out
264    * if reference_count == 0).
265    */
266   struct GNUNET_TIME_Absolute last_activity;
267
268   /**
269    * Transmission handle we return for this client from
270    * GNUNET_SERVER_notify_transmit_ready.
271    */
272   struct GNUNET_SERVER_TransmitHandle th;
273
274   /**
275    * After how long should an idle connection time
276    * out (on write).
277    */
278   struct GNUNET_TIME_Relative idle_timeout;
279
280   /**
281    * Number of external entities with a reference to
282    * this client object.
283    */
284   unsigned int reference_count;
285
286   /**
287    * Was processing if incoming messages suspended while
288    * we were still processing data already received?
289    * This is a counter saying how often processing was
290    * suspended (once per handler invoked).
291    */
292   unsigned int suspended;
293
294   /**
295    * Last size given when user context was initialized; used for
296    * sanity check.
297    */
298   size_t user_context_size;
299
300   /**
301    * Are we currently in the "process_client_buffer" function (and
302    * will hence restart the receive job on exit if suspended == 0 once
303    * we are done?).  If this is set, then "receive_done" will
304    * essentially only decrement suspended; if this is not set, then
305    * "receive_done" may need to restart the receive process (either
306    * from the side-buffer or via select/recv).
307    */
308   int in_process_client_buffer;
309
310   /**
311    * We're about to close down this client.
312    */
313   int shutdown_now;
314
315   /**
316    * Are we currently trying to receive? (YES if we are, NO if we are not,
317    * SYSERR if data is already available in MST).
318    */
319   int receive_pending;
320
321   /**
322    * Persist the file handle for this client no matter what happens,
323    * force the OS to close once the process actually dies.  Should only
324    * be used in special cases!
325    */
326   int persist;
327
328   /**
329    * Is this client a 'monitor' client that should not be counted
330    * when deciding on destroying the server during soft shutdown?
331    * (see also GNUNET_SERVICE_start)
332    */
333   int is_monitor;
334
335   /**
336    * Type of last message processed (for warn_no_receive_done).
337    */
338   uint16_t warn_type;
339 };
340
341
342
343 /**
344  * Return user context associated with the given client.
345  * Note: you should probably use the macro (call without the underscore).
346  *
347  * @param client client to query
348  * @param size number of bytes in user context struct (for verification only)
349  * @return pointer to user context
350  */
351 void *
352 GNUNET_SERVER_client_get_user_context_ (struct GNUNET_SERVER_Client *client,
353                                         size_t size)
354 {
355   if ((0 == client->user_context_size) &&
356       (NULL == client->user_context))
357     return NULL; /* never set */
358   GNUNET_assert (size == client->user_context_size);
359   return client->user_context;
360 }
361
362
363 /**
364  * Set user context to be associated with the given client.
365  * Note: you should probably use the macro (call without the underscore).
366  *
367  * @param client client to query
368  * @param ptr pointer to user context
369  * @param size number of bytes in user context struct (for verification only)
370  */
371 void 
372 GNUNET_SERVER_client_set_user_context_ (struct GNUNET_SERVER_Client *client,
373                                         void *ptr,
374                                         size_t size)
375 {
376   client->user_context_size = size;
377   client->user_context = ptr;
378 }
379
380
381 /**
382  * Scheduler says our listen socket is ready.  Process it!
383  *
384  * @param cls handle to our server for which we are processing the listen
385  *        socket
386  * @param tc reason why we are running right now
387  */
388 static void
389 process_listen_socket (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
390 {
391   struct GNUNET_SERVER_Handle *server = cls;
392   struct GNUNET_CONNECTION_Handle *sock;
393   struct GNUNET_SERVER_Client *client;
394   unsigned int i;
395
396   server->listen_task = GNUNET_SCHEDULER_NO_TASK;
397   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
398   {
399     /* ignore shutdown, someone else will take care of it! */
400     GNUNET_SERVER_resume (server);
401     return;
402   }
403   i = 0;
404   while (NULL != server->listen_sockets[i])
405   {
406     if (GNUNET_NETWORK_fdset_isset (tc->read_ready, server->listen_sockets[i]))
407     {
408       sock =
409           GNUNET_CONNECTION_create_from_accept (server->access,
410                                                 server->access_cls,
411                                                 server->listen_sockets[i]);
412       if (NULL != sock)
413       {
414         LOG (GNUNET_ERROR_TYPE_DEBUG, "Server accepted incoming connection.\n");
415         client = GNUNET_SERVER_connect_socket (server, sock);
416         /* decrement reference count, we don't keep "client" alive */
417         GNUNET_SERVER_client_drop (client);
418       }
419     }
420     i++;
421   }
422   /* listen for more! */
423   GNUNET_SERVER_resume (server);
424 }
425
426
427 /**
428  * Create and initialize a listen socket for the server.
429  *
430  * @param serverAddr address to listen on
431  * @param socklen length of address
432  * @return NULL on error, otherwise the listen socket
433  */
434 static struct GNUNET_NETWORK_Handle *
435 open_listen_socket (const struct sockaddr *serverAddr, socklen_t socklen)
436 {
437   struct GNUNET_NETWORK_Handle *sock;
438   uint16_t port;
439   int eno;
440
441   switch (serverAddr->sa_family)
442   {
443   case AF_INET:
444     port = ntohs (((const struct sockaddr_in *) serverAddr)->sin_port);
445     break;
446   case AF_INET6:
447     port = ntohs (((const struct sockaddr_in6 *) serverAddr)->sin6_port);
448     break;
449   case AF_UNIX:
450     port = 0;
451     break;
452   default:
453     GNUNET_break (0);
454     port = 0;
455     break;
456   }
457   sock = GNUNET_NETWORK_socket_create (serverAddr->sa_family, SOCK_STREAM, 0);
458   if (NULL == sock)
459   {
460     LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "socket");
461     errno = 0;
462     return NULL;
463   }
464   /* bind the socket */
465   if (GNUNET_OK != GNUNET_NETWORK_socket_bind (sock, serverAddr, socklen, 0))
466   {
467     eno = errno;
468     if (EADDRINUSE != errno)
469     {
470       /* we don't log 'EADDRINUSE' here since an IPv4 bind may
471        * fail if we already took the port on IPv6; if both IPv4 and
472        * IPv6 binds fail, then our caller will log using the
473        * errno preserved in 'eno' */
474       LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
475       if (0 != port)
476         LOG (GNUNET_ERROR_TYPE_ERROR, _("`%s' failed for port %d (%s).\n"),
477              "bind", port,
478              (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
479       eno = 0;
480     }
481     else
482     {
483       if (0 != port)
484         LOG (GNUNET_ERROR_TYPE_WARNING,
485              _("`%s' failed for port %d (%s): address already in use\n"),
486              "bind", port,
487              (AF_INET == serverAddr->sa_family) ? "IPv4" : "IPv6");
488       else if (AF_UNIX == serverAddr->sa_family)
489       {
490         const struct sockaddr_un *un = (const struct sockaddr_un *) serverAddr;
491         unsigned int off = 0;
492
493         if ('\0' == un->sun_path[0])
494           off = 1; /* some UNIXPATHs start with 0 */
495         LOG (GNUNET_ERROR_TYPE_WARNING,
496              _("`%s' failed for `%.*s': address already in use\n"), "bind",
497              (int) ((sizeof (un->sun_path) - off)),
498              (&un->sun_path[off]));
499       }
500     }
501     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
502     errno = eno;
503     return NULL;
504   }
505   if (GNUNET_OK != GNUNET_NETWORK_socket_listen (sock, 5))
506   {
507     LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "listen");
508     GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_close (sock));
509     errno = 0;
510     return NULL;
511   }
512   if (0 != port)
513     LOG (GNUNET_ERROR_TYPE_DEBUG, "Server starts to listen on port %u.\n",
514          port);
515   return sock;
516 }
517
518
519 /**
520  * Create a new server.
521  *
522  * @param access function for access control
523  * @param access_cls closure for access
524  * @param lsocks NULL-terminated array of listen sockets
525  * @param idle_timeout after how long should we timeout idle connections?
526  * @param require_found if YES, connections sending messages of unknown type
527  *        will be closed
528  * @return handle for the new server, NULL on error
529  *         (typically, "port" already in use)
530  */
531 struct GNUNET_SERVER_Handle *
532 GNUNET_SERVER_create_with_sockets (GNUNET_CONNECTION_AccessCheck access,
533                                    void *access_cls,
534                                    struct GNUNET_NETWORK_Handle **lsocks,
535                                    struct GNUNET_TIME_Relative idle_timeout,
536                                    int require_found)
537 {
538   struct GNUNET_SERVER_Handle *server;
539
540   server = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Handle));
541   server->idle_timeout = idle_timeout;
542   server->listen_sockets = lsocks;
543   server->access = access;
544   server->access_cls = access_cls;
545   server->require_found = require_found;
546   if (NULL != lsocks)
547     GNUNET_SERVER_resume (server);
548   return server;
549 }
550
551
552 /**
553  * Create a new server.
554  *
555  * @param access function for access control
556  * @param access_cls closure for access
557  * @param serverAddr address to listen on (including port), NULL terminated array
558  * @param socklen length of serverAddr
559  * @param idle_timeout after how long should we timeout idle connections?
560  * @param require_found if YES, connections sending messages of unknown type
561  *        will be closed
562  * @return handle for the new server, NULL on error
563  *         (typically, "port" already in use)
564  */
565 struct GNUNET_SERVER_Handle *
566 GNUNET_SERVER_create (GNUNET_CONNECTION_AccessCheck access, void *access_cls,
567                       struct sockaddr *const *serverAddr,
568                       const socklen_t * socklen,
569                       struct GNUNET_TIME_Relative idle_timeout,
570                       int require_found)
571 {
572   struct GNUNET_NETWORK_Handle **lsocks;
573   unsigned int i;
574   unsigned int j;
575   unsigned int k;
576   int seen;
577
578   i = 0;
579   while (NULL != serverAddr[i])
580     i++;
581   if (i > 0)
582   {
583     lsocks = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle *) * (i + 1));
584     i = 0;
585     j = 0;
586     while (NULL != serverAddr[i])
587     {
588       seen = 0;
589       for (k=0;k<i;k++)
590         if ( (socklen[k] == socklen[i]) &&
591              (0 == memcmp (serverAddr[k], serverAddr[i], socklen[i])) )
592         {
593           seen = 1;
594           break;
595         }
596       if (0 != seen)
597       {
598         /* duplicate address, skip */
599         i++;
600         continue;
601       }
602       lsocks[j] = open_listen_socket (serverAddr[i], socklen[i]);
603       if (NULL != lsocks[j])
604         j++;
605       i++;
606     }
607     if (0 == j)
608     {
609       if (0 != errno)
610         LOG_STRERROR (GNUNET_ERROR_TYPE_ERROR, "bind");
611       GNUNET_free (lsocks);
612       lsocks = NULL;
613     }
614   }
615   else
616   {
617     lsocks = NULL;
618   }
619   return GNUNET_SERVER_create_with_sockets (access, access_cls, lsocks,
620                                             idle_timeout, require_found);
621 }
622
623
624 /**
625  * Set the 'monitor' flag on this client.  Clients which have been
626  * marked as 'monitors' won't prevent the server from shutting down
627  * once 'GNUNET_SERVER_stop_listening' has been invoked.  The idea is
628  * that for "normal" clients we likely want to allow them to process
629  * their requests; however, monitor-clients are likely to 'never'
630  * disconnect during shutdown and thus will not be considered when
631  * determining if the server should continue to exist after
632  * 'GNUNET_SERVER_destroy' has been called.
633  *
634  * @param client the client to set the 'monitor' flag on
635  */
636 void
637 GNUNET_SERVER_client_mark_monitor (struct GNUNET_SERVER_Client *client)
638 {
639   client->is_monitor = GNUNET_YES;
640 }
641
642
643 /**
644  * Helper function for 'test_monitor_clients' to trigger
645  * 'GNUNET_SERVER_destroy' after the stack has unwound.
646  *
647  * @param cls the 'struct GNUNET_SERVER_Handle' to destroy
648  * @param tc unused
649  */
650 static void
651 do_destroy (void *cls,
652             const struct GNUNET_SCHEDULER_TaskContext *tc)
653 {
654   struct GNUNET_SERVER_Handle *server = cls;
655   GNUNET_SERVER_destroy (server);
656 }
657
658
659 /**
660  * Check if only 'monitor' clients are left.  If so, destroy the
661  * server completely.
662  *
663  * @param server server to test for full shutdown
664  */
665 static void
666 test_monitor_clients (struct GNUNET_SERVER_Handle *server)
667 {
668   struct GNUNET_SERVER_Client *client;
669
670   if (GNUNET_YES != server->in_soft_shutdown)
671     return;
672   for (client = server->clients_head; NULL != client; client = client->next)
673     if (GNUNET_NO == client->is_monitor)
674       return; /* not done yet */
675   server->in_soft_shutdown = GNUNET_SYSERR;
676   GNUNET_SCHEDULER_add_continuation (&do_destroy, server,
677                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
678 }
679
680
681 /**
682  * Suspend accepting connections from the listen socket temporarily.
683  *
684  * @param server server to stop accepting connections.
685  */
686 void
687 GNUNET_SERVER_suspend (struct GNUNET_SERVER_Handle *server)
688 {
689   if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
690   {
691     GNUNET_SCHEDULER_cancel (server->listen_task);
692     server->listen_task = GNUNET_SCHEDULER_NO_TASK;
693   }
694 }
695
696
697 /**
698  * Resume accepting connections from the listen socket.
699  *
700  * @param server server to stop accepting connections.
701  */
702 void
703 GNUNET_SERVER_resume (struct GNUNET_SERVER_Handle *server)
704 {
705   struct GNUNET_NETWORK_FDSet *r;
706   unsigned int i;
707
708   if (NULL == server->listen_sockets)
709     return;
710   if (NULL == server->listen_sockets[0])
711     return; /* nothing to do, no listen sockets! */
712   if (NULL == server->listen_sockets[1])
713   {
714     /* simplified method: no fd set needed; this is then much simpler and
715        much more efficient */
716     server->listen_task =
717       GNUNET_SCHEDULER_add_read_net_with_priority (GNUNET_TIME_UNIT_FOREVER_REL,
718                                                    GNUNET_SCHEDULER_PRIORITY_HIGH,
719                                                    server->listen_sockets[0],
720                                                    &process_listen_socket, server);
721     return;
722   }
723   r = GNUNET_NETWORK_fdset_create ();
724   i = 0;
725   while (NULL != server->listen_sockets[i])
726     GNUNET_NETWORK_fdset_set (r, server->listen_sockets[i++]);
727   server->listen_task =
728     GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
729                                  GNUNET_TIME_UNIT_FOREVER_REL, r, NULL,
730                                  &process_listen_socket, server);
731   GNUNET_NETWORK_fdset_destroy (r);
732 }
733
734
735 /**
736  * Stop the listen socket and get ready to shutdown the server
737  * once only 'monitor' clients are left.
738  *
739  * @param server server to stop listening on
740  */
741 void
742 GNUNET_SERVER_stop_listening (struct GNUNET_SERVER_Handle *server)
743 {
744   unsigned int i;
745
746   LOG (GNUNET_ERROR_TYPE_DEBUG, "Server in soft shutdown\n");
747   if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
748   {
749     GNUNET_SCHEDULER_cancel (server->listen_task);
750     server->listen_task = GNUNET_SCHEDULER_NO_TASK;
751   }
752   if (NULL != server->listen_sockets)
753   {
754     i = 0;
755     while (NULL != server->listen_sockets[i])
756       GNUNET_break (GNUNET_OK ==
757                     GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
758     GNUNET_free (server->listen_sockets);
759     server->listen_sockets = NULL;
760   }
761   if (GNUNET_NO == server->in_soft_shutdown)
762     server->in_soft_shutdown = GNUNET_YES;
763   test_monitor_clients (server);
764 }
765
766
767 /**
768  * Free resources held by this server.
769  *
770  * @param server server to destroy
771  */
772 void
773 GNUNET_SERVER_destroy (struct GNUNET_SERVER_Handle *server)
774 {
775   struct HandlerList *hpos;
776   struct NotifyList *npos;
777   unsigned int i;
778
779   LOG (GNUNET_ERROR_TYPE_DEBUG, "Server shutting down.\n");
780   if (GNUNET_SCHEDULER_NO_TASK != server->listen_task)
781   {
782     GNUNET_SCHEDULER_cancel (server->listen_task);
783     server->listen_task = GNUNET_SCHEDULER_NO_TASK;
784   }
785   if (NULL != server->listen_sockets)
786   {
787     i = 0;
788     while (NULL != server->listen_sockets[i])
789       GNUNET_break (GNUNET_OK ==
790                     GNUNET_NETWORK_socket_close (server->listen_sockets[i++]));
791     GNUNET_free (server->listen_sockets);
792     server->listen_sockets = NULL;
793   }
794   while (NULL != server->clients_head)
795     GNUNET_SERVER_client_disconnect (server->clients_head);
796   while (NULL != (hpos = server->handlers))
797   {
798     server->handlers = hpos->next;
799     GNUNET_free (hpos);
800   }
801   while (NULL != (npos = server->disconnect_notify_list_head))
802   {
803     npos->callback (npos->callback_cls, NULL);
804     GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
805                                  server->disconnect_notify_list_tail,
806                                  npos);
807     GNUNET_free (npos);
808   }
809   while (NULL != (npos = server->connect_notify_list_head))
810   {
811     npos->callback (npos->callback_cls, NULL);
812     GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
813                                  server->connect_notify_list_tail,
814                                  npos);
815     GNUNET_free (npos);
816   }
817   GNUNET_free (server);
818 }
819
820
821 /**
822  * Add additional handlers to an existing server.
823  *
824  * @param server the server to add handlers to
825  * @param handlers array of message handlers for
826  *        incoming messages; the last entry must
827  *        have "NULL" for the "callback"; multiple
828  *        entries for the same type are allowed,
829  *        they will be called in order of occurence.
830  *        These handlers can be removed later;
831  *        the handlers array must exist until removed
832  *        (or server is destroyed).
833  */
834 void
835 GNUNET_SERVER_add_handlers (struct GNUNET_SERVER_Handle *server,
836                             const struct GNUNET_SERVER_MessageHandler *handlers)
837 {
838   struct HandlerList *p;
839
840   p = GNUNET_malloc (sizeof (struct HandlerList));
841   p->handlers = handlers;
842   p->next = server->handlers;
843   server->handlers = p;
844 }
845
846
847 /**
848  * Change functions used by the server to tokenize the message stream.
849  * (very rarely used).
850  *
851  * @param server server to modify
852  * @param create new tokenizer initialization function
853  * @param destroy new tokenizer destruction function
854  * @param receive new tokenizer receive function
855  * @param cls closure for 'create', 'receive', 'destroy' 
856  */
857 void
858 GNUNET_SERVER_set_callbacks (struct GNUNET_SERVER_Handle *server,
859                              GNUNET_SERVER_MstCreateCallback create,
860                              GNUNET_SERVER_MstDestroyCallback destroy,
861                              GNUNET_SERVER_MstReceiveCallback receive,
862                              void *cls)
863 {
864   server->mst_create = create;
865   server->mst_destroy = destroy;
866   server->mst_receive = receive;
867   server->mst_cls = cls;
868 }
869
870
871 /**
872  * Task run to warn about missing calls to 'GNUNET_SERVER_receive_done'.
873  *
874  * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
875  * @param tc scheduler context (unused)
876  */
877 static void
878 warn_no_receive_done (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
879 {
880   struct GNUNET_SERVER_Client *client = cls;
881
882   GNUNET_break (0 != client->warn_type); /* type should never be 0 here, as we don't use 0 */
883   client->warn_task =
884       GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
885                                     &warn_no_receive_done, client);
886   if (0 == (GNUNET_SCHEDULER_REASON_SHUTDOWN & tc->reason))
887     LOG (GNUNET_ERROR_TYPE_WARNING,
888          _("Processing code for message of type %u did not call `GNUNET_SERVER_receive_done' after %s\n"),
889          (unsigned int) client->warn_type,
890          GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_duration (client->warn_start), GNUNET_YES));
891 }
892
893
894 /**
895  * Disable the warning the server issues if a message is not acknowledged
896  * in a timely fashion.  Use this call if a client is intentionally delayed
897  * for a while.  Only applies to the current message.
898  *
899  * @param client client for which to disable the warning
900  */
901 void
902 GNUNET_SERVER_disable_receive_done_warning (struct GNUNET_SERVER_Client *client)
903 {
904   if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
905   {
906     GNUNET_SCHEDULER_cancel (client->warn_task);
907     client->warn_task = GNUNET_SCHEDULER_NO_TASK;
908   }
909 }
910
911
912 /**
913  * Inject a message into the server, pretend it came
914  * from the specified client.  Delivery of the message
915  * will happen instantly (if a handler is installed;
916  * otherwise the call does nothing).
917  *
918  * @param server the server receiving the message
919  * @param sender the "pretended" sender of the message
920  *        can be NULL!
921  * @param message message to transmit
922  * @return GNUNET_OK if the message was OK and the
923  *                   connection can stay open
924  *         GNUNET_SYSERR if the connection to the
925  *         client should be shut down
926  */
927 int
928 GNUNET_SERVER_inject (struct GNUNET_SERVER_Handle *server,
929                       struct GNUNET_SERVER_Client *sender,
930                       const struct GNUNET_MessageHeader *message)
931 {
932   struct HandlerList *pos;
933   const struct GNUNET_SERVER_MessageHandler *mh;
934   unsigned int i;
935   uint16_t type;
936   uint16_t size;
937   int found;
938
939   type = ntohs (message->type);
940   size = ntohs (message->size);
941   LOG (GNUNET_ERROR_TYPE_DEBUG,
942        "Server schedules transmission of %u-byte message of type %u to client.\n",
943        size, type);
944   found = GNUNET_NO;
945   for (pos = server->handlers; NULL != pos; pos = pos->next)
946   {
947     i = 0;
948     while (pos->handlers[i].callback != NULL)
949     {
950       mh = &pos->handlers[i];
951       if ((mh->type == type) || (mh->type == GNUNET_MESSAGE_TYPE_ALL))
952       {
953         if ((0 != mh->expected_size) && (mh->expected_size != size))
954         {
955 #if GNUNET8_NETWORK_IS_DEAD
956           LOG (GNUNET_ERROR_TYPE_WARNING,
957                "Expected %u bytes for message of type %u, got %u\n",
958                mh->expected_size, mh->type, size);
959           GNUNET_break_op (0);
960 #else
961           LOG (GNUNET_ERROR_TYPE_DEBUG,
962                "Expected %u bytes for message of type %u, got %u\n",
963                mh->expected_size, mh->type, size);
964 #endif
965           return GNUNET_SYSERR;
966         }
967         if (NULL != sender)
968         {
969           if ( (0 == sender->suspended) &&
970                (GNUNET_SCHEDULER_NO_TASK == sender->warn_task) )
971           {
972             GNUNET_break (0 != type); /* type should never be 0 here, as we don't use 0 */
973             sender->warn_start = GNUNET_TIME_absolute_get ();       
974             sender->warn_task =
975                 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MINUTES,
976                                               &warn_no_receive_done, sender);
977             sender->warn_type = type;
978           }
979           sender->suspended++;
980         }
981         mh->callback (mh->callback_cls, sender, message);
982         found = GNUNET_YES;
983       }
984       i++;
985     }
986   }
987   if (GNUNET_NO == found)
988   {
989     LOG (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
990          "Received message of unknown type %d\n", type);
991     if (GNUNET_YES == server->require_found)
992       return GNUNET_SYSERR;
993   }
994   return GNUNET_OK;
995 }
996
997
998 /**
999  * We are receiving an incoming message.  Process it.
1000  *
1001  * @param cls our closure (handle for the client)
1002  * @param buf buffer with data received from network
1003  * @param available number of bytes available in buf
1004  * @param addr address of the sender
1005  * @param addrlen length of addr
1006  * @param errCode code indicating errors receiving, 0 for success
1007  */
1008 static void
1009 process_incoming (void *cls, const void *buf, size_t available,
1010                   const struct sockaddr *addr, socklen_t addrlen, int errCode);
1011
1012
1013 /**
1014  * Process messages from the client's message tokenizer until either
1015  * the tokenizer is empty (and then schedule receiving more), or
1016  * until some handler is not immediately done (then wait for restart_processing)
1017  * or shutdown.
1018  *
1019  * @param client the client to process, RC must have already been increased
1020  *        using GNUNET_SERVER_client_keep and will be decreased by one in this
1021  *        function
1022  * @param ret GNUNET_NO to start processing from the buffer,
1023  *            GNUNET_OK if the mst buffer is drained and we should instantly go back to receiving
1024  *            GNUNET_SYSERR if we should instantly abort due to error in a previous step
1025  */
1026 static void
1027 process_mst (struct GNUNET_SERVER_Client *client, int ret)
1028 {
1029   while ((GNUNET_SYSERR != ret) && (NULL != client->server) &&
1030          (GNUNET_YES != client->shutdown_now) && (0 == client->suspended))
1031   {
1032     if (GNUNET_OK == ret)
1033     {
1034       LOG (GNUNET_ERROR_TYPE_DEBUG,
1035            "Server re-enters receive loop, timeout: %s.\n",
1036            GNUNET_STRINGS_relative_time_to_string (client->idle_timeout, GNUNET_YES));
1037       client->receive_pending = GNUNET_YES;
1038       GNUNET_CONNECTION_receive (client->connection,
1039                                  GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1040                                  client->idle_timeout, &process_incoming,
1041                                  client);
1042       break;
1043     }
1044     LOG (GNUNET_ERROR_TYPE_DEBUG,
1045          "Server processes additional messages instantly.\n");
1046     if (NULL != client->server->mst_receive)
1047       ret =
1048           client->server->mst_receive (client->server->mst_cls, client->mst,
1049                                        client, NULL, 0, GNUNET_NO, GNUNET_YES);
1050     else
1051       ret =
1052           GNUNET_SERVER_mst_receive (client->mst, client, NULL, 0, GNUNET_NO,
1053                                      GNUNET_YES);
1054   }
1055   LOG (GNUNET_ERROR_TYPE_DEBUG,
1056        "Server leaves instant processing loop: ret = %d, server = %p, shutdown = %d, suspended = %u\n",
1057        ret, client->server, client->shutdown_now, client->suspended);
1058   if (GNUNET_NO == ret)
1059   {
1060     LOG (GNUNET_ERROR_TYPE_DEBUG,
1061          "Server has more data pending but is suspended.\n");
1062     client->receive_pending = GNUNET_SYSERR;    /* data pending */
1063   }
1064   if ((GNUNET_SYSERR == ret) || (GNUNET_YES == client->shutdown_now))
1065     GNUNET_SERVER_client_disconnect (client);
1066 }
1067
1068
1069 /**
1070  * We are receiving an incoming message.  Process it.
1071  *
1072  * @param cls our closure (handle for the client)
1073  * @param buf buffer with data received from network
1074  * @param available number of bytes available in buf
1075  * @param addr address of the sender
1076  * @param addrlen length of addr
1077  * @param errCode code indicating errors receiving, 0 for success
1078  */
1079 static void
1080 process_incoming (void *cls, const void *buf, size_t available,
1081                   const struct sockaddr *addr, socklen_t addrlen, int errCode)
1082 {
1083   struct GNUNET_SERVER_Client *client = cls;
1084   struct GNUNET_SERVER_Handle *server = client->server;
1085   struct GNUNET_TIME_Absolute end;
1086   struct GNUNET_TIME_Absolute now;
1087   int ret;
1088
1089   GNUNET_assert (GNUNET_YES == client->receive_pending);
1090   client->receive_pending = GNUNET_NO;
1091   now = GNUNET_TIME_absolute_get ();
1092   end = GNUNET_TIME_absolute_add (client->last_activity, client->idle_timeout);
1093
1094   if ((NULL == buf) && (0 == available) && (NULL == addr) && (0 == errCode) &&
1095       (GNUNET_YES != client->shutdown_now) && (NULL != server) &&
1096       (GNUNET_YES == GNUNET_CONNECTION_check (client->connection)) &&
1097       (end.abs_value_us > now.abs_value_us))
1098   {
1099     /* wait longer, timeout changed (i.e. due to us sending) */
1100     LOG (GNUNET_ERROR_TYPE_DEBUG,
1101          "Receive time out, but no disconnect due to sending (%p)\n",
1102          GNUNET_a2s (addr, addrlen));
1103     client->receive_pending = GNUNET_YES;
1104     GNUNET_CONNECTION_receive (client->connection,
1105                                GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1106                                GNUNET_TIME_absolute_get_remaining (end),
1107                                &process_incoming, client);
1108     return;
1109   }
1110   if ((NULL == buf) || (0 == available) || (0 != errCode) || (NULL == server) ||
1111       (GNUNET_YES == client->shutdown_now) ||
1112       (GNUNET_YES != GNUNET_CONNECTION_check (client->connection)))
1113   {
1114     /* other side closed connection, error connecting, etc. */
1115     GNUNET_SERVER_client_disconnect (client);
1116     return;
1117   }
1118   LOG (GNUNET_ERROR_TYPE_DEBUG, "Server receives %u bytes from `%s'.\n",
1119        (unsigned int) available, GNUNET_a2s (addr, addrlen));
1120   GNUNET_SERVER_client_keep (client);
1121   client->last_activity = now;
1122
1123   if (NULL != server->mst_receive)
1124     ret =
1125         client->server->mst_receive (client->server->mst_cls, client->mst,
1126                                      client, buf, available, GNUNET_NO, GNUNET_YES);
1127   else if (NULL != client->mst)
1128   {
1129     ret =
1130         GNUNET_SERVER_mst_receive (client->mst, client, buf, available, GNUNET_NO,
1131                                    GNUNET_YES);
1132   }
1133   else
1134   {
1135     GNUNET_break (0);
1136     return;
1137   }
1138
1139   process_mst (client, ret);
1140   GNUNET_SERVER_client_drop (client);
1141 }
1142
1143
1144 /**
1145  * Task run to start again receiving from the network
1146  * and process requests.
1147  *
1148  * @param cls our 'struct GNUNET_SERVER_Client*' to process more requests from
1149  * @param tc scheduler context (unused)
1150  */
1151 static void
1152 restart_processing (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1153 {
1154   struct GNUNET_SERVER_Client *client = cls;
1155
1156   GNUNET_assert (GNUNET_YES != client->shutdown_now);
1157   client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1158   if (GNUNET_NO == client->receive_pending)
1159   {
1160     LOG (GNUNET_ERROR_TYPE_DEBUG, "Server begins to read again from client.\n");
1161     client->receive_pending = GNUNET_YES;
1162     GNUNET_CONNECTION_receive (client->connection,
1163                                GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1164                                client->idle_timeout, &process_incoming, client);
1165     return;
1166   }
1167   LOG (GNUNET_ERROR_TYPE_DEBUG,
1168        "Server continues processing messages still in the buffer.\n");
1169   GNUNET_SERVER_client_keep (client);
1170   client->receive_pending = GNUNET_NO;
1171   process_mst (client, GNUNET_NO);
1172   GNUNET_SERVER_client_drop (client);
1173 }
1174
1175
1176 /**
1177  * This function is called whenever our inbound message tokenizer has
1178  * received a complete message.
1179  *
1180  * @param cls closure (struct GNUNET_SERVER_Handle)
1181  * @param client identification of the client (struct GNUNET_SERVER_Client*)
1182  * @param message the actual message
1183  *
1184  * @return GNUNET_OK on success, GNUNET_SYSERR to stop further processing
1185  */
1186 static int
1187 client_message_tokenizer_callback (void *cls, void *client,
1188                                    const struct GNUNET_MessageHeader *message)
1189 {
1190   struct GNUNET_SERVER_Handle *server = cls;
1191   struct GNUNET_SERVER_Client *sender = client;
1192   int ret;
1193
1194   LOG (GNUNET_ERROR_TYPE_DEBUG,
1195        "Tokenizer gives server message of type %u from client\n",
1196        ntohs (message->type));
1197   sender->in_process_client_buffer = GNUNET_YES;
1198   ret = GNUNET_SERVER_inject (server, sender, message);
1199   sender->in_process_client_buffer = GNUNET_NO;
1200   if ( (GNUNET_OK != ret) || (GNUNET_YES == sender->shutdown_now) )
1201   {
1202     GNUNET_SERVER_client_disconnect (sender);
1203     return GNUNET_SYSERR;
1204   }
1205   return GNUNET_OK;
1206 }
1207
1208
1209 /**
1210  * Add a TCP socket-based connection to the set of handles managed by
1211  * this server.  Use this function for outgoing (P2P) connections that
1212  * we initiated (and where this server should process incoming
1213  * messages).
1214  *
1215  * @param server the server to use
1216  * @param connection the connection to manage (client must
1217  *        stop using this connection from now on)
1218  * @return the client handle (client should call
1219  *         "client_drop" on the return value eventually)
1220  */
1221 struct GNUNET_SERVER_Client *
1222 GNUNET_SERVER_connect_socket (struct GNUNET_SERVER_Handle *server,
1223                               struct GNUNET_CONNECTION_Handle *connection)
1224 {
1225   struct GNUNET_SERVER_Client *client;
1226   struct NotifyList *n;
1227
1228   client = GNUNET_malloc (sizeof (struct GNUNET_SERVER_Client));
1229   client->connection = connection;
1230   client->reference_count = 1;
1231   client->server = server;
1232   client->last_activity = GNUNET_TIME_absolute_get ();
1233   client->idle_timeout = server->idle_timeout;
1234   GNUNET_CONTAINER_DLL_insert (server->clients_head,
1235                                server->clients_tail,
1236                                client);
1237   if (NULL != server->mst_create)
1238     client->mst =
1239         server->mst_create (server->mst_cls, client);
1240   else
1241     client->mst =
1242         GNUNET_SERVER_mst_create (&client_message_tokenizer_callback, server);
1243   GNUNET_assert (NULL != client->mst);
1244   for (n = server->connect_notify_list_head; NULL != n; n = n->next)
1245     n->callback (n->callback_cls, client);
1246
1247   client->receive_pending = GNUNET_YES;
1248   GNUNET_CONNECTION_receive (client->connection,
1249                              GNUNET_SERVER_MAX_MESSAGE_SIZE - 1,
1250                              client->idle_timeout, &process_incoming, client);
1251   return client;
1252 }
1253
1254
1255 /**
1256  * Change the timeout for a particular client.  Decreasing the timeout
1257  * may not go into effect immediately (only after the previous timeout
1258  * times out or activity happens on the socket).
1259  *
1260  * @param client the client to update
1261  * @param timeout new timeout for activities on the socket
1262  */
1263 void
1264 GNUNET_SERVER_client_set_timeout (struct GNUNET_SERVER_Client *client,
1265                                   struct GNUNET_TIME_Relative timeout)
1266 {
1267   client->idle_timeout = timeout;
1268 }
1269
1270
1271 /**
1272  * Notify the server that the given client handle should
1273  * be kept (keeps the connection up if possible, increments
1274  * the internal reference counter).
1275  *
1276  * @param client the client to keep
1277  */
1278 void
1279 GNUNET_SERVER_client_keep (struct GNUNET_SERVER_Client *client)
1280 {
1281   client->reference_count++;
1282 }
1283
1284
1285 /**
1286  * Notify the server that the given client handle is no
1287  * longer required.  Decrements the reference counter.  If
1288  * that counter reaches zero an inactive connection maybe
1289  * closed.
1290  *
1291  * @param client the client to drop
1292  */
1293 void
1294 GNUNET_SERVER_client_drop (struct GNUNET_SERVER_Client *client)
1295 {
1296   GNUNET_assert (client->reference_count > 0);
1297   client->reference_count--;
1298   if ((GNUNET_YES == client->shutdown_now) && (0 == client->reference_count))
1299     GNUNET_SERVER_client_disconnect (client);
1300 }
1301
1302
1303 /**
1304  * Obtain the network address of the other party.
1305  *
1306  * @param client the client to get the address for
1307  * @param addr where to store the address
1308  * @param addrlen where to store the length of the address
1309  * @return GNUNET_OK on success
1310  */
1311 int
1312 GNUNET_SERVER_client_get_address (struct GNUNET_SERVER_Client *client,
1313                                   void **addr, size_t * addrlen)
1314 {
1315   return GNUNET_CONNECTION_get_address (client->connection, addr, addrlen);
1316 }
1317
1318
1319 /**
1320  * Ask the server to notify us whenever a client disconnects.
1321  * This function is called whenever the actual network connection
1322  * is closed; the reference count may be zero or larger than zero
1323  * at this point.
1324  *
1325  * @param server the server manageing the clients
1326  * @param callback function to call on disconnect
1327  * @param callback_cls closure for callback
1328  */
1329 void
1330 GNUNET_SERVER_disconnect_notify (struct GNUNET_SERVER_Handle *server,
1331                                  GNUNET_SERVER_DisconnectCallback callback,
1332                                  void *callback_cls)
1333 {
1334   struct NotifyList *n;
1335
1336   n = GNUNET_malloc (sizeof (struct NotifyList));
1337   n->callback = callback;
1338   n->callback_cls = callback_cls;
1339   GNUNET_CONTAINER_DLL_insert (server->disconnect_notify_list_head,
1340                                server->disconnect_notify_list_tail,
1341                                n);
1342 }
1343
1344
1345 /**
1346  * Ask the server to notify us whenever a client connects.
1347  * This function is called whenever the actual network connection
1348  * is opened. If the server is destroyed before this
1349  * notification is explicitly cancelled, the 'callback' will
1350  * once be called with a 'client' argument of NULL to indicate
1351  * that the server itself is now gone (and that the callback
1352  * won't be called anymore and also can no longer be cancelled).
1353  *
1354  * @param server the server manageing the clients
1355  * @param callback function to call on sconnect
1356  * @param callback_cls closure for callback
1357  */
1358 void
1359 GNUNET_SERVER_connect_notify (struct GNUNET_SERVER_Handle *server,
1360     GNUNET_SERVER_ConnectCallback callback, void *callback_cls)
1361 {
1362   struct NotifyList *n;
1363
1364   n = GNUNET_malloc (sizeof (struct NotifyList));
1365   n->callback = callback;
1366   n->callback_cls = callback_cls;
1367   GNUNET_CONTAINER_DLL_insert (server->connect_notify_list_head,
1368                                server->connect_notify_list_tail,
1369                                n);
1370 }
1371
1372
1373 /**
1374  * Ask the server to stop notifying us whenever a client connects.
1375  *
1376  * @param server the server manageing the clients
1377  * @param callback function to call on connect
1378  * @param callback_cls closure for callback
1379  */
1380 void
1381 GNUNET_SERVER_disconnect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1382                                         GNUNET_SERVER_DisconnectCallback
1383                                         callback, void *callback_cls)
1384 {
1385   struct NotifyList *pos;
1386
1387   for (pos = server->disconnect_notify_list_head; NULL != pos; pos = pos->next)
1388     if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1389       break;
1390   if (NULL == pos)
1391   {
1392     GNUNET_break (0);
1393     return;
1394   }
1395   GNUNET_CONTAINER_DLL_remove (server->disconnect_notify_list_head,
1396                                server->disconnect_notify_list_tail,
1397                                pos);
1398   GNUNET_free (pos);
1399 }
1400
1401
1402 /**
1403  * Ask the server to stop notifying us whenever a client disconnects.
1404  *
1405  * @param server the server manageing the clients
1406  * @param callback function to call on disconnect
1407  * @param callback_cls closure for callback
1408  */
1409 void
1410 GNUNET_SERVER_connect_notify_cancel (struct GNUNET_SERVER_Handle *server,
1411     GNUNET_SERVER_ConnectCallback callback, void *callback_cls)
1412 {
1413   struct NotifyList *pos;
1414
1415   for (pos = server->connect_notify_list_head; NULL != pos; pos = pos->next)
1416     if ((pos->callback == callback) && (pos->callback_cls == callback_cls))
1417       break;
1418   if (NULL == pos)
1419   {
1420     GNUNET_break (0);
1421     return;
1422   }
1423   GNUNET_CONTAINER_DLL_remove (server->connect_notify_list_head,
1424                                server->connect_notify_list_tail,
1425                                pos);
1426   GNUNET_free (pos);
1427 }
1428
1429
1430 /**
1431  * Destroy the connection that is passed in via 'cls'.  Used
1432  * as calling 'GNUNET_CONNECTION_destroy' from within a function
1433  * that was itself called from within 'process_notify' of
1434  * 'connection.c' is not allowed (see #2329).
1435  *
1436  * @param cls connection to destroy
1437  * @param tc scheduler context (unused)
1438  */
1439 static void
1440 destroy_connection (void *cls,
1441                     const struct GNUNET_SCHEDULER_TaskContext *tc)
1442 {
1443   struct GNUNET_CONNECTION_Handle *connection = cls;
1444   
1445   GNUNET_CONNECTION_destroy (connection);
1446 }
1447
1448
1449 /**
1450  * Ask the server to disconnect from the given client.
1451  * This is the same as returning GNUNET_SYSERR from a message
1452  * handler, except that it allows dropping of a client even
1453  * when not handling a message from that client.
1454  *
1455  * @param client the client to disconnect from
1456  */
1457 void
1458 GNUNET_SERVER_client_disconnect (struct GNUNET_SERVER_Client *client)
1459 {
1460   struct GNUNET_SERVER_Handle *server = client->server;
1461   struct NotifyList *n;
1462
1463   LOG (GNUNET_ERROR_TYPE_DEBUG,
1464        "Client is being disconnected from the server.\n");
1465   if (GNUNET_SCHEDULER_NO_TASK != client->restart_task)
1466   {
1467     GNUNET_SCHEDULER_cancel (client->restart_task);
1468     client->restart_task = GNUNET_SCHEDULER_NO_TASK;
1469   }
1470   if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1471   {
1472     GNUNET_SCHEDULER_cancel (client->warn_task);
1473     client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1474   }
1475   if (GNUNET_YES == client->receive_pending)
1476   {
1477     GNUNET_CONNECTION_receive_cancel (client->connection);
1478     client->receive_pending = GNUNET_NO;
1479   }
1480   client->shutdown_now = GNUNET_YES;    
1481   client->reference_count++; /* make sure nobody else clean up client... */
1482   if ( (NULL != client->mst) &&
1483        (NULL != server) )
1484   {
1485     GNUNET_CONTAINER_DLL_remove (server->clients_head,
1486                                  server->clients_tail,
1487                                  client);
1488     if (NULL != server->mst_destroy)
1489       server->mst_destroy (server->mst_cls, client->mst);
1490     else
1491       GNUNET_SERVER_mst_destroy (client->mst);
1492     client->mst = NULL;
1493     for (n = server->disconnect_notify_list_head; NULL != n; n = n->next)
1494       n->callback (n->callback_cls, client);
1495   }
1496   client->reference_count--;
1497   if (client->reference_count > 0)
1498   {
1499     LOG (GNUNET_ERROR_TYPE_DEBUG,
1500          "RC still positive, not destroying everything.\n");
1501     client->server = NULL;
1502     return;
1503   }
1504   if (GNUNET_YES == client->in_process_client_buffer)
1505   {
1506     LOG (GNUNET_ERROR_TYPE_DEBUG,
1507          "Still processing inputs, not destroying everything.\n");
1508     return;
1509   }
1510   if (GNUNET_YES == client->persist)
1511     GNUNET_CONNECTION_persist_ (client->connection);
1512   if (NULL != client->th.cth)
1513     GNUNET_SERVER_notify_transmit_ready_cancel (&client->th);
1514   (void) GNUNET_SCHEDULER_add_now (&destroy_connection,
1515                                    client->connection);
1516   /* need to cancel again, as it might have been re-added
1517      in the meantime (i.e. during callbacks) */
1518   if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1519   {
1520     GNUNET_SCHEDULER_cancel (client->warn_task);
1521     client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1522   }
1523   if (GNUNET_YES == client->receive_pending)
1524   {
1525     GNUNET_CONNECTION_receive_cancel (client->connection);
1526     client->receive_pending = GNUNET_NO;
1527   }
1528   GNUNET_free (client);
1529   /* we might be in soft-shutdown, test if we're done */
1530   if (NULL != server)
1531     test_monitor_clients (server);
1532 }
1533
1534
1535 /**
1536  * Disable the "CORK" feature for communication with the given client,
1537  * forcing the OS to immediately flush the buffer on transmission
1538  * instead of potentially buffering multiple messages.
1539  *
1540  * @param client handle to the client
1541  * @return GNUNET_OK on success
1542  */
1543 int
1544 GNUNET_SERVER_client_disable_corking (struct GNUNET_SERVER_Client *client)
1545 {
1546   return GNUNET_CONNECTION_disable_corking (client->connection);
1547 }
1548
1549
1550 /**
1551  * Wrapper for transmission notification that calls the original
1552  * callback and update the last activity time for our connection.
1553  *
1554  * @param cls the 'struct GNUNET_SERVER_Client'
1555  * @param size number of bytes we can transmit
1556  * @param buf where to copy the message
1557  * @return number of bytes actually transmitted
1558  */
1559 static size_t
1560 transmit_ready_callback_wrapper (void *cls, size_t size, void *buf)
1561 {
1562   struct GNUNET_SERVER_Client *client = cls;
1563   GNUNET_CONNECTION_TransmitReadyNotify callback;
1564
1565   client->th.cth = NULL;
1566   callback = client->th.callback;
1567   client->th.callback = NULL;
1568   client->last_activity = GNUNET_TIME_absolute_get ();
1569   return callback (client->th.callback_cls, size, buf);
1570 }
1571
1572
1573 /**
1574  * Notify us when the server has enough space to transmit
1575  * a message of the given size to the given client.
1576  *
1577  * @param client client to transmit message to
1578  * @param size requested amount of buffer space
1579  * @param timeout after how long should we give up (and call
1580  *        notify with buf NULL and size 0)?
1581  * @param callback function to call when space is available
1582  * @param callback_cls closure for callback
1583  * @return non-NULL if the notify callback was queued; can be used
1584  *           to cancel the request using
1585  *           GNUNET_SERVER_notify_transmit_ready_cancel.
1586  *         NULL if we are already going to notify someone else (busy)
1587  */
1588 struct GNUNET_SERVER_TransmitHandle *
1589 GNUNET_SERVER_notify_transmit_ready (struct GNUNET_SERVER_Client *client,
1590                                      size_t size,
1591                                      struct GNUNET_TIME_Relative timeout,
1592                                      GNUNET_CONNECTION_TransmitReadyNotify
1593                                      callback, void *callback_cls)
1594 {
1595   if (NULL != client->th.callback)
1596     return NULL;
1597   client->th.callback_cls = callback_cls;
1598   client->th.callback = callback;
1599   client->th.cth = GNUNET_CONNECTION_notify_transmit_ready (client->connection, size,
1600                                                             timeout,
1601                                                             &transmit_ready_callback_wrapper,
1602                                                             client);
1603   return &client->th;
1604 }
1605
1606
1607 /**
1608  * Abort transmission request.
1609  *
1610  * @param th request to abort
1611  */
1612 void
1613 GNUNET_SERVER_notify_transmit_ready_cancel (struct GNUNET_SERVER_TransmitHandle *th)
1614 {
1615   GNUNET_CONNECTION_notify_transmit_ready_cancel (th->cth);
1616   th->cth = NULL;
1617   th->callback = NULL;
1618 }
1619
1620
1621 /**
1622  * Set the persistent flag on this client, used to setup client connection
1623  * to only be killed when the service it's connected to is actually dead.
1624  *
1625  * @param client the client to set the persistent flag on
1626  */
1627 void
1628 GNUNET_SERVER_client_persist_ (struct GNUNET_SERVER_Client *client)
1629 {
1630   client->persist = GNUNET_YES;
1631 }
1632
1633
1634 /**
1635  * Resume receiving from this client, we are done processing the
1636  * current request.  This function must be called from within each
1637  * GNUNET_SERVER_MessageCallback (or its respective continuations).
1638  *
1639  * @param client client we were processing a message of
1640  * @param success GNUNET_OK to keep the connection open and
1641  *                          continue to receive
1642  *                GNUNET_NO to close the connection (normal behavior)
1643  *                GNUNET_SYSERR to close the connection (signal
1644  *                          serious error)
1645  */
1646 void
1647 GNUNET_SERVER_receive_done (struct GNUNET_SERVER_Client *client, int success)
1648 {
1649   if (NULL == client)
1650     return;
1651   GNUNET_assert (client->suspended > 0);
1652   client->suspended--;
1653   if (GNUNET_OK != success)
1654   {
1655     LOG (GNUNET_ERROR_TYPE_DEBUG,
1656          "GNUNET_SERVER_receive_done called with failure indication\n");
1657     if ( (client->reference_count > 0) || (client->suspended > 0) )
1658       client->shutdown_now = GNUNET_YES;
1659     else
1660       GNUNET_SERVER_client_disconnect (client);
1661     return;
1662   }
1663   if (client->suspended > 0)
1664   {
1665     LOG (GNUNET_ERROR_TYPE_DEBUG,
1666          "GNUNET_SERVER_receive_done called, but more clients pending\n");
1667     return;
1668   }
1669   if (GNUNET_SCHEDULER_NO_TASK != client->warn_task)
1670   {
1671     GNUNET_SCHEDULER_cancel (client->warn_task);
1672     client->warn_task = GNUNET_SCHEDULER_NO_TASK;
1673   }
1674   if (GNUNET_YES == client->in_process_client_buffer)
1675   {
1676     LOG (GNUNET_ERROR_TYPE_DEBUG,
1677          "GNUNET_SERVER_receive_done called while still in processing loop\n");
1678     return;
1679   }
1680   if ((NULL == client->server) || (GNUNET_YES == client->shutdown_now))
1681   {
1682     GNUNET_SERVER_client_disconnect (client);
1683     return;
1684   }
1685   LOG (GNUNET_ERROR_TYPE_DEBUG,
1686        "GNUNET_SERVER_receive_done causes restart in reading from the socket\n");
1687   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == client->restart_task);
1688   client->restart_task = GNUNET_SCHEDULER_add_now (&restart_processing, client);
1689 }
1690
1691
1692 /* end of server.c */