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