From caa4196867da8684c5b1fd2e747ecb9d21cbba8b Mon Sep 17 00:00:00 2001 From: Bart Polot Date: Wed, 20 Jul 2016 12:07:02 +0000 Subject: [PATCH] Change CADET ports from uint32 to HashCode --- src/cadet/cadet.h | 48 ++-- src/cadet/cadet_api.c | 265 ++++++++++++++--------- src/cadet/cadet_common.c | 15 +- src/cadet/cadet_protocol.h | 2 +- src/cadet/cadet_test_lib.c | 4 +- src/cadet/gnunet-cadet.c | 42 ++-- src/cadet/gnunet-service-cadet_channel.c | 31 ++- src/cadet/gnunet-service-cadet_channel.h | 2 +- src/cadet/gnunet-service-cadet_local.c | 151 +++++++------ src/cadet/gnunet-service-cadet_local.h | 6 +- src/cadet/gnunet-service-cadet_tunnel.c | 2 +- src/cadet/test_cadet.c | 2 +- src/include/gnunet_cadet_service.h | 66 ++++-- src/include/gnunet_protocols.h | 14 +- 14 files changed, 387 insertions(+), 263 deletions(-) diff --git a/src/cadet/cadet.h b/src/cadet/cadet.h index df0f02a22..61388bb15 100644 --- a/src/cadet/cadet.h +++ b/src/cadet/cadet.h @@ -63,22 +63,24 @@ extern "C" GNUNET_NETWORK_STRUCT_BEGIN + /** - * Message for a client to register to the service + * Message for a client to create and destroy channels. */ -struct GNUNET_CADET_ClientConnect +struct GNUNET_CADET_PortMessage { /** - * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT + * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_[OPEN|CLOSE] * - * Size: sizeof(struct GNUNET_CADET_ClientConnect) + - * sizeof(CADET_ApplicationType) * applications + - * sizeof(uint16_t) * types + * Size: sizeof(struct GNUNET_CADET_ChannelMessage) */ struct GNUNET_MessageHeader header; - /* uint32_t list_ports[] */ -}; + /** + * Port to open/close. + */ + struct GNUNET_HashCode port GNUNET_PACKED; +}; /** * Type for channel numbering. @@ -90,14 +92,14 @@ typedef uint32_t CADET_ChannelNumber; /** - * Message for a client to create and destroy channels. + * Message for a client to create channels. */ -struct GNUNET_CADET_ChannelMessage +struct GNUNET_CADET_ChannelCreateMessage { /** - * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_[CREATE|DESTROY] + * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_CREATE * - * Size: sizeof(struct GNUNET_CADET_ChannelMessage) + * Size: sizeof(struct GNUNET_CADET_ChannelCreateMessage) */ struct GNUNET_MessageHeader header; @@ -114,7 +116,7 @@ struct GNUNET_CADET_ChannelMessage /** * Port of the channel. */ - uint32_t port GNUNET_PACKED; + struct GNUNET_HashCode port; /** * Options. @@ -123,6 +125,25 @@ struct GNUNET_CADET_ChannelMessage }; +/** + * Message for a client to destroy channels. + */ +struct GNUNET_CADET_ChannelDestroyMessage +{ + /** + * Type: GNUNET_MESSAGE_TYPE_CADET_LOCAL_TUNNEL_DESTROY + * + * Size: sizeof(struct GNUNET_CADET_ChannelDestroyMessage) + */ + struct GNUNET_MessageHeader header; + + /** + * ID of a channel controlled by this client. + */ + CADET_ChannelNumber channel_id GNUNET_PACKED; +}; + + /** * Message for cadet data traffic. */ @@ -262,7 +283,6 @@ struct GNUNET_CADET_LocalInfoTunnel GNUNET_NETWORK_STRUCT_END - /** * @brief Translate a fwd variable into a string representation, for logging. * diff --git a/src/cadet/cadet_api.c b/src/cadet/cadet_api.c index 1ad3ec5b0..1ca8bad9d 100644 --- a/src/cadet/cadet_api.c +++ b/src/cadet/cadet_api.c @@ -142,12 +142,7 @@ struct GNUNET_CADET_Handle /** * Ports open. */ - const uint32_t *ports; - - /** - * Number of ports. - */ - unsigned int n_ports; + struct GNUNET_CONTAINER_MultiHashMap *ports; /** * Double linked list of the channels this client is connected to, head. @@ -159,11 +154,6 @@ struct GNUNET_CADET_Handle */ struct GNUNET_CADET_Channel *channels_tail; - /** - * Callback for inbound channel creation - */ - GNUNET_CADET_InboundChannelNotificationHandler *new_channel; - /** * Callback for inbound channel disconnection */ @@ -249,7 +239,6 @@ struct GNUNET_CADET_Peer */ struct GNUNET_CADET_Channel { - /** * DLL next */ @@ -271,9 +260,9 @@ struct GNUNET_CADET_Channel CADET_ChannelNumber chid; /** - * Port number. + * Channel's port, if any. */ - uint32_t port; + struct GNUNET_CADET_Port *port; /** * Other end of the channel. @@ -302,6 +291,32 @@ struct GNUNET_CADET_Channel }; +/** + * Opaque handle to a port. + */ +struct GNUNET_CADET_Port +{ + /** + * Handle to the CADET session this port belongs to. + */ + struct GNUNET_CADET_Handle *cadet; + + /** + * Port ID. + */ + struct GNUNET_HashCode *hash; + + /** + * Callback handler for incoming channels on this port. + */ + GNUNET_CADET_InboundChannelNotificationHandler *handler; + + /** + * Closure for @a handler. + */ + void *cls; +}; + /** * Implementation state for cadet's message queue. @@ -358,6 +373,25 @@ th_is_payload (struct GNUNET_CADET_TransmitHandle *th) } +/** + * Find the Port struct for a hash. + * + * @param h CADET handle. + * @param hash HashCode for the port number. + * + * @return The port handle if known, NULL otherwise. + */ +static struct GNUNET_CADET_Port * +find_port (const struct GNUNET_CADET_Handle *h, + const struct GNUNET_HashCode *hash) +{ + struct GNUNET_CADET_Port *p; + + p = GNUNET_CONTAINER_multihashmap_get (h->ports, hash); + + return p; +} + /** * Check whether there is any message ready in the queue and find the size. * @@ -603,45 +637,6 @@ static void reconnect_cbk (void *cls); -/** - * Send a connect packet to the service with the applications and types - * requested by the user. - * - * @param h The cadet handle. - * - */ -static void -send_connect (struct GNUNET_CADET_Handle *h) -{ - size_t size; - - size = sizeof (struct GNUNET_CADET_ClientConnect); - size += h->n_ports * sizeof (uint32_t); - { - char buf[size] GNUNET_ALIGN; - struct GNUNET_CADET_ClientConnect *msg; - uint32_t *ports; - uint16_t i; - - /* build connection packet */ - msg = (struct GNUNET_CADET_ClientConnect *) buf; - msg->header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT); - msg->header.size = htons (size); - ports = (uint32_t *) &msg[1]; - for (i = 0; i < h->n_ports; i++) - { - ports[i] = htonl (h->ports[i]); - LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", - h->ports[i]); - } - LOG (GNUNET_ERROR_TYPE_DEBUG, - "Sending %lu bytes long message with %u ports\n", - ntohs (msg->header.size), h->n_ports); - send_packet (h, &msg->header, NULL); - } -} - - /** * Reconnect to the service, retransmit all infomation to try to restore the * original state. @@ -690,7 +685,6 @@ do_reconnect (struct GNUNET_CADET_Handle *h) { h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS; } - send_connect (h); return GNUNET_YES; } @@ -746,21 +740,24 @@ reconnect (struct GNUNET_CADET_Handle *h) */ static void process_channel_created (struct GNUNET_CADET_Handle *h, - const struct GNUNET_CADET_ChannelMessage *msg) + const struct GNUNET_CADET_ChannelCreateMessage *msg) { struct GNUNET_CADET_Channel *ch; + struct GNUNET_CADET_Port *port; + const struct GNUNET_HashCode *port_number; CADET_ChannelNumber chid; - uint32_t port; chid = ntohl (msg->channel_id); - port = ntohl (msg->port); - LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X:%u\n", chid, port); + port_number = &msg->port; + LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating incoming channel %X [%s]\n", + chid, GNUNET_h2s (port_number)); if (chid < GNUNET_CADET_LOCAL_CHANNEL_ID_SERV) { GNUNET_break (0); return; } - if (NULL != h->new_channel) + port = find_port (h, port_number); + if (NULL != port) { void *ctx; @@ -773,23 +770,20 @@ process_channel_created (struct GNUNET_CADET_Handle *h, ch->options = ntohl (msg->opt); LOG (GNUNET_ERROR_TYPE_DEBUG, " created channel %p\n", ch); - ctx = h->new_channel (h->cls, ch, &msg->peer, ch->port, ch->options); + ctx = port->handler (port->cls, ch, &msg->peer, port->hash, ch->options); if (NULL != ctx) ch->ctx = ctx; LOG (GNUNET_ERROR_TYPE_DEBUG, "User notified\n"); } else { - struct GNUNET_CADET_ChannelMessage d_msg; + struct GNUNET_CADET_ChannelDestroyMessage d_msg; LOG (GNUNET_ERROR_TYPE_DEBUG, "No handler for incoming channels\n"); d_msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); - d_msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage)); + d_msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelDestroyMessage)); d_msg.channel_id = msg->channel_id; - memset (&d_msg.peer, 0, sizeof (struct GNUNET_PeerIdentity)); - d_msg.port = 0; - d_msg.opt = 0; send_packet (h, &d_msg.header, NULL); } @@ -805,7 +799,7 @@ process_channel_created (struct GNUNET_CADET_Handle *h, */ static void process_channel_destroy (struct GNUNET_CADET_Handle *h, - const struct GNUNET_CADET_ChannelMessage *msg) + const struct GNUNET_CADET_ChannelDestroyMessage *msg) { struct GNUNET_CADET_Channel *ch; CADET_ChannelNumber chid; @@ -1270,12 +1264,14 @@ msg_received (void *cls, const struct GNUNET_MessageHeader *msg) { /* Notify of a new incoming channel */ case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: - process_channel_created (h, (struct GNUNET_CADET_ChannelMessage *) msg); + process_channel_created (h, + (struct GNUNET_CADET_ChannelCreateMessage *) msg); break; /* Notify of a channel disconnection */ case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: /* TODO separate(gid problem)*/ case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_NACK: - process_channel_destroy (h, (struct GNUNET_CADET_ChannelMessage *) msg); + process_channel_destroy (h, + (struct GNUNET_CADET_ChannelDestroyMessage *) msg); break; case GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA: process_incoming_data (h, msg); @@ -1496,10 +1492,8 @@ send_packet (struct GNUNET_CADET_Handle *h, struct GNUNET_CADET_Handle * GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, - GNUNET_CADET_InboundChannelNotificationHandler new_channel, GNUNET_CADET_ChannelEndHandler cleaner, - const struct GNUNET_CADET_MessageHandler *handlers, - const uint32_t *ports) + const struct GNUNET_CADET_MessageHandler *handlers) { struct GNUNET_CADET_Handle *h; @@ -1507,8 +1501,8 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, h = GNUNET_new (struct GNUNET_CADET_Handle); LOG (GNUNET_ERROR_TYPE_DEBUG, " addr %p\n", h); h->cfg = cfg; - h->new_channel = new_channel; h->cleaner = cleaner; + h->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_YES); h->client = GNUNET_CLIENT_connect ("cadet", cfg); if (h->client == NULL) { @@ -1518,31 +1512,14 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, } h->cls = cls; h->message_handlers = handlers; - h->ports = ports; h->next_chid = GNUNET_CADET_LOCAL_CHANNEL_ID_CLI; h->reconnect_time = GNUNET_TIME_UNIT_MILLISECONDS; h->reconnect_task = NULL; - if (NULL != ports && ports[0] != 0 && NULL == new_channel) - { - GNUNET_break (0); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "no new channel handler given, ports parameter is useless!!\n"); - } - if ((NULL == ports || ports[0] == 0) && NULL != new_channel) - { - GNUNET_break (0); - LOG (GNUNET_ERROR_TYPE_DEBUG, - "no ports given, new channel handler will never be called!!\n"); - } /* count handlers */ for (h->n_handlers = 0; handlers && handlers[h->n_handlers].type; h->n_handlers++) ; - for (h->n_ports = 0; - ports && ports[h->n_ports]; - h->n_ports++) ; - send_connect (h); LOG (GNUNET_ERROR_TYPE_DEBUG, "GNUNET_CADET_connect() END\n"); return h; } @@ -1580,9 +1557,10 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle) msg = (struct GNUNET_MessageHeader *) &th[1]; switch (ntohs(msg->type)) { - case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT: case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE: case GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY: + case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN: + case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE: case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNELS: case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_CHANNEL: case GNUNET_MESSAGE_TYPE_CADET_LOCAL_INFO_PEER: @@ -1615,10 +1593,75 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle) GNUNET_SCHEDULER_cancel(handle->reconnect_task); handle->reconnect_task = NULL; } + + GNUNET_CONTAINER_multihashmap_destroy (handle->ports); + handle->ports = NULL; GNUNET_free (handle); } +/** + * Open a port to receive incomming channels. + * + * @param h CADET handle. + * @param port Hash representing the port number. + * @param new_channel Function called when an channel is received. + * @param new_channel_cls Closure for @a new_channel. + * + * @return Port handle. + */ +struct GNUNET_CADET_Port * +GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h, + const struct GNUNET_HashCode *port, + GNUNET_CADET_InboundChannelNotificationHandler + new_channel, + void *new_channel_cls) +{ + struct GNUNET_CADET_Port *p; + struct GNUNET_CADET_PortMessage msg; + + GNUNET_assert (NULL != new_channel); + p = GNUNET_new (struct GNUNET_CADET_Port); + p->cadet = h; + p->hash = GNUNET_new (struct GNUNET_HashCode); + *p->hash = *port; + p->handler = new_channel; + p->cls = new_channel_cls; + GNUNET_assert (GNUNET_OK == + GNUNET_CONTAINER_multihashmap_put (h->ports, + p->hash, + p, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)); + + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN); + msg.port = *p->hash; + send_packet (p->cadet, &msg.header, NULL); + + return p; +} + +/** + * Close a port opened with @a GNUNET_CADET_open_port. + * The @a new_channel callback will no longer be called. + * + * @param p Port handle. + */ +void +GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p) +{ + struct GNUNET_CADET_PortMessage msg; + + msg.header.size = htons (sizeof (msg)); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE); + msg.port = *p->hash; + send_packet (p->cadet, &msg.header, NULL); + GNUNET_CONTAINER_multihashmap_remove (p->cadet->ports, p->hash, p); + GNUNET_free (p->hash); + GNUNET_free (p); +} + + /** * Create a new channel towards a remote peer. * @@ -1629,7 +1672,7 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle) * @param h cadet handle * @param channel_ctx client's channel context to associate with the channel * @param peer peer identity the channel should go to - * @param port Port number. + * @param port Port hash (port number). * @param options CadetOption flag field, with all desired option bits set to 1. * * @return handle to the channel @@ -1638,11 +1681,11 @@ struct GNUNET_CADET_Channel * GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h, void *channel_ctx, const struct GNUNET_PeerIdentity *peer, - uint32_t port, + const struct GNUNET_HashCode *port, enum GNUNET_CADET_ChannelOption options) { struct GNUNET_CADET_Channel *ch; - struct GNUNET_CADET_ChannelMessage msg; + struct GNUNET_CADET_ChannelCreateMessage msg; LOG (GNUNET_ERROR_TYPE_DEBUG, "Creating new channel to %s:%u\n", @@ -1652,14 +1695,16 @@ GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h, LOG (GNUNET_ERROR_TYPE_DEBUG, " number %X\n", ch->chid); ch->ctx = channel_ctx; ch->peer = GNUNET_PEER_intern (peer); + msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE); - msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage)); + msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelCreateMessage)); msg.channel_id = htonl (ch->chid); - msg.port = htonl (port); + msg.port = *port; msg.peer = *peer; msg.opt = htonl (options); ch->allow_send = GNUNET_NO; send_packet (h, &msg.header, ch); + return ch; } @@ -1668,18 +1713,15 @@ void GNUNET_CADET_channel_destroy (struct GNUNET_CADET_Channel *channel) { struct GNUNET_CADET_Handle *h; - struct GNUNET_CADET_ChannelMessage msg; + struct GNUNET_CADET_ChannelDestroyMessage msg; struct GNUNET_CADET_TransmitHandle *th; LOG (GNUNET_ERROR_TYPE_DEBUG, "Destroying channel\n"); h = channel->cadet; msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); - msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelMessage)); + msg.header.size = htons (sizeof (struct GNUNET_CADET_ChannelDestroyMessage)); msg.channel_id = htonl (channel->chid); - memset (&msg.peer, 0, sizeof (struct GNUNET_PeerIdentity)); - msg.port = 0; - msg.opt = 0; th = h->th_head; while (th != NULL) { @@ -2179,3 +2221,26 @@ GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel) NULL); /* no handler cls */ return mq; } + + +/** + * Transitional function to convert an unsigned int port to a hash value. + * WARNING: local static value returned, NOT reentrant! + * WARNING: do not use this function for new code! + * + * @param port Numerical port (unsigned int format). + * + * @return A GNUNET_HashCode usable for the new CADET API. + */ +const struct GNUNET_HashCode * +GC_u2h (uint32_t port) +{ + static struct GNUNET_HashCode hash; + + GNUNET_log (GNUNET_ERROR_TYPE_WARNING, + "This is a transitional function, " + "use proper crypto hashes as CADET ports\n"); + GNUNET_CRYPTO_hash (&port, sizeof (port), &hash); + + return &hash; +} diff --git a/src/cadet/cadet_common.c b/src/cadet/cadet_common.c index 2376722fa..2fbd97283 100644 --- a/src/cadet/cadet_common.c +++ b/src/cadet/cadet_common.c @@ -247,11 +247,18 @@ GC_m2s (uint16_t m) s = "KEEPALIVE"; break; - /** - * Connect to the cadet service, specifying subscriptions + /** + * Open port + */ + case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN: + s = "OPEN_PORT"; + break; + + /** + * Close port */ - case GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT: - s = "LOC_CONNCT"; + case GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE: + s = "CLOSE_PORT"; break; /** diff --git a/src/cadet/cadet_protocol.h b/src/cadet/cadet_protocol.h index dbb4f604f..1a42e920f 100644 --- a/src/cadet/cadet_protocol.h +++ b/src/cadet/cadet_protocol.h @@ -362,7 +362,7 @@ struct GNUNET_CADET_ChannelCreate /** * Destination port. */ - uint32_t port GNUNET_PACKED; + struct GNUNET_HashCode port; /** * Channel options. diff --git a/src/cadet/cadet_test_lib.c b/src/cadet/cadet_test_lib.c index ce348018f..cd9e47b0b 100644 --- a/src/cadet/cadet_test_lib.c +++ b/src/cadet/cadet_test_lib.c @@ -122,10 +122,8 @@ cadet_connect_adapter (void *cls, h = GNUNET_CADET_connect (cfg, (void *) (long) actx->peer, - ctx->new_channel, ctx->cleaner, - ctx->handlers, - ctx->ports); + ctx->handlers); return h; } diff --git a/src/cadet/gnunet-cadet.c b/src/cadet/gnunet-cadet.c index cf415448f..4801dbbdf 100644 --- a/src/cadet/gnunet-cadet.c +++ b/src/cadet/gnunet-cadet.c @@ -390,20 +390,21 @@ channel_ended (void *cls, */ static void * channel_incoming (void *cls, - struct GNUNET_CADET_Channel * channel, - const struct GNUNET_PeerIdentity * initiator, - uint32_t port, enum GNUNET_CADET_ChannelOption options) + struct GNUNET_CADET_Channel *channel, + const struct GNUNET_PeerIdentity *initiator, + const struct GNUNET_HashCode *port, + enum GNUNET_CADET_ChannelOption options) { GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, - "Incoming channel %p on port %u\n", - channel, port); + "Incoming channel %p on port %s\n", + channel, GNUNET_h2s (port)); if (NULL != ch) { GNUNET_break (0); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "A channel already exists (%p)\n", ch); GNUNET_log (GNUNET_ERROR_TYPE_ERROR, - "Incoming channel %p on port %u\n", channel, port); + "Incoming channel %p on port %s\n", channel, GNUNET_h2s (port)); return NULL; } if (0 == listen_port) @@ -480,7 +481,7 @@ create_channel (void *cls) } GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to `%s'\n", target_id); opt = GNUNET_CADET_OPTION_DEFAULT | GNUNET_CADET_OPTION_RELIABLE; - ch = GNUNET_CADET_channel_create (mh, NULL, &pid, target_port, opt); + ch = GNUNET_CADET_channel_create (mh, NULL, &pid, GC_u2h (target_port), opt); if (GNUNET_NO == echo) listen_stdio (); else @@ -835,13 +836,11 @@ run (void *cls, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg) { - GNUNET_CADET_InboundChannelNotificationHandler *newch = NULL; - GNUNET_CADET_ChannelEndHandler *endch = NULL; static const struct GNUNET_CADET_MessageHandler handlers[] = { {&data_callback, GNUNET_MESSAGE_TYPE_CADET_CLI, 0}, {NULL, 0, 0} /* FIXME add option to monitor msg types */ }; - static uint32_t *ports = NULL; + /* FIXME add option to monitor apps */ target_id = args[0]; @@ -871,15 +870,6 @@ run (void *cls, "Creating channel to %s\n", target_id); GNUNET_SCHEDULER_add_now (&create_channel, NULL); - endch = &channel_ended; - } - else if (0 != listen_port) - { - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Listen\n"); - newch = &channel_incoming; - endch = &channel_ended; - ports = GNUNET_malloc (sizeof (uint32_t) * 2); - ports[0] = listen_port; } else if (NULL != peer_id) { @@ -920,15 +910,19 @@ run (void *cls, GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connecting to cadet\n"); mh = GNUNET_CADET_connect (cfg, NULL, /* cls */ - newch, /* new channel */ - endch, /* cleaner */ - handlers, - ports); - GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Done\n"); + &channel_ended, /* cleaner */ + handlers); if (NULL == mh) GNUNET_SCHEDULER_add_now (&shutdown_task, NULL); else sd = GNUNET_SCHEDULER_add_shutdown (&shutdown_task, NULL); + + if (0 != listen_port) + { + GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Opening listen port\n"); + GNUNET_CADET_open_port (mh, GC_u2h (listen_port), + &channel_incoming, NULL); + } } diff --git a/src/cadet/gnunet-service-cadet_channel.c b/src/cadet/gnunet-service-cadet_channel.c index 3d204e5b3..d01356c3a 100644 --- a/src/cadet/gnunet-service-cadet_channel.c +++ b/src/cadet/gnunet-service-cadet_channel.c @@ -211,7 +211,7 @@ struct CadetChannel /** * Destination port of the channel. */ - uint32_t port; + struct GNUNET_HashCode port; /** * Global channel number ( < GNUNET_CADET_LOCAL_CHANNEL_ID_CLI) @@ -552,7 +552,7 @@ send_client_create (struct CadetChannel *ch) opt = 0; opt |= GNUNET_YES == ch->reliable ? GNUNET_CADET_OPTION_RELIABLE : 0; opt |= GNUNET_YES == ch->nobuffer ? GNUNET_CADET_OPTION_NOBUFFER : 0; - GML_send_channel_create (ch->dest, ch->lid_dest, ch->port, opt, + GML_send_channel_create (ch->dest, ch->lid_dest, &ch->port, opt, GCT_get_destination (ch->t)); } @@ -884,7 +884,7 @@ send_create (struct CadetChannel *ch) msgcc.header.size = htons (sizeof (msgcc)); msgcc.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE); msgcc.chid = htonl (ch->gid); - msgcc.port = htonl (ch->port); + msgcc.port = ch->port; msgcc.opt = htonl (channel_get_options (ch)); GCCH_send_prebuilt_message (&msgcc.header, ch, GNUNET_YES, NULL); @@ -1840,7 +1840,7 @@ GCCH_handle_local_destroy (struct CadetChannel *ch, */ int GCCH_handle_local_create (struct CadetClient *c, - struct GNUNET_CADET_ChannelMessage *msg) + struct GNUNET_CADET_ChannelCreateMessage *msg) { struct CadetChannel *ch; struct CadetTunnel *t; @@ -1848,7 +1848,7 @@ GCCH_handle_local_create (struct CadetClient *c, CADET_ChannelNumber chid; LOG (GNUNET_ERROR_TYPE_DEBUG, " towards %s:%u\n", - GNUNET_i2s (&msg->peer), ntohl (msg->port)); + GNUNET_i2s (&msg->peer), GNUNET_h2s (&msg->port)); chid = ntohl (msg->channel_id); /* Sanity check for duplicate channel IDs */ @@ -1879,7 +1879,7 @@ GCCH_handle_local_create (struct CadetClient *c, GNUNET_break (0); return GNUNET_SYSERR; } - ch->port = ntohl (msg->port); + ch->port = msg->port; channel_set_options (ch, ntohl (msg->opt)); /* In unreliable channels, we'll use the DLL to buffer BCK data */ @@ -2149,7 +2149,7 @@ GCCH_handle_create (struct CadetTunnel *t, struct CadetChannel *ch; struct CadetClient *c; int new_channel; - uint32_t port; + struct GNUNET_HashCode *port; chid = ntohl (msg->chid); @@ -2166,7 +2166,7 @@ GCCH_handle_create (struct CadetTunnel *t, { new_channel = GNUNET_NO; } - port = ntohl (msg->port); + port = &msg->port; LOG (GNUNET_ERROR_TYPE_INFO, "<== %s ( 0x%08X %4u) on chan %s (%p) %s [%5u]\n", @@ -2176,9 +2176,9 @@ GCCH_handle_create (struct CadetTunnel *t, if (GNUNET_YES == new_channel || GCT_is_loopback (t)) { /* Find a destination client */ - ch->port = port; - LOG (GNUNET_ERROR_TYPE_DEBUG, " port %u\n", ch->port); - c = GML_client_get_by_port (ch->port); + ch->port = *port; + LOG (GNUNET_ERROR_TYPE_DEBUG, " port %s\n", GNUNET_h2s (port)); + c = GML_client_get_by_port (port); if (NULL == c) { LOG (GNUNET_ERROR_TYPE_DEBUG, " no client has port registered\n"); @@ -2371,6 +2371,7 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message, type = ntohs (message->type); size = ntohs (message->size); + data_id = 0; switch (type) { case GNUNET_MESSAGE_TYPE_CADET_DATA: @@ -2399,7 +2400,6 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message, { struct GNUNET_CADET_ChannelCreate *cc_msg; cc_msg = (struct GNUNET_CADET_ChannelCreate *) message; - data_id = ntohl (cc_msg->port); SPRINTF (info, " 0x%08X", ntohl (cc_msg->chid)); break; } @@ -2409,12 +2409,10 @@ GCCH_send_prebuilt_message (const struct GNUNET_MessageHeader *message, { struct GNUNET_CADET_ChannelManage *m_msg; m_msg = (struct GNUNET_CADET_ChannelManage *) message; - data_id = 0; SPRINTF (info, " 0x%08X", ntohl (m_msg->chid)); break; } default: - data_id = 0; info[0] = '\0'; } LOG (GNUNET_ERROR_TYPE_INFO, @@ -2540,8 +2538,9 @@ GCCH_2s (const struct CadetChannel *ch) if (NULL == ch) return "(NULL Channel)"; - SPRINTF (buf, "%s:%u gid:%X (%X / %X)", - GCT_2s (ch->t), ch->port, ch->gid, ch->lid_root, ch->lid_dest); + SPRINTF (buf, "%s:%s gid:%X (%X / %X)", + GCT_2s (ch->t), GNUNET_h2s (&ch->port), + ch->gid, ch->lid_root, ch->lid_dest); return buf; } diff --git a/src/cadet/gnunet-service-cadet_channel.h b/src/cadet/gnunet-service-cadet_channel.h index 0508a10f1..cc1eb4d8a 100644 --- a/src/cadet/gnunet-service-cadet_channel.h +++ b/src/cadet/gnunet-service-cadet_channel.h @@ -223,7 +223,7 @@ GCCH_handle_local_destroy (struct CadetChannel *ch, */ int GCCH_handle_local_create (struct CadetClient *c, - struct GNUNET_CADET_ChannelMessage *msg); + struct GNUNET_CADET_ChannelCreateMessage *msg); /** * Handler for cadet network payload traffic. diff --git a/src/cadet/gnunet-service-cadet_local.c b/src/cadet/gnunet-service-cadet_local.c index ce1f45a10..5e73887e6 100644 --- a/src/cadet/gnunet-service-cadet_local.c +++ b/src/cadet/gnunet-service-cadet_local.c @@ -81,7 +81,7 @@ struct CadetClient * Ports that this client has declared interest in. * Indexed by port, contains *Client. */ - struct GNUNET_CONTAINER_MultiHashMap32 *ports; + struct GNUNET_CONTAINER_MultiHashMap *ports; /** * Whether the client is active or shutting down (don't send confirmations @@ -127,7 +127,7 @@ unsigned int next_client_id; /** * All ports clients of this peer have opened. */ -static struct GNUNET_CONTAINER_MultiHashMap32 *ports; +static struct GNUNET_CONTAINER_MultiHashMap *ports; /** * Notification context, to send messages to local clients. @@ -150,18 +150,18 @@ static struct GNUNET_SERVER_NotificationContext *nc; */ static int client_release_ports (void *cls, - uint32_t key, + const struct GNUNET_HashCode *key, void *value) { int res; - res = GNUNET_CONTAINER_multihashmap32_remove (ports, key, value); + res = GNUNET_CONTAINER_multihashmap_remove (ports, key, value); if (GNUNET_YES != res) { GNUNET_break (0); LOG (GNUNET_ERROR_TYPE_WARNING, - "Port %u by client %p was not registered.\n", - key, value); + "Port %s by client %p was not registered.\n", + GNUNET_h2s (key), value); } return GNUNET_OK; } @@ -219,9 +219,9 @@ client_destroy (struct CadetClient *c) } if (NULL != c->ports) { - GNUNET_CONTAINER_multihashmap32_iterate (c->ports, - &client_release_ports, c); - GNUNET_CONTAINER_multihashmap32_destroy (c->ports); + GNUNET_CONTAINER_multihashmap_iterate (c->ports, + &client_release_ports, c); + GNUNET_CONTAINER_multihashmap_destroy (c->ports); } GNUNET_CONTAINER_DLL_remove (clients_head, clients_tail, c); @@ -312,81 +312,78 @@ handle_client_disconnect (void *cls, struct GNUNET_SERVER_Client *client) /** - * Handler for new clients + * Handler for port open requests. * - * @param cls closure - * @param client identification of the client - * @param message the actual message, which includes messages the client wants + * @param cls Closure. + * @param client Identification of the client. + * @param message The actual message. */ static void -handle_new_client (void *cls, struct GNUNET_SERVER_Client *client, - const struct GNUNET_MessageHeader *message) +handle_port_open (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) { - struct GNUNET_CADET_ClientConnect *cc_msg; struct CadetClient *c; - unsigned int size; - uint32_t *p; - unsigned int i; + struct GNUNET_CADET_PortMessage *pmsg; - LOG (GNUNET_ERROR_TYPE_DEBUG, "new client registering %p\n", client); + LOG (GNUNET_ERROR_TYPE_DEBUG, "open port requested\n"); - /* Check data sanity */ - size = ntohs (message->size); - if (size < sizeof (struct GNUNET_CADET_ClientConnect)) + /* Sanity check for client registration */ + if (NULL == (c = GML_client_get (client))) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - size -= sizeof (struct GNUNET_CADET_ClientConnect); /* Array size */ - if (0 != (size % sizeof (uint32_t))) + LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); + + /* Message size sanity check */ + if (sizeof (struct GNUNET_CADET_PortMessage) != ntohs (message->size)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - size /= sizeof (uint32_t); /* Number of ports */ - cc_msg = (struct GNUNET_CADET_ClientConnect *) message; - /* Retrieve client structure */ - c = GNUNET_SERVER_client_get_user_context (client, struct CadetClient); - if (NULL == c) + pmsg = (struct GNUNET_CADET_PortMessage *) message; + if (NULL == c->ports) + { + c->ports = GNUNET_CONTAINER_multihashmap_create (4, GNUNET_NO); + } + /* store in client's hashmap */ + if (GNUNET_OK != + GNUNET_CONTAINER_multihashmap_put (c->ports, &pmsg->port, c, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - - LOG (GNUNET_ERROR_TYPE_DEBUG, " client %u has %u ports\n", c-> id, size); - if (size > 0) - { - uint32_t u32; - - p = (uint32_t *) &cc_msg[1]; - c->ports = GNUNET_CONTAINER_multihashmap32_create (size); - for (i = 0; i < size; i++) - { - u32 = ntohl (p[i]); - LOG (GNUNET_ERROR_TYPE_DEBUG, " port: %u\n", u32); - - /* store in client's hashmap */ - GNUNET_CONTAINER_multihashmap32_put (c->ports, u32, c, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST); - /* store in global hashmap */ - /* FIXME only allow one client to have the port open, - * have a backup hashmap with waiting clients */ - GNUNET_CONTAINER_multihashmap32_put (ports, u32, c, - GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); - } - } + /* store in global hashmap */ + /* FIXME only allow one client to have the port open, + * have a backup hashmap with waiting clients */ + GNUNET_CONTAINER_multihashmap_put (ports, &pmsg->port, c, + GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE); GNUNET_SERVER_receive_done (client, GNUNET_OK); - LOG (GNUNET_ERROR_TYPE_DEBUG, "new regitering processed\n"); } /** - * Handler for requests of new tunnels + * Handler for port close requests. + * + * @param cls Closure. + * @param client Identification of the client. + * @param message The actual message. + */ +static void +handle_port_close (void *cls, struct GNUNET_SERVER_Client *client, + const struct GNUNET_MessageHeader *message) +{ +} + + +/** + * Handler for requests of new channels. * * @param cls Closure. * @param client Identification of the client. @@ -411,7 +408,8 @@ handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client, LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); /* Message size sanity check */ - if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size)) + if (sizeof (struct GNUNET_CADET_ChannelCreateMessage) + != ntohs (message->size)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); @@ -420,14 +418,14 @@ handle_channel_create (void *cls, struct GNUNET_SERVER_Client *client, if (GNUNET_OK != GCCH_handle_local_create (c, - (struct GNUNET_CADET_ChannelMessage *) message)) + (struct GNUNET_CADET_ChannelCreateMessage *) + message)) { GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } GNUNET_SERVER_receive_done (client, GNUNET_OK); - return; } @@ -442,7 +440,7 @@ static void handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client, const struct GNUNET_MessageHeader *message) { - struct GNUNET_CADET_ChannelMessage *msg; + struct GNUNET_CADET_ChannelDestroyMessage *msg; struct CadetClient *c; struct CadetChannel *ch; CADET_ChannelNumber chid; @@ -459,14 +457,15 @@ handle_channel_destroy (void *cls, struct GNUNET_SERVER_Client *client, LOG (GNUNET_ERROR_TYPE_DEBUG, " by client %u\n", c->id); /* Message sanity check */ - if (sizeof (struct GNUNET_CADET_ChannelMessage) != ntohs (message->size)) + if (sizeof (struct GNUNET_CADET_ChannelDestroyMessage) + != ntohs (message->size)) { GNUNET_break (0); GNUNET_SERVER_receive_done (client, GNUNET_SYSERR); return; } - msg = (struct GNUNET_CADET_ChannelMessage *) message; + msg = (struct GNUNET_CADET_ChannelDestroyMessage *) message; /* Retrieve tunnel */ chid = ntohl (msg->channel_id); @@ -1069,7 +1068,7 @@ handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client, c->id, c, c->handle); if (NULL != c->ports) LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u ports registered\n", - GNUNET_CONTAINER_multihashmap32_size (c->ports)); + GNUNET_CONTAINER_multihashmap_size (c->ports)); else LOG (GNUNET_ERROR_TYPE_ERROR, "\t no ports registered\n"); LOG (GNUNET_ERROR_TYPE_ERROR, "\t%3u own channles\n", @@ -1091,11 +1090,14 @@ handle_info_dump (void *cls, struct GNUNET_SERVER_Client *client, * Functions to handle messages from clients */ static struct GNUNET_SERVER_MessageHandler client_handlers[] = { - {&handle_new_client, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT, 0}, + {&handle_port_open, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN, + sizeof (struct GNUNET_CADET_PortMessage)}, + {&handle_port_close, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE, + sizeof (struct GNUNET_CADET_PortMessage)}, {&handle_channel_create, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE, - sizeof (struct GNUNET_CADET_ChannelMessage)}, + sizeof (struct GNUNET_CADET_ChannelCreateMessage)}, {&handle_channel_destroy, NULL, GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY, - sizeof (struct GNUNET_CADET_ChannelMessage)}, + sizeof (struct GNUNET_CADET_ChannelDestroyMessage)}, {&handle_data, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_DATA, 0}, {&handle_ack, NULL, GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK, sizeof (struct GNUNET_CADET_LocalAck)}, @@ -1129,7 +1131,7 @@ GML_init (struct GNUNET_SERVER_Handle *handle) LOG (GNUNET_ERROR_TYPE_DEBUG, "init\n"); server_handle = handle; GNUNET_SERVER_suspend (server_handle); - ports = GNUNET_CONTAINER_multihashmap32_create (32); + ports = GNUNET_CONTAINER_multihashmap_create (16, GNUNET_NO); } @@ -1310,9 +1312,9 @@ GML_client_get (struct GNUNET_SERVER_Client *client) * @return non-NULL if a client has the port. */ struct CadetClient * -GML_client_get_by_port (uint32_t port) +GML_client_get_by_port (struct GNUNET_HashCode *port) { - return GNUNET_CONTAINER_multihashmap32_get (ports, port); + return GNUNET_CONTAINER_multihashmap_get (ports, port); } @@ -1390,15 +1392,15 @@ GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id) */ void GML_send_channel_create (struct CadetClient *c, - uint32_t id, uint32_t port, uint32_t opt, - const struct GNUNET_PeerIdentity *peer) + uint32_t id, struct GNUNET_HashCode *port, + uint32_t opt, const struct GNUNET_PeerIdentity *peer) { - struct GNUNET_CADET_ChannelMessage msg; + struct GNUNET_CADET_ChannelCreateMessage msg; msg.header.size = htons (sizeof (msg)); msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_CREATE); msg.channel_id = htonl (id); - msg.port = htonl (port); + msg.port = *port; msg.opt = htonl (opt); msg.peer = *peer; GNUNET_SERVER_notification_context_unicast (nc, c->handle, @@ -1440,7 +1442,7 @@ GML_send_channel_nack (struct CadetClient *c, CADET_ChannelNumber id) void GML_send_channel_destroy (struct CadetClient *c, uint32_t id) { - struct GNUNET_CADET_ChannelMessage msg; + struct GNUNET_CADET_ChannelDestroyMessage msg; if (NULL == c) { @@ -1452,9 +1454,6 @@ GML_send_channel_destroy (struct CadetClient *c, uint32_t id) msg.header.size = htons (sizeof (msg)); msg.header.type = htons (GNUNET_MESSAGE_TYPE_CADET_CHANNEL_DESTROY); msg.channel_id = htonl (id); - msg.port = htonl (0); - memset (&msg.peer, 0, sizeof (msg.peer)); - msg.opt = htonl (0); GNUNET_SERVER_notification_context_unicast (nc, c->handle, &msg.header, GNUNET_NO); } diff --git a/src/cadet/gnunet-service-cadet_local.h b/src/cadet/gnunet-service-cadet_local.h index 6ca79c90d..877f8ad6e 100644 --- a/src/cadet/gnunet-service-cadet_local.h +++ b/src/cadet/gnunet-service-cadet_local.h @@ -134,7 +134,7 @@ GML_client_get (struct GNUNET_SERVER_Client *client); * @return non-NULL if a client has the port. */ struct CadetClient * -GML_client_get_by_port (uint32_t port); +GML_client_get_by_port (struct GNUNET_HashCode *port); /** * Deletes a tunnel from a client (either owner or destination). @@ -170,8 +170,8 @@ GML_send_ack (struct CadetClient *c, CADET_ChannelNumber id); */ void GML_send_channel_create (struct CadetClient *c, - uint32_t id, uint32_t port, uint32_t opt, - const struct GNUNET_PeerIdentity *peer); + uint32_t id, struct GNUNET_HashCode *port, + uint32_t opt, const struct GNUNET_PeerIdentity *peer); /** * Build a local channel NACK message and send it to a local client. diff --git a/src/cadet/gnunet-service-cadet_tunnel.c b/src/cadet/gnunet-service-cadet_tunnel.c index fe763e655..bdb8a8ea4 100644 --- a/src/cadet/gnunet-service-cadet_tunnel.c +++ b/src/cadet/gnunet-service-cadet_tunnel.c @@ -40,7 +40,7 @@ #if !defined(GNUNET_CULL_LOGGING) #define DUMP_KEYS_TO_STDERR GNUNET_YES #else -#define DUMP_KEYS_TO_STDERR GNUNET_NO +#define DUMP_KEYS_TO_STDERR GNUNET_YES #endif #define MIN_TUNNEL_BUFFER 8 diff --git a/src/cadet/test_cadet.c b/src/cadet/test_cadet.c index 8c8d7223e..557524ca3 100644 --- a/src/cadet/test_cadet.c +++ b/src/cadet/test_cadet.c @@ -33,7 +33,7 @@ /** * How many messages to send */ -#define TOTAL_PACKETS 200 /* Cannot exceed 64k! */ +#define TOTAL_PACKETS 5000 /* Cannot exceed 64k! */ /** * How long until we give up on connecting the peers? diff --git a/src/include/gnunet_cadet_service.h b/src/include/gnunet_cadet_service.h index 264bc3154..1c440fc46 100644 --- a/src/include/gnunet_cadet_service.h +++ b/src/include/gnunet_cadet_service.h @@ -49,7 +49,7 @@ extern "C" /** * Version number of GNUnet-cadet API. */ -#define GNUNET_CADET_VERSION 0x00000003 +#define GNUNET_CADET_VERSION 0x00000004 /** @@ -62,6 +62,11 @@ struct GNUNET_CADET_Handle; */ struct GNUNET_CADET_Channel; +/** + * Opaque handle to a port. + */ +struct GNUNET_CADET_Port; + /** * Hash to be used in Cadet communication. Only 256 bits needed, * instead of the 512 from `struct GNUNET_HashCode`. @@ -180,7 +185,7 @@ typedef void * (GNUNET_CADET_InboundChannelNotificationHandler) (void *cls, struct GNUNET_CADET_Channel *channel, const struct GNUNET_PeerIdentity *initiator, - uint32_t port, + const struct GNUNET_HashCode *port, enum GNUNET_CADET_ChannelOption options); @@ -208,9 +213,6 @@ typedef void * @param cfg Configuration to use. * @param cls Closure for the various callbacks that follow (including * handlers in the handlers array). - * @param new_channel Function called when an *incoming* channel is created. - * Can be NULL if no inbound channels are desired. - * See @a ports. * @param cleaner Function called when a channel is destroyed. * It is called immediately if #GNUNET_CADET_channel_destroy * is called on the channel. @@ -218,8 +220,6 @@ typedef void * one must call #GNUNET_CADET_receive_done on the channel to * receive the next message. Messages of a type that is not * in the handlers array are ignored if received. - * @param ports NULL or 0-terminated array of port numbers for incoming channels. - * See @a new_channel. * * @return handle to the cadet service NULL on error * (in this case, init is never called) @@ -227,10 +227,8 @@ typedef void struct GNUNET_CADET_Handle * GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void *cls, - GNUNET_CADET_InboundChannelNotificationHandler new_channel, GNUNET_CADET_ChannelEndHandler cleaner, - const struct GNUNET_CADET_MessageHandler *handlers, - const uint32_t *ports); + const struct GNUNET_CADET_MessageHandler *handlers); /** @@ -244,6 +242,31 @@ GNUNET_CADET_connect (const struct GNUNET_CONFIGURATION_Handle *cfg, void GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle); +/** + * Open a port to receive incomming channels. + * + * @param h CADET handle. + * @param port Hash representing the port number. + * @param new_channel Function called when an channel is received. + * @param new_channel_cls Closure for @a new_channel. + * + * @return Port handle. + */ +struct GNUNET_CADET_Port * +GNUNET_CADET_open_port (struct GNUNET_CADET_Handle *h, + const struct GNUNET_HashCode *port, + GNUNET_CADET_InboundChannelNotificationHandler + new_channel, + void *new_channel_cls); + +/** + * Close a port opened with @a GNUNET_CADET_open_port. + * The @a new_channel callback will no longer be called. + * + * @param p Port handle. + */ +void +GNUNET_CADET_close_port (struct GNUNET_CADET_Port *p); /** * Create a new channel towards a remote peer. @@ -255,17 +278,17 @@ GNUNET_CADET_disconnect (struct GNUNET_CADET_Handle *handle); * @param h cadet handle * @param channel_ctx client's channel context to associate with the channel * @param peer peer identity the channel should go to - * @param port Port number. + * @param port Port hash (port number). * @param options CadetOption flag field, with all desired option bits set to 1. * * @return handle to the channel */ struct GNUNET_CADET_Channel * GNUNET_CADET_channel_create (struct GNUNET_CADET_Handle *h, - void *channel_ctx, - const struct GNUNET_PeerIdentity *peer, - uint32_t port, - enum GNUNET_CADET_ChannelOption options); + void *channel_ctx, + const struct GNUNET_PeerIdentity *peer, + const struct GNUNET_HashCode *port, + enum GNUNET_CADET_ChannelOption options); /** @@ -634,6 +657,19 @@ struct GNUNET_MQ_Handle * GNUNET_CADET_mq_create (struct GNUNET_CADET_Channel *channel); +/** + * Transitional function to convert an unsigned int port to a hash value. + * WARNING: local static value returned, NOT reentrant! + * WARNING: do not use this function for new code! + * + * @param port Numerical port (unsigned int format). + * + * @return A GNUNET_HashCode usable for the new CADET API. + */ +const struct GNUNET_HashCode * +GC_u2h (uint32_t port); + + #if 0 /* keep Emacsens' auto-indent happy */ { #endif diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index 2887f3d27..cc6432be5 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h @@ -876,10 +876,6 @@ extern "C" */ #define GNUNET_MESSAGE_TYPE_CADET_KEEPALIVE 270 -/** - * Connect to the cadet service, specifying subscriptions - */ -#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_CONNECT 272 /** * Ask the cadet service to create a new channel. @@ -926,6 +922,16 @@ extern "C" */ #define GNUNET_MESSAGE_TYPE_CADET_LOCAL_ACK 286 +/** + * Start listening on a port. + */ +#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_OPEN 287 + +/** + * Stop listening on a port. + */ +#define GNUNET_MESSAGE_TYPE_CADET_LOCAL_PORT_CLOSE 288 + /** * Local information about all channels of service. */ -- 2.25.1