GNUnet  0.10.x
transport_api_hello_get.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2009-2013, 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  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_constants.h"
29 #include "gnunet_arm_service.h"
30 #include "gnunet_hello_lib.h"
31 #include "gnunet_protocols.h"
33 #include "transport.h"
34 
35 
44 
49 
54 
58  void *rec_cls;
59 
64 
69 
74 
79 };
80 
81 
89 static int
90 check_hello(void *cls,
91  const struct GNUNET_MessageHeader *msg)
92 {
93  struct GNUNET_PeerIdentity me;
94 
95  if (GNUNET_OK !=
96  GNUNET_HELLO_get_id((const struct GNUNET_HELLO_Message *)msg,
97  &me))
98  {
99  GNUNET_break(0);
100  return GNUNET_SYSERR;
101  }
103  "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
104  (unsigned int)ntohs(msg->size),
105  GNUNET_i2s(&me));
106  return GNUNET_OK;
107 }
108 
109 
116 static void
117 handle_hello(void *cls,
118  const struct GNUNET_MessageHeader *msg)
119 {
120  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
121 
122  ghh->rec(ghh->rec_cls,
123  msg);
124 }
125 
126 
133 static void
135 
136 
146 static void
148  enum GNUNET_MQ_Error error)
149 {
150  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
151 
153  "Error receiving from transport service, disconnecting temporarily.\n");
154  GNUNET_MQ_destroy(ghh->mq);
155  ghh->mq = NULL;
156  schedule_reconnect(ghh);
157 }
158 
159 
165 static void
166 reconnect(void *cls)
167 {
168  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
169  struct GNUNET_MQ_MessageHandler handlers[] = {
170  GNUNET_MQ_hd_var_size(hello,
172  struct GNUNET_MessageHeader,
173  ghh),
175  };
176  struct GNUNET_MQ_Envelope *env;
177  struct StartMessage *s;
178 
179  ghh->reconnect_task = NULL;
181  "Connecting to transport service.\n");
182  GNUNET_assert(NULL == ghh->mq);
183  ghh->mq = GNUNET_CLIENT_connect(ghh->cfg,
184  "transport",
185  handlers,
187  ghh);
188  if (NULL == ghh->mq)
189  return;
190  env = GNUNET_MQ_msg(s,
192  s->options = htonl(0);
193  GNUNET_MQ_send(ghh->mq,
194  env);
195 }
196 
197 
204 static void
206 {
207  ghh->reconnect_task =
209  &reconnect,
210  ghh);
212 }
213 
214 
232  void *rec_cls)
233 {
235 
237  ghh->rec = rec;
238  ghh->rec_cls = rec_cls;
239  ghh->cfg = cfg;
240  ghh->ac = ac;
241  reconnect(ghh);
242  if (NULL == ghh->mq)
243  {
244  GNUNET_free(ghh);
245  return NULL;
246  }
247  return ghh;
248 }
249 
250 
256 void
258 {
259  if (NULL != ghh->reconnect_task)
260  {
262  ghh->reconnect_task = NULL;
263  }
264  if (NULL != ghh->mq)
265  {
266  GNUNET_MQ_destroy(ghh->mq);
267  ghh->mq = NULL;
268  }
269  GNUNET_free(ghh);
270 }
271 
272 
273 /* end of transport_api_hello_get.c */
int GNUNET_HELLO_get_id(const struct GNUNET_HELLO_Message *hello, struct GNUNET_PeerIdentity *peer)
Get the peer identity from a HELLO message.
Definition: hello.c:662
A HELLO message is used to exchange information about transports with other peers.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Handle * mq
Transport handle.
common internal definitions for transport 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
GNUNET_MQ_Error
Error codes for the queue.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void(* GNUNET_TRANSPORT_HelloUpdateCallback)(void *cls, const struct GNUNET_MessageHeader *hello)
Function called whenever there is an update to the HELLO of this peer.
static int check_hello(void *cls, const struct GNUNET_MessageHeader *msg)
Function we use for checking incoming HELLO messages.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
#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.
static void handle_hello(void *cls, const struct GNUNET_MessageHeader *msg)
Function we use for handling incoming HELLO messages.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_SCHEDULER_Task * notify_task
Task for calling the HelloUpdateCallback when we already have a HELLO.
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
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
uint32_t options
0: no options 1: The self field should be checked 2: this client is interested in payload traffic ...
Definition: transport.h:102
GNUNET_TRANSPORT_HelloUpdateCallback rec
Callback to call once we got our HELLO.
#define GNUNET_MESSAGE_TYPE_TRANSPORT_START
Message from the core saying that the transport server should start giving it messages.
Message handler for a specific message type.
Message from the transport service to the library asking to check if both processes agree about this ...
Definition: transport.h:91
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#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 struct GNUNET_TRANSPORT_HelloGetHandle * ghh
Handle to get our current HELLO.
GNUNET_TRANSPORT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
enum GNUNET_TRANSPORT_AddressClass ac
Type of HELLOs client cares about.
Handle to a message queue.
Definition: mq.c:84
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
struct GNUNET_SCHEDULER_Task * reconnect_task
ID of the task trying to reconnect to the service.
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:83
struct GNUNET_TRANSPORT_HelloGetHandle * GNUNET_TRANSPORT_hello_get(const struct GNUNET_CONFIGURATION_Handle *cfg, enum GNUNET_TRANSPORT_AddressClass ac, GNUNET_TRANSPORT_HelloUpdateCallback rec, void *rec_cls)
Obtain the HELLO message for this peer.
static void reconnect(void *cls)
Try again to connect to transport service.
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
Functions to call with this peer&#39;s HELLO.
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
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
#define GNUNET_MESSAGE_TYPE_HELLO
HELLO message with friend only flag used for communicating peer addresses.
struct GNUNET_TIME_Relative reconnect_delay
Delay until we try to reconnect.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
void GNUNET_TRANSPORT_hello_get_cancel(struct GNUNET_TRANSPORT_HelloGetHandle *ghh)
Stop receiving updates about changes to our HELLO message.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956
static void schedule_reconnect(struct GNUNET_TRANSPORT_HelloGetHandle *ghh)
Function that will schedule the job that will try to connect us again to the client.