directory.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /* Copyright 2001,2002,2003 Roger Dingledine. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. static int directory_send_command(connection_t *conn, int command);
  6. static int directory_handle_command(connection_t *conn);
  7. /********* START VARIABLES **********/
  8. extern or_options_t options; /* command-line and config-file options */
  9. static char fetchstring[] = "GET / HTTP/1.0\r\n\r\n";
  10. static char answerstring[] = "HTTP/1.0 200 OK\r\n\r\n";
  11. /* XXX the_directory is the same name as a different variable in·
  12. * dirserv.c, are you crazy?? */
  13. static char the_directory[MAX_DIR_SIZE+1];
  14. static int directorylen=0;
  15. /********* END VARIABLES ************/
  16. void directory_initiate_command(routerinfo_t *router, int command) {
  17. connection_t *conn;
  18. if (command == DIR_CONN_STATE_CONNECTING_FETCH)
  19. log_fn(LOG_DEBUG,"initiating directory fetch");
  20. else
  21. log_fn(LOG_DEBUG,"initiating directory upload");
  22. if (!router) { /* i guess they didn't have one in mind for me to use */
  23. log_fn(LOG_WARN,"No running dirservers known. Not trying.");
  24. return;
  25. }
  26. conn = connection_new(CONN_TYPE_DIR);
  27. /* set up conn so it's got all the data we need to remember */
  28. conn->addr = router->addr;
  29. conn->port = router->dir_port;
  30. conn->address = tor_strdup(router->address);
  31. conn->nickname = tor_strdup(router->nickname);
  32. assert(router->identity_pkey);
  33. conn->identity_pkey = crypto_pk_dup_key(router->identity_pkey);
  34. if(connection_add(conn) < 0) { /* no space, forget it */
  35. connection_free(conn);
  36. return;
  37. }
  38. switch(connection_connect(conn, router->address, router->addr, router->dir_port)) {
  39. case -1:
  40. router_mark_as_down(conn->nickname); /* don't try him again */
  41. connection_remove(conn);
  42. connection_free(conn);
  43. return;
  44. case 0:
  45. connection_set_poll_socket(conn);
  46. connection_watch_events(conn, POLLIN | POLLOUT | POLLERR);
  47. /* writable indicates finish, readable indicates broken link,
  48. error indicates broken link in windowsland. */
  49. conn->state = command;
  50. return;
  51. /* case 1: fall through */
  52. }
  53. connection_set_poll_socket(conn);
  54. if(directory_send_command(conn, command) < 0) {
  55. connection_remove(conn);
  56. connection_free(conn);
  57. }
  58. }
  59. static int directory_send_command(connection_t *conn, int command) {
  60. const char *s;
  61. char tmp[8192];
  62. assert(conn && conn->type == CONN_TYPE_DIR);
  63. switch(command) {
  64. case DIR_CONN_STATE_CONNECTING_FETCH:
  65. connection_write_to_buf(fetchstring, strlen(fetchstring), conn);
  66. conn->state = DIR_CONN_STATE_CLIENT_SENDING_FETCH;
  67. break;
  68. case DIR_CONN_STATE_CONNECTING_UPLOAD:
  69. s = router_get_my_descriptor();
  70. if(!s) {
  71. log_fn(LOG_WARN,"Failed to get my descriptor.");
  72. return -1;
  73. }
  74. snprintf(tmp, sizeof(tmp), "POST / HTTP/1.0\r\nContent-Length: %d\r\n\r\n%s",
  75. strlen(s), s);
  76. connection_write_to_buf(tmp, strlen(tmp), conn);
  77. conn->state = DIR_CONN_STATE_CLIENT_SENDING_UPLOAD;
  78. break;
  79. }
  80. return 0;
  81. }
  82. int connection_dir_process_inbuf(connection_t *conn) {
  83. assert(conn && conn->type == CONN_TYPE_DIR);
  84. if(conn->inbuf_reached_eof) {
  85. switch(conn->state) {
  86. case DIR_CONN_STATE_CLIENT_READING_FETCH:
  87. /* kill it, but first process the_directory and learn about new routers. */
  88. switch(fetch_from_buf_http(conn->inbuf,
  89. NULL, 0, the_directory, MAX_DIR_SIZE)) {
  90. case -1: /* overflow */
  91. log_fn(LOG_WARN,"'fetch' response too large. Failing.");
  92. return -1;
  93. case 0:
  94. log_fn(LOG_INFO,"'fetch' response not all here, but we're at eof. Closing.");
  95. return -1;
  96. /* case 1, fall through */
  97. }
  98. /* XXX check headers, at least make sure returned 2xx */
  99. directorylen = strlen(the_directory);
  100. log_fn(LOG_INFO,"Received directory (size %d):\n%s", directorylen, the_directory);
  101. if(directorylen == 0) {
  102. log_fn(LOG_INFO,"Empty directory. Ignoring.");
  103. return -1;
  104. }
  105. if(router_set_routerlist_from_directory(the_directory, conn->identity_pkey) < 0){
  106. log_fn(LOG_INFO,"...but parsing failed. Ignoring.");
  107. } else {
  108. log_fn(LOG_INFO,"updated routers.");
  109. }
  110. if(options.ORPort) { /* connect to them all */
  111. router_retry_connections();
  112. }
  113. return -1;
  114. case DIR_CONN_STATE_CLIENT_READING_UPLOAD:
  115. /* XXX make sure there's a 200 OK on the buffer */
  116. log_fn(LOG_INFO,"eof while reading upload response. Finished.");
  117. return -1;
  118. default:
  119. log_fn(LOG_INFO,"conn reached eof, not reading. Closing.");
  120. return -1;
  121. }
  122. }
  123. if(conn->state == DIR_CONN_STATE_SERVER_COMMAND_WAIT)
  124. return directory_handle_command(conn);
  125. /* XXX for READ states, might want to make sure inbuf isn't too big */
  126. log_fn(LOG_DEBUG,"Got data, not eof. Leaving on inbuf.");
  127. return 0;
  128. }
  129. static int directory_handle_command(connection_t *conn) {
  130. char headers[2048];
  131. char body[50000]; /* XXX */
  132. size_t dlen;
  133. const char *cp;
  134. assert(conn && conn->type == CONN_TYPE_DIR);
  135. switch(fetch_from_buf_http(conn->inbuf,
  136. headers, sizeof(headers), body, sizeof(body))) {
  137. case -1: /* overflow */
  138. log_fn(LOG_WARN,"input too large. Failing.");
  139. return -1;
  140. case 0:
  141. log_fn(LOG_DEBUG,"command not all here yet.");
  142. return 0;
  143. /* case 1, fall through */
  144. }
  145. log_fn(LOG_DEBUG,"headers '%s', body '%s'.",headers,body);
  146. if(!strncasecmp(headers,"GET",3)) {
  147. /* XXX should check url and http version */
  148. log_fn(LOG_DEBUG,"Received GET command.");
  149. dlen = dirserv_get_directory(&cp);
  150. if(dlen == 0) {
  151. log_fn(LOG_WARN,"My directory is empty. Closing.");
  152. return -1; /* XXX send some helpful http error code */
  153. }
  154. log_fn(LOG_DEBUG,"Dumping directory to client.");
  155. connection_write_to_buf(answerstring, strlen(answerstring), conn);
  156. connection_write_to_buf(cp, dlen, conn);
  157. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  158. return 0;
  159. }
  160. if(!strncasecmp(headers,"POST",4)) {
  161. /* XXX should check url and http version */
  162. log_fn(LOG_DEBUG,"Received POST command.");
  163. cp = body;
  164. if(dirserv_add_descriptor(&cp) < 0) {
  165. log_fn(LOG_WARN,"dirserv_add_descriptor() failed. Dropping.");
  166. return -1; /* XXX should write an http failed code */
  167. }
  168. dirserv_get_directory(&cp); /* rebuild and write to disk */
  169. connection_write_to_buf(answerstring, strlen(answerstring), conn);
  170. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  171. return 0;
  172. }
  173. log_fn(LOG_WARN,"Got headers with unknown command. Closing.");
  174. return -1;
  175. }
  176. int connection_dir_finished_flushing(connection_t *conn) {
  177. int e, len=sizeof(e);
  178. assert(conn && conn->type == CONN_TYPE_DIR);
  179. switch(conn->state) {
  180. case DIR_CONN_STATE_CONNECTING_FETCH:
  181. case DIR_CONN_STATE_CONNECTING_UPLOAD:
  182. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  183. if(!ERRNO_CONN_EINPROGRESS(errno)) {
  184. log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
  185. router_mark_as_down(conn->nickname); /* don't try him again */
  186. return -1;
  187. } else {
  188. return 0; /* no change, see if next time is better */
  189. }
  190. }
  191. /* the connect has finished. */
  192. log_fn(LOG_INFO,"Dir connection to router %s:%u established.",
  193. conn->address,conn->port);
  194. return directory_send_command(conn, conn->state);
  195. case DIR_CONN_STATE_CLIENT_SENDING_FETCH:
  196. log_fn(LOG_DEBUG,"client finished sending fetch command.");
  197. conn->state = DIR_CONN_STATE_CLIENT_READING_FETCH;
  198. connection_watch_events(conn, POLLIN);
  199. return 0;
  200. case DIR_CONN_STATE_CLIENT_SENDING_UPLOAD:
  201. log_fn(LOG_DEBUG,"client finished sending upload command.");
  202. conn->state = DIR_CONN_STATE_CLIENT_READING_UPLOAD;
  203. connection_watch_events(conn, POLLIN);
  204. return 0;
  205. case DIR_CONN_STATE_SERVER_WRITING:
  206. log_fn(LOG_INFO,"Finished writing server response. Closing.");
  207. return -1; /* kill it */
  208. default:
  209. log_fn(LOG_WARN,"BUG: called in unexpected state.");
  210. return -1;
  211. }
  212. return 0;
  213. }
  214. /*
  215. Local Variables:
  216. mode:c
  217. indent-tabs-mode:nil
  218. c-basic-offset:2
  219. End:
  220. */