tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

fakecircs.c (2710B)


      1 /* Copyright (c) 2019-2021, The Tor Project, Inc. */
      2 /* See LICENSE for licensing information */
      3 
      4 /**
      5 * \file fakecircs.c
      6 * \brief Fake circuits API for unit test.
      7 **/
      8 
      9 #define CIRCUITBUILD_PRIVATE
     10 #define CIRCUITLIST_PRIVATE
     11 #define CRYPT_PATH_PRIVATE
     12 
     13 #include "core/or/or.h"
     14 
     15 #include "core/crypto/relay_crypto.h"
     16 #include "core/or/channel.h"
     17 #include "core/or/circuitbuild.h"
     18 #include "core/or/circuitlist.h"
     19 #include "core/or/circuitpadding.h"
     20 #include "core/or/congestion_control_common.h"
     21 #include "core/or/conflux_pool.h"
     22 #include "core/or/conflux.h"
     23 #include "core/or/crypt_path.h"
     24 #include "core/or/relay.h"
     25 #include "core/crypto/relay_crypto_st.h"
     26 
     27 #include "test/fakecircs.h"
     28 
     29 /** Return newly allocated OR circuit using the given nchan and pchan. It must
     30 * be freed with the free_fake_orcirc(). */
     31 or_circuit_t *
     32 new_fake_orcirc(channel_t *nchan, channel_t *pchan)
     33 {
     34  or_circuit_t *orcirc = NULL;
     35  circuit_t *circ = NULL;
     36  crypt_path_t tmp_cpath;
     37  char whatevs_key[CPATH_KEY_MATERIAL_LEN];
     38 
     39  orcirc = tor_malloc_zero(sizeof(*orcirc));
     40  circ = &(orcirc->base_);
     41  circ->magic = OR_CIRCUIT_MAGIC;
     42 
     43  circuit_set_n_circid_chan(circ, get_unique_circ_id_by_chan(nchan), nchan);
     44  cell_queue_init(&(circ->n_chan_cells));
     45 
     46  circ->n_hop = NULL;
     47  circ->circuit_blocked_on_n_chan = 0;
     48  circ->circuit_blocked_on_p_chan = 0;
     49  circ->n_delete_pending = 0;
     50  circ->p_delete_pending = 0;
     51  circ->received_destroy = 0;
     52  circ->state = CIRCUIT_STATE_OPEN;
     53  circ->purpose = CIRCUIT_PURPOSE_OR;
     54  circ->package_window = CIRCWINDOW_START_MAX;
     55  circ->deliver_window = CIRCWINDOW_START_MAX;
     56  circ->n_chan_create_cell = NULL;
     57 
     58  circuit_set_p_circid_chan(orcirc, get_unique_circ_id_by_chan(pchan), pchan);
     59  cell_queue_init(&(orcirc->p_chan_cells));
     60 
     61  memset(&tmp_cpath, 0, sizeof(tmp_cpath));
     62  if (cpath_init_circuit_crypto(RELAY_CRYPTO_ALG_TOR1,
     63                                &tmp_cpath, whatevs_key,
     64                                sizeof(whatevs_key))<0) {
     65    log_warn(LD_BUG,"Circuit initialization failed");
     66    return NULL;
     67  }
     68  orcirc->crypto = tmp_cpath.pvt_crypto;
     69 
     70  return orcirc;
     71 }
     72 
     73 /** Free fake OR circuit which MUST be created by new_fake_orcirc(). */
     74 void
     75 free_fake_orcirc(or_circuit_t *orcirc)
     76 {
     77  if (!orcirc) {
     78    return;
     79  }
     80 
     81  circuit_t *circ = TO_CIRCUIT(orcirc);
     82 
     83  relay_crypto_clear(&orcirc->crypto);
     84 
     85  circpad_circuit_free_all_machineinfos(circ);
     86 
     87  if (orcirc->p_chan && orcirc->p_chan->cmux) {
     88    circuitmux_detach_circuit(orcirc->p_chan->cmux, circ);
     89  }
     90  if (circ->n_chan && circ->n_chan->cmux) {
     91    circuitmux_detach_circuit(circ->n_chan->cmux, circ);
     92  }
     93 
     94  conflux_circuit_about_to_free(circ);
     95  congestion_control_free(circ->ccontrol);
     96 
     97  tor_free_(circ);
     98 }