GNUnet  0.10.x
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 {
45 
50 
55 
60 };
61 
62 
67 {
68 
73 
80 
85 
90 
95 };
96 
97 
103 static void
105 
106 
112 static void
113 reconnect_task (void *cls)
114 {
116 
117  ch->task = NULL;
118  reconnect (ch);
119 }
120 
121 
127 static void
129 {
130  if (NULL != ch->mq)
131  {
132  GNUNET_MQ_destroy (ch->mq);
133  ch->mq = NULL;
134  }
137 }
138 
139 
147 static void
148 error_handler (void *cls, enum GNUNET_MQ_Error error)
149 {
151 
153  "TRANSPORT connection died (code %d), reconnecting\n",
154  (int) error);
155  force_reconnect (ch);
156 }
157 
158 
168 static int
170  const struct GNUNET_PeerIdentity *peer,
171  void *value)
172 {
175  struct GNUNET_MQ_Envelope *ev;
176  struct ExpressPreferenceMessage *m;
177 
178  if (NULL == ch->mq)
179  return GNUNET_SYSERR;
181  m->pk = htonl ((uint32_t) sh->pk);
182  m->bw = sh->bw;
183  m->peer = *peer;
184  GNUNET_MQ_send (ch->mq, ev);
185  return GNUNET_OK;
186 }
187 
188 
194 static void
196 {
197  static const struct GNUNET_MQ_MessageHandler handlers[] = {{NULL, 0, 0}};
198 
199  GNUNET_assert (NULL == ch->mq);
200  ch->mq =
201  GNUNET_CLIENT_connect (ch->cfg, "transport", handlers, &error_handler, ch);
202  if (NULL == ch->mq)
203  {
204  force_reconnect (ch);
205  return;
206  }
209  ch);
210 }
211 
212 
221  const struct GNUNET_CONFIGURATION_Handle *cfg)
222 {
224 
226  ch->cfg = cfg;
228  reconnect (ch);
229  return ch;
230 }
231 
232 
242 static int
243 free_sug_handle (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
244 {
246 
247  GNUNET_free (cur);
248  return GNUNET_OK;
249 }
250 
251 
257 void
260 {
261  if (NULL != ch->mq)
262  {
263  GNUNET_MQ_destroy (ch->mq);
264  ch->mq = NULL;
265  }
266  if (NULL != ch->task)
267  {
269  ch->task = NULL;
270  }
273  NULL);
275  GNUNET_free (ch);
276 }
277 
278 
292  const struct GNUNET_PeerIdentity *peer,
294  struct GNUNET_BANDWIDTH_Value32NBO bw)
295 {
297 
299  s->ch = ch;
300  s->id = *peer;
301  s->pk = pk;
302  s->bw = bw;
304  ch->sug_requests,
305  &s->id,
306  s,
309  "Requesting TRANSPORT to suggest address for `%s'\n",
310  GNUNET_i2s (peer));
311  if (NULL == ch->mq)
312  return s;
313  GNUNET_assert (GNUNET_OK == transmit_suggestion (ch, &s->id, s));
314  return s;
315 }
316 
317 
323 void
326 {
328  struct GNUNET_MQ_Envelope *ev;
329  struct ExpressPreferenceMessage *m;
330 
332  "Telling TRANSPORT we no longer care for an address for `%s'\n",
333  GNUNET_i2s (&sh->id));
334  GNUNET_assert (
335  GNUNET_OK ==
337  if (NULL == ch->mq)
338  {
339  GNUNET_free (sh);
340  return;
341  }
343  m->pk = htonl ((uint32_t) sh->pk);
344  m->bw = sh->bw;
345  m->peer = sh->id;
346  GNUNET_MQ_send (ch->mq, ev);
347  GNUNET_free (sh);
348 }
349 
350 
366 void
369  const struct GNUNET_PeerIdentity *peer,
370  enum GNUNET_NetworkType nt,
371  const char *addr)
372 {
373  struct GNUNET_MQ_Envelope *ev;
374  struct RequestHelloValidationMessage *m;
375  size_t alen;
376 
377  if (NULL == ch->mq)
378  {
379  GNUNET_log (
381  "Address validation for %s:%s skipped as transport is not connected\n",
382  GNUNET_i2s (peer),
383  addr);
384  return;
385  }
386  alen = strlen (addr) + 1;
387  ev =
389  alen,
391  m->peer = *peer;
392  m->nt = htonl ((uint32_t) nt);
393  memcpy (&m[1], addr, alen);
394  GNUNET_MQ_send (ch->mq, ev);
395 }
396 
397 
398 /* end of transport_api2_application.c */
common internal definitions for transport service
static struct GNUNET_TIME_Relative backoff
How long should we wait to reconnect?
Definition: resolver_api.c:81
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:901
struct GNUNET_TRANSPORT_ApplicationHandle * ch
Connecitivity handle this suggestion handle belongs to.
GNUNET_MQ_Error
Error codes for the queue.
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...
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
Bandwidth allocation API for applications to interact with.
Handle for TRANSPORT address suggestion requests.
void GNUNET_TRANSPORT_application_suggest_cancel(struct GNUNET_TRANSPORT_ApplicationSuggestHandle *sh)
We no longer care about being connected to a peer.
struct GNUNET_CONTAINER_MultiPeerMap * sug_requests
Map with the identities of all the peers for which we would like to have address suggestions.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
int 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.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
GNUNET_MQ_PriorityPreferences
Per envelope preferences and priorities.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define LOG(kind,...)
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).
static void force_reconnect(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Disconnect from TRANSPORT and then reconnect.
enum GNUNET_MQ_PriorityPreferences pk
What preference is being expressed?
void GNUNET_TRANSPORT_application_done(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Client is done with TRANSPORT application management, release resources.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:35
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
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.
uint32_t pk
What type of performance preference does the client have? A enum GNUNET_MQ_PreferenceKind in NBO...
Definition: ats2.h:125
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...
struct GNUNET_TIME_Relative backoff
Reconnect backoff delay.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:99
#define GNUNET_MESSAGE_TYPE_TRANSPORT_REQUEST_HELLO_VALIDATION
Type of the &#39;struct RequestHelloValidationMessage&#39; send by clients to TRANSPORT to trigger validation...
#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:52
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:1246
static int transmit_suggestion(void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
Transmit request for an address suggestion.
static void reconnect_task(void *cls)
Re-establish the connection to the TRANSPORT service.
static char * value
Value of the record to add/remove.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct GNUNET_MQ_Handle * mq
Message queue for sending requests to the TRANSPORT service.
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
We encountered an error handling the MQ to the TRANSPORT service.
#define GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST_CANCEL
Type of the &#39;struct ExpressPreferenceMessage&#39; send by clients to TRANSPORT to abandon bandwidth prefe...
static struct SolverHandle * sh
Internal representation of the hash map.
Message handler for a specific message type.
static struct GNUNET_NAT_AUTO_Test * nt
Handle to a NAT test operation.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
struct GNUNET_TESTBED_Peer * peer
The peer associated with this model.
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#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_PeerIdentity id
ID of the peer for which address suggestion was requested.
Allow multiple values with the same key.
Handle to a message queue.
Definition: mq.c:85
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.
int GNUNET_CONTAINER_multipeermap_iterate(struct GNUNET_CONTAINER_MultiPeerMap *map, GNUNET_CONTAINER_PeerMapIterator it, void *it_cls)
Iterate over all entries in the map.
The identity of the host (wraps the signing key of the peer).
#define GNUNET_MESSAGE_TYPE_TRANSPORT_SUGGEST
Type of the &#39;struct ExpressPreferenceMessage&#39; send by clients to TRANSPORT to establish bandwidth pre...
struct GNUNET_TRANSPORT_ApplicationHandle * GNUNET_TRANSPORT_application_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the TRANSPORT application suggestion client handle.
configuration data
Definition: configuration.c:85
struct GNUNET_SCHEDULER_Task * task
Task to trigger reconnect.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:134
struct GNUNET_BANDWIDTH_Value32NBO bw
How much bandwidth does the client expect?
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
#define GNUNET_YES
Definition: gnunet_common.h:80
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:353
struct GNUNET_BANDWIDTH_Value32NBO bw
How much bandwidth in bytes/second does the application expect?
Definition: ats2.h:135
static void reconnect(struct GNUNET_TRANSPORT_ApplicationHandle *ch)
Re-establish the connection to the TRANSPORT service.
Application client to ATS service: we would like to have address suggestions for this peer...
Definition: ats2.h:112
Handle to the TRANSPORT subsystem for application management.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_PeerIdentity peer
Peer to get address suggestions for.
Definition: ats2.h:130
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965