cpuworker.c 9.4 KB

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