2 This file is part of GNUnet.
3 Copyright (C) 2001, 2002, 2004, 2005, 2007, 2009, 2010 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file peerinfo/peerinfo_api_notify.c
23 * @brief notify API to access peerinfo service
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_peerinfo_service.h"
29 #include "gnunet_protocols.h"
32 #define LOG(kind,...) GNUNET_log_from (kind, "peerinfo-api",__VA_ARGS__)
35 * Context for the info handler.
37 struct GNUNET_PEERINFO_NotifyContext
41 * Our connection to the PEERINFO service.
43 struct GNUNET_MQ_Handle *mq;
46 * Function to call with information.
48 GNUNET_PEERINFO_Processor callback;
51 * Closure for @e callback.
58 const struct GNUNET_CONFIGURATION_Handle *cfg;
61 * Tasked used for delayed re-connection attempt.
63 struct GNUNET_SCHEDULER_Task *task;
66 * Include friend only HELLOs in callbacks
68 int include_friend_only;
73 * Task to re-try connecting to peerinfo.
75 * @param cls the `struct GNUNET_PEERINFO_NotifyContext *`
78 reconnect (void *cls);
82 * We encountered an error, reconnect to the service.
84 * @param nc context to reconnect
87 do_reconnect (struct GNUNET_PEERINFO_NotifyContext *nc)
89 GNUNET_MQ_destroy (nc->mq);
91 nc->task = GNUNET_SCHEDULER_add_now (&reconnect,
97 * We got a disconnect after asking regex to do the announcement.
100 * @param cls the `struct GNUNET_PEERINFO_NotifyContext` to retry
101 * @param error error code
104 mq_error_handler (void *cls,
105 enum GNUNET_MQ_Error error)
107 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
114 * Check that a peerinfo information message is well-formed.
117 * @param im message received
118 * @return #GNUNET_OK if the message is well-formed
121 check_notification (void *cls,
122 const struct InfoMessage *im)
124 uint16_t ms = ntohs (im->header.size) - sizeof (*im);
126 if (ms >= sizeof (struct GNUNET_MessageHeader))
128 const struct GNUNET_HELLO_Message *hello;
130 hello = (const struct GNUNET_HELLO_Message *) &im[1];
131 if (ms != GNUNET_HELLO_size (hello))
134 return GNUNET_SYSERR;
141 return GNUNET_SYSERR;
143 return GNUNET_OK; /* odd... */
148 * Receive a peerinfo information message, process it.
151 * @param im message received
154 handle_notification (void *cls,
155 const struct InfoMessage *im)
157 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
158 const struct GNUNET_HELLO_Message *hello;
159 uint16_t ms = ntohs (im->header.size) - sizeof (struct InfoMessage);
163 hello = (const struct GNUNET_HELLO_Message *) &im[1];
164 LOG (GNUNET_ERROR_TYPE_DEBUG,
165 "Received information about peer `%s' from peerinfo database\n",
166 GNUNET_i2s (&im->peer));
167 nc->callback (nc->callback_cls,
175 * Type of a function to call when we receive a message from the
176 * service. Call the iterator with the result and (if applicable)
177 * continue to receive more messages or trigger processing the next
178 * event (if applicable).
181 * @param msg message received, NULL on timeout or fatal error
184 handle_end_iteration (void *cls,
185 const struct GNUNET_MessageHeader *msg)
187 /* these are ignored by the notify API */
192 * Task to re-try connecting to peerinfo.
194 * @param cls the `struct GNUNET_PEERINFO_NotifyContext *`
197 reconnect (void *cls)
199 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
200 struct GNUNET_MQ_MessageHandler handlers[] = {
201 GNUNET_MQ_hd_var_size (notification,
202 GNUNET_MESSAGE_TYPE_PEERINFO_INFO,
205 GNUNET_MQ_hd_fixed_size (end_iteration,
206 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END,
207 struct GNUNET_MessageHeader,
209 GNUNET_MQ_handler_end ()
211 struct GNUNET_MQ_Envelope *env;
212 struct NotifyMessage *nm;
215 nc->mq = GNUNET_CLIENT_connect (nc->cfg,
222 env = GNUNET_MQ_msg (nm,
223 GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
224 nm->include_friend_only = htonl (nc->include_friend_only);
225 GNUNET_MQ_send (nc->mq,
231 * Call a method whenever our known information about peers
232 * changes. Initially calls the given function for all known
233 * peers and then only signals changes.
235 * If @a include_friend_only is set to #GNUNET_YES peerinfo will include HELLO
236 * messages which are intended for friend to friend mode and which do not
237 * have to be gossiped. Otherwise these messages are skipped.
239 * @param cfg configuration to use
240 * @param include_friend_only include HELLO messages for friends only
241 * @param callback the method to call for each peer
242 * @param callback_cls closure for @a callback
243 * @return NULL on error
245 struct GNUNET_PEERINFO_NotifyContext *
246 GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
247 int include_friend_only,
248 GNUNET_PEERINFO_Processor callback,
251 struct GNUNET_PEERINFO_NotifyContext *nc;
253 nc = GNUNET_new (struct GNUNET_PEERINFO_NotifyContext);
255 nc->callback = callback;
256 nc->callback_cls = callback_cls;
257 nc->include_friend_only = include_friend_only;
261 LOG (GNUNET_ERROR_TYPE_WARNING,
262 "Could not connect to PEERINFO service.\n");
271 * Stop notifying about changes.
273 * @param nc context to stop notifying
276 GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
280 GNUNET_MQ_destroy (nc->mq);
283 if (NULL != nc->task)
285 GNUNET_SCHEDULER_cancel (nc->task);
291 /* end of peerinfo_api_notify.c */