GNUnet  0.11.x
Data Structures | Macros | Functions
peerinfo_api_notify.c File Reference

notify API to access peerinfo service More...

#include "platform.h"
#include "gnunet_util_lib.h"
#include "gnunet_peerinfo_service.h"
#include "gnunet_protocols.h"
#include "peerinfo.h"
Include dependency graph for peerinfo_api_notify.c:

Go to the source code of this file.

Data Structures

struct  GNUNET_PEERINFO_NotifyContext
 Context for the info handler. More...
 

Macros

#define LOG(kind, ...)   GNUNET_log_from (kind, "peerinfo-api", __VA_ARGS__)
 

Functions

static void reconnect (void *cls)
 Task to re-try connecting to peerinfo. More...
 
static void do_reconnect (struct GNUNET_PEERINFO_NotifyContext *nc)
 We encountered an error, reconnect to the service. More...
 
static void mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
 We got a disconnect after asking regex to do the announcement. More...
 
static int check_notification (void *cls, const struct InfoMessage *im)
 Check that a peerinfo information message is well-formed. More...
 
static void handle_notification (void *cls, const struct InfoMessage *im)
 Receive a peerinfo information message, process it. More...
 
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. More...
 
struct GNUNET_PEERINFO_NotifyContextGNUNET_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. More...
 
void GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
 Stop notifying about changes. More...
 

Detailed Description

notify API to access peerinfo service

Author
Christian Grothoff

Definition in file peerinfo_api_notify.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)    GNUNET_log_from (kind, "peerinfo-api", __VA_ARGS__)

Definition at line 32 of file peerinfo_api_notify.c.

Referenced by GNUNET_PEERINFO_notify(), and handle_notification().

Function Documentation

◆ reconnect()

static void reconnect ( void *  cls)
static

Task to re-try connecting to peerinfo.

Parameters
clsthe struct GNUNET_PEERINFO_NotifyContext *

Definition at line 196 of file peerinfo_api_notify.c.

References GNUNET_PEERINFO_NotifyContext::cfg, env, GNUNET_CLIENT_connect(), GNUNET_MESSAGE_TYPE_PEERINFO_INFO, GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END, GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY, GNUNET_MQ_handler_end, GNUNET_MQ_hd_fixed_size, GNUNET_MQ_hd_var_size, GNUNET_MQ_msg, GNUNET_MQ_send(), GNUNET_PEERINFO_NotifyContext::include_friend_only, NotifyMessage::include_friend_only, GNUNET_PEERINFO_NotifyContext::mq, mq_error_handler(), nc, and GNUNET_PEERINFO_NotifyContext::task.

Referenced by do_reconnect(), and GNUNET_PEERINFO_notify().

197 {
198  struct GNUNET_PEERINFO_NotifyContext *nc = cls;
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 }
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.
Message used to inform the client about a particular peer; this message is optionally followed by a H...
Definition: peerinfo.h:102
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
static struct GNUNET_CADET_MessageHandler handlers[]
Handlers, for diverse services.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
Message handler for a specific message type.
static struct GNUNET_PEERINFO_NotifyContext * nc
Iterator context.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
#define GNUNET_MESSAGE_TYPE_PEERINFO_INFO
Information about one of the peers.
struct GNUNET_SCHEDULER_Task * task
Tasked used for delayed re-connection attempt.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration.
Header for all communications.
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.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
Header for all communications.
Definition: peerinfo.h:81
Here is the call graph for this function:
Here is the caller graph for this function:

◆ do_reconnect()

static void do_reconnect ( struct GNUNET_PEERINFO_NotifyContext nc)
static

We encountered an error, reconnect to the service.

Parameters
nccontext to reconnect

Definition at line 86 of file peerinfo_api_notify.c.

References GNUNET_MQ_destroy(), GNUNET_SCHEDULER_add_now(), GNUNET_PEERINFO_NotifyContext::mq, reconnect(), and GNUNET_PEERINFO_NotifyContext::task.

Referenced by mq_error_handler().

87 {
88  GNUNET_MQ_destroy (nc->mq);
89  nc->mq = NULL;
91  nc);
92 }
static void reconnect(void *cls)
Task to re-try connecting to peerinfo.
struct GNUNET_MQ_Handle * mq
Our connection to the PEERINFO service.
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
struct GNUNET_SCHEDULER_Task * task
Tasked used for delayed re-connection attempt.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
Here is the call graph for this function:
Here is the caller graph for this function:

◆ mq_error_handler()

static void mq_error_handler ( void *  cls,
enum GNUNET_MQ_Error  error 
)
static

We got a disconnect after asking regex to do the announcement.

Retry.

Parameters
clsthe struct GNUNET_PEERINFO_NotifyContext to retry
errorerror code

Definition at line 103 of file peerinfo_api_notify.c.

References do_reconnect(), and nc.

Referenced by reconnect().

105 {
106  struct GNUNET_PEERINFO_NotifyContext *nc = cls;
107 
108  do_reconnect (nc);
109 }
static void do_reconnect(struct GNUNET_PEERINFO_NotifyContext *nc)
We encountered an error, reconnect to the service.
Context for the info handler.
static struct GNUNET_PEERINFO_NotifyContext * nc
Iterator context.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_notification()

static int check_notification ( void *  cls,
const struct InfoMessage im 
)
static

Check that a peerinfo information message is well-formed.

Parameters
clsclosure
immessage received
Returns
GNUNET_OK if the message is well-formed

Definition at line 120 of file peerinfo_api_notify.c.

References GNUNET_break, GNUNET_HELLO_size(), GNUNET_OK, GNUNET_SYSERR, InfoMessage::header, and GNUNET_MessageHeader::size.

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 }
A HELLO message is used to exchange information about transports with other peers.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
struct GNUNET_MessageHeader header
Type will be GNUNET_MESSAGE_TYPE_PEERINFO_INFO.
Definition: peerinfo.h:107
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
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.
Here is the call graph for this function:

◆ handle_notification()

static void handle_notification ( void *  cls,
const struct InfoMessage im 
)
static

Receive a peerinfo information message, process it.

Parameters
clsclosure
immessage received

Definition at line 153 of file peerinfo_api_notify.c.

References GNUNET_PEERINFO_NotifyContext::callback, GNUNET_PEERINFO_NotifyContext::callback_cls, GNUNET_ERROR_TYPE_DEBUG, GNUNET_i2s(), InfoMessage::header, LOG, nc, InfoMessage::peer, and GNUNET_MessageHeader::size.

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 }
A HELLO message is used to exchange information about transports with other peers.
Context for the info handler.
Message used to inform the client about a particular peer; this message is optionally followed by a H...
Definition: peerinfo.h:102
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.
struct GNUNET_PeerIdentity peer
About which peer are we talking here?
Definition: peerinfo.h:117
#define LOG(kind,...)
struct GNUNET_MessageHeader header
Type will be GNUNET_MESSAGE_TYPE_PEERINFO_INFO.
Definition: peerinfo.h:107
static struct GNUNET_PEERINFO_NotifyContext * nc
Iterator context.
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).
Here is the call graph for this function:

◆ handle_end_iteration()

static void handle_end_iteration ( void *  cls,
const struct GNUNET_MessageHeader msg 
)
static

Type of a function to call when we receive a message from the service.

Call the iterator with the result and (if applicable) continue to receive more messages or trigger processing the next event (if applicable).

Parameters
clsclosure
msgmessage received, NULL on timeout or fatal error

Definition at line 183 of file peerinfo_api_notify.c.

185 {
186  /* these are ignored by the notify API */
187 }