From 33d31c58c1036acdef9f7d6cb3bb27881e6e26cb Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Mon, 20 Sep 2021 19:08:36 +0200 Subject: dhtu: handle connect --- src/dhtu/plugin_dhtu_gnunet.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/dhtu/plugin_dhtu_gnunet.c b/src/dhtu/plugin_dhtu_gnunet.c index ccd329e8e..e01f90bde 100644 --- a/src/dhtu/plugin_dhtu_gnunet.c +++ b/src/dhtu/plugin_dhtu_gnunet.c @@ -40,6 +40,7 @@ struct GNUNET_DHTU_PrivateKey }; +GNUNET_NETWORK_STRUCT_BEGIN /** * Handle for a public key used by this underlay. @@ -55,11 +56,13 @@ struct PublicKey /** * GNUnet uses eddsa for peers. */ - struct GNUNET_CRYPTO_EddsaPublicKey eddsa_pub; + struct GNUNET_PeerIdentity peer_pub; }; +GNUNET_NETWORK_STRUCT_END + /** * Opaque handle that the underlay offers for our address to be used when * sending messages to another peer. @@ -86,6 +89,22 @@ struct GNUNET_DHTU_Target */ void *app_ctx; + /** + * CORE MQ to send messages to this peer. + */ + struct GNUNET_MQ_Handle *mq; + + /** + * Public key of the peer. + */ + struct PublicKey pk; + + /** + * Hash of the @a pk to identify position of the peer + * in the DHT. + */ + struct GNUNET_DHTU_Hash peer_id; + /** * Head of preferences expressed for this target. */ @@ -204,7 +223,7 @@ ip_verify (void *cls, GNUNET_CRYPTO_eddsa_verify_ (ntohl (purpose->purpose), purpose, es, - &pub->eddsa_pub)) + &pub->peer_pub.public_key)) { GNUNET_break_op (0); return GNUNET_SYSERR; @@ -311,7 +330,22 @@ core_connect_cb (void *cls, const struct GNUNET_PeerIdentity *peer, struct GNUNET_MQ_Handle *mq) { - return NULL; + struct Plugin *plugin = cls; + struct GNUNET_DHTU_Target *target; + + target = GNUNET_new (struct GNUNET_DHTU_Target); + target->mq = mq; + target->pk.header.size = htons (sizeof (struct PublicKey)); + target->pk.peer_pub = *peer; + GNUNET_CRYPTO_hash (peer, + sizeof (struct GNUNET_PeerIdentity), + &target->peer_id.hc); + plugin->env->connect_cb (plugin->env->cls, + &target->pk.header, + &target->peer_id, + target, + &target->app_ctx); + return target; } -- cgit v1.2.3 From c221e30ea67751237058b6a5aa7b46e12fca5395 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 22 Sep 2021 08:49:48 +0200 Subject: -work on dhtu --- src/dhtu/Makefile.am | 1 + src/dhtu/plugin_dhtu_gnunet.c | 137 ++++++++++++++++++++++++++++++++++++++++- src/include/gnunet_mq_lib.h | 18 ++++-- src/include/gnunet_protocols.h | 5 ++ 4 files changed, 154 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/dhtu/Makefile.am b/src/dhtu/Makefile.am index f4b968526..601f31bf5 100644 --- a/src/dhtu/Makefile.am +++ b/src/dhtu/Makefile.am @@ -29,6 +29,7 @@ libgnunet_plugin_dhtu_gnunet_la_SOURCES = \ plugin_dhtu_gnunet.c libgnunet_plugin_dhtu_gnunet_la_LIBADD = \ $(top_builddir)/src/core/libgnunetcore.la \ + $(top_builddir)/src/nse/libgnunetnse.la \ $(top_builddir)/src/util/libgnunetutil.la \ $(XLIBS) \ $(LTLIBINTL) diff --git a/src/dhtu/plugin_dhtu_gnunet.c b/src/dhtu/plugin_dhtu_gnunet.c index e01f90bde..b57c65e44 100644 --- a/src/dhtu/plugin_dhtu_gnunet.c +++ b/src/dhtu/plugin_dhtu_gnunet.c @@ -27,6 +27,8 @@ #include "platform.h" #include "gnunet_dhtu_plugin.h" #include "gnunet_core_service.h" +#include "gnunet_nse_service.h" + /** * Handle for a private key used by this underlay. @@ -74,6 +76,17 @@ struct GNUNET_DHTU_Source * Application context for this source. */ void *app_ctx; + + /** + * Hash position of this peer in the DHT. + */ + struct GNUNET_DHTU_Hash my_id; + + /** + * Private key of this peer. + */ + struct GNUNET_DHTU_PrivateKey pk; + }; @@ -89,6 +102,11 @@ struct GNUNET_DHTU_Target */ void *app_ctx; + /** + * Our plugin with its environment. + */ + struct Plugin *plugin; + /** * CORE MQ to send messages to this peer. */ @@ -159,6 +177,17 @@ struct Plugin * Handle to the CORE service. */ struct GNUNET_CORE_Handle *core; + + /** + * Our "source" address. Traditional CORE API does not tell us which source + * it is, so they are all identical. + */ + struct GNUNET_DHTU_Source src; + + /** + * Handle to the NSE service. + */ + struct GNUNET_NSE_Handle *nse; }; @@ -242,6 +271,9 @@ static void ip_try_connect (void *cls, const char *address) { + struct Plugin *plugin = cls; + + // FIXME: ask ATS/TRANSPORT to 'connect' GNUNET_break (0); } @@ -258,6 +290,7 @@ static struct GNUNET_DHTU_PreferenceHandle * ip_hold (void *cls, struct GNUNET_DHTU_Target *target) { + struct Plugin *plugin = cls; struct GNUNET_DHTU_PreferenceHandle *ph; ph = GNUNET_new (struct GNUNET_DHTU_PreferenceHandle); @@ -266,6 +299,7 @@ ip_hold (void *cls, target->ph_tail, ph); target->ph_count++; + // FIXME: update ATS about 'hold' return ph; } @@ -286,6 +320,7 @@ ip_drop (struct GNUNET_DHTU_PreferenceHandle *ph) ph); target->ph_count--; GNUNET_free (ph); + // FIXME: update ATS about 'drop' } @@ -312,7 +347,20 @@ ip_send (void *cls, GNUNET_SCHEDULER_TaskCallback finished_cb, void *finished_cb_cls) { - GNUNET_break (0); + struct GNUNET_MQ_Envelope *env; + struct GNUNET_MessageHeader *cmsg; + + env = GNUNET_MQ_msg_extra (cmsg, + msg_size, + GNUNET_MESSAGE_TYPE_DHT_CORE); + GNUNET_MQ_notify_sent (env, + finished_cb, + finished_cb_cls); + memcpy (&cmsg[1], + msg, + msg_size); + GNUNET_MQ_send (target->mq, + env); } @@ -334,6 +382,7 @@ core_connect_cb (void *cls, struct GNUNET_DHTU_Target *target; target = GNUNET_new (struct GNUNET_DHTU_Target); + target->plugin = plugin; target->mq = mq; target->pk.header.size = htons (sizeof (struct PublicKey)); target->pk.peer_pub = *peer; @@ -362,6 +411,11 @@ core_disconnect_cb (void *cls, const struct GNUNET_PeerIdentity *peer, void *peer_cls) { + struct Plugin *plugin = cls; + struct GNUNET_DHTU_Target *target = peer_cls; + + plugin->env->disconnect_cb (target->app_ctx); + GNUNET_free (target); } @@ -382,6 +436,79 @@ core_init_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity) { struct Plugin *plugin = cls; + char *addr = NULL; + + // FIXME: initialize src.my_id and src.pk and addr! + // (note: with legacy CORE, we only have one addr) + plugin->env->address_add_cb (plugin->env->cls, + &plugin->src.my_id, + &plugin->src.pk, + addr, + &plugin->src, + &plugin->src.app_ctx); + GNUNET_free (addr); +} + + +/** + * Anything goes, always return #GNUNET_OK. + * + * @param cls unused + * @param msg message to check + * @return #GNUNET_OK if all is fine + */ +static int +check_core_message (void *cls, + const struct GNUNET_MessageHeader *msg) +{ + (void) cls; + (void) msg; + return GNUNET_OK; +} + + +/** + * Handle message from CORE for the DHT. Passes it to the + * DHT logic. + * + * @param cls a `struct GNUNET_DHTU_Target` of the sender + * @param msg the message we received + */ +static void +handle_core_message (void *cls, + const struct GNUNET_MessageHeader *msg) +{ + struct GNUNET_DHTU_Target *origin = cls; + struct Plugin *plugin = origin->plugin; + + plugin->env->receive_cb (plugin->env->cls, + &origin->app_ctx, + &plugin->src.app_ctx, + &msg[1], + ntohs (msg->size) - sizeof (*msg)); +} + + +/** + * Callback to call when network size estimate is updated. + * + * @param cls closure + * @param timestamp time when the estimate was received from the server (or created by the server) + * @param logestimate the log(Base 2) value of the current network size estimate + * @param std_dev standard deviation for the estimate + */ +static void +nse_cb (void *cls, + struct GNUNET_TIME_Absolute timestamp, + double logestimate, + double std_dev) +{ + struct Plugin *plugin = cls; + + plugin->env->network_size_cb (plugin->env->cls, + timestamp, + logestimate, + std_dev); } @@ -398,6 +525,10 @@ libgnunet_plugin_dhtu_ip_init (void *cls) struct GNUNET_DHTU_PluginFunctions *api; struct Plugin *plugin; struct GNUNET_MQ_MessageHandler handlers[] = { + GNUNET_MQ_hd_var_size (core_message, + GNUNET_MESSAGE_TYPE_DHT_CORE, + struct GNUNET_MessageHeader, + NULL), GNUNET_MQ_handler_end () }; @@ -417,6 +548,9 @@ libgnunet_plugin_dhtu_ip_init (void *cls) &core_connect_cb, &core_disconnect_cb, handlers); + plugin->nse = GNUNET_NSE_connect (env->cfg, + &nse_cb, + plugin); return api; } @@ -433,6 +567,7 @@ libgnunet_plugin_dhtu_gnunet_done (void *cls) struct GNUNET_DHTU_PluginFunctions *api = cls; struct Plugin *plugin = api->cls; + GNUNET_NSE_disconnect (plugin->nse); GNUNET_CORE_disconnect (plugin->core); GNUNET_free (plugin); GNUNET_free (api); diff --git a/src/include/gnunet_mq_lib.h b/src/include/gnunet_mq_lib.h index 765647a98..a1c5c4957 100644 --- a/src/include/gnunet_mq_lib.h +++ b/src/include/gnunet_mq_lib.h @@ -305,7 +305,8 @@ enum GNUNET_MQ_PriorityPreferences * @param cls closure * @param msg the received message */ -typedef void (*GNUNET_MQ_MessageCallback) ( +typedef void +(*GNUNET_MQ_MessageCallback) ( void *cls, const struct GNUNET_MessageHeader *msg); @@ -318,7 +319,8 @@ typedef void (*GNUNET_MQ_MessageCallback) ( * @return #GNUNET_OK if the message is well-formed, * #GNUNET_SYSERR if not */ -typedef int (*GNUNET_MQ_MessageValidationCallback) ( +typedef int +(*GNUNET_MQ_MessageValidationCallback) ( void *cls, const struct GNUNET_MessageHeader *msg); @@ -826,7 +828,8 @@ GNUNET_MQ_get_length (struct GNUNET_MQ_Handle *mq); * @param ev the envelope with the message to send. */ void -GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev); +GNUNET_MQ_send (struct GNUNET_MQ_Handle *mq, + struct GNUNET_MQ_Envelope *ev); /** @@ -859,7 +862,8 @@ GNUNET_MQ_send_cancel (struct GNUNET_MQ_Envelope *ev); * @param assoc_data to associate */ uint32_t -GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data); +GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, + void *assoc_data); /** @@ -870,7 +874,8 @@ GNUNET_MQ_assoc_add (struct GNUNET_MQ_Handle *mq, void *assoc_data); * @return the associated data */ void * -GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id); +GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, + uint32_t request_id); /** @@ -881,7 +886,8 @@ GNUNET_MQ_assoc_get (struct GNUNET_MQ_Handle *mq, uint32_t request_id); * @return the associated data */ void * -GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, uint32_t request_id); +GNUNET_MQ_assoc_remove (struct GNUNET_MQ_Handle *mq, + uint32_t request_id); /** diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index 6b61dfc72..41f2876e6 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h @@ -659,6 +659,11 @@ extern "C" { */ #define GNUNET_MESSAGE_TYPE_DHT_CLIENT_GET_RESULTS_KNOWN 156 +/** + * DHT wants to use CORE to transmit data. + */ +#define GNUNET_MESSAGE_TYPE_DHT_CORE 143 + /** * Further X-VINE DHT messages continued from 880 */ -- cgit v1.2.3 From 5da4ba31a2fb0e12ff0daf2fc6e1c7d8c2749655 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 22 Sep 2021 09:29:56 +0200 Subject: -do ATS integration in DHTU --- src/dhtu/Makefile.am | 1 + src/dhtu/plugin_dhtu_gnunet.c | 114 +++++++++++++++++++++++++-------- src/include/gnunet_transport_service.h | 4 +- 3 files changed, 91 insertions(+), 28 deletions(-) (limited to 'src') diff --git a/src/dhtu/Makefile.am b/src/dhtu/Makefile.am index 601f31bf5..67810e8cc 100644 --- a/src/dhtu/Makefile.am +++ b/src/dhtu/Makefile.am @@ -28,6 +28,7 @@ libgnunet_plugin_dhtu_ip_la_LDFLAGS = \ libgnunet_plugin_dhtu_gnunet_la_SOURCES = \ plugin_dhtu_gnunet.c libgnunet_plugin_dhtu_gnunet_la_LIBADD = \ + $(top_builddir)/src/ats/libgnunetats.la \ $(top_builddir)/src/core/libgnunetcore.la \ $(top_builddir)/src/nse/libgnunetnse.la \ $(top_builddir)/src/util/libgnunetutil.la \ diff --git a/src/dhtu/plugin_dhtu_gnunet.c b/src/dhtu/plugin_dhtu_gnunet.c index b57c65e44..9cc14361d 100644 --- a/src/dhtu/plugin_dhtu_gnunet.c +++ b/src/dhtu/plugin_dhtu_gnunet.c @@ -26,6 +26,7 @@ */ #include "platform.h" #include "gnunet_dhtu_plugin.h" +#include "gnunet_ats_service.h" #include "gnunet_core_service.h" #include "gnunet_nse_service.h" @@ -133,6 +134,11 @@ struct GNUNET_DHTU_Target */ struct GNUNET_DHTU_PreferenceHandle *ph_tail; + /** + * ATS preference handle for this peer, or NULL. + */ + struct GNUNET_ATS_ConnectivitySuggestHandle *csh; + /** * Preference counter, length of the @a ph_head DLL. */ @@ -178,6 +184,11 @@ struct Plugin */ struct GNUNET_CORE_Handle *core; + /** + * Handle to ATS service. + */ + struct GNUNET_ATS_ConnectivityHandle *ats; + /** * Our "source" address. Traditional CORE API does not tell us which source * it is, so they are all identical. @@ -274,6 +285,7 @@ ip_try_connect (void *cls, struct Plugin *plugin = cls; // FIXME: ask ATS/TRANSPORT to 'connect' + // => needs HELLO! GNUNET_break (0); } @@ -299,7 +311,12 @@ ip_hold (void *cls, target->ph_tail, ph); target->ph_count++; - // FIXME: update ATS about 'hold' + if (NULL != target->csh) + GNUNET_ATS_connectivity_suggest_cancel (target->csh); + target->csh + = GNUNET_ATS_connectivity_suggest (plugin->ats, + &target->pk.peer_pub, + target->ph_count); return ph; } @@ -314,13 +331,22 @@ static void ip_drop (struct GNUNET_DHTU_PreferenceHandle *ph) { struct GNUNET_DHTU_Target *target = ph->target; - + struct Plugin *plugin = target->plugin; + GNUNET_CONTAINER_DLL_remove (target->ph_head, target->ph_tail, ph); target->ph_count--; GNUNET_free (ph); - // FIXME: update ATS about 'drop' + if (NULL != target->csh) + GNUNET_ATS_connectivity_suggest_cancel (target->csh); + if (0 == target->ph_count) + target->csh = NULL; + else + target->csh + = GNUNET_ATS_connectivity_suggest (plugin->ats, + &target->pk.peer_pub, + target->ph_count); } @@ -415,6 +441,8 @@ core_disconnect_cb (void *cls, struct GNUNET_DHTU_Target *target = peer_cls; plugin->env->disconnect_cb (target->app_ctx); + if (NULL != target->csh) + GNUNET_ATS_connectivity_suggest_cancel (target->csh); GNUNET_free (target); } @@ -436,10 +464,20 @@ core_init_cb (void *cls, const struct GNUNET_PeerIdentity *my_identity) { struct Plugin *plugin = cls; - char *addr = NULL; - - // FIXME: initialize src.my_id and src.pk and addr! - // (note: with legacy CORE, we only have one addr) + char *addr; + char *pid; + + // FIXME: to later ask ATS/TRANSPORT to 'connect' we need a HELLO, + // not merely a vanilla PID... + pid = GNUNET_STRINGS_data_to_string_alloc (my_identity, + sizeof (struct GNUNET_PeerIdentity)); + GNUNET_asprintf (&addr, + "gnunet-core-v15://%s/", + pid); + GNUNET_free (pid); + GNUNET_CRYPTO_hash (my_identity, + sizeof (struct GNUNET_PeerIdentity), + &plugin->src.my_id.hc); plugin->env->address_add_cb (plugin->env->cls, &plugin->src.my_id, &plugin->src.pk, @@ -512,6 +550,30 @@ nse_cb (void *cls, } +/** + * Exit point from the plugin. + * + * @param cls closure (our `struct Plugin`) + * @return NULL + */ +void * +libgnunet_plugin_dhtu_gnunet_done (void *cls) +{ + struct GNUNET_DHTU_PluginFunctions *api = cls; + struct Plugin *plugin = api->cls; + + if (NULL != plugin->nse) + GNUNET_NSE_disconnect (plugin->nse); + if (NULL != plugin->core) + GNUNET_CORE_disconnect (plugin->core); + if (NULL != plugin->ats) + GNUNET_ATS_connectivity_done (plugin->ats); + GNUNET_free (plugin); + GNUNET_free (api); + return NULL; +} + + /** * Entry point for the plugin. * @@ -531,9 +593,18 @@ libgnunet_plugin_dhtu_ip_init (void *cls) NULL), GNUNET_MQ_handler_end () }; - + struct GNUNET_CRYPTO_EddsaPrivateKey *pk; + + pk = GNUNET_CRYPTO_eddsa_key_create_from_configuration (env->cfg); + if (NULL == pk) + { + GNUNET_break (0); + return NULL; + } plugin = GNUNET_new (struct Plugin); plugin->env = env; + plugin->src.pk.eddsa_priv = *pk; + GNUNET_free (pk); api = GNUNET_new (struct GNUNET_DHTU_PluginFunctions); api->cls = plugin; api->sign = &ip_sign; @@ -542,6 +613,7 @@ libgnunet_plugin_dhtu_ip_init (void *cls) api->hold = &ip_hold; api->drop = &ip_drop; api->send = &ip_send; + plugin->ats = GNUNET_ATS_connectivity_init (env->cfg); plugin->core = GNUNET_CORE_connect (env->cfg, plugin, &core_init_cb, @@ -551,25 +623,15 @@ libgnunet_plugin_dhtu_ip_init (void *cls) plugin->nse = GNUNET_NSE_connect (env->cfg, &nse_cb, plugin); + if ( (NULL == plugin->ats) || + (NULL == plugin->core) || + (NULL == plugin->nse) ) + { + GNUNET_break (0); + libgnunet_plugin_dhtu_gnunet_done (plugin); + return NULL; + } return api; } -/** - * Exit point from the plugin. - * - * @param cls closure (our `struct Plugin`) - * @return NULL - */ -void * -libgnunet_plugin_dhtu_gnunet_done (void *cls) -{ - struct GNUNET_DHTU_PluginFunctions *api = cls; - struct Plugin *plugin = api->cls; - - GNUNET_NSE_disconnect (plugin->nse); - GNUNET_CORE_disconnect (plugin->core); - GNUNET_free (plugin); - GNUNET_free (api); - return NULL; -} diff --git a/src/include/gnunet_transport_service.h b/src/include/gnunet_transport_service.h index 545bb28d2..459efc506 100644 --- a/src/include/gnunet_transport_service.h +++ b/src/include/gnunet_transport_service.h @@ -117,8 +117,8 @@ struct GNUNET_TRANSPORT_AddressToStringContext; */ typedef void (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls, - const char *address, - int res); + const char *address, + int res); /** -- cgit v1.2.3