gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r29139 - gnunet/src/scalarproduct


From: gnunet
Subject: [GNUnet-SVN] r29139 - gnunet/src/scalarproduct
Date: Mon, 9 Sep 2013 18:15:32 +0200

Author: cfuchs
Date: 2013-09-09 18:15:32 +0200 (Mon, 09 Sep 2013)
New Revision: 29139

Modified:
   gnunet/src/scalarproduct/gnunet-scalarproduct.c
   gnunet/src/scalarproduct/gnunet-service-scalarproduct.c
   gnunet/src/scalarproduct/scalarproduct_api.c
Log:
sanitized the scalarproduct client
fixed a typo in the SP API
changed SP-service to always use GCRYMPI_FMT_STD when sending the result back 
to client. Even though gcry currently can not print negative numbers, we can 
just remove the if-else block above it once that's supported



Modified: gnunet/src/scalarproduct/gnunet-scalarproduct.c
===================================================================
--- gnunet/src/scalarproduct/gnunet-scalarproduct.c     2013-09-09 15:51:14 UTC 
(rev 29138)
+++ gnunet/src/scalarproduct/gnunet-scalarproduct.c     2013-09-09 16:15:32 UTC 
(rev 29139)
@@ -34,6 +34,7 @@
 #include "scalarproduct.h"
 
 #define LOG(kind,...) GNUNET_log_from (kind, 
"gnunet-scalarproduct",__VA_ARGS__)
+#define INPUTSTRINGLENGTH       1024
 
 struct ScalarProductCallbackClosure
 {
@@ -71,7 +72,7 @@
 /**
  * Global return value
  */
-static int ret;
+static int ret = -1;
 
 
 /**
@@ -85,7 +86,6 @@
                     enum GNUNET_SCALARPRODUCT_ResponseStatus status)
 {
   struct ScalarProductCallbackClosure * closure = cls;
-  ret = -1;
 
   switch (status)
   {
@@ -104,9 +104,6 @@
   default:
     LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s failed: return code %d\n", 
GNUNET_h2s (&closure->key), status);
   }
-
-  GNUNET_SCALARPRODUCT_disconnect ();
-  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -129,16 +126,17 @@
   struct ScalarProductCallbackClosure * closure = cls;
   unsigned char * buf;
   gcry_error_t rc;
-  ret = -1;
 
   switch (status)
   {
   case GNUNET_SCALARPRODUCT_Status_Success:
-
     if (0 == (rc = gcry_mpi_aprint (GCRYMPI_FMT_HEX, &buf, NULL, result)))
-      printf("%s", buf);
+    {
+      ret = 0;
+      printf ("%s", buf);
+    }
     else
-      LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
+      LOG_GCRY (GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_aprint", rc);
     break;
   case GNUNET_SCALARPRODUCT_Status_InvalidResponse:
     LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: invalid 
response received\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer));
@@ -151,8 +149,6 @@
   default:
     LOG (GNUNET_ERROR_TYPE_ERROR, "Session %s with peer %s failed: return code 
%d\n", GNUNET_h2s (&closure->key), GNUNET_i2s (&closure->peer), status);
   }
-  GNUNET_SCALARPRODUCT_disconnect ();
-  GNUNET_SCHEDULER_shutdown ();
 }
 
 
@@ -174,156 +170,161 @@
   char * end;
   int32_t element;
   int i;
-  ret = -1;
   int32_t * elements;
   unsigned char * mask;
   unsigned short mask_bytes;
-  unsigned short element_count;
+  unsigned short element_count = 0;
   struct ScalarProductCallbackClosure * closure;
 
   if (NULL == input_elements)
   {
-    FPRINTF (stderr, "%s", _ ("You must specify at least one message ID to 
check!\n"));
+    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("You must specify at least one message ID 
to check!\n"));
     return;
   }
 
   if (NULL == input_key)
   {
-    FPRINTF (stderr, "%s", _ ("This program needs a session identifier for 
comparing vectors.\n"));
+    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("This program needs a session identifier 
for comparing vectors.\n"));
     return;
   }
 
   if (1 > strnlen (input_key, sizeof (struct GNUNET_HashCode)))
   {
-    FPRINTF (stderr, _ ("Please give a session key for --input_key!\n"));
+    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Please give a session key for 
--input_key!\n"));
     return;
   }
-  closure = GNUNET_new(struct ScalarProductCallbackClosure);
+  closure = GNUNET_new (struct ScalarProductCallbackClosure);
   GNUNET_CRYPTO_hash (input_key, strlen (input_key), &closure->key);
 
   if (input_peer_id && GNUNET_OK != GNUNET_CRYPTO_hash_from_string 
(input_peer_id,
                                                                     (struct 
GNUNET_HashCode *) &closure->peer))
   {
-    FPRINTF (stderr, _ ("Tried to set initiator mode, as peer ID was given. "
-                        "However, `%s' is not a valid peer identifier.\n"),
-             input_peer_id);
+    LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Tried to set initiator mode, as peer ID 
was given. "
+                                     "However, `%s' is not a valid peer 
identifier.\n"),
+         input_peer_id);
     return;
   }
 
-  int exit_loop = 0;
-  /* Read input_elements_peer1, and put in elements_peer1 array */
+  /* Count input_elements_peer1, and put in elements_peer1 array */
   do
   {
-    unsigned int mcount = element_count;
-    //ignore empty rows of ,,,,,,
-    while (*begin == ',')
-      begin++;
     // get the length of the current element and replace , with null
     for (end = begin; *end && *end != ','; end++);
 
-    if (*end == '\0')
-      exit_loop = 1;
-
-    if (*end == ',')
-      *end = '\0';
-
-    if (1 != sscanf (begin, "%" SCNd32, &element))
+    if (1 == sscanf (begin, "%" SCNd32",", &element))
     {
-      FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
+      //element in the middle
+      element_count++;
+      begin = end + 1;
+    }
+    else if (*begin == 0)
+    {
+      break;
+    }
+    else
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to 
int32_t.\n"), begin);
       return;
     }
+  }
+  while (1);
+  if (0 == element_count)
+  {
 
-    GNUNET_array_append (elements, mcount, element);
-    element_count++;
+    return;
+  }
 
-    begin = ++end;
+  begin = input_elements;
+  elements = GNUNET_malloc (sizeof (int32_t) * element_count);
+  /* Read input_elements_peer1, and put in elements_peer1 array */
+  do
+  {
+    // get the length of the current element and replace , with null
+    for (end = begin; *end && *end != ','; end++);
+
+    if (1 == sscanf (begin, "%" SCNd32",", &element))
+    {
+      //element in the middle
+      element_count++;
+      begin = end + 1;
+    }
+    else if (*begin == 0)
+    {
+      break;
+    }
   }
-  while (!exit_loop);
+  while (1);
 
-  GNUNET_assert (elements != NULL);
-  GNUNET_assert (element_count > 1);
   mask_bytes = element_count / 8 + (element_count % 8 ? 1 : 0);
   mask = GNUNET_malloc ((element_count / 8) + 2);
 
   /* Read input_mask_peer1 and read in mask_peer1 array */
-  if (NULL != input_mask)
+  if ((NULL != input_peer_id) && (NULL != input_mask))
   {
     begin = input_mask;
     unsigned short mask_count = 0;
-    int exit_loop = 0;
 
     do
     {
-      //ignore empty rows of ,,,,,,
-      while (* begin == ',')
-        begin++;
       // get the length of the current element and replace , with null
-      // gnunet_ascii-armor uses base32, thus we can use , as separator!
       for (end = begin; *end && *end != ','; end++);
 
-      if (*end == '\0')
-        exit_loop = 1;
-
-      if (*end == ',')
-        *end = '\0';
-
-      if (1 != sscanf (begin, "%" SCNd32, &element))
+      if (1 == sscanf (begin, "%" SCNd32 ",", &element))
       {
-        FPRINTF (stderr, _ ("Could not convert `%s' to int32_t.\n"), begin);
+        //element in the middle
+        begin = end + 1;
+      }
+      else if (*begin == 0)
+      {
+        break;
+      }
+      else
+      {
+        LOG (GNUNET_ERROR_TYPE_ERROR, _ ("Could not convert `%s' to 
int32_t.\n"), begin);
         return;
       }
 
-      GNUNET_assert (mask_count <= element_count);
-
       if (element)
         mask[mask_count / 8] = mask[mask_count / 8] | 1 << (mask_count % 8);
-
       mask_count++;
-      begin = ++end;
     }
-    while (!exit_loop);
-    // +1 to see if we would have more data, which would indicate 
malformed/superficial input
-    GNUNET_assert (mask_count == element_count);
+    while (mask_count < element_count);
   }
-  else if (input_peer_id)
-  {
+  else if (NULL != input_peer_id)
     for (i = 0; i <= mask_bytes; i++)
       mask[i] = UCHAR_MAX; // all 1's
-  }
 
-
-  if (input_peer_id && !GNUNET_SCALARPRODUCT_request (cfg,
-                                                      &closure->key,
-                                                      &closure->peer,
-                                                      elements, element_count,
-                                                      mask, mask_bytes,
-                                                      &requester_callback,
-                                                      (void *) &closure))
+  if (input_peer_id && (NULL == GNUNET_SCALARPRODUCT_request (cfg,
+                                                              &closure->key,
+                                                              &closure->peer,
+                                                              elements, 
element_count,
+                                                              mask, mask_bytes,
+                                                              
&requester_callback,
+                                                              (void *) 
&closure)))
     return;
 
-
-  if (!input_peer_id && !GNUNET_SCALARPRODUCT_response (cfg,
-                                                        &closure->key,
-                                                        elements, 
element_count,
-                                                        &responder_callback,
-                                                        (void *) &closure))
+  if ((NULL == input_peer_id) && (NULL == GNUNET_SCALARPRODUCT_response (cfg,
+                                                                         
&closure->key,
+                                                                         
elements, element_count,
+                                                                         
&responder_callback,
+                                                                         (void 
*) &closure)))
     return;
 
   ret = 0;
 }
 
 
-      /**
+/**
  * The main function to the scalarproduct client.
  *
  * @param argc number of arguments from the command line
  * @param argv command line arguments
  * @return 0 ok, 1 on error
  */
-      int
+int
 main (int argc, char *const *argv)
 {
-      static const struct GNUNET_GETOPT_CommandLineOption options[] = {
+  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
     {'e', "elements", "\"val1,val2,...,valn\"",
       gettext_noop ("A comma separated list of elements to compare as vector 
with our remote peer."),
       1, &GNUNET_GETOPT_set_string, &input_elements},
@@ -336,14 +337,14 @@
     {'k', "key", "TRANSACTION_ID",
       gettext_noop ("Transaction ID shared with peer."),
       1, &GNUNET_GETOPT_set_string, &input_key},
-      GNUNET_GETOPT_OPTION_END
+    GNUNET_GETOPT_OPTION_END
   };
 
-      return (GNUNET_OK ==
-              GNUNET_PROGRAM_run (argc,
-                                  argv,
-                                  "gnunet-scalarproduct",
-                                  gettext_noop ("Calculate the Vectorproduct 
with a GNUnet peer."),
-                                  options, &run, NULL)) ? ret : 1;
+  return (GNUNET_OK ==
+          GNUNET_PROGRAM_run (argc,
+                              argv,
+                              "gnunet-scalarproduct",
+                              gettext_noop ("Calculate the Vectorproduct with 
a GNUnet peer."),
+                              options, &run, NULL)) ? ret : 1;
 }
 

Modified: gnunet/src/scalarproduct/gnunet-service-scalarproduct.c
===================================================================
--- gnunet/src/scalarproduct/gnunet-service-scalarproduct.c     2013-09-09 
15:51:14 UTC (rev 29138)
+++ gnunet/src/scalarproduct/gnunet-service-scalarproduct.c     2013-09-09 
16:15:32 UTC (rev 29139)
@@ -504,12 +504,14 @@
       written = size;
     }
 
-  if (GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT == 
ntohs(session->msg->type)){
-    session->state = FINALIZED;
-    session->client_transmit_handle = NULL;
+  switch (ntohs(session->msg->type)){
+    case GNUNET_MESSAGE_TYPE_SCALARPRODUCT_SERVICE_TO_CLIENT:
+      session->state = FINALIZED;
+      session->client_transmit_handle = NULL;
+      break;
+    default:
+      session->service_transmit_handle = NULL;
   }
-  else
-    session->service_transmit_handle = NULL;
     
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 
              "Sent a message of type %hu.\n", 
@@ -1642,6 +1644,7 @@
       
       sign = gcry_mpi_cmp_ui(session->product, 0);
       // libgcrypt can not handle a print of a negative number
+      // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it 
yet. */
       if (0 > sign){
           gcry_mpi_sub(value, value, session->product);
       }
@@ -1652,23 +1655,20 @@
       else
         range = 0;
       
+      gcry_mpi_release (session->product);
+      session->product = NULL;
+      
       // get representation as string
-      // unfortunately libgcrypt is too stupid to implement print-support in
-      // signed GCRYMPI_FMT_STD format, and simply asserts in that case.
-      // here is the associated sourcecode:
-      // if (a->sign) return gcry_error (GPG_ERR_INTERNAL); /* Can't handle it 
yet. */
       if (range
-          && (0 != (rc =  gcry_mpi_aprint (GCRYMPI_FMT_USG,
+          && (0 != (rc =  gcry_mpi_aprint (GCRYMPI_FMT_STD,
                                              &product_exported,
                                              &product_length,
-                                             session->product)))){
+                                             value)))){
         LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
         product_length = 0;
         range = -1; // signal error with product-length = 0 and range = -1
       }
-      
-      gcry_mpi_release (session->product);
-      session->product = NULL;
+      gcry_mpi_release (value);
     }
 
   msg_length = sizeof (struct GNUNET_SCALARPRODUCT_client_response) + 
product_length;

Modified: gnunet/src/scalarproduct/scalarproduct_api.c
===================================================================
--- gnunet/src/scalarproduct/scalarproduct_api.c        2013-09-09 15:51:14 UTC 
(rev 29138)
+++ gnunet/src/scalarproduct/scalarproduct_api.c        2013-09-09 16:15:32 UTC 
(rev 29139)
@@ -184,7 +184,7 @@
           gcry_mpi_t num;
           size_t read = 0;
 
-          if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_USG, &msg[1], 
product_len, &read)))
+          if (0 != (rc = gcry_mpi_scan (&num, GCRYMPI_FMT_STD, &msg[1], 
product_len, &read)))
             {
               LOG_GCRY(GNUNET_ERROR_TYPE_ERROR, "gcry_mpi_scan", rc);
               gcry_mpi_release (result);
@@ -338,7 +338,7 @@
       return NULL;
     }
   h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
-  if (!h->th)
+  if (!h->stats)
     {
       LOG (GNUNET_ERROR_TYPE_ERROR,
            _ ("Failed to send a message to the statistics service\n"));
@@ -431,7 +431,7 @@
       return NULL;
     }
   h->stats = GNUNET_STATISTICS_create ("scalarproduct-api", cfg);
-  if (!h->th)
+  if (!h->stats)
     {
       LOG (GNUNET_ERROR_TYPE_ERROR,
            _ ("Failed to send a message to the statistics service\n"));




reply via email to

[Prev in Thread] Current Thread [Next in Thread]