directory.c 8.8 KB

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