-fixes
[oweals/gnunet.git] / src / gns / gnunet-gns-fcfsd.c
index 5d9fbd27f820f6a6e6ccf738b158e76337cb0e61..8d6a58302b4b4d752d0d55faee80cbacf3ef27c8 100644 (file)
@@ -42,7 +42,7 @@
 /**
  * Front page. (/)
  */
-#define MAIN_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>GNUnet FCFS Authority Name Registration Service</title></head><body><form action=\"/S\" method=\"post\">What is your desired domain name? <input type=\"text\" name=\"domain\" /> <p> What is your public key? <input type=\"text\" name=\"pkey\" /> <input type=\"submit\" value=\"Next\" /></body></html>"
+#define MAIN_PAGE "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"><html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\"><html><head><title>GNUnet FCFS Authority Name Registration Service</title></head><body><form action=\"S\" method=\"post\">What is your desired domain name? <input type=\"text\" name=\"domain\" /> <p> What is your public key? <input type=\"text\" name=\"pkey\" /> <input type=\"submit\" value=\"Next\" /></body></html>"
 
 /**
  * Second page (/S)
@@ -133,7 +133,7 @@ struct Request
   /**
    * Public key submitted via form.
    */
-  char public_key[1024];
+  char public_key[64];
 
 };
 
@@ -156,7 +156,7 @@ static struct GNUNET_NAMESTORE_Handle *ns;
 /**
  * Hash of the public key of the fcfsd zone.
  */
-static GNUNET_HashCode fcfsd_zone;
+static struct GNUNET_CRYPTO_ShortHashCode fcfsd_zone;
 
 /**
  * Private key for the fcfsd zone.
@@ -216,7 +216,7 @@ fill_s_reply (const char *info,
                                              (void *) reply,
                                              MHD_RESPMEM_MUST_FREE);
   MHD_add_response_header (response,
-                          MHD_HTTP_HEADER_CONTENT_ENCODING,
+                          MHD_HTTP_HEADER_CONTENT_TYPE,
                           MIME_HTML);
   ret = MHD_queue_response (connection, 
                            MHD_HTTP_OK, 
@@ -342,7 +342,7 @@ put_continuation (void *cls,
 
 
 /**
- * Process a record that was stored in the namestore.
+ * Test if a name mapping was found, if so, refuse.  If not, initiate storing of the record.
  *
  * @param cls closure
  * @param zone_key public key of the zone
@@ -358,33 +358,32 @@ put_continuation (void *cls,
  *        because the user queried for a particular record type only)
  */
 static void 
-lookup_result_processor (void *cls,
-                        const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
-                        struct GNUNET_TIME_Absolute expire,                        
-                        const char *name,
-                        unsigned int rd_count,
-                        const struct GNUNET_NAMESTORE_RecordData *rd,
-                        const struct GNUNET_CRYPTO_RsaSignature *signature)
+zone_to_name_cb (void *cls,
+                const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
+                struct GNUNET_TIME_Absolute expire,                        
+                const char *name,
+                unsigned int rd_count,
+                const struct GNUNET_NAMESTORE_RecordData *rd,
+                const struct GNUNET_CRYPTO_RsaSignature *signature)
 {
   struct Request *request = cls;
   struct GNUNET_NAMESTORE_RecordData r;
-  GNUNET_HashCode pub;
+  struct GNUNET_CRYPTO_ShortHashCode pub;
   
-  GNUNET_assert (GNUNET_OK ==
-                GNUNET_CRYPTO_hash_from_string2 (request->public_key,
-                                                 strlen (request->public_key),
-                                                 &pub));
   request->qe = NULL;
-  if (0 != rd_count)
+  if (NULL != name)
   {
     GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-               _("Found %u existing records for domain `%s'\n"),
-               rd_count,
-               request->domain_name);
+               _("Found existing name `%s' for the given key\n"),
+               name);
     request->phase = RP_FAIL;
     run_httpd_now ();
     return;
   }
+  GNUNET_assert (GNUNET_OK ==
+                GNUNET_CRYPTO_short_hash_from_string2 (request->public_key,
+                                                       strlen (request->public_key),
+                                                       &pub));
   r.data = &pub;
   r.data_size = sizeof (pub);
   r.expiration = GNUNET_TIME_UNIT_FOREVER_ABS;
@@ -399,6 +398,59 @@ lookup_result_processor (void *cls,
 }
 
 
+/**
+ * Process a record that was stored in the namestore.  Used to check if
+ * the requested name already exists in the namestore.  If not,
+ * proceed to check if the requested key already exists.
+ *
+ * @param cls closure
+ * @param zone_key public key of the zone
+ * @param expire when does the corresponding block in the DHT expire (until
+ *               when should we never do a DHT lookup for the same name again)?; 
+ *               GNUNET_TIME_UNIT_ZERO_ABS if there are no records of any type in the namestore,
+ *               or the expiration time of the block in the namestore (even if there are zero
+ *               records matching the desired record type)
+ * @param name name that is being mapped (at most 255 characters long)
+ * @param rd_count number of entries in 'rd' array
+ * @param rd array of records with data to store
+ * @param signature signature of the record block, NULL if signature is unavailable (i.e. 
+ *        because the user queried for a particular record type only)
+ */
+static void 
+lookup_result_processor (void *cls,
+                        const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key,
+                        struct GNUNET_TIME_Absolute expire,                        
+                        const char *name,
+                        unsigned int rd_count,
+                        const struct GNUNET_NAMESTORE_RecordData *rd,
+                        const struct GNUNET_CRYPTO_RsaSignature *signature)
+{
+  struct Request *request = cls;
+  struct GNUNET_CRYPTO_ShortHashCode pub;
+  
+  request->qe = NULL;
+  GNUNET_assert (GNUNET_OK ==
+                GNUNET_CRYPTO_short_hash_from_string2 (request->public_key,
+                                                       strlen (request->public_key),
+                                                       &pub));
+  if (0 != rd_count)
+  {
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+               _("Found %u existing records for domain `%s'\n"),
+               rd_count,
+               request->domain_name);
+    request->phase = RP_FAIL;
+    run_httpd_now ();
+    return;
+  }
+  request->qe = GNUNET_NAMESTORE_zone_to_name (ns,
+                                              &fcfsd_zone,
+                                              &pub,
+                                              &zone_to_name_cb,
+                                              request);
+}
+
+
 /**
  * Main MHD callback for handling requests.
  *
@@ -435,7 +487,7 @@ create_response (void *cls,
   struct MHD_Response *response;
   struct Request *request;
   int ret;
-  GNUNET_HashCode pub;
+  struct GNUNET_CRYPTO_ShortHashCode pub;
 
   if ( (0 == strcmp (method, MHD_HTTP_METHOD_GET)) ||
        (0 == strcmp (method, MHD_HTTP_METHOD_HEAD)) )
@@ -481,9 +533,9 @@ create_response (void *cls,
        request->pp = NULL;
       }
       if (GNUNET_OK !=
-         GNUNET_CRYPTO_hash_from_string2 (request->public_key,
-                                          strlen (request->public_key),
-                                          &pub))
+         GNUNET_CRYPTO_short_hash_from_string2 (request->public_key,
+                                                strlen (request->public_key),
+                                                &pub))
       {
        /* parse error */
        return fill_s_reply ("Failed to parse given public key",
@@ -697,11 +749,7 @@ run (void *cls, char *const *args, const char *cfgfile,
   }
   GNUNET_CRYPTO_rsa_key_get_public (fcfs_zone_pkey,
                                    &pub);
-  GNUNET_CRYPTO_hash (&pub, sizeof (pub), &fcfsd_zone);
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-             _("Managing `%s' as FCFS zone on port %llu\n"),
-             GNUNET_h2s_full (&fcfsd_zone),
-             port);
+  GNUNET_CRYPTO_short_hash (&pub, sizeof (pub), &fcfsd_zone);
   ns = GNUNET_NAMESTORE_connect (cfg);
   if (NULL == ns)
     {