- added check against statistics
[oweals/gnunet.git] / src / arm / arm_api.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010, 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 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 arm/arm_api.c
23  * @brief API for accessing the ARM service
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "gnunet_arm_service.h"
28 #include "gnunet_client_lib.h"
29 #include "gnunet_getopt_lib.h"
30 #include "gnunet_os_lib.h"
31 #include "gnunet_protocols.h"
32 #include "gnunet_server_lib.h"
33 #include "arm.h"
34
35 #define LOG(kind,...) GNUNET_log_from (kind, "arm-api",__VA_ARGS__)
36
37 /**
38  * Handle for interacting with ARM.
39  */
40 struct GNUNET_ARM_Handle
41 {
42
43   /**
44    * Our connection to the ARM service.
45    */
46   struct GNUNET_CLIENT_Connection *client;
47
48   /**
49    * The configuration that we are using.
50    */
51   struct GNUNET_CONFIGURATION_Handle *cfg;
52
53 };
54
55 /**
56  * Context for handling the shutdown of a service.
57  */
58 struct ShutdownContext
59 {
60   /**
61    * Connection to the service that is being shutdown.
62    */
63   struct GNUNET_CLIENT_Connection *sock;
64
65   /**
66    * Time allowed for shutdown to happen.
67    */
68   struct GNUNET_TIME_Absolute timeout;
69
70   /**
71    * Task set up to cancel the shutdown request on timeout.
72    */
73   GNUNET_SCHEDULER_TaskIdentifier cancel_task;
74
75   /**
76    * Task to call once shutdown complete
77    */
78   GNUNET_CLIENT_ShutdownTask cont;
79
80   /**
81    * Closure for shutdown continuation
82    */
83   void *cont_cls;
84
85   /**
86    * Handle for transmission request.
87    */
88   struct GNUNET_CLIENT_TransmitHandle *th;
89
90   /**
91    * Result of the operation
92    */
93   enum GNUNET_ARM_ProcessStatus confirmed;
94
95 };
96
97
98 /**
99  * Handler receiving response to service shutdown requests.
100  * First call with NULL: service misbehaving, or something.
101  * First call with GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN_ACK:
102  *   - service will shutdown
103  * Second call with NULL:
104  *   - service has now really shut down.
105  *
106  * @param cls closure
107  * @param msg NULL, indicating socket closure.
108  */
109 static void
110 service_shutdown_handler (void *cls, const struct GNUNET_MessageHeader *msg)
111 {
112   struct ShutdownContext *shutdown_ctx = cls;
113   const struct GNUNET_ARM_ResultMessage *rmsg;
114
115   if (msg == NULL)
116   {
117     if (shutdown_ctx->cont != NULL)
118     {
119       if (shutdown_ctx->confirmed == GNUNET_ARM_PROCESS_SHUTDOWN)
120       {
121         /* shutdown is now complete, as we waited for the network disconnect... */
122         shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_DOWN);
123       }
124       else
125       {
126         /* communication error */
127         shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
128       }
129     }
130     GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
131     GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
132     GNUNET_free (shutdown_ctx);
133     return;
134   }
135   if (ntohs (msg->size) ==
136       sizeof (struct GNUNET_ARM_ResultMessage))
137   {
138     rmsg = (const struct GNUNET_ARM_ResultMessage*) msg;
139     shutdown_ctx->confirmed = (enum GNUNET_ARM_ProcessStatus) ntohl (rmsg->status);
140     if (shutdown_ctx->confirmed != GNUNET_ARM_PROCESS_SHUTDOWN)
141     {
142       /* ARM is not shutting down, well, report the error and be done with it... */
143       shutdown_ctx->cont (shutdown_ctx->cont_cls, shutdown_ctx->confirmed);
144       GNUNET_SCHEDULER_cancel (shutdown_ctx->cancel_task);
145       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
146       GNUNET_free (shutdown_ctx);
147       return;
148     }
149   }
150   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
151                          shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
152 }
153
154
155 /**
156  * Shutting down took too long, cancel receive and return error.
157  *
158  * @param cls closure
159  * @param tc context information (why was this task triggered now)
160  */
161 static void
162 service_shutdown_cancel (void *cls,
163                          const struct GNUNET_SCHEDULER_TaskContext *tc)
164 {
165   struct ShutdownContext *shutdown_ctx = cls;
166
167   shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_TIMEOUT);
168   GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
169   GNUNET_free (shutdown_ctx);
170 }
171
172
173 /**
174  * If possible, write a shutdown message to the target
175  * buffer and destroy the client connection.
176  *
177  * @param cls the "struct GNUNET_CLIENT_Connection" to destroy
178  * @param size number of bytes available in buf
179  * @param buf NULL on error, otherwise target buffer
180  * @return number of bytes written to buf
181  */
182 static size_t
183 write_shutdown (void *cls, size_t size, void *buf)
184 {
185   struct ShutdownContext *shutdown_ctx = cls;
186   struct GNUNET_MessageHeader *msg;
187
188   shutdown_ctx->th = NULL;
189   if (size < sizeof (struct GNUNET_MessageHeader))
190     {
191       LOG (GNUNET_ERROR_TYPE_WARNING,
192            _("Failed to transmit shutdown request to client.\n"));
193       shutdown_ctx->cont (shutdown_ctx->cont_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
194       GNUNET_CLIENT_disconnect (shutdown_ctx->sock, GNUNET_NO);
195       GNUNET_free (shutdown_ctx);
196       return 0;                 /* client disconnected */
197     }
198
199   GNUNET_CLIENT_receive (shutdown_ctx->sock, &service_shutdown_handler,
200                          shutdown_ctx, GNUNET_TIME_UNIT_FOREVER_REL);
201   shutdown_ctx->cancel_task =
202     GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining
203                                   (shutdown_ctx->timeout),
204                                   &service_shutdown_cancel, shutdown_ctx);
205   msg = (struct GNUNET_MessageHeader *) buf;
206   msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_SHUTDOWN);
207   msg->size = htons (sizeof (struct GNUNET_MessageHeader));
208   return sizeof (struct GNUNET_MessageHeader);
209 }
210
211
212 /**
213  * Request that the service should shutdown.
214  * Afterwards, the connection will automatically be
215  * disconnected.  Hence the "sock" should not
216  * be used by the caller after this call
217  * (calling this function frees "sock" after a while).
218  *
219  * @param sock the socket connected to the service
220  * @param timeout how long to wait before giving up on transmission
221  * @param cont continuation to call once the service is really down
222  * @param cont_cls closure for continuation
223  *
224  */
225 static void
226 arm_service_shutdown (struct GNUNET_CLIENT_Connection *sock,
227                       struct GNUNET_TIME_Relative timeout,
228                       GNUNET_CLIENT_ShutdownTask cont, void *cont_cls)
229 {
230   struct ShutdownContext *shutdown_ctx;
231
232   shutdown_ctx = GNUNET_malloc (sizeof (struct ShutdownContext));
233   shutdown_ctx->cont = cont;
234   shutdown_ctx->cont_cls = cont_cls;
235   shutdown_ctx->sock = sock;
236   shutdown_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
237   shutdown_ctx->confirmed = GNUNET_ARM_PROCESS_COMMUNICATION_ERROR;    
238   shutdown_ctx->th = GNUNET_CLIENT_notify_transmit_ready (sock,
239                                                           sizeof (struct GNUNET_MessageHeader),
240                                                           timeout, GNUNET_NO, &write_shutdown,
241                                                           shutdown_ctx);
242 }
243
244
245 /**
246  * Setup a context for communicating with ARM.  Note that this
247  * can be done even if the ARM service is not yet running.
248  *
249  * @param cfg configuration to use (needed to contact ARM;
250  *        the ARM service may internally use a different
251  *        configuration to determine how to start the service).
252  * @param service service that *this* process is implementing/providing, can be NULL
253  * @return context to use for further ARM operations, NULL on error
254  */
255 struct GNUNET_ARM_Handle *
256 GNUNET_ARM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
257                     const char *service)
258 {
259   struct GNUNET_ARM_Handle *ret;
260
261   ret = GNUNET_malloc (sizeof (struct GNUNET_ARM_Handle));
262   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
263   return ret;
264 }
265
266
267 /**
268  * Disconnect from the ARM service.
269  *
270  * @param h the handle that was being used
271  */
272 void
273 GNUNET_ARM_disconnect (struct GNUNET_ARM_Handle *h)
274 {
275   if (h->client != NULL)
276     GNUNET_CLIENT_disconnect (h->client, GNUNET_NO);
277   GNUNET_CONFIGURATION_destroy (h->cfg);
278   GNUNET_free (h);
279 }
280
281
282 struct ARM_ShutdownContext
283 {
284   /**
285    * Callback to call once shutdown complete.
286    */
287   GNUNET_ARM_Callback cb;
288
289   /**
290    * Closure for callback.
291    */
292   void *cb_cls;
293 };
294
295
296 /**
297  * Internal state for a request with ARM.
298  */
299 struct RequestContext
300 {
301
302   /**
303    * Pointer to our handle with ARM.
304    */
305   struct GNUNET_ARM_Handle *h;
306
307   /**
308    * Function to call with a status code for the requested operation.
309    */
310   GNUNET_ARM_Callback callback;
311
312   /**
313    * Closure for "callback".
314    */
315   void *cls;
316
317   /**
318    * Timeout for the operation.
319    */
320   struct GNUNET_TIME_Absolute timeout;
321
322   /**
323    * Type of the request expressed as a message type (start or stop).
324    */
325   uint16_t type;
326
327 };
328
329 #include "do_start_process.c"
330
331
332 /**
333  * A client specifically requested starting of ARM itself.
334  * This function is called with information about whether
335  * or not ARM is running; if it is, report success.  If
336  * it is not, start the ARM process.
337  *
338  * @param cls the context for the request that we will report on (struct RequestContext*)
339  * @param tc why were we called (reason says if ARM is running)
340  */
341 static void
342 arm_service_report (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
343 {
344   struct RequestContext *pos = cls;
345   struct GNUNET_OS_Process *proc;
346   char *binary;
347   char *config;
348   char *loprefix;
349   char *lopostfix;
350
351   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_PREREQ_DONE))
352     {
353 #if DEBUG_ARM
354       LOG (GNUNET_ERROR_TYPE_DEBUG, "Looks like `%s' is already running.\n",
355            "gnunet-service-arm");
356 #endif
357       /* arm is running! */
358       if (pos->callback != NULL)
359         pos->callback (pos->cls, GNUNET_ARM_PROCESS_ALREADY_RUNNING);
360       GNUNET_free (pos);
361       return;
362     }
363 #if DEBUG_ARM
364   LOG (GNUNET_ERROR_TYPE_DEBUG,
365        "Looks like `%s' is not running, will start it.\n",
366        "gnunet-service-arm");
367 #endif
368   if (GNUNET_OK !=
369       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "PREFIX",
370                                              &loprefix))
371     loprefix = GNUNET_strdup ("");
372   if (GNUNET_OK !=
373       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "OPTIONS",
374                                              &lopostfix))
375     lopostfix = GNUNET_strdup ("");
376   if (GNUNET_OK !=
377       GNUNET_CONFIGURATION_get_value_string (pos->h->cfg, "arm", "BINARY",
378                                              &binary))
379     {
380       LOG (GNUNET_ERROR_TYPE_WARNING,
381            _
382            ("Configuration failes to specify option `%s' in section `%s'!\n"),
383            "BINARY", "arm");
384       if (pos->callback != NULL)
385         pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
386       GNUNET_free (pos);
387       GNUNET_free (loprefix);
388       GNUNET_free (lopostfix);
389       return;
390     }
391   if (GNUNET_OK !=
392       GNUNET_CONFIGURATION_get_value_filename (pos->h->cfg, "arm", "CONFIG",
393                                                &config))
394     {
395       LOG (GNUNET_ERROR_TYPE_WARNING,
396            _("Configuration fails to specify option `%s' in section `%s'!\n"),
397            "CONFIG", "arm");
398       if (pos->callback != NULL)
399         pos->callback (pos->cls, GNUNET_ARM_PROCESS_UNKNOWN);
400       GNUNET_free (binary);
401       GNUNET_free (pos);
402       GNUNET_free (loprefix);
403       GNUNET_free (lopostfix);
404       return;
405     }
406   if ((GNUNET_YES ==
407        GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING", "WEAKRANDOM"))
408       && (GNUNET_YES ==
409           GNUNET_CONFIGURATION_get_value_yesno (pos->h->cfg, "TESTING",
410                                                 "WEAKRANDOM"))
411       && (GNUNET_NO ==
412           GNUNET_CONFIGURATION_have_value (pos->h->cfg, "TESTING",
413                                            "HOSTFILE")))
414     {
415       /* Means we are ONLY running locally */
416       /* we're clearly running a test, don't daemonize */
417       proc = do_start_process (GNUNET_NO,
418                                NULL, loprefix, binary, "-c", config,
419 #if DEBUG_ARM
420                                "-L", "DEBUG",
421 #endif
422                                /* no daemonization! */
423                                lopostfix, NULL);
424     }
425   else
426     {
427       proc = do_start_process (GNUNET_NO,
428                                NULL, loprefix, binary, "-c", config,
429 #if DEBUG_ARM
430                                "-L", "DEBUG",
431 #endif
432                                "-d", lopostfix, NULL);
433     }
434   GNUNET_free (binary);
435   GNUNET_free (config);
436   GNUNET_free (loprefix);
437   GNUNET_free (lopostfix);
438   if (proc == NULL)
439     {
440       if (pos->callback != NULL)
441         pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
442       GNUNET_free (pos);
443       return;
444     }
445   if (pos->callback != NULL)
446     pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
447   GNUNET_free (proc);
448   GNUNET_free (pos);
449 }
450
451
452 /**
453  * Process a response from ARM to a request for a change in service
454  * status.
455  *
456  * @param cls the request context
457  * @param msg the response
458  */
459 static void
460 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
461 {
462   struct RequestContext *sc = cls;
463   const struct GNUNET_ARM_ResultMessage *res;
464   enum GNUNET_ARM_ProcessStatus status;
465
466   if ((msg == NULL) ||
467       (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
468     {
469       LOG (GNUNET_ERROR_TYPE_WARNING,
470            _
471            ("Error receiving response to `%s' request from ARM for service `%s'\n"),
472            (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
473            (const char *) &sc[1]);
474       GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
475       sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
476       GNUNET_assert (NULL != sc->h->client);
477       if (sc->callback != NULL)
478         sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
479       GNUNET_free (sc);
480       return;
481     }
482   res = (const struct GNUNET_ARM_ResultMessage *) msg;
483 #if DEBUG_ARM
484   LOG (GNUNET_ERROR_TYPE_DEBUG,
485        "Received response from ARM for service `%s': %u\n",
486        (const char *) &sc[1], ntohs (msg->type));
487 #endif
488   status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
489   if (sc->callback != NULL)
490     sc->callback (sc->cls, status);
491   GNUNET_free (sc);
492 }
493
494
495 /**
496  * Start or stop a service.
497  *
498  * @param h handle to ARM
499  * @param service_name name of the service
500  * @param timeout how long to wait before failing for good
501  * @param cb callback to invoke when service is ready
502  * @param cb_cls closure for callback
503  * @param type type of the request
504  */
505 static void
506 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
507                 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
508                 void *cb_cls, uint16_t type)
509 {
510   struct RequestContext *sctx;
511   size_t slen;
512   struct GNUNET_MessageHeader *msg;
513
514   slen = strlen (service_name) + 1;
515   if (slen + sizeof (struct GNUNET_MessageHeader) >=
516       GNUNET_SERVER_MAX_MESSAGE_SIZE)
517     {
518       GNUNET_break (0);
519       if (cb != NULL)
520         cb (cb_cls, GNUNET_NO);
521       return;
522     }
523 #if DEBUG_ARM
524   LOG (GNUNET_ERROR_TYPE_DEBUG,
525        (type ==
526         GNUNET_MESSAGE_TYPE_ARM_START) ?
527        _("Requesting start of service `%s'.\n") :
528        _("Requesting termination of service `%s'.\n"), service_name);
529 #endif
530   sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
531   sctx->h = h;
532   sctx->callback = cb;
533   sctx->cls = cb_cls;
534   sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
535   sctx->type = type;
536   memcpy (&sctx[1], service_name, slen);
537   msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
538   msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
539   msg->type = htons (sctx->type);
540   memcpy (&msg[1], service_name, slen);
541   if (GNUNET_OK !=
542       GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
543                                                GNUNET_TIME_absolute_get_remaining
544                                                (sctx->timeout), GNUNET_YES,
545                                                &handle_response, sctx))
546     {
547       LOG (GNUNET_ERROR_TYPE_WARNING,
548            (type ==
549             GNUNET_MESSAGE_TYPE_ARM_START) ?
550            _("Error while trying to transmit request to start `%s' to ARM\n")
551            :
552            _("Error while trying to transmit request to stop `%s' to ARM\n"),
553            (const char *) &service_name);
554       if (cb != NULL)
555         cb (cb_cls, GNUNET_SYSERR);
556       GNUNET_free (sctx);
557       GNUNET_free (msg);
558       return;
559     }
560   GNUNET_free (msg);
561 }
562
563
564 /**
565  * Start a service.
566  *
567  * @param h handle to ARM
568  * @param service_name name of the service
569  * @param timeout how long to wait before failing for good
570  * @param cb callback to invoke when service is ready
571  * @param cb_cls closure for callback
572  */
573 void
574 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
575                           const char *service_name,
576                           struct GNUNET_TIME_Relative timeout,
577                           GNUNET_ARM_Callback cb, void *cb_cls)
578 {
579   struct RequestContext *sctx;
580   struct GNUNET_CLIENT_Connection *client;
581   size_t slen;
582
583 #if DEBUG_ARM
584   LOG (GNUNET_ERROR_TYPE_DEBUG,
585        _("Asked to start service `%s' within %llu ms\n"), service_name,
586        (unsigned long long) timeout.rel_value);
587 #endif
588   if (0 == strcasecmp ("arm", service_name))
589     {
590       slen = strlen ("arm") + 1;
591       sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
592       sctx->h = h;
593       sctx->callback = cb;
594       sctx->cls = cb_cls;
595       sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
596       memcpy (&sctx[1], service_name, slen);
597       GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
598                                   sctx);
599       return;
600     }
601   if (h->client == NULL)
602     {
603       client = GNUNET_CLIENT_connect ("arm", h->cfg);
604       if (client == NULL)
605         {
606           LOG (GNUNET_ERROR_TYPE_DEBUG,
607                "arm_api, GNUNET_CLIENT_connect returned NULL\n");
608           cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
609           return;
610         }
611       LOG (GNUNET_ERROR_TYPE_DEBUG,
612            "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
613       h->client = client;
614     }
615   LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
616   change_service (h, service_name, timeout, cb, cb_cls,
617                   GNUNET_MESSAGE_TYPE_ARM_START);
618 }
619
620
621 /**
622  * Callback from the arm stop service call, indicates that the arm service
623  * is well and truly dead, won't die, or an error occurred.
624  *
625  * @param cls closure for the callback
626  * @param reason reason for callback
627  */
628 static void
629 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
630 {
631   struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
632
633   if (arm_shutdown_ctx->cb != NULL)
634     arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
635
636   GNUNET_free (arm_shutdown_ctx);
637 }
638
639
640 /**
641  * Stop a service.
642  *
643  * @param h handle to ARM
644  * @param service_name name of the service
645  * @param timeout how long to wait before failing for good
646  * @param cb callback to invoke when service is ready
647  * @param cb_cls closure for callback
648  */
649 void
650 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
651                          const char *service_name,
652                          struct GNUNET_TIME_Relative timeout,
653                          GNUNET_ARM_Callback cb, void *cb_cls)
654 {
655   struct ARM_ShutdownContext *arm_shutdown_ctx;
656   struct GNUNET_CLIENT_Connection *client;
657
658   LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
659        service_name, (unsigned long long) timeout.rel_value);
660   if (h->client == NULL)
661     {
662       client = GNUNET_CLIENT_connect ("arm", h->cfg);
663       if (client == NULL)
664         {
665           cb (cb_cls, GNUNET_SYSERR);
666           return;
667         }
668       h->client = client;
669     }
670   if (0 == strcasecmp ("arm", service_name))
671     {
672       arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
673       arm_shutdown_ctx->cb = cb;
674       arm_shutdown_ctx->cb_cls = cb_cls;
675       arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
676                             arm_shutdown_ctx);
677       h->client = NULL;
678       return;
679     }
680   change_service (h, service_name, timeout, cb, cb_cls,
681                   GNUNET_MESSAGE_TYPE_ARM_STOP);
682 }
683
684
685 /**
686  * Internal state for a list request with ARM.
687  */
688 struct ListRequestContext
689 {
690
691   /**
692    * Pointer to our handle with ARM.
693    */
694   struct GNUNET_ARM_Handle *h;
695
696   /**
697    * Function to call with a status code for the requested operation.
698    */
699   GNUNET_ARM_List_Callback callback;
700
701   /**
702    * Closure for "callback".
703    */
704   void *cls;
705
706   /**
707    * Timeout for the operation.
708    */
709   struct GNUNET_TIME_Absolute timeout;
710 };
711
712
713 /**
714  * Process a response from ARM for the list request.
715  *
716  * @param cls the list request context
717  * @param msg the response
718  */
719 static void
720 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
721 {
722   struct ListRequestContext *sc = cls;
723   const struct GNUNET_ARM_ListResultMessage *res;
724   const char *pos;
725   uint16_t size_check;
726   uint16_t rcount;
727   uint16_t msize;
728   
729   if (NULL == msg)
730   {
731     LOG (GNUNET_ERROR_TYPE_WARNING,
732          "Error receiving response to LIST request from ARM\n");
733     GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
734     sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
735     GNUNET_assert (NULL != sc->h->client);
736     if (sc->callback != NULL)
737       sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
738     GNUNET_free (sc);
739     return;
740   }
741    
742   if (NULL == sc->callback) 
743   {
744     GNUNET_break (0);
745     GNUNET_free (sc);
746     return;
747   }  
748   msize = ntohs (msg->size);
749   if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
750        (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
751   {
752     GNUNET_break (0);
753     sc->callback (sc->cls, GNUNET_NO, 0, NULL);
754     GNUNET_free (sc);
755     return;
756   }
757   size_check = 0;
758   res = (const struct GNUNET_ARM_ListResultMessage *) msg;
759   rcount = ntohs (res->count);
760   {
761     const char *list[rcount];
762     unsigned int i;
763     
764     pos = (const char *)&res[1];   
765     for (i=0; i<rcount; i++)
766     {
767       const char *end = memchr (pos, 0, msize - size_check);
768       if (NULL == end)
769       {
770         GNUNET_break (0);
771         sc->callback (sc->cls, GNUNET_NO, 0, NULL);
772         GNUNET_free (sc);
773         return;
774       }
775       list[i] = pos;
776       size_check += (end - pos) + 1;
777       pos = end + 1;
778     }
779     sc->callback (sc->cls, GNUNET_YES, rcount, list);
780   }
781   GNUNET_free (sc);
782 }
783
784
785 /**
786  * List all running services.
787  * 
788  * @param h handle to ARM
789  * @param timeout how long to wait before failing for good
790  * @param cb callback to invoke when service is ready
791  * @param cb_cls closure for callback
792  */
793 void
794 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
795                                   struct GNUNET_TIME_Relative timeout,
796                                   GNUNET_ARM_List_Callback cb, void *cb_cls)
797 {
798   struct ListRequestContext *sctx;
799   struct GNUNET_MessageHeader msg;
800   struct GNUNET_CLIENT_Connection *client;
801   
802     if (h->client == NULL)
803     {
804       client = GNUNET_CLIENT_connect ("arm", h->cfg);
805       if (client == NULL)
806         {
807           LOG (GNUNET_ERROR_TYPE_DEBUG,
808                "arm_api, GNUNET_CLIENT_connect returned NULL\n");
809           cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
810           return;
811         }
812       LOG (GNUNET_ERROR_TYPE_DEBUG,
813            "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
814       h->client = client;
815     }
816
817   sctx = GNUNET_malloc (sizeof (struct RequestContext));
818   sctx->h = h;
819   sctx->callback = cb;
820   sctx->cls = cb_cls;
821   sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
822   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
823   msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
824   
825   LOG (GNUNET_ERROR_TYPE_DEBUG, 
826        "Requesting LIST from ARM service with timeout: %llu ms\n", 
827        (unsigned long long)timeout.rel_value);
828   
829     if (GNUNET_OK !=
830       GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, 
831                                                &msg,
832                                                GNUNET_TIME_absolute_get_remaining
833                                                (sctx->timeout), 
834                                                GNUNET_YES,
835                                                &handle_list_response, 
836                                                sctx))
837     {
838       LOG (GNUNET_ERROR_TYPE_WARNING, 
839            "Error while trying to transmit request to list services to ARM\n");
840       if (cb != NULL)
841         cb (cb_cls, GNUNET_SYSERR, 0, NULL);
842       GNUNET_free (sctx);
843       return;
844     }
845 }
846
847 /* end of arm_api.c */