hxing
[oweals/gnunet.git] / src / core / gnunet-service-core_typemap.c
1 /*
2      This file is part of GNUnet.
3      (C) 2011 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
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
25  */
26 #include "platform.h"
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"
32 #include <zlib.h>
33
34
35 /**
36  * A type map describing which messages a given neighbour is able
37  * to process.
38  */
39 struct GSC_TypeMap 
40 {
41   uint32_t bits[(UINT16_MAX + 1) / 32];
42 };
43
44 /**
45  * Bitmap of message types this peer is able to handle.
46  */
47 static struct GSC_TypeMap my_type_map;
48
49 /**
50  * Counters for message types this peer is able to handle.
51  */
52 static uint8_t map_counters[UINT16_MAX + 1];
53
54
55 /**
56  * Compute a type map message for this peer.
57  *
58  * @return this peers current type map message.
59  */
60 struct GNUNET_MessageHeader *
61 GSC_TYPEMAP_compute_type_map_message ()
62 {
63   char *tmp;
64   uLongf dlen;
65   struct GNUNET_MessageHeader *hdr;
66
67 #ifdef compressBound
68   dlen = compressBound (sizeof (my_type_map));
69 #else
70   dlen = sizeof (my_type_map) + (sizeof (my_type_map) / 100) + 20;
71   /* documentation says 100.1% oldSize + 12 bytes, but we
72    * should be able to overshoot by more to be safe */
73 #endif
74   hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader));
75   hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader));
76   tmp = (char *) &hdr[1];
77   if ((Z_OK !=
78        compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map,
79                   sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
80   {
81     dlen = sizeof (my_type_map);
82     memcpy (tmp, &my_type_map, sizeof (my_type_map));
83     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP);
84   }
85   else
86   {
87     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
88   }
89   return hdr;
90 }
91
92
93 /**
94  * Send my type map to all connected peers (it got changed).
95  */
96 static void
97 broadcast_my_type_map ()
98 {
99   struct GNUNET_MessageHeader *hdr;
100
101   hdr = GSC_TYPEMAP_compute_type_map_message ();
102   GSC_SESSIONS_broadcast (hdr);
103   GNUNET_free (hdr);
104 }
105
106
107 /**
108  * Add a set of types to our type map.
109  */
110 void
111 GSC_TYPEMAP_add (const uint16_t *types,
112                  unsigned int tlen)
113 {
114   unsigned int i;
115   int changed;
116
117   changed = GNUNET_NO;
118   for (i=0;i<tlen;i++)
119   {
120     if (0 == map_counters[types[i]]++)
121     {
122       my_type_map.bits[types[i] / 32] |= (1 << (types[i] % 32));
123       changed = GNUNET_YES;
124     }
125   }
126   if (GNUNET_YES == changed)
127     broadcast_my_type_map ();
128 }
129
130
131 /**
132  * Remove a set of types from our type map.
133  */
134 void
135 GSC_TYPEMAP_remove (const uint16_t *types,
136                     unsigned int tlen)
137 {
138   unsigned int i;
139   int changed;
140
141   changed = GNUNET_NO;
142   for (i=0;i<tlen;i++)
143   {
144     if (0 == --map_counters[types[i]])
145     {
146       my_type_map.bits[types[i] / 32] &= ~(1 << (types[i] % 32));
147       changed = GNUNET_YES;
148     }
149   }
150   if (GNUNET_YES == changed)
151     broadcast_my_type_map ();
152 }
153
154
155 /**
156  * Test if any of the types from the types array is in the
157  * given type map.
158  *
159  * @param map map to test
160  * @param types array of types
161  * @param tcnt number of entries in types
162  * @return GNUNET_YES if a type is in the map, GNUNET_NO if not
163  */ 
164 int
165 GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
166                         const uint16_t *types,
167                         unsigned int tcnt)
168 {  
169   unsigned int i;
170
171   for (i=0;i<tcnt;i++) 
172     if (0 != (my_type_map.bits[types[i] / 32] & (1 << (types[i] % 32))))
173       return GNUNET_YES;
174   return GNUNET_NO;
175 }
176
177
178 /**
179  * Initialize typemap subsystem.
180  */
181 void
182 GSC_TYPEMAP_init ()
183 {
184   /* nothing to do */
185 }
186
187
188 /**
189  * Shutdown typemap subsystem.
190  */
191 void
192 GSC_TYPEMAP_done ()
193 {
194   /* nothing to do */
195 }
196
197 /* end of gnunet-service-core_typemap.c */