command.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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:");
  38. log(LOG_INFO,"Create: %d (%d ms)", num_create, create_time/1000);
  39. log(LOG_INFO,"Created: %d (%d ms)", num_created, created_time/1000);
  40. log(LOG_INFO,"Relay: %d (%d ms)", num_relay, relay_time/1000);
  41. log(LOG_INFO,"Destroy: %d (%d ms)", 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. memcpy(circ->onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  88. /* hand it off to the cpuworkers, and then return */
  89. if(assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  90. log_fn(LOG_WARN,"Failed to hand off onionskin. Closing.");
  91. circuit_mark_for_close(circ);
  92. return;
  93. }
  94. log_fn(LOG_DEBUG,"success: handed off onionskin.");
  95. }
  96. static void command_process_created_cell(cell_t *cell, connection_t *conn) {
  97. circuit_t *circ;
  98. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  99. if(!circ) {
  100. log_fn(LOG_INFO,"(circID %d) unknown circ (probably got a destroy earlier). Dropping.", cell->circ_id);
  101. return;
  102. }
  103. if(circ->n_circ_id != cell->circ_id) {
  104. log_fn(LOG_WARN,"got created cell from OPward? Closing.");
  105. circuit_mark_for_close(circ);
  106. return;
  107. }
  108. if(circ->cpath) { /* we're the OP. Handshake this. */
  109. log_fn(LOG_DEBUG,"at OP. Finishing handshake.");
  110. if(circuit_finish_handshake(circ, cell->payload) < 0) {
  111. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  112. circuit_mark_for_close(circ);
  113. return;
  114. }
  115. log_fn(LOG_DEBUG,"Moving to next skin.");
  116. if(circuit_send_next_onion_skin(circ) < 0) {
  117. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  118. circuit_mark_for_close(circ); /* XXX push this circuit_close lower */
  119. return;
  120. }
  121. } else { /* pack it into an extended relay cell, and send it. */
  122. log_fn(LOG_DEBUG,"Converting created cell to extended relay cell, sending.");
  123. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  124. cell->payload, ONIONSKIN_REPLY_LEN, NULL);
  125. }
  126. }
  127. static void command_process_relay_cell(cell_t *cell, connection_t *conn) {
  128. circuit_t *circ;
  129. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  130. if(!circ) {
  131. log_fn(LOG_INFO,"unknown circuit %d. Dropping.", cell->circ_id);
  132. return;
  133. }
  134. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  135. log_fn(LOG_WARN,"circuit in create_wait. Closing.");
  136. circuit_mark_for_close(circ);
  137. return;
  138. }
  139. if(cell->circ_id == circ->p_circ_id) { /* it's an outgoing cell */
  140. if(circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
  141. log_fn(LOG_WARN,"circuit_receive_relay_cell (forward) failed. Closing.");
  142. circuit_mark_for_close(circ);
  143. return;
  144. }
  145. } else { /* it's an ingoing cell */
  146. if(circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
  147. log_fn(LOG_WARN,"circuit_receive_relay_cell (backward) failed. Closing.");
  148. circuit_mark_for_close(circ);
  149. return;
  150. }
  151. }
  152. }
  153. static void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  154. circuit_t *circ;
  155. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  156. if(!circ) {
  157. log_fn(LOG_INFO,"unknown circuit %d. Dropping.", cell->circ_id);
  158. return;
  159. }
  160. log_fn(LOG_DEBUG,"Received for circID %d.",cell->circ_id);
  161. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  162. onion_pending_remove(circ);
  163. }
  164. if(cell->circ_id == circ->p_circ_id || circ->cpath) {
  165. /* either the destroy came from behind, or we're the AP */
  166. circ->p_conn = NULL;
  167. circuit_mark_for_close(circ);
  168. } else { /* the destroy came from ahead */
  169. circ->n_conn = NULL;
  170. log_fn(LOG_DEBUG, "Delivering 'truncated' back.");
  171. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  172. NULL, 0, NULL);
  173. }
  174. }
  175. /*
  176. Local Variables:
  177. mode:c
  178. indent-tabs-mode:nil
  179. c-basic-offset:2
  180. End:
  181. */