GNUnet  0.20.0
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 
40 {
45 
50 
55 
59  void *rec_cls;
60 
65 
70 
75 
80 };
81 
82 
90 static int
91 check_hello (void *cls,
92  const struct GNUNET_MessageHeader *msg)
93 {
94  struct GNUNET_PeerIdentity me;
95 
96  if (GNUNET_OK !=
98  &me))
99  {
100  GNUNET_break (0);
101  return GNUNET_SYSERR;
102  }
104  "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
105  (unsigned int) ntohs (msg->size),
106  GNUNET_i2s (&me));
107  return GNUNET_OK;
108 }
109 
110 
117 static void
118 handle_hello (void *cls,
119  const struct GNUNET_MessageHeader *msg)
120 {
121  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
122 
123  ghh->rec (ghh->rec_cls,
124  msg);
125 }
126 
127 
134 static void
136 
137 
147 static void
148 mq_error_handler (void *cls,
149  enum GNUNET_MQ_Error error)
150 {
151  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
152 
154  "Error receiving from transport service, disconnecting temporarily.\n");
155  GNUNET_MQ_destroy (ghh->mq);
156  ghh->mq = NULL;
157  schedule_reconnect (ghh);
158 }
159 
160 
166 static void
167 reconnect (void *cls)
168 {
169  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
171  GNUNET_MQ_hd_var_size (hello,
173  struct GNUNET_MessageHeader,
174  ghh),
176  };
177  struct GNUNET_MQ_Envelope *env;
178  struct StartMessage *s;
179 
180  ghh->reconnect_task = NULL;
182  "Connecting to transport service.\n");
183  GNUNET_assert (NULL == ghh->mq);
184  ghh->mq = GNUNET_CLIENT_connect (ghh->cfg,
185  "transport",
186  handlers,
188  ghh);
189  if (NULL == ghh->mq)
190  return;
191  env = GNUNET_MQ_msg (s,
193  s->options = htonl (0);
194  GNUNET_MQ_send (ghh->mq,
195  env);
196 }
197 
198 
205 static void
207 {
208  ghh->reconnect_task =
210  &reconnect,
211  ghh);
213 }
214 
215 
233  void *rec_cls)
234 {
236 
238  ghh->rec = rec;
239  ghh->rec_cls = rec_cls;
240  ghh->cfg = cfg;
241  ghh->ac = ac;
242  reconnect (ghh);
243  if (NULL == ghh->mq)
244  {
245  GNUNET_free (ghh);
246  return NULL;
247  }
248  return ghh;
249 }
250 
251 
257 void
259 {
260  if (NULL != ghh->reconnect_task)
261  {
263  ghh->reconnect_task = NULL;
264  }
265  if (NULL != ghh->mq)
266  {
267  GNUNET_MQ_destroy (ghh->mq);
268  ghh->mq = NULL;
269  }
270  GNUNET_free (ghh);
271 }
272 
273 
274 /* end of transport_api_hello_get.c */
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
Definition: gnunet-abd.c:36
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
static struct GNUNET_MQ_Envelope * ac
Handle to current GNUNET_PEERINFO_add_peer() operation.
Helper library for handling HELLOs.
Constants for network protocols.
obtain information about our current address
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:1057
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:649
#define GNUNET_log(kind,...)
@ GNUNET_OK
@ GNUNET_SYSERR
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.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
@ 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:304
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:78
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:683
#define GNUNET_MESSAGE_TYPE_TRANSPORT_START
Message from the core saying that the transport server should start giving it messages.
#define GNUNET_MESSAGE_TYPE_HELLO
HELLO message with friend only flag used for communicating peer addresses.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:975
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:1272
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
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.
void(* GNUNET_TRANSPORT_HelloUpdateCallback)(void *cls, const struct GNUNET_MessageHeader *hello)
Function called whenever there is an update to the HELLO of this peer.
void GNUNET_TRANSPORT_hello_get_cancel(struct GNUNET_TRANSPORT_HelloGetHandle *ghh)
Stop receiving updates about changes to our HELLO message.
GNUNET_TRANSPORT_AddressClass
Some addresses contain sensitive information or are not suitable for global distribution.
A HELLO message is used to exchange information about transports with other peers.
Handle to a message queue.
Definition: mq.c:87
Message handler for a specific message type.
Header for all communications.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
The identity of the host (wraps the signing key of the peer).
Entry in list of pending tasks.
Definition: scheduler.c:136
Time for relative time used by GNUnet, in microseconds.
Functions to call with this peer's HELLO.
struct GNUNET_SCHEDULER_Task * reconnect_task
ID of the task trying to reconnect to the service.
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
struct GNUNET_SCHEDULER_Task * notify_task
Task for calling the HelloUpdateCallback when we already have a HELLO.
GNUNET_TRANSPORT_HelloUpdateCallback rec
Callback to call once we got our HELLO.
enum GNUNET_TRANSPORT_AddressClass ac
Type of HELLOs client cares about.
struct GNUNET_TIME_Relative reconnect_delay
Delay until we try to reconnect.
struct GNUNET_MQ_Handle * mq
Transport handle.
Message from the transport service to the library asking to check if both processes agree about this ...
Definition: transport.h:92
uint32_t options
0: no options 1: The self field should be checked 2: this client is interested in payload traffic
Definition: transport.h:103
common internal definitions for transport service
static void handle_hello(void *cls, const struct GNUNET_MessageHeader *msg)
Function we use for handling incoming HELLO messages.
static int check_hello(void *cls, const struct GNUNET_MessageHeader *msg)
Function we use for checking incoming HELLO messages.
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.
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...
static void reconnect(void *cls)
Try again to connect to transport service.