directory.c 8.8 KB

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