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 
40 {
41 
46 
51 
56 
60  void *rec_cls;
61 
66 
71 
76 
81 };
82 
83 
91 static int
92 check_hello (void *cls,
93  const struct GNUNET_MessageHeader *msg)
94 {
95  struct GNUNET_PeerIdentity me;
96 
97  if (GNUNET_OK !=
98  GNUNET_HELLO_get_id ((const struct GNUNET_HELLO_Message *) msg,
99  &me))
100  {
101  GNUNET_break (0);
102  return GNUNET_SYSERR;
103  }
105  "Receiving (my own) HELLO message (%u bytes), I am `%s'.\n",
106  (unsigned int) ntohs (msg->size),
107  GNUNET_i2s (&me));
108  return GNUNET_OK;
109 }
110 
111 
118 static void
119 handle_hello (void *cls,
120  const struct GNUNET_MessageHeader *msg)
121 {
122  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
123 
124  ghh->rec (ghh->rec_cls,
125  msg);
126 }
127 
128 
135 static void
137 
138 
148 static void
149 mq_error_handler (void *cls,
150  enum GNUNET_MQ_Error error)
151 {
152  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
153 
155  "Error receiving from transport service, disconnecting temporarily.\n");
156  GNUNET_MQ_destroy (ghh->mq);
157  ghh->mq = NULL;
158  schedule_reconnect (ghh);
159 }
160 
161 
167 static void
168 reconnect (void *cls)
169 {
170  struct GNUNET_TRANSPORT_HelloGetHandle *ghh = cls;
171  struct GNUNET_MQ_MessageHandler handlers[] = {
172  GNUNET_MQ_hd_var_size (hello,
174  struct GNUNET_MessageHeader,
175  ghh),
177  };
178  struct GNUNET_MQ_Envelope *env;
179  struct StartMessage *s;
180 
181  ghh->reconnect_task = NULL;
183  "Connecting to transport service.\n");
184  GNUNET_assert (NULL == ghh->mq);
185  ghh->mq = GNUNET_CLIENT_connect (ghh->cfg,
186  "transport",
187  handlers,
189  ghh);
190  if (NULL == ghh->mq)
191  return;
192  env = GNUNET_MQ_msg (s,
194  s->options = htonl (0);
195  GNUNET_MQ_send (ghh->mq,
196  env);
197 }
198 
199 
206 static void
208 {
209  ghh->reconnect_task =
211  &reconnect,
212  ghh);
214 }
215 
216 
234  void *rec_cls)
235 {
237 
239  ghh->rec = rec;
240  ghh->rec_cls = rec_cls;
241  ghh->cfg = cfg;
242  ghh->ac = ac;
243  reconnect (ghh);
244  if (NULL == ghh->mq)
245  {
246  GNUNET_free (ghh);
247  return NULL;
248  }
249  return ghh;
250 }
251 
252 
258 void
260 {
261  if (NULL != ghh->reconnect_task)
262  {
264  ghh->reconnect_task = NULL;
265  }
266  if (NULL != ghh->mq)
267  {
268  GNUNET_MQ_destroy (ghh->mq);
269  ghh->mq = NULL;
270  }
271  GNUNET_free (ghh);
272 }
273 
274 
275 /* 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:668
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:901
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:78
#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:1246
#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:104
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:79
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:85
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:85
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:134
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:824
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
#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:965
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.