command.c 9.9 KB

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