GNUnet  0.10.x
ats_api_connectivity.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2016 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"
27 #include "gnunet_ats_service.h"
28 #include "ats.h"
29 
30 
31 #define LOG(kind, ...) GNUNET_log_from(kind, "ats-connectivity-api", __VA_ARGS__)
32 
33 
42 
47 
51  uint32_t strength;
52 };
53 
54 
63 
70 
75 
80 
85 };
86 
87 
93 static void
95 
96 
102 static void
103 reconnect_task(void *cls)
104 {
105  struct GNUNET_ATS_ConnectivityHandle *ch = cls;
106 
107  ch->task = NULL;
108  reconnect(ch);
109 }
110 
111 
117 static void
119 {
120  if (NULL != ch->mq)
121  {
122  GNUNET_MQ_destroy(ch->mq);
123  ch->mq = NULL;
124  }
128  ch);
129 }
130 
131 
139 static void
140 error_handler(void *cls,
141  enum GNUNET_MQ_Error error)
142 {
143  struct GNUNET_ATS_ConnectivityHandle *ch = cls;
144 
146  "ATS connection died (code %d), reconnecting\n",
147  (int)error);
148  force_reconnect(ch);
149 }
150 
151 
161 static int
163  const struct GNUNET_PeerIdentity *peer,
164  void *value)
165 {
166  struct GNUNET_ATS_ConnectivityHandle *ch = cls;
168  struct GNUNET_MQ_Envelope *ev;
169  struct RequestAddressMessage *m;
170 
171  if (NULL == ch->mq)
172  return GNUNET_SYSERR;
174  m->strength = htonl(sh->strength);
175  m->peer = *peer;
176  GNUNET_MQ_send(ch->mq, ev);
177  return GNUNET_OK;
178 }
179 
180 
186 static void
188 {
189  static const struct GNUNET_MQ_MessageHandler handlers[] =
190  { { NULL, 0, 0 } };
191  struct GNUNET_MQ_Envelope *ev;
192  struct ClientStartMessage *init;
193 
194  GNUNET_assert(NULL == ch->mq);
195  ch->mq = GNUNET_CLIENT_connect(ch->cfg,
196  "ats",
197  handlers,
198  &error_handler,
199  ch);
200  if (NULL == ch->mq)
201  {
202  force_reconnect(ch);
203  return;
204  }
205  ev = GNUNET_MQ_msg(init,
208  GNUNET_MQ_send(ch->mq, ev);
209  if (NULL == ch->mq)
210  return;
213  ch);
214 }
215 
216 
225 {
227 
229  ch->cfg = cfg;
231  GNUNET_YES);
232  reconnect(ch);
233  return ch;
234 }
235 
236 
246 static int
247 free_sug_handle(void *cls,
248  const struct GNUNET_PeerIdentity *key,
249  void *value)
250 {
252 
253  GNUNET_free(cur);
254  return GNUNET_OK;
255 }
256 
257 
263 void
265 {
266  if (NULL != ch->mq)
267  {
268  GNUNET_MQ_destroy(ch->mq);
269  ch->mq = NULL;
270  }
271  if (NULL != ch->task)
272  {
274  ch->task = NULL;
275  }
278  NULL);
280  GNUNET_free(ch);
281 }
282 
283 
297  const struct GNUNET_PeerIdentity *peer,
298  uint32_t strength)
299 {
301 
303  s->ch = ch;
304  s->id = *peer;
305  s->strength = strength;
306  if (GNUNET_OK !=
308  &s->id,
309  s,
311  {
313  "Not requesting ATS to suggest address for `%s', request already pending\n",
314  GNUNET_i2s(peer));
315  GNUNET_free(s);
316  return NULL;
317  }
319  "Requesting ATS to suggest address for `%s'\n",
320  GNUNET_i2s(peer));
321  if (NULL == ch->mq)
322  return s;
323  (void)transmit_suggestion(ch,
324  &s->id,
325  s);
326  return s;
327 }
328 
329 
335 void
337 {
338  struct GNUNET_ATS_ConnectivityHandle *ch = sh->ch;
339  struct GNUNET_MQ_Envelope *ev;
340  struct RequestAddressMessage *m;
341 
343  "Telling ATS we no longer care for an address for `%s'\n",
344  GNUNET_i2s(&sh->id));
347  &sh->id,
348  sh));
349  if (NULL == ch->mq)
350  {
351  GNUNET_free(sh);
352  return;
353  }
354  ev = GNUNET_MQ_msg(m,
356  m->strength = htonl(0);
357  m->peer = sh->id;
358  GNUNET_MQ_send(ch->mq, ev);
359  GNUNET_free(sh);
360 }
361 
362 
363 /* end of ats_api_connectivity.c */
Connectivity client to ATS service: we would like to have address suggestions for this peer...
Definition: ats.h:85
#define GNUNET_MESSAGE_TYPE_ATS_START
Type of the &#39;struct ClientStartMessage&#39; sent by clients to ATS to identify the type of the client...
static struct GNUNET_TIME_Relative backoff
How long should we wait to reconnect?
Definition: resolver_api.c:81
struct GNUNET_MQ_Handle * mq
Message queue for sending requests to the ATS service.
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:900
#define LOG(kind,...)
uint32_t start_flag
NBO value of an enum StartFlag.
Definition: ats.h:77
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static void reconnect(struct GNUNET_ATS_ConnectivityHandle *ch)
Re-establish the connection to the ATS service.
#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:75
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_ATS_connectivity_suggest_cancel(struct GNUNET_ATS_ConnectivitySuggestHandle *sh)
We no longer care about being connected to a peer.
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).
uint32_t strength
How "strong" is our need for an address for this peer?
Definition: ats.h:96
static void reconnect_task(void *cls)
Re-establish the connection to the ATS service.
void GNUNET_CONTAINER_multipeermap_destroy(struct GNUNET_CONTAINER_MultiPeerMap *map)
Destroy a hash map.
uint32_t strength
How urgent is the request.
struct GNUNET_ATS_ConnectivitySuggestHandle * GNUNET_ATS_connectivity_suggest(struct GNUNET_ATS_ConnectivityHandle *ch, const struct GNUNET_PeerIdentity *peer, uint32_t strength)
We would like to receive address suggestions for a peer.
struct GNUNET_SCHEDULER_Task * task
Task to trigger reconnect.
static struct GNUNET_ARM_MonitorHandle * m
Monitor connection with ARM.
Definition: gnunet-arm.c:99
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:1237
static char * value
Value of the record to add/remove.
static struct SolverHandle * sh
Handle for ATS address suggestion requests.
#define GNUNET_MESSAGE_TYPE_ATS_REQUEST_ADDRESS_CANCEL
Type of the &#39;struct RequestAddressMessage&#39; sent by clients to ATS to request an address to help conne...
Internal representation of the hash map.
struct GNUNET_CONTAINER_MultiPeerMap * sug_requests
Map with the identities of all the peers for which we would like to have address suggestions.
Message handler for a specific message type.
static int transmit_suggestion(void *cls, const struct GNUNET_PeerIdentity *peer, void *value)
Transmit request for an address suggestion.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
There must only be one value per key; storing a value should fail if a value under the same key alrea...
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:76
Connection suggestion handle.
Definition: ats.h:58
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static char * init
Set to the name of a service to start.
Definition: gnunet-arm.c:69
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
We encountered an error handling the MQ to the ATS service.
Handle to a message queue.
Definition: mq.c:84
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.
struct GNUNET_ATS_ConnectivityHandle * GNUNET_ATS_connectivity_init(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the ATS connectivity suggestion client handle.
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_ATS_REQUEST_ADDRESS
Type of the &#39;struct RequestAddressMessage&#39; sent by clients to ATS to request an address to help conne...
static int free_sug_handle(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Function called to free all struct GNUNET_ATS_ConnectivitySuggestHandles in the map.
configuration data
Definition: configuration.c:83
Automatic transport selection and outbound bandwidth determination.
Entry in list of pending tasks.
Definition: scheduler.c:131
struct GNUNET_ATS_ConnectivityHandle * ch
Connecitivity handle this suggestion handle belongs to.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
#define GNUNET_YES
Definition: gnunet_common.h:77
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:351
Handle to the ATS subsystem for connectivity management.
void GNUNET_ATS_connectivity_done(struct GNUNET_ATS_ConnectivityHandle *ch)
Client is done with ATS connectivity management, release resources.
static void force_reconnect(struct GNUNET_ATS_ConnectivityHandle *ch)
Disconnect from ATS and then reconnect.
struct GNUNET_PeerIdentity peer
Peer to get address suggestions for.
Definition: ats.h:101
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
automatic transport selection messages
First message any client sends to ATS, used to self-identify (what type of client this is)...
Definition: ats.h:68
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
struct GNUNET_PeerIdentity id
ID of the peer for which address suggestion was requested.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
struct GNUNET_TIME_Relative backoff
Reconnect backoff delay.