/**
 * Function scheduled to be run on the successful start of services
 * tries to look up the dns record for TEST_DOMAIN
 */
static void
commence_testing (void *cls, int32_t success, const char *emsg)
{
  char name[MAX_DNS_NAME_LENGTH];
  char* pos;
  struct GNUNET_CRYPTO_ShortHashAsciiEncoded hash_str;
  
  GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);

  gns_handle = GNUNET_GNS_connect(cfg);

  if (NULL == gns_handle)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to connect to GNS!\n");
  }

  pos = name;
  strcpy(pos, TEST_RECORD_NAME);
  pos += strlen(TEST_RECORD_NAME);
  strcpy(pos, ".");
  pos++;
  GNUNET_CRYPTO_short_hash_to_enc(&bob_hash, &hash_str);
  strcpy(pos, (char*)&hash_str);
  pos += strlen((char*)&hash_str);
  strcpy(pos, ".");
  pos++;
  strcpy(pos, GNUNET_GNS_TLD_ZKEY);

  GNUNET_GNS_lookup(gns_handle, name, GNUNET_GNS_RECORD_TYPE_A,
                    &on_lookup_result, NULL);
}
/**
 * Function scheduled to be run on the successful start of services
 * tries to look up the dns record for TEST_DOMAIN
 *
 * @param cls closure
 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
 *                GNUNET_NO if content was already there or not found
 *                GNUNET_YES (or other positive value) on success
 * @param emsg NULL on success, otherwise an error message
 */
static void
commence_testing (void *cls,
		  int32_t success,
		  const char *emsg)
{
  nsqe = NULL;
  if (NULL != emsg)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to store record in namestore: %s\n",
		emsg);
    end_badly_now ();
    return;
  }
  gns_handle = GNUNET_GNS_connect (cfg);
  if (NULL == gns_handle)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to connect to GNS!\n");
    end_badly_now ();
    return;
  }
  lr = GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_A,
			  GNUNET_YES,
			  NULL,
			  &on_lookup_result, TEST_DOMAIN);
}
static int
setup_alice (const struct GNUNET_CONFIGURATION_Handle * cfg)
{
  char* keyfile;
  struct GNUNET_CRYPTO_RsaPrivateKey *key;
  struct GNUNET_NAMESTORE_RecordData rd;

  cfg_handles[2] = GNUNET_CONFIGURATION_dup (cfg);
  GNUNET_assert (NULL != cfg);
  if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_filename (cfg, "gns",
                                                            "ZONEKEY",
                                                            &keyfile))
  {
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
    return GNUNET_SYSERR;
  }

  key = GNUNET_CRYPTO_rsa_key_create_from_file (keyfile);
  if (NULL == key)
  {

    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to get key from cfg\n");
    GNUNET_free (keyfile);
    return GNUNET_SYSERR;
  }

  nh[2] = GNUNET_NAMESTORE_connect (cfg_handles[2]);
  if (NULL == nh[2])
  {
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to namestore\n");
    GNUNET_CRYPTO_rsa_key_free (key);
    GNUNET_free (keyfile);
    return GNUNET_SYSERR;
  }

  rd.expiration_time = UINT64_MAX;
  rd.data_size = sizeof(struct GNUNET_CRYPTO_ShortHashCode);
  rd.data = &bob_hash;
  rd.record_type = GNUNET_GNS_RECORD_PKEY;
  rd.flags = GNUNET_NAMESTORE_RF_AUTHORITY;

  GNUNET_NAMESTORE_record_create (nh[2], key, "bob", &rd, &cont_ns, nh[2]);

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up alice gns\n");
  gh = GNUNET_GNS_connect (cfg_handles[2]);
  if (NULL == gh)
  {
    GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Failed to connect to gns\n");
    GNUNET_CRYPTO_rsa_key_free (key);
    GNUNET_free (keyfile);
    return GNUNET_SYSERR;
  }

  GNUNET_CRYPTO_rsa_key_free (key);
  GNUNET_free (keyfile);
  alice_is_setup = GNUNET_YES;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Setting up alice  done\n");
  return GNUNET_OK;
}
/**
 * Function scheduled to be run on the successful start of services
 * tries to look up the dns record for TEST_DOMAIN
 */
static void
commence_testing (void *cls, int32_t success, const char *emsg)
{
    GNUNET_NAMESTORE_disconnect(namestore_handle, GNUNET_YES);

    gns_handle = GNUNET_GNS_connect(cfg);

    if (NULL == gns_handle)
    {
        GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                    "Failed to connect to GNS!\n");
    }

    GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_MX,
                      &on_lookup_result, TEST_DOMAIN);
}
/**
 * Function scheduled to be run on the successful start of services
 * tries to look up the dns record for TEST_DOMAIN
 */
static void
commence_testing (void *cls, int32_t success, const char *emsg)
{
  gns_handle = GNUNET_GNS_connect(cfg);
  if (NULL == gns_handle)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to connect to GNS!\n");
    end_badly_now();
    return;
  }
  GNUNET_GNS_lookup(gns_handle, TEST_DOMAIN, GNUNET_GNS_RECORD_MX,
                    GNUNET_NO,
                    NULL,
                    &on_lookup_result, TEST_DOMAIN);
}
/**
 * Function scheduled to be run on the successful start of services
 * tries to shorten the name TEST_DOMAIN using gns
 */
static void
commence_testing (void *cls, int32_t success, const char *emsg)
{

  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
              "Connecting to gns\n");
  gns_handle = GNUNET_GNS_connect(cfg);
  if (NULL == gns_handle)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to connect to gns\n");
    end_badly_now();
    return;
  }

  GNUNET_GNS_get_authority(gns_handle, TEST_DOMAIN, &process_auth_result,
                     TEST_DOMAIN);
}
/**
 * Function scheduled to be run on the successful start of services
 * tries to look up the dns record for TEST_DOMAIN
 *
 * @param cls closure
 * @param success GNUNET_SYSERR on failure (including timeout/queue drop/failure to validate)
 *                GNUNET_NO if content was already there or not found
 *                GNUNET_YES (or other positive value) on success
 * @param emsg NULL on success, otherwise an error message
 */
static void
commence_testing (void *cls, int32_t success, const char *emsg)
{
  if (NULL != emsg)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to store record in namestore: %s\n",
		emsg);
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  gns_handle = GNUNET_GNS_connect(cfg);
  if (NULL == gns_handle)
  {
    GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                "Failed to connect to GNS!\n");
    GNUNET_SCHEDULER_shutdown ();
    return;
  }
  GNUNET_GNS_lookup (gns_handle, TEST_DOMAIN_PLUS, GNUNET_GNS_RECORD_A,
		     GNUNET_YES,
		     NULL,
		     &on_lookup_result_plus, TEST_DOMAIN_PLUS);
}