GNUnet  0.11.x
peerinfo_api_notify.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 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"
29 #include "gnunet_protocols.h"
30 #include "peerinfo.h"
31 
32 #define LOG(kind, ...) GNUNET_log_from (kind, "peerinfo-api", __VA_ARGS__)
33 
38 {
43 
48 
52  void *callback_cls;
53 
58 
63 
68 };
69 
70 
76 static void
77 reconnect (void *cls);
78 
79 
85 static void
87 {
88  GNUNET_MQ_destroy (nc->mq);
89  nc->mq = NULL;
91  nc);
92 }
93 
94 
102 static void
103 mq_error_handler (void *cls,
104  enum GNUNET_MQ_Error error)
105 {
106  struct GNUNET_PEERINFO_NotifyContext *nc = cls;
107 
108  do_reconnect (nc);
109 }
110 
111 
119 static int
121  const struct InfoMessage *im)
122 {
123  uint16_t ms = ntohs (im->header.size) - sizeof(*im);
124 
125  if (ms >= sizeof(struct GNUNET_MessageHeader))
126  {
127  const struct GNUNET_HELLO_Message *hello;
128 
129  hello = (const struct GNUNET_HELLO_Message *) &im[1];
130  if (ms != GNUNET_HELLO_size (hello))
131  {
132  GNUNET_break (0);
133  return GNUNET_SYSERR;
134  }
135  return GNUNET_OK;
136  }
137  if (0 != ms)
138  {
139  GNUNET_break (0);
140  return GNUNET_SYSERR;
141  }
142  return GNUNET_OK; /* odd... */
143 }
144 
145 
152 static void
154  const struct InfoMessage *im)
155 {
156  struct GNUNET_PEERINFO_NotifyContext *nc = cls;
157  const struct GNUNET_HELLO_Message *hello;
158  uint16_t ms = ntohs (im->header.size) - sizeof(struct InfoMessage);
159 
160  if (0 == ms)
161  return;
162  hello = (const struct GNUNET_HELLO_Message *) &im[1];
164  "Received information about peer `%s' from peerinfo database\n",
165  GNUNET_i2s (&im->peer));
166  nc->callback (nc->callback_cls,
167  &im->peer,
168  hello,
169  NULL);
170 }
171 
172 
182 static void
184  const struct GNUNET_MessageHeader *msg)
185 {
186  /* these are ignored by the notify API */
187 }
188 
189 
195 static void
196 reconnect (void *cls)
197 {
198  struct GNUNET_PEERINFO_NotifyContext *nc = cls;
199  struct GNUNET_MQ_MessageHandler handlers[] = {
200  GNUNET_MQ_hd_var_size (notification,
202  struct InfoMessage,
203  nc),
204  GNUNET_MQ_hd_fixed_size (end_iteration,
206  struct GNUNET_MessageHeader,
207  nc),
209  };
210  struct GNUNET_MQ_Envelope *env;
211  struct NotifyMessage *nm;
212 
213  nc->task = NULL;
214  nc->mq = GNUNET_CLIENT_connect (nc->cfg,
215  "peerinfo",
216  handlers,
218  nc);
219  if (NULL == nc->mq)
220  return;
221  env = GNUNET_MQ_msg (nm,
223  nm->include_friend_only = htonl (nc->include_friend_only);
224  GNUNET_MQ_send (nc->mq,
225  env);
226 }
227 
228 
248  void *callback_cls)
249 {
251 
253  nc->cfg = cfg;
254  nc->callback = callback;
257  reconnect (nc);
258  if (NULL == nc->mq)
259  {
261  "Could not connect to PEERINFO service.\n");
262  GNUNET_free (nc);
263  return NULL;
264  }
265  return nc;
266 }
267 
268 
274 void
276 {
277  if (NULL != nc->mq)
278  {
279  GNUNET_MQ_destroy (nc->mq);
280  nc->mq = NULL;
281  }
282  if (NULL != nc->task)
283  {
285  nc->task = NULL;
286  }
287  GNUNET_free (nc);
288 }
289 
290 
291 /* end of peerinfo_api_notify.c */
void(* GNUNET_PEERINFO_Processor)(void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_HELLO_Message *hello, const char *err_msg)
Type of an iterator over the hosts.
static void do_reconnect(struct GNUNET_PEERINFO_NotifyContext *nc)
We encountered an error, reconnect to the service.
A HELLO message is used to exchange information about transports with other peers.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
uint32_t include_friend_only
Include friend only HELLOs and peers in callbacks.
Definition: peerinfo.h:91
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
Context for the info handler.
static void reconnect(void *cls)
Task to re-try connecting to peerinfo.
GNUNET_MQ_Error
Error codes for the queue.
Message used to inform the client about a particular peer; this message is optionally followed by a H...
Definition: peerinfo.h:102
void GNUNET_PEERINFO_notify_cancel(struct GNUNET_PEERINFO_NotifyContext *nc)
Stop notifying about changes.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
We got a disconnect after asking regex to do the announcement.
#define GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY
Start notifying this client about all changes to the known peers until it disconnects.
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
int include_friend_only
Include friend only HELLOs in callbacks.
struct GNUNET_MQ_Handle * mq
Our connection to the PEERINFO service.
#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.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void * callback_cls
Closure for callback.
static void handle_end_iteration(void *cls, const struct GNUNET_MessageHeader *msg)
Type of a function to call when we receive a message from the service.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_PEERINFO_NotifyContext * GNUNET_PEERINFO_notify(const struct GNUNET_CONFIGURATION_Handle *cfg, int include_friend_only, GNUNET_PEERINFO_Processor callback, void *callback_cls)
Call a method whenever our known information about peers changes.
struct GNUNET_PeerIdentity peer
About which peer are we talking here?
Definition: peerinfo.h:117
static void handle_notification(void *cls, const struct InfoMessage *im)
Receive a peerinfo information message, process it.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition: scheduler.c:1280
#define LOG(kind,...)
struct GNUNET_MessageHeader header
Type will be GNUNET_MESSAGE_TYPE_PEERINFO_INFO.
Definition: peerinfo.h:107
Message handler for a specific message type.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static struct GNUNET_PEERINFO_NotifyContext * nc
Iterator context.
common internal definitions for peerinfo service
static int check_notification(void *cls, const struct InfoMessage *im)
Check that a peerinfo information message is well-formed.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_MESSAGE_TYPE_PEERINFO_INFO
Information about one of the peers.
Handle to a message queue.
Definition: mq.c:85
struct GNUNET_SCHEDULER_Task * task
Tasked used for delayed re-connection attempt.
configuration data
Definition: configuration.c:85
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration.
Entry in list of pending tasks.
Definition: scheduler.c:134
uint16_t GNUNET_HELLO_size(const struct GNUNET_HELLO_Message *hello)
Return the size of the given HELLO message.
Definition: hello.c:652
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_PEERINFO_INFO_END
End of information about other peers.
GNUNET_PEERINFO_Processor callback
Function to call with information.
const char * GNUNET_i2s(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.
Header for all communications.
Definition: peerinfo.h:81
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:966