summaryrefslogtreecommitdiff
path: root/src/peerinfo/peerinfo_api_notify.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/peerinfo/peerinfo_api_notify.c')
-rw-r--r--src/peerinfo/peerinfo_api_notify.c163
1 files changed, 82 insertions, 81 deletions
diff --git a/src/peerinfo/peerinfo_api_notify.c b/src/peerinfo/peerinfo_api_notify.c
index 130ce3a5d..778fe2344 100644
--- a/src/peerinfo/peerinfo_api_notify.c
+++ b/src/peerinfo/peerinfo_api_notify.c
@@ -29,12 +29,13 @@
29#include "gnunet_protocols.h" 29#include "gnunet_protocols.h"
30#include "peerinfo.h" 30#include "peerinfo.h"
31 31
32#define LOG(kind, ...) GNUNET_log_from(kind, "peerinfo-api", __VA_ARGS__) 32#define LOG(kind, ...) GNUNET_log_from (kind, "peerinfo-api", __VA_ARGS__)
33 33
34/** 34/**
35 * Context for the info handler. 35 * Context for the info handler.
36 */ 36 */
37struct GNUNET_PEERINFO_NotifyContext { 37struct GNUNET_PEERINFO_NotifyContext
38{
38 /** 39 /**
39 * Our connection to the PEERINFO service. 40 * Our connection to the PEERINFO service.
40 */ 41 */
@@ -73,7 +74,7 @@ struct GNUNET_PEERINFO_NotifyContext {
73 * @param cls the `struct GNUNET_PEERINFO_NotifyContext *` 74 * @param cls the `struct GNUNET_PEERINFO_NotifyContext *`
74 */ 75 */
75static void 76static void
76reconnect(void *cls); 77reconnect (void *cls);
77 78
78 79
79/** 80/**
@@ -82,12 +83,12 @@ reconnect(void *cls);
82 * @param nc context to reconnect 83 * @param nc context to reconnect
83 */ 84 */
84static void 85static void
85do_reconnect(struct GNUNET_PEERINFO_NotifyContext *nc) 86do_reconnect (struct GNUNET_PEERINFO_NotifyContext *nc)
86{ 87{
87 GNUNET_MQ_destroy(nc->mq); 88 GNUNET_MQ_destroy (nc->mq);
88 nc->mq = NULL; 89 nc->mq = NULL;
89 nc->task = GNUNET_SCHEDULER_add_now(&reconnect, 90 nc->task = GNUNET_SCHEDULER_add_now (&reconnect,
90 nc); 91 nc);
91} 92}
92 93
93 94
@@ -99,12 +100,12 @@ do_reconnect(struct GNUNET_PEERINFO_NotifyContext *nc)
99 * @param error error code 100 * @param error error code
100 */ 101 */
101static void 102static void
102mq_error_handler(void *cls, 103mq_error_handler (void *cls,
103 enum GNUNET_MQ_Error error) 104 enum GNUNET_MQ_Error error)
104{ 105{
105 struct GNUNET_PEERINFO_NotifyContext *nc = cls; 106 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
106 107
107 do_reconnect(nc); 108 do_reconnect (nc);
108} 109}
109 110
110 111
@@ -116,28 +117,28 @@ mq_error_handler(void *cls,
116 * @return #GNUNET_OK if the message is well-formed 117 * @return #GNUNET_OK if the message is well-formed
117 */ 118 */
118static int 119static int
119check_notification(void *cls, 120check_notification (void *cls,
120 const struct InfoMessage *im) 121 const struct InfoMessage *im)
121{ 122{
122 uint16_t ms = ntohs(im->header.size) - sizeof(*im); 123 uint16_t ms = ntohs (im->header.size) - sizeof(*im);
123 124
124 if (ms >= sizeof(struct GNUNET_MessageHeader)) 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))
125 { 131 {
126 const struct GNUNET_HELLO_Message *hello; 132 GNUNET_break (0);
127
128 hello = (const struct GNUNET_HELLO_Message *)&im[1];
129 if (ms != GNUNET_HELLO_size(hello))
130 {
131 GNUNET_break(0);
132 return GNUNET_SYSERR;
133 }
134 return GNUNET_OK;
135 }
136 if (0 != ms)
137 {
138 GNUNET_break(0);
139 return GNUNET_SYSERR; 133 return GNUNET_SYSERR;
140 } 134 }
135 return GNUNET_OK;
136 }
137 if (0 != ms)
138 {
139 GNUNET_break (0);
140 return GNUNET_SYSERR;
141 }
141 return GNUNET_OK; /* odd... */ 142 return GNUNET_OK; /* odd... */
142} 143}
143 144
@@ -149,23 +150,23 @@ check_notification(void *cls,
149 * @param im message received 150 * @param im message received
150 */ 151 */
151static void 152static void
152handle_notification(void *cls, 153handle_notification (void *cls,
153 const struct InfoMessage *im) 154 const struct InfoMessage *im)
154{ 155{
155 struct GNUNET_PEERINFO_NotifyContext *nc = cls; 156 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
156 const struct GNUNET_HELLO_Message *hello; 157 const struct GNUNET_HELLO_Message *hello;
157 uint16_t ms = ntohs(im->header.size) - sizeof(struct InfoMessage); 158 uint16_t ms = ntohs (im->header.size) - sizeof(struct InfoMessage);
158 159
159 if (0 == ms) 160 if (0 == ms)
160 return; 161 return;
161 hello = (const struct GNUNET_HELLO_Message *)&im[1]; 162 hello = (const struct GNUNET_HELLO_Message *) &im[1];
162 LOG(GNUNET_ERROR_TYPE_DEBUG, 163 LOG (GNUNET_ERROR_TYPE_DEBUG,
163 "Received information about peer `%s' from peerinfo database\n", 164 "Received information about peer `%s' from peerinfo database\n",
164 GNUNET_i2s(&im->peer)); 165 GNUNET_i2s (&im->peer));
165 nc->callback(nc->callback_cls, 166 nc->callback (nc->callback_cls,
166 &im->peer, 167 &im->peer,
167 hello, 168 hello,
168 NULL); 169 NULL);
169} 170}
170 171
171 172
@@ -179,8 +180,8 @@ handle_notification(void *cls,
179 * @param msg message received, NULL on timeout or fatal error 180 * @param msg message received, NULL on timeout or fatal error
180 */ 181 */
181static void 182static void
182handle_end_iteration(void *cls, 183handle_end_iteration (void *cls,
183 const struct GNUNET_MessageHeader *msg) 184 const struct GNUNET_MessageHeader *msg)
184{ 185{
185 /* these are ignored by the notify API */ 186 /* these are ignored by the notify API */
186} 187}
@@ -192,36 +193,36 @@ handle_end_iteration(void *cls,
192 * @param cls the `struct GNUNET_PEERINFO_NotifyContext *` 193 * @param cls the `struct GNUNET_PEERINFO_NotifyContext *`
193 */ 194 */
194static void 195static void
195reconnect(void *cls) 196reconnect (void *cls)
196{ 197{
197 struct GNUNET_PEERINFO_NotifyContext *nc = cls; 198 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
198 struct GNUNET_MQ_MessageHandler handlers[] = { 199 struct GNUNET_MQ_MessageHandler handlers[] = {
199 GNUNET_MQ_hd_var_size(notification, 200 GNUNET_MQ_hd_var_size (notification,
200 GNUNET_MESSAGE_TYPE_PEERINFO_INFO, 201 GNUNET_MESSAGE_TYPE_PEERINFO_INFO,
201 struct InfoMessage, 202 struct InfoMessage,
202 nc), 203 nc),
203 GNUNET_MQ_hd_fixed_size(end_iteration, 204 GNUNET_MQ_hd_fixed_size (end_iteration,
204 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END, 205 GNUNET_MESSAGE_TYPE_PEERINFO_INFO_END,
205 struct GNUNET_MessageHeader, 206 struct GNUNET_MessageHeader,
206 nc), 207 nc),
207 GNUNET_MQ_handler_end() 208 GNUNET_MQ_handler_end ()
208 }; 209 };
209 struct GNUNET_MQ_Envelope *env; 210 struct GNUNET_MQ_Envelope *env;
210 struct NotifyMessage *nm; 211 struct NotifyMessage *nm;
211 212
212 nc->task = NULL; 213 nc->task = NULL;
213 nc->mq = GNUNET_CLIENT_connect(nc->cfg, 214 nc->mq = GNUNET_CLIENT_connect (nc->cfg,
214 "peerinfo", 215 "peerinfo",
215 handlers, 216 handlers,
216 &mq_error_handler, 217 &mq_error_handler,
217 nc); 218 nc);
218 if (NULL == nc->mq) 219 if (NULL == nc->mq)
219 return; 220 return;
220 env = GNUNET_MQ_msg(nm, 221 env = GNUNET_MQ_msg (nm,
221 GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY); 222 GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
222 nm->include_friend_only = htonl(nc->include_friend_only); 223 nm->include_friend_only = htonl (nc->include_friend_only);
223 GNUNET_MQ_send(nc->mq, 224 GNUNET_MQ_send (nc->mq,
224 env); 225 env);
225} 226}
226 227
227 228
@@ -241,26 +242,26 @@ reconnect(void *cls)
241 * @return NULL on error 242 * @return NULL on error
242 */ 243 */
243struct GNUNET_PEERINFO_NotifyContext * 244struct GNUNET_PEERINFO_NotifyContext *
244GNUNET_PEERINFO_notify(const struct GNUNET_CONFIGURATION_Handle *cfg, 245GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
245 int include_friend_only, 246 int include_friend_only,
246 GNUNET_PEERINFO_Processor callback, 247 GNUNET_PEERINFO_Processor callback,
247 void *callback_cls) 248 void *callback_cls)
248{ 249{
249 struct GNUNET_PEERINFO_NotifyContext *nc; 250 struct GNUNET_PEERINFO_NotifyContext *nc;
250 251
251 nc = GNUNET_new(struct GNUNET_PEERINFO_NotifyContext); 252 nc = GNUNET_new (struct GNUNET_PEERINFO_NotifyContext);
252 nc->cfg = cfg; 253 nc->cfg = cfg;
253 nc->callback = callback; 254 nc->callback = callback;
254 nc->callback_cls = callback_cls; 255 nc->callback_cls = callback_cls;
255 nc->include_friend_only = include_friend_only; 256 nc->include_friend_only = include_friend_only;
256 reconnect(nc); 257 reconnect (nc);
257 if (NULL == nc->mq) 258 if (NULL == nc->mq)
258 { 259 {
259 LOG(GNUNET_ERROR_TYPE_WARNING, 260 LOG (GNUNET_ERROR_TYPE_WARNING,
260 "Could not connect to PEERINFO service.\n"); 261 "Could not connect to PEERINFO service.\n");
261 GNUNET_free(nc); 262 GNUNET_free (nc);
262 return NULL; 263 return NULL;
263 } 264 }
264 return nc; 265 return nc;
265} 266}
266 267
@@ -271,19 +272,19 @@ GNUNET_PEERINFO_notify(const struct GNUNET_CONFIGURATION_Handle *cfg,
271 * @param nc context to stop notifying 272 * @param nc context to stop notifying
272 */ 273 */
273void 274void
274GNUNET_PEERINFO_notify_cancel(struct GNUNET_PEERINFO_NotifyContext *nc) 275GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
275{ 276{
276 if (NULL != nc->mq) 277 if (NULL != nc->mq)
277 { 278 {
278 GNUNET_MQ_destroy(nc->mq); 279 GNUNET_MQ_destroy (nc->mq);
279 nc->mq = NULL; 280 nc->mq = NULL;
280 } 281 }
281 if (NULL != nc->task) 282 if (NULL != nc->task)
282 { 283 {
283 GNUNET_SCHEDULER_cancel(nc->task); 284 GNUNET_SCHEDULER_cancel (nc->task);
284 nc->task = NULL; 285 nc->task = NULL;
285 } 286 }
286 GNUNET_free(nc); 287 GNUNET_free (nc);
287} 288}
288 289
289/* end of peerinfo_api_notify.c */ 290/* end of peerinfo_api_notify.c */