diff options
author | Christian Grothoff <christian@grothoff.org> | 2016-06-18 21:01:02 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2016-06-18 21:01:02 +0000 |
commit | a83a664ca22342a0ba23d6b76c2d33d7cb82a9ac (patch) | |
tree | 20fa8c8644db060902ac424c89123b7e68cfc1e2 /src/rps/rps_api.c | |
parent | 9687e96fd44fd4ccfc99836f41117c9b808481d2 (diff) | |
download | gnunet-a83a664ca22342a0ba23d6b76c2d33d7cb82a9ac.tar.gz gnunet-a83a664ca22342a0ba23d6b76c2d33d7cb82a9ac.zip |
fix RPS to match new MQ API -- and to check message format
Diffstat (limited to 'src/rps/rps_api.c')
-rw-r--r-- | src/rps/rps_api.c | 69 |
1 files changed, 51 insertions, 18 deletions
diff --git a/src/rps/rps_api.c b/src/rps/rps_api.c index 6971fb716..b055f6265 100644 --- a/src/rps/rps_api.c +++ b/src/rps/rps_api.c | |||
@@ -1,6 +1,6 @@ | |||
1 | /* | 1 | /* |
2 | This file is part of GNUnet. | 2 | This file is part of GNUnet. |
3 | Copyright (C) | 3 | Copyright (C) |
4 | 4 | ||
5 | GNUnet is free software; you can redistribute it and/or modify | 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 | 6 | it under the terms of the GNU General Public License as published |
@@ -114,28 +114,52 @@ struct cb_cls_pack | |||
114 | }; | 114 | }; |
115 | 115 | ||
116 | 116 | ||
117 | |||
118 | /** | ||
119 | * This function is called, when the service replies to our request. | ||
120 | * It verifies that @a msg is well-formed. | ||
121 | * | ||
122 | * @param cls the closure | ||
123 | * @param msg the message | ||
124 | * @return #GNUNET_OK if @a msg is well-formed | ||
125 | */ | ||
126 | static int | ||
127 | check_reply (void *cls, | ||
128 | const struct GNUNET_RPS_CS_ReplyMessage *msg) | ||
129 | { | ||
130 | uint16_t msize = ntohs (msg->header.size); | ||
131 | uint32_t num_peers = ntohl (msg->num_peers); | ||
132 | |||
133 | msize -= sizeof (struct GNUNET_RPS_CS_ReplyMessage); | ||
134 | if ( (msize / sizeof (struct GNUNET_PeerIdentity) != num_peers) || | ||
135 | (msize % sizeof (struct GNUNET_PeerIdentity) != 0) ) | ||
136 | { | ||
137 | GNUNET_break (0); | ||
138 | return GNUNET_SYSERR; | ||
139 | } | ||
140 | return GNUNET_OK; | ||
141 | } | ||
142 | |||
143 | |||
117 | /** | 144 | /** |
118 | * This function is called, when the service replies to our request. | 145 | * This function is called, when the service replies to our request. |
119 | * It calls the callback the caller gave us with the provided closure | 146 | * It calls the callback the caller gave us with the provided closure |
120 | * and disconnects afterwards. | 147 | * and disconnects afterwards. |
121 | * | 148 | * |
122 | * @param cls the closure | 149 | * @param cls the closure |
123 | * @param message the message | 150 | * @param msg the message |
124 | */ | 151 | */ |
125 | static void | 152 | static void |
126 | handle_reply (void *cls, | 153 | handle_reply (void *cls, |
127 | const struct GNUNET_MessageHeader *message) | 154 | const struct GNUNET_RPS_CS_ReplyMessage *msg) |
128 | { | 155 | { |
129 | struct GNUNET_RPS_Handle *h = (struct GNUNET_RPS_Handle *) cls; | 156 | struct GNUNET_RPS_Handle *h = cls; |
130 | struct GNUNET_RPS_CS_ReplyMessage *msg; | ||
131 | struct GNUNET_PeerIdentity *peers; | 157 | struct GNUNET_PeerIdentity *peers; |
132 | struct GNUNET_RPS_Request_Handle *rh; | 158 | struct GNUNET_RPS_Request_Handle *rh; |
133 | uint32_t id; | 159 | uint32_t id; |
134 | 160 | ||
135 | /* Give the peers back */ | 161 | /* Give the peers back */ |
136 | msg = (struct GNUNET_RPS_CS_ReplyMessage *) message; | ||
137 | id = ntohl (msg->id); | 162 | id = ntohl (msg->id); |
138 | |||
139 | LOG (GNUNET_ERROR_TYPE_DEBUG, | 163 | LOG (GNUNET_ERROR_TYPE_DEBUG, |
140 | "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n", | 164 | "Service replied with %" PRIu32 " peers for id %" PRIu32 "\n", |
141 | ntohl (msg->num_peers), | 165 | ntohl (msg->num_peers), |
@@ -147,9 +171,12 @@ handle_reply (void *cls, | |||
147 | rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id); | 171 | rh = GNUNET_CONTAINER_multihashmap32_get (h->req_handlers, id); |
148 | GNUNET_assert (NULL != rh); | 172 | GNUNET_assert (NULL != rh); |
149 | GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id); | 173 | GNUNET_CONTAINER_multihashmap32_remove_all (h->req_handlers, id); |
150 | rh->ready_cb((rh)->ready_cb_cls, ntohl (msg->num_peers), peers); | 174 | rh->ready_cb (rh->ready_cb_cls, |
175 | ntohl (msg->num_peers), | ||
176 | peers); | ||
151 | } | 177 | } |
152 | 178 | ||
179 | |||
153 | /** | 180 | /** |
154 | * Reconnect to the service | 181 | * Reconnect to the service |
155 | */ | 182 | */ |
@@ -166,8 +193,9 @@ reconnect (struct GNUNET_RPS_Handle *h); | |||
166 | * @param cls the closure | 193 | * @param cls the closure |
167 | * @param error error code without specyfied meaning | 194 | * @param error error code without specyfied meaning |
168 | */ | 195 | */ |
169 | static void | 196 | static void |
170 | mq_error_handler (void *cls, enum GNUNET_MQ_Error error) | 197 | mq_error_handler (void *cls, |
198 | enum GNUNET_MQ_Error error) | ||
171 | { | 199 | { |
172 | struct GNUNET_RPS_Handle *h = cls; | 200 | struct GNUNET_RPS_Handle *h = cls; |
173 | //TODO LOG | 201 | //TODO LOG |
@@ -179,15 +207,19 @@ mq_error_handler (void *cls, enum GNUNET_MQ_Error error) | |||
179 | reconnect (h); | 207 | reconnect (h); |
180 | } | 208 | } |
181 | 209 | ||
210 | |||
182 | /** | 211 | /** |
183 | * Reconnect to the service | 212 | * Reconnect to the service |
184 | */ | 213 | */ |
185 | static void | 214 | static void |
186 | reconnect (struct GNUNET_RPS_Handle *h) | 215 | reconnect (struct GNUNET_RPS_Handle *h) |
187 | { | 216 | { |
188 | static const struct GNUNET_MQ_MessageHandler mq_handlers[] = { | 217 | GNUNET_MQ_hd_var_size (reply, |
189 | {&handle_reply, GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, 0}, | 218 | GNUNET_MESSAGE_TYPE_RPS_CS_REPLY, |
190 | GNUNET_MQ_HANDLERS_END | 219 | struct GNUNET_RPS_CS_ReplyMessage); |
220 | struct GNUNET_MQ_MessageHandler mq_handlers[] = { | ||
221 | make_reply_handler (h), | ||
222 | GNUNET_MQ_handler_end () | ||
191 | }; | 223 | }; |
192 | 224 | ||
193 | if (NULL != h->mq) | 225 | if (NULL != h->mq) |
@@ -196,12 +228,13 @@ reconnect (struct GNUNET_RPS_Handle *h) | |||
196 | GNUNET_CLIENT_disconnect (h->conn); | 228 | GNUNET_CLIENT_disconnect (h->conn); |
197 | h->conn = GNUNET_CLIENT_connect ("rps", h->cfg); | 229 | h->conn = GNUNET_CLIENT_connect ("rps", h->cfg); |
198 | GNUNET_assert (NULL != h->conn); | 230 | GNUNET_assert (NULL != h->conn); |
199 | h->mq = GNUNET_MQ_queue_for_connection_client(h->conn, | 231 | h->mq = GNUNET_MQ_queue_for_connection_client (h->conn, |
200 | mq_handlers, | 232 | mq_handlers, |
201 | mq_error_handler, // TODO implement | 233 | &mq_error_handler, |
202 | h); | 234 | h); |
203 | } | 235 | } |
204 | 236 | ||
237 | |||
205 | /** | 238 | /** |
206 | * Connect to the rps service | 239 | * Connect to the rps service |
207 | * | 240 | * |