2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file core/gnunet-service-core_typemap.c
23 * @brief management of map that specifies which message types this peer supports
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_transport_service.h"
29 #include "gnunet-service-core.h"
30 #include "gnunet-service-core_sessions.h"
31 #include "gnunet-service-core_typemap.h"
36 * A type map describing which messages a given neighbour is able
41 uint32_t bits[(UINT16_MAX + 1) / 32];
46 * Bitmap of message types this peer is able to handle.
48 static uint32_t my_type_map[(UINT16_MAX + 1) / 32];
52 * Compute a type map message for this peer.
54 * @return this peers current type map message.
56 static struct GNUNET_MessageHeader *
57 compute_type_map_message ()
61 struct GNUNET_MessageHeader *hdr;
64 dlen = compressBound (sizeof (my_type_map));
66 dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
67 /* documentation says 100.1% oldSize + 12 bytes, but we
68 * should be able to overshoot by more to be safe */
70 hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
71 hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
72 tmp = (char *) &hdr[1];
74 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
75 sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
77 dlen = sizeof (my_type_map);
78 memcpy (tmp, my_type_map, sizeof (my_type_map));
79 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
83 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
90 * Send my type map to all connected peers (it got changed).
93 broadcast_my_type_map ()
95 struct GNUNET_MessageHeader *hdr;
97 hdr = compute_type_map_message ();
98 GSC_SESSIONS_broadcast (hdr);x
104 * Add a set of types to our type map.
107 GSC_TYPEMAP_add (const uint16_t *types,
113 my_type_map[types[i] / 32] |= (1 << (types[i] % 32));
115 broadcast_my_type_map ();
120 * Remove a set of types from our type map.
123 GSC_TYPEMAP_remove (const uint16_t *types,
126 /* rebuild my_type_map */
127 memset (my_type_map, 0, sizeof (my_type_map));
128 for (pos = clients; NULL != pos; pos = pos->next)
130 wtypes = (const uint16_t *) &pos[1];
131 for (i = 0; i < pos->tcnt; i++)
132 my_type_map[wtypes[i] / 32] |= (1 << (wtypes[i] % 32));
134 broadcast_my_type_map ();
139 * Test if any of the types from the types array is in the
142 * @param map map to test
143 * @param types array of types
144 * @param tcnt number of entries in types
145 * @return GNUNET_YES if a type is in the map, GNUNET_NO if not
148 GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
149 const uint16_t *types,
152 return GNUNET_YES; /* FIXME */
167 /* end of gnunet-service-core_typemap.c */