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