directory.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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,"and got an %s directory; updated routers.",
  107. conn->identity_pkey ? "authenticated" : "unauthenticated");
  108. }
  109. if(options.OnionRouter) { /* connect to them all */
  110. router_retry_connections();
  111. }
  112. return -1;
  113. case DIR_CONN_STATE_CLIENT_READING_UPLOAD:
  114. /* XXX make sure there's a 200 OK on the buffer */
  115. log_fn(LOG_INFO,"eof while reading upload response. Finished.");
  116. return -1;
  117. default:
  118. log_fn(LOG_INFO,"conn reached eof, not reading. Closing.");
  119. return -1;
  120. }
  121. }
  122. if(conn->state == DIR_CONN_STATE_SERVER_COMMAND_WAIT)
  123. return directory_handle_command(conn);
  124. /* XXX for READ states, might want to make sure inbuf isn't too big */
  125. log_fn(LOG_DEBUG,"Got data, not eof. Leaving on inbuf.");
  126. return 0;
  127. }
  128. static int directory_handle_command(connection_t *conn) {
  129. char headers[1024];
  130. char body[50000]; /* XXX */
  131. size_t dlen;
  132. const char *cp;
  133. assert(conn && conn->type == CONN_TYPE_DIR);
  134. switch(fetch_from_buf_http(conn->inbuf,
  135. headers, sizeof(headers), body, sizeof(body))) {
  136. case -1: /* overflow */
  137. log_fn(LOG_WARN,"input too large. Failing.");
  138. return -1;
  139. case 0:
  140. log_fn(LOG_DEBUG,"command not all here yet.");
  141. return 0;
  142. /* case 1, fall through */
  143. }
  144. log_fn(LOG_DEBUG,"headers '%s', body '%s'.",headers,body);
  145. if(!strncasecmp(headers,"GET",3)) {
  146. /* XXX should check url and http version */
  147. log_fn(LOG_DEBUG,"Received GET command.");
  148. dlen = dirserv_get_directory(&cp);
  149. if(dlen == 0) {
  150. log_fn(LOG_WARN,"My directory is empty. Closing.");
  151. return -1; /* XXX send some helpful http error code */
  152. }
  153. log_fn(LOG_DEBUG,"Dumping directory to client.");
  154. connection_write_to_buf(answerstring, strlen(answerstring), conn);
  155. connection_write_to_buf(cp, dlen, conn);
  156. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  157. return 0;
  158. }
  159. if(!strncasecmp(headers,"POST",4)) {
  160. /* XXX should check url and http version */
  161. log_fn(LOG_DEBUG,"Received POST command.");
  162. cp = body;
  163. if(dirserv_add_descriptor(&cp) < 0) {
  164. log_fn(LOG_WARN,"dirserv_add_descriptor() failed. Dropping.");
  165. return -1; /* XXX should write an http failed code */
  166. }
  167. dirserv_get_directory(&cp); /* rebuild and write to disk */
  168. connection_write_to_buf(answerstring, strlen(answerstring), conn);
  169. conn->state = DIR_CONN_STATE_SERVER_WRITING;
  170. return 0;
  171. }
  172. log_fn(LOG_WARN,"Got headers with unknown command. Closing.");
  173. return -1;
  174. }
  175. int connection_dir_finished_flushing(connection_t *conn) {
  176. int e, len=sizeof(e);
  177. assert(conn && conn->type == CONN_TYPE_DIR);
  178. switch(conn->state) {
  179. case DIR_CONN_STATE_CONNECTING_FETCH:
  180. case DIR_CONN_STATE_CONNECTING_UPLOAD:
  181. if (getsockopt(conn->s, SOL_SOCKET, SO_ERROR, (void*)&e, &len) < 0) { /* not yet */
  182. if(!ERRNO_CONN_EINPROGRESS(errno)) {
  183. log_fn(LOG_DEBUG,"in-progress connect failed. Removing.");
  184. router_mark_as_down(conn->nickname); /* don't try him again */
  185. return -1;
  186. } else {
  187. return 0; /* no change, see if next time is better */
  188. }
  189. }
  190. /* the connect has finished. */
  191. log_fn(LOG_INFO,"Dir connection to router %s:%u established.",
  192. conn->address,conn->port);
  193. return directory_send_command(conn, conn->state);
  194. case DIR_CONN_STATE_CLIENT_SENDING_FETCH:
  195. log_fn(LOG_DEBUG,"client finished sending fetch command.");
  196. conn->state = DIR_CONN_STATE_CLIENT_READING_FETCH;
  197. connection_watch_events(conn, POLLIN);
  198. return 0;
  199. case DIR_CONN_STATE_CLIENT_SENDING_UPLOAD:
  200. log_fn(LOG_DEBUG,"client finished sending upload command.");
  201. conn->state = DIR_CONN_STATE_CLIENT_READING_UPLOAD;
  202. connection_watch_events(conn, POLLIN);
  203. return 0;
  204. case DIR_CONN_STATE_SERVER_WRITING:
  205. log_fn(LOG_INFO,"Finished writing server response. Closing.");
  206. return -1; /* kill it */
  207. default:
  208. log_fn(LOG_WARN,"BUG: called in unexpected state.");
  209. return -1;
  210. }
  211. return 0;
  212. }
  213. /*
  214. Local Variables:
  215. mode:c
  216. indent-tabs-mode:nil
  217. c-basic-offset:2
  218. End:
  219. */