Line data Source code
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/crypt_path.h" 21 : #include "core/or/relay.h" 22 : #include "core/or/relay_crypto_st.h" 23 : 24 : #include "test/fakecircs.h" 25 : 26 : /** Return newly allocated OR circuit using the given nchan and pchan. It must 27 : * be freed with the free_fake_orcirc(). */ 28 : or_circuit_t * 29 19 : new_fake_orcirc(channel_t *nchan, channel_t *pchan) 30 : { 31 19 : or_circuit_t *orcirc = NULL; 32 19 : circuit_t *circ = NULL; 33 19 : crypt_path_t tmp_cpath; 34 19 : char whatevs_key[CPATH_KEY_MATERIAL_LEN]; 35 : 36 19 : orcirc = tor_malloc_zero(sizeof(*orcirc)); 37 19 : circ = &(orcirc->base_); 38 19 : circ->magic = OR_CIRCUIT_MAGIC; 39 : 40 19 : circuit_set_n_circid_chan(circ, get_unique_circ_id_by_chan(nchan), nchan); 41 19 : cell_queue_init(&(circ->n_chan_cells)); 42 : 43 19 : circ->n_hop = NULL; 44 19 : circ->streams_blocked_on_n_chan = 0; 45 19 : circ->streams_blocked_on_p_chan = 0; 46 19 : circ->n_delete_pending = 0; 47 19 : circ->p_delete_pending = 0; 48 19 : circ->received_destroy = 0; 49 19 : circ->state = CIRCUIT_STATE_OPEN; 50 19 : circ->purpose = CIRCUIT_PURPOSE_OR; 51 19 : circ->package_window = CIRCWINDOW_START_MAX; 52 19 : circ->deliver_window = CIRCWINDOW_START_MAX; 53 19 : circ->n_chan_create_cell = NULL; 54 : 55 19 : circuit_set_p_circid_chan(orcirc, get_unique_circ_id_by_chan(pchan), pchan); 56 19 : cell_queue_init(&(orcirc->p_chan_cells)); 57 : 58 19 : memset(&tmp_cpath, 0, sizeof(tmp_cpath)); 59 19 : if (cpath_init_circuit_crypto(&tmp_cpath, whatevs_key, 60 : sizeof(whatevs_key), 0, 0)<0) { 61 0 : log_warn(LD_BUG,"Circuit initialization failed"); 62 0 : return NULL; 63 : } 64 19 : orcirc->crypto = tmp_cpath.pvt_crypto; 65 : 66 19 : return orcirc; 67 : } 68 : 69 : /** Free fake OR circuit which MUST be created by new_fake_orcirc(). */ 70 : void 71 18 : free_fake_orcirc(or_circuit_t *orcirc) 72 : { 73 18 : if (!orcirc) { 74 : return; 75 : } 76 : 77 18 : circuit_t *circ = TO_CIRCUIT(orcirc); 78 : 79 18 : relay_crypto_clear(&orcirc->crypto); 80 : 81 18 : circpad_circuit_free_all_machineinfos(circ); 82 : 83 18 : if (orcirc->p_chan && orcirc->p_chan->cmux) { 84 18 : circuitmux_detach_circuit(orcirc->p_chan->cmux, circ); 85 : } 86 18 : if (circ->n_chan && circ->n_chan->cmux) { 87 18 : circuitmux_detach_circuit(circ->n_chan->cmux, circ); 88 : } 89 : 90 18 : tor_free_(circ); 91 : }