summaryrefslogtreecommitdiff
path: root/src/core/gnunet-service-core_typemap.c
blob: c9774ddecdf288e0787893de3645c9e719d6191e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
     This file is part of GNUnet.
     (C) 2011 Christian Grothoff (and other contributing authors)

     GNUnet is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     GNUnet is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     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.
*/

/**
 * @file core/gnunet-service-core_typemap.c
 * @brief management of map that specifies which message types this peer supports
 * @author Christian Grothoff
 */
#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_transport_service.h"
#include "gnunet-service-core.h"
#include "gnunet-service-core_sessions.h"
#include "gnunet-service-core_typemap.h"
#include <zlib.h>


/**
 * A type map describing which messages a given neighbour is able
 * to process.
 */
struct GSC_TypeMap
{
  uint32_t bits[(UINT16_MAX + 1) / 32];
};

/**
 * Bitmap of message types this peer is able to handle.
 */
static struct GSC_TypeMap my_type_map;

/**
 * Counters for message types this peer is able to handle.
 */
static uint8_t map_counters[UINT16_MAX + 1];


/**
 * Compute a type map message for this peer.
 *
 * @return this peers current type map message.
 */
struct GNUNET_MessageHeader *
GSC_TYPEMAP_compute_type_map_message ()
{
  char *tmp;
  uLongf dlen;
  struct GNUNET_MessageHeader *hdr;

#ifdef compressBound
  dlen = compressBound (sizeof (my_type_map));
#else
  dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
  /* documentation says 100.1% oldSize + 12 bytes, but we
   * should be able to overshoot by more to be safe */
#endif
  hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
  tmp = (char *) &hdr[1];
  if ((Z_OK !=
       compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
                  sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
  {
    dlen = sizeof (my_type_map);
    memcpy (tmp, &my_type_map, sizeof (my_type_map));
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
  }
  else
  {
    hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
  }
  hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
  return hdr;
}


/**
 * Extract a type map from a TYPE_MAP message.
 *
 * @param msg a type map message
 * @return NULL on error
 */
struct GSC_TypeMap *
GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg)
{
  struct GSC_TypeMap *ret;
  uint16_t size;
  uLongf dlen;

  size = ntohs (msg->size);
  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);
    if (size != sizeof (struct GSC_TypeMap))
    {
      GNUNET_break_op (0);
      return NULL;
    }
    ret = GNUNET_new (struct GSC_TypeMap);
    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_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)))
    {
      GNUNET_break_op (0);
      GNUNET_free (ret);
      return NULL;
    }
    return ret;
  default:
    GNUNET_break (0);
    return NULL;
  }
}


/**
 * Send my type map to all connected peers (it got changed).
 */
static void
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_free (hdr);
}


/**
 * Add a set of types to our type map.
 */
void
GSC_TYPEMAP_add (const uint16_t * types, unsigned int tlen)
{
  unsigned int i;
  int changed;

  changed = GNUNET_NO;
  for (i = 0; i < tlen; i++)
  {
    if (0 == map_counters[types[i]]++)
    {
      my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
      changed = GNUNET_YES;
    }
  }
  if (GNUNET_YES == changed)
    broadcast_my_type_map ();
}


/**
 * Remove a set of types from our type map.
 */
void
GSC_TYPEMAP_remove (const uint16_t * types, unsigned int tlen)
{
  unsigned int i;
  int changed;

  changed = GNUNET_NO;
  for (i = 0; i < tlen; i++)
  {
    if (0 == --map_counters[types[i]])
    {
      my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
      changed = GNUNET_YES;
    }
  }
  if (GNUNET_YES == changed)
    broadcast_my_type_map ();
}


/**
 * Test if any of the types from the types array is in the
 * given type map.
 *
 * @param tmap map to test
 * @param types array of types
 * @param tcnt number of entries in types
 * @return GNUNET_YES if a type is in the map, GNUNET_NO if not
 */
int
GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap, const uint16_t * types,
                        unsigned int tcnt)
{
  unsigned int i;

  if (NULL == tmap)
    return GNUNET_NO;
  if (0 == tcnt)
    return GNUNET_YES;          /* matches all */
  for (i = 0; i < tcnt; i++)
    if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32))))
      return GNUNET_YES;
  return GNUNET_NO;
}


/**
 * Add additional types to a given typemap.
 *
 * @param tmap map to extend (not changed)
 * @param types array of types to add
 * @param tcnt number of entries in 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)
{
  struct GSC_TypeMap *ret;
  unsigned int i;

  ret = GNUNET_new (struct GSC_TypeMap);
  if (NULL != tmap)
    memcpy (ret, tmap, sizeof (struct GSC_TypeMap));
  for (i = 0; i < tcnt; i++)
    ret->bits[types[i] / 32] |= (1 << (types[i] % 32));
  return ret;
}


/**
 * Create an empty type map.
 *
 * @return an empty type map
 */
struct GSC_TypeMap *
GSC_TYPEMAP_create ()
{
  return GNUNET_new (struct GSC_TypeMap);
}


/**
 * Free the given type map.
 *
 * @param tmap a type map
 */
void
GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap)
{
  GNUNET_free (tmap);
}


/**
 * Initialize typemap subsystem.
 */
void
GSC_TYPEMAP_init ()
{
  /* nothing to do */
}


/**
 * Shutdown typemap subsystem.
 */
void
GSC_TYPEMAP_done ()
{
  /* nothing to do */
}

/* end of gnunet-service-core_typemap.c */