⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs.c

📁 GNUnet是一个安全的点对点网络框架
💻 C
📖 第 1 页 / 共 3 页
字号:
      ttl -= 2 * GNUNET_GAP_TTL_DECREMENT +
        GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK,
                           GNUNET_GAP_TTL_DECREMENT);
      if (ttl > 0)
        /* integer underflow => drop (should be very rare)! */
        return GNUNET_OK;
    }
  else
    {
      ttl -= 2 * GNUNET_GAP_TTL_DECREMENT +
        GNUNET_random_u32 (GNUNET_RANDOM_QUALITY_WEAK,
                           GNUNET_GAP_TTL_DECREMENT);
    }
  preference = (double) prio;
  if (preference < GNUNET_GAP_QUERY_BANDWIDTH_VALUE)
    preference = GNUNET_GAP_QUERY_BANDWIDTH_VALUE;
  coreAPI->p2p_connection_preference_increase (sender, preference);
  GNUNET_FS_GAP_execute_query (sender,
                               prio,
                               ntohl (req->priority),
                               policy,
                               ttl,
                               type,
                               query_count,
                               &req->queries[0],
                               ntohl (req->filter_mutator),
                               bloomfilter_size,
                               &req->queries[query_count + 1]);
  return GNUNET_OK;
}


/**
 * Use content (forward to whoever sent the query).
 * @param hostId the peer from where the content came,
 *     NULL for the local peer
 */
static int
handle_p2p_content (const GNUNET_PeerIdentity * sender,
                    const GNUNET_MessageHeader * pmsg)
{
  const P2P_gap_reply_MESSAGE *msg;
  const GNUNET_EC_DBlock *dblock;
  GNUNET_DatastoreValue *value;
  GNUNET_HashCode query;
  unsigned short size;
  unsigned int data_size;
  unsigned int prio;
  unsigned long long expiration;
  double preference;
  GNUNET_CronTime now;

  size = ntohs (pmsg->size);
  if (size < sizeof (P2P_gap_reply_MESSAGE))
    {
      GNUNET_GE_BREAK_OP (ectx, 0);
      return GNUNET_SYSERR;     /* invalid! */
    }
  msg = (const P2P_gap_reply_MESSAGE *) pmsg;
  data_size = size - sizeof (P2P_gap_reply_MESSAGE);
  dblock = (const GNUNET_EC_DBlock *) &msg[1];

  expiration = GNUNET_ntohll (msg->expiration);
  if ((expiration > GNUNET_GAP_MAX_MIGRATION_EXP_KSK) &&
      (ntohl (dblock->type) == GNUNET_ECRS_BLOCKTYPE_KEYWORD))
    return GNUNET_OK;           /* expired KSK block -- ignore! */
  if (GNUNET_OK !=
      GNUNET_EC_file_block_check_and_get_query (data_size,
                                                dblock, GNUNET_YES, &query))
    {
      GNUNET_GE_BREAK_OP (ectx, 0);
      return GNUNET_SYSERR;     /* invalid! */
    }
  if ((stats != NULL) && (sender != NULL))
    stats->change (stat_gap_content_received, 1);
  /* forward to other peers */
  prio = GNUNET_FS_GAP_handle_response (sender,
                                        &query,
                                        expiration, data_size, dblock);
  /* convert expiration to absolute time and bound properly for
     storage in local datastore */
  now = GNUNET_get_time ();
  if (expiration > GNUNET_GAP_MAX_MIGRATION_EXP)
    {
      /* expired, sometime in the past */
      expiration = now - 1;
    }
  else
    {
      /* expires in future, apply bounding! */
      if (ntohl (dblock->type) == GNUNET_ECRS_BLOCKTYPE_KEYWORD)
        expiration %= GNUNET_GAP_MAX_MIGRATION_EXP_KSK;
      else
        expiration %= GNUNET_GAP_MAX_MIGRATION_EXP;
      expiration += now;
    }
  /* forward to local clients */
  prio += GNUNET_FS_QUERYMANAGER_handle_response (sender,
                                                  &query,
                                                  expiration,
                                                  data_size, dblock);
  if ((sender != NULL) &&
      (active_migration == GNUNET_YES) &&
      ((prio > 0) || (!test_load_too_high ())))
    {
      /* consider storing in local datastore */
      value = GNUNET_malloc (data_size + sizeof (GNUNET_DatastoreValue));
      value->size = htonl (data_size + sizeof (GNUNET_DatastoreValue));
      value->type = dblock->type;
      value->priority = htonl (prio);
      value->anonymity_level = htonl (1);
      value->expiration_time = GNUNET_htonll (expiration);
      memcpy (&value[1], dblock, data_size);
      datastore->putUpdate (&query, value);
      GNUNET_free (value);
    }
  if (sender != NULL)
    {                           /* if we are the sender, sender will be NULL */
      identity->changeHostTrust (sender, prio);
      if (stats != NULL)
        stats->change (stat_gap_trust_awarded, prio);
      preference = (double) prio;
      if (preference < GNUNET_GAP_CONTENT_BANDWIDTH_VALUE)
        preference = GNUNET_GAP_CONTENT_BANDWIDTH_VALUE;
      coreAPI->p2p_connection_preference_increase (sender, preference);
    }
  return GNUNET_OK;
}


/**
 * Initialize the FS module. This method name must match
 * the library name (libgnunet_XXX => initialize_XXX).
 *
 * @return GNUNET_SYSERR on errors
 */
int
initialize_module_fs (GNUNET_CoreAPIForPlugins * capi)
{
  ectx = capi->ectx;
  coreAPI = capi;
  GNUNET_GE_ASSERT (ectx, sizeof (GNUNET_EC_ContentHashKey) == 128);
  GNUNET_GE_ASSERT (ectx, sizeof (GNUNET_EC_DBlock) == 4);
  GNUNET_GE_ASSERT (ectx, sizeof (GNUNET_EC_IBlock) == 132);
  GNUNET_GE_ASSERT (ectx, sizeof (GNUNET_EC_KBlock) == 524);
  GNUNET_GE_ASSERT (ectx, sizeof (GNUNET_EC_SBlock) == 588);
  GNUNET_GE_ASSERT (ectx, sizeof (GNUNET_EC_KSBlock) == 1116);

  if ((-1 == GNUNET_GC_get_configuration_value_number (coreAPI->cfg, "LOAD", "HARDCPULIMIT", 0, 100000, /* 1000 CPUs!? */
                                                       0,       /* 0 == no limit */
                                                       &hardCPULimit)) || (-1 == GNUNET_GC_get_configuration_value_number (coreAPI->cfg, "LOAD", "HARDUPLIMIT", 0, 999999999, 0,        /* 0 == no limit */
                                                                                                                           &hardUpLimit)))
    return GNUNET_SYSERR;
  active_migration
    = GNUNET_GC_get_configuration_value_yesno (coreAPI->cfg,
                                               "FS",
                                               "ACTIVEMIGRATION", GNUNET_NO);
  stats = capi->service_request ("stats");
  if (stats != NULL)
    {
      stat_gap_query_received =
        stats->create (gettext_noop ("# gap requests total received"));
      stat_gap_query_drop_busy =
        stats->create (gettext_noop ("# gap requests dropped due to load"));
      stat_gap_content_received =
        stats->create (gettext_noop ("# gap content total received"));
      stat_gap_trust_awarded =
        stats->create (gettext_noop ("# gap total trust awarded"));
    }
  identity = capi->service_request ("identity");
  if (identity == NULL)
    {
      GNUNET_GE_BREAK (ectx, 0);
      capi->service_release (stats);
      return GNUNET_SYSERR;
    }
  datastore = capi->service_request ("datastore");
  if (datastore == NULL)
    {
      capi->service_release (identity);
      capi->service_release (stats);
      GNUNET_GE_BREAK (ectx, 0);
      return GNUNET_SYSERR;
    }
  GNUNET_FS_lock = capi->global_lock_get ();    // GNUNET_mutex_create (GNUNET_YES);
  GNUNET_FS_ANONYMITY_init (capi);
  GNUNET_FS_PLAN_init (capi);
  GNUNET_FS_ONDEMAND_init (capi);
  GNUNET_FS_PT_init (ectx, stats);
  GNUNET_FS_QUERYMANAGER_init (capi);
  GNUNET_FS_DHT_init (capi);
  GNUNET_FS_GAP_init (capi);
  GNUNET_FS_MIGRATION_init (capi);
  GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
                 _
                 ("`%s' registering client handlers %d %d %d %d %d %d %d %d and P2P handlers %d %d\n"),
                 "fs", GNUNET_CS_PROTO_GAP_QUERY_START,
                 GNUNET_CS_PROTO_GAP_QUERY_STOP,
                 GNUNET_CS_PROTO_GAP_INSERT,
                 GNUNET_CS_PROTO_GAP_INDEX, GNUNET_CS_PROTO_GAP_DELETE,
                 GNUNET_CS_PROTO_GAP_UNINDEX, GNUNET_CS_PROTO_GAP_TESTINDEX,
                 GNUNET_CS_PROTO_GAP_INIT_INDEX,
                 GNUNET_P2P_PROTO_GAP_QUERY, GNUNET_P2P_PROTO_GAP_RESULT);
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->p2p_ciphertext_handler_register
                    (GNUNET_P2P_PROTO_GAP_QUERY, &handle_p2p_query));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->p2p_ciphertext_handler_register
                    (GNUNET_P2P_PROTO_GAP_RESULT, &handle_p2p_content));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register
                    (GNUNET_CS_PROTO_GAP_QUERY_START,
                     &handle_cs_query_start_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register
                    (GNUNET_CS_PROTO_GAP_QUERY_STOP,
                     &handle_cs_query_stop_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register (GNUNET_CS_PROTO_GAP_INSERT,
                                               &handle_cs_insert_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register (GNUNET_CS_PROTO_GAP_INDEX,
                                               &handle_cs_index_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register (GNUNET_CS_PROTO_GAP_INIT_INDEX,
                                               &handle_cs_init_index_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register (GNUNET_CS_PROTO_GAP_DELETE,
                                               &handle_cs_delete_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register (GNUNET_CS_PROTO_GAP_UNINDEX,
                                               &handle_cs_unindex_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    capi->cs_handler_register (GNUNET_CS_PROTO_GAP_TESTINDEX,
                                               &handle_cs_test_indexed_request));
  GNUNET_GE_ASSERT (capi->ectx,
                    0 == GNUNET_GC_set_configuration_value_string (capi->cfg,
                                                                   capi->ectx,
                                                                   "ABOUT",
                                                                   "fs",
                                                                   gettext_noop
                                                                   ("enables (anonymous) file-sharing")));
  return GNUNET_OK;
}

void
done_module_fs ()
{
  GNUNET_GE_LOG (ectx, GNUNET_GE_DEBUG | GNUNET_GE_REQUEST | GNUNET_GE_USER,
                 "fs shutdown\n");

  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->p2p_ciphertext_handler_unregister
                    (GNUNET_P2P_PROTO_GAP_QUERY, &handle_p2p_query));

  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->p2p_ciphertext_handler_unregister
                    (GNUNET_P2P_PROTO_GAP_RESULT, &handle_p2p_content));

  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->cs_handler_unregister
                    (GNUNET_CS_PROTO_GAP_QUERY_START,
                     &handle_cs_query_start_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->cs_handler_unregister
                    (GNUNET_CS_PROTO_GAP_INSERT, &handle_cs_insert_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->cs_handler_unregister (GNUNET_CS_PROTO_GAP_INDEX,
                                                    &handle_cs_index_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->cs_handler_unregister
                    (GNUNET_CS_PROTO_GAP_INIT_INDEX,
                     &handle_cs_init_index_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->cs_handler_unregister
                    (GNUNET_CS_PROTO_GAP_DELETE, &handle_cs_delete_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->cs_handler_unregister
                    (GNUNET_CS_PROTO_GAP_UNINDEX,
                     &handle_cs_unindex_request));
  GNUNET_GE_ASSERT (ectx,
                    GNUNET_SYSERR !=
                    coreAPI->cs_handler_unregister
                    (GNUNET_CS_PROTO_GAP_TESTINDEX,
                     &handle_cs_test_indexed_request));
  GNUNET_FS_MIGRATION_done ();
  GNUNET_FS_GAP_done ();
  GNUNET_FS_DHT_done ();
  GNUNET_FS_QUERYMANAGER_done ();
  GNUNET_FS_ONDEMAND_done ();
  GNUNET_FS_PLAN_done ();
  GNUNET_FS_ANONYMITY_done ();
  GNUNET_FS_PT_done ();
  if (stats != NULL)
    {
      coreAPI->service_release (stats);
      stats = NULL;
    }
  coreAPI->service_release (datastore);
  datastore = NULL;
  coreAPI->service_release (identity);
  identity = NULL;
  GNUNET_FS_lock = NULL;
}


/**
 * Update FS.
 */
void
update_module_fs (GNUNET_UpdateAPI * uapi)
{
  uapi->service_update ("datastore");
}


/* end of fs.c */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -