X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=src%2Fcore%2Fgnunet-service-core_typemap.c;h=0600f59ef937af81afb905438ae8d222961a5767;hb=17f5db6f7c8d60930367738b3d872fbf891486ee;hp=c54e446c67f94c7f006bbfd20c7453f15b2e614b;hpb=d2efe262a0dda14e37d3787e8c52dae4ae6a8bab;p=oweals%2Fgnunet.git diff --git a/src/core/gnunet-service-core_typemap.c b/src/core/gnunet-service-core_typemap.c index c54e446c6..0600f59ef 100644 --- a/src/core/gnunet-service-core_typemap.c +++ b/src/core/gnunet-service-core_typemap.c @@ -1,6 +1,6 @@ /* This file is part of GNUnet. - (C) 2011 Christian Grothoff (and other contributing authors) + Copyright (C) 2011-2014 GNUnet e.V. GNUnet is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published @@ -14,8 +14,8 @@ You should have received a copy of the GNU General Public License along with GNUnet; see the file COPYING. If not, write to the - Free Software Foundation, Inc., 59 Temple Place - Suite 330, - Boston, MA 02111-1307, USA. + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ /** @@ -36,7 +36,7 @@ * A type map describing which messages a given neighbour is able * to process. */ -struct GSC_TypeMap +struct GSC_TypeMap { uint32_t bits[(UINT16_MAX + 1) / 32]; }; @@ -51,6 +51,63 @@ static struct GSC_TypeMap my_type_map; */ static uint8_t map_counters[UINT16_MAX + 1]; +/** + * Current hash of our (uncompressed) type map. + * Lazily computed when needed. + */ +static struct GNUNET_HashCode my_tm_hash; + +/** + * Is #my_tm_hash() current with respect to our type map? + */ +static int hash_current; + + +/** + * Our type map changed, recompute its hash. + */ +static void +rehash_typemap () +{ + hash_current = GNUNET_NO; +} + + +/** + * Hash the contents of a type map. + * + * @param tm map to hash + * @param hc where to store the hash code + */ +void +GSC_TYPEMAP_hash (const struct GSC_TypeMap *tm, + struct GNUNET_HashCode *hc) +{ + GNUNET_CRYPTO_hash (tm, + sizeof (struct GSC_TypeMap), + hc); +} + + +/** + * Check if the given hash matches our current type map. + * + * @param hc hash code to check if it matches our type map + * @return #GNUNET_YES if the hash matches, #GNUNET_NO if not + */ +int +GSC_TYPEMAP_check_hash (const struct GNUNET_HashCode *hc) +{ + if (GNUNET_NO == hash_current) + { + GSC_TYPEMAP_hash (&my_type_map, + &my_tm_hash); + hash_current = GNUNET_YES; + } + return (0 == memcmp (hc, &my_tm_hash, sizeof (struct GNUNET_HashCode))) + ? GNUNET_YES : GNUNET_NO; +} + /** * Compute a type map message for this peer. @@ -77,12 +134,14 @@ GSC_TYPEMAP_compute_type_map_message () compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map, sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map))) { + /* compression failed, use uncompressed map */ dlen = sizeof (my_type_map); - memcpy (tmp, &my_type_map, sizeof (my_type_map)); + GNUNET_memcpy (tmp, &my_type_map, sizeof (my_type_map)); hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP); } else { + /* compression worked, use compressed map */ hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP); } hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader)); @@ -107,29 +166,26 @@ GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg) switch (ntohs (msg->type)) { case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP: - GNUNET_STATISTICS_update (GSC_stats, - gettext_noop ("# type maps received"), - 1, - GNUNET_NO); + GNUNET_STATISTICS_update (GSC_stats, gettext_noop ("# type maps received"), + 1, GNUNET_NO); if (size != sizeof (struct GSC_TypeMap)) { GNUNET_break_op (0); return NULL; } - ret = GNUNET_malloc (sizeof (struct GSC_TypeMap)); - memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap)); + ret = GNUNET_new (struct GSC_TypeMap); + GNUNET_memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap)); return ret; case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP: - GNUNET_STATISTICS_update (GSC_stats, - gettext_noop ("# type maps received"), - 1, - GNUNET_NO); - ret = GNUNET_malloc (sizeof (struct GSC_TypeMap)); + GNUNET_STATISTICS_update (GSC_stats, + gettext_noop ("# type maps received"), + 1, + GNUNET_NO); + ret = GNUNET_new (struct GSC_TypeMap); dlen = sizeof (struct GSC_TypeMap); - if ( (Z_OK != - uncompress ((Bytef*) ret, &dlen, - (const Bytef*) &msg[1], (uLong) size)) || - (dlen != sizeof (struct GSC_TypeMap) ) ) + if ((Z_OK != + uncompress ((Bytef *) ret, &dlen, (const Bytef *) &msg[1], + (uLong) size)) || (dlen != sizeof (struct GSC_TypeMap))) { GNUNET_break_op (0); GNUNET_free (ret); @@ -152,27 +208,30 @@ broadcast_my_type_map () struct GNUNET_MessageHeader *hdr; hdr = GSC_TYPEMAP_compute_type_map_message (); - GNUNET_STATISTICS_update (GSC_stats, - gettext_noop ("# updates to my type map"), - 1, - GNUNET_NO); - GSC_SESSIONS_broadcast (hdr); + GNUNET_STATISTICS_update (GSC_stats, + gettext_noop ("# updates to my type map"), + 1, + GNUNET_NO); + GSC_SESSIONS_broadcast_typemap (hdr); GNUNET_free (hdr); } /** * Add a set of types to our type map. + * + * @param types array of message types supported by this peer + * @param tlen number of entries in @a types */ void GSC_TYPEMAP_add (const uint16_t *types, - unsigned int tlen) + unsigned int tlen) { unsigned int i; int changed; changed = GNUNET_NO; - for (i=0;ibits[types[i] / 32] & (1 << (types[i] % 32)))) return GNUNET_YES; return GNUNET_NO; @@ -239,23 +306,22 @@ GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap, /** * Add additional types to a given typemap. * - * @param map map to extend (not changed) + * @param tmap map to extend (not changed) * @param types array of types to add - * @param tcnt number of entries in types + * @param tcnt number of entries in @a types * @return updated type map (fresh copy) - */ + */ struct GSC_TypeMap * GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap, - const uint16_t *types, - unsigned int tcnt) + const uint16_t *types, + unsigned int tcnt) { struct GSC_TypeMap *ret; - unsigned int i; - ret = GNUNET_malloc (sizeof (struct GSC_TypeMap)); + ret = GNUNET_new (struct GSC_TypeMap); if (NULL != tmap) - memcpy (ret, tmap, sizeof (struct GSC_TypeMap)); - for (i=0;ibits[types[i] / 32] |= (1 << (types[i] % 32)); return ret; } @@ -264,19 +330,19 @@ GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap, /** * Create an empty type map. * - * @param map a type map + * @return an empty type map */ struct GSC_TypeMap * GSC_TYPEMAP_create () { - return GNUNET_malloc (sizeof (struct GSC_TypeMap)); + return GNUNET_new (struct GSC_TypeMap); } /** * Free the given type map. * - * @param map a type map + * @param tmap a type map */ void GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)