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