drafting typemap API
[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
31
32 /**
33  * A type map describing which messages a given neighbour is able
34  * to process.
35  */
36 struct GSC_TypeMap 
37 {
38   uint32_t bits[(UINT16_MAX + 1) / 32];
39 };
40
41
42 /**
43  * Bitmap of message types this peer is able to handle.
44  */
45 static uint32_t my_type_map[(UINT16_MAX + 1) / 32];
46
47
48 /**
49  * Compute a type map message for this peer.
50  *
51  * @return this peers current type map message.
52  */
53 static struct GNUNET_MessageHeader *
54 compute_type_map_message ()
55 {
56   char *tmp;
57   uLongf dlen;
58   struct GNUNET_MessageHeader *hdr;
59
60 #ifdef compressBound
61   dlen = compressBound (sizeof (my_type_map));
62 #else
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 */
66 #endif
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];
70   if ((Z_OK !=
71        compress2 ((Bytef *) tmp, &dlen, (const Bytef *) my_type_map,
72                   sizeof (my_type_map), 9)) || (dlen >= sizeof (my_type_map)))
73   {
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);
77   }
78   else
79   {
80     hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP);
81   }
82   return hdr;
83 }
84
85
86 /**
87  * Send my type map to all connected peers (it got changed).
88  */
89 static void
90 broadcast_my_type_map ()
91 {
92   struct GNUNET_MessageHeader *hdr;
93
94   hdr = compute_type_map_message ();
95   GSC_SESSIONS_broadcast (hdr);x
96   GNUNET_free (hdr);
97 }
98
99
100 /**
101  * Add a set of types to our type map.
102  */
103 void
104 GSC_TYPEMAP_add (const uint16_t *types,
105                  unsigned int tlen)
106 {
107   unsigned int i;
108
109   for (i=0;i<tlen;i++)
110     my_type_map[types[i] / 32] |= (1 << (types[i] % 32));
111   if (tlen > 0)
112     broadcast_my_type_map ();
113 }
114
115
116 /**
117  * Remove a set of types from our type map.
118  */
119 void
120 GSC_TYPEMAP_remove (const uint16_t *types,
121                     unsigned int tlen)
122 {
123   /* rebuild my_type_map */
124   memset (my_type_map, 0, sizeof (my_type_map));
125   for (pos = clients; NULL != pos; pos = pos->next)
126   {
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));
130   }
131   broadcast_my_type_map ();
132 }
133
134
135 /**
136  * Test if any of the types from the types array is in the
137  * given type map.
138  *
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
143  */ 
144 int
145 GSC_TYPEMAP_test_match (struct GSC_TypeMap *tmap,
146                         const uint16_t *types,
147                         unsigned int tcnt)
148 {  
149   return GNUNET_YES; /* FIXME */
150 }
151
152
153 void
154 GSC_TYPEMAP_init ()
155 {
156 }
157
158
159 void
160 GSC_TYPEMAP_done ()
161 {
162 }
163
164 /* end of gnunet-service-core_typemap.c */