aboutsummaryrefslogtreecommitdiff
path: root/src/mesh/mesh_api_new.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/mesh/mesh_api_new.c')
-rw-r--r--src/mesh/mesh_api_new.c46
1 files changed, 37 insertions, 9 deletions
diff --git a/src/mesh/mesh_api_new.c b/src/mesh/mesh_api_new.c
index d68408a3b..ca68279ed 100644
--- a/src/mesh/mesh_api_new.c
+++ b/src/mesh/mesh_api_new.c
@@ -137,26 +137,30 @@ send_connect_packet (void *cls, size_t size, void *buf) {
137 int napps; 137 int napps;
138 138
139 if(0 == size || buf == NULL) { 139 if(0 == size || buf == NULL) {
140 /* TODO treat error / retry */ 140 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size 0 or buffer invalid\n");
141 return 0; 141 return 0;
142 } 142 }
143 if(sizeof(struct GNUNET_MessageHeader) > size) { 143 if(sizeof(struct GNUNET_MessageHeader) > size) {
144 /* TODO treat error / retry */ 144 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Send: buffer size too small\n");
145 return 0; 145 return 0;
146 } 146 }
147
147 msg = (struct GNUNET_MESH_ClientConnect *) buf; 148 msg = (struct GNUNET_MESH_ClientConnect *) buf;
148 h = (struct GNUNET_MESH_Handle *) cls; 149 h = (struct GNUNET_MESH_Handle *) cls;
149 msg->header.type = GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT; 150 msg->header.type = GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT;
151
150 for(ntypes = 0, types = NULL; h->message_handlers[ntypes].type; ntypes++) { 152 for(ntypes = 0, types = NULL; h->message_handlers[ntypes].type; ntypes++) {
151 types = GNUNET_realloc(types, sizeof(uint16_t) * (ntypes + 1)); 153 types = GNUNET_realloc(types, sizeof(uint16_t) * (ntypes + 1));
152 types[ntypes] = h->message_handlers[ntypes].type; 154 types[ntypes] = h->message_handlers[ntypes].type;
153 } 155 }
156
154 for(napps = 0, apps = NULL; h->applications[napps]; napps++) { 157 for(napps = 0, apps = NULL; h->applications[napps]; napps++) {
155 apps = GNUNET_realloc(apps, 158 apps = GNUNET_realloc(apps,
156 sizeof(GNUNET_MESH_ApplicationType) * 159 sizeof(GNUNET_MESH_ApplicationType) *
157 (napps + 1)); 160 (napps + 1));
158 apps[napps] = h->applications[napps]; 161 apps[napps] = h->applications[napps];
159 } 162 }
163
160 msg->header.size = sizeof(struct GNUNET_MESH_ClientConnect) + 164 msg->header.size = sizeof(struct GNUNET_MESH_ClientConnect) +
161 sizeof(uint16_t) * ntypes + 165 sizeof(uint16_t) * ntypes +
162 sizeof(GNUNET_MESH_ApplicationType) * napps; 166 sizeof(GNUNET_MESH_ApplicationType) * napps;
@@ -164,14 +168,36 @@ send_connect_packet (void *cls, size_t size, void *buf) {
164 /* TODO treat error / retry */ 168 /* TODO treat error / retry */
165 return 0; 169 return 0;
166 } 170 }
171
167 memcpy(&msg[1], types, sizeof(uint16_t) * ntypes); 172 memcpy(&msg[1], types, sizeof(uint16_t) * ntypes);
168 memcpy(&msg[1] + sizeof(uint16_t) * ntypes, 173 memcpy(&msg[1] + sizeof(uint16_t) * ntypes,
169 apps, 174 apps,
170 sizeof(GNUNET_MESH_ApplicationType) * napps); 175 sizeof(GNUNET_MESH_ApplicationType) * napps);
176
171 return msg->header.size; 177 return msg->header.size;
172} 178}
173 179
174 180/**
181 * Type of a function to call when we receive a message
182 * from the service.
183 *
184 * @param cls closure
185 * @param msg message received, NULL on timeout or fatal error
186 */
187void msg_received (void *cls,
188 const struct
189 GNUNET_MessageHeader * msg) {
190 uint16_t t;
191 if(msg != NULL){
192 t = ntohs(msg->type);
193 } else {
194 t = 0;
195 }
196 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
197 "received a message from mesh (of size %d)\n",
198 t);
199 return;
200}
175 201
176/** 202/**
177 * Connect to the mesh service. 203 * Connect to the mesh service.
@@ -199,21 +225,23 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
199 225
200 h = GNUNET_malloc(sizeof(struct GNUNET_MESH_Handle)); 226 h = GNUNET_malloc(sizeof(struct GNUNET_MESH_Handle));
201 227
202
203 h->cleaner = cleaner; 228 h->cleaner = cleaner;
204 h->mesh = GNUNET_CLIENT_connect("mesh", cfg); 229 h->mesh = GNUNET_CLIENT_connect("mesh", cfg);
230 GNUNET_CLIENT_receive (h->mesh,
231 &msg_received,
232 h,
233 GNUNET_TIME_UNIT_FOREVER_REL);
205 if(h->mesh == NULL) { 234 if(h->mesh == NULL) {
206 GNUNET_free(h); 235 GNUNET_free(h);
207 return NULL; 236 return NULL;
208 } 237 }
238
209 h->cls = cls; 239 h->cls = cls;
210 h->message_handlers = handlers; 240 h->message_handlers = handlers;
211 h->applications = stypes; 241 h->applications = stypes;
212 242
213 for(h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++); 243 for(h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++);
214 for(h->n_applications = 0; stypes[h->n_applications]; h->n_applications++); 244 for(h->n_applications = 0; stypes[h->n_applications]; h->n_applications++);
215 h->n_handlers--;
216 h->n_applications--;
217 245
218 size = sizeof(struct GNUNET_MESH_ClientConnect); 246 size = sizeof(struct GNUNET_MESH_ClientConnect);
219 size += h->n_handlers * sizeof(uint16_t); 247 size += h->n_handlers * sizeof(uint16_t);
@@ -221,8 +249,8 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
221 249
222 GNUNET_CLIENT_notify_transmit_ready(h->mesh, 250 GNUNET_CLIENT_notify_transmit_ready(h->mesh,
223 size, 251 size,
224 GNUNET_TIME_relative_get_forever(), 252 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
225 GNUNET_YES, 253 GNUNET_NO,
226 &send_connect_packet, 254 &send_connect_packet,
227 (void *)h); 255 (void *)h);
228 256
@@ -236,7 +264,7 @@ GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
236 * @param handle connection to mesh to disconnect 264 * @param handle connection to mesh to disconnect
237 */ 265 */
238void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) { 266void GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle) {
239 267
240 GNUNET_free(handle); 268 GNUNET_free(handle);
241 return; 269 return;
242} 270}