GNUnet  0.17.6
transport_api2_application.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010--2019 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
26 #include "platform.h"
29 #include "transport.h"
30 
31 
32 #define LOG(kind, ...) \
33  GNUNET_log_from (kind, "transport-application-api", __VA_ARGS__)
34 
35 
40 {
44  struct GNUNET_PeerIdentity id;
45 
50 
55 
60 };
61 
62 
67 {
72 
79 
84 
89 
94 };
95 
96 
102 static void
104 
105 
111 static void
112 reconnect_task (void *cls)
113 {
115 
116  ch->task = NULL;
117  reconnect (ch);
118 }
119 
120 
126 static void
128 {
129  if (NULL != ch->mq)
130  {
132  ch->mq = NULL;
133  }
134  ch->backoff = GNUNET_TIME_STD_BACKOFF (ch->backoff);
135  ch->task = GNUNET_SCHEDULER_add_delayed (ch->backoff, &reconnect_task, ch);
136 }
137 
138 
146 static void
147 error_handler (void *cls, enum GNUNET_MQ_Error error)
148 {
150 
152  "TRANSPORT connection died (code %d), reconnecting\n",
153  (int) error);
155 }
156 
157 
167 static int
169  const struct GNUNET_PeerIdentity *peer,
170  void *value)
171 {
174  struct GNUNET_MQ_Envelope *ev;
175  struct ExpressPreferenceMessage *m;
176 
177  if (NULL == ch->mq)
178  return GNUNET_SYSERR;
180  m->pk = htonl ((uint32_t) sh->pk);
181  m->bw = sh->bw;
182  m->peer = *peer;
183  GNUNET_MQ_send (ch->mq, ev);
184  return GNUNET_OK;
185 }
186 
187 
193 static void
195 {
196  static const struct GNUNET_MQ_MessageHandler handlers[] = { { NULL, 0, 0 } };
197 
198  GNUNET_assert (NULL == ch->mq);
199  ch->mq =
200  GNUNET_CLIENT_connect (ch->cfg, "transport", handlers, &error_handler, ch);
201  if (NULL == ch->mq)
202  {
204  return;
205  }
208  ch);
209 }
210 
211 
220  const struct GNUNET_CONFIGURATION_Handle *cfg)
221 {
223 
225  ch->cfg = cfg;
226  ch->sug_requests = GNUNET_CONTAINER_multipeermap_create (32, GNUNET_YES);
227  reconnect (ch);
228  return ch;
229 }
230 
231 
241 static int
242 free_sug_handle (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
243 {
245 
246  GNUNET_free (cur);
247  return GNUNET_OK;
248 }
249 
250 
256 void
259 {
260  if (NULL != ch->mq)
261  {
263  ch->mq = NULL;
264  }
265  if (NULL != ch->task)
266  {
267  GNUNET_SCHEDULER_cancel (ch->task);
268  ch->task = NULL;
269  }
272  NULL);
274  GNUNET_free (ch);
275 }
276 
277 
291  const struct GNUNET_PeerIdentity *peer,
294 {
296 
298  s->ch = ch;
299  s->id = *peer;
300  s->pk = pk;
301  s->bw = bw;
303  ch->sug_requests,
304  &s->id,
305  s,
308  "Requesting TRANSPORT to suggest address for `%s'\n",
309  GNUNET_i2s (peer));
310  if (NULL == ch->mq)
311  return s;
313  return s;
314 }
315 
316 
322 void
325 {
327  struct GNUNET_MQ_Envelope *ev;
328  struct ExpressPreferenceMessage *m;
329 
331  "Telling TRANSPORT we no longer care for an address for `%s'\n",
332  GNUNET_i2s (&sh->id));
333  GNUNET_assert (
334  GNUNET_OK ==
335  GNUNET_CONTAINER_multipeermap_remove (ch->sug_requests, &sh->id, sh));
336  if (NULL == ch->mq)
337  {
338  GNUNET_free (sh);
339  return;
340  }
342  m->pk = htonl ((uint32_t) sh->pk);
343  m->bw = sh->bw;
344  m->peer = sh->id;
345  GNUNET_MQ_send (ch->mq, ev);
346  GNUNET_free (sh);
347 }
348 
349 
365 void
368  const struct GNUNET_PeerIdentity *peer,
369  enum GNUNET_NetworkType nt,
370  const char *addr)
371 {
372  struct GNUNET_MQ_Envelope *ev;
373  struct RequestHelloValidationMessage *m;
374  size_t alen;
375 
376  if (NULL == ch->mq)
377  {
378  GNUNET_log (
380  "Address validation for %s:%s skipped as transport is not connected\n",
381  GNUNET_i2s (peer),
382  addr);
383  return;
384  }
385  alen = strlen (addr) + 1;
386  ev =
388  alen,
390  m->peer = *peer;
391  m->nt = htonl ((uint32_t) nt);
392  memcpy (&m[1], addr, alen);
393  GNUNET_MQ_send (ch->mq, ev);
394 }
395 
396 
397 /* end of transport_api2_application.c */
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:104
static struct SolverHandle * sh
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static struct GNUNET_CADET_Channel * ch
Channel handle.
Definition: gnunet-cadet.c:117
struct GNUNET_HashCode key
The key used in the DHT.
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
static char * value
Value of the record to add/remove.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
struct GNUNET_BIO_WriteHandle * bw
handle to the file to write the load statistics to
Bandwidth allocation API for applications to interact with.
API of the transport service towards the CORE service (TNG version)
void GNUNET_TRANSPORT_application_done(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Client is done with TRANSPORT application management, release resources.
struct GNUNET_TRANSPORT_ApplicationSuggestHandle * GNUNET_TRANSPORT_application_suggest(struct GNUNET_TRANSPORT_ApplicationHandle *ch, const struct GNUNET_PeerIdentity *peer, enum GNUNET_MQ_PriorityPreferences pk, struct GNUNET_BANDWIDTH_Value32NBO bw)
An application would like TRANSPORT to connect to a peer.
struct GNUNET_TRANSPORT_ApplicationHandle * GNUNET_TRANSPORT_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the TRANSPORT application suggestion client handle.
void GNUNET_TRANSPORT_application_validate(struct GNUNET_TRANSPORT_ApplicationHandle *ch, const struct GNUNET_PeerIdentity *peer, enum GNUNET_NetworkType nt, const char *addr)
An application (or a communicator) has received a HELLO (or other address data of another peer) and w...
void GNUNET_TRANSPORT_application_suggest_cancel(struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh)
We no longer care about being connected to a peer.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:1055
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
struct GNUNET_CONTAINER_MultiPeerMap * GNUNET_CONTAINER_multipeermap_create(unsigned int len, int do_not_copy_keys)
Create a multi peer map (hash map for public keys of peers).
int GNUNET_CONTAINER_multipeermap_put(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multipeermap_remove(struct GNUNET_CONTAINER_MultiPeerMap *map, const struct GNUNET_PeerIdentity *key, const void *value)
Remove the given key-value pair from the map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE
Allow multiple values with the same key.
#define GNUNET_log(kind,...)
@ GNUNET_OK
Definition: gnunet_common.h:99
@ GNUNET_YES
@ GNUNET_SYSERR
Definition: gnunet_common.h:97
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:302
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:56
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:71
GNUNET_MQ_PriorityPreferences
Per envelope preferences and priorities.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:685
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:36
#define GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION
Type of the 'struct RequestHelloValidationMessage' send by clients to TRANSPORT to trigger validation...
#define GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST_CANCEL
Type of the 'struct ExpressPreferenceMessage' send by clients to TRANSPORT to abandon bandwidth prefe...
#define GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST
Type of the 'struct ExpressPreferenceMessage' send by clients to TRANSPORT to establish bandwidth pre...
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:957
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1254
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
struct GNUNET_MQ_Handle * mq
Message Queue for the channel (which we are implementing).
Definition: cadet.h:143
Internal representation of the hash map.
Handle to a message queue.
Definition: mq.c:86
Message handler for a specific message type.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition: scheduler.c:135
Time for relative time used by GNUnet, in microseconds.
Handle to the TRANSPORT subsystem for application management.
struct GNUNET_CONTAINER_MultiPeerMap * sug_requests
Map with the identities of all the peers for which we would like to have address suggestions.
struct GNUNET_SCHEDULER_Task * task
Task to trigger reconnect.
struct GNUNET_MQ_Handle * mq
Message queue for sending requests to the TRANSPORT service.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct GNUNET_TIME_Relative backoff
Reconnect backoff delay.
Handle for TRANSPORT address suggestion requests.
enum GNUNET_MQ_PriorityPreferences pk
What preference is being expressed?
struct GNUNET_TRANSPORT_ApplicationHandle * ch
Connecitivity handle this suggestion handle belongs to.
struct GNUNET_BANDWIDTH_Value32NBO bw
How much bandwidth does the client expect?
struct GNUNET_PeerIdentity id
ID of the peer for which address suggestion was requested.
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
common internal definitions for transport service
static void force_reconnect(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Disconnect from TRANSPORT and then reconnect.
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
We encountered an error handling the MQ to the TRANSPORT service.
static int transmit_suggestion(void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
Transmit request for an address suggestion.
static int free_sug_handle(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Function called to free all struct GNUNET_TRANSPORT_ApplicationSuggestHandles in the map.
#define LOG(kind,...)
static void reconnect(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Re-establish the connection to the TRANSPORT service.
static void reconnect_task(void *cls)
Re-establish the connection to the TRANSPORT service.