cpuworker.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /* Copyright 2003 Roger Dingledine. */
  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. #define MAX_CPUWORKERS 16
  7. #define MIN_CPUWORKERS 1
  8. #define TAG_LEN 8
  9. #define LEN_ONION_QUESTION (1+TAG_LEN+ONIONSKIN_CHALLENGE_LEN)
  10. #define LEN_ONION_RESPONSE (1+TAG_LEN+ONIONSKIN_REPLY_LEN+40+32)
  11. int num_cpuworkers=0;
  12. int num_cpuworkers_busy=0;
  13. int cpuworker_main(void *data);
  14. static int spawn_cpuworker(void);
  15. static void spawn_enough_cpuworkers(void);
  16. static void process_pending_task(connection_t *cpuworker);
  17. void cpu_init(void) {
  18. spawn_enough_cpuworkers();
  19. }
  20. int connection_cpu_finished_flushing(connection_t *conn) {
  21. assert(conn && conn->type == CONN_TYPE_CPUWORKER);
  22. connection_stop_writing(conn);
  23. return 0;
  24. }
  25. static void tag_pack(char *tag, uint32_t addr, uint16_t port, uint16_t circ_id) {
  26. *(uint32_t *)tag = addr;
  27. *(uint16_t *)(tag+4) = port;
  28. *(uint16_t *)(tag+6) = circ_id;
  29. }
  30. static void tag_unpack(char *tag, uint32_t *addr, uint16_t *port, uint16_t *circ_id) {
  31. struct in_addr in;
  32. *addr = *(uint32_t *)tag;
  33. *port = *(uint16_t *)(tag+4);
  34. *circ_id = *(uint16_t *)(tag+6);
  35. in.s_addr = htonl(*addr);
  36. log_fn(LOG_DEBUG,"onion was from %s:%d, circ_id %d.", inet_ntoa(in), *port, *circ_id);
  37. }
  38. int connection_cpu_process_inbuf(connection_t *conn) {
  39. char success;
  40. unsigned char buf[LEN_ONION_RESPONSE];
  41. uint32_t addr;
  42. uint16_t port;
  43. uint16_t circ_id;
  44. connection_t *p_conn;
  45. circuit_t *circ;
  46. assert(conn && conn->type == CONN_TYPE_CPUWORKER);
  47. if(conn->inbuf_reached_eof) {
  48. log_fn(LOG_WARN,"Read eof. Worker died unexpectedly.");
  49. if(conn->state != CPUWORKER_STATE_IDLE) {
  50. /* the circ associated with this cpuworker will have to wait until
  51. * it gets culled in run_connection_housekeeping(), since we have
  52. * no way to find out which circ it was. */
  53. log_fn(LOG_WARN,"...and it left a circuit queued; abandoning circ.");
  54. num_cpuworkers_busy--;
  55. }
  56. num_cpuworkers--;
  57. spawn_enough_cpuworkers(); /* try to regrow. hope we don't end up spinning. */
  58. connection_mark_for_close(conn,0);
  59. return 0;
  60. }
  61. if(conn->state == CPUWORKER_STATE_BUSY_ONION) {
  62. if(buf_datalen(conn->inbuf) < LEN_ONION_RESPONSE) /* entire answer available? */
  63. return 0; /* not yet */
  64. assert(buf_datalen(conn->inbuf) == LEN_ONION_RESPONSE);
  65. connection_fetch_from_buf(&success,1,conn);
  66. connection_fetch_from_buf(buf,LEN_ONION_RESPONSE-1,conn);
  67. /* parse out the circ it was talking about */
  68. tag_unpack(buf, &addr, &port, &circ_id);
  69. circ = NULL;
  70. p_conn = connection_exact_get_by_addr_port(addr,port);
  71. if(p_conn)
  72. circ = circuit_get_by_circ_id_conn(circ_id, p_conn);
  73. if(!circ) {
  74. log_fn(LOG_INFO,"processed onion for a circ that's gone. Dropping.");
  75. goto done_processing;
  76. }
  77. assert(circ->p_conn);
  78. if(success == 0) {
  79. log_fn(LOG_WARN,"decoding onionskin failed. Closing.");
  80. circuit_mark_for_close(circ);
  81. goto done_processing;
  82. }
  83. if(onionskin_answer(circ, buf+TAG_LEN, buf+TAG_LEN+ONIONSKIN_REPLY_LEN) < 0) {
  84. log_fn(LOG_WARN,"onionskin_answer failed. Closing.");
  85. circuit_mark_for_close(circ);
  86. goto done_processing;
  87. }
  88. log_fn(LOG_DEBUG,"onionskin_answer succeeded. Yay.");
  89. } else {
  90. assert(0); /* don't ask me to do handshakes yet */
  91. }
  92. done_processing:
  93. conn->state = CPUWORKER_STATE_IDLE;
  94. num_cpuworkers_busy--;
  95. process_pending_task(conn);
  96. return 0;
  97. }
  98. int cpuworker_main(void *data) {
  99. unsigned char question[ONIONSKIN_CHALLENGE_LEN];
  100. unsigned char question_type;
  101. int *fdarray = data;
  102. int fd;
  103. /* variables for onion processing */
  104. unsigned char keys[40+32];
  105. unsigned char reply_to_proxy[ONIONSKIN_REPLY_LEN];
  106. unsigned char buf[LEN_ONION_RESPONSE];
  107. char tag[TAG_LEN];
  108. close(fdarray[0]); /* this is the side of the socketpair the parent uses */
  109. fd = fdarray[1]; /* this side is ours */
  110. #ifndef MS_WINDOWS
  111. connection_free_all(); /* so the child doesn't hold the parent's fd's open */
  112. #endif
  113. for(;;) {
  114. if(recv(fd, &question_type, 1, 0) != 1) {
  115. // log_fn(LOG_ERR,"read type failed. Exiting.");
  116. log_fn(LOG_INFO,"cpuworker exiting because tor process died.");
  117. spawn_exit();
  118. }
  119. assert(question_type == CPUWORKER_TASK_ONION);
  120. if(read_all(fd, tag, TAG_LEN, 1) != TAG_LEN) {
  121. log_fn(LOG_ERR,"read tag failed. Exiting.");
  122. spawn_exit();
  123. }
  124. if(read_all(fd, question, ONIONSKIN_CHALLENGE_LEN, 1) != ONIONSKIN_CHALLENGE_LEN) {
  125. log_fn(LOG_ERR,"read question failed. Exiting.");
  126. spawn_exit();
  127. }
  128. if(question_type == CPUWORKER_TASK_ONION) {
  129. if(onion_skin_server_handshake(question, get_onion_key(),
  130. reply_to_proxy, keys, 40+32) < 0) {
  131. /* failure */
  132. log_fn(LOG_WARN,"onion_skin_server_handshake failed.");
  133. memset(buf,0,LEN_ONION_RESPONSE); /* send all zeros for failure */
  134. } else {
  135. /* success */
  136. log_fn(LOG_INFO,"onion_skin_server_handshake succeeded.");
  137. buf[0] = 1; /* 1 means success */
  138. memcpy(buf+1,tag,TAG_LEN);
  139. memcpy(buf+1+TAG_LEN,reply_to_proxy,ONIONSKIN_REPLY_LEN);
  140. memcpy(buf+1+TAG_LEN+ONIONSKIN_REPLY_LEN,keys,40+32);
  141. }
  142. if(write_all(fd, buf, LEN_ONION_RESPONSE, 1) != LEN_ONION_RESPONSE) {
  143. log_fn(LOG_ERR,"writing response buf failed. Exiting.");
  144. spawn_exit();
  145. }
  146. log_fn(LOG_DEBUG,"finished writing response.");
  147. }
  148. }
  149. return 0; /* windows wants this function to return an int */
  150. }
  151. static int spawn_cpuworker(void) {
  152. int fd[2];
  153. connection_t *conn;
  154. if(tor_socketpair(AF_UNIX, SOCK_STREAM, 0, fd) < 0) {
  155. log(LOG_ERR, "Couldn't construct socketpair: %s", strerror(errno));
  156. exit(1);
  157. }
  158. spawn_func(cpuworker_main, (void*)fd);
  159. log_fn(LOG_DEBUG,"just spawned a worker.");
  160. close(fd[1]); /* we don't need the worker's side of the pipe */
  161. conn = connection_new(CONN_TYPE_CPUWORKER);
  162. set_socket_nonblocking(fd[0]);
  163. /* set up conn so it's got all the data we need to remember */
  164. conn->s = fd[0];
  165. conn->address = tor_strdup("localhost");
  166. if(connection_add(conn) < 0) { /* no space, forget it */
  167. log_fn(LOG_WARN,"connection_add failed. Giving up.");
  168. connection_free(conn); /* this closes fd[0] */
  169. return -1;
  170. }
  171. conn->state = CPUWORKER_STATE_IDLE;
  172. connection_start_reading(conn);
  173. return 0; /* success */
  174. }
  175. static void spawn_enough_cpuworkers(void) {
  176. int num_cpuworkers_needed = options.NumCpus;
  177. if(num_cpuworkers_needed < MIN_CPUWORKERS)
  178. num_cpuworkers_needed = MIN_CPUWORKERS;
  179. if(num_cpuworkers_needed > MAX_CPUWORKERS)
  180. num_cpuworkers_needed = MAX_CPUWORKERS;
  181. while(num_cpuworkers < num_cpuworkers_needed) {
  182. if(spawn_cpuworker() < 0) {
  183. log_fn(LOG_WARN,"spawn failed!");
  184. return;
  185. }
  186. num_cpuworkers++;
  187. }
  188. }
  189. static void process_pending_task(connection_t *cpuworker) {
  190. circuit_t *circ;
  191. assert(cpuworker);
  192. /* for now only process onion tasks */
  193. circ = onion_next_task();
  194. if(!circ)
  195. return;
  196. if(assign_to_cpuworker(cpuworker, CPUWORKER_TASK_ONION, circ) < 0)
  197. log_fn(LOG_WARN,"assign_to_cpuworker failed. Ignoring.");
  198. }
  199. /* if cpuworker is defined, assert that he's idle, and use him. else,
  200. * look for an idle cpuworker and use him. if none idle, queue task onto
  201. * the pending onion list and return.
  202. * If question_type is CPUWORKER_TASK_ONION then task is a circ.
  203. * No other question_types are allowed.
  204. */
  205. int assign_to_cpuworker(connection_t *cpuworker, unsigned char question_type,
  206. void *task) {
  207. circuit_t *circ;
  208. char tag[TAG_LEN];
  209. assert(question_type == CPUWORKER_TASK_ONION);
  210. if(question_type == CPUWORKER_TASK_ONION) {
  211. circ = task;
  212. if(num_cpuworkers_busy == num_cpuworkers) {
  213. log_fn(LOG_DEBUG,"No idle cpuworkers. Queuing.");
  214. if(onion_pending_add(circ) < 0)
  215. return -1;
  216. return 0;
  217. }
  218. if(!cpuworker)
  219. cpuworker = connection_get_by_type_state(CONN_TYPE_CPUWORKER, CPUWORKER_STATE_IDLE);
  220. assert(cpuworker);
  221. if(!circ->p_conn) {
  222. log_fn(LOG_INFO,"circ->p_conn gone. Failing circ.");
  223. return -1;
  224. }
  225. tag_pack(tag, circ->p_conn->addr, circ->p_conn->port, circ->p_circ_id);
  226. cpuworker->state = CPUWORKER_STATE_BUSY_ONION;
  227. num_cpuworkers_busy++;
  228. connection_write_to_buf(&question_type, 1, cpuworker);
  229. connection_write_to_buf(tag, sizeof(tag), cpuworker);
  230. connection_write_to_buf(circ->onionskin, ONIONSKIN_CHALLENGE_LEN, cpuworker);
  231. }
  232. return 0;
  233. }
  234. /*
  235. Local Variables:
  236. mode:c
  237. indent-tabs-mode:nil
  238. c-basic-offset:2
  239. End:
  240. */