2007-02-12 22:39:53 +01:00
|
|
|
/* Copyright 2004-2007 Roger Dingledine, Nick Mathewson. */
|
2004-03-31 04:07:38 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
2005-12-14 21:40:40 +01:00
|
|
|
const char rendcommon_c_id[] =
|
|
|
|
"$Id$";
|
2004-03-31 04:07:38 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/**
|
|
|
|
* \file rendcommon.c
|
|
|
|
* \brief Rendezvous implementation: shared code between
|
2004-05-05 23:32:43 +02:00
|
|
|
* introducers, services, clients, and rendezvous points.
|
2004-05-09 18:47:25 +02:00
|
|
|
**/
|
2004-05-05 23:32:43 +02:00
|
|
|
|
2004-03-31 04:07:38 +02:00
|
|
|
#include "or.h"
|
|
|
|
|
2004-05-12 22:58:27 +02:00
|
|
|
/** Return 0 if one and two are the same service ids, else -1 or 1 */
|
2005-09-30 03:09:52 +02:00
|
|
|
int
|
|
|
|
rend_cmp_service_ids(const char *one, const char *two)
|
|
|
|
{
|
2004-05-12 22:58:27 +02:00
|
|
|
return strcasecmp(one,two);
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Free the storage held by the service descriptor <b>desc</b>.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2005-09-30 03:09:52 +02:00
|
|
|
void
|
|
|
|
rend_service_descriptor_free(rend_service_descriptor_t *desc)
|
2004-03-31 04:07:38 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
if (desc->pk)
|
|
|
|
crypto_free_pk_env(desc->pk);
|
|
|
|
if (desc->intro_points) {
|
|
|
|
for (i=0; i < desc->n_intro_points; ++i) {
|
|
|
|
tor_free(desc->intro_points[i]);
|
|
|
|
}
|
|
|
|
tor_free(desc->intro_points);
|
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
if (desc->intro_point_extend_info) {
|
|
|
|
for (i=0; i < desc->n_intro_points; ++i) {
|
|
|
|
if (desc->intro_point_extend_info[i])
|
|
|
|
extend_info_free(desc->intro_point_extend_info[i]);
|
|
|
|
}
|
|
|
|
tor_free(desc->intro_point_extend_info);
|
|
|
|
}
|
2004-03-31 04:07:38 +02:00
|
|
|
tor_free(desc);
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Encode a service descriptor for <b>desc</b>, and sign it with
|
|
|
|
* <b>key</b>. Store the descriptor in *<b>str_out</b>, and set
|
|
|
|
* *<b>len_out</b> to its length.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2004-03-31 04:07:38 +02:00
|
|
|
int
|
|
|
|
rend_encode_service_descriptor(rend_service_descriptor_t *desc,
|
2005-06-29 23:46:55 +02:00
|
|
|
int version,
|
2004-04-02 00:12:00 +02:00
|
|
|
crypto_pk_env_t *key,
|
2004-10-14 04:47:09 +02:00
|
|
|
char **str_out, size_t *len_out)
|
2004-03-31 04:07:38 +02:00
|
|
|
{
|
2005-06-29 23:46:55 +02:00
|
|
|
char *cp;
|
|
|
|
char *end;
|
2004-10-14 04:47:09 +02:00
|
|
|
int i;
|
2005-06-29 23:46:55 +02:00
|
|
|
size_t asn1len;
|
2005-12-14 03:19:27 +01:00
|
|
|
size_t buflen = PK_BYTES*2*(desc->n_intro_points+2);/*Too long, but ok*/
|
|
|
|
cp = *str_out = tor_malloc(buflen);
|
2005-06-29 23:46:55 +02:00
|
|
|
end = cp + PK_BYTES*2*(desc->n_intro_points+1);
|
|
|
|
if (version) {
|
|
|
|
*(uint8_t*)cp = (uint8_t)0xff;
|
|
|
|
*(uint8_t*)(cp+1) = (uint8_t)version;
|
|
|
|
cp += 2;
|
2004-03-31 04:07:38 +02:00
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
asn1len = crypto_pk_asn1_encode(desc->pk, cp+2, end-(cp+2));
|
2004-04-08 06:47:39 +02:00
|
|
|
set_uint16(cp, htons((uint16_t)asn1len));
|
2005-06-29 23:46:55 +02:00
|
|
|
cp += 2+asn1len;
|
2004-04-08 06:47:39 +02:00
|
|
|
set_uint32(cp, htonl((uint32_t)desc->timestamp));
|
2004-03-31 04:07:38 +02:00
|
|
|
cp += 4;
|
2005-06-29 23:46:55 +02:00
|
|
|
if (version == 1) {
|
|
|
|
set_uint16(cp, htons(desc->protocols));
|
|
|
|
cp += 2;
|
|
|
|
}
|
2004-04-08 06:47:39 +02:00
|
|
|
set_uint16(cp, htons((uint16_t)desc->n_intro_points));
|
2004-03-31 04:07:38 +02:00
|
|
|
cp += 2;
|
2005-06-29 23:46:55 +02:00
|
|
|
if (version == 0) {
|
|
|
|
for (i=0; i < desc->n_intro_points; ++i) {
|
|
|
|
char *ipoint = (char*)desc->intro_points[i];
|
2005-12-14 03:19:27 +01:00
|
|
|
strlcpy(cp, ipoint, buflen-(cp-*str_out));
|
2005-06-29 23:46:55 +02:00
|
|
|
cp += strlen(ipoint)+1;
|
|
|
|
}
|
|
|
|
} else {
|
2005-07-22 02:14:58 +02:00
|
|
|
if (desc->n_intro_points)
|
|
|
|
tor_assert(desc->intro_point_extend_info);
|
2005-06-29 23:46:55 +02:00
|
|
|
for (i=0; i < desc->n_intro_points; ++i) {
|
|
|
|
extend_info_t *info = desc->intro_point_extend_info[i];
|
|
|
|
int klen;
|
|
|
|
set_uint32(cp, htonl(info->addr));
|
|
|
|
set_uint16(cp+4, htons(info->port));
|
|
|
|
memcpy(cp+6, info->identity_digest, DIGEST_LEN);
|
|
|
|
klen = crypto_pk_asn1_encode(info->onion_key, cp+6+DIGEST_LEN+2,
|
|
|
|
(end-(cp+6+DIGEST_LEN+2)));
|
|
|
|
set_uint16(cp+6+DIGEST_LEN, htons((uint16_t)klen));
|
|
|
|
cp += 6+DIGEST_LEN+2+klen;
|
|
|
|
}
|
2004-03-31 04:07:38 +02:00
|
|
|
}
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(REND_SERVER);
|
2004-11-02 03:28:51 +01:00
|
|
|
i = crypto_pk_private_sign_digest(key, cp, *str_out, cp-*str_out);
|
2004-03-31 04:07:38 +02:00
|
|
|
if (i<0) {
|
|
|
|
tor_free(*str_out);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
cp += i;
|
2005-06-29 23:46:55 +02:00
|
|
|
*len_out = (size_t)(cp-*str_out);
|
2004-03-31 04:07:38 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Parse a service descriptor at <b>str</b> (<b>len</b> bytes). On
|
|
|
|
* success, return a newly alloced service_descriptor_t. On failure,
|
|
|
|
* return NULL.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
rend_service_descriptor_t *
|
|
|
|
rend_parse_service_descriptor(const char *str, size_t len)
|
2004-03-31 04:07:38 +02:00
|
|
|
{
|
|
|
|
rend_service_descriptor_t *result = NULL;
|
2004-10-14 04:47:09 +02:00
|
|
|
int i;
|
|
|
|
size_t keylen, asn1len;
|
2004-03-31 04:07:38 +02:00
|
|
|
const char *end, *cp, *eos;
|
2005-06-29 23:46:55 +02:00
|
|
|
int version = 0;
|
2004-04-02 00:12:00 +02:00
|
|
|
|
2004-03-31 04:07:38 +02:00
|
|
|
result = tor_malloc_zero(sizeof(rend_service_descriptor_t));
|
|
|
|
cp = str;
|
|
|
|
end = str+len;
|
2005-06-29 23:46:55 +02:00
|
|
|
if (end-cp<2) goto truncated;
|
|
|
|
if (*(uint8_t*)cp == 0xff) {
|
|
|
|
result->version = version = *(uint8_t*)(cp+1);
|
|
|
|
cp += 2;
|
|
|
|
} else {
|
|
|
|
result->version = version = 0;
|
|
|
|
}
|
2004-03-31 04:07:38 +02:00
|
|
|
if (end-cp < 2) goto truncated;
|
2004-04-08 06:47:39 +02:00
|
|
|
asn1len = ntohs(get_uint16(cp));
|
2004-03-31 04:07:38 +02:00
|
|
|
cp += 2;
|
2004-10-14 04:47:09 +02:00
|
|
|
if ((size_t)(end-cp) < asn1len) goto truncated;
|
2004-03-31 04:07:38 +02:00
|
|
|
result->pk = crypto_pk_asn1_decode(cp, asn1len);
|
|
|
|
if (!result->pk) goto truncated;
|
|
|
|
cp += asn1len;
|
|
|
|
if (end-cp < 4) goto truncated;
|
2004-04-08 06:47:39 +02:00
|
|
|
result->timestamp = (time_t) ntohl(get_uint32(cp));
|
2004-03-31 04:07:38 +02:00
|
|
|
cp += 4;
|
2005-06-29 23:46:55 +02:00
|
|
|
if (version == 1) {
|
|
|
|
if (end-cp < 2) goto truncated;
|
|
|
|
result->protocols = ntohs(get_uint16(cp));
|
|
|
|
cp += 2;
|
|
|
|
} else {
|
|
|
|
result->protocols = 1;
|
|
|
|
}
|
2004-03-31 04:07:38 +02:00
|
|
|
if (end-cp < 2) goto truncated;
|
2004-04-08 06:47:39 +02:00
|
|
|
result->n_intro_points = ntohs(get_uint16(cp));
|
2004-03-31 04:07:38 +02:00
|
|
|
cp += 2;
|
2005-07-22 02:14:58 +02:00
|
|
|
|
|
|
|
if (version == 0 && result->n_intro_points != 0) {
|
2005-12-14 21:40:40 +01:00
|
|
|
result->intro_points =
|
|
|
|
tor_malloc_zero(sizeof(char*)*result->n_intro_points);
|
2005-06-29 23:46:55 +02:00
|
|
|
for (i=0;i<result->n_intro_points;++i) {
|
|
|
|
if (end-cp < 2) goto truncated;
|
|
|
|
eos = (const char *)memchr(cp,'\0',end-cp);
|
|
|
|
if (!eos) goto truncated;
|
|
|
|
result->intro_points[i] = tor_strdup(cp);
|
|
|
|
cp = eos+1;
|
|
|
|
}
|
2005-07-22 02:14:58 +02:00
|
|
|
} else if (version != 0 && result->n_intro_points != 0) {
|
2005-06-29 23:46:55 +02:00
|
|
|
result->intro_point_extend_info =
|
|
|
|
tor_malloc_zero(sizeof(extend_info_t*)*result->n_intro_points);
|
2005-12-14 21:40:40 +01:00
|
|
|
result->intro_points =
|
|
|
|
tor_malloc_zero(sizeof(char*)*result->n_intro_points);
|
2005-06-29 23:46:55 +02:00
|
|
|
for (i=0;i<result->n_intro_points;++i) {
|
|
|
|
extend_info_t *info = result->intro_point_extend_info[i] =
|
|
|
|
tor_malloc_zero(sizeof(extend_info_t));
|
|
|
|
int klen;
|
|
|
|
if (end-cp < 8+DIGEST_LEN) goto truncated;
|
|
|
|
info->addr = ntohl(get_uint32(cp));
|
|
|
|
info->port = ntohs(get_uint16(cp+4));
|
|
|
|
memcpy(info->identity_digest, cp+6, DIGEST_LEN);
|
|
|
|
info->nickname[0] = '$';
|
|
|
|
base16_encode(info->nickname+1, sizeof(info->nickname)-1,
|
|
|
|
info->identity_digest, DIGEST_LEN);
|
|
|
|
result->intro_points[i] = tor_strdup(info->nickname);
|
|
|
|
klen = ntohs(get_uint16(cp+6+DIGEST_LEN));
|
|
|
|
cp += 8+DIGEST_LEN;
|
|
|
|
if (end-cp < klen) goto truncated;
|
|
|
|
if (!(info->onion_key = crypto_pk_asn1_decode(cp,klen))) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,
|
|
|
|
"Internal error decoding onion key for intro point.");
|
2005-06-29 23:46:55 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
cp += klen;
|
|
|
|
}
|
2004-03-31 04:07:38 +02:00
|
|
|
}
|
|
|
|
keylen = crypto_pk_keysize(result->pk);
|
2004-10-14 04:47:09 +02:00
|
|
|
tor_assert(end-cp >= 0);
|
|
|
|
if ((size_t)(end-cp) < keylen) goto truncated;
|
|
|
|
if ((size_t)(end-cp) > keylen) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,
|
|
|
|
"Signature is %d bytes too long on service descriptor.",
|
|
|
|
(int)((size_t)(end-cp) - keylen));
|
2004-04-03 05:37:11 +02:00
|
|
|
goto error;
|
|
|
|
}
|
2006-10-31 20:17:07 +01:00
|
|
|
note_crypto_pk_op(REND_CLIENT);
|
2004-04-02 00:12:00 +02:00
|
|
|
if (crypto_pk_public_checksig_digest(result->pk,
|
2004-04-03 04:14:20 +02:00
|
|
|
(char*)str,cp-str, /* data */
|
|
|
|
(char*)cp,end-cp /* signature*/
|
|
|
|
)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL, "Bad signature on service descriptor.");
|
2004-03-31 04:07:38 +02:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
truncated:
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL, "Truncated service descriptor.");
|
2004-03-31 04:07:38 +02:00
|
|
|
error:
|
|
|
|
rend_service_descriptor_free(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Sets <b>out</b> to the first 10 bytes of the digest of <b>pk</b>,
|
|
|
|
* base32 encoded. NUL-terminates out. (We use this string to
|
|
|
|
* identify services in directory requests and .onion URLs.)
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
rend_get_service_id(crypto_pk_env_t *pk, char *out)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
2004-04-03 04:40:30 +02:00
|
|
|
char buf[DIGEST_LEN];
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(pk);
|
2004-03-31 05:42:56 +02:00
|
|
|
if (crypto_pk_get_digest(pk, buf) < 0)
|
|
|
|
return -1;
|
2004-07-22 10:30:06 +02:00
|
|
|
base32_encode(out, REND_SERVICE_ID_LEN+1, buf, 10);
|
2004-03-31 05:42:56 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-03-31 06:10:10 +02:00
|
|
|
/* ==== Rendezvous service descriptor cache. */
|
2004-05-09 18:47:25 +02:00
|
|
|
|
2006-03-12 23:48:18 +01:00
|
|
|
/** How old do we let hidden service descriptors get discarding them as too
|
|
|
|
* old? */
|
2006-03-13 00:31:16 +01:00
|
|
|
#define REND_CACHE_MAX_AGE (2*24*60*60)
|
2006-06-06 02:05:39 +02:00
|
|
|
/** How wrong do we assume our clock may be when checking whether hidden
|
2006-03-12 23:48:18 +01:00
|
|
|
* services are too old or too new? */
|
2005-04-03 07:36:23 +02:00
|
|
|
#define REND_CACHE_MAX_SKEW (24*60*60)
|
2004-03-31 05:42:56 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Map from service id (as generated by rend_get_service_id) to
|
2004-05-05 23:32:43 +02:00
|
|
|
* rend_cache_entry_t. */
|
2004-03-31 06:10:10 +02:00
|
|
|
static strmap_t *rend_cache = NULL;
|
2004-03-31 05:42:56 +02:00
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Initializes the service descriptor cache.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
rend_cache_init(void)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache = strmap_new();
|
2004-03-31 05:42:56 +02:00
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Helper: free storage held by a single service descriptor cache entry. */
|
2005-02-28 23:38:00 +01:00
|
|
|
static void
|
|
|
|
_rend_cache_entry_free(void *p)
|
|
|
|
{
|
|
|
|
rend_cache_entry_t *e = p;
|
|
|
|
rend_service_descriptor_free(e->parsed);
|
|
|
|
tor_free(e->desc);
|
|
|
|
tor_free(e);
|
|
|
|
}
|
|
|
|
|
2005-06-11 20:52:12 +02:00
|
|
|
/** Free all storage held by the service descriptor cache. */
|
2005-02-28 23:38:00 +01:00
|
|
|
void
|
|
|
|
rend_cache_free_all(void)
|
|
|
|
{
|
|
|
|
strmap_free(rend_cache, _rend_cache_entry_free);
|
|
|
|
rend_cache = NULL;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Removes all old entries from the service descriptor cache.
|
2004-04-06 05:44:36 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
rend_cache_clean(void)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
|
|
|
strmap_iter_t *iter;
|
|
|
|
const char *key;
|
|
|
|
void *val;
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache_entry_t *ent;
|
2004-03-31 05:42:56 +02:00
|
|
|
time_t cutoff;
|
2005-04-03 07:36:23 +02:00
|
|
|
cutoff = time(NULL) - REND_CACHE_MAX_AGE - REND_CACHE_MAX_SKEW;
|
2004-03-31 06:10:10 +02:00
|
|
|
for (iter = strmap_iter_init(rend_cache); !strmap_iter_done(iter); ) {
|
2004-03-31 05:42:56 +02:00
|
|
|
strmap_iter_get(iter, &key, &val);
|
2004-03-31 06:10:10 +02:00
|
|
|
ent = (rend_cache_entry_t*)val;
|
2004-03-31 05:42:56 +02:00
|
|
|
if (ent->parsed->timestamp < cutoff) {
|
2004-03-31 06:10:10 +02:00
|
|
|
iter = strmap_iter_next_rmv(rend_cache, iter);
|
2005-02-28 23:38:00 +01:00
|
|
|
_rend_cache_entry_free(ent);
|
2004-03-31 05:42:56 +02:00
|
|
|
} else {
|
2004-03-31 06:10:10 +02:00
|
|
|
iter = strmap_iter_next(rend_cache, iter);
|
2004-03-31 05:42:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** Return true iff <b>query</b> is a syntactically valid service ID (as
|
2004-05-05 23:32:43 +02:00
|
|
|
* generated by rend_get_service_id). */
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
|
|
|
rend_valid_service_id(const char *query)
|
|
|
|
{
|
2004-11-28 10:05:49 +01:00
|
|
|
if (strlen(query) != REND_SERVICE_ID_LEN)
|
2004-04-01 21:39:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2004-04-03 01:30:54 +02:00
|
|
|
if (strspn(query, BASE32_CHARS) != REND_SERVICE_ID_LEN)
|
|
|
|
return 0;
|
|
|
|
|
2004-04-01 21:39:11 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2005-06-29 23:46:55 +02:00
|
|
|
/** If we have a cached rend_cache_entry_t for the service ID <b>query</b>,
|
|
|
|
* set *<b>e</b> to that entry and return 1. Else return 0. If
|
|
|
|
* <b>version</b> is nonnegative, only return an entry in that descriptor
|
|
|
|
* format version. Otherwise (if <b>version</b> is negative), return the most
|
|
|
|
* recent format we have.
|
2004-05-05 23:32:43 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2005-06-29 23:46:55 +02:00
|
|
|
rend_cache_lookup_entry(const char *query, int version, rend_cache_entry_t **e)
|
2004-04-08 00:00:54 +02:00
|
|
|
{
|
2006-03-15 00:40:37 +01:00
|
|
|
char key[REND_SERVICE_ID_LEN+2]; /* 1<query>\0 or 0<query>\0 */
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(rend_cache);
|
2004-04-08 00:00:54 +02:00
|
|
|
if (!rend_valid_service_id(query))
|
|
|
|
return -1;
|
2005-06-29 23:46:55 +02:00
|
|
|
*e = NULL;
|
|
|
|
if (version != 0) {
|
|
|
|
tor_snprintf(key, sizeof(key), "1%s", query);
|
|
|
|
*e = strmap_get_lc(rend_cache, key);
|
|
|
|
}
|
|
|
|
if (!*e && version != 1) {
|
|
|
|
tor_snprintf(key, sizeof(key), "0%s", query);
|
|
|
|
*e = strmap_get_lc(rend_cache, key);
|
|
|
|
}
|
2004-04-08 00:00:54 +02:00
|
|
|
if (!*e)
|
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-10 06:34:48 +02:00
|
|
|
/** <b>query</b> is a base-32'ed service id. If it's malformed, return -1.
|
2004-04-01 21:39:11 +02:00
|
|
|
* Else look it up.
|
2004-05-10 06:34:48 +02:00
|
|
|
* - If it is found, point *desc to it, and write its length into
|
|
|
|
* *desc_len, and return 1.
|
|
|
|
* - If it is not found, return 0.
|
2004-05-05 23:32:43 +02:00
|
|
|
* Note: calls to rend_cache_clean or rend_cache_store may invalidate
|
|
|
|
* *desc.
|
2004-04-01 21:39:11 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2005-12-14 21:40:40 +01:00
|
|
|
rend_cache_lookup_desc(const char *query, int version, const char **desc,
|
|
|
|
size_t *desc_len)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache_entry_t *e;
|
2004-04-08 00:00:54 +02:00
|
|
|
int r;
|
2005-06-29 23:46:55 +02:00
|
|
|
r = rend_cache_lookup_entry(query,version,&e);
|
2004-04-08 00:00:54 +02:00
|
|
|
if (r <= 0) return r;
|
2004-03-31 05:42:56 +02:00
|
|
|
*desc = e->desc;
|
|
|
|
*desc_len = e->len;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Parse *desc, calculate its service id, and store it in the cache.
|
2004-05-05 23:32:43 +02:00
|
|
|
* If we have a newer descriptor with the same ID, ignore this one.
|
|
|
|
* If we have an older descriptor with the same ID, replace it.
|
2005-01-20 00:15:59 +01:00
|
|
|
* Return -1 if it's malformed or otherwise rejected; return 0 if
|
|
|
|
* it's the same or older than one we've already got; return 1 if
|
2007-04-30 19:46:13 +02:00
|
|
|
* it's novel. The published flag tells us if we store the descriptor
|
2007-04-30 19:46:19 +02:00
|
|
|
* in our role as directory (1) or if we cache it as client (0).
|
2004-04-01 21:39:11 +02:00
|
|
|
*/
|
2005-06-11 20:52:12 +02:00
|
|
|
int
|
2007-04-30 19:46:13 +02:00
|
|
|
rend_cache_store(const char *desc, size_t desc_len, int published)
|
2004-03-31 05:42:56 +02:00
|
|
|
{
|
2004-03-31 06:10:10 +02:00
|
|
|
rend_cache_entry_t *e;
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_t *parsed;
|
|
|
|
char query[REND_SERVICE_ID_LEN+1];
|
2007-04-25 08:05:46 +02:00
|
|
|
char key[REND_SERVICE_ID_LEN+2]; /* 1<query>\0 or 0<query>\0 */
|
2004-03-31 05:42:56 +02:00
|
|
|
time_t now;
|
2007-04-30 19:46:13 +02:00
|
|
|
or_options_t *options = get_options();
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(rend_cache);
|
2004-03-31 05:42:56 +02:00
|
|
|
parsed = rend_parse_service_descriptor(desc,desc_len);
|
|
|
|
if (!parsed) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_PROTOCOL,"Couldn't parse service descriptor.");
|
2004-03-31 05:42:56 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (rend_get_service_id(parsed->pk, query)<0) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_warn(LD_BUG,"Couldn't compute service ID.");
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
tor_snprintf(key, sizeof(key), "%c%s", parsed->version?'1':'0', query);
|
2004-03-31 05:42:56 +02:00
|
|
|
now = time(NULL);
|
2005-04-03 07:36:23 +02:00
|
|
|
if (parsed->timestamp < now-REND_CACHE_MAX_AGE-REND_CACHE_MAX_SKEW) {
|
2006-02-21 07:23:57 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_REND,
|
|
|
|
"Service descriptor %s is too old.", safe_str(query));
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
2004-03-31 06:10:10 +02:00
|
|
|
if (parsed->timestamp > now+REND_CACHE_MAX_SKEW) {
|
2006-02-21 07:23:57 +01:00
|
|
|
log_fn(LOG_PROTOCOL_WARN, LD_REND,
|
|
|
|
"Service descriptor %s is too far in the future.", safe_str(query));
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_free(parsed);
|
|
|
|
return -1;
|
|
|
|
}
|
2007-04-30 19:46:19 +02:00
|
|
|
/* report novel publication to statistics */
|
2007-04-30 19:46:13 +02:00
|
|
|
if (published && options->HSAuthorityRecordStats) {
|
|
|
|
hs_usage_note_publish_total(query, time(NULL));
|
|
|
|
}
|
2005-06-29 23:46:55 +02:00
|
|
|
e = (rend_cache_entry_t*) strmap_get_lc(rend_cache, key);
|
2004-03-31 05:42:56 +02:00
|
|
|
if (e && e->parsed->timestamp > parsed->timestamp) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"We already have a newer service descriptor %s with the "
|
|
|
|
"same ID and version.", safe_str(query));
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_free(parsed);
|
2004-04-08 07:08:27 +02:00
|
|
|
return 0;
|
2004-03-31 05:42:56 +02:00
|
|
|
}
|
|
|
|
if (e && e->len == desc_len && !memcmp(desc,e->desc,desc_len)) {
|
2006-02-13 11:33:00 +01:00
|
|
|
log_info(LD_REND,"We already have this service descriptor %s.",
|
|
|
|
safe_str(query));
|
2004-04-08 00:00:54 +02:00
|
|
|
e->received = time(NULL);
|
2004-03-31 05:42:56 +02:00
|
|
|
rend_service_descriptor_free(parsed);
|
2004-04-08 07:08:27 +02:00
|
|
|
return 0;
|
2004-03-31 05:42:56 +02:00
|
|
|
}
|
|
|
|
if (!e) {
|
2004-03-31 06:10:10 +02:00
|
|
|
e = tor_malloc_zero(sizeof(rend_cache_entry_t));
|
2005-06-29 23:46:55 +02:00
|
|
|
strmap_set_lc(rend_cache, key, e);
|
2007-04-30 19:46:19 +02:00
|
|
|
/* report novel publication to statistics */
|
2007-04-30 19:46:13 +02:00
|
|
|
if (published && options->HSAuthorityRecordStats) {
|
|
|
|
hs_usage_note_publish_novel(query, time(NULL));
|
|
|
|
}
|
2004-03-31 05:42:56 +02:00
|
|
|
} else {
|
|
|
|
rend_service_descriptor_free(e->parsed);
|
|
|
|
tor_free(e->desc);
|
|
|
|
}
|
2004-04-08 00:00:54 +02:00
|
|
|
e->received = time(NULL);
|
2004-03-31 05:42:56 +02:00
|
|
|
e->parsed = parsed;
|
|
|
|
e->len = desc_len;
|
2004-04-03 05:39:31 +02:00
|
|
|
e->desc = tor_malloc(desc_len);
|
|
|
|
memcpy(e->desc, desc, desc_len);
|
2004-03-31 05:42:56 +02:00
|
|
|
|
2006-02-13 11:33:00 +01:00
|
|
|
log_debug(LD_REND,"Successfully stored rend desc '%s', len %d.",
|
|
|
|
safe_str(query), (int)desc_len);
|
2005-01-20 00:15:59 +01:00
|
|
|
return 1;
|
2004-03-31 05:42:56 +02:00
|
|
|
}
|
|
|
|
|
2004-05-09 18:47:25 +02:00
|
|
|
/** Called when we get a rendezvous-related relay cell on circuit
|
2004-05-10 06:34:48 +02:00
|
|
|
* <b>circ</b>. Dispatch on rendezvous relay command. */
|
2005-06-11 20:52:12 +02:00
|
|
|
void
|
|
|
|
rend_process_relay_cell(circuit_t *circ, int command, size_t length,
|
|
|
|
const char *payload)
|
2004-04-03 05:37:11 +02:00
|
|
|
{
|
2006-07-23 09:37:35 +02:00
|
|
|
or_circuit_t *or_circ = NULL;
|
|
|
|
origin_circuit_t *origin_circ = NULL;
|
2004-04-03 05:37:11 +02:00
|
|
|
int r;
|
2006-07-23 09:37:35 +02:00
|
|
|
if (CIRCUIT_IS_ORIGIN(circ))
|
|
|
|
origin_circ = TO_ORIGIN_CIRCUIT(circ);
|
|
|
|
else
|
|
|
|
or_circ = TO_OR_CIRCUIT(circ);
|
|
|
|
|
2004-11-28 10:05:49 +01:00
|
|
|
switch (command) {
|
2004-04-03 05:37:11 +02:00
|
|
|
case RELAY_COMMAND_ESTABLISH_INTRO:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_mid_establish_intro(or_circ,payload,length);
|
2004-04-03 05:37:11 +02:00
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_ESTABLISH_RENDEZVOUS:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_mid_establish_rendezvous(or_circ,payload,length);
|
2004-04-03 05:37:11 +02:00
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_INTRODUCE1:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_mid_introduce(or_circ,payload,length);
|
2004-04-03 05:37:11 +02:00
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_INTRODUCE2:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_service_introduce(origin_circ,payload,length);
|
2004-04-03 05:37:11 +02:00
|
|
|
break;
|
2004-04-13 01:33:47 +02:00
|
|
|
case RELAY_COMMAND_INTRODUCE_ACK:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_client_introduction_acked(origin_circ,payload,length);
|
2004-04-13 01:33:47 +02:00
|
|
|
break;
|
2004-04-03 05:37:11 +02:00
|
|
|
case RELAY_COMMAND_RENDEZVOUS1:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_mid_rendezvous(or_circ,payload,length);
|
2004-04-03 05:37:11 +02:00
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS2:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_client_receive_rendezvous(origin_circ,payload,length);
|
2004-04-03 05:37:11 +02:00
|
|
|
break;
|
2004-04-03 06:55:22 +02:00
|
|
|
case RELAY_COMMAND_INTRO_ESTABLISHED:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_service_intro_established(origin_circ,payload,length);
|
2004-04-03 06:55:22 +02:00
|
|
|
break;
|
|
|
|
case RELAY_COMMAND_RENDEZVOUS_ESTABLISHED:
|
2006-07-23 09:37:35 +02:00
|
|
|
r = rend_client_rendezvous_acked(origin_circ,payload,length);
|
2004-04-03 06:55:22 +02:00
|
|
|
break;
|
2004-04-03 05:37:11 +02:00
|
|
|
default:
|
2004-04-25 22:37:37 +02:00
|
|
|
tor_assert(0);
|
2004-04-03 05:37:11 +02:00
|
|
|
}
|
2006-10-09 04:35:51 +02:00
|
|
|
|
|
|
|
(void)r;
|
2004-04-03 05:37:11 +02:00
|
|
|
}
|
2005-06-09 21:03:31 +02:00
|
|
|
|
2007-04-30 19:46:19 +02:00
|
|
|
/** Return the number of entries in our rendezvous descriptor cache. */
|
2007-04-30 19:46:13 +02:00
|
|
|
int
|
|
|
|
rend_cache_size(void)
|
|
|
|
{
|
|
|
|
return strmap_size(rend_cache);
|
|
|
|
}
|
|
|
|
|