rendclient.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Copyright 2004 Roger Dingledine */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include "or.h"
  5. /* send the introduce cell */
  6. void
  7. rend_client_introcirc_is_ready(connection_t *apconn, circuit_t *circ)
  8. {
  9. log_fn(LOG_WARN,"introcirc is ready");
  10. }
  11. int
  12. rend_client_send_establish_rendezvous(circuit_t *circ)
  13. {
  14. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  15. log_fn(LOG_INFO, "Sending an ESTABLISH_RENDEZVOUS cell");
  16. if (crypto_rand(REND_COOKIE_LEN, circ->rend_cookie)<0) {
  17. log_fn(LOG_WARN, "Couldn't get random cookie");
  18. return -1;
  19. }
  20. if (connection_edge_send_command(NULL,circ,
  21. RELAY_COMMAND_ESTABLISH_RENDEZVOUS,
  22. circ->rend_cookie, REND_COOKIE_LEN,
  23. circ->cpath->prev)<0) {
  24. log_fn(LOG_WARN, "Couldn't send ESTABLISH_RENDEZVOUS cell");
  25. return -1;
  26. }
  27. return 0;
  28. }
  29. /* send the rendezvous cell */
  30. void
  31. rend_client_rendcirc_is_ready(connection_t *apconn, circuit_t *circ)
  32. {
  33. circuit_t *introcirc;
  34. assert(apconn->purpose == AP_PURPOSE_RENDPOINT_WAIT);
  35. assert(circ->purpose == CIRCUIT_PURPOSE_C_ESTABLISH_REND);
  36. assert(circ->cpath);
  37. log_fn(LOG_INFO,"rendcirc is ready");
  38. /* XXX generate a rendezvous cookie, store it in circ */
  39. /* store rendcirc in apconn */
  40. apconn->purpose = AP_PURPOSE_INTROPOINT_WAIT;
  41. if (connection_ap_handshake_attach_circuit(apconn) < 0) {
  42. log_fn(LOG_WARN,"failed to start intro point. Closing conn.");
  43. connection_mark_for_close(apconn,0);
  44. }
  45. }
  46. /* bob sent us a rendezvous cell, join the circs. */
  47. void
  48. rend_client_rendezvous(connection_t *apconn, circuit_t *circ)
  49. {
  50. }
  51. /* Find all the apconns in purpose AP_PURPOSE_RENDDESC_WAIT that
  52. * are waiting on query. If success==1, move them to the next state.
  53. * If success==0, fail them.
  54. */
  55. void rend_client_desc_fetched(char *query, int success) {
  56. connection_t **carray;
  57. connection_t *conn;
  58. int n, i;
  59. get_connection_array(&carray, &n);
  60. for (i = 0; i < n; ++i) {
  61. conn = carray[i];
  62. if (conn->type != CONN_TYPE_AP ||
  63. conn->state != AP_CONN_STATE_CIRCUIT_WAIT)
  64. continue;
  65. if (conn->purpose != AP_PURPOSE_RENDDESC_WAIT)
  66. continue;
  67. if (rend_cmp_service_ids(conn->rend_query, query))
  68. continue;
  69. /* great, this guy was waiting */
  70. if(success) {
  71. log_fn(LOG_INFO,"Rend desc retrieved. Launching rend circ.");
  72. conn->purpose = AP_PURPOSE_RENDPOINT_WAIT;
  73. if (connection_ap_handshake_attach_circuit(conn) < 0) {
  74. /* it will never work */
  75. log_fn(LOG_WARN,"attaching to a rend circ failed. Closing conn.");
  76. connection_mark_for_close(conn,0);
  77. }
  78. } else { /* 404 */
  79. log_fn(LOG_WARN,"service id '%s' not found. Closing conn.", query);
  80. connection_mark_for_close(conn,0);
  81. }
  82. }
  83. }
  84. int rend_cmp_service_ids(char *one, char *two) {
  85. return strcasecmp(one,two);
  86. }
  87. /* If address is of the form "y.onion" with a well-formed handle y,
  88. * then put a '\0' after y, lower-case it, and return 0.
  89. * Else return -1 and change nothing.
  90. */
  91. int rend_parse_rendezvous_address(char *address) {
  92. char *s;
  93. char query[REND_SERVICE_ID_LEN+1];
  94. s = strrchr(address,'.');
  95. if(!s) return -1; /* no dot */
  96. if (strcasecmp(s+1,"onion"))
  97. return -1; /* not .onion */
  98. *s = 0; /* null terminate it */
  99. if(strlcpy(query, address, REND_SERVICE_ID_LEN+1) >= REND_SERVICE_ID_LEN+1)
  100. goto failed;
  101. tor_strlower(query);
  102. if(rend_valid_service_id(query)) {
  103. tor_strlower(address);
  104. return 0; /* success */
  105. }
  106. failed:
  107. /* otherwise, return to previous state and return -1 */
  108. *s = '.';
  109. return -1;
  110. }
  111. /*
  112. Local Variables:
  113. mode:c
  114. indent-tabs-mode:nil
  115. c-basic-offset:2
  116. End:
  117. */