diff options
author | Christian Grothoff <christian@grothoff.org> | 2011-10-11 08:56:00 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2011-10-11 08:56:00 +0000 |
commit | 8afe6f260b19f148398784a2a918006b91bb4096 (patch) | |
tree | 6cd8160e8e0a183d4071d75abcbbde90b4d532f1 /src/core/gnunet-service-core_typemap.c | |
parent | 1f0797c47af2a197b05ac640ce17adc86982d3c0 (diff) | |
download | gnunet-8afe6f260b19f148398784a2a918006b91bb4096.tar.gz gnunet-8afe6f260b19f148398784a2a918006b91bb4096.zip |
process inbound type map messages:
Diffstat (limited to 'src/core/gnunet-service-core_typemap.c')
-rw-r--r-- | src/core/gnunet-service-core_typemap.c | 86 |
1 files changed, 84 insertions, 2 deletions
diff --git a/src/core/gnunet-service-core_typemap.c b/src/core/gnunet-service-core_typemap.c index 78dfc2bb9..db9b2a136 100644 --- a/src/core/gnunet-service-core_typemap.c +++ b/src/core/gnunet-service-core_typemap.c | |||
@@ -72,7 +72,6 @@ GSC_TYPEMAP_compute_type_map_message () | |||
72 | * should be able to overshoot by more to be safe */ | 72 | * should be able to overshoot by more to be safe */ |
73 | #endif | 73 | #endif |
74 | hdr = GNUNET_malloc (dlen + sizeof (struct GNUNET_MessageHeader)); | 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]; | 75 | tmp = (char *) &hdr[1]; |
77 | if ((Z_OK != | 76 | if ((Z_OK != |
78 | compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map, | 77 | compress2 ((Bytef *) tmp, &dlen, (const Bytef *) &my_type_map, |
@@ -86,11 +85,57 @@ GSC_TYPEMAP_compute_type_map_message () | |||
86 | { | 85 | { |
87 | hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP); | 86 | hdr->type = htons (GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP); |
88 | } | 87 | } |
88 | hdr->size = htons ((uint16_t) dlen + sizeof (struct GNUNET_MessageHeader)); | ||
89 | return hdr; | 89 | return hdr; |
90 | } | 90 | } |
91 | 91 | ||
92 | 92 | ||
93 | /** | 93 | /** |
94 | * Extract a type map from a TYPE_MAP message. | ||
95 | * | ||
96 | * @param msg a type map message | ||
97 | * @return NULL on error | ||
98 | */ | ||
99 | struct GSC_TypeMap * | ||
100 | GSC_TYPEMAP_get_from_message (const struct GNUNET_MessageHeader *msg) | ||
101 | { | ||
102 | struct GSC_TypeMap *ret; | ||
103 | uint16_t size; | ||
104 | uLongf dlen; | ||
105 | |||
106 | size = ntohs (msg->size); | ||
107 | switch (msg->type) | ||
108 | { | ||
109 | case GNUNET_MESSAGE_TYPE_CORE_BINARY_TYPE_MAP: | ||
110 | if (size != sizeof (struct GSC_TypeMap)) | ||
111 | { | ||
112 | GNUNET_break_op (0); | ||
113 | return NULL; | ||
114 | } | ||
115 | ret = GNUNET_malloc (sizeof (struct GSC_TypeMap)); | ||
116 | memcpy (ret, &msg[1], sizeof (struct GSC_TypeMap)); | ||
117 | return ret; | ||
118 | case GNUNET_MESSAGE_TYPE_CORE_COMPRESSED_TYPE_MAP: | ||
119 | ret = GNUNET_malloc (sizeof (struct GSC_TypeMap)); | ||
120 | dlen = sizeof (struct GSC_TypeMap); | ||
121 | if ( (Z_OK != | ||
122 | uncompress ((Bytef*) ret, &dlen, | ||
123 | (const Bytef*) &msg[1], (uLong) size)) || | ||
124 | (dlen != sizeof (struct GSC_TypeMap) ) ) | ||
125 | { | ||
126 | GNUNET_break_op (0); | ||
127 | GNUNET_free (ret); | ||
128 | return NULL; | ||
129 | } | ||
130 | return ret; | ||
131 | default: | ||
132 | GNUNET_break (0); | ||
133 | return NULL; | ||
134 | } | ||
135 | } | ||
136 | |||
137 | |||
138 | /** | ||
94 | * Send my type map to all connected peers (it got changed). | 139 | * Send my type map to all connected peers (it got changed). |
95 | */ | 140 | */ |
96 | static void | 141 | static void |
@@ -169,13 +214,50 @@ GSC_TYPEMAP_test_match (const struct GSC_TypeMap *tmap, | |||
169 | unsigned int i; | 214 | unsigned int i; |
170 | 215 | ||
171 | for (i=0;i<tcnt;i++) | 216 | for (i=0;i<tcnt;i++) |
172 | if (0 != (my_type_map.bits[types[i] / 32] & (1 << (types[i] % 32)))) | 217 | if (0 != (tmap->bits[types[i] / 32] & (1 << (types[i] % 32)))) |
173 | return GNUNET_YES; | 218 | return GNUNET_YES; |
174 | return GNUNET_NO; | 219 | return GNUNET_NO; |
175 | } | 220 | } |
176 | 221 | ||
177 | 222 | ||
178 | /** | 223 | /** |
224 | * Add additional types to a given typemap. | ||
225 | * | ||
226 | * @param map map to extend (not changed) | ||
227 | * @param types array of types to add | ||
228 | * @param tcnt number of entries in types | ||
229 | * @return updated type map (fresh copy) | ||
230 | */ | ||
231 | struct GSC_TypeMap * | ||
232 | GSC_TYPEMAP_extend (const struct GSC_TypeMap *tmap, | ||
233 | const uint16_t *types, | ||
234 | unsigned int tcnt) | ||
235 | { | ||
236 | struct GSC_TypeMap *ret; | ||
237 | unsigned int i; | ||
238 | |||
239 | ret = GNUNET_malloc (sizeof (struct GSC_TypeMap)); | ||
240 | if (NULL != tmap) | ||
241 | memcpy (ret, tmap, sizeof (struct GSC_TypeMap)); | ||
242 | for (i=0;i<tcnt;i++) | ||
243 | ret->bits[types[i] / 32] |= (1 << (types[i] % 32)); | ||
244 | return ret; | ||
245 | } | ||
246 | |||
247 | |||
248 | /** | ||
249 | * Free the given type map. | ||
250 | * | ||
251 | * @param map a type map | ||
252 | */ | ||
253 | void | ||
254 | GSC_TYPEMAP_destroy (struct GSC_TypeMap *tmap) | ||
255 | { | ||
256 | GNUNET_free (tmap); | ||
257 | } | ||
258 | |||
259 | |||
260 | /** | ||
179 | * Initialize typemap subsystem. | 261 | * Initialize typemap subsystem. |
180 | */ | 262 | */ |
181 | void | 263 | void |