aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBart Polot <bart@net.in.tum.de>2011-09-02 01:41:05 +0000
committerBart Polot <bart@net.in.tum.de>2011-09-02 01:41:05 +0000
commit1cdf3963878d15df45a2f10fd1514f551bd2010d (patch)
tree4eeb042dc70aed35837c5c058d47f3f66f8e565b /src
parent7f5493f255c22e8e8823c5490ba5fe5ff537dfdc (diff)
downloadgnunet-1cdf3963878d15df45a2f10fd1514f551bd2010d.tar.gz
gnunet-1cdf3963878d15df45a2f10fd1514f551bd2010d.zip
Implemented reconnect, creation of incoming tunnels, refactoring
Diffstat (limited to 'src')
-rw-r--r--src/include/gnunet_mesh_service_new.h3
-rw-r--r--src/mesh/mesh_api_new.c95
2 files changed, 70 insertions, 28 deletions
diff --git a/src/include/gnunet_mesh_service_new.h b/src/include/gnunet_mesh_service_new.h
index 6e8d4b557..b9447fe09 100644
--- a/src/include/gnunet_mesh_service_new.h
+++ b/src/include/gnunet_mesh_service_new.h
@@ -57,6 +57,7 @@ struct GNUNET_MESH_Tunnel;
57/** 57/**
58 * Functions with this signature are called whenever a message is 58 * Functions with this signature are called whenever a message is
59 * received or transmitted. 59 * received or transmitted.
60 * FIXME: transmitted???
60 * 61 *
61 * @param cls closure (set from GNUNET_MESH_connect) 62 * @param cls closure (set from GNUNET_MESH_connect)
62 * @param tunnel connection to the other end 63 * @param tunnel connection to the other end
@@ -241,13 +242,11 @@ GNUNET_MESH_peer_request_connect_del (struct GNUNET_MESH_Tunnel *tunnel,
241 * message type. 242 * message type.
242 * 243 *
243 * @param tunnel handle to existing tunnel 244 * @param tunnel handle to existing tunnel
244 * @param timeout how long to try to establish a connection
245 * @param app_type application type that must be supported by the peer 245 * @param app_type application type that must be supported by the peer
246 * (MESH should discover peer in proximity handling this type) 246 * (MESH should discover peer in proximity handling this type)
247 */ 247 */
248void 248void
249GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel, 249GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
250 struct GNUNET_TIME_Relative timeout,
251 GNUNET_MESH_ApplicationType app_type); 250 GNUNET_MESH_ApplicationType app_type);
252 251
253 252
diff --git a/src/mesh/mesh_api_new.c b/src/mesh/mesh_api_new.c
index edd9e1ab2..34e8f4747 100644
--- a/src/mesh/mesh_api_new.c
+++ b/src/mesh/mesh_api_new.c
@@ -22,7 +22,6 @@
22 * @author Bartlomiej Polot 22 * @author Bartlomiej Polot
23 * 23 *
24 * TODO: 24 * TODO:
25 * - handle reconnect (service crash/disconnect) properly
26 * - callbacks to client missing on certain events 25 * - callbacks to client missing on certain events
27 * - processing messages from service is incomplete 26 * - processing messages from service is incomplete
28 * 27 *
@@ -280,7 +279,7 @@ struct GNUNET_MESH_Tunnel
280 * List of application types that have been requested for this tunnel 279 * List of application types that have been requested for this tunnel
281 */ 280 */
282 GNUNET_MESH_ApplicationType *apps; 281 GNUNET_MESH_ApplicationType *apps;
283 282
284 /** 283 /**
285 * Number of peers added to the tunnel 284 * Number of peers added to the tunnel
286 */ 285 */
@@ -326,6 +325,34 @@ retrieve_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
326 325
327 326
328/** 327/**
328 * Get the tunnel handler for the tunnel specified by id from the given handle
329 * @param h Mesh handle
330 * @param tid ID of the wanted tunnel
331 * @return handle to the required tunnel or NULL if not found
332 */
333static struct GNUNET_MESH_Tunnel *
334create_tunnel (struct GNUNET_MESH_Handle *h, MESH_TunnelNumber tid)
335{
336 struct GNUNET_MESH_Tunnel *t;
337
338 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
339 t->mesh = h;
340 if (0 == tid)
341 {
342 t->tid = h->next_tid++;
343 }
344 else
345 {
346 t->tid = tid;
347 h->next_tid = tid + 1;
348 }
349 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
350 GNUNET_CONTAINER_DLL_insert (h->tunnels_head, h->tunnels_tail, t);
351 return t;
352}
353
354
355/**
329 * Get the peer descriptor for the peer with id from the given tunnel 356 * Get the peer descriptor for the peer with id from the given tunnel
330 * @param t Tunnel handle 357 * @param t Tunnel handle
331 * @param id Short form ID of the wanted peer 358 * @param id Short form ID of the wanted peer
@@ -453,18 +480,19 @@ send_packet (struct GNUNET_MESH_Handle *h,
453/** 480/**
454 * Reconnect to the service, retransmit all infomation to try to restore the 481 * Reconnect to the service, retransmit all infomation to try to restore the
455 * original state. 482 * original state.
456 * 483 * FIXME: notify user about it? (call all disconnect callbacks?)
484 *
457 * @param h handle to the mesh 485 * @param h handle to the mesh
458 * 486 *
459 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...) 487 * @return GNUNET_YES in case of sucess, GNUNET_NO otherwise (service down...)
460 */ 488 */
461static int 489static int
462reconnect (struct GNUNET_MESH_Handle *h) 490reconnect (struct GNUNET_MESH_Handle *h)
463{ 491{
464 struct GNUNET_MESH_Tunnel *t; 492 struct GNUNET_MESH_Tunnel *t;
465
466 unsigned int i; 493 unsigned int i;
467 494
495 h->in_receive = GNUNET_NO;
468 /* disconnect */ 496 /* disconnect */
469 if (NULL != h->th) 497 if (NULL != h->th)
470 { 498 {
@@ -485,16 +513,23 @@ reconnect (struct GNUNET_MESH_Handle *h)
485 } 513 }
486 for (t = h->tunnels_head; NULL != t; t = t->next) 514 for (t = h->tunnels_head; NULL != t; t = t->next)
487 { 515 {
488 /* create tunnel */ 516 struct GNUNET_MESH_TunnelMessage msg;
517
518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
519 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
520 msg.tunnel_id = htonl (t->tid);
521 send_packet (h, &msg.header);
489 for (i = 0; i < t->npeers; i++) 522 for (i = 0; i < t->npeers; i++)
490 { 523 {
491 struct GNUNET_MESH_PeerControl msg; 524 struct GNUNET_MESH_PeerControl msg;
525
492 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl)); 526 msg.header.size = htons (sizeof (struct GNUNET_MESH_PeerControl));
493 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD); 527 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT_PEER_ADD);
494 msg.tunnel_id = htonl (t->tid); 528 msg.tunnel_id = htonl (t->tid);
495// msg.peer = *peer; FIXME 529 GNUNET_PEER_resolve (t->peers[i]->id, &msg.peer);
496 send_packet (t->mesh, &msg.header); 530 send_packet (t->mesh, &msg.header);
497 } 531 }
532 /* FIXME what about connects by type? */
498 } 533 }
499 return GNUNET_YES; 534 return GNUNET_YES;
500} 535}
@@ -521,7 +556,7 @@ process_tunnel_create (struct GNUNET_MESH_Handle *h,
521 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK) 556 if (tid >= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK)
522 { 557 {
523 GNUNET_break (0); 558 GNUNET_break (0);
524 return; //FIXME abort? reconnect? 559 return;
525 } 560 }
526 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel)); 561 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel));
527 t->cls = h->cls; 562 t->cls = h->cls;
@@ -556,7 +591,14 @@ process_peer_event (struct GNUNET_MESH_Handle *h,
556 t = retrieve_tunnel (h, ntohl (msg->tunnel_id)); 591 t = retrieve_tunnel (h, ntohl (msg->tunnel_id));
557 if (NULL == t) 592 if (NULL == t)
558 { 593 {
559 GNUNET_break (0); 594 t = create_tunnel (h, msg->tunnel_id);
595 t->owner = GNUNET_PEER_intern (&msg->peer);
596 t->npeers = 1;
597 t->peers = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer *));
598 t->peers[0] = GNUNET_malloc (sizeof (struct GNUNET_MESH_Peer));
599 t->peers[0]->t = t;
600 t->peers[0]->connected = 1;
601 t->peers[0]->id = t->owner;
560 return; 602 return;
561 } 603 }
562 id = GNUNET_PEER_search (&msg->peer); 604 id = GNUNET_PEER_search (&msg->peer);
@@ -639,9 +681,14 @@ process_incoming_data (struct GNUNET_MESH_Handle *h,
639 handler = &h->message_handlers[i]; 681 handler = &h->message_handlers[i];
640 if (handler->type == type) 682 if (handler->type == type)
641 { 683 {
642 if (GNUNET_OK == handler->callback (h->cls, t, NULL, /* FIXME ctx */ 684 struct GNUNET_TRANSPORT_ATS_Information atsi;
643 peer, payload, NULL)) 685
644 { /* FIXME atsi */ 686 atsi.type = 0;
687 atsi.value = 0;
688 /* FIXME ctx */
689 if (GNUNET_OK ==
690 handler->callback (h->cls, t, NULL, peer, payload, &atsi))
691 {
645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
646 "MESH: callback completed successfully\n"); 693 "MESH: callback completed successfully\n");
647 } 694 }
@@ -670,10 +717,7 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg)
670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n"); 717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: received a message from MESH\n");
671 if (msg == NULL) 718 if (msg == NULL)
672 { 719 {
673 GNUNET_break (0); 720 reconnect (h);
674 h->in_receive = GNUNET_NO;
675 // rather: do_reconnect () -- and set 'in_receive' to NO there...
676 // FIXME: service disconnect, handle!
677 return; 721 return;
678 } 722 }
679 switch (ntohs (msg->type)) 723 switch (ntohs (msg->type))
@@ -732,7 +776,6 @@ send_callback (void *cls, size_t size, void *buf)
732 h->th = NULL; 776 h->th = NULL;
733 if ((0 == size) || (NULL == buf)) 777 if ((0 == size) || (NULL == buf))
734 { 778 {
735 // FIXME: disconnect, reconnect, retry?
736 reconnect (h); 779 reconnect (h);
737 return 0; 780 return 0;
738 } 781 }
@@ -973,13 +1016,10 @@ GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
973 struct GNUNET_MESH_TunnelMessage msg; 1016 struct GNUNET_MESH_TunnelMessage msg;
974 1017
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n"); 1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Creating new tunnel\n");
976 t = GNUNET_malloc (sizeof (struct GNUNET_MESH_Tunnel)); 1019 t = create_tunnel (h, 0);
977 t->connect_handler = connect_handler; 1020 t->connect_handler = connect_handler;
978 t->disconnect_handler = disconnect_handler; 1021 t->disconnect_handler = disconnect_handler;
979 t->cls = handler_cls; 1022 t->cls = handler_cls;
980 t->mesh = h;
981 t->tid = h->next_tid++;
982 h->next_tid |= GNUNET_MESH_LOCAL_TUNNEL_ID_MARK; // keep in range
983 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE); 1023 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_CREATE);
984 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage)); 1024 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
985 msg.tunnel_id = htonl (t->tid); 1025 msg.tunnel_id = htonl (t->tid);
@@ -994,18 +1034,21 @@ GNUNET_MESH_tunnel_create (struct GNUNET_MESH_Handle *h,
994 * @param tun tunnel handle 1034 * @param tun tunnel handle
995 */ 1035 */
996void 1036void
997GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *tun) 1037GNUNET_MESH_tunnel_destroy (struct GNUNET_MESH_Tunnel *t)
998{ 1038{
999 struct GNUNET_MESH_Handle *h; 1039 struct GNUNET_MESH_Handle *h;
1000 struct GNUNET_MESH_TunnelMessage msg; 1040 struct GNUNET_MESH_TunnelMessage msg;
1001 1041
1002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n"); 1042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "mesh: Destroying tunnel\n");
1003 h = tun->mesh; 1043 h = t->mesh;
1044
1045 if (0 != t->owner)
1046 GNUNET_PEER_change_rc (t->owner, -1);
1004 1047
1005 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY); 1048 msg.header.type = htons (GNUNET_MESSAGE_TYPE_MESH_LOCAL_TUNNEL_DESTROY);
1006 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage)); 1049 msg.header.size = htons (sizeof (struct GNUNET_MESH_TunnelMessage));
1007 msg.tunnel_id = htonl (tun->tid); 1050 msg.tunnel_id = htonl (t->tid);
1008 GNUNET_free (tun); 1051 GNUNET_free (t);
1009 send_packet (h, &msg.header); 1052 send_packet (h, &msg.header);
1010} 1053}
1011 1054
@@ -1108,7 +1151,7 @@ GNUNET_MESH_peer_request_connect_by_type (struct GNUNET_MESH_Tunnel *tunnel,
1108{ 1151{
1109 struct GNUNET_MESH_ConnectPeerByType msg; 1152 struct GNUNET_MESH_ConnectPeerByType msg;
1110 1153
1111 GNUNET_array_append(tunnel->apps, tunnel->napps, app_type); 1154 GNUNET_array_append (tunnel->apps, tunnel->napps, app_type);
1112 /* FIXME: add a new api call disconnect by type? */ 1155 /* FIXME: add a new api call disconnect by type? */
1113 1156
1114 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType)); 1157 msg.header.size = htons (sizeof (struct GNUNET_MESH_ConnectPeerByType));