From: Matthias Wachs Date: Mon, 27 Feb 2012 16:03:12 +0000 (+0000) Subject: - more communication X-Git-Tag: initial-import-from-subversion-38251~14604 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=8139f58584f3094323241763469e2ea87ebf0488;p=oweals%2Fgnunet.git - more communication --- diff --git a/src/namestore/gnunet-service-namestore.c b/src/namestore/gnunet-service-namestore.c index 1700925de..7d9eb4801 100644 --- a/src/namestore/gnunet-service-namestore.c +++ b/src/namestore/gnunet-service-namestore.c @@ -437,6 +437,145 @@ static void handle_record_put (void *cls, GNUNET_SERVER_receive_done (client, GNUNET_OK); } + +static void handle_record_create (void *cls, + struct GNUNET_SERVER_Client * client, + const struct GNUNET_MessageHeader * message) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_CREATE"); + struct GNUNET_NAMESTORE_Client *nc; + struct RecordCreateResponseMessage rcr_msg; + size_t name_len; + size_t msg_size; + size_t msg_size_exp; + uint32_t id = 0; + + int res = GNUNET_SYSERR; + + if (ntohs (message->size) < sizeof (struct RecordCreateMessage)) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + nc = client_lookup(client); + if (nc == NULL) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + struct RecordCreateMessage * rp_msg = (struct RecordCreateMessage *) message; + id = ntohl (rp_msg->op_id); + name_len = ntohs (rp_msg->name_len); + msg_size = ntohs (message->size); + msg_size_exp = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData); + + if (msg_size != msg_size_exp) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size); + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + + if ((name_len == 0) || (name_len > 256)) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + /* DO WORK HERE */ + + /* Send response */ + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_CREATE_RESPONSE"); + rcr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE); + rcr_msg.op_id = rp_msg->op_id; + rcr_msg.header.size = htons (sizeof (struct RecordCreateResponseMessage)); + if (GNUNET_OK == res) + rcr_msg.op_result = htons (GNUNET_OK); + else + rcr_msg.op_result = htons (GNUNET_NO); + GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rcr_msg, GNUNET_NO); + + GNUNET_SERVER_receive_done (client, GNUNET_OK); +} + +static void handle_record_remove (void *cls, + struct GNUNET_SERVER_Client * client, + const struct GNUNET_MessageHeader * message) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' message\n", "NAMESTORE_RECORD_REMOVE"); + struct GNUNET_NAMESTORE_Client *nc; + struct RecordRemoveResponseMessage rrr_msg; + size_t name_len; + size_t msg_size; + size_t msg_size_exp; + uint32_t id = 0; + + int res = GNUNET_SYSERR; + + if (ntohs (message->size) < sizeof (struct RecordRemoveMessage)) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + nc = client_lookup(client); + if (nc == NULL) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + struct RecordRemoveMessage * rp_msg = (struct RecordRemoveMessage *) message; + id = ntohl (rp_msg->op_id); + name_len = ntohs (rp_msg->name_len); + msg_size = ntohs (message->size); + msg_size_exp = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData); + + if (msg_size != msg_size_exp) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Expected message %u size but message size is %u \n", msg_size_exp, msg_size); + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + + if ((name_len == 0) || (name_len > 256)) + { + GNUNET_break_op (0); + GNUNET_SERVER_receive_done (client, GNUNET_OK); + return; + } + + /* DO WORK HERE */ + + /* Send response */ + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message\n", "RECORD_REMOVE_RESPONSE"); + rrr_msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE); + rrr_msg.op_id = rp_msg->op_id; + rrr_msg.header.size = htons (sizeof (struct RecordRemoveResponseMessage)); + if (GNUNET_OK == res) + rrr_msg.op_result = htons (GNUNET_OK); + else + rrr_msg.op_result = htons (GNUNET_NO); + GNUNET_SERVER_notification_context_unicast (snc, nc->client, (const struct GNUNET_MessageHeader *) &rrr_msg, GNUNET_NO); + + GNUNET_SERVER_receive_done (client, GNUNET_OK); +} + + + /** * Process template requests. * @@ -457,8 +596,12 @@ run (void *cls, struct GNUNET_SERVER_Handle *server, GNUNET_MESSAGE_TYPE_NAMESTORE_START, sizeof (struct StartMessage)}, {&handle_lookup_name, NULL, GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME, 0}, - {&handle_record_put, NULL, - GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0}, + {&handle_record_put, NULL, + GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT, 0}, + {&handle_record_create, NULL, + GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE, 0}, + {&handle_record_remove, NULL, + GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE, 0}, {NULL, NULL, 0, 0} }; diff --git a/src/namestore/namestore.h b/src/namestore/namestore.h index 03b4bb45a..af46ee4cb 100644 --- a/src/namestore/namestore.h +++ b/src/namestore/namestore.h @@ -33,6 +33,11 @@ #define GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPONSE 432 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT 433 #define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE 434 +#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE 435 +#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE 436 +#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE 437 +#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE 438 + GNUNET_NETWORK_STRUCT_BEGIN /** @@ -159,16 +164,69 @@ struct RecordPutMessage }; GNUNET_NETWORK_STRUCT_END +/** + * Put a record to the namestore response + */ +GNUNET_NETWORK_STRUCT_BEGIN +struct RecordPutResponseMessage +{ + /** + * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE + */ + struct GNUNET_MessageHeader header; + + /** + * Operation ID in NBO + */ + uint32_t op_id; + + /* Contenct starts here */ + + /** + * name length: GNUNET_NO (0) on error, GNUNET_OK (1) on success + */ + uint16_t op_result; +}; +GNUNET_NETWORK_STRUCT_END + + /** * Put a record to the namestore * Memory layout: * [struct RecordPutMessage][struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded][char *name][rc_count * struct GNUNET_NAMESTORE_RecordData] */ GNUNET_NETWORK_STRUCT_BEGIN -struct RecordPutResponseMessage +struct RecordCreateMessage { /** - * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_PUT_RESPONSE + * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE + */ + struct GNUNET_MessageHeader header; + + /** + * Operation ID in NBO + */ + uint32_t op_id; + + /* Contenct starts here */ + + /* name length */ + uint16_t name_len; + + struct GNUNET_CRYPTO_RsaSignature signature; +}; +GNUNET_NETWORK_STRUCT_END + + +/** + * Create a record to the namestore response + * Memory layout: + */ +GNUNET_NETWORK_STRUCT_BEGIN +struct RecordCreateResponseMessage +{ + /** + * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE */ struct GNUNET_MessageHeader header; @@ -186,6 +244,58 @@ struct RecordPutResponseMessage }; GNUNET_NETWORK_STRUCT_END +/** + * Remove a record from the namestore + * Memory layout: + * [struct RecordRemoveMessage][char *name][struct GNUNET_NAMESTORE_RecordData] + */ +GNUNET_NETWORK_STRUCT_BEGIN +struct RecordRemoveMessage +{ + /** + * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE + */ + struct GNUNET_MessageHeader header; + + /** + * Operation ID in NBO + */ + uint32_t op_id; + + /* Contenct starts here */ + + /* name length */ + uint16_t name_len; + + struct GNUNET_CRYPTO_RsaSignature signature; +}; +GNUNET_NETWORK_STRUCT_END + + +/** + * Remove a record from the namestore response + */ +GNUNET_NETWORK_STRUCT_BEGIN +struct RecordRemoveResponseMessage +{ + /** + * Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE + */ + struct GNUNET_MessageHeader header; + + /** + * Operation ID in NBO + */ + uint32_t op_id; + + /* Contenct starts here */ + + /** + * name length: GNUNET_NO (0) on error, GNUNET_OK (1) on success + */ + uint16_t op_result; +}; +GNUNET_NETWORK_STRUCT_END /* end of namestore.h */ diff --git a/src/namestore/namestore_api.c b/src/namestore/namestore_api.c index 882e5a5de..474a40230 100644 --- a/src/namestore/namestore_api.c +++ b/src/namestore/namestore_api.c @@ -285,6 +285,46 @@ handle_record_put_response (struct GNUNET_NAMESTORE_QueueEntry *qe, return; } + /* Operation done, remove */ + GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe); + + GNUNET_free (qe); +} + + +static void +handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe, + struct RecordCreateResponseMessage* msg, + size_t size) +{ + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received `%s' \n", + "RECORD_CREATE_RESPONSE"); + + struct GNUNET_NAMESTORE_Handle *h = qe->nsh; + int res = GNUNET_OK; + + if (ntohs (msg->op_result) == GNUNET_OK) + { + res = GNUNET_OK; + if (qe->cont != NULL) + { + qe->cont (qe->cont_cls, res, _("Namestore added record successfully")); + } + + } + else if (ntohs (msg->op_result) == GNUNET_NO) + { + res = GNUNET_SYSERR; + if (qe->cont != NULL) + { + qe->cont (qe->cont_cls, res, _("Namestore failed to add record")); + } + } + else + { + GNUNET_break_op (0); + return; + } /* Operation done, remove */ GNUNET_CONTAINER_DLL_remove(h->op_head, h->op_tail, qe); @@ -373,6 +413,14 @@ process_namestore_message (void *cls, const struct GNUNET_MessageHeader *msg) } handle_record_put_response (qe, (struct RecordPutResponseMessage *) msg, size); break; + case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE_RESPONSE: + if (size != sizeof (struct RecordCreateResponseMessage)) + { + GNUNET_break_op (0); + break; + } + handle_record_create_response (qe, (struct RecordCreateResponseMessage *) msg, size); + break; default: GNUNET_break_op (0); break; @@ -549,6 +597,13 @@ GNUNET_NAMESTORE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) } +/** + * Disconnect from the namestore service (and free associated + * resources). + * + * @param h handle to the namestore + * @param drop set to GNUNET_YES to delete all data in namestore (!) + */ void GNUNET_NAMESTORE_disconnect (struct GNUNET_NAMESTORE_Handle *h, int drop) { @@ -667,12 +722,6 @@ GNUNET_NAMESTORE_record_put (struct GNUNET_NAMESTORE_Handle *h, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_PUT", name, msg_size); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "CALC: %u %u %u %u\n", - sizeof (struct RecordPutMessage), - sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), - name_len, - rd_count * (sizeof (struct GNUNET_NAMESTORE_RecordData))); - GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe); do_transmit(h); @@ -724,20 +773,43 @@ GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h, { struct GNUNET_NAMESTORE_QueueEntry *qe; struct PendingMessage *pe; + struct GNUNET_NAMESTORE_RecordData * rd_tmp; + char * name_tmp; size_t msg_size = 0; + size_t name_len = 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; - get_op_id(h); + qe->op_id = id; /* set msg_size*/ - pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); + struct RecordCreateMessage * msg; + msg_size = sizeof (struct RecordCreateMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData); /* create msg here */ + pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); + pe->size = msg_size; + pe->is_init = GNUNET_NO; + msg = (struct RecordCreateMessage *) &pe[1]; + + name_tmp = (char *) &msg[1]; + rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len]; + + msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_CREATE); + msg->header.size = htons (msg_size); + msg->op_id = htonl (id); + //msg->signature = *signature; + msg->name_len = htons (name_len); + memcpy (name_tmp, name, name_len); + memcpy (rd_tmp, rd, sizeof (struct GNUNET_NAMESTORE_RecordData)); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_CREATE", name, msg_size); GNUNET_CONTAINER_DLL_insert (h->pending_head, h->pending_tail, pe); do_transmit(h); @@ -770,38 +842,46 @@ GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h, { struct GNUNET_NAMESTORE_QueueEntry *qe; struct PendingMessage *pe; + struct GNUNET_NAMESTORE_RecordData * rd_tmp; + char * name_tmp; size_t msg_size = 0; + size_t name_len = 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; - get_op_id(h); + qe->op_id = id; /* set msg_size*/ - pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); + struct RecordRemoveMessage * msg; + msg_size = sizeof (struct RecordRemoveMessage) + name_len + sizeof (struct GNUNET_NAMESTORE_RecordData); /* create msg here */ + pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); + pe->size = msg_size; + pe->is_init = GNUNET_NO; + msg = (struct RecordRemoveMessage *) &pe[1]; + + name_tmp = (char *) &msg[1]; + rd_tmp = (struct GNUNET_NAMESTORE_RecordData *) &name_tmp[name_len]; + + msg->header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE); + msg->header.size = htons (msg_size); + msg->op_id = htonl (id); + //msg->signature = *signature; + msg->name_len = htons (name_len); + memcpy (name_tmp, name, name_len); + memcpy (rd_tmp, rd, sizeof (struct GNUNET_NAMESTORE_RecordData)); + + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message for name `%s' with size %u\n", "NAMESTORE_RECORD_REMOVE", name, msg_size); 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) - { - if (strcmp ( iter->name, name ) && - iter->record_type == record_type && - GNUNET_CRYPTO_hash_cmp (iter->zone, zone)) - break; - } - if (iter) - GNUNET_CONTAINER_DLL_remove(h->records_head, - h->records_tail, - iter); -#endif return qe; }