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"
33 * A type map describing which messages a given neighbour is able
38 uint32_t bits[(UINT16_MAX + 1) / 32];
43 * Bitmap of message types this peer is able to handle.
45 static uint32_t my_type_map[(UINT16_MAX + 1) / 32];
49 * Compute a type map message for this peer.
51 * @return this peers current type map message.
53 static struct GNUNET_MessageHeader *
54 compute_type_map_message ()
58 struct GNUNET_MessageHeader *hdr;
61 dlen = compressBound (sizeof (my_type_map));
63 dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
64 /* documentation says 100.1% oldSize + 12 bytes, but we
65 * should be able to overshoot by more to be safe */
67 hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
68 hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
69 tmp = (char *) &hdr[1];
71 compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
72 sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
74 dlen = sizeof (my_type_map);
75 memcpy (tmp, my_type_map, sizeof (my_type_map));
76 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
80 hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
87 * Send my type map to all connected peers (it got changed).
90 broadcast_my_type_map ()
92 struct GNUNET_MessageHeader *hdr;
94 hdr = compute_type_map_message ();
95 GSC_SESSIONS_broadcast (hdr);x
101 * Add a set of types to our type map.
104 GSC_TYPEMAP_add (const uint16_t *types,
110 my_type_map[types[i] / 32] |= (1 << (types[i] % 32));
112 broadcast_my_type_map ();
117 * Remove a set of types from our type map.
120 GSC_TYPEMAP_remove (const uint16_t *types,
123 /* rebuild my_type_map */
124 memset (my_type_map, 0, sizeof (my_type_map));
125 for (pos = clients; NULL != pos; pos = pos->next)
127 wtypes = (const uint16_t *) &pos[1];
128 for (i = 0; i < pos->tcnt; i++)
129 my_type_map[wtypes[i] / 32] |= (1 << (wtypes[i] % 32));
131 broadcast_my_type_map ();
136 * Test if any of the types from the types array is in the
139 * @param map map to test
140 * @param types array of types
141 * @param tcnt number of entries in types
142 * @return GNUNET_YES if a type is in the map, GNUNET_NO if not
145 GSC_TYPEMAP_test_match (struct GSC_TypeMap *tmap,
146 const uint16_t *types,
149 return GNUNET_YES; /* FIXME */
164 /* end of gnunet-service-core_typemap.c */