command.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 > 5000) { /* more than 5ms */
  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_WARNING,"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_aci_conn(cell->aci, conn);
  81. if(circ) {
  82. log_fn(LOG_WARNING,"received CREATE cell (aci %d) for known circ. Dropping.", cell->aci);
  83. return;
  84. }
  85. circ = circuit_new(cell->aci, conn);
  86. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  87. if(cell->length != DH_ONIONSKIN_LEN) {
  88. log_fn(LOG_WARNING,"Bad cell length %d. Dropping.", cell->length);
  89. circuit_close(circ);
  90. return;
  91. }
  92. memcpy(circ->onionskin,cell->payload,cell->length);
  93. /* hand it off to the cpuworkers, and then return */
  94. if(assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  95. log_fn(LOG_WARNING,"Failed to hand off onionskin. Closing.");
  96. circuit_close(circ);
  97. return;
  98. }
  99. log_fn(LOG_INFO,"success: handed off onionskin.");
  100. }
  101. static void command_process_created_cell(cell_t *cell, connection_t *conn) {
  102. circuit_t *circ;
  103. circ = circuit_get_by_aci_conn(cell->aci, conn);
  104. if(!circ) {
  105. log_fn(LOG_INFO,"(aci %d) unknown circ (probably got a destroy earlier). Dropping.", cell->aci);
  106. return;
  107. }
  108. if(circ->n_aci != cell->aci) {
  109. log_fn(LOG_WARNING,"got created cell from OPward? Closing.");
  110. circuit_close(circ);
  111. return;
  112. }
  113. assert(cell->length == DH_KEY_LEN);
  114. if(circ->cpath) { /* we're the OP. Handshake this. */
  115. log_fn(LOG_DEBUG,"at OP. Finishing handshake.");
  116. if(circuit_finish_handshake(circ, cell->payload) < 0) {
  117. log_fn(LOG_WARNING,"circuit_finish_handshake failed.");
  118. circuit_close(circ);
  119. return;
  120. }
  121. log_fn(LOG_DEBUG,"Moving to next skin.");
  122. if(circuit_send_next_onion_skin(circ) < 0) {
  123. log_fn(LOG_WARNING,"circuit_send_next_onion_skin failed.");
  124. circuit_close(circ);
  125. return;
  126. }
  127. } else { /* pack it into an extended relay cell, and send it. */
  128. log_fn(LOG_INFO,"Converting created cell to extended relay cell, sending.");
  129. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  130. cell->payload, DH_KEY_LEN, NULL);
  131. }
  132. }
  133. static void command_process_relay_cell(cell_t *cell, connection_t *conn) {
  134. circuit_t *circ;
  135. circ = circuit_get_by_aci_conn(cell->aci, conn);
  136. if(!circ) {
  137. log_fn(LOG_INFO,"unknown circuit %d. Dropping.", cell->aci);
  138. return;
  139. }
  140. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  141. log_fn(LOG_WARNING,"circuit in create_wait. Closing.");
  142. circuit_close(circ);
  143. return;
  144. }
  145. if(cell->aci == circ->p_aci) { /* it's an outgoing cell */
  146. cell->aci = circ->n_aci; /* switch it */
  147. if(circuit_deliver_relay_cell(cell, circ, CELL_DIRECTION_OUT, conn->cpath_layer) < 0) {
  148. log_fn(LOG_WARNING,"circuit_deliver_relay_cell (forward) failed. Closing.");
  149. circuit_close(circ);
  150. return;
  151. }
  152. } else { /* it's an ingoing cell */
  153. cell->aci = circ->p_aci; /* switch it */
  154. if(circuit_deliver_relay_cell(cell, circ, CELL_DIRECTION_IN, NULL) < 0) {
  155. log_fn(LOG_WARNING,"circuit_deliver_relay_cell (backward) failed. Closing.");
  156. circuit_close(circ);
  157. return;
  158. }
  159. }
  160. }
  161. static void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  162. circuit_t *circ;
  163. circ = circuit_get_by_aci_conn(cell->aci, conn);
  164. if(!circ) {
  165. log_fn(LOG_INFO,"unknown circuit %d. Dropping.", cell->aci);
  166. return;
  167. }
  168. log_fn(LOG_DEBUG,"Received for aci %d.",cell->aci);
  169. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  170. onion_pending_remove(circ);
  171. }
  172. if(cell->aci == circ->p_aci || circ->cpath) {
  173. /* either the destroy came from behind, or we're the AP */
  174. circ->p_conn = NULL;
  175. circuit_close(circ);
  176. } else { /* the destroy came from ahead */
  177. circ->n_conn = NULL;
  178. log_fn(LOG_DEBUG, "Delivering 'truncated' back.");
  179. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  180. NULL, 0, NULL);
  181. }
  182. }
  183. /*
  184. Local Variables:
  185. mode:c
  186. indent-tabs-mode:nil
  187. c-basic-offset:2
  188. End:
  189. */