directory.c 8.1 KB

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