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