cpuworker.c 7.7 KB

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