2 This file is part of GNUnet.
3 (C) 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file mesh/mesh_api_new.c
23 * @brief mesh api: client implementation of mesh service
24 * @author Bartlomiej Polot
31 * - API CALL DEFINITIONS
38 #if 0 /* keep Emacsens' auto-indent happy */
45 #include "gnunet_common.h"
46 #include "gnunet_client_lib.h"
47 #include "gnunet_util_lib.h"
48 #include "gnunet_peer_lib.h"
49 #include "gnunet_mesh_service_new.h"
52 /******************************************************************************/
53 /************************** CONSTANTS ******************************/
54 /******************************************************************************/
56 #define GNUNET_MESH_LOCAL_TUNNEL_ID_MARK 0x80000000
58 /******************************************************************************/
59 /************************ DATA STRUCTURES ****************************/
60 /******************************************************************************/
63 * Opaque handle to the service.
65 struct GNUNET_MESH_Handle {
67 * Handle to the server connection, to send messages later
69 struct GNUNET_CLIENT_Connection *client;
72 * Set of handlers used for processing incoming messages in the tunnels
74 const struct GNUNET_MESH_MessageHandler *message_handlers;
78 * Set of applications that should be claimed to be offered at this node.
79 * Note that this is just informative, the appropiate handlers must be
80 * registered independently and the mapping is up to the developer of the
83 const GNUNET_MESH_ApplicationType *applications;
87 * Double linked list of the tunnels this client is connected to.
89 struct GNUNET_MESH_Tunnel *tunnels_head;
90 struct GNUNET_MESH_Tunnel *tunnels_tail;
93 * tid of the next tunnel to create (to avoid reusing IDs often)
95 MESH_TunnelID next_tid;
98 * Callback for tunnel disconnection
100 GNUNET_MESH_TunnelEndHandler *cleaner;
103 * Handle to cancel pending transmissions in case of disconnection
105 struct GNUNET_CLIENT_TransmitHandle *th;
108 * Closure for all the handlers given by the client
114 * Opaque handle to a tunnel.
116 struct GNUNET_MESH_Tunnel {
121 struct GNUNET_MESH_Tunnel *next;
122 struct GNUNET_MESH_Tunnel *prev;
125 * Local ID of the tunnel
130 * Callback to execute when peers connect to the tunnel
132 GNUNET_MESH_TunnelConnectHandler connect_handler;
135 * Callback to execute when peers disconnect to the tunnel
137 GNUNET_MESH_TunnelDisconnectHandler disconnect_handler;
140 * All peers added to the tunnel
142 GNUNET_PEER_Id *peers;
145 * Closure for the connect/disconnect handlers
150 * Handle to the mesh this tunnel belongs to
152 struct GNUNET_MESH_Handle *mesh;
155 struct GNUNET_MESH_TransmitHandle {
159 /******************************************************************************/
160 /*********************** AUXILIARY FUNCTIONS *************************/
161 /******************************************************************************/
164 * Get the tunnel handler for the tunnel specified by id from the given handle
165 * @param h Mesh handle
166 * @param tid ID of the wanted tunnel
167 * @return handle to the required tunnel or NULL if not found
169 static struct GNUNET_MESH_Tunnel *
170 retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelID tid)
172 struct GNUNET_MESH_Tunnel *t;
176 if (t->tid == tid) return t;
183 /******************************************************************************/
184 /************************ SEND CALLBACKS ****************************/
185 /******************************************************************************/
189 * Function called to send a connect message to the service, specifying the
190 * types and applications that the client is interested in.
191 * "buf" will be NULL and "size" zero if the socket was closed for writing in
194 * @param cls closure, the mesh handle
195 * @param size number of bytes available in buf
196 * @param buf where the callee should write the connect message
197 * @return number of bytes written to buf
200 send_connect_packet (void *cls, size_t size, void *buf)
202 struct GNUNET_MESH_Handle *h = cls;
203 struct GNUNET_MESH_ClientConnect *msg;
206 GNUNET_MESH_ApplicationType *apps;
210 if (0 == size || buf == NULL) {
211 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
212 "Send connect packet: buffer size 0 or buffer invalid\n");
213 // FIXME: disconnect, reconnect, retry!
216 if (sizeof(struct GNUNET_MessageHeader) > size) {
217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
218 "Send connect packet: buffer size too small\n");
219 // FIXME: disconnect, reconnect, retry!
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Send connect packet: %lu bytes buffer\n",
225 msg = (struct GNUNET_MESH_ClientConnect *) buf;
226 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
228 for (ntypes = 0, types = NULL; ntypes < h->n_handlers; ntypes++) {
229 types = GNUNET_realloc(types, sizeof(uint16_t) * (ntypes + 1));
230 types[ntypes] = h->message_handlers[ntypes].type;
233 for(napps = 0, apps = NULL; napps < h->n_applications; napps++) {
234 apps = GNUNET_realloc(apps,
235 sizeof(GNUNET_MESH_ApplicationType) *
237 apps[napps] = h->applications[napps];
240 msg->header.size = htons(sizeof(struct GNUNET_MESH_ClientConnect) +
241 sizeof(uint16_t) * ntypes +
242 sizeof(GNUNET_MESH_ApplicationType) * napps);
244 memcpy(&msg[1], types, sizeof(uint16_t) * ntypes);
245 memcpy(&msg[1] + sizeof(uint16_t) * ntypes,
247 sizeof(GNUNET_MESH_ApplicationType) * napps);
248 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
249 "Sent %lu bytes long message %d types and %d apps\n",
250 ntohs(msg->header.size),
254 msg->applications = htons(napps);
255 msg->types = htons(ntypes);
257 return ntohs(msg->header.size);
262 * Function called to send a create tunnel message, specifying the tunnel
263 * number chosen by the client.
264 * "buf" will be NULL and "size" zero if the socket was closed for
265 * writing in the meantime.
267 * @param cls closure, the tunnel handle
268 * @param size number of bytes available in buf
269 * @param buf where the callee should write the create tunnel message
270 * @return number of bytes written to buf
273 send_tunnel_create_packet (void *cls, size_t size, void *buf)
275 struct GNUNET_MESH_Tunnel *t = cls;
276 struct GNUNET_MESH_Handle *h;
277 struct GNUNET_MESH_TunnelMessage *msg;
281 if (0 == size || buf == NULL) {
282 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
283 "Send connect packet: buffer size 0 or buffer invalid\n");
284 // FIXME: disconnect, reconnect, retry!
287 if (sizeof(struct GNUNET_MessageHeader) > size) {
288 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
289 "Send connect packet: buffer size too small\n");
290 // FIXME: disconnect, reconnect, retry!
293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
294 "Send connect packet: %lu bytes buffer\n",
296 msg = (struct GNUNET_MESH_TunnelMessage *) buf;
297 msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT);
299 msg->header.size = htons(sizeof(struct GNUNET_MESH_TunnelMessage));
300 msg->tunnel_id = htonl(t->tid);
302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303 "Sent %lu bytes long message\n",
304 ntohs(msg->header.size));
306 return ntohs(msg->header.size);
310 /******************************************************************************/
311 /*********************** RECEIVE HANDLERS ****************************/
312 /******************************************************************************/
315 * Process the new tunnel notification and add it to the tunnels in the handle
317 * @param h The mesh handle
318 * @param msh A message with the details of the new incoming tunnel
321 process_tunnel_create(struct GNUNET_MESH_Handle *h,
322 const struct GNUNET_MESH_TunnelMessage *msg)
324 struct GNUNET_MESH_Tunnel *t;
327 tid = ntohl(msg->tunnel_id);
328 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK) {
329 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
330 "MESH: received an incoming tunnel with tid in local range (%X)\n",
332 return; //FIXME abort? reconnect?
334 t = GNUNET_malloc(sizeof(struct GNUNET_MESH_Tunnel));
336 t->connect_handler = NULL;
337 t->disconnect_handler = NULL;
346 * Process the incoming data packets
348 * @param h The mesh handle
349 * @param msh A message encapsulating the data
352 process_incoming_data(struct GNUNET_MESH_Handle *h,
353 const struct GNUNET_MESH_Data *msg)
355 const struct GNUNET_MESH_Data *payload;
356 const struct GNUNET_MESH_MessageHandler *handler;
357 struct GNUNET_MESH_Tunnel *t;
361 t = retrieve_tunnel(h, ntohl(msg->tunnel_id));
363 payload = (struct GNUNET_MESH_Data *) &msg[1];
364 type = ntohs(payload->header.type);
365 for (i = 0; i < h->n_handlers; i++) {
366 handler = &h->message_handlers[i];
367 if (handler->type == type) {
369 if (GNUNET_OK == handler->callback (h->cls,
376 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
377 "MESH: callback completed successfully\n");
379 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
380 "MESH: callback caused disconnection\n");
381 GNUNET_MESH_disconnect(h);
390 * Function to process all messages received from the service
393 * @param msg message received, NULL on timeout or fatal error
396 msg_received (void *cls, const struct GNUNET_MessageHeader * msg)
398 struct GNUNET_MESH_Handle *h = cls;
401 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
402 "received a NULL message from mesh\n");
406 switch (ntohs(msg->type)) {
407 /* Notify of a new incoming tunnel */
408 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE:
409 process_tunnel_create(h, (struct GNUNET_MESH_TunnelMessage *)msg);
411 /* Notify of a new peer in the tunnel */
412 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_CONNECTED:
414 /* Notify of a peer leaving the tunnel */
415 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_PEER_DISCONNECTED:
417 /* Notify of a new data packet in the tunnel */
418 case GNUNET_MESSAGE_TYPE_MESH_LOCAL_DATA:
419 process_incoming_data(h, (struct GNUNET_MESH_Data *)msg);
421 /* We shouldn't get any other packages, log and ignore */
423 GNUNET_log(GNUNET_ERROR_TYPE_WARNING,
424 "MESH: unsolicited message form service (type %d)\n",
428 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
429 "received a message from mesh\n");
430 GNUNET_CLIENT_receive (h->client,
433 GNUNET_TIME_UNIT_FOREVER_REL);
437 /******************************************************************************/
438 /********************** API CALL DEFINITIONS *************************/
439 /******************************************************************************/
442 * Connect to the mesh service.
444 * @param cfg configuration to use
445 * @param cls closure for the various callbacks that follow
446 * (including handlers in the handlers array)
447 * @param cleaner function called when an *inbound* tunnel is destroyed
448 * @param handlers callbacks for messages we care about, NULL-terminated
449 * note that the mesh is allowed to drop notifications about
450 * inbound messages if the client does not process them fast
451 * enough (for this notification type, a bounded queue is used)
452 * @param stypes Application Types the client claims to offer
453 * @return handle to the mesh service
454 * NULL on error (in this case, init is never called)
456 struct GNUNET_MESH_Handle *
457 GNUNET_MESH_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
459 GNUNET_MESH_TunnelEndHandler cleaner,
460 const struct GNUNET_MESH_MessageHandler *handlers,
461 const GNUNET_MESH_ApplicationType *stypes)
463 struct GNUNET_MESH_Handle *h;
466 h = GNUNET_malloc(sizeof(struct GNUNET_MESH_Handle));
468 h->cleaner = cleaner;
469 h->client = GNUNET_CLIENT_connect("mesh", cfg);
470 GNUNET_CLIENT_receive (h->client,
473 GNUNET_TIME_UNIT_FOREVER_REL);
474 if(h->client == NULL) {
480 h->message_handlers = handlers;
481 h->applications = stypes;
482 h->next_tid = 0x80000000;
484 for(h->n_handlers = 0; handlers[h->n_handlers].type; h->n_handlers++);
485 for(h->n_applications = 0; stypes[h->n_applications]; h->n_applications++);
487 size = sizeof(struct GNUNET_MESH_ClientConnect);
488 size += h->n_handlers * sizeof(uint16_t);
489 size += h->n_applications * sizeof(GNUNET_MESH_ApplicationType);
491 h->th = GNUNET_CLIENT_notify_transmit_ready(h->client,
493 GNUNET_TIME_UNIT_FOREVER_REL,
495 &send_connect_packet,
503 * Disconnect from the mesh service.
505 * @param handle connection to mesh to disconnect
508 GNUNET_MESH_disconnect (struct GNUNET_MESH_Handle *handle)
510 if (NULL != handle->th) {
511 GNUNET_CLIENT_notify_transmit_ready_cancel (handle->th);
513 if (NULL != handle->client) {
514 GNUNET_CLIENT_disconnect (handle->client, GNUNET_NO);
521 * Create a new tunnel (we're initiator and will be allowed to add/remove peers
524 * @param h mesh handle
525 * @param connect_handler function to call when peers are actually connected
526 * @param disconnect_handler function to call when peers are disconnected
527 * @param handler_cls closure for connect/disconnect handlers
529 struct GNUNET_MESH_Tunnel *
530 GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
531 GNUNET_MESH_TunnelConnectHandler
533 GNUNET_MESH_TunnelDisconnectHandler
537 struct GNUNET_MESH_Tunnel *tunnel;
539 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
540 "MESH: Creating new tunnel\n");
541 tunnel = GNUNET_malloc(sizeof(struct GNUNET_MESH_Tunnel));
543 tunnel->connect_handler = connect_handler;
544 tunnel->disconnect_handler = disconnect_handler;
545 tunnel->cls = handler_cls;
547 tunnel->tid = h->next_tid++;
548 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
550 h->th = GNUNET_CLIENT_notify_transmit_ready(h->client,
551 sizeof(struct GNUNET_MESH_TunnelMessage),
552 GNUNET_TIME_UNIT_FOREVER_REL,
554 &send_tunnel_create_packet,
562 * Request that a peer should be added to the tunnel. The existing
563 * connect handler will be called ONCE with either success or failure.
565 * @param tunnel handle to existing tunnel
566 * @param timeout how long to try to establish a connection
567 * @param peer peer to add
570 GNUNET_MESH_peer_request_connect_add (struct GNUNET_MESH_Tunnel *tunnel,
571 struct GNUNET_TIME_Relative timeout,
572 const struct GNUNET_PeerIdentity *peer)
574 static GNUNET_PEER_Id peer_id;
576 peer_id = GNUNET_PEER_intern(peer);
578 /* FIXME ACTUALLY DO STUFF */
579 tunnel->peers = &peer_id;
580 tunnel->connect_handler(tunnel->cls, peer, NULL);
586 * Request that a peer should be removed from the tunnel. The existing
587 * disconnect handler will be called ONCE if we were connected.
589 * @param tunnel handle to existing tunnel
590 * @param peer peer to remove
593 GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
594 const struct GNUNET_PeerIdentity *peer)
596 /* FIXME ACTUALLY DO STUFF */
597 tunnel->peers = NULL;
598 tunnel->disconnect_handler(tunnel->cls, peer);
604 * Request that the mesh should try to connect to a peer supporting the given
607 * @param tunnel handle to existing tunnel
608 * @param timeout how long to try to establish a connection
609 * @param app_type application type that must be supported by the peer (MESH
610 * should discover peer in proximity handling this type)
613 GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
614 struct GNUNET_TIME_Relative timeout,
615 GNUNET_MESH_ApplicationType
623 * Ask the mesh to call "notify" once it is ready to transmit the
624 * given number of bytes to the specified "target". If we are not yet
625 * connected to the specified peer, a call to this function will cause
626 * us to try to establish a connection.
628 * @param tunnel tunnel to use for transmission
629 * @param cork is corking allowed for this transmission?
630 * @param priority how important is the message?
631 * @param maxdelay how long can the message wait?
632 * @param target destination for the message,
633 * NULL for multicast to all tunnel targets
634 * @param notify_size how many bytes of buffer space does notify want?
635 * @param notify function to call when buffer space is available;
636 * will be called with NULL on timeout or if the overall queue
637 * for this peer is larger than queue_size and this is currently
638 * the message with the lowest priority
639 * @param notify_cls closure for notify
640 * @return non-NULL if the notify callback was queued,
641 * NULL if we can not even queue the request (insufficient
642 * memory); if NULL is returned, "notify" will NOT be called.
644 struct GNUNET_MESH_TransmitHandle *
645 GNUNET_MESH_notify_transmit_ready (struct GNUNET_MESH_Tunnel *tunnel,
648 struct GNUNET_TIME_Relative maxdelay,
649 const struct GNUNET_PeerIdentity *target,
651 GNUNET_CONNECTION_TransmitReadyNotify
655 struct GNUNET_MESH_TransmitHandle *handle;
657 handle = GNUNET_malloc(sizeof(struct GNUNET_MESH_TransmitHandle));
663 #if 0 /* keep Emacsens' auto-indent happy */