command.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /* Copyright 2001,2002,2003 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. extern or_options_t options; /* command-line and config-file options */
  6. unsigned long stats_n_padding_cells_processed = 0;
  7. unsigned long stats_n_create_cells_processed = 0;
  8. unsigned long stats_n_created_cells_processed = 0;
  9. unsigned long stats_n_relay_cells_processed = 0;
  10. unsigned long stats_n_destroy_cells_processed = 0;
  11. static void command_process_create_cell(cell_t *cell, connection_t *conn);
  12. static void command_process_created_cell(cell_t *cell, connection_t *conn);
  13. static void command_process_relay_cell(cell_t *cell, connection_t *conn);
  14. static void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  15. static void command_time_process_cell(cell_t *cell, connection_t *conn,
  16. int *num, int *time,
  17. void (*func)(cell_t *, connection_t *)) {
  18. struct timeval start, end;
  19. long time_passed;
  20. *num += 1;
  21. tor_gettimeofday(&start);
  22. (*func)(cell, conn);
  23. tor_gettimeofday(&end);
  24. time_passed = tv_udiff(&start, &end) ;
  25. if (time_passed > 10000) { /* more than 10ms */
  26. log_fn(LOG_INFO,"That call just took %ld ms.",time_passed/1000);
  27. }
  28. *time += time_passed;
  29. }
  30. void command_process_cell(cell_t *cell, connection_t *conn) {
  31. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  32. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  33. static time_t current_second = 0; /* from previous calls to time */
  34. time_t now = time(NULL);
  35. if(now > current_second) { /* the second has rolled over */
  36. /* print stats */
  37. log(LOG_INFO,"At end of second: %d creates (%d ms), %d createds (%d ms), %d relays (%d ms), %d destroys (%d ms)",
  38. num_create, create_time/1000,
  39. num_created, created_time/1000,
  40. num_relay, relay_time/1000,
  41. num_destroy, destroy_time/1000);
  42. /* zero out stats */
  43. num_create = num_created = num_relay = num_destroy = 0;
  44. create_time = created_time = relay_time = destroy_time = 0;
  45. /* remember which second it is, for next time */
  46. current_second = now;
  47. }
  48. switch(cell->command) {
  49. case CELL_PADDING:
  50. ++stats_n_padding_cells_processed;
  51. /* do nothing */
  52. break;
  53. case CELL_CREATE:
  54. ++stats_n_create_cells_processed;
  55. command_time_process_cell(cell, conn, &num_create, &create_time,
  56. command_process_create_cell);
  57. break;
  58. case CELL_CREATED:
  59. ++stats_n_created_cells_processed;
  60. command_time_process_cell(cell, conn, &num_created, &created_time,
  61. command_process_created_cell);
  62. break;
  63. case CELL_RELAY:
  64. ++stats_n_relay_cells_processed;
  65. command_time_process_cell(cell, conn, &num_relay, &relay_time,
  66. command_process_relay_cell);
  67. break;
  68. case CELL_DESTROY:
  69. ++stats_n_destroy_cells_processed;
  70. command_time_process_cell(cell, conn, &num_destroy, &destroy_time,
  71. command_process_destroy_cell);
  72. break;
  73. default:
  74. log_fn(LOG_WARN,"Cell of unknown type (%d) received. Dropping.", cell->command);
  75. break;
  76. }
  77. }
  78. static void command_process_create_cell(cell_t *cell, connection_t *conn) {
  79. circuit_t *circ;
  80. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  81. if(circ) {
  82. log_fn(LOG_WARN,"received CREATE cell (circID %d) for known circ. Dropping.", cell->circ_id);
  83. return;
  84. }
  85. circ = circuit_new(cell->circ_id, conn);
  86. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  87. circ->purpose = CIRCUIT_PURPOSE_OR;
  88. memcpy(circ->onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  89. /* hand it off to the cpuworkers, and then return */
  90. if(assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  91. log_fn(LOG_WARN,"Failed to hand off onionskin. Closing.");
  92. circuit_mark_for_close(circ);
  93. return;
  94. }
  95. log_fn(LOG_DEBUG,"success: handed off onionskin.");
  96. }
  97. static void command_process_created_cell(cell_t *cell, connection_t *conn) {
  98. circuit_t *circ;
  99. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  100. if(!circ) {
  101. log_fn(LOG_INFO,"(circID %d) unknown circ (probably got a destroy earlier). Dropping.", cell->circ_id);
  102. return;
  103. }
  104. if(circ->n_circ_id != cell->circ_id) {
  105. log_fn(LOG_WARN,"got created cell from OPward? Closing.");
  106. circuit_mark_for_close(circ);
  107. return;
  108. }
  109. if(CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  110. log_fn(LOG_DEBUG,"at OP. Finishing handshake.");
  111. if(circuit_finish_handshake(circ, cell->payload) < 0) {
  112. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  113. circuit_mark_for_close(circ);
  114. return;
  115. }
  116. log_fn(LOG_DEBUG,"Moving to next skin.");
  117. if(circuit_send_next_onion_skin(circ) < 0) {
  118. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  119. circuit_mark_for_close(circ); /* XXX push this circuit_close lower */
  120. return;
  121. }
  122. } else { /* pack it into an extended relay cell, and send it. */
  123. log_fn(LOG_DEBUG,"Converting created cell to extended relay cell, sending.");
  124. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  125. cell->payload, ONIONSKIN_REPLY_LEN, NULL);
  126. }
  127. }
  128. static void command_process_relay_cell(cell_t *cell, connection_t *conn) {
  129. circuit_t *circ;
  130. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  131. if(!circ) {
  132. log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
  133. cell->circ_id, conn->address, conn->port);
  134. return;
  135. }
  136. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  137. log_fn(LOG_WARN,"circuit in create_wait. Closing.");
  138. circuit_mark_for_close(circ);
  139. return;
  140. }
  141. if(cell->circ_id == circ->p_circ_id) { /* it's an outgoing cell */
  142. if(circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
  143. log_fn(LOG_WARN,"circuit_receive_relay_cell (forward) failed. Closing.");
  144. circuit_mark_for_close(circ);
  145. return;
  146. }
  147. } else { /* it's an ingoing cell */
  148. if(circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
  149. log_fn(LOG_WARN,"circuit_receive_relay_cell (backward) failed. Closing.");
  150. circuit_mark_for_close(circ);
  151. return;
  152. }
  153. }
  154. }
  155. static void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  156. circuit_t *circ;
  157. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  158. if(!circ) {
  159. log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
  160. cell->circ_id, conn->address, conn->port);
  161. return;
  162. }
  163. log_fn(LOG_INFO,"Received for circID %d.",cell->circ_id);
  164. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  165. onion_pending_remove(circ);
  166. }
  167. if(cell->circ_id == circ->p_circ_id || CIRCUIT_IS_ORIGIN(circ)) {
  168. /* either the destroy came from behind, or we're the AP */
  169. circ->p_conn = NULL;
  170. circuit_mark_for_close(circ);
  171. } else { /* the destroy came from ahead */
  172. circ->n_conn = NULL;
  173. log_fn(LOG_DEBUG, "Delivering 'truncated' back.");
  174. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  175. NULL, 0, NULL);
  176. }
  177. }
  178. /*
  179. Local Variables:
  180. mode:c
  181. indent-tabs-mode:nil
  182. c-basic-offset:2
  183. End:
  184. */