Esempio n. 1
0
static tau_transaction_t
tau_transaction_new (void)
{
    tau_transaction_t tmp;
    tr_rand_buffer (&tmp, sizeof (tau_transaction_t));
    return tmp;
}
Esempio n. 2
0
static int
test_single_directory_random_payload_impl (const tr_tracker_info * trackers,
                                           const size_t            trackerCount,
                                           const size_t            maxFileCount,
                                           const size_t            maxFileSize,
                                           const char            * comment,
                                           const bool              isPrivate)
{
  size_t i;
  void ** payloads;
  size_t * payloadSizes;
  size_t payloadCount;

  /* build random payloads */
  payloadCount = 1 + tr_rand_int_weak (maxFileCount);
  payloads = tr_new0 (void*, payloadCount);
  payloadSizes = tr_new0 (size_t, payloadCount);
  for (i=0; i<payloadCount; i++)
    {
      const size_t n = 1 + tr_rand_int_weak (maxFileSize);
      payloads[i] = tr_new (char, n);
      tr_rand_buffer (payloads[i], n);
      payloadSizes[i] = n;
    }

  /* run the test */
  test_single_directory_impl (trackers,
                              trackerCount,
                              (const void**) payloads,
                              payloadSizes,
                              payloadCount,
                              comment,
                              isPrivate);

  /* cleanup */
  for (i=0; i<payloadCount; i++)
    tr_free (payloads[i]);
  tr_free (payloads);
  tr_free (payloadSizes);

  return 0;
}
Esempio n. 3
0
int
tr_dhtInit (tr_session *ss)
{
    tr_variant benc;
    int rc;
    bool have_id = false;
    char * dat_file;
    uint8_t * nodes = NULL, * nodes6 = NULL;
    const uint8_t * raw;
    size_t len, len6;
    struct bootstrap_closure * cl;

    if (session) /* already initialized */
        return -1;

    tr_logAddNamedDbg ("DHT", "Initializing DHT");

    if (tr_env_key_exists ("TR_DHT_VERBOSE"))
        dht_debug = stderr;

    dat_file = tr_buildPath (ss->configDir, "dht.dat", NULL);
    rc = tr_variantFromFile (&benc, TR_VARIANT_FMT_BENC, dat_file, NULL) ? 0 : -1;
    tr_free (dat_file);
    if (rc == 0) {
        have_id = tr_variantDictFindRaw (&benc, TR_KEY_id, &raw, &len);
        if (have_id && len==20)
            memcpy (myid, raw, len);
        if (ss->udp_socket != TR_BAD_SOCKET &&
            tr_variantDictFindRaw (&benc, TR_KEY_nodes, &raw, &len) && ! (len%6)) {
                nodes = tr_memdup (raw, len);
        }
        if (ss->udp6_socket != TR_BAD_SOCKET &&
            tr_variantDictFindRaw (&benc, TR_KEY_nodes6, &raw, &len6) && ! (len6%18)) {
            nodes6 = tr_memdup (raw, len6);
        }
        tr_variantFree (&benc);
    }

    if (nodes == NULL)
        len = 0;
    if (nodes6 == NULL)
        len6 = 0;

    if (have_id)
        tr_logAddNamedInfo ("DHT", "Reusing old id");
    else {
        /* Note that DHT ids need to be distributed uniformly,
         * so it should be something truly random. */
        tr_logAddNamedInfo ("DHT", "Generating new id");
        tr_rand_buffer (myid, 20);
    }

    rc = dht_init (ss->udp_socket, ss->udp6_socket, myid, NULL);
    if (rc < 0)
        goto fail;

    session = ss;

    cl = tr_new (struct bootstrap_closure, 1);
    cl->session = session;
    cl->nodes = nodes;
    cl->nodes6 = nodes6;
    cl->len = len;
    cl->len6 = len6;
    tr_threadNew (dht_bootstrap, cl);

    dht_timer = evtimer_new (session->event_base, timer_callback, session);
    tr_timerAdd (dht_timer, 0, tr_rand_int_weak (1000000));

    tr_logAddNamedDbg ("DHT", "DHT initialized");

    return 1;

 fail:
    tr_logAddNamedDbg ("DHT", "DHT initialization failed (errno = %d)", errno);
    session = NULL;
    return -1;
}