From: Christian Grothoff Date: Fri, 22 Jan 2010 15:52:40 +0000 (+0000) Subject: stuff X-Git-Tag: initial-import-from-subversion-38251~22874 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=48436941449d867ef9a32ddf29439c5017d37775;p=oweals%2Fgnunet.git stuff --- diff --git a/src/core/core_api.c b/src/core/core_api.c index e7e958bd7..a23313499 100644 --- a/src/core/core_api.c +++ b/src/core/core_api.c @@ -87,9 +87,14 @@ struct GNUNET_CORE_Handle const struct GNUNET_CORE_MessageHandler *handlers; /** - * Our connection to the service. + * Our connection to the service for notifications. */ - struct GNUNET_CLIENT_Connection *client; + struct GNUNET_CLIENT_Connection *client_notifications; + + /** + * Our connection to the service for normal requests. + */ + struct GNUNET_CLIENT_Connection *client_requests; /** * Handle for our current transmission request. @@ -263,10 +268,10 @@ static size_t transmit_start (void *cls, size_t size, void *buf); static void reconnect (struct GNUNET_CORE_Handle *h) { - GNUNET_CLIENT_disconnect (h->client); + GNUNET_CLIENT_disconnect (h->client_notifications); h->currently_down = GNUNET_YES; - h->client = GNUNET_CLIENT_connect (h->sched, "core", h->cfg); - h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, + h->client_notifications = GNUNET_CLIENT_connect (h->sched, "core", h->cfg); + h->th = GNUNET_CLIENT_notify_transmit_ready (h->client_notifications, sizeof (struct InitMessage) + sizeof (uint16_t) * h->hcnt, GNUNET_TIME_UNIT_SECONDS, @@ -346,6 +351,7 @@ static void trigger_next_request (struct GNUNET_CORE_Handle *h) { struct GNUNET_CORE_TransmitHandle *th; + if (h->currently_down) return; /* connection temporarily down */ if (NULL == (th = h->pending_head)) @@ -356,7 +362,7 @@ trigger_next_request (struct GNUNET_CORE_Handle *h) GNUNET_SCHEDULER_cancel (h->sched, th->timeout_task); th->timeout_task = GNUNET_SCHEDULER_NO_TASK; } - h->th = GNUNET_CLIENT_notify_transmit_ready (h->client, + h->th = GNUNET_CLIENT_notify_transmit_ready (h->client_requests, th->msize, GNUNET_TIME_absolute_get_remaining (th->timeout), @@ -367,20 +373,19 @@ trigger_next_request (struct GNUNET_CORE_Handle *h) /** - * Handler for most messages received from the core. + * Handler for notification messages received from the core. * * @param cls our "struct GNUNET_CORE_Handle" * @param msg the message received from the core service */ static void -main_handler (void *cls, const struct GNUNET_MessageHeader *msg) +main_notify_handler (void *cls, const struct GNUNET_MessageHeader *msg) { struct GNUNET_CORE_Handle *h = cls; unsigned int hpos; const struct ConnectNotifyMessage *cnm; const struct DisconnectNotifyMessage *dnm; const struct NotifyTrafficMessage *ntm; - const struct ConfigurationInfoMessage *cim; const struct GNUNET_MessageHeader *em; uint16_t msize; uint16_t et; @@ -524,38 +529,15 @@ main_handler (void *cls, const struct GNUNET_MessageHeader *msg) GNUNET_TIME_relative_ntoh (ntm->latency), ntohl (ntm->distance)); break; - case GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO: - if (msize != sizeof (struct ConfigurationInfoMessage)) - { - GNUNET_break (0); - break; - } - if (NULL == h->submitted) - break; - cim = (const struct ConfigurationInfoMessage *) msg; - - /* process configuration data */ - if (h->submitted->info != NULL) - h->submitted->info (h->submitted->info_cls, - &h->submitted->peer, - ntohl (cim->bpm_in), - ntohl (cim->bpm_out), - (int) ntohl (cim->reserved_amount), - cim->preference); - /* done, clean up! */ - GNUNET_CORE_notify_transmit_ready_cancel (h->submitted); // HUH? - trigger_next_request (h); - break; default: GNUNET_break (0); break; } - GNUNET_CLIENT_receive (h->client, - &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); + GNUNET_CLIENT_receive (h->client_notifications, + &main_notify_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); } - /** * Function called when we are ready to transmit our * "START" message (or when this operation timed out). @@ -606,8 +588,8 @@ init_reply_handler (void *cls, const struct GNUNET_MessageHeader *msg) #endif h->currently_down = GNUNET_NO; trigger_next_request (h); - GNUNET_CLIENT_receive (h->client, - &main_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); + GNUNET_CLIENT_receive (h->client_notifications, + &main_notify_handler, h, GNUNET_TIME_UNIT_FOREVER_REL); if (NULL != (init = h->init)) { /* mark so we don't call init on reconnect */ @@ -710,7 +692,7 @@ transmit_start (void *cls, size_t size, void *buf) ts = (uint16_t *) & init[1]; for (hpos = 0; hpos < h->hcnt; hpos++) ts[hpos] = htons (h->handlers[hpos].type); - GNUNET_CLIENT_receive (h->client, + GNUNET_CLIENT_receive (h->client_notifications, &init_reply_handler, h, GNUNET_TIME_absolute_get_remaining @@ -774,8 +756,8 @@ GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched, h->inbound_hdr_only = inbound_hdr_only; h->outbound_hdr_only = outbound_hdr_only; h->handlers = handlers; - h->client = GNUNET_CLIENT_connect (sched, "core", cfg); - if (h->client == NULL) + h->client_notifications = GNUNET_CLIENT_connect (sched, "core", cfg); + if (h->client_notifications == NULL) { GNUNET_free (h); return NULL; @@ -793,7 +775,7 @@ GNUNET_CORE_connect (struct GNUNET_SCHEDULER_Handle *sched, timeout.value); #endif h->th = - GNUNET_CLIENT_notify_transmit_ready (h->client, + GNUNET_CLIENT_notify_transmit_ready (h->client_notifications, sizeof (struct InitMessage) + sizeof (uint16_t) * h->hcnt, timeout, GNUNET_YES, @@ -816,7 +798,9 @@ GNUNET_CORE_disconnect (struct GNUNET_CORE_Handle *handle) GNUNET_CORE_notify_transmit_ready_cancel (handle->solicit_transmit_req); if (handle->reconnect_task != GNUNET_SCHEDULER_NO_TASK) GNUNET_SCHEDULER_cancel (handle->sched, handle->reconnect_task); - GNUNET_CLIENT_disconnect (handle->client); + GNUNET_CLIENT_disconnect (handle->client_notifications); + if (handle->client_requests != NULL) + GNUNET_CLIENT_disconnect (handle->client_requests); GNUNET_free_non_null (handle->solicit_buffer); GNUNET_free (handle); } diff --git a/src/core/gnunet-service-core.c b/src/core/gnunet-service-core.c index 915217ea1..842197fb1 100644 --- a/src/core/gnunet-service-core.c +++ b/src/core/gnunet-service-core.c @@ -538,31 +538,6 @@ struct Neighbour }; -/** - * Events are messages for clients. The struct - * itself is followed by the actual message. - */ -struct Event -{ - /** - * This is a linked list. - */ - struct Event *next; - - /** - * Size of the message. - */ - size_t size; - - /** - * Could this event be dropped if this queue - * is getting too large? (NOT YET USED!) - */ - int can_drop; - -}; - - /** * Data structure for each client connected to the core service. */ @@ -578,23 +553,6 @@ struct Client */ struct GNUNET_SERVER_Client *client_handle; - /** - * Linked list of messages we still need to deliver to - * this client. - */ - struct Event *event_head; - - /** - * Tail of the linked list of events. - */ - struct Event *event_tail; - - /** - * Current transmit handle, NULL if no transmission request - * is pending. - */ - struct GNUNET_CONNECTION_TransmitHandle *th; - /** * Array of the types of messages this peer cares * about (with "tcnt" entries). Allocated as part @@ -657,6 +615,11 @@ static struct GNUNET_TRANSPORT_Handle *transport; */ static struct Client *clients; +/** + * Context for notifications we need to send to our clients. + */ +static struct GNUNET_SERVER_NotificationContext *notifier; + /** * We keep neighbours in a linked list (for now). */ @@ -761,110 +724,6 @@ find_neighbour (const struct GNUNET_PeerIdentity *peer) } -/** - * Find the entry for the given client. - * - * @param client handle for the client - * @return NULL if we are not connected, otherwise the - * client's struct. - */ -static struct Client * -find_client (const struct GNUNET_SERVER_Client *client) -{ - struct Client *ret; - - ret = clients; - while ((ret != NULL) && (client != ret->client_handle)) - ret = ret->next; - return ret; -} - - -/** - * If necessary, initiate a request with the server to - * transmit messages from the queue of the given client. - * @param client who to transfer messages to - */ -static void request_transmit (struct Client *client); - - -/** - * Client is ready to receive data, provide it. - * - * @param cls closure - * @param size number of bytes available in buf - * @param buf where the callee should write the message - * @return number of bytes written to buf - */ -static size_t -do_client_transmit (void *cls, size_t size, void *buf) -{ - struct Client *client = cls; - struct Event *e; - char *tgt; - size_t ret; - - client->th = NULL; -#if DEBUG_CORE_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Client ready to receive %u bytes.\n", size); -#endif - if (buf == NULL) - { -#if DEBUG_CORE - GNUNET_log (GNUNET_ERROR_TYPE_WARNING, - "Failed to transmit data to client (disconnect)?\n"); -#endif - return 0; /* we'll surely get a disconnect soon... */ - } - tgt = buf; - ret = 0; - while ((NULL != (e = client->event_head)) && (e->size <= size)) - { - memcpy (&tgt[ret], &e[1], e->size); - size -= e->size; - ret += e->size; - client->event_head = e->next; - GNUNET_free (e); - } - GNUNET_assert (ret > 0); - if (client->event_head == NULL) - client->event_tail = NULL; -#if DEBUG_CORE_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Transmitting %u bytes to client\n", ret); -#endif - request_transmit (client); - return ret; -} - - -/** - * If necessary, initiate a request with the server to - * transmit messages from the queue of the given client. - * @param client who to transfer messages to - */ -static void -request_transmit (struct Client *client) -{ - - if (NULL != client->th) - return; /* already pending */ - if (NULL == client->event_head) - return; /* no more events pending */ -#if DEBUG_CORE_CLIENT - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Asking server to transmit %u bytes to client\n", - client->event_head->size); -#endif - client->th - = GNUNET_SERVER_notify_transmit_ready (client->client_handle, - client->event_head->size, - GNUNET_TIME_UNIT_FOREVER_REL, - &do_client_transmit, client); -} - - /** * Send a message to one of our clients. * @@ -875,51 +734,28 @@ request_transmit (struct Client *client) */ static void send_to_client (struct Client *client, - const struct GNUNET_MessageHeader *msg, int can_drop) + const struct GNUNET_MessageHeader *msg, + int can_drop) { - struct Event *e; - unsigned int queue_size; - uint16_t msize; - #if DEBUG_CORE_CLIENT GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Preparing to send message of type %u to client.\n", ntohs (msg->type)); -#endif - queue_size = 0; - e = client->event_head; - while (e != NULL) - { - queue_size++; - e = e->next; - } - if ( (queue_size >= MAX_CLIENT_QUEUE_SIZE) && - (can_drop == GNUNET_YES) ) - { -#if DEBUG_CORE - GNUNET_log (GNUNET_ERROR_TYPE_INFO, - "Too many messages in queue for the client, dropping the new message.\n"); -#endif - return; - } - - msize = ntohs (msg->size); - e = GNUNET_malloc (sizeof (struct Event) + msize); - /* append */ - if (client->event_tail != NULL) - client->event_tail->next = e; - else - client->event_head = e; - client->event_tail = e; - e->can_drop = can_drop; - e->size = msize; - memcpy (&e[1], msg, msize); - request_transmit (client); +#endif + GNUNET_SERVER_notification_context_unicast (notifier, + client->client_handle, + msg, + can_drop); } /** - * Send a message to all of our current clients. + * Send a message to all of our current clients that have + * the right options set. + * + * @param msg message to multicast + * @param can_drop can this message be discarded if the queue is too long + * @param options mask to use */ static void send_to_all_clients (const struct GNUNET_MessageHeader *msg, @@ -978,6 +814,7 @@ handle_client_init (void *cls, GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } + GNUNET_SERVER_notification_context_add (notifier, client); im = (const struct InitMessage *) message; types = (const uint16_t *) &im[1]; msize -= sizeof (struct InitMessage); @@ -1017,7 +854,6 @@ handle_client_init (void *cls, send_to_client (c, &cnm.header, GNUNET_NO); n = n->next; } - GNUNET_SERVER_receive_done (client, GNUNET_OK); } @@ -1032,7 +868,6 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) { struct Client *pos; struct Client *prev; - struct Event *e; if (client == NULL) return; @@ -1050,13 +885,6 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) clients = pos->next; else prev->next = pos->next; - if (pos->th != NULL) - GNUNET_CONNECTION_notify_transmit_ready_cancel (pos->th); - while (NULL != (e = pos->event_head)) - { - pos->event_head = e->next; - GNUNET_free (e); - } GNUNET_free (pos); return; } @@ -1072,15 +900,15 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) */ static void handle_client_request_info (void *cls, - struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) + struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { const struct RequestInfoMessage *rcm; struct Neighbour *n; struct ConfigurationInfoMessage cim; - struct Client *c; int reserv; unsigned long long old_preference; + struct GNUNET_SERVER_TransmitContext *tc; #if DEBUG_CORE_CLIENT GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, @@ -1128,17 +956,15 @@ handle_client_request_info (void *cls, cim.header.size = htons (sizeof (struct ConfigurationInfoMessage)); cim.header.type = htons (GNUNET_MESSAGE_TYPE_CORE_CONFIGURATION_INFO); cim.peer = rcm->peer; - c = find_client (client); - if (c == NULL) - { - GNUNET_break (0); - return; - } + #if DEBUG_CORE_CLIENT GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending `%s' message to client.\n", "CONFIGURATION_INFO"); #endif - send_to_client (c, &cim.header, GNUNET_NO); + tc = GNUNET_SERVER_transmit_context_create (client); + GNUNET_SERVER_transmit_context_append_message (tc, &cim.header); + GNUNET_SERVER_transmit_context_run (tc, + GNUNET_TIME_UNIT_FOREVER_REL); } @@ -3252,6 +3078,8 @@ cleaning_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc) neighbour_count--; free_neighbour (n); } + GNUNET_SERVER_notification_context_destroy (notifier); + notifier = NULL; while (NULL != (c = clients)) handle_client_disconnect (NULL, c->client_handle); if (my_private_key != NULL) @@ -3281,7 +3109,7 @@ run (void *cls, char *keyfile; sched = s; - cfg = c; + cfg = c; /* parse configuration */ if ( (GNUNET_OK != @@ -3330,6 +3158,7 @@ run (void *cls, sizeof (my_public_key), &my_identity.hashPubKey); /* setup notification */ server = serv; + notifier = GNUNET_SERVER_notification_context_create (server, 0); GNUNET_SERVER_disconnect_notify (server, &handle_client_disconnect, NULL); /* setup transport connection */ transport = GNUNET_TRANSPORT_connect (sched, diff --git a/src/fs/gnunet-service-fs.c b/src/fs/gnunet-service-fs.c index b8855632c..fa83eb889 100644 --- a/src/fs/gnunet-service-fs.c +++ b/src/fs/gnunet-service-fs.c @@ -961,9 +961,9 @@ signal_index_ok (struct IndexInfo *ii) ii->filename, (const char*) GNUNET_CONTAINER_multihashmap_get (ifm, &ii->file_id)); - GNUNET_SERVER_transmit_context_append (ii->tc, - NULL, 0, - GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK); + GNUNET_SERVER_transmit_context_append_data (ii->tc, + NULL, 0, + GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK); GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES); GNUNET_free (ii); @@ -972,9 +972,9 @@ signal_index_ok (struct IndexInfo *ii) ii->next = indexed_files; indexed_files = ii; write_index_list (); - GNUNET_SERVER_transmit_context_append (ii->tc, - NULL, 0, - GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK); + GNUNET_SERVER_transmit_context_append_data (ii->tc, + NULL, 0, + GNUNET_MESSAGE_TYPE_FS_INDEX_START_OK); GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES); ii->tc = NULL; @@ -1009,9 +1009,9 @@ hash_for_index_val (void *cls, "Wanted `%s'\n", GNUNET_h2s (&ii->file_id)); #endif - GNUNET_SERVER_transmit_context_append (ii->tc, - NULL, 0, - GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED); + GNUNET_SERVER_transmit_context_append_data (ii->tc, + NULL, 0, + GNUNET_MESSAGE_TYPE_FS_INDEX_START_FAILED); GNUNET_SERVER_transmit_context_run (ii->tc, GNUNET_TIME_UNIT_MINUTES); GNUNET_free (ii); @@ -1110,17 +1110,13 @@ handle_index_list_get (void *cls, char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE]; size_t slen; const char *fn; - struct GNUNET_MessageHeader *msg; struct IndexInfo *pos; tc = GNUNET_SERVER_transmit_context_create (client); iim = (struct IndexInfoMessage*) buf; - msg = &iim->header; pos = indexed_files; while (NULL != pos) { - iim->reserved = 0; - iim->file_id = pos->file_id; fn = pos->filename; slen = strlen (fn) + 1; if (slen + sizeof (struct IndexInfoMessage) > @@ -1129,18 +1125,18 @@ handle_index_list_get (void *cls, GNUNET_break (0); break; } + iim->header.type = htons (GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY); + iim->header.size = htons (slen + sizeof (struct IndexInfoMessage)); + iim->reserved = 0; + iim->file_id = pos->file_id; memcpy (&iim[1], fn, slen); - GNUNET_SERVER_transmit_context_append - (tc, - &msg[1], - sizeof (struct IndexInfoMessage) - - sizeof (struct GNUNET_MessageHeader) + slen, - GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_ENTRY); + GNUNET_SERVER_transmit_context_append_message (tc, + &iim->header); pos = pos->next; } - GNUNET_SERVER_transmit_context_append (tc, - NULL, 0, - GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END); + GNUNET_SERVER_transmit_context_append_data (tc, + NULL, 0, + GNUNET_MESSAGE_TYPE_FS_INDEX_LIST_END); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_MINUTES); } @@ -1198,9 +1194,9 @@ handle_unindex (void *cls, if (GNUNET_YES == found) write_index_list (); tc = GNUNET_SERVER_transmit_context_create (client); - GNUNET_SERVER_transmit_context_append (tc, - NULL, 0, - GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK); + GNUNET_SERVER_transmit_context_append_data (tc, + NULL, 0, + GNUNET_MESSAGE_TYPE_FS_UNINDEX_OK); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_MINUTES); } diff --git a/src/include/gnunet_server_lib.h b/src/include/gnunet_server_lib.h index 5609d7a9e..351c0a7c1 100644 --- a/src/include/gnunet_server_lib.h +++ b/src/include/gnunet_server_lib.h @@ -492,9 +492,23 @@ struct GNUNET_SERVER_TransmitContext * @param type type of the message */ void -GNUNET_SERVER_transmit_context_append (struct GNUNET_SERVER_TransmitContext - *tc, const void *data, size_t length, - uint16_t type); +GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext + *tc, const void *data, size_t length, + uint16_t type); + + +/** + * Append a message to the transmission context. + * All messages in the context will be sent by + * the transmit_context_run method. + * + * @param tc context to use + * @param msg message to append + */ +void +GNUNET_SERVER_transmit_context_append_message (struct GNUNET_SERVER_TransmitContext + *tc, const struct GNUNET_MessageHeader *msg); + /** * Execute a transmission context. If there is diff --git a/src/peerinfo/gnunet-service-peerinfo.c b/src/peerinfo/gnunet-service-peerinfo.c index 5b2b999d6..9ac38ea9f 100644 --- a/src/peerinfo/gnunet-service-peerinfo.c +++ b/src/peerinfo/gnunet-service-peerinfo.c @@ -470,7 +470,6 @@ send_to_each_host (const struct GNUNET_PeerIdentity *only, { struct HostEntry *pos; struct InfoMessage *im; - const struct GNUNET_MessageHeader *end; uint16_t hs; char buf[GNUNET_SERVER_MAX_MESSAGE_SIZE]; struct GNUNET_SERVER_TransmitContext *tc; @@ -487,6 +486,8 @@ send_to_each_host (const struct GNUNET_PeerIdentity *only, change_host_trust (&pos->identity, trust_change); hs = 0; im = (struct InfoMessage *) buf; + im->header.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_INFO); + im->header.size = htons (sizeof (struct InfoMessage) + hs); if (pos->hello != NULL) { hs = GNUNET_HELLO_size (pos->hello); @@ -497,19 +498,13 @@ send_to_each_host (const struct GNUNET_PeerIdentity *only, } im->trust = htonl (pos->trust); im->peer = pos->identity; - end = &im->header; - GNUNET_SERVER_transmit_context_append (tc, - &end[1], - hs + - sizeof (struct InfoMessage) - - sizeof (struct - GNUNET_MessageHeader), - GNUNET_MESSAGE_TYPE_PEERINFO_INFO); + GNUNET_SERVER_transmit_context_append_message (tc, + &im->header); } pos = pos->next; } - GNUNET_SERVER_transmit_context_append (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); + GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, + GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } diff --git a/src/statistics/gnunet-service-statistics.c b/src/statistics/gnunet-service-statistics.c index c90038e03..428ce932f 100644 --- a/src/statistics/gnunet-service-statistics.c +++ b/src/statistics/gnunet-service-statistics.c @@ -218,16 +218,15 @@ transmit (struct GNUNET_SERVER_TransmitContext *tc, const struct StatsEntry *e) { struct GNUNET_STATISTICS_ReplyMessage *m; - struct GNUNET_MessageHeader *h; size_t size; - uint16_t msize; size = sizeof (struct GNUNET_STATISTICS_ReplyMessage) + strlen (e->service) + 1 + strlen (e->name) + 1; GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE); - msize = size - sizeof (struct GNUNET_MessageHeader); m = GNUNET_malloc (size); + m->header.type = htons (GNUNET_MESSAGE_TYPE_STATISTICS_VALUE); + m->header.size = htons (size); m->uid = htonl (e->uid); if (e->persistent) m->uid |= htonl (GNUNET_STATISTICS_PERSIST_BIT); @@ -241,11 +240,7 @@ transmit (struct GNUNET_SERVER_TransmitContext *tc, "Transmitting value for `%s:%s': %llu\n", e->service, e->name, e->value); #endif - h = &m->header; - GNUNET_SERVER_transmit_context_append (tc, - &h[1], - msize, - GNUNET_MESSAGE_TYPE_STATISTICS_VALUE); + GNUNET_SERVER_transmit_context_append_message (tc, &m->header); GNUNET_free (m); } @@ -303,8 +298,8 @@ handle_get (void *cls, transmit (tc, pos); pos = pos->next; } - GNUNET_SERVER_transmit_context_append (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_STATISTICS_END); + GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, + GNUNET_MESSAGE_TYPE_STATISTICS_END); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } diff --git a/src/transport/gnunet-service-transport.c b/src/transport/gnunet-service-transport.c index fe8b5043e..4251e7890 100644 --- a/src/transport/gnunet-service-transport.c +++ b/src/transport/gnunet-service-transport.c @@ -2587,8 +2587,8 @@ transmit_address_to_client (void *cls, const char *address) slen = 0; else slen = strlen (address) + 1; - GNUNET_SERVER_transmit_context_append (tc, address, slen, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_SERVER_transmit_context_append_data (tc, address, slen, + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); if (NULL == address) GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } @@ -2644,8 +2644,8 @@ handle_address_lookup (void *cls, if (NULL == lsPlugin) { tc = GNUNET_SERVER_transmit_context_create (client); - GNUNET_SERVER_transmit_context_append (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); + GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, + GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY); GNUNET_SERVER_transmit_context_run (tc, rtimeout); return; } diff --git a/src/util/gnunet-service-resolver.c b/src/util/gnunet-service-resolver.c index b3e40b889..59d4c1d97 100644 --- a/src/util/gnunet-service-resolver.c +++ b/src/util/gnunet-service-resolver.c @@ -227,12 +227,12 @@ get_ip_as_string (struct GNUNET_SERVER_Client *client, } tc = GNUNET_SERVER_transmit_context_create (client); if (cache->addr != NULL) - GNUNET_SERVER_transmit_context_append (tc, - cache->addr, - strlen (cache->addr) + 1, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); - GNUNET_SERVER_transmit_context_append (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_SERVER_transmit_context_append_data (tc, + cache->addr, + strlen (cache->addr) + 1, + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } @@ -280,10 +280,10 @@ getaddrinfo_resolve (struct GNUNET_SERVER_TransmitContext *tc, pos = result; while (pos != NULL) { - GNUNET_SERVER_transmit_context_append (tc, - result->ai_addr, - result->ai_addrlen, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_SERVER_transmit_context_append_data (tc, + result->ai_addr, + result->ai_addrlen, + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); pos = pos->ai_next; } freeaddrinfo (result); @@ -327,10 +327,10 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, memset (&a4, 0, sizeof (a4)); a4.sin_family = AF_INET; memcpy (&a4.sin_addr, hp->h_addr_list[0], hp->h_length); - GNUNET_SERVER_transmit_context_append (tc, - &a4, - sizeof (a4), - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_SERVER_transmit_context_append_data (tc, + &a4, + sizeof (a4), + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); } else { @@ -338,10 +338,10 @@ gethostbyname2_resolve (struct GNUNET_SERVER_TransmitContext *tc, memset (&a6, 0, sizeof (a6)); a6.sin6_family = AF_INET6; memcpy (&a6.sin6_addr, hp->h_addr_list[0], hp->h_length); - GNUNET_SERVER_transmit_context_append (tc, - &a6, - sizeof (a6), - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_SERVER_transmit_context_append_data (tc, + &a6, + sizeof (a6), + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); } return GNUNET_OK; } @@ -372,10 +372,10 @@ gethostbyname_resolve (struct GNUNET_SERVER_TransmitContext *tc, memset (&addr, 0, sizeof (addr)); addr.sin_family = AF_INET; memcpy (&addr.sin_addr, hp->h_addr_list[0], hp->h_length); - GNUNET_SERVER_transmit_context_append (tc, - &addr, - sizeof (addr), - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_SERVER_transmit_context_append_data (tc, + &addr, + sizeof (addr), + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); return GNUNET_OK; } #endif @@ -409,8 +409,8 @@ get_ip_from_hostname (struct GNUNET_SERVER_Client *client, if ((ret == GNUNET_NO) && ((domain == AF_UNSPEC) || (domain == PF_INET))) gethostbyname_resolve (tc, hostname); #endif - GNUNET_SERVER_transmit_context_append (tc, NULL, 0, - GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); + GNUNET_SERVER_transmit_context_append_data (tc, NULL, 0, + GNUNET_MESSAGE_TYPE_RESOLVER_RESPONSE); GNUNET_SERVER_transmit_context_run (tc, GNUNET_TIME_UNIT_FOREVER_REL); } diff --git a/src/util/server_tc.c b/src/util/server_tc.c index bffb892b1..7514b0cd3 100644 --- a/src/util/server_tc.c +++ b/src/util/server_tc.c @@ -149,9 +149,9 @@ GNUNET_SERVER_transmit_context_create (struct GNUNET_SERVER_Client *client) * @param type type of the message */ void -GNUNET_SERVER_transmit_context_append (struct GNUNET_SERVER_TransmitContext - *tc, const void *data, size_t length, - uint16_t type) +GNUNET_SERVER_transmit_context_append_data (struct GNUNET_SERVER_TransmitContext + *tc, const void *data, size_t length, + uint16_t type) { struct GNUNET_MessageHeader *msg; size_t size; @@ -168,6 +168,29 @@ GNUNET_SERVER_transmit_context_append (struct GNUNET_SERVER_TransmitContext } +/** + * Append a message to the transmission context. + * All messages in the context will be sent by + * the transmit_context_run method. + * + * @param tc context to use + * @param msg message to append + */ +void +GNUNET_SERVER_transmit_context_append_message(struct GNUNET_SERVER_TransmitContext + *tc, const struct GNUNET_MessageHeader *msg) +{ + struct GNUNET_MessageHeader *m; + uint16_t size; + + size = ntohs (msg->size); + tc->buf = GNUNET_realloc (tc->buf, tc->total + size); + m = (struct GNUNET_MessageHeader *) &tc->buf[tc->total]; + tc->total += size; + memcpy (m, msg, size); +} + + /** * Execute a transmission context. If there is * an error in the transmission, the receive_done