command.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /* Copyright 2001 Matej Pfajfar, 2001-2004 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. /**
  5. * \file command.c
  6. * \brief Functions for processing incoming cells
  7. **/
  8. /* In-points to command.c:
  9. *
  10. * - command_process_cell(), called from
  11. * connection_or_process_cells_from_inbuf() in connection_or.c
  12. */
  13. #include "or.h"
  14. extern or_options_t options; /* command-line and config-file options */
  15. /** Keep statistics about how many of each type of cell we've received. */
  16. unsigned long stats_n_padding_cells_processed = 0;
  17. unsigned long stats_n_create_cells_processed = 0;
  18. unsigned long stats_n_created_cells_processed = 0;
  19. unsigned long stats_n_relay_cells_processed = 0;
  20. unsigned long stats_n_destroy_cells_processed = 0;
  21. /** These are the main four functions for processing cells */
  22. static void command_process_create_cell(cell_t *cell, connection_t *conn);
  23. static void command_process_created_cell(cell_t *cell, connection_t *conn);
  24. static void command_process_relay_cell(cell_t *cell, connection_t *conn);
  25. static void command_process_destroy_cell(cell_t *cell, connection_t *conn);
  26. /** This is a wrapper function around the actual function that processes the
  27. * <b>cell</b> that just arrived on <b>conn</b>. Increment <b>*time</b>
  28. * by the number of microseconds used by the call to <b>*func(cell, conn)</b>.
  29. */
  30. static void command_time_process_cell(cell_t *cell, connection_t *conn, int *time,
  31. void (*func)(cell_t *, connection_t *)) {
  32. struct timeval start, end;
  33. long time_passed;
  34. tor_gettimeofday(&start);
  35. (*func)(cell, conn);
  36. tor_gettimeofday(&end);
  37. time_passed = tv_udiff(&start, &end) ;
  38. if (time_passed > 10000) { /* more than 10ms */
  39. log_fn(LOG_DEBUG,"That call just took %ld ms.",time_passed/1000);
  40. }
  41. *time += time_passed;
  42. }
  43. /** Process a <b>cell</b> that was just received on <b>conn</b>. Keep internal
  44. * statistics about how many of each cell we've processed so far
  45. * this second, and the total number of microseconds it took to
  46. * process each type of cell.
  47. */
  48. void command_process_cell(cell_t *cell, connection_t *conn) {
  49. /* how many of each cell have we seen so far this second? needs better
  50. * name. */
  51. static int num_create=0, num_created=0, num_relay=0, num_destroy=0;
  52. /* how long has it taken to process each type of cell? */
  53. static int create_time=0, created_time=0, relay_time=0, destroy_time=0;
  54. static time_t current_second = 0; /* from previous calls to time */
  55. time_t now = time(NULL);
  56. if(now > current_second) { /* the second has rolled over */
  57. /* print stats */
  58. log(LOG_INFO,"At end of second: %d creates (%d ms), %d createds (%d ms), %d relays (%d ms), %d destroys (%d ms)",
  59. num_create, create_time/1000,
  60. num_created, created_time/1000,
  61. num_relay, relay_time/1000,
  62. num_destroy, destroy_time/1000);
  63. /* zero out stats */
  64. num_create = num_created = num_relay = num_destroy = 0;
  65. create_time = created_time = relay_time = destroy_time = 0;
  66. /* remember which second it is, for next time */
  67. current_second = now;
  68. }
  69. switch(cell->command) {
  70. case CELL_PADDING:
  71. ++stats_n_padding_cells_processed;
  72. /* do nothing */
  73. break;
  74. case CELL_CREATE:
  75. ++stats_n_create_cells_processed;
  76. ++num_create;
  77. command_time_process_cell(cell, conn, &create_time,
  78. command_process_create_cell);
  79. break;
  80. case CELL_CREATED:
  81. ++stats_n_created_cells_processed;
  82. ++num_created;
  83. command_time_process_cell(cell, conn, &created_time,
  84. command_process_created_cell);
  85. break;
  86. case CELL_RELAY:
  87. ++stats_n_relay_cells_processed;
  88. ++num_relay;
  89. command_time_process_cell(cell, conn, &relay_time,
  90. command_process_relay_cell);
  91. break;
  92. case CELL_DESTROY:
  93. ++stats_n_destroy_cells_processed;
  94. ++num_destroy;
  95. command_time_process_cell(cell, conn, &destroy_time,
  96. command_process_destroy_cell);
  97. break;
  98. default:
  99. log_fn(LOG_WARN,"Cell of unknown type (%d) received. Dropping.", cell->command);
  100. break;
  101. }
  102. }
  103. /** Process a 'create' <b>cell</b> that just arrived from <b>conn</b>. Make a new circuit
  104. * with the p_circ_id specified in cell. Put the circuit in state
  105. * onionskin_pending, and pass the onionskin to the cpuworker. Circ will
  106. * get picked up again when the cpuworker finishes decrypting it.
  107. */
  108. static void command_process_create_cell(cell_t *cell, connection_t *conn) {
  109. circuit_t *circ;
  110. if(we_are_hibernating()) {
  111. log_fn(LOG_INFO,"Received create cell but we're shutting down. Sending back destroy.");
  112. connection_send_destroy(cell->circ_id, conn);
  113. return;
  114. }
  115. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  116. if(circ) {
  117. log_fn(LOG_WARN,"received CREATE cell (circID %d) for known circ. Dropping.", cell->circ_id);
  118. return;
  119. }
  120. circ = circuit_new(cell->circ_id, conn);
  121. circ->state = CIRCUIT_STATE_ONIONSKIN_PENDING;
  122. circ->purpose = CIRCUIT_PURPOSE_OR;
  123. memcpy(circ->onionskin, cell->payload, ONIONSKIN_CHALLENGE_LEN);
  124. /* hand it off to the cpuworkers, and then return */
  125. if(assign_to_cpuworker(NULL, CPUWORKER_TASK_ONION, circ) < 0) {
  126. log_fn(LOG_WARN,"Failed to hand off onionskin. Closing.");
  127. circuit_mark_for_close(circ);
  128. return;
  129. }
  130. log_fn(LOG_DEBUG,"success: handed off onionskin.");
  131. }
  132. /** Process a 'created' <b>cell</b> that just arrived from <b>conn</b>. Find the circuit
  133. * that it's intended for. If we're not the origin of the circuit, package
  134. * the 'created' cell in an 'extended' relay cell and pass it back. If we
  135. * are the origin of the circuit, send it to circuit_finish_handshake() to
  136. * finish processing keys, and then call circuit_send_next_onion_skin() to
  137. * extend to the next hop in the circuit if necessary.
  138. */
  139. static void command_process_created_cell(cell_t *cell, connection_t *conn) {
  140. circuit_t *circ;
  141. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  142. if(!circ) {
  143. log_fn(LOG_INFO,"(circID %d) unknown circ (probably got a destroy earlier). Dropping.", cell->circ_id);
  144. return;
  145. }
  146. if(circ->n_circ_id != cell->circ_id) {
  147. log_fn(LOG_WARN,"got created cell from OPward? Closing.");
  148. circuit_mark_for_close(circ);
  149. return;
  150. }
  151. if(CIRCUIT_IS_ORIGIN(circ)) { /* we're the OP. Handshake this. */
  152. log_fn(LOG_DEBUG,"at OP. Finishing handshake.");
  153. if(circuit_finish_handshake(circ, cell->payload) < 0) {
  154. log_fn(LOG_WARN,"circuit_finish_handshake failed.");
  155. circuit_mark_for_close(circ);
  156. return;
  157. }
  158. log_fn(LOG_DEBUG,"Moving to next skin.");
  159. if(circuit_send_next_onion_skin(circ) < 0) {
  160. log_fn(LOG_INFO,"circuit_send_next_onion_skin failed.");
  161. circuit_mark_for_close(circ); /* XXX push this circuit_close lower */
  162. return;
  163. }
  164. } else { /* pack it into an extended relay cell, and send it. */
  165. log_fn(LOG_DEBUG,"Converting created cell to extended relay cell, sending.");
  166. connection_edge_send_command(NULL, circ, RELAY_COMMAND_EXTENDED,
  167. cell->payload, ONIONSKIN_REPLY_LEN, NULL);
  168. }
  169. }
  170. /** Process a 'relay' <b>cell</b> that just arrived from <b>conn</b>. Make sure
  171. * it came in with a recognized circ_id. Pass it on to
  172. * circuit_receive_relay_cell() for actual processing.
  173. */
  174. static void command_process_relay_cell(cell_t *cell, connection_t *conn) {
  175. circuit_t *circ;
  176. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  177. if(!circ) {
  178. log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
  179. cell->circ_id, conn->address, conn->port);
  180. return;
  181. }
  182. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  183. log_fn(LOG_WARN,"circuit in create_wait. Closing.");
  184. circuit_mark_for_close(circ);
  185. return;
  186. }
  187. if(cell->circ_id == circ->p_circ_id) { /* it's an outgoing cell */
  188. if(circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
  189. log_fn(LOG_WARN,"circuit_receive_relay_cell (forward) failed. Closing.");
  190. circuit_mark_for_close(circ);
  191. return;
  192. }
  193. } else { /* it's an ingoing cell */
  194. if(circuit_receive_relay_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
  195. log_fn(LOG_WARN,"circuit_receive_relay_cell (backward) failed. Closing.");
  196. circuit_mark_for_close(circ);
  197. return;
  198. }
  199. }
  200. }
  201. /** Process a 'destroy' <b>cell</b> that just arrived from
  202. * <b>conn</b>. Find the circ that it refers to (if any).
  203. *
  204. * If the circ is in state
  205. * onionskin_pending, then call onion_pending_remove() to remove it
  206. * from the pending onion list (note that if it's already being
  207. * processed by the cpuworker, it won't be in the list anymore; but
  208. * when the cpuworker returns it, the circuit will be gone, and the
  209. * cpuworker response will be dropped).
  210. *
  211. * Then mark the circuit for close (which marks all edges for close,
  212. * and passes the destroy cell onward if necessary).
  213. */
  214. static void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  215. circuit_t *circ;
  216. circ = circuit_get_by_circ_id_conn(cell->circ_id, conn);
  217. if(!circ) {
  218. log_fn(LOG_INFO,"unknown circuit %d on connection to %s:%d. Dropping.",
  219. cell->circ_id, conn->address, conn->port);
  220. return;
  221. }
  222. log_fn(LOG_INFO,"Received for circID %d.",cell->circ_id);
  223. if(circ->state == CIRCUIT_STATE_ONIONSKIN_PENDING) {
  224. onion_pending_remove(circ);
  225. }
  226. if(cell->circ_id == circ->p_circ_id) {
  227. /* the destroy came from behind */
  228. circ->p_conn = NULL;
  229. circuit_mark_for_close(circ);
  230. } else { /* the destroy came from ahead */
  231. circ->n_conn = NULL;
  232. #if 0
  233. if(!CIRCUIT_IS_ORIGIN(circ)) {
  234. log_fn(LOG_DEBUG, "Delivering 'truncated' back.");
  235. connection_edge_send_command(NULL, circ, RELAY_COMMAND_TRUNCATED,
  236. NULL, 0, NULL);
  237. }
  238. #endif
  239. circuit_mark_for_close(circ);
  240. }
  241. }
  242. /*
  243. Local Variables:
  244. mode:c
  245. indent-tabs-mode:nil
  246. c-basic-offset:2
  247. End:
  248. */