- fix for 601 assertion
[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                              /* no daemonization! */
387                              lopostfix, NULL);
388   }
389   else
390   {
391     proc = do_start_process (GNUNET_NO,
392                              NULL, loprefix, binary, "-c", config,
393                              "-d", lopostfix, NULL);
394   }
395   GNUNET_free (binary);
396   GNUNET_free (config);
397   GNUNET_free (loprefix);
398   GNUNET_free (lopostfix);
399   if (proc == NULL)
400     {
401       if (pos->callback != NULL)
402         pos->callback (pos->cls, GNUNET_ARM_PROCESS_FAILURE);
403       GNUNET_free (pos);
404       return;
405     }
406   if (pos->callback != NULL)
407     pos->callback (pos->cls, GNUNET_ARM_PROCESS_STARTING);
408   GNUNET_free (proc);
409   GNUNET_free (pos);
410 }
411
412
413 /**
414  * Process a response from ARM to a request for a change in service
415  * status.
416  *
417  * @param cls the request context
418  * @param msg the response
419  */
420 static void
421 handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
422 {
423   struct RequestContext *sc = cls;
424   const struct GNUNET_ARM_ResultMessage *res;
425   enum GNUNET_ARM_ProcessStatus status;
426
427   if ((msg == NULL) ||
428       (ntohs (msg->size) != sizeof (struct GNUNET_ARM_ResultMessage)))
429   {
430     LOG (GNUNET_ERROR_TYPE_WARNING,
431          _
432          ("Error receiving response to `%s' request from ARM for service `%s'\n"),
433          (sc->type == GNUNET_MESSAGE_TYPE_ARM_START) ? "START" : "STOP",
434          (const char *) &sc[1]);
435     GNUNET_CLIENT_disconnect (sc->h->client);
436     sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
437     GNUNET_assert (NULL != sc->h->client);
438     if (sc->callback != NULL)
439       sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
440     GNUNET_free (sc);
441     return;
442   }
443   res = (const struct GNUNET_ARM_ResultMessage *) msg;
444   LOG (GNUNET_ERROR_TYPE_DEBUG,
445        "Received response from ARM for service `%s': %u\n",
446        (const char *) &sc[1], ntohs (msg->type));
447   status = (enum GNUNET_ARM_ProcessStatus) ntohl (res->status);
448   if (sc->callback != NULL)
449     sc->callback (sc->cls, status);
450   GNUNET_free (sc);
451 }
452
453
454 /**
455  * Start or stop a service.
456  *
457  * @param h handle to ARM
458  * @param service_name name of the service
459  * @param timeout how long to wait before failing for good
460  * @param cb callback to invoke when service is ready
461  * @param cb_cls closure for callback
462  * @param type type of the request
463  */
464 static void
465 change_service (struct GNUNET_ARM_Handle *h, const char *service_name,
466                 struct GNUNET_TIME_Relative timeout, GNUNET_ARM_Callback cb,
467                 void *cb_cls, uint16_t type)
468 {
469   struct RequestContext *sctx;
470   size_t slen;
471   struct GNUNET_MessageHeader *msg;
472
473   slen = strlen (service_name) + 1;
474   if (slen + sizeof (struct GNUNET_MessageHeader) >=
475       GNUNET_SERVER_MAX_MESSAGE_SIZE)
476   {
477     GNUNET_break (0);
478     if (cb != NULL)
479       cb (cb_cls, GNUNET_NO);
480     return;
481   }
482   LOG (GNUNET_ERROR_TYPE_DEBUG,
483        (type ==
484         GNUNET_MESSAGE_TYPE_ARM_START) ?
485        _("Requesting start of service `%s'.\n") :
486        _("Requesting termination of service `%s'.\n"), service_name);
487   sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
488   sctx->h = h;
489   sctx->callback = cb;
490   sctx->cls = cb_cls;
491   sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
492   sctx->type = type;
493   memcpy (&sctx[1], service_name, slen);
494   msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader) + slen);
495   msg->size = htons (sizeof (struct GNUNET_MessageHeader) + slen);
496   msg->type = htons (sctx->type);
497   memcpy (&msg[1], service_name, slen);
498   if (GNUNET_OK !=
499       GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, msg,
500                                                GNUNET_TIME_absolute_get_remaining
501                                                (sctx->timeout), GNUNET_YES,
502                                                &handle_response, sctx))
503   {
504     LOG (GNUNET_ERROR_TYPE_WARNING,
505          (type ==
506           GNUNET_MESSAGE_TYPE_ARM_START)
507          ? _("Error while trying to transmit request to start `%s' to ARM\n")    
508          : _("Error while trying to transmit request to stop `%s' to ARM\n"),
509          (const char *) &service_name);
510     if (cb != NULL)
511       cb (cb_cls, GNUNET_SYSERR);
512     GNUNET_free (sctx);
513     GNUNET_free (msg);
514     return;
515   }
516   GNUNET_free (msg);
517 }
518
519
520 /**
521  * Start a service.
522  *
523  * @param h handle to ARM
524  * @param service_name name of the service
525  * @param timeout how long to wait before failing for good
526  * @param cb callback to invoke when service is ready
527  * @param cb_cls closure for callback
528  */
529 void
530 GNUNET_ARM_start_service (struct GNUNET_ARM_Handle *h,
531                           const char *service_name,
532                           struct GNUNET_TIME_Relative timeout,
533                           GNUNET_ARM_Callback cb, void *cb_cls)
534 {
535   struct RequestContext *sctx;
536   struct GNUNET_CLIENT_Connection *client;
537   size_t slen;
538
539   LOG (GNUNET_ERROR_TYPE_DEBUG,
540        _("Asked to start service `%s' within %llu ms\n"), service_name,
541        (unsigned long long) timeout.rel_value);
542   if (0 == strcasecmp ("arm", service_name))
543   {
544     slen = strlen ("arm") + 1;
545     sctx = GNUNET_malloc (sizeof (struct RequestContext) + slen);
546     sctx->h = h;
547     sctx->callback = cb;
548     sctx->cls = cb_cls;
549     sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
550     memcpy (&sctx[1], service_name, slen);
551     GNUNET_CLIENT_service_test ("arm", h->cfg, timeout, &arm_service_report,
552                                 sctx);
553     return;
554   }
555   if (h->client == NULL)
556   {
557     client = GNUNET_CLIENT_connect ("arm", h->cfg);
558     if (client == NULL)
559     {
560       LOG (GNUNET_ERROR_TYPE_DEBUG,
561            "arm_api, GNUNET_CLIENT_connect returned NULL\n");
562       cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR);
563       return;
564     }
565     LOG (GNUNET_ERROR_TYPE_DEBUG,
566          "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
567     h->client = client;
568   }
569   LOG (GNUNET_ERROR_TYPE_DEBUG, "arm_api, h->client non-NULL\n");
570   change_service (h, service_name, timeout, cb, cb_cls,
571                   GNUNET_MESSAGE_TYPE_ARM_START);
572 }
573
574
575 /**
576  * Callback from the arm stop service call, indicates that the arm service
577  * is well and truly dead, won't die, or an error occurred.
578  *
579  * @param cls closure for the callback
580  * @param reason reason for callback
581  */
582 static void
583 arm_shutdown_callback (void *cls, enum GNUNET_ARM_ProcessStatus reason)
584 {
585   struct ARM_ShutdownContext *arm_shutdown_ctx = cls;
586
587   if (arm_shutdown_ctx->cb != NULL)
588     arm_shutdown_ctx->cb (arm_shutdown_ctx->cb_cls, reason);
589
590   GNUNET_free (arm_shutdown_ctx);
591 }
592
593
594 /**
595  * Stop a service.
596  *
597  * @param h handle to ARM
598  * @param service_name name of the service
599  * @param timeout how long to wait before failing for good
600  * @param cb callback to invoke when service is ready
601  * @param cb_cls closure for callback
602  */
603 void
604 GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
605                          const char *service_name,
606                          struct GNUNET_TIME_Relative timeout,
607                          GNUNET_ARM_Callback cb, void *cb_cls)
608 {
609   struct ARM_ShutdownContext *arm_shutdown_ctx;
610   struct GNUNET_CLIENT_Connection *client;
611
612   LOG (GNUNET_ERROR_TYPE_INFO, _("Stopping service `%s' within %llu ms\n"),
613        service_name, (unsigned long long) timeout.rel_value);
614   if (h->client == NULL)
615   {
616     client = GNUNET_CLIENT_connect ("arm", h->cfg);
617     if (client == NULL)
618     {
619       cb (cb_cls, GNUNET_SYSERR);
620       return;
621     }
622     h->client = client;
623   }
624   if (0 == strcasecmp ("arm", service_name))
625   {
626     arm_shutdown_ctx = GNUNET_malloc (sizeof (struct ARM_ShutdownContext));
627     arm_shutdown_ctx->cb = cb;
628     arm_shutdown_ctx->cb_cls = cb_cls;
629     arm_service_shutdown (h->client, timeout, &arm_shutdown_callback,
630                           arm_shutdown_ctx);
631     h->client = NULL;
632     return;
633   }
634   change_service (h, service_name, timeout, cb, cb_cls,
635                   GNUNET_MESSAGE_TYPE_ARM_STOP);
636 }
637
638
639 /**
640  * Internal state for a list request with ARM.
641  */
642 struct ListRequestContext
643 {
644
645   /**
646    * Pointer to our handle with ARM.
647    */
648   struct GNUNET_ARM_Handle *h;
649
650   /**
651    * Function to call with a status code for the requested operation.
652    */
653   GNUNET_ARM_List_Callback callback;
654
655   /**
656    * Closure for "callback".
657    */
658   void *cls;
659
660   /**
661    * Timeout for the operation.
662    */
663   struct GNUNET_TIME_Absolute timeout;
664 };
665
666
667 /**
668  * Process a response from ARM for the list request.
669  *
670  * @param cls the list request context
671  * @param msg the response
672  */
673 static void
674 handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
675 {
676   struct ListRequestContext *sc = cls;
677   const struct GNUNET_ARM_ListResultMessage *res;
678   const char *pos;
679   uint16_t size_check;
680   uint16_t rcount;
681   uint16_t msize;
682   
683   if (NULL == msg)
684   {
685     LOG (GNUNET_ERROR_TYPE_WARNING,
686          "Error receiving response to LIST request from ARM\n");
687     GNUNET_CLIENT_disconnect (sc->h->client);
688     sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
689     GNUNET_assert (NULL != sc->h->client);
690     if (sc->callback != NULL)
691       sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
692     GNUNET_free (sc);
693     return;
694   }
695    
696   if (NULL == sc->callback) 
697   {
698     GNUNET_break (0);
699     GNUNET_free (sc);
700     return;
701   }  
702   msize = ntohs (msg->size);
703   if ( (msize < sizeof ( struct GNUNET_ARM_ListResultMessage)) ||
704        (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT) )
705   {
706     GNUNET_break (0);
707     sc->callback (sc->cls, GNUNET_NO, 0, NULL);
708     GNUNET_free (sc);
709     return;
710   }
711   size_check = 0;
712   res = (const struct GNUNET_ARM_ListResultMessage *) msg;
713   rcount = ntohs (res->count);
714   {
715     const char *list[rcount];
716     unsigned int i;
717     
718     pos = (const char *)&res[1];   
719     for (i=0; i<rcount; i++)
720     {
721       const char *end = memchr (pos, 0, msize - size_check);
722       if (NULL == end)
723       {
724         GNUNET_break (0);
725         sc->callback (sc->cls, GNUNET_NO, 0, NULL);
726         GNUNET_free (sc);
727         return;
728       }
729       list[i] = pos;
730       size_check += (end - pos) + 1;
731       pos = end + 1;
732     }
733     sc->callback (sc->cls, GNUNET_YES, rcount, list);
734   }
735   GNUNET_free (sc);
736 }
737
738
739 /**
740  * List all running services.
741  * 
742  * @param h handle to ARM
743  * @param timeout how long to wait before failing for good
744  * @param cb callback to invoke when service is ready
745  * @param cb_cls closure for callback
746  */
747 void
748 GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
749                                   struct GNUNET_TIME_Relative timeout,
750                                   GNUNET_ARM_List_Callback cb, void *cb_cls)
751 {
752   struct ListRequestContext *sctx;
753   struct GNUNET_MessageHeader msg;
754   struct GNUNET_CLIENT_Connection *client;
755   
756   if (h->client == NULL)
757   {
758     client = GNUNET_CLIENT_connect ("arm", h->cfg);
759     if (client == NULL)
760     {
761       LOG (GNUNET_ERROR_TYPE_DEBUG,
762            "arm_api, GNUNET_CLIENT_connect returned NULL\n");
763       cb (cb_cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
764       return;
765     }
766     LOG (GNUNET_ERROR_TYPE_DEBUG,
767          "arm_api, GNUNET_CLIENT_connect returned non-NULL\n");
768     h->client = client;
769   }
770   
771   sctx = GNUNET_malloc (sizeof (struct RequestContext));
772   sctx->h = h;
773   sctx->callback = cb;
774   sctx->cls = cb_cls;
775   sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
776   msg.size = htons (sizeof (struct GNUNET_MessageHeader));
777   msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
778   
779   LOG (GNUNET_ERROR_TYPE_DEBUG, 
780        "Requesting LIST from ARM service with timeout: %llu ms\n", 
781        (unsigned long long)timeout.rel_value);
782   
783   if (GNUNET_OK !=
784       GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, 
785                                                &msg,
786                                                GNUNET_TIME_absolute_get_remaining
787                                                (sctx->timeout), 
788                                                GNUNET_YES,
789                                                &handle_list_response, 
790                                                sctx))
791   {
792     LOG (GNUNET_ERROR_TYPE_WARNING, 
793          "Error while trying to transmit request to list services to ARM\n");
794     if (cb != NULL)
795       cb (cb_cls, GNUNET_SYSERR, 0, NULL);
796     GNUNET_free (sctx);
797     return;
798   }
799 }
800
801 /* end of arm_api.c */