diff options
author | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-08-16 18:48:04 +0200 |
---|---|---|
committer | Schanzenbach, Martin <martin.schanzenbach@aisec.fraunhofer.de> | 2018-08-16 18:48:04 +0200 |
commit | 1940b899b3620786b0265144ab756a3f05bcc8b0 (patch) | |
tree | 92e16378a5b6f6211bf937597b429b6c092b04bb /src/zklaim | |
parent | 5ef8d3c6cbe7970bc9c9e54b4ab23c8727200b15 (diff) | |
download | gnunet-1940b899b3620786b0265144ab756a3f05bcc8b0.tar.gz gnunet-1940b899b3620786b0265144ab756a3f05bcc8b0.zip |
start zklaim service
Diffstat (limited to 'src/zklaim')
-rw-r--r-- | src/zklaim/Makefile.am | 47 | ||||
-rw-r--r-- | src/zklaim/plugin_reclaim_attribute_zklaim.c | 181 | ||||
-rw-r--r-- | src/zklaim/zklaim_api.c | 433 | ||||
-rw-r--r-- | src/zklaim/zklaim_api.h | 90 |
4 files changed, 749 insertions, 2 deletions
diff --git a/src/zklaim/Makefile.am b/src/zklaim/Makefile.am index a3432794a..1b25ac1a3 100644 --- a/src/zklaim/Makefile.am +++ b/src/zklaim/Makefile.am | |||
@@ -12,9 +12,51 @@ if USE_COVERAGE | |||
12 | XLIB = -lgcov | 12 | XLIB = -lgcov |
13 | endif | 13 | endif |
14 | 14 | ||
15 | plugin_LTLIBRARIES = \ | ||
16 | libgnunet_plugin_reclaim_attribute_zklaim.la | ||
15 | 17 | ||
16 | gnunet_zklaim_SOURCES = zklaim.c | 18 | lib_LTLIBRARIES = \ |
19 | libgnunetzklaim.la | ||
20 | |||
21 | libgnunetzklaim_la_SOURCES = \ | ||
22 | zklaim_api.c \ | ||
23 | zklaim_api.h | ||
24 | libgnunetzklaim_la_LIBADD = \ | ||
25 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
26 | $(GN_LIBINTL) $(XLIB) | ||
27 | libgnunetzklaim_la_LDFLAGS = \ | ||
28 | $(GN_LIB_LDFLAGS) $(WINFLAGS) \ | ||
29 | -version-info 0:0:0 | ||
30 | |||
31 | |||
32 | libgnunet_plugin_reclaim_attribute_zklaim_la_SOURCES = \ | ||
33 | plugin_reclaim_attribute_zklaim.c | ||
34 | libgnunet_plugin_reclaim_attribute_zklaim_la_LIBADD = \ | ||
35 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
36 | $(LTLIBINTL) | ||
37 | libgnunet_plugin_reclaim_attribute_zklaim_la_LDFLAGS = \ | ||
38 | $(GN_PLUGIN_LDFLAGS) | ||
39 | |||
40 | test_zklaim_SOURCES = zklaim.c | ||
41 | test_zklaim_LDADD = \ | ||
42 | $(GCLIBADD)\ | ||
43 | $(LIBGCRYPT_LIBS) \ | ||
44 | -lzklaim \ | ||
45 | -lgcrypt \ | ||
46 | -lgmp \ | ||
47 | -lgmpxx \ | ||
48 | -lcrypto \ | ||
49 | -lprocps \ | ||
50 | -lstdc++ \ | ||
51 | -lm | ||
52 | test_zklaim_LDFLAGS = \ | ||
53 | $(GN_LIB_LDFLAGS) | ||
54 | |||
55 | gnunet_zklaim_SOURCES = gnunet-zklaim.c | ||
17 | gnunet_zklaim_LDADD = \ | 56 | gnunet_zklaim_LDADD = \ |
57 | $(top_builddir)/src/util/libgnunetutil.la \ | ||
58 | $(top_builddir)/src/identity/libgnunetidentity.la \ | ||
59 | libgnunetzklaim.la \ | ||
18 | $(GCLIBADD)\ | 60 | $(GCLIBADD)\ |
19 | $(LIBGCRYPT_LIBS) \ | 61 | $(LIBGCRYPT_LIBS) \ |
20 | -lzklaim \ | 62 | -lzklaim \ |
@@ -28,8 +70,9 @@ gnunet_zklaim_LDADD = \ | |||
28 | gnunet_zklaim_LDFLAGS = \ | 70 | gnunet_zklaim_LDFLAGS = \ |
29 | $(GN_LIB_LDFLAGS) | 71 | $(GN_LIB_LDFLAGS) |
30 | 72 | ||
31 | #lib_LTLIBRARIES = libgnunetzklaim.la | ||
32 | bin_PROGRAMS = gnunet-zklaim | 73 | bin_PROGRAMS = gnunet-zklaim |
74 | #lib_LTLIBRARIES = libgnunetzklaim.la | ||
75 | check_PROGRAMS = test_zklaim | ||
33 | 76 | ||
34 | if ENABLE_TEST_RUN | 77 | if ENABLE_TEST_RUN |
35 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; | 78 | AM_TESTS_ENVIRONMENT=export GNUNET_PREFIX=$${GNUNET_PREFIX:-@libdir@};export PATH=$${GNUNET_PREFIX:-@prefix@}/bin:$$PATH;unset XDG_DATA_HOME;unset XDG_CONFIG_HOME; |
diff --git a/src/zklaim/plugin_reclaim_attribute_zklaim.c b/src/zklaim/plugin_reclaim_attribute_zklaim.c new file mode 100644 index 000000000..a79576cb1 --- /dev/null +++ b/src/zklaim/plugin_reclaim_attribute_zklaim.c | |||
@@ -0,0 +1,181 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet | ||
3 | Copyright (C) 2013, 2014, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file zklaim/plugin_reclaim_attribute_zklaim.c | ||
21 | * @brief identity attribute plugin to provide the API for ZKlaims | ||
22 | * | ||
23 | * @author Martin Schanzenbach | ||
24 | */ | ||
25 | #include "platform.h" | ||
26 | #include "gnunet_util_lib.h" | ||
27 | #include "gnunet_reclaim_attribute_plugin.h" | ||
28 | #include <inttypes.h> | ||
29 | |||
30 | |||
31 | /** | ||
32 | * Convert the 'value' of an attribute to a string. | ||
33 | * | ||
34 | * @param cls closure, unused | ||
35 | * @param type type of the attribute | ||
36 | * @param data value in binary encoding | ||
37 | * @param data_size number of bytes in @a data | ||
38 | * @return NULL on error, otherwise human-readable representation of the value | ||
39 | */ | ||
40 | static char * | ||
41 | zklaim_value_to_string (void *cls, | ||
42 | uint32_t type, | ||
43 | const void *data, | ||
44 | size_t data_size) | ||
45 | { | ||
46 | |||
47 | switch (type) | ||
48 | { | ||
49 | case GNUNET_RECLAIM_ATTRIBUTE_TYPE_ZKLAIM: | ||
50 | return GNUNET_strndup (data, data_size); | ||
51 | default: | ||
52 | return NULL; | ||
53 | } | ||
54 | } | ||
55 | |||
56 | |||
57 | /** | ||
58 | * Convert human-readable version of a 'value' of an attribute to the binary | ||
59 | * representation. | ||
60 | * | ||
61 | * @param cls closure, unused | ||
62 | * @param type type of the attribute | ||
63 | * @param s human-readable string | ||
64 | * @param data set to value in binary encoding (will be allocated) | ||
65 | * @param data_size set to number of bytes in @a data | ||
66 | * @return #GNUNET_OK on success | ||
67 | */ | ||
68 | static int | ||
69 | zklaim_string_to_value (void *cls, | ||
70 | uint32_t type, | ||
71 | const char *s, | ||
72 | void **data, | ||
73 | size_t *data_size) | ||
74 | { | ||
75 | if (NULL == s) | ||
76 | return GNUNET_SYSERR; | ||
77 | switch (type) | ||
78 | { | ||
79 | |||
80 | case GNUNET_RECLAIM_ATTRIBUTE_TYPE_ZKLAIM: | ||
81 | *data = GNUNET_strdup (s); | ||
82 | *data_size = strlen (s); | ||
83 | return GNUNET_OK; | ||
84 | default: | ||
85 | return GNUNET_SYSERR; | ||
86 | } | ||
87 | } | ||
88 | |||
89 | |||
90 | /** | ||
91 | * Mapping of attribute type numbers to human-readable | ||
92 | * attribute type names. | ||
93 | */ | ||
94 | static struct { | ||
95 | const char *name; | ||
96 | uint32_t number; | ||
97 | } zklaim_name_map[] = { | ||
98 | { "ZKLAIM", GNUNET_RECLAIM_ATTRIBUTE_TYPE_ZKLAIM }, | ||
99 | { NULL, UINT32_MAX } | ||
100 | }; | ||
101 | |||
102 | |||
103 | /** | ||
104 | * Convert a type name to the corresponding number. | ||
105 | * | ||
106 | * @param cls closure, unused | ||
107 | * @param zklaim_typename name to convert | ||
108 | * @return corresponding number, UINT32_MAX on error | ||
109 | */ | ||
110 | static uint32_t | ||
111 | zklaim_typename_to_number (void *cls, | ||
112 | const char *zklaim_typename) | ||
113 | { | ||
114 | unsigned int i; | ||
115 | |||
116 | i=0; | ||
117 | while ( (NULL != zklaim_name_map[i].name) && | ||
118 | (0 != strcasecmp (zklaim_typename, | ||
119 | zklaim_name_map[i].name)) ) | ||
120 | i++; | ||
121 | return zklaim_name_map[i].number; | ||
122 | } | ||
123 | |||
124 | |||
125 | /** | ||
126 | * Convert a type number (i.e. 1) to the corresponding type string | ||
127 | * | ||
128 | * @param cls closure, unused | ||
129 | * @param type number of a type to convert | ||
130 | * @return corresponding typestring, NULL on error | ||
131 | */ | ||
132 | static const char * | ||
133 | zklaim_number_to_typename (void *cls, | ||
134 | uint32_t type) | ||
135 | { | ||
136 | unsigned int i; | ||
137 | |||
138 | i=0; | ||
139 | while ( (NULL != zklaim_name_map[i].name) && | ||
140 | (type != zklaim_name_map[i].number) ) | ||
141 | i++; | ||
142 | return zklaim_name_map[i].name; | ||
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Entry point for the plugin. | ||
148 | * | ||
149 | * @param cls NULL | ||
150 | * @return the exported block API | ||
151 | */ | ||
152 | void * | ||
153 | libgnunet_plugin_reclaim_attribute_zklaim_init (void *cls) | ||
154 | { | ||
155 | struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api; | ||
156 | |||
157 | api = GNUNET_new (struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions); | ||
158 | api->value_to_string = &zklaim_value_to_string; | ||
159 | api->string_to_value = &zklaim_string_to_value; | ||
160 | api->typename_to_number = &zklaim_typename_to_number; | ||
161 | api->number_to_typename = &zklaim_number_to_typename; | ||
162 | return api; | ||
163 | } | ||
164 | |||
165 | |||
166 | /** | ||
167 | * Exit point from the plugin. | ||
168 | * | ||
169 | * @param cls the return value from #libgnunet_plugin_block_test_init() | ||
170 | * @return NULL | ||
171 | */ | ||
172 | void * | ||
173 | libgnunet_plugin_reclaim_attribute_zklaim_done (void *cls) | ||
174 | { | ||
175 | struct GNUNET_RECLAIM_ATTRIBUTE_PluginFunctions *api = cls; | ||
176 | |||
177 | GNUNET_free (api); | ||
178 | return NULL; | ||
179 | } | ||
180 | |||
181 | /* end of plugin_reclaim_attribute_type_zklaim.c */ | ||
diff --git a/src/zklaim/zklaim_api.c b/src/zklaim/zklaim_api.c new file mode 100644 index 000000000..f54ede342 --- /dev/null +++ b/src/zklaim/zklaim_api.c | |||
@@ -0,0 +1,433 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013, 2016 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @file zklaim/zklaim_api.c | ||
21 | * @brief api to interact with the zklaim service | ||
22 | * @author Martin Schanzenbach | ||
23 | */ | ||
24 | #include "platform.h" | ||
25 | #include "gnunet_util_lib.h" | ||
26 | #include "gnunet_constants.h" | ||
27 | #include "gnunet_protocols.h" | ||
28 | #include "gnunet_zklaim_service.h" | ||
29 | #include "zklaim/zklaim.h" | ||
30 | #include "zklaim_api.h" | ||
31 | |||
32 | #define LOG(kind,...) GNUNET_log_from (kind, "zklaim-api",__VA_ARGS__) | ||
33 | |||
34 | /** | ||
35 | * Handle for an ego. | ||
36 | */ | ||
37 | struct GNUNET_ZKLAIM_Context | ||
38 | { | ||
39 | /** | ||
40 | * ZKlaim context. | ||
41 | */ | ||
42 | struct zklaim_ctx *zk_ctx; | ||
43 | |||
44 | /** | ||
45 | * Current name associated with this context. | ||
46 | */ | ||
47 | char *name; | ||
48 | |||
49 | /** | ||
50 | * Attributes associated with context | ||
51 | */ | ||
52 | char *attrs; | ||
53 | |||
54 | /** | ||
55 | * Client context associated with this ego. | ||
56 | */ | ||
57 | void *ctx; | ||
58 | |||
59 | }; | ||
60 | |||
61 | |||
62 | /** | ||
63 | * Handle for an operation with the service. | ||
64 | */ | ||
65 | struct GNUNET_ZKLAIM_Operation | ||
66 | { | ||
67 | |||
68 | /** | ||
69 | * Main handle. | ||
70 | */ | ||
71 | struct GNUNET_ZKLAIM_Handle *h; | ||
72 | |||
73 | /** | ||
74 | * We keep operations in a DLL. | ||
75 | */ | ||
76 | struct GNUNET_ZKLAIM_Operation *next; | ||
77 | |||
78 | /** | ||
79 | * We keep operations in a DLL. | ||
80 | */ | ||
81 | struct GNUNET_ZKLAIM_Operation *prev; | ||
82 | |||
83 | /** | ||
84 | * Message to send to the zklaim service. | ||
85 | * Allocated at the end of this struct. | ||
86 | */ | ||
87 | const struct GNUNET_MessageHeader *msg; | ||
88 | |||
89 | /** | ||
90 | * Continuation to invoke with the result of the transmission; @e cb | ||
91 | * will be NULL in this case. | ||
92 | */ | ||
93 | GNUNET_ZKLAIM_ContinuationWithStatus cont; | ||
94 | |||
95 | /** | ||
96 | * Closure for @e cont or @e cb. | ||
97 | */ | ||
98 | void *cls; | ||
99 | |||
100 | }; | ||
101 | |||
102 | |||
103 | /** | ||
104 | * Handle for the service. | ||
105 | */ | ||
106 | struct GNUNET_ZKLAIM_Handle | ||
107 | { | ||
108 | /** | ||
109 | * Configuration to use. | ||
110 | */ | ||
111 | const struct GNUNET_CONFIGURATION_Handle *cfg; | ||
112 | |||
113 | /** | ||
114 | * Connection to service. | ||
115 | */ | ||
116 | struct GNUNET_MQ_Handle *mq; | ||
117 | |||
118 | /** | ||
119 | * Hash map from the hash of the public key to the | ||
120 | * respective `GNUNET_ZKLAIM_Ego` handle. | ||
121 | */ | ||
122 | struct GNUNET_CONTAINER_MultiHashMap *egos; | ||
123 | |||
124 | /** | ||
125 | * Closure for @e cb. | ||
126 | */ | ||
127 | void *cb_cls; | ||
128 | |||
129 | /** | ||
130 | * Head of active operations. | ||
131 | */ | ||
132 | struct GNUNET_ZKLAIM_Operation *op_head; | ||
133 | |||
134 | /** | ||
135 | * Tail of active operations. | ||
136 | */ | ||
137 | struct GNUNET_ZKLAIM_Operation *op_tail; | ||
138 | |||
139 | /** | ||
140 | * Task doing exponential back-off trying to reconnect. | ||
141 | */ | ||
142 | struct GNUNET_SCHEDULER_Task *reconnect_task; | ||
143 | |||
144 | /** | ||
145 | * Time for next connect retry. | ||
146 | */ | ||
147 | struct GNUNET_TIME_Relative reconnect_delay; | ||
148 | |||
149 | }; | ||
150 | |||
151 | |||
152 | /** | ||
153 | * Try again to connect to the zklaim service. | ||
154 | * | ||
155 | * @param cls handle to the zklaim service. | ||
156 | */ | ||
157 | static void | ||
158 | reconnect (void *cls); | ||
159 | |||
160 | /** | ||
161 | * Reschedule a connect attempt to the service. | ||
162 | * | ||
163 | * @param h transport service to reconnect | ||
164 | */ | ||
165 | static void | ||
166 | reschedule_connect (struct GNUNET_ZKLAIM_Handle *h) | ||
167 | { | ||
168 | struct GNUNET_ZKLAIM_Operation *op; | ||
169 | |||
170 | GNUNET_assert (NULL == h->reconnect_task); | ||
171 | |||
172 | if (NULL != h->mq) | ||
173 | { | ||
174 | GNUNET_MQ_destroy (h->mq); | ||
175 | h->mq = NULL; | ||
176 | } | ||
177 | while (NULL != (op = h->op_head)) | ||
178 | { | ||
179 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
180 | h->op_tail, | ||
181 | op); | ||
182 | if (NULL != op->cont) | ||
183 | op->cont (op->cls, | ||
184 | GNUNET_SYSERR, | ||
185 | "Error in communication with the zklaim service"); | ||
186 | GNUNET_free (op); | ||
187 | } | ||
188 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
189 | "Scheduling task to reconnect to zklaim service in %s.\n", | ||
190 | GNUNET_STRINGS_relative_time_to_string (h->reconnect_delay, | ||
191 | GNUNET_YES)); | ||
192 | h->reconnect_task = | ||
193 | GNUNET_SCHEDULER_add_delayed (h->reconnect_delay, | ||
194 | &reconnect, | ||
195 | h); | ||
196 | h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay); | ||
197 | } | ||
198 | |||
199 | |||
200 | /** | ||
201 | * Generic error handler, called with the appropriate error code and | ||
202 | * the same closure specified at the creation of the message queue. | ||
203 | * Not every message queue implementation supports an error handler. | ||
204 | * | ||
205 | * @param cls closure with the `struct GNUNET_ZKLAIM_Handle *` | ||
206 | * @param error error code | ||
207 | */ | ||
208 | static void | ||
209 | mq_error_handler (void *cls, | ||
210 | enum GNUNET_MQ_Error error) | ||
211 | { | ||
212 | struct GNUNET_ZKLAIM_Handle *h = cls; | ||
213 | |||
214 | reschedule_connect (h); | ||
215 | } | ||
216 | |||
217 | |||
218 | /** | ||
219 | * We received a result code from the service. Check the message | ||
220 | * is well-formed. | ||
221 | * | ||
222 | * @param cls closure | ||
223 | * @param rcm result message received | ||
224 | * @return #GNUNET_OK if the message is well-formed | ||
225 | */ | ||
226 | static int | ||
227 | check_zklaim_result_code (void *cls, | ||
228 | const struct ResultCodeMessage *rcm) | ||
229 | { | ||
230 | uint16_t size = ntohs (rcm->header.size) - sizeof (*rcm); | ||
231 | const char *str = (const char *) &rcm[1]; | ||
232 | |||
233 | if (0 == size) | ||
234 | return GNUNET_OK; | ||
235 | if ('\0' != str[size - 1]) | ||
236 | { | ||
237 | GNUNET_break (0); | ||
238 | return GNUNET_SYSERR; | ||
239 | } | ||
240 | return GNUNET_OK; | ||
241 | } | ||
242 | |||
243 | |||
244 | /** | ||
245 | * We received a result code from the service. | ||
246 | * | ||
247 | * @param cls closure | ||
248 | * @param rcm result message received | ||
249 | */ | ||
250 | static void | ||
251 | handle_zklaim_result_code (void *cls, | ||
252 | const struct ResultCodeMessage *rcm) | ||
253 | { | ||
254 | struct GNUNET_ZKLAIM_Handle *h = cls; | ||
255 | struct GNUNET_ZKLAIM_Operation *op; | ||
256 | uint16_t size = ntohs (rcm->header.size) - sizeof (*rcm); | ||
257 | const char *str = (0 == size) ? NULL : (const char *) &rcm[1]; | ||
258 | |||
259 | op = h->op_head; | ||
260 | if (NULL == op) | ||
261 | { | ||
262 | GNUNET_break (0); | ||
263 | reschedule_connect (h); | ||
264 | return; | ||
265 | } | ||
266 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
267 | h->op_tail, | ||
268 | op); | ||
269 | if (NULL != op->cont) | ||
270 | op->cont (op->cls, | ||
271 | GNUNET_OK, | ||
272 | str); | ||
273 | GNUNET_free (op); | ||
274 | } | ||
275 | |||
276 | |||
277 | /** | ||
278 | * Try again to connect to the zklaim service. | ||
279 | * | ||
280 | * @param cls handle to the zklaim service. | ||
281 | */ | ||
282 | static void | ||
283 | reconnect (void *cls) | ||
284 | { | ||
285 | struct GNUNET_ZKLAIM_Handle *h = cls; | ||
286 | struct GNUNET_MQ_MessageHandler handlers[] = { | ||
287 | GNUNET_MQ_hd_var_size (zklaim_result_code, | ||
288 | GNUNET_MESSAGE_TYPE_ZKLAIM_RESULT_CODE, | ||
289 | struct ResultCodeMessage, | ||
290 | h), | ||
291 | GNUNET_MQ_handler_end () | ||
292 | }; | ||
293 | |||
294 | h->reconnect_task = NULL; | ||
295 | LOG (GNUNET_ERROR_TYPE_DEBUG, | ||
296 | "Connecting to zklaim service.\n"); | ||
297 | GNUNET_assert (NULL == h->mq); | ||
298 | h->mq = GNUNET_CLIENT_connect (h->cfg, | ||
299 | "zklaim", | ||
300 | handlers, | ||
301 | &mq_error_handler, | ||
302 | h); | ||
303 | if (NULL == h->mq) | ||
304 | return; | ||
305 | } | ||
306 | |||
307 | |||
308 | /** | ||
309 | * Connect to the zklaim service. | ||
310 | * | ||
311 | * @param cfg the configuration to use | ||
312 | * @param cb function to call on all zklaim events, can be NULL | ||
313 | * @param cb_cls closure for @a cb | ||
314 | * @return handle to use | ||
315 | */ | ||
316 | struct GNUNET_ZKLAIM_Handle * | ||
317 | GNUNET_ZKLAIM_connect (const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
318 | { | ||
319 | struct GNUNET_ZKLAIM_Handle *h; | ||
320 | |||
321 | h = GNUNET_new (struct GNUNET_ZKLAIM_Handle); | ||
322 | h->cfg = cfg; | ||
323 | reconnect (h); | ||
324 | if (NULL == h->mq) | ||
325 | { | ||
326 | GNUNET_free (h); | ||
327 | return NULL; | ||
328 | } | ||
329 | return h; | ||
330 | } | ||
331 | |||
332 | |||
333 | /** | ||
334 | * Create a new zklaim with the given name. | ||
335 | * | ||
336 | * @param h zklaim service to use | ||
337 | * @param name desired name | ||
338 | * @param cont function to call with the result (will only be called once) | ||
339 | * @param cont_cls closure for @a cont | ||
340 | * @return handle to abort the operation | ||
341 | */ | ||
342 | struct GNUNET_ZKLAIM_Operation * | ||
343 | GNUNET_ZKLAIM_context_create (struct GNUNET_ZKLAIM_Handle *h, | ||
344 | const struct GNUNET_CRYPTO_EcdsaPrivateKey *pk, | ||
345 | const char *name, | ||
346 | const char *attr_list, | ||
347 | GNUNET_ZKLAIM_ContinuationWithStatus cont, | ||
348 | void *cont_cls) | ||
349 | { | ||
350 | struct GNUNET_ZKLAIM_Operation *op; | ||
351 | struct GNUNET_MQ_Envelope *env; | ||
352 | struct CreateRequestMessage *crm; | ||
353 | size_t slen; | ||
354 | |||
355 | if (NULL == h->mq) | ||
356 | return NULL; | ||
357 | slen = strlen (name) + 1; | ||
358 | if (slen >= GNUNET_MAX_MESSAGE_SIZE - sizeof (struct CreateRequestMessage)) | ||
359 | { | ||
360 | GNUNET_break (0); | ||
361 | return NULL; | ||
362 | } | ||
363 | op = GNUNET_new (struct GNUNET_ZKLAIM_Operation); | ||
364 | op->h = h; | ||
365 | op->cont = cont; | ||
366 | op->cls = cont_cls; | ||
367 | GNUNET_CONTAINER_DLL_insert_tail (h->op_head, | ||
368 | h->op_tail, | ||
369 | op); | ||
370 | env = GNUNET_MQ_msg_extra (crm, | ||
371 | slen, | ||
372 | GNUNET_MESSAGE_TYPE_ZKLAIM_CREATE); | ||
373 | crm->name_len = htons (slen); | ||
374 | crm->reserved = htons (0); | ||
375 | crm->private_key = *pk; | ||
376 | GNUNET_memcpy (&crm[1], | ||
377 | name, | ||
378 | slen); | ||
379 | GNUNET_MQ_send (h->mq, | ||
380 | env); | ||
381 | //TODO add attrs | ||
382 | return op; | ||
383 | } | ||
384 | |||
385 | |||
386 | /** | ||
387 | * Cancel an zklaim operation. Note that the operation MAY still | ||
388 | * be executed; this merely cancels the continuation; if the request | ||
389 | * was already transmitted, the service may still choose to complete | ||
390 | * the operation. | ||
391 | * | ||
392 | * @param op operation to cancel | ||
393 | */ | ||
394 | void | ||
395 | GNUNET_ZKLAIM_cancel (struct GNUNET_ZKLAIM_Operation *op) | ||
396 | { | ||
397 | op->cont = NULL; | ||
398 | } | ||
399 | |||
400 | |||
401 | /** | ||
402 | * Disconnect from zklaim service | ||
403 | * | ||
404 | * @param h handle to destroy | ||
405 | */ | ||
406 | void | ||
407 | GNUNET_ZKLAIM_disconnect (struct GNUNET_ZKLAIM_Handle *h) | ||
408 | { | ||
409 | struct GNUNET_ZKLAIM_Operation *op; | ||
410 | |||
411 | GNUNET_assert (NULL != h); | ||
412 | if (h->reconnect_task != NULL) | ||
413 | { | ||
414 | GNUNET_SCHEDULER_cancel (h->reconnect_task); | ||
415 | h->reconnect_task = NULL; | ||
416 | } | ||
417 | while (NULL != (op = h->op_head)) | ||
418 | { | ||
419 | GNUNET_break (NULL == op->cont); | ||
420 | GNUNET_CONTAINER_DLL_remove (h->op_head, | ||
421 | h->op_tail, | ||
422 | op); | ||
423 | GNUNET_free (op); | ||
424 | } | ||
425 | if (NULL != h->mq) | ||
426 | { | ||
427 | GNUNET_MQ_destroy (h->mq); | ||
428 | h->mq = NULL; | ||
429 | } | ||
430 | GNUNET_free (h); | ||
431 | } | ||
432 | |||
433 | /* end of zklaim_api.c */ | ||
diff --git a/src/zklaim/zklaim_api.h b/src/zklaim/zklaim_api.h new file mode 100644 index 000000000..77053c667 --- /dev/null +++ b/src/zklaim/zklaim_api.h | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2013 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
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 | Affero General Public License for more details. | ||
14 | |||
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/>. | ||
17 | */ | ||
18 | |||
19 | /** | ||
20 | * @author Martin Schanzenbach | ||
21 | * @file zklaim/zklaim.h | ||
22 | * | ||
23 | * @brief Common type definitions for the zklaim | ||
24 | * service and API. | ||
25 | */ | ||
26 | #ifndef ZKLAIM_API_H | ||
27 | #define ZKLAIM_API_H | ||
28 | |||
29 | #include "gnunet_common.h" | ||
30 | |||
31 | |||
32 | GNUNET_NETWORK_STRUCT_BEGIN | ||
33 | |||
34 | |||
35 | /** | ||
36 | * Answer from service to client about last operation; | ||
37 | * GET_DEFAULT maybe answered with this message on failure; | ||
38 | * CREATE and RENAME will always be answered with this message. | ||
39 | */ | ||
40 | struct ResultCodeMessage | ||
41 | { | ||
42 | /** | ||
43 | * Type: #GNUNET_MESSAGE_TYPE_ZKLAIM_RESULT_CODE | ||
44 | */ | ||
45 | struct GNUNET_MessageHeader header; | ||
46 | |||
47 | /** | ||
48 | * Status code for the last operation, in NBO. | ||
49 | * (currently not used). | ||
50 | */ | ||
51 | uint32_t result_code GNUNET_PACKED; | ||
52 | |||
53 | /* followed by 0-terminated error message (on error) */ | ||
54 | |||
55 | }; | ||
56 | |||
57 | |||
58 | /** | ||
59 | * Client requests creation of an identity. Service | ||
60 | * will respond with a result code. | ||
61 | */ | ||
62 | struct CreateRequestMessage | ||
63 | { | ||
64 | /** | ||
65 | * Type: #GNUNET_MESSAGE_TYPE_ZKLAIM_CREATE | ||
66 | */ | ||
67 | struct GNUNET_MessageHeader header; | ||
68 | |||
69 | /** | ||
70 | * Number of bytes in identity name string including 0-termination, in NBO. | ||
71 | */ | ||
72 | uint16_t name_len GNUNET_PACKED; | ||
73 | |||
74 | /** | ||
75 | * Always zero. | ||
76 | */ | ||
77 | uint16_t reserved GNUNET_PACKED; | ||
78 | |||
79 | /** | ||
80 | * The private key | ||
81 | */ | ||
82 | struct GNUNET_CRYPTO_EcdsaPrivateKey private_key; | ||
83 | |||
84 | /* followed by 0-terminated identity name */ | ||
85 | |||
86 | }; | ||
87 | |||
88 | GNUNET_NETWORK_STRUCT_END | ||
89 | |||
90 | #endif | ||