aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-03-01 13:59:23 +0000
committerChristian Grothoff <christian@grothoff.org>2012-03-01 13:59:23 +0000
commit370ec6d2cd26fa31f134da67e2a6a9e0d8902d6f (patch)
tree4158092c13add351264bfd3f000d9f91b03c1630
parentbd048eb574eca410e76d81ee6dd1b214b6966e92 (diff)
downloadgnunet-370ec6d2cd26fa31f134da67e2a6a9e0d8902d6f.tar.gz
gnunet-370ec6d2cd26fa31f134da67e2a6a9e0d8902d6f.zip
-minor fixes to arm service list API (#2141)
-rw-r--r--doc/man/gnunet-arm.13
-rw-r--r--src/arm/arm_api.c206
-rw-r--r--src/arm/gnunet-arm.c22
-rw-r--r--src/include/gnunet_arm_service.h4
4 files changed, 123 insertions, 112 deletions
diff --git a/doc/man/gnunet-arm.1 b/doc/man/gnunet-arm.1
index 1bd44677d..86a748e2f 100644
--- a/doc/man/gnunet-arm.1
+++ b/doc/man/gnunet-arm.1
@@ -34,6 +34,9 @@ Stop the specified SERVICE if it is running. While this will kill the service r
34.IP "\-s, \-\-start" 34.IP "\-s, \-\-start"
35Start all GNUnet default services on this system (and also ARM). Naturally, if a service is demanded by a default service, it will then also be started. Running "gnunet-arm -s" is the usual way to start a GNUnet peer. 35Start all GNUnet default services on this system (and also ARM). Naturally, if a service is demanded by a default service, it will then also be started. Running "gnunet-arm -s" is the usual way to start a GNUnet peer.
36.B 36.B
37.IP "\-I, \-\-info"
38List all running services.
39.B
37.IP "\-v, \-\-version" 40.IP "\-v, \-\-version"
38Print GNUnet version number. 41Print GNUnet version number.
39 42
diff --git a/src/arm/arm_api.c b/src/arm/arm_api.c
index d0babf0ae..16de88929 100644
--- a/src/arm/arm_api.c
+++ b/src/arm/arm_api.c
@@ -486,94 +486,6 @@ handle_response (void *cls, const struct GNUNET_MessageHeader *msg)
486 GNUNET_free (sc); 486 GNUNET_free (sc);
487} 487}
488 488
489/**
490 * Internal state for a list request with ARM.
491 */
492struct ListRequestContext
493{
494
495 /**
496 * Pointer to our handle with ARM.
497 */
498 struct GNUNET_ARM_Handle *h;
499
500 /**
501 * Function to call with a status code for the requested operation.
502 */
503 GNUNET_ARM_List_Callback callback;
504
505 /**
506 * Closure for "callback".
507 */
508 void *cls;
509
510 /**
511 * Timeout for the operation.
512 */
513 struct GNUNET_TIME_Absolute timeout;
514};
515
516/**
517 * Process a response from ARM for the list request.
518 *
519 * @param cls the list request context
520 * @param msg the response
521 */
522static void
523handle_list_response (void *cls, const struct GNUNET_MessageHeader *msg)
524{
525 struct ListRequestContext *sc = cls;
526 const struct GNUNET_ARM_ListResultMessage *res;
527 int success;
528
529 if (msg == NULL)
530 {
531 LOG (GNUNET_ERROR_TYPE_WARNING,
532 "Error receiving response to LIST request from ARM\n");
533 GNUNET_CLIENT_disconnect (sc->h->client, GNUNET_NO);
534 sc->h->client = GNUNET_CLIENT_connect ("arm", sc->h->cfg);
535 GNUNET_assert (NULL != sc->h->client);
536 if (sc->callback != NULL)
537 sc->callback (sc->cls, GNUNET_ARM_PROCESS_COMMUNICATION_ERROR, 0, NULL);
538 GNUNET_free (sc);
539 return;
540 }
541
542 if (sc->callback != NULL)
543 {
544 char **list;
545 const char *pos;
546 uint16_t size_check;
547
548 size_check = 0;
549 res = (const struct GNUNET_ARM_ListResultMessage *) msg;
550 success = (ntohs (res->header.type)
551 == GNUNET_MESSAGE_TYPE_ARM_LIST_RESULT ?
552 GNUNET_YES : GNUNET_NO);
553 list = GNUNET_malloc (ntohs (res->count) * sizeof (char *));
554 pos = (const char *)&res[1];
555
556 int i;
557 for (i=0; i<ntohs (res->count); i++)
558 {
559 list[i] = GNUNET_malloc (strlen (pos) + 1);
560 memcpy (list[i], pos, strlen (pos) + 1);
561 pos += strlen (pos) + 1;
562 size_check += strlen (pos) +1;
563
564 if (size_check > ntohs (res->header.size))
565 {
566 GNUNET_free (list);
567 GNUNET_free (sc);
568 sc->callback (sc->cls, GNUNET_NO, 0, NULL);
569 return;
570 }
571 }
572
573 sc->callback (sc->cls, success, ntohs (res->count), (const char**)list);
574 }
575 GNUNET_free (sc);
576}
577 489
578/** 490/**
579 * Start or stop a service. 491 * Start or stop a service.
@@ -764,6 +676,107 @@ GNUNET_ARM_stop_service (struct GNUNET_ARM_Handle *h,
764 GNUNET_MESSAGE_TYPE_ARM_STOP); 676 GNUNET_MESSAGE_TYPE_ARM_STOP);
765} 677}
766 678
679
680/**
681 * Internal state for a list request with ARM.
682 */
683struct 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 */
714static void
715handle_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
767/** 780/**
768 * List all running services. 781 * List all running services.
769 * 782 *
@@ -778,7 +791,7 @@ GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
778 GNUNET_ARM_List_Callback cb, void *cb_cls) 791 GNUNET_ARM_List_Callback cb, void *cb_cls)
779{ 792{
780 struct ListRequestContext *sctx; 793 struct ListRequestContext *sctx;
781 struct GNUNET_MessageHeader *msg; 794 struct GNUNET_MessageHeader msg;
782 struct GNUNET_CLIENT_Connection *client; 795 struct GNUNET_CLIENT_Connection *client;
783 796
784 if (h->client == NULL) 797 if (h->client == NULL)
@@ -801,17 +814,16 @@ GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
801 sctx->callback = cb; 814 sctx->callback = cb;
802 sctx->cls = cb_cls; 815 sctx->cls = cb_cls;
803 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout); 816 sctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
804 msg = GNUNET_malloc (sizeof (struct GNUNET_MessageHeader)); 817 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
805 msg->size = htons (sizeof (struct GNUNET_MessageHeader)); 818 msg.type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
806 msg->type = htons (GNUNET_MESSAGE_TYPE_ARM_LIST);
807 819
808 LOG(GNUNET_ERROR_TYPE_DEBUG, 820 LOG (GNUNET_ERROR_TYPE_DEBUG,
809 "Requesting LIST from ARM service with timeout: %llu ms\n", 821 "Requesting LIST from ARM service with timeout: %llu ms\n",
810 (unsigned long long)timeout.rel_value); 822 (unsigned long long)timeout.rel_value);
811 823
812 if (GNUNET_OK != 824 if (GNUNET_OK !=
813 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client, 825 GNUNET_CLIENT_transmit_and_get_response (sctx->h->client,
814 msg, 826 &msg,
815 GNUNET_TIME_absolute_get_remaining 827 GNUNET_TIME_absolute_get_remaining
816 (sctx->timeout), 828 (sctx->timeout),
817 GNUNET_YES, 829 GNUNET_YES,
@@ -823,10 +835,8 @@ GNUNET_ARM_list_running_services (struct GNUNET_ARM_Handle *h,
823 if (cb != NULL) 835 if (cb != NULL)
824 cb (cb_cls, GNUNET_SYSERR, 0, NULL); 836 cb (cb_cls, GNUNET_SYSERR, 0, NULL);
825 GNUNET_free (sctx); 837 GNUNET_free (sctx);
826 GNUNET_free (msg);
827 return; 838 return;
828 } 839 }
829 GNUNET_free (msg);
830} 840}
831 841
832/* end of arm_api.c */ 842/* end of arm_api.c */
diff --git a/src/arm/gnunet-arm.c b/src/arm/gnunet-arm.c
index b3ab2dc0e..f236a65de 100644
--- a/src/arm/gnunet-arm.c
+++ b/src/arm/gnunet-arm.c
@@ -213,20 +213,18 @@ confirm_cb (void *cls,
213 * @param list copy of the list of running services 213 * @param list copy of the list of running services
214 */ 214 */
215static void 215static void
216list_cb (void *cls, int result, uint16_t count, const char **list) 216list_cb (void *cls, int result, unsigned int count, const char *const*list)
217{ 217{
218 if (result == GNUNET_YES && list != NULL) 218 unsigned int i;
219
220 if ( (result != GNUNET_YES) || (NULL == list) )
219 { 221 {
220 FPRINTF (stdout, "%s", _("Running services:\n-----------------\n"));
221 int i;
222 for (i=0; i<count; i++)
223 {
224 FPRINTF (stdout, "%s\n", list[i]);
225 }
226 GNUNET_free (list);
227 } else {
228 FPRINTF (stderr, "%s", _("Error communicating with ARM. ARM not running?\n")); 222 FPRINTF (stderr, "%s", _("Error communicating with ARM. ARM not running?\n"));
223 return;
229 } 224 }
225 FPRINTF (stdout, "%s", _("Running services:\n"));
226 for (i=0; i<count; i++)
227 FPRINTF (stdout, "%s\n", list[i]);
230} 228}
231 229
232/** 230/**
@@ -353,7 +351,7 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
353 start = 1; 351 start = 1;
354 restart = 0; 352 restart = 0;
355 h = GNUNET_ARM_connect (cfg, NULL); 353 h = GNUNET_ARM_connect (cfg, NULL);
356 if (h == NULL) 354 if (NULL == h)
357 { 355 {
358 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 356 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
359 _("Fatal error initializing ARM API.\n")); 357 _("Fatal error initializing ARM API.\n"));
@@ -368,7 +366,7 @@ cps_loop (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Going to list all running services controlled by ARM.\n"); 367 "Going to list all running services controlled by ARM.\n");
370 368
371 if (h == NULL) 369 if (NULL == h)
372 { 370 {
373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
374 _("Fatal error initializing ARM API.\n")); 372 _("Fatal error initializing ARM API.\n"));
diff --git a/src/include/gnunet_arm_service.h b/src/include/gnunet_arm_service.h
index 42d42a0cd..116bfc5b4 100644
--- a/src/include/gnunet_arm_service.h
+++ b/src/include/gnunet_arm_service.h
@@ -121,8 +121,8 @@ typedef void (*GNUNET_ARM_Callback) (void *cls,
121 */ 121 */
122typedef void (*GNUNET_ARM_List_Callback) (void *cls, 122typedef void (*GNUNET_ARM_List_Callback) (void *cls,
123 int result, 123 int result,
124 uint16_t count, 124 unsigned int count,
125 const char **list); 125 const char *const *list);
126 126
127 127
128/** 128/**