onion.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2012, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file onion.c
  8. * \brief Functions to queue create cells, and handle onionskin
  9. * parsing and creation.
  10. **/
  11. #include "or.h"
  12. #include "circuitlist.h"
  13. #include "config.h"
  14. #include "onion.h"
  15. #include "rephist.h"
  16. /** Type for a linked list of circuits that are waiting for a free CPU worker
  17. * to process a waiting onion handshake. */
  18. typedef struct onion_queue_t {
  19. or_circuit_t *circ;
  20. char *onionskin;
  21. time_t when_added;
  22. struct onion_queue_t *next;
  23. } onion_queue_t;
  24. /** 5 seconds on the onion queue til we just send back a destroy */
  25. #define ONIONQUEUE_WAIT_CUTOFF 5
  26. /** First and last elements in the linked list of circuits waiting for CPU
  27. * workers, or NULL if the list is empty.
  28. * @{ */
  29. static onion_queue_t *ol_list=NULL;
  30. static onion_queue_t *ol_tail=NULL;
  31. /**@}*/
  32. /** Length of ol_list */
  33. static int ol_length=0;
  34. /** Add <b>circ</b> to the end of ol_list and return 0, except
  35. * if ol_list is too long, in which case do nothing and return -1.
  36. */
  37. int
  38. onion_pending_add(or_circuit_t *circ, char *onionskin)
  39. {
  40. onion_queue_t *tmp;
  41. time_t now = time(NULL);
  42. tmp = tor_malloc_zero(sizeof(onion_queue_t));
  43. tmp->circ = circ;
  44. tmp->onionskin = onionskin;
  45. tmp->when_added = now;
  46. if (!ol_tail) {
  47. tor_assert(!ol_list);
  48. tor_assert(!ol_length);
  49. ol_list = tmp;
  50. ol_tail = tmp;
  51. ol_length++;
  52. return 0;
  53. }
  54. tor_assert(ol_list);
  55. tor_assert(!ol_tail->next);
  56. if (ol_length >= get_options()->MaxOnionsPending) {
  57. #define WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL (60)
  58. static ratelim_t last_warned =
  59. RATELIM_INIT(WARN_TOO_MANY_CIRC_CREATIONS_INTERVAL);
  60. char *m;
  61. if ((m = rate_limit_log(&last_warned, approx_time()))) {
  62. log_warn(LD_GENERAL,
  63. "Your computer is too slow to handle this many circuit "
  64. "creation requests! Please consider using the "
  65. "MaxAdvertisedBandwidth config option or choosing a more "
  66. "restricted exit policy.%s",m);
  67. tor_free(m);
  68. }
  69. tor_free(tmp);
  70. return -1;
  71. }
  72. ol_length++;
  73. ol_tail->next = tmp;
  74. ol_tail = tmp;
  75. while ((int)(now - ol_list->when_added) >= ONIONQUEUE_WAIT_CUTOFF) {
  76. /* cull elderly requests. */
  77. circ = ol_list->circ;
  78. onion_pending_remove(ol_list->circ);
  79. log_info(LD_CIRC,
  80. "Circuit create request is too old; canceling due to overload.");
  81. circuit_mark_for_close(TO_CIRCUIT(circ), END_CIRC_REASON_RESOURCELIMIT);
  82. }
  83. return 0;
  84. }
  85. /** Remove the first item from ol_list and return it, or return
  86. * NULL if the list is empty.
  87. */
  88. or_circuit_t *
  89. onion_next_task(char **onionskin_out)
  90. {
  91. or_circuit_t *circ;
  92. if (!ol_list)
  93. return NULL; /* no onions pending, we're done */
  94. tor_assert(ol_list->circ);
  95. tor_assert(ol_list->circ->p_chan); /* make sure it's still valid */
  96. tor_assert(ol_length > 0);
  97. circ = ol_list->circ;
  98. *onionskin_out = ol_list->onionskin;
  99. ol_list->onionskin = NULL; /* prevent free. */
  100. onion_pending_remove(ol_list->circ);
  101. return circ;
  102. }
  103. /** Go through ol_list, find the onion_queue_t element which points to
  104. * circ, remove and free that element. Leave circ itself alone.
  105. */
  106. void
  107. onion_pending_remove(or_circuit_t *circ)
  108. {
  109. onion_queue_t *tmpo, *victim;
  110. if (!ol_list)
  111. return; /* nothing here. */
  112. /* first check to see if it's the first entry */
  113. tmpo = ol_list;
  114. if (tmpo->circ == circ) {
  115. /* it's the first one. remove it from the list. */
  116. ol_list = tmpo->next;
  117. if (!ol_list)
  118. ol_tail = NULL;
  119. ol_length--;
  120. victim = tmpo;
  121. } else { /* we need to hunt through the rest of the list */
  122. for ( ;tmpo->next && tmpo->next->circ != circ; tmpo=tmpo->next) ;
  123. if (!tmpo->next) {
  124. log_debug(LD_GENERAL,
  125. "circ (p_circ_id %d) not in list, probably at cpuworker.",
  126. circ->p_circ_id);
  127. return;
  128. }
  129. /* now we know tmpo->next->circ == circ */
  130. victim = tmpo->next;
  131. tmpo->next = victim->next;
  132. if (ol_tail == victim)
  133. ol_tail = tmpo;
  134. ol_length--;
  135. }
  136. /* now victim points to the element that needs to be removed */
  137. tor_free(victim->onionskin);
  138. tor_free(victim);
  139. }
  140. /** Remove all circuits from the pending list. Called from tor_free_all. */
  141. void
  142. clear_pending_onions(void)
  143. {
  144. while (ol_list) {
  145. onion_queue_t *victim = ol_list;
  146. ol_list = victim->next;
  147. tor_free(victim->onionskin);
  148. tor_free(victim);
  149. }
  150. ol_list = ol_tail = NULL;
  151. ol_length = 0;
  152. }