0ef9a623663664b7465ae80540b4f37bf0b9274d
[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 /**
46  * Bitmap of message types this peer is able to handle.
47  */
48 static uint32_t my_type_map[(UINT16_MAX + 1) / 32];
49
50
51 /**
52  * Compute a type map message for this peer.
53  *
54  * @return this peers current type map message.
55  */
56 static struct GNUNET_MessageHeader *
57 compute_type_map_message ()
58 {
59   char *tmp;
60   uLongf dlen;
61   struct GNUNET_MessageHeader *hdr;
62
63 #ifdef compressBound
64   dlen = compressBound (sizeof (my_type_map));
65 #else
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 */
69 #endif
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];
73   if ((Z_OK !=
74        compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
75                   sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
76   {
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);
80   }
81   else
82   {
83     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
84   }
85   return hdr;
86 }
87
88
89 /**
90  * Send my type map to all connected peers (it got changed).
91  */
92 static void
93 broadcast_my_type_map ()
94 {
95   struct GNUNET_MessageHeader *hdr;
96
97   hdr = compute_type_map_message ();
98   GSC_SESSIONS_broadcast (hdr);x
99   GNUNET_free (hdr);
100 }
101
102
103 /**
104  * Add a set of types to our type map.
105  */
106 void
107 GSC_TYPEMAP_add (const uint16_t *types,
108                  unsigned int tlen)
109 {
110   unsigned int i;
111
112   for (i=0;i<tlen;i++)
113     my_type_map[types[i] / 32] |= (1 << (types[i] % 32));
114   if (tlen > 0)
115     broadcast_my_type_map ();
116 }
117
118
119 /**
120  * Remove a set of types from our type map.
121  */
122 void
123 GSC_TYPEMAP_remove (const uint16_t *types,
124                     unsigned int tlen)
125 {
126   /* rebuild my_type_map */
127   memset (my_type_map, 0, sizeof (my_type_map));
128   for (pos = clients; NULL != pos; pos = pos->next)
129   {
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));
133   }
134   broadcast_my_type_map ();
135 }
136
137
138 /**
139  * Test if any of the types from the types array is in the
140  * given type map.
141  *
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
146  */ 
147 int
148 GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap,
149                         const uint16_t *types,
150                         unsigned int tcnt)
151 {  
152   return GNUNET_YES; /* FIXME */
153 }
154
155
156 void
157 GSC_TYPEMAP_init ()
158 {
159 }
160
161
162 void
163 GSC_TYPEMAP_done ()
164 {
165 }
166
167 /* end of gnunet-service-core_typemap.c */