command.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /* Copyright 2001,2002 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. void command_time_process_cell(cell_t *cell, connection_t *conn,
  7. int *num, int *time,
  8. void (*func)(cell_t *, connection_t *)) {
  9. struct timeval start, end;
  10. int time_passed;
  11. *num += 1;
  12. if(gettimeofday(&start,NULL) < 0) {
  13. log(LOG_ERR,"command_time_process_cell(): gettimeofday failed.");
  14. return;
  15. }
  16. (*func)(cell, conn);
  17. if(gettimeofday(&end,NULL) < 0) {
  18. log(LOG_ERR,"command_time_process_cell(): gettimeofday failed.");
  19. return;
  20. }
  21. if(end.tv_usec < start.tv_usec) {
  22. end.tv_sec--;
  23. end.tv_usec += 1000000;
  24. }
  25. time_passed = ((end.tv_sec - start.tv_sec)*1000000) + (end.tv_usec - start.tv_usec);
  26. if(time_passed > 5000) { /* more than 5ms */
  27. log(LOG_INFO,"command_time_process_cell(): That call just took %d ms.",time_passed/1000);
  28. }
  29. *time += time_passed;
  30. }
  31. void command_process_cell(cell_t *cell, connection_t *conn) {
  32. static int num_create=0, num_data=0, num_destroy=0, num_sendme=0;
  33. static int create_time=0, data_time=0, destroy_time=0, sendme_time=0;
  34. static long current_second = 0; /* from previous calls to gettimeofday */
  35. struct timeval now;
  36. if(gettimeofday(&now,NULL) < 0) {
  37. log(LOG_ERR,"command_process_cell(): gettimeofday failed.");
  38. return;
  39. }
  40. if(now.tv_sec > current_second) { /* the second has rolled over */
  41. /* print stats */
  42. log(LOG_INFO,"At end of second:");
  43. log(LOG_INFO,"Create: %d (%d ms)", num_create, create_time/1000);
  44. log(LOG_INFO,"Data: %d (%d ms)", num_data, data_time/1000);
  45. log(LOG_INFO,"Destroy: %d (%d ms)", num_destroy, destroy_time/1000);
  46. log(LOG_INFO,"Sendme: %d (%d ms)", num_sendme, sendme_time/1000);
  47. /* zero out stats */
  48. num_create = num_data = num_destroy = num_sendme = 0;
  49. create_time = data_time = destroy_time = sendme_time = 0;
  50. /* remember which second it is, for next time */
  51. current_second = now.tv_sec;
  52. }
  53. switch(cell->command) {
  54. case CELL_PADDING:
  55. /* do nothing */
  56. break;
  57. case CELL_CREATE:
  58. command_time_process_cell(cell, conn, &num_create, &create_time,
  59. command_process_create_cell);
  60. break;
  61. case CELL_DATA:
  62. command_time_process_cell(cell, conn, &num_data, &data_time,
  63. command_process_data_cell);
  64. break;
  65. case CELL_DESTROY:
  66. command_time_process_cell(cell, conn, &num_destroy, &destroy_time,
  67. command_process_destroy_cell);
  68. break;
  69. case CELL_SENDME:
  70. command_time_process_cell(cell, conn, &num_sendme, &sendme_time,
  71. command_process_sendme_cell);
  72. break;
  73. default:
  74. log(LOG_DEBUG,"Cell of unknown type (%d) received. Dropping.", cell->command);
  75. break;
  76. }
  77. }
  78. 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 && circ->state != CIRCUIT_STATE_ONION_WAIT) {
  82. log(LOG_DEBUG,"command_process_create_cell(): received CREATE cell, not in onion_wait. Dropping.");
  83. return;
  84. }
  85. if(!circ) { /* if it's not there, create it */
  86. circ = circuit_new(cell->aci, conn);
  87. circ->state = CIRCUIT_STATE_ONION_WAIT;
  88. circ->onionlen = ntohl(*(int*)cell->payload);
  89. log(LOG_DEBUG,"command_process_create_cell(): Onion length is %u.",circ->onionlen);
  90. if(circ->onionlen > 50000 || circ->onionlen < 1) { /* too big or too small */
  91. log(LOG_DEBUG,"That's ludicrous. Closing.");
  92. circuit_close(circ);
  93. return;
  94. }
  95. circ->onion = malloc(circ->onionlen);
  96. if(!circ->onion) {
  97. log(LOG_DEBUG,"command_process_create_cell(): Out of memory. Closing.");
  98. circuit_close(circ);
  99. return;
  100. }
  101. if(circ->onionlen < cell->length-4) { /* protect from buffer overflow */
  102. log(LOG_DEBUG,"command_process_create_cell(): Onion too small. Closing.");
  103. circuit_close(circ);
  104. return;
  105. }
  106. memcpy((void *)circ->onion,(void *)(cell->payload+4),cell->length-4);
  107. circ->recvlen = cell->length-4;
  108. log(LOG_DEBUG,"command_process_create_cell(): Primary create cell handled, have received %d of %d onion bytes.",
  109. circ->recvlen,circ->onionlen);
  110. } else { /* pull over as much of the onion as we can */
  111. if(cell->length + circ->recvlen > circ->onionlen) { /* protect from buffer overflow */
  112. log(LOG_DEBUG,"command_process_create_cell(): payload too big for onion. Closing.");
  113. circuit_close(circ);
  114. return;
  115. }
  116. memcpy((void *)(circ->onion+circ->recvlen),(void *)cell->payload,cell->length);
  117. circ->recvlen += cell->length;
  118. log(LOG_DEBUG,"command_process_create_cell(): Secondary create cell handled, have received %d of %d onion bytes (aci %d)",
  119. circ->recvlen,circ->onionlen,circ->p_aci);
  120. }
  121. if(circ->recvlen != circ->onionlen) {
  122. log(LOG_DEBUG,"command_process_create_cell(): Onion not all here yet. Ok.");
  123. return;
  124. }
  125. /* add it to the pending onions queue, and then return */
  126. circ->state = CIRCUIT_STATE_ONION_PENDING;
  127. if(onion_pending_add(circ) < 0) {
  128. log(LOG_DEBUG,"command_process_create_cell(): Failed to queue onion. Closing.");
  129. circuit_close(circ);
  130. }
  131. return;
  132. }
  133. void command_process_sendme_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(LOG_DEBUG,"command_process_sendme_cell(): unknown circuit %d. Dropping.", cell->aci);
  138. return;
  139. }
  140. if(circ->state == CIRCUIT_STATE_ONION_WAIT) {
  141. log(LOG_DEBUG,"command_process_sendme_cell(): circuit in onion_wait. Dropping.");
  142. return;
  143. }
  144. if(circ->state == CIRCUIT_STATE_OR_WAIT) {
  145. log(LOG_DEBUG,"command_process_sendme_cell(): circuit in or_wait. Dropping.");
  146. return;
  147. }
  148. /* at this point both circ->n_conn and circ->p_conn are guaranteed to be set */
  149. if(cell->length != CIRCWINDOW_INCREMENT) {
  150. log(LOG_WARNING,"command_process_sendme_cell(): non-standard sendme value %d.",cell->length);
  151. }
  152. if(cell->aci == circ->p_aci) { /* it's an outgoing cell */
  153. circ->n_receive_circwindow += cell->length;
  154. assert(circ->n_receive_circwindow <= CIRCWINDOW_START);
  155. log(LOG_DEBUG,"command_process_sendme_cell(): n_receive_circwindow for aci %d is %d.",circ->n_aci,circ->n_receive_circwindow);
  156. if(!circ->n_conn || circ->n_conn->type == CONN_TYPE_EXIT) {
  157. circuit_resume_edge_reading(circ, EDGE_EXIT);
  158. } else {
  159. cell->aci = circ->n_aci; /* switch it */
  160. if(connection_write_cell_to_buf(cell, circ->n_conn) < 0) {
  161. circuit_close(circ);
  162. return;
  163. }
  164. }
  165. } else { /* it's an ingoing cell */
  166. assert(cell->aci == circ->n_aci);
  167. circ->p_receive_circwindow += cell->length;
  168. log(LOG_DEBUG,"command_process_sendme_cell(): p_receive_circwindow for aci %d is %d.",circ->p_aci,circ->p_receive_circwindow);
  169. assert(circ->p_receive_circwindow <= CIRCWINDOW_START);
  170. if(!circ->p_conn || circ->p_conn->type == CONN_TYPE_AP) {
  171. circuit_resume_edge_reading(circ, EDGE_AP);
  172. } else {
  173. cell->aci = circ->p_aci; /* switch it */
  174. if(connection_write_cell_to_buf(cell, circ->p_conn) < 0) {
  175. circuit_close(circ);
  176. return;
  177. }
  178. }
  179. }
  180. }
  181. void command_process_data_cell(cell_t *cell, connection_t *conn) {
  182. circuit_t *circ;
  183. circ = circuit_get_by_aci_conn(cell->aci, conn);
  184. if(!circ) {
  185. log(LOG_DEBUG,"command_process_data_cell(): unknown circuit %d. Dropping.", cell->aci);
  186. return;
  187. }
  188. if(circ->state == CIRCUIT_STATE_ONION_PENDING) {
  189. log(LOG_DEBUG,"command_process_data_cell(): circuit in create_wait. Queueing data cell.");
  190. onion_pending_data_add(circ, cell);
  191. return;
  192. }
  193. if(cell->aci == circ->p_aci) { /* it's an outgoing cell */
  194. if(--circ->p_receive_circwindow < 0) { /* is it less than 0 after decrement? */
  195. log(LOG_INFO,"connection_process_data_cell(): Too many data cells for out circuit (aci %d). Closing.", circ->p_aci);
  196. circuit_close(circ);
  197. return;
  198. }
  199. log(LOG_DEBUG,"connection_process_data_cell(): p_receive_circwindow for aci %d is %d.",circ->p_aci,circ->p_receive_circwindow);
  200. }
  201. if(cell->aci == circ->n_aci) { /* it's an ingoing cell */
  202. if(--circ->n_receive_circwindow < 0) { /* is it less than 0 after decrement? */
  203. log(LOG_INFO,"connection_process_data_cell(): Too many data cells for in circuit (aci %d). Closing.", circ->n_aci);
  204. circuit_close(circ);
  205. return;
  206. }
  207. log(LOG_DEBUG,"connection_process_data_cell(): n_receive_circwindow for aci %d is %d.",circ->n_aci,circ->n_receive_circwindow);
  208. }
  209. if(circ->state == CIRCUIT_STATE_ONION_WAIT) {
  210. log(LOG_WARNING,"command_process_data_cell(): circuit in onion_wait. Dropping data cell.");
  211. return;
  212. }
  213. if(circ->state == CIRCUIT_STATE_OR_WAIT) {
  214. log(LOG_WARNING,"command_process_data_cell(): circuit in or_wait. Dropping data cell.");
  215. return;
  216. }
  217. /* circ->p_conn and n_conn are only null if we're at an edge point with no connections yet */
  218. if(cell->aci == circ->p_aci) { /* it's an outgoing cell */
  219. cell->aci = circ->n_aci; /* switch it */
  220. if(circuit_deliver_data_cell(cell, circ, CELL_DIRECTION_OUT) < 0) {
  221. log(LOG_INFO,"command_process_data_cell(): circuit_deliver_data_cell (forward) failed. Closing.");
  222. circuit_close(circ);
  223. return;
  224. }
  225. } else { /* it's an ingoing cell */
  226. cell->aci = circ->p_aci; /* switch it */
  227. if(circuit_deliver_data_cell(cell, circ, CELL_DIRECTION_IN) < 0) {
  228. log(LOG_DEBUG,"command_process_data_cell(): circuit_deliver_data_cell (backward) failed. Closing.");
  229. circuit_close(circ);
  230. return;
  231. }
  232. }
  233. }
  234. void command_process_destroy_cell(cell_t *cell, connection_t *conn) {
  235. circuit_t *circ;
  236. connection_t *tmpconn;
  237. circ = circuit_get_by_aci_conn(cell->aci, conn);
  238. if(!circ) {
  239. log(LOG_DEBUG,"command_process_destroy_cell(): unknown circuit %d. Dropping.", cell->aci);
  240. return;
  241. }
  242. log(LOG_DEBUG,"command_process_destroy_cell(): Received for aci %d.",cell->aci);
  243. if(circ->state == CIRCUIT_STATE_ONION_PENDING) {
  244. onion_pending_remove(circ);
  245. }
  246. circuit_remove(circ);
  247. if(cell->aci == circ->p_aci) { /* the destroy came from behind */
  248. for(tmpconn = circ->n_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  249. connection_send_destroy(circ->n_aci, tmpconn);
  250. }
  251. }
  252. if(cell->aci == circ->n_aci) { /* the destroy came from ahead */
  253. for(tmpconn = circ->p_conn; tmpconn; tmpconn=tmpconn->next_topic) {
  254. connection_send_destroy(circ->p_aci, tmpconn);
  255. }
  256. }
  257. circuit_free(circ);
  258. }