From 8fb297a546b46ddb45e2b87b8d01a82e8f827ba7 Mon Sep 17 00:00:00 2001 From: Matthias Wachs Date: Fri, 24 Feb 2012 13:49:26 +0000 Subject: [PATCH] - communication --- src/namestore/gnunet-service-namestore.c | 67 ++++++++++++-- src/namestore/namestore.h | 43 ++++++++- src/namestore/namestore_api.c | 108 +++++++++++++++-------- src/namestore/test_namestore_api.c | 23 +++-- 4 files changed, 186 insertions(+), 55 deletions(-) diff --git a/src/namestore/gnunet-service-namestore.c b/src/namestore/gnunet-service-namestore.c index 000243e34..58830b304 100644 --- a/src/namestore/gnunet-service-namestore.c +++ b/src/namestore/gnunet-service-namestore.c @@ -69,6 +69,11 @@ const struct GNUNET_CONFIGURATION_Handle *GSN_cfg; static struct GNUNET_NAMESTORE_PluginFunctions *GSN_database; +/** + * Our notification context. + */ +static struct GNUNET_SERVER_NotificationContext *snc; + static char *db_lib_name; static struct GNUNET_NAMESTORE_Client *client_head; @@ -101,10 +106,29 @@ cleanup_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) GNUNET_CONTAINER_DLL_remove (client_head, client_tail, nc); GNUNET_free (nc); + GNUNET_SERVER_notification_context_destroy (snc); + snc = NULL; + GNUNET_break (NULL == GNUNET_PLUGIN_unload (db_lib_name, GSN_database)); GNUNET_free (db_lib_name); } +static struct GNUNET_NAMESTORE_Client * +client_lookup (struct GNUNET_SERVER_Client *client) +{ + struct GNUNET_NAMESTORE_Client * nc; + + GNUNET_assert (NULL != client); + + for (nc = client_head; nc != NULL; nc = nc->next) + { + if (client == nc->client) + break; + } + return nc; +} + + /** * Called whenever a client is disconnected. Frees our * resources associated with that client. @@ -122,12 +146,9 @@ client_disconnect_notification (void *cls, struct GNUNET_SERVER_Client *client) GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p disconnected \n", client); - for (nc = client_head; nc != NULL; nc = nc->next) - { - if (client == nc->client) - break; - } - if (NULL == client) + nc = client_lookup (client); + + if ((NULL == client) || (NULL == nc)) return; for (no = nc->op_head; no != NULL; no = no->next) @@ -144,11 +165,11 @@ static void handle_start (void *cls, struct GNUNET_SERVER_Client * client, const struct GNUNET_MessageHeader * message) { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n"); + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Client %p connected\n", client); struct GNUNET_NAMESTORE_Client * nc = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_Client)); nc->client = client; - + GNUNET_SERVER_notification_context_add (snc, client); GNUNET_CONTAINER_DLL_insert(client_head, client_tail, nc); GNUNET_SERVER_receive_done (client, GNUNET_OK); @@ -159,6 +180,33 @@ static void handle_lookup_name (void *cls, const struct GNUNET_MessageHeader * message) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_LOOKUP_NAME"); + + struct GNUNET_NAMESTORE_Client *nc; + uint32_t id = 0; + size_t r_size = 0; + + nc = client_lookup(client); + if (nc == NULL) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + struct LookupNameMessage * ln_msg = (struct LookupNameMessage *) message; + id = ntohl (ln_msg->op_id); + + /* do the actual lookup */ + + /* send response */ + struct LookupNameResponseMessage lnr_msg; + r_size = sizeof (struct LookupNameResponseMessage); + + lnr_msg.header.type = ntohs (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE); + lnr_msg.header.size = ntohs (r_size); + lnr_msg.op_id = htonl (id); + + GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &lnr_msg, GNUNET_NO); GNUNET_SERVER_receive_done (client, GNUNET_OK); } @@ -182,7 +230,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, {&handle_start, NULL, GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)}, {&handle_lookup_name, NULL, - GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0}, + GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, sizeof (struct LookupNameMessage)}, {NULL, NULL, 0, 0} }; @@ -203,6 +251,7 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, /* Configuring server handles */ GNUNET_SERVER_add_handlers (server, handlers); + snc = GNUNET_SERVER_notification_context_create (server, 16); GNUNET_SERVER_disconnect_notify (server, &client_disconnect_notification, NULL); diff --git a/src/namestore/namestore.h b/src/namestore/namestore.h index d469ac836..27d36d78b 100644 --- a/src/namestore/namestore.h +++ b/src/namestore/namestore.h @@ -30,6 +30,7 @@ * Collect message types here, move to protocols later */ #define GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME 431 +#define GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE 432 GNUNET_NETWORK_STRUCT_BEGIN /** @@ -46,21 +47,61 @@ struct StartMessage }; GNUNET_NETWORK_STRUCT_END + +GNUNET_NETWORK_STRUCT_BEGIN +/** + * Generic namestore message with op id + */ +struct GenericMessage +{ + /** + * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_* + */ + struct GNUNET_MessageHeader header; + + /** + * Operation ID in NBO + */ + uint32_t op_id; +}; +GNUNET_NETWORK_STRUCT_END + GNUNET_NETWORK_STRUCT_BEGIN /** * Connect to namestore service */ struct LookupNameMessage { - /** * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME */ struct GNUNET_MessageHeader header; + /** + * Operation ID in NBO + */ + uint32_t op_id; }; GNUNET_NETWORK_STRUCT_END +GNUNET_NETWORK_STRUCT_BEGIN + +struct LookupNameResponseMessage +{ + /** + * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE + */ + struct GNUNET_MessageHeader header; + + /** + * Operation ID in NBO + */ + uint32_t op_id; +}; + + +GNUNET_NETWORK_STRUCT_END + /* end of namestore.h */ #endif diff --git a/src/namestore/namestore_api.c b/src/namestore/namestore_api.c index 16e42a7b1..e78814db7 100644 --- a/src/namestore/namestore_api.c +++ b/src/namestore/namestore_api.c @@ -45,7 +45,7 @@ struct GNUNET_NAMESTORE_QueueEntry struct GNUNET_NAMESTORE_Handle *nsh; - uint64_t op_id; + uint32_t op_id; GNUNET_NAMESTORE_ContinuationWithStatus cont; void *cont_cls; @@ -148,7 +148,7 @@ struct GNUNET_NAMESTORE_Handle struct GNUNET_NAMESTORE_QueueEntry * op_head; struct GNUNET_NAMESTORE_QueueEntry * op_tail; - uint64_t op_id; + uint32_t op_id; /** * Pending namestore zone iterator entries @@ -187,6 +187,36 @@ struct GNUNET_NAMESTORE_SimpleRecord static void force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh); +static void +handle_lookup_name_response (struct GNUNET_NAMESTORE_QueueEntry *qe, + struct LookupNameResponseMessage * msg, + size_t size) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n", + "LOOKUP_NAME_RESPONSE"); + + struct GNUNET_NAMESTORE_Handle *nsh = qe->nsh; + + struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key = NULL; + struct GNUNET_TIME_Absolute expire = GNUNET_TIME_absolute_get_forever(); + const char *name = ""; + unsigned int rd_count = 0; + const struct GNUNET_NAMESTORE_RecordData *rd = NULL; + const struct GNUNET_CRYPTO_RsaSignature *signature = NULL; + + /* TODO: extract real values */ + + /* Lookup complete, remove queue entry */ + GNUNET_CONTAINER_DLL_remove (nsh->op_head, nsh->op_tail, qe); + + /* Notify */ + if (qe->proc != NULL) + qe->proc (qe->proc_cls, zone_key, expire, name, rd_count, rd, signature); + + GNUNET_free (qe); + +} + /** * Type of a function to call when we receive a message @@ -199,10 +229,11 @@ static void process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg) { struct GNUNET_NAMESTORE_Handle *nsh = cls; + struct GenericMessage * gm; struct GNUNET_NAMESTORE_QueueEntry *qe; uint16_t size; uint16_t type; - uint64_t op_id = UINT64_MAX; + uint32_t op_id = UINT32_MAX; if (NULL == msg) { @@ -213,9 +244,23 @@ process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg) size = ntohs (msg->size); type = ntohs (msg->type); - /* find matching operation */ + if (size < sizeof (struct GenericMessage)) + { + GNUNET_break_op (0); + GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh, + GNUNET_TIME_UNIT_FOREVER_REL); + return; + } + + gm = (struct GenericMessage *) msg; + op_id = ntohl (gm->op_id); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %i size %i op %u\n", type, size, op_id); + + /* Find matching operation */ if (op_id > nsh->op_id) { + /* No matching pending operation found */ GNUNET_break_op (0); GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh, GNUNET_TIME_UNIT_FOREVER_REL); @@ -228,20 +273,23 @@ process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg) } if (qe == NULL) { + /* No matching pending operation found */ GNUNET_break_op (0); GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh, GNUNET_TIME_UNIT_FOREVER_REL); return; } + /* handle different message type */ switch (type) { - case GNUNET_MESSAGE_TYPE_TEST: - /* handle message here */ + case GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE: + handle_lookup_name_response (qe, (struct LookupNameResponseMessage *) msg, size); break; default: + GNUNET_break_op (0); break; } - size++; // FIXME: just working around compiler warning here... + GNUNET_CLIENT_receive (nsh->client, &process_namestore_message, nsh, GNUNET_TIME_UNIT_FOREVER_REL); @@ -387,12 +435,12 @@ force_reconnect (struct GNUNET_NAMESTORE_Handle *nsh) nsh); } -static void -enqeue_namestore_operation (struct GNUNET_NAMESTORE_Handle *nsh, struct GNUNET_NAMESTORE_QueueEntry *qe) +static uint32_t +get_op_id (struct GNUNET_NAMESTORE_Handle *nsh) { - qe->op_id = nsh->op_id; + uint32_t op_id = nsh->op_id; nsh->op_id ++; - GNUNET_CONTAINER_DLL_insert(nsh->op_head, nsh->op_tail, qe); + return op_id; } /** @@ -492,14 +540,16 @@ GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h, struct GNUNET_NAMESTORE_QueueEntry *qe; struct PendingMessage *pe; size_t msg_size = 0; + uint32_t id = 0; GNUNET_assert (NULL != h); - + id = get_op_id(h); qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry)); qe->nsh = h; qe->cont = cont; qe->cont_cls = cont_cls; - enqeue_namestore_operation(h, qe); + qe->op_id = id; + GNUNET_CONTAINER_DLL_insert(h->op_head, h->op_tail, qe); /* set msg_size*/ pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); @@ -576,7 +626,7 @@ GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h, qe->nsh = h; qe->cont = cont; qe->cont_cls = cont_cls; - enqeue_namestore_operation(h, qe); + get_op_id(h); /* set msg_size*/ pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); @@ -622,7 +672,7 @@ GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h, qe->nsh = h; qe->cont = cont; qe->cont_cls = cont_cls; - enqeue_namestore_operation(h, qe); + get_op_id(h); /* set msg_size*/ pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); @@ -674,50 +724,34 @@ GNUNET_NAMESTORE_lookup_record (struct GNUNET_NAMESTORE_Handle *h, struct GNUNET_NAMESTORE_QueueEntry *qe; struct PendingMessage *pe; size_t msg_size = 0; + uint32_t id = 0; GNUNET_assert (NULL != h); - + id = get_op_id(h); qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry)); qe->nsh = h; qe->proc = proc; qe->proc_cls = proc_cls; - enqeue_namestore_operation(h, qe); + qe->op_id = id; + GNUNET_CONTAINER_DLL_insert(h->op_head, h->op_tail, qe); /* set msg_size*/ msg_size = sizeof (struct LookupNameMessage); pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); /* create msg here */ - struct LookupNameMessage * msg; pe->size = msg_size; pe->is_init = GNUNET_NO; msg = (struct LookupNameMessage *) &pe[1]; msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME); msg->header.size = htons (msg_size); + msg->op_id = htonl (id); - /* create msg done */ - + /* transmit message */ GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe); do_transmit(h); -#if 0 - struct GNUNET_NAMESTORE_SimpleRecord *iter; - for (iter=h->records_head; iter != NULL; iter=iter->next) - { - proc(proc_cls, iter->zone, iter->name, iter->record_type, - iter->expiration, - iter->flags, - NULL /*sig loc*/, - iter->data_size /*size*/, - iter->data /* data */); - } - proc(proc_cls, zone, name, record_type, - GNUNET_TIME_absolute_get_forever(), 0, NULL, 0, NULL); /*TERMINATE*/ -#endif - - - return qe; } diff --git a/src/namestore/test_namestore_api.c b/src/namestore/test_namestore_api.c index 765dbec65..335bbbfba 100644 --- a/src/namestore/test_namestore_api.c +++ b/src/namestore/test_namestore_api.c @@ -82,9 +82,9 @@ endbadly (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) res = 1; } -/* + static void -end (void) +end (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) { if (endbadly_task != GNUNET_SCHEDULER_NO_TASK) { @@ -102,9 +102,20 @@ end (void) res = 0; } -*/ +void name_lookup_proc (void *cls, + const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *zone_key, + struct GNUNET_TIME_Absolute expire, + const char *name, + unsigned int rd_count, + const struct GNUNET_NAMESTORE_RecordData *rd, + const struct GNUNET_CRYPTO_RsaSignature *signature) +{ + res = 0; + GNUNET_SCHEDULER_add_now(&end, NULL); +} + static void run (void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) @@ -117,11 +128,7 @@ run (void *cls, char *const *args, const char *cfgfile, nsh = GNUNET_NAMESTORE_connect (cfg); GNUNET_break (NULL != nsh); - //GNUNET_NAMESTORE_lookup_name (nsh, NULL, NULL, 0, NULL, NULL); - - //stop_arm (); - //end (); - res = 0; + GNUNET_NAMESTORE_lookup_record (nsh, NULL, NULL, 0, &name_lookup_proc, NULL); } static int -- 2.25.1