aboutsummaryrefslogtreecommitdiff
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.c128
1 files changed, 59 insertions, 69 deletions
diff --git a/src/peerinfo/peerinfo_api_notify.c b/src/peerinfo/peerinfo_api_notify.c
index eb6e29ce5..0c7474f4a 100644
--- a/src/peerinfo/peerinfo_api_notify.c
+++ b/src/peerinfo/peerinfo_api_notify.c
@@ -76,8 +76,7 @@ struct GNUNET_PEERINFO_NotifyContext
76 * 76 *
77 * @param nc our context 77 * @param nc our context
78 */ 78 */
79static void 79static void request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
80request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
81 80
82 81
83/** 82/**
@@ -86,8 +85,7 @@ request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
86 * 85 *
87 * @param nc our context 86 * @param nc our context
88 */ 87 */
89static void 88static void receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
90receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
91 89
92 90
93/** 91/**
@@ -95,23 +93,21 @@ receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc);
95 * 93 *
96 * @param cls the 'struct GNUNET_PEERINFO_NotifyContext' 94 * @param cls the 'struct GNUNET_PEERINFO_NotifyContext'
97 * @param tc scheduler context 95 * @param tc scheduler context
98 */ 96 */
99static void 97static void
100reconnect (void *cls, 98reconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
101 const struct GNUNET_SCHEDULER_TaskContext *tc)
102{ 99{
103 struct GNUNET_PEERINFO_NotifyContext *nc = cls; 100 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
104 101
105 nc->task = GNUNET_SCHEDULER_NO_TASK; 102 nc->task = GNUNET_SCHEDULER_NO_TASK;
106 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg); 103 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
107 if (NULL == nc->client) 104 if (NULL == nc->client)
108 { 105 {
109 /* ugh */ 106 /* ugh */
110 nc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, 107 nc->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
111 &reconnect, 108 &reconnect, nc);
112 nc); 109 return;
113 return; 110 }
114 }
115 request_notifications (nc); 111 request_notifications (nc);
116} 112}
117 113
@@ -124,9 +120,7 @@ reconnect (void *cls,
124 * @param msg message received, NULL on timeout or fatal error 120 * @param msg message received, NULL on timeout or fatal error
125 */ 121 */
126static void 122static void
127process_notification (void *cls, 123process_notification (void *cls, const struct GNUNET_MessageHeader *msg)
128 const struct
129 GNUNET_MessageHeader * msg)
130{ 124{
131 struct GNUNET_PEERINFO_NotifyContext *nc = cls; 125 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
132 const struct InfoMessage *im; 126 const struct InfoMessage *im;
@@ -134,14 +128,27 @@ process_notification (void *cls,
134 uint16_t ms; 128 uint16_t ms;
135 129
136 if (msg == NULL) 130 if (msg == NULL)
137 { 131 {
138 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO); 132 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
139 reconnect (nc, NULL); 133 reconnect (nc, NULL);
140 return; 134 return;
141 } 135 }
142 ms = ntohs (msg->size); 136 ms = ntohs (msg->size);
143 if ((ms < sizeof (struct InfoMessage)) || 137 if ((ms < sizeof (struct InfoMessage)) ||
144 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO)) 138 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_PEERINFO_INFO))
139 {
140 GNUNET_break (0);
141 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
142 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
143 request_notifications (nc);
144 return;
145 }
146 im = (const struct InfoMessage *) msg;
147 hello = NULL;
148 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
149 {
150 hello = (const struct GNUNET_HELLO_Message *) &im[1];
151 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
145 { 152 {
146 GNUNET_break (0); 153 GNUNET_break (0);
147 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO); 154 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
@@ -149,24 +156,11 @@ process_notification (void *cls,
149 request_notifications (nc); 156 request_notifications (nc);
150 return; 157 return;
151 } 158 }
152 im = (const struct InfoMessage *) msg; 159 }
153 hello = NULL;
154 if (ms > sizeof (struct InfoMessage) + sizeof (struct GNUNET_MessageHeader))
155 {
156 hello = (const struct GNUNET_HELLO_Message *) &im[1];
157 if (ms != sizeof (struct InfoMessage) + GNUNET_HELLO_size (hello))
158 {
159 GNUNET_break (0);
160 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
161 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
162 request_notifications (nc);
163 return;
164 }
165 }
166#if DEBUG_PEERINFO 160#if DEBUG_PEERINFO
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Received information about peer `%s' from peerinfo database\n", 162 "Received information about peer `%s' from peerinfo database\n",
169 GNUNET_i2s (&im->peer)); 163 GNUNET_i2s (&im->peer));
170#endif 164#endif
171 nc->callback (nc->callback_cls, &im->peer, hello, NULL); 165 nc->callback (nc->callback_cls, &im->peer, hello, NULL);
172 receive_notifications (nc); 166 receive_notifications (nc);
@@ -183,9 +177,8 @@ static void
183receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc) 177receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
184{ 178{
185 GNUNET_CLIENT_receive (nc->client, 179 GNUNET_CLIENT_receive (nc->client,
186 &process_notification, 180 &process_notification,
187 nc, 181 nc, GNUNET_TIME_UNIT_FOREVER_REL);
188 GNUNET_TIME_UNIT_FOREVER_REL);
189} 182}
190 183
191 184
@@ -197,22 +190,20 @@ receive_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
197 * @param buf where the callee should write the message 190 * @param buf where the callee should write the message
198 * @return number of bytes written to buf 191 * @return number of bytes written to buf
199 */ 192 */
200static size_t 193static size_t
201transmit_notify_request (void *cls, 194transmit_notify_request (void *cls, size_t size, void *buf)
202 size_t size,
203 void *buf)
204{ 195{
205 struct GNUNET_PEERINFO_NotifyContext *nc = cls; 196 struct GNUNET_PEERINFO_NotifyContext *nc = cls;
206 struct GNUNET_MessageHeader hdr; 197 struct GNUNET_MessageHeader hdr;
207 198
208 nc->init = NULL; 199 nc->init = NULL;
209 if (buf == NULL) 200 if (buf == NULL)
210 { 201 {
211 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO); 202 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
212 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg); 203 nc->client = GNUNET_CLIENT_connect ("peerinfo", nc->cfg);
213 request_notifications (nc); 204 request_notifications (nc);
214 return 0; 205 return 0;
215 } 206 }
216 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader)); 207 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
217 hdr.size = htons (sizeof (struct GNUNET_MessageHeader)); 208 hdr.size = htons (sizeof (struct GNUNET_MessageHeader));
218 hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY); 209 hdr.type = htons (GNUNET_MESSAGE_TYPE_PEERINFO_NOTIFY);
@@ -232,12 +223,12 @@ static void
232request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc) 223request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
233{ 224{
234 GNUNET_assert (NULL == nc->init); 225 GNUNET_assert (NULL == nc->init);
235 nc->init =GNUNET_CLIENT_notify_transmit_ready (nc->client, 226 nc->init = GNUNET_CLIENT_notify_transmit_ready (nc->client,
236 sizeof (struct GNUNET_MessageHeader), 227 sizeof (struct
237 GNUNET_TIME_UNIT_FOREVER_REL, 228 GNUNET_MessageHeader),
238 GNUNET_YES, 229 GNUNET_TIME_UNIT_FOREVER_REL,
239 &transmit_notify_request, 230 GNUNET_YES,
240 nc); 231 &transmit_notify_request, nc);
241} 232}
242 233
243 234
@@ -253,24 +244,23 @@ request_notifications (struct GNUNET_PEERINFO_NotifyContext *nc)
253 */ 244 */
254struct GNUNET_PEERINFO_NotifyContext * 245struct GNUNET_PEERINFO_NotifyContext *
255GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg, 246GNUNET_PEERINFO_notify (const struct GNUNET_CONFIGURATION_Handle *cfg,
256 GNUNET_PEERINFO_Processor callback, 247 GNUNET_PEERINFO_Processor callback, void *callback_cls)
257 void *callback_cls)
258{ 248{
259 struct GNUNET_PEERINFO_NotifyContext *nc; 249 struct GNUNET_PEERINFO_NotifyContext *nc;
260 struct GNUNET_CLIENT_Connection *client; 250 struct GNUNET_CLIENT_Connection *client;
261 251
262 client = GNUNET_CLIENT_connect ("peerinfo", cfg); 252 client = GNUNET_CLIENT_connect ("peerinfo", cfg);
263 if (client == NULL) 253 if (client == NULL)
264 { 254 {
265 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, 255 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
266 _("Could not connect to `%s' service.\n"), "peerinfo"); 256 _("Could not connect to `%s' service.\n"), "peerinfo");
267 return NULL; 257 return NULL;
268 } 258 }
269 nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext)); 259 nc = GNUNET_malloc (sizeof (struct GNUNET_PEERINFO_NotifyContext));
270 nc->cfg = cfg; 260 nc->cfg = cfg;
271 nc->client = client; 261 nc->client = client;
272 nc->callback = callback; 262 nc->callback = callback;
273 nc->callback_cls = callback_cls; 263 nc->callback_cls = callback_cls;
274 request_notifications (nc); 264 request_notifications (nc);
275 return nc; 265 return nc;
276} 266}
@@ -285,10 +275,10 @@ void
285GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc) 275GNUNET_PEERINFO_notify_cancel (struct GNUNET_PEERINFO_NotifyContext *nc)
286{ 276{
287 if (NULL != nc->init) 277 if (NULL != nc->init)
288 { 278 {
289 GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init); 279 GNUNET_CLIENT_notify_transmit_ready_cancel (nc->init);
290 nc->init = NULL; 280 nc->init = NULL;
291 } 281 }
292 if (NULL != nc->client) 282 if (NULL != nc->client)
293 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO); 283 GNUNET_CLIENT_disconnect (nc->client, GNUNET_NO);
294 if (GNUNET_SCHEDULER_NO_TASK != nc->task) 284 if (GNUNET_SCHEDULER_NO_TASK != nc->task)