From bb0f1a9c37476ebe08b2de7b6fb7f8b2ed0514b5 Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Thu, 12 May 2011 22:21:16 +0000 Subject: [PATCH] WiP --- src/mesh/gnunet-service-mesh.c | 13 +++++++-- src/mesh/mesh.h | 4 +-- src/mesh/mesh_api_new.c | 39 ++++++++++++++------------ src/mesh/mesh_protocol.h | 28 +++++++++---------- src/mesh/test_mesh_api.c | 51 +++++++++++++++++++--------------- 5 files changed, 76 insertions(+), 59 deletions(-) diff --git a/src/mesh/gnunet-service-mesh.c b/src/mesh/gnunet-service-mesh.c index d3b857c6a..2080a375f 100644 --- a/src/mesh/gnunet-service-mesh.c +++ b/src/mesh/gnunet-service-mesh.c @@ -657,8 +657,7 @@ dht_get_response_handler(void *cls, + (p->length * sizeof (struct GNUNET_PeerIdentity)), &send_core_create_path_for_peer, - peer_info - ); + peer_info); return; } @@ -676,9 +675,13 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) struct MeshClient *c, *next; struct MESH_tunnel *t; + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "MESH: client disconnected\n"); c = clients_head; while (NULL != c) { if (c->handle == client) { + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "MESH: cleaning client structures\n"); GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c); while (NULL != (t = c->tunnels_head)) { destroy_tunnel(c, t); @@ -710,8 +713,10 @@ handle_local_new_client (void *cls, struct MeshClient *c; unsigned int payload_size; + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "MESH: new client connected\n"); /* Check data sanity */ - payload_size = message->size - sizeof(struct GNUNET_MessageHeader); + payload_size = ntohs(message->size) - sizeof(struct GNUNET_MessageHeader); if (0 != payload_size % sizeof(GNUNET_MESH_ApplicationType)) { GNUNET_break(0); GNUNET_SERVER_receive_done(client, GNUNET_SYSERR); @@ -728,6 +733,8 @@ handle_local_new_client (void *cls, c->messages_subscribed = NULL; } c->subscription_counter = payload_size/sizeof(GNUNET_MESH_ApplicationType); + GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, + "MESH: client has %u subscriptions\n", c->subscription_counter); /* Insert new client in DLL */ GNUNET_CONTAINER_DLL_insert (clients_head, clients_tail, c); diff --git a/src/mesh/mesh.h b/src/mesh/mesh.h index dc90f781a..d317e8054 100644 --- a/src/mesh/mesh.h +++ b/src/mesh/mesh.h @@ -76,8 +76,8 @@ struct GNUNET_MESH_ClientConnect { * sizeof(MESH_ApplicationType) * applications */ struct GNUNET_MessageHeader header; - uint16_t types; - uint16_t applications; + uint16_t types GNUNET_PACKED; + uint16_t applications GNUNET_PACKED; }; diff --git a/src/mesh/mesh_api_new.c b/src/mesh/mesh_api_new.c index 11376681a..1fe9714e5 100644 --- a/src/mesh/mesh_api_new.c +++ b/src/mesh/mesh_api_new.c @@ -133,34 +133,35 @@ struct GNUNET_MESH_TransmitHandle { * @return number of bytes written to buf */ static size_t -send_connect_packet (void *cls, size_t size, void *buf) { +send_connect_packet (void *cls, size_t size, void *buf) +{ struct GNUNET_MESH_Handle *h = cls; struct GNUNET_MESH_ClientConnect *msg; uint16_t *types; - int ntypes; + uint16_t ntypes; GNUNET_MESH_ApplicationType *apps; - int napps; + uint16_t napps; h->th = NULL; if (0 == size || buf == NULL) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Send: buffer size 0 or buffer invalid\n"); + "Send connect packet: buffer size 0 or buffer invalid\n"); // FIXME: disconnect, reconnect, retry! return 0; } if (sizeof(struct GNUNET_MessageHeader) > size) { GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Send: buffer size too small\n"); + "Send connect packet: buffer size too small\n"); // FIXME: disconnect, reconnect, retry! return 0; } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Sending into %lu bytes buffer\n", + "Send connect packet: %lu bytes buffer\n", size); msg = (struct GNUNET_MESH_ClientConnect *) buf; - msg->header.type = GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT; + msg->header.type = htons(GNUNET_MESSAGE_TYPE_MESH_LOCAL_CONNECT); - for(ntypes = 0, types = NULL; h->message_handlers[ntypes].type; ntypes++) { + for (ntypes = 0, types = NULL; h->message_handlers[ntypes].type; ntypes++) { types = GNUNET_realloc(types, sizeof(uint16_t) * (ntypes + 1)); types[ntypes] = h->message_handlers[ntypes].type; } @@ -172,20 +173,24 @@ send_connect_packet (void *cls, size_t size, void *buf) { apps[napps] = h->applications[napps]; } - msg->header.size = sizeof(struct GNUNET_MESH_ClientConnect) + - sizeof(uint16_t) * ntypes + - sizeof(GNUNET_MESH_ApplicationType) * napps; - if(msg->header.size > size) { - /* TODO treat error / retry */ - return 0; - } + msg->header.size = htons(sizeof(struct GNUNET_MESH_ClientConnect) + + sizeof(uint16_t) * ntypes + + sizeof(GNUNET_MESH_ApplicationType) * napps); memcpy(&msg[1], types, sizeof(uint16_t) * ntypes); memcpy(&msg[1] + sizeof(uint16_t) * ntypes, apps, sizeof(GNUNET_MESH_ApplicationType) * napps); - - return msg->header.size; + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, + "Sent %lu bytes long message %d types and %d apps\n", + ntohs(msg->header.size), + ntypes, + napps + ); + msg->applications = htons(napps); + msg->types = htons(ntypes); + + return ntohs(msg->header.size); } diff --git a/src/mesh/mesh_protocol.h b/src/mesh/mesh_protocol.h index 50bb50b02..01a44b58b 100644 --- a/src/mesh/mesh_protocol.h +++ b/src/mesh/mesh_protocol.h @@ -55,18 +55,18 @@ struct GNUNET_MESH_ManipulatePath * Global id of the tunnel this path belongs to, * unique in conjunction with the origin. */ - uint32_t tid GNUNET_PACKED; + uint32_t tid GNUNET_PACKED; /** * Information about speed requirements. If the tunnel cannot sustain the * minimum bandwidth, packets are to be dropped. */ - uint32_t speed_min GNUNET_PACKED; + uint32_t speed_min GNUNET_PACKED; /** * 64-bit alignment. */ - uint32_t reserved GNUNET_PACKED; + uint32_t reserved GNUNET_PACKED; /** * path_length structs defining the *whole* path from the origin [0] to the @@ -88,12 +88,12 @@ struct GNUNET_MESH_OriginMulticast /** * TID of the tunnel */ - uint32_t tid GNUNET_PACKED; + uint32_t tid GNUNET_PACKED; /** * OID of the tunnel */ - struct GNUNET_PeerIdentity oid; + struct GNUNET_PeerIdentity oid; /** * Payload follows @@ -114,17 +114,17 @@ struct GNUNET_MESH_DataMessageFromOrigin /** * TID of the tunnel */ - uint32_t tid GNUNET_PACKED; + uint32_t tid GNUNET_PACKED; /** * OID of the tunnel */ - struct GNUNET_PeerIdentity oid; + struct GNUNET_PeerIdentity oid; /** * Destination. */ - struct GNUNET_PeerIdentity destination; + struct GNUNET_PeerIdentity destination; /** * Payload follows @@ -145,17 +145,17 @@ struct GNUNET_MESH_DataMessageToOrigin /** * TID of the tunnel */ - uint32_t tid GNUNET_PACKED; + uint32_t tid GNUNET_PACKED; /** * OID of the tunnel */ - struct GNUNET_PeerIdentity oid; + struct GNUNET_PeerIdentity oid; /** * Sender of the message. */ - struct GNUNET_PeerIdentity sender; + struct GNUNET_PeerIdentity sender; /** * Payload follows @@ -175,17 +175,17 @@ struct GNUNET_MESH_SpeedNotify /** * TID of the tunnel */ - uint32_t tid GNUNET_PACKED; + uint32_t tid GNUNET_PACKED; /** * OID of the tunnel */ - struct GNUNET_PeerIdentity oid; + struct GNUNET_PeerIdentity oid; /** * Slowest link down the path (above minimum speed requirement). */ - uint32_t speed_min; + uint32_t speed_min; }; diff --git a/src/mesh/test_mesh_api.c b/src/mesh/test_mesh_api.c index 5f988f555..6c3bd60a9 100644 --- a/src/mesh/test_mesh_api.c +++ b/src/mesh/test_mesh_api.c @@ -11,28 +11,40 @@ static struct GNUNET_OS_Process *arm_pid; static struct GNUNET_MESH_Handle *mesh; -static struct GNUNET_DHT_Handle *dht; - static void - do_shutdown (void *cls, - const struct GNUNET_SCHEDULER_TaskContext *tc) +do_shutdown (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) { - if (NULL != mesh) - GNUNET_MESH_disconnect (mesh); - if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) + if (NULL != mesh) { + GNUNET_MESH_disconnect (mesh); + } + if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) { GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); + } GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid)); GNUNET_OS_process_close (arm_pid); } +static void +error_shutdown (void *cls, + const struct GNUNET_SCHEDULER_TaskContext *tc) +{ + if (NULL != mesh) { + GNUNET_MESH_disconnect (mesh); + } + if (0 != GNUNET_OS_process_kill (arm_pid, SIGTERM)) { + GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill"); + } + GNUNET_assert (GNUNET_OK == GNUNET_OS_process_wait (arm_pid)); + GNUNET_OS_process_close (arm_pid); +} + static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { GNUNET_MESH_ApplicationType app; - // char buffer[2048]; - arm_pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm", @@ -40,13 +52,6 @@ run (void *cls, "-L", "DEBUG", "-c", "test_mesh.conf", NULL); - dht = GNUNET_DHT_connect(cfg, 100); - if(NULL == dht) { - GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Couldn't connect to dht :(\n"); - } else { - GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "YAY! CONNECTED TO DHT :D\n"); - } - app = 0; mesh = GNUNET_MESH_connect(cfg, NULL, NULL, handlers, &app); if(NULL == mesh) { @@ -56,15 +61,13 @@ run (void *cls, } /* do real test work here */ - GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, - &do_shutdown, - NULL); + GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply( + GNUNET_TIME_UNIT_SECONDS, 5), + &do_shutdown, + NULL); } - - - int main (int argc, char *argv[]) { int ret; char *const argv2[] = {"test-mesh-api", @@ -80,7 +83,9 @@ int main (int argc, char *argv[]) { argv2, "test-mesh-api", "nohelp", options, &run, NULL); if (ret != GNUNET_OK) { - GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "test-mesh-api': Failed with error code %d\n", ret); + GNUNET_log(GNUNET_ERROR_TYPE_WARNING, + "test-mesh-api': Failed with error code %d\n", ret); + return 1; } return 0; } -- 2.25.1