aboutsummaryrefslogtreecommitdiff
path: root/src/namecache
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
committerChristian Grothoff <christian@grothoff.org>2019-10-05 15:09:28 +0200
commitc4e9ba925ffd758aaa3feee2ccfc0b76f26fe207 (patch)
treecac3ce030d77b4cbe7c7dc62ed58cfe6d24f73e1 /src/namecache
parentfbb71d527c7d6babf269a8fefce1db291b9f7068 (diff)
downloadgnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.tar.gz
gnunet-c4e9ba925ffd758aaa3feee2ccfc0b76f26fe207.zip
global reindent, now with uncrustify hook enabled
Diffstat (limited to 'src/namecache')
-rw-r--r--src/namecache/gnunet-namecache.c182
-rw-r--r--src/namecache/gnunet-service-namecache.c319
-rw-r--r--src/namecache/namecache.h15
-rw-r--r--src/namecache/namecache_api.c353
-rw-r--r--src/namecache/plugin_namecache_flat.c414
-rw-r--r--src/namecache/plugin_namecache_postgres.c291
-rw-r--r--src/namecache/plugin_namecache_sqlite.c629
-rw-r--r--src/namecache/test_namecache_api_cache_block.c241
-rw-r--r--src/namecache/test_plugin_namecache.c76
9 files changed, 1279 insertions, 1241 deletions
diff --git a/src/namecache/gnunet-namecache.c b/src/namecache/gnunet-namecache.c
index 2d387b543..20729e125 100644
--- a/src/namecache/gnunet-namecache.c
+++ b/src/namecache/gnunet-namecache.c
@@ -70,18 +70,18 @@ static int ret;
70 * @param cls unused 70 * @param cls unused
71 */ 71 */
72static void 72static void
73do_shutdown(void *cls) 73do_shutdown (void *cls)
74{ 74{
75 if (NULL != qe) 75 if (NULL != qe)
76 { 76 {
77 GNUNET_NAMECACHE_cancel(qe); 77 GNUNET_NAMECACHE_cancel (qe);
78 qe = NULL; 78 qe = NULL;
79 } 79 }
80 if (NULL != ns) 80 if (NULL != ns)
81 { 81 {
82 GNUNET_NAMECACHE_disconnect(ns); 82 GNUNET_NAMECACHE_disconnect (ns);
83 ns = NULL; 83 ns = NULL;
84 } 84 }
85} 85}
86 86
87 87
@@ -93,37 +93,37 @@ do_shutdown(void *cls)
93 * @param rd array of records with data to store 93 * @param rd array of records with data to store
94 */ 94 */
95static void 95static void
96display_records_from_block(void *cls, 96display_records_from_block (void *cls,
97 unsigned int rd_len, 97 unsigned int rd_len,
98 const struct GNUNET_GNSRECORD_Data *rd) 98 const struct GNUNET_GNSRECORD_Data *rd)
99{ 99{
100 const char *typestring; 100 const char *typestring;
101 char *s; 101 char *s;
102 unsigned int i; 102 unsigned int i;
103 103
104 if (0 == rd_len) 104 if (0 == rd_len)
105 { 105 {
106 fprintf(stdout, _("No records found for `%s'"), name); 106 fprintf (stdout, _ ("No records found for `%s'"), name);
107 return; 107 return;
108 } 108 }
109 fprintf(stdout, "%s:\n", name); 109 fprintf (stdout, "%s:\n", name);
110 for (i = 0; i < rd_len; i++) 110 for (i = 0; i < rd_len; i++)
111 {
112 typestring = GNUNET_GNSRECORD_number_to_typename (rd[i].record_type);
113 s = GNUNET_GNSRECORD_value_to_string (rd[i].record_type,
114 rd[i].data,
115 rd[i].data_size);
116 if (NULL == s)
111 { 117 {
112 typestring = GNUNET_GNSRECORD_number_to_typename(rd[i].record_type); 118 fprintf (stdout,
113 s = GNUNET_GNSRECORD_value_to_string(rd[i].record_type, 119 _ ("\tCorrupt or unsupported record of type %u\n"),
114 rd[i].data, 120 (unsigned int) rd[i].record_type);
115 rd[i].data_size); 121 continue;
116 if (NULL == s)
117 {
118 fprintf(stdout,
119 _("\tCorrupt or unsupported record of type %u\n"),
120 (unsigned int)rd[i].record_type);
121 continue;
122 }
123 fprintf(stdout, "\t%s: %s\n", typestring, s);
124 GNUNET_free(s);
125 } 122 }
126 fprintf(stdout, "%s", "\n"); 123 fprintf (stdout, "\t%s: %s\n", typestring, s);
124 GNUNET_free (s);
125 }
126 fprintf (stdout, "%s", "\n");
127} 127}
128 128
129 129
@@ -134,23 +134,23 @@ display_records_from_block(void *cls,
134 * @param block NULL if not found 134 * @param block NULL if not found
135 */ 135 */
136static void 136static void
137handle_block(void *cls, const struct GNUNET_GNSRECORD_Block *block) 137handle_block (void *cls, const struct GNUNET_GNSRECORD_Block *block)
138{ 138{
139 qe = NULL; 139 qe = NULL;
140 if (NULL == block) 140 if (NULL == block)
141 { 141 {
142 fprintf(stderr, "No matching block found\n"); 142 fprintf (stderr, "No matching block found\n");
143 } 143 }
144 else if (GNUNET_OK != 144 else if (GNUNET_OK !=
145 GNUNET_GNSRECORD_block_decrypt(block, 145 GNUNET_GNSRECORD_block_decrypt (block,
146 &pubkey, 146 &pubkey,
147 name, 147 name,
148 &display_records_from_block, 148 &display_records_from_block,
149 NULL)) 149 NULL))
150 { 150 {
151 fprintf(stderr, "Failed to decrypt block!\n"); 151 fprintf (stderr, "Failed to decrypt block!\n");
152 } 152 }
153 GNUNET_SCHEDULER_shutdown(); 153 GNUNET_SCHEDULER_shutdown ();
154} 154}
155 155
156 156
@@ -163,36 +163,36 @@ handle_block(void *cls, const struct GNUNET_GNSRECORD_Block *block)
163 * @param cfg configuration 163 * @param cfg configuration
164 */ 164 */
165static void 165static void
166run(void *cls, 166run (void *cls,
167 char *const *args, 167 char *const *args,
168 const char *cfgfile, 168 const char *cfgfile,
169 const struct GNUNET_CONFIGURATION_Handle *cfg) 169 const struct GNUNET_CONFIGURATION_Handle *cfg)
170{ 170{
171 struct GNUNET_HashCode dhash; 171 struct GNUNET_HashCode dhash;
172 172
173 if (NULL == pkey) 173 if (NULL == pkey)
174 { 174 {
175 fprintf(stderr, _("You must specify which zone should be accessed\n")); 175 fprintf (stderr, _ ("You must specify which zone should be accessed\n"));
176 return; 176 return;
177 } 177 }
178 178
179 if (GNUNET_OK != 179 if (GNUNET_OK !=
180 GNUNET_CRYPTO_ecdsa_public_key_from_string(pkey, strlen(pkey), &pubkey)) 180 GNUNET_CRYPTO_ecdsa_public_key_from_string (pkey, strlen (pkey), &pubkey))
181 { 181 {
182 fprintf(stderr, _("Invalid public key for zone `%s'\n"), pkey); 182 fprintf (stderr, _ ("Invalid public key for zone `%s'\n"), pkey);
183 GNUNET_SCHEDULER_shutdown(); 183 GNUNET_SCHEDULER_shutdown ();
184 return; 184 return;
185 } 185 }
186 if (NULL == name) 186 if (NULL == name)
187 { 187 {
188 fprintf(stderr, _("You must specify a name\n")); 188 fprintf (stderr, _ ("You must specify a name\n"));
189 return; 189 return;
190 } 190 }
191 191
192 GNUNET_SCHEDULER_add_shutdown(&do_shutdown, NULL); 192 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, NULL);
193 ns = GNUNET_NAMECACHE_connect(cfg); 193 ns = GNUNET_NAMECACHE_connect (cfg);
194 GNUNET_GNSRECORD_query_from_public_key(&pubkey, name, &dhash); 194 GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &dhash);
195 qe = GNUNET_NAMECACHE_lookup_block(ns, &dhash, &handle_block, NULL); 195 qe = GNUNET_NAMECACHE_lookup_block (ns, &dhash, &handle_block, NULL);
196} 196}
197 197
198 198
@@ -204,41 +204,41 @@ run(void *cls,
204 * @return 0 ok, 1 on error 204 * @return 0 ok, 1 on error
205 */ 205 */
206int 206int
207main(int argc, char *const *argv) 207main (int argc, char *const *argv)
208{ 208{
209 struct GNUNET_GETOPT_CommandLineOption options[] = 209 struct GNUNET_GETOPT_CommandLineOption options[] =
210 { GNUNET_GETOPT_option_string('n', 210 { GNUNET_GETOPT_option_string ('n',
211 "name", 211 "name",
212 "NAME", 212 "NAME",
213 gettext_noop( 213 gettext_noop (
214 "name of the record to add/delete/display"), 214 "name of the record to add/delete/display"),
215 &name), 215 &name),
216 216
217 GNUNET_GETOPT_option_string( 217 GNUNET_GETOPT_option_string (
218 'z', 218 'z',
219 "zone", 219 "zone",
220 "PKEY", 220 "PKEY",
221 gettext_noop("specifies the public key of the zone to look in"), 221 gettext_noop ("specifies the public key of the zone to look in"),
222 &pkey), 222 &pkey),
223 223
224 GNUNET_GETOPT_OPTION_END }; 224 GNUNET_GETOPT_OPTION_END };
225 225
226 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args(argc, argv, &argc, &argv)) 226 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
227 return 2; 227 return 2;
228 228
229 GNUNET_log_setup("gnunet-namecache", "WARNING", NULL); 229 GNUNET_log_setup ("gnunet-namecache", "WARNING", NULL);
230 if (GNUNET_OK != GNUNET_PROGRAM_run(argc, 230 if (GNUNET_OK != GNUNET_PROGRAM_run (argc,
231 argv, 231 argv,
232 "gnunet-namecache", 232 "gnunet-namecache",
233 _("GNUnet zone manipulation tool"), 233 _ ("GNUnet zone manipulation tool"),
234 options, 234 options,
235 &run, 235 &run,
236 NULL)) 236 NULL))
237 { 237 {
238 GNUNET_free((void *)argv); 238 GNUNET_free ((void *) argv);
239 return 1; 239 return 1;
240 } 240 }
241 GNUNET_free((void *)argv); 241 GNUNET_free ((void *) argv);
242 return ret; 242 return ret;
243} 243}
244 244
diff --git a/src/namecache/gnunet-service-namecache.c b/src/namecache/gnunet-service-namecache.c
index e9f45a4fc..7cf216ce3 100644
--- a/src/namecache/gnunet-service-namecache.c
+++ b/src/namecache/gnunet-service-namecache.c
@@ -33,13 +33,17 @@
33#include "gnunet_signatures.h" 33#include "gnunet_signatures.h"
34#include "namecache.h" 34#include "namecache.h"
35 35
36#define LOG_STRERROR_FILE(kind, syscall, filename) GNUNET_log_from_strerror_file(kind, "util", syscall, filename) 36#define LOG_STRERROR_FILE(kind, syscall, \
37 filename) GNUNET_log_from_strerror_file (kind, "util", \
38 syscall, \
39 filename)
37 40
38 41
39/** 42/**
40 * A namecache client 43 * A namecache client
41 */ 44 */
42struct NamecacheClient { 45struct NamecacheClient
46{
43 /** 47 /**
44 * The client 48 * The client
45 */ 49 */
@@ -79,21 +83,21 @@ static char *db_lib_name;
79 * @param cls unused 83 * @param cls unused
80 */ 84 */
81static void 85static void
82cleanup_task(void *cls) 86cleanup_task (void *cls)
83{ 87{
84 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 88 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Stopping namecache service\n"); 89 "Stopping namecache service\n");
86 GNUNET_break(NULL == 90 GNUNET_break (NULL ==
87 GNUNET_PLUGIN_unload(db_lib_name, 91 GNUNET_PLUGIN_unload (db_lib_name,
88 GSN_database)); 92 GSN_database));
89 GNUNET_free(db_lib_name); 93 GNUNET_free (db_lib_name);
90 db_lib_name = NULL; 94 db_lib_name = NULL;
91 if (NULL != statistics) 95 if (NULL != statistics)
92 { 96 {
93 GNUNET_STATISTICS_destroy(statistics, 97 GNUNET_STATISTICS_destroy (statistics,
94 GNUNET_NO); 98 GNUNET_NO);
95 statistics = NULL; 99 statistics = NULL;
96 } 100 }
97} 101}
98 102
99 103
@@ -106,16 +110,16 @@ cleanup_task(void *cls)
106 * @param app_ctx the `struct NamecacheClient` for this @a client 110 * @param app_ctx the `struct NamecacheClient` for this @a client
107 */ 111 */
108static void 112static void
109client_disconnect_cb(void *cls, 113client_disconnect_cb (void *cls,
110 struct GNUNET_SERVICE_Client *client, 114 struct GNUNET_SERVICE_Client *client,
111 void *app_ctx) 115 void *app_ctx)
112{ 116{
113 struct NamecacheClient *nc = app_ctx; 117 struct NamecacheClient *nc = app_ctx;
114 118
115 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
116 "Client %p disconnected\n", 120 "Client %p disconnected\n",
117 client); 121 client);
118 GNUNET_free(nc); 122 GNUNET_free (nc);
119} 123}
120 124
121 125
@@ -128,16 +132,16 @@ client_disconnect_cb(void *cls,
128 * @return internal namecache client structure for this client 132 * @return internal namecache client structure for this client
129 */ 133 */
130static void * 134static void *
131client_connect_cb(void *cls, 135client_connect_cb (void *cls,
132 struct GNUNET_SERVICE_Client *client, 136 struct GNUNET_SERVICE_Client *client,
133 struct GNUNET_MQ_Handle *mq) 137 struct GNUNET_MQ_Handle *mq)
134{ 138{
135 struct NamecacheClient *nc; 139 struct NamecacheClient *nc;
136 140
137 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
138 "Client %p connected\n", 142 "Client %p connected\n",
139 client); 143 client);
140 nc = GNUNET_new(struct NamecacheClient); 144 nc = GNUNET_new (struct NamecacheClient);
141 nc->client = client; 145 nc->client = client;
142 nc->mq = mq; 146 nc->mq = mq;
143 return nc; 147 return nc;
@@ -148,7 +152,8 @@ client_connect_cb(void *cls,
148 * Context for name lookups passed from #handle_lookup_block to 152 * Context for name lookups passed from #handle_lookup_block to
149 * #handle_lookup_block_it as closure 153 * #handle_lookup_block_it as closure
150 */ 154 */
151struct LookupBlockContext { 155struct LookupBlockContext
156{
152 /** 157 /**
153 * The client to send the response to 158 * The client to send the response to
154 */ 159 */
@@ -173,8 +178,8 @@ struct LookupBlockContext {
173 * @param block the block 178 * @param block the block
174 */ 179 */
175static void 180static void
176handle_lookup_block_it(void *cls, 181handle_lookup_block_it (void *cls,
177 const struct GNUNET_GNSRECORD_Block *block) 182 const struct GNUNET_GNSRECORD_Block *block)
178{ 183{
179 struct LookupBlockContext *lnc = cls; 184 struct LookupBlockContext *lnc = cls;
180 struct GNUNET_MQ_Envelope *env; 185 struct GNUNET_MQ_Envelope *env;
@@ -182,37 +187,39 @@ handle_lookup_block_it(void *cls,
182 size_t esize; 187 size_t esize;
183 size_t bsize; 188 size_t bsize;
184 189
185 bsize = ntohl(block->purpose.size); 190 bsize = ntohl (block->purpose.size);
186 if (bsize < 191 if (bsize <
187 (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof(struct GNUNET_TIME_AbsoluteNBO))) 192 (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + sizeof(struct
188 { 193 GNUNET_TIME_AbsoluteNBO)))
189 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, 194 {
190 "Malformed block."); 195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
191 lnc->status = GNUNET_SYSERR; 196 "Malformed block.");
192 return; 197 lnc->status = GNUNET_SYSERR;
193 } 198 return;
194 esize = ntohl(block->purpose.size) 199 }
200 esize = ntohl (block->purpose.size)
195 - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) 201 - sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
196 - sizeof(struct GNUNET_TIME_AbsoluteNBO); 202 - sizeof(struct GNUNET_TIME_AbsoluteNBO);
197 env = GNUNET_MQ_msg_extra(r, 203 env = GNUNET_MQ_msg_extra (r,
198 esize, 204 esize,
199 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); 205 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
200 r->gns_header.r_id = htonl(lnc->request_id); 206 r->gns_header.r_id = htonl (lnc->request_id);
201 r->expire = block->expiration_time; 207 r->expire = block->expiration_time;
202 r->signature = block->signature; 208 r->signature = block->signature;
203 r->derived_key = block->derived_key; 209 r->derived_key = block->derived_key;
204 GNUNET_memcpy(&r[1], 210 GNUNET_memcpy (&r[1],
205 &block[1], 211 &block[1],
206 esize); 212 esize);
207 GNUNET_STATISTICS_update(statistics, 213 GNUNET_STATISTICS_update (statistics,
208 "blocks found in cache", 214 "blocks found in cache",
209 1, 215 1,
210 GNUNET_NO); 216 GNUNET_NO);
211 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 217 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
212 "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message with expiration time %s\n", 218 "Sending NAMECACHE_LOOKUP_BLOCK_RESPONSE message with expiration time %s\n",
213 GNUNET_STRINGS_absolute_time_to_string(GNUNET_TIME_absolute_ntoh(r->expire))); 219 GNUNET_STRINGS_absolute_time_to_string (
214 GNUNET_MQ_send(lnc->nc->mq, 220 GNUNET_TIME_absolute_ntoh (r->expire)));
215 env); 221 GNUNET_MQ_send (lnc->nc->mq,
222 env);
216} 223}
217 224
218 225
@@ -223,8 +230,8 @@ handle_lookup_block_it(void *cls,
223 * @param the inbound message 230 * @param the inbound message
224 */ 231 */
225static void 232static void
226handle_lookup_block(void *cls, 233handle_lookup_block (void *cls,
227 const struct LookupBlockMessage *ln_msg) 234 const struct LookupBlockMessage *ln_msg)
228{ 235{
229 struct NamecacheClient *nc = cls; 236 struct NamecacheClient *nc = cls;
230 struct GNUNET_MQ_Envelope *env; 237 struct GNUNET_MQ_Envelope *env;
@@ -232,40 +239,40 @@ handle_lookup_block(void *cls,
232 struct LookupBlockResponseMessage *zir_end; 239 struct LookupBlockResponseMessage *zir_end;
233 int ret; 240 int ret;
234 241
235 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
236 "Received NAMECACHE_LOOKUP_BLOCK message\n"); 243 "Received NAMECACHE_LOOKUP_BLOCK message\n");
237 GNUNET_STATISTICS_update(statistics, 244 GNUNET_STATISTICS_update (statistics,
238 "blocks looked up", 245 "blocks looked up",
239 1, 246 1,
240 GNUNET_NO); 247 GNUNET_NO);
241 lnc.request_id = ntohl(ln_msg->gns_header.r_id); 248 lnc.request_id = ntohl (ln_msg->gns_header.r_id);
242 lnc.nc = nc; 249 lnc.nc = nc;
243 lnc.status = GNUNET_OK; 250 lnc.status = GNUNET_OK;
244 if (GNUNET_SYSERR == 251 if (GNUNET_SYSERR ==
245 (ret = GSN_database->lookup_block(GSN_database->cls, 252 (ret = GSN_database->lookup_block (GSN_database->cls,
246 &ln_msg->query, 253 &ln_msg->query,
247 &handle_lookup_block_it, 254 &handle_lookup_block_it,
248 &lnc))) 255 &lnc)))
249 { 256 {
250 /* internal error (in database plugin); might be best to just hang up on 257 /* internal error (in database plugin); might be best to just hang up on
251 plugin rather than to signal that there are 'no' results, which 258 plugin rather than to signal that there are 'no' results, which
252 might also be false... */ 259 might also be false... */
253 GNUNET_break(0); 260 GNUNET_break (0);
254 GNUNET_SERVICE_client_drop(nc->client); 261 GNUNET_SERVICE_client_drop (nc->client);
255 return; 262 return;
256 } 263 }
257 if ((0 == ret) || (GNUNET_SYSERR == lnc.status)) 264 if ((0 == ret) || (GNUNET_SYSERR == lnc.status))
258 { 265 {
259 /* no records match at all, generate empty response */ 266 /* no records match at all, generate empty response */
260 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n"); 268 "Sending empty NAMECACHE_LOOKUP_BLOCK_RESPONSE message\n");
262 env = GNUNET_MQ_msg(zir_end, 269 env = GNUNET_MQ_msg (zir_end,
263 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE); 270 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE);
264 zir_end->gns_header.r_id = ln_msg->gns_header.r_id; 271 zir_end->gns_header.r_id = ln_msg->gns_header.r_id;
265 GNUNET_MQ_send(nc->mq, 272 GNUNET_MQ_send (nc->mq,
266 env); 273 env);
267 } 274 }
268 GNUNET_SERVICE_client_continue(nc->client); 275 GNUNET_SERVICE_client_continue (nc->client);
269} 276}
270 277
271 278
@@ -277,8 +284,8 @@ handle_lookup_block(void *cls,
277 * @return #GNUNET_OK (always fine) 284 * @return #GNUNET_OK (always fine)
278 */ 285 */
279static int 286static int
280check_block_cache(void *cls, 287check_block_cache (void *cls,
281 const struct BlockCacheMessage *rp_msg) 288 const struct BlockCacheMessage *rp_msg)
282{ 289{
283 return GNUNET_OK; 290 return GNUNET_OK;
284} 291}
@@ -291,8 +298,8 @@ check_block_cache(void *cls,
291 * @param rp_msg message to process 298 * @param rp_msg message to process
292 */ 299 */
293static void 300static void
294handle_block_cache(void *cls, 301handle_block_cache (void *cls,
295 const struct BlockCacheMessage *rp_msg) 302 const struct BlockCacheMessage *rp_msg)
296{ 303{
297 struct NamecacheClient *nc = cls; 304 struct NamecacheClient *nc = cls;
298 struct GNUNET_MQ_Envelope *env; 305 struct GNUNET_MQ_Envelope *env;
@@ -301,34 +308,36 @@ handle_block_cache(void *cls,
301 size_t esize; 308 size_t esize;
302 int res; 309 int res;
303 310
304 GNUNET_STATISTICS_update(statistics, 311 GNUNET_STATISTICS_update (statistics,
305 "blocks cached", 312 "blocks cached",
306 1, 313 1,
307 GNUNET_NO); 314 GNUNET_NO);
308 esize = ntohs(rp_msg->gns_header.header.size) - sizeof(struct BlockCacheMessage); 315 esize = ntohs (rp_msg->gns_header.header.size) - sizeof(struct
309 block = GNUNET_malloc(sizeof(struct GNUNET_GNSRECORD_Block) + esize); 316 BlockCacheMessage);
317 block = GNUNET_malloc (sizeof(struct GNUNET_GNSRECORD_Block) + esize);
310 block->signature = rp_msg->signature; 318 block->signature = rp_msg->signature;
311 block->derived_key = rp_msg->derived_key; 319 block->derived_key = rp_msg->derived_key;
312 block->purpose.size = htonl(sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose) + 320 block->purpose.size = htonl (sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)
313 sizeof(struct GNUNET_TIME_AbsoluteNBO) + 321 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
314 esize); 322 + esize);
315 block->expiration_time = rp_msg->expire; 323 block->expiration_time = rp_msg->expire;
316 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Received NAMECACHE_BLOCK_CACHE message with expiration time %s\n", 325 "Received NAMECACHE_BLOCK_CACHE message with expiration time %s\n",
318 GNUNET_STRINGS_absolute_time_to_string(GNUNET_TIME_absolute_ntoh(block->expiration_time))); 326 GNUNET_STRINGS_absolute_time_to_string (
319 GNUNET_memcpy(&block[1], 327 GNUNET_TIME_absolute_ntoh (block->expiration_time)));
320 &rp_msg[1], 328 GNUNET_memcpy (&block[1],
321 esize); 329 &rp_msg[1],
322 res = GSN_database->cache_block(GSN_database->cls, 330 esize);
323 block); 331 res = GSN_database->cache_block (GSN_database->cls,
324 GNUNET_free(block); 332 block);
325 env = GNUNET_MQ_msg(rpr_msg, 333 GNUNET_free (block);
326 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE); 334 env = GNUNET_MQ_msg (rpr_msg,
335 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE);
327 rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id; 336 rpr_msg->gns_header.r_id = rp_msg->gns_header.r_id;
328 rpr_msg->op_result = htonl(res); 337 rpr_msg->op_result = htonl (res);
329 GNUNET_MQ_send(nc->mq, 338 GNUNET_MQ_send (nc->mq,
330 env); 339 env);
331 GNUNET_SERVICE_client_continue(nc->client); 340 GNUNET_SERVICE_client_continue (nc->client);
332} 341}
333 342
334 343
@@ -340,46 +349,46 @@ handle_block_cache(void *cls,
340 * @param service the initialized service 349 * @param service the initialized service
341 */ 350 */
342static void 351static void
343run(void *cls, 352run (void *cls,
344 const struct GNUNET_CONFIGURATION_Handle *cfg, 353 const struct GNUNET_CONFIGURATION_Handle *cfg,
345 struct GNUNET_SERVICE_Handle *service) 354 struct GNUNET_SERVICE_Handle *service)
346{ 355{
347 char *database; 356 char *database;
348 357
349 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
350 "Starting namecache service\n"); 359 "Starting namecache service\n");
351 GSN_cfg = cfg; 360 GSN_cfg = cfg;
352 361
353 /* Loading database plugin */ 362 /* Loading database plugin */
354 if (GNUNET_OK != 363 if (GNUNET_OK !=
355 GNUNET_CONFIGURATION_get_value_string(cfg, 364 GNUNET_CONFIGURATION_get_value_string (cfg,
356 "namecache", 365 "namecache",
357 "database", 366 "database",
358 &database)) 367 &database))
359 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 368 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
360 "No database backend configured\n"); 369 "No database backend configured\n");
361 370
362 GNUNET_asprintf(&db_lib_name, 371 GNUNET_asprintf (&db_lib_name,
363 "libgnunet_plugin_namecache_%s", 372 "libgnunet_plugin_namecache_%s",
364 database); 373 database);
365 GSN_database = GNUNET_PLUGIN_load(db_lib_name, 374 GSN_database = GNUNET_PLUGIN_load (db_lib_name,
366 (void *)GSN_cfg); 375 (void *) GSN_cfg);
367 GNUNET_free(database); 376 GNUNET_free (database);
368 if (NULL == GSN_database) 377 if (NULL == GSN_database)
369 { 378 {
370 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
371 "Could not load database backend `%s'\n", 380 "Could not load database backend `%s'\n",
372 db_lib_name); 381 db_lib_name);
373 GNUNET_SCHEDULER_add_now(&cleanup_task, 382 GNUNET_SCHEDULER_add_now (&cleanup_task,
374 NULL); 383 NULL);
375 return; 384 return;
376 } 385 }
377 statistics = GNUNET_STATISTICS_create("namecache", 386 statistics = GNUNET_STATISTICS_create ("namecache",
378 cfg); 387 cfg);
379 388
380 /* Configuring server handles */ 389 /* Configuring server handles */
381 GNUNET_SCHEDULER_add_shutdown(&cleanup_task, 390 GNUNET_SCHEDULER_add_shutdown (&cleanup_task,
382 NULL); 391 NULL);
383} 392}
384 393
385 394
@@ -393,15 +402,15 @@ GNUNET_SERVICE_MAIN
393 &client_connect_cb, 402 &client_connect_cb,
394 &client_disconnect_cb, 403 &client_disconnect_cb,
395 NULL, 404 NULL,
396 GNUNET_MQ_hd_fixed_size(lookup_block, 405 GNUNET_MQ_hd_fixed_size (lookup_block,
397 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK, 406 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK,
398 struct LookupBlockMessage, 407 struct LookupBlockMessage,
399 NULL), 408 NULL),
400 GNUNET_MQ_hd_var_size(block_cache, 409 GNUNET_MQ_hd_var_size (block_cache,
401 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE, 410 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE,
402 struct BlockCacheMessage, 411 struct BlockCacheMessage,
403 NULL), 412 NULL),
404 GNUNET_MQ_handler_end()); 413 GNUNET_MQ_handler_end ());
405 414
406 415
407/* end of gnunet-service-namecache.c */ 416/* end of gnunet-service-namecache.c */
diff --git a/src/namecache/namecache.h b/src/namecache/namecache.h
index fa7c75c11..1657662c2 100644
--- a/src/namecache/namecache.h
+++ b/src/namecache/namecache.h
@@ -37,7 +37,8 @@ GNUNET_NETWORK_STRUCT_BEGIN
37/** 37/**
38 * Generic namecache message with op id 38 * Generic namecache message with op id
39 */ 39 */
40struct GNUNET_NAMECACHE_Header { 40struct GNUNET_NAMECACHE_Header
41{
41 /** 42 /**
42 * header.type will be GNUNET_MESSAGE_TYPE_NAMECACHE_* 43 * header.type will be GNUNET_MESSAGE_TYPE_NAMECACHE_*
43 * header.size will be message size 44 * header.size will be message size
@@ -54,7 +55,8 @@ struct GNUNET_NAMECACHE_Header {
54/** 55/**
55 * Lookup a block in the namecache 56 * Lookup a block in the namecache
56 */ 57 */
57struct LookupBlockMessage { 58struct LookupBlockMessage
59{
58 /** 60 /**
59 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK 61 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK
60 */ 62 */
@@ -70,7 +72,8 @@ struct LookupBlockMessage {
70/** 72/**
71 * Lookup response 73 * Lookup response
72 */ 74 */
73struct LookupBlockResponseMessage { 75struct LookupBlockResponseMessage
76{
74 /** 77 /**
75 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE 78 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE
76 */ 79 */
@@ -98,7 +101,8 @@ struct LookupBlockResponseMessage {
98/** 101/**
99 * Cache a record in the namecache. 102 * Cache a record in the namecache.
100 */ 103 */
101struct BlockCacheMessage { 104struct BlockCacheMessage
105{
102 /** 106 /**
103 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE 107 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE
104 */ 108 */
@@ -126,7 +130,8 @@ struct BlockCacheMessage {
126/** 130/**
127 * Response to a request to cache a block. 131 * Response to a request to cache a block.
128 */ 132 */
129struct BlockCacheResponseMessage { 133struct BlockCacheResponseMessage
134{
130 /** 135 /**
131 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE 136 * Type will be #GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE
132 */ 137 */
diff --git a/src/namecache/namecache_api.c b/src/namecache/namecache_api.c
index 9cf9eca77..0c904c9ed 100644
--- a/src/namecache/namecache_api.c
+++ b/src/namecache/namecache_api.c
@@ -37,14 +37,15 @@
37#include "namecache.h" 37#include "namecache.h"
38 38
39 39
40#define LOG(kind, ...) GNUNET_log_from(kind, "namecache-api", __VA_ARGS__) 40#define LOG(kind, ...) GNUNET_log_from (kind, "namecache-api", __VA_ARGS__)
41 41
42 42
43/** 43/**
44 * An QueueEntry used to store information for a pending 44 * An QueueEntry used to store information for a pending
45 * NAMECACHE record operation 45 * NAMECACHE record operation
46 */ 46 */
47struct GNUNET_NAMECACHE_QueueEntry { 47struct GNUNET_NAMECACHE_QueueEntry
48{
48 /** 49 /**
49 * Kept in a DLL. 50 * Kept in a DLL.
50 */ 51 */
@@ -90,7 +91,8 @@ struct GNUNET_NAMECACHE_QueueEntry {
90/** 91/**
91 * Connection to the NAMECACHE service. 92 * Connection to the NAMECACHE service.
92 */ 93 */
93struct GNUNET_NAMECACHE_Handle { 94struct GNUNET_NAMECACHE_Handle
95{
94 /** 96 /**
95 * Configuration to use. 97 * Configuration to use.
96 */ 98 */
@@ -144,7 +146,7 @@ struct GNUNET_NAMECACHE_Handle {
144 * @param h our handle 146 * @param h our handle
145 */ 147 */
146static void 148static void
147force_reconnect(struct GNUNET_NAMECACHE_Handle *h); 149force_reconnect (struct GNUNET_NAMECACHE_Handle *h);
148 150
149 151
150/** 152/**
@@ -155,21 +157,21 @@ force_reconnect(struct GNUNET_NAMECACHE_Handle *h);
155 * @return NULL if not found, otherwise the queue entry (removed from the queue) 157 * @return NULL if not found, otherwise the queue entry (removed from the queue)
156 */ 158 */
157static struct GNUNET_NAMECACHE_QueueEntry * 159static struct GNUNET_NAMECACHE_QueueEntry *
158find_qe(struct GNUNET_NAMECACHE_Handle *h, 160find_qe (struct GNUNET_NAMECACHE_Handle *h,
159 uint32_t rid) 161 uint32_t rid)
160{ 162{
161 struct GNUNET_NAMECACHE_QueueEntry *qe; 163 struct GNUNET_NAMECACHE_QueueEntry *qe;
162 164
163 for (qe = h->op_head; qe != NULL; qe = qe->next) 165 for (qe = h->op_head; qe != NULL; qe = qe->next)
166 {
167 if (qe->op_id == rid)
164 { 168 {
165 if (qe->op_id == rid) 169 GNUNET_CONTAINER_DLL_remove (h->op_head,
166 { 170 h->op_tail,
167 GNUNET_CONTAINER_DLL_remove(h->op_head, 171 qe);
168 h->op_tail, 172 return qe;
169 qe);
170 return qe;
171 }
172 } 173 }
174 }
173 return NULL; 175 return NULL;
174} 176}
175 177
@@ -182,8 +184,8 @@ find_qe(struct GNUNET_NAMECACHE_Handle *h,
182 * @param msg the message we received 184 * @param msg the message we received
183 */ 185 */
184static int 186static int
185check_lookup_block_response(void *cls, 187check_lookup_block_response (void *cls,
186 const struct LookupBlockResponseMessage *msg) 188 const struct LookupBlockResponseMessage *msg)
187{ 189{
188 /* any length will do, format validation is in handler */ 190 /* any length will do, format validation is in handler */
189 return GNUNET_OK; 191 return GNUNET_OK;
@@ -198,62 +200,63 @@ check_lookup_block_response(void *cls,
198 * @param msg the message we received 200 * @param msg the message we received
199 */ 201 */
200static void 202static void
201handle_lookup_block_response(void *cls, 203handle_lookup_block_response (void *cls,
202 const struct LookupBlockResponseMessage *msg) 204 const struct LookupBlockResponseMessage *msg)
203{ 205{
204 struct GNUNET_NAMECACHE_Handle *h = cls; 206 struct GNUNET_NAMECACHE_Handle *h = cls;
205 size_t size; 207 size_t size;
206 struct GNUNET_NAMECACHE_QueueEntry *qe; 208 struct GNUNET_NAMECACHE_QueueEntry *qe;
207 209
208 LOG(GNUNET_ERROR_TYPE_DEBUG, 210 LOG (GNUNET_ERROR_TYPE_DEBUG,
209 "Received LOOKUP_BLOCK_RESPONSE\n"); 211 "Received LOOKUP_BLOCK_RESPONSE\n");
210 qe = find_qe(h, 212 qe = find_qe (h,
211 ntohl(msg->gns_header.r_id)); 213 ntohl (msg->gns_header.r_id));
212 if (NULL == qe) 214 if (NULL == qe)
213 return; 215 return;
214 if (0 == GNUNET_TIME_absolute_ntoh(msg->expire).abs_value_us) 216 if (0 == GNUNET_TIME_absolute_ntoh (msg->expire).abs_value_us)
215 { 217 {
216 /* no match found */ 218 /* no match found */
217 if (NULL != qe->block_proc) 219 if (NULL != qe->block_proc)
218 qe->block_proc(qe->block_proc_cls, 220 qe->block_proc (qe->block_proc_cls,
219 NULL); 221 NULL);
220 GNUNET_free(qe); 222 GNUNET_free (qe);
221 return; 223 return;
222 } 224 }
223 size = ntohs(msg->gns_header.header.size) 225 size = ntohs (msg->gns_header.header.size)
224 - sizeof(struct LookupBlockResponseMessage); 226 - sizeof(struct LookupBlockResponseMessage);
225 { 227 {
226 char buf[size + sizeof(struct GNUNET_GNSRECORD_Block)] GNUNET_ALIGN; 228 char buf[size + sizeof(struct GNUNET_GNSRECORD_Block)] GNUNET_ALIGN;
227 struct GNUNET_GNSRECORD_Block *block; 229 struct GNUNET_GNSRECORD_Block *block;
228 230
229 block = (struct GNUNET_GNSRECORD_Block *)buf; 231 block = (struct GNUNET_GNSRECORD_Block *) buf;
230 block->signature = msg->signature; 232 block->signature = msg->signature;
231 block->derived_key = msg->derived_key; 233 block->derived_key = msg->derived_key;
232 block->purpose.purpose = htonl(GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN); 234 block->purpose.purpose = htonl (GNUNET_SIGNATURE_PURPOSE_GNS_RECORD_SIGN);
233 block->purpose.size = htonl(size + 235 block->purpose.size = htonl (size
234 sizeof(struct GNUNET_TIME_AbsoluteNBO) + 236 + sizeof(struct GNUNET_TIME_AbsoluteNBO)
235 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); 237 + sizeof(struct
238 GNUNET_CRYPTO_EccSignaturePurpose));
236 block->expiration_time = msg->expire; 239 block->expiration_time = msg->expire;
237 GNUNET_memcpy(&block[1], 240 GNUNET_memcpy (&block[1],
238 &msg[1], 241 &msg[1],
239 size); 242 size);
240 if (GNUNET_OK != 243 if (GNUNET_OK !=
241 GNUNET_GNSRECORD_block_verify(block)) 244 GNUNET_GNSRECORD_block_verify (block))
242 { 245 {
243 GNUNET_break(0); 246 GNUNET_break (0);
244 if (NULL != qe->block_proc) 247 if (NULL != qe->block_proc)
245 qe->block_proc(qe->block_proc_cls, 248 qe->block_proc (qe->block_proc_cls,
246 NULL); 249 NULL);
247 force_reconnect(h); 250 force_reconnect (h);
248 } 251 }
249 else 252 else
250 { 253 {
251 if (NULL != qe->block_proc) 254 if (NULL != qe->block_proc)
252 qe->block_proc(qe->block_proc_cls, 255 qe->block_proc (qe->block_proc_cls,
253 block); 256 block);
254 } 257 }
255 } 258 }
256 GNUNET_free(qe); 259 GNUNET_free (qe);
257} 260}
258 261
259 262
@@ -267,28 +270,28 @@ handle_lookup_block_response(void *cls,
267 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client 270 * @return #GNUNET_OK on success, #GNUNET_SYSERR on error and we did NOT notify the client
268 */ 271 */
269static void 272static void
270handle_block_cache_response(void *cls, 273handle_block_cache_response (void *cls,
271 const struct BlockCacheResponseMessage *msg) 274 const struct BlockCacheResponseMessage *msg)
272{ 275{
273 struct GNUNET_NAMECACHE_Handle *h = cls; 276 struct GNUNET_NAMECACHE_Handle *h = cls;
274 struct GNUNET_NAMECACHE_QueueEntry *qe; 277 struct GNUNET_NAMECACHE_QueueEntry *qe;
275 int res; 278 int res;
276 279
277 LOG(GNUNET_ERROR_TYPE_DEBUG, 280 LOG (GNUNET_ERROR_TYPE_DEBUG,
278 "Received BLOCK_CACHE_RESPONSE\n"); 281 "Received BLOCK_CACHE_RESPONSE\n");
279 qe = find_qe(h, 282 qe = find_qe (h,
280 ntohl(msg->gns_header.r_id)); 283 ntohl (msg->gns_header.r_id));
281 if (NULL == qe) 284 if (NULL == qe)
282 return; 285 return;
283 res = ntohl(msg->op_result); 286 res = ntohl (msg->op_result);
284 /* TODO: add actual error message from namecache to response... */ 287 /* TODO: add actual error message from namecache to response... */
285 if (NULL != qe->cont) 288 if (NULL != qe->cont)
286 qe->cont(qe->cont_cls, 289 qe->cont (qe->cont_cls,
287 res, 290 res,
288 (GNUNET_OK == res) 291 (GNUNET_OK == res)
289 ? NULL 292 ? NULL
290 : _("Namecache failed to cache block")); 293 : _ ("Namecache failed to cache block"));
291 GNUNET_free(qe); 294 GNUNET_free (qe);
292} 295}
293 296
294 297
@@ -301,12 +304,12 @@ handle_block_cache_response(void *cls,
301 * @param error error code 304 * @param error error code
302 */ 305 */
303static void 306static void
304mq_error_handler(void *cls, 307mq_error_handler (void *cls,
305 enum GNUNET_MQ_Error error) 308 enum GNUNET_MQ_Error error)
306{ 309{
307 struct GNUNET_NAMECACHE_Handle *h = cls; 310 struct GNUNET_NAMECACHE_Handle *h = cls;
308 311
309 force_reconnect(h); 312 force_reconnect (h);
310} 313}
311 314
312 315
@@ -316,26 +319,26 @@ mq_error_handler(void *cls,
316 * @param h the handle to the NAMECACHE service 319 * @param h the handle to the NAMECACHE service
317 */ 320 */
318static void 321static void
319reconnect(struct GNUNET_NAMECACHE_Handle *h) 322reconnect (struct GNUNET_NAMECACHE_Handle *h)
320{ 323{
321 struct GNUNET_MQ_MessageHandler handlers[] = { 324 struct GNUNET_MQ_MessageHandler handlers[] = {
322 GNUNET_MQ_hd_var_size(lookup_block_response, 325 GNUNET_MQ_hd_var_size (lookup_block_response,
323 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE, 326 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK_RESPONSE,
324 struct LookupBlockResponseMessage, 327 struct LookupBlockResponseMessage,
325 h), 328 h),
326 GNUNET_MQ_hd_fixed_size(block_cache_response, 329 GNUNET_MQ_hd_fixed_size (block_cache_response,
327 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE, 330 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE_RESPONSE,
328 struct BlockCacheResponseMessage, 331 struct BlockCacheResponseMessage,
329 h), 332 h),
330 GNUNET_MQ_handler_end() 333 GNUNET_MQ_handler_end ()
331 }; 334 };
332 335
333 GNUNET_assert(NULL == h->mq); 336 GNUNET_assert (NULL == h->mq);
334 h->mq = GNUNET_CLIENT_connect(h->cfg, 337 h->mq = GNUNET_CLIENT_connect (h->cfg,
335 "namecache", 338 "namecache",
336 handlers, 339 handlers,
337 &mq_error_handler, 340 &mq_error_handler,
338 h); 341 h);
339} 342}
340 343
341 344
@@ -345,12 +348,12 @@ reconnect(struct GNUNET_NAMECACHE_Handle *h)
345 * @param cls handle to use to re-connect. 348 * @param cls handle to use to re-connect.
346 */ 349 */
347static void 350static void
348reconnect_task(void *cls) 351reconnect_task (void *cls)
349{ 352{
350 struct GNUNET_NAMECACHE_Handle *h = cls; 353 struct GNUNET_NAMECACHE_Handle *h = cls;
351 354
352 h->reconnect_task = NULL; 355 h->reconnect_task = NULL;
353 reconnect(h); 356 reconnect (h);
354} 357}
355 358
356 359
@@ -360,30 +363,30 @@ reconnect_task(void *cls)
360 * @param h our handle 363 * @param h our handle
361 */ 364 */
362static void 365static void
363force_reconnect(struct GNUNET_NAMECACHE_Handle *h) 366force_reconnect (struct GNUNET_NAMECACHE_Handle *h)
364{ 367{
365 struct GNUNET_NAMECACHE_QueueEntry *qe; 368 struct GNUNET_NAMECACHE_QueueEntry *qe;
366 369
367 h->reconnect = GNUNET_NO; 370 h->reconnect = GNUNET_NO;
368 GNUNET_MQ_destroy(h->mq); 371 GNUNET_MQ_destroy (h->mq);
369 h->mq = NULL; 372 h->mq = NULL;
370 while (NULL != (qe = h->op_head)) 373 while (NULL != (qe = h->op_head))
371 { 374 {
372 GNUNET_CONTAINER_DLL_remove(h->op_head, 375 GNUNET_CONTAINER_DLL_remove (h->op_head,
373 h->op_tail, 376 h->op_tail,
374 qe); 377 qe);
375 if (NULL != qe->cont) 378 if (NULL != qe->cont)
376 qe->cont(qe->cont_cls, 379 qe->cont (qe->cont_cls,
377 GNUNET_SYSERR, 380 GNUNET_SYSERR,
378 _("Error communicating with namecache service")); 381 _ ("Error communicating with namecache service"));
379 GNUNET_free(qe); 382 GNUNET_free (qe);
380 } 383 }
381 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
382 "Reconnecting to namecache\n"); 385 "Reconnecting to namecache\n");
383 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF(h->reconnect_delay); 386 h->reconnect_delay = GNUNET_TIME_STD_BACKOFF (h->reconnect_delay);
384 h->reconnect_task = GNUNET_SCHEDULER_add_delayed(h->reconnect_delay, 387 h->reconnect_task = GNUNET_SCHEDULER_add_delayed (h->reconnect_delay,
385 &reconnect_task, 388 &reconnect_task,
386 h); 389 h);
387} 390}
388 391
389 392
@@ -394,7 +397,7 @@ force_reconnect(struct GNUNET_NAMECACHE_Handle *h)
394 * @return next operation id to use 397 * @return next operation id to use
395 */ 398 */
396static uint32_t 399static uint32_t
397get_op_id(struct GNUNET_NAMECACHE_Handle *h) 400get_op_id (struct GNUNET_NAMECACHE_Handle *h)
398{ 401{
399 return h->last_op_id_used++; 402 return h->last_op_id_used++;
400} 403}
@@ -407,18 +410,18 @@ get_op_id(struct GNUNET_NAMECACHE_Handle *h)
407 * @return handle to the GNS service, or NULL on error 410 * @return handle to the GNS service, or NULL on error
408 */ 411 */
409struct GNUNET_NAMECACHE_Handle * 412struct GNUNET_NAMECACHE_Handle *
410GNUNET_NAMECACHE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg) 413GNUNET_NAMECACHE_connect (const struct GNUNET_CONFIGURATION_Handle *cfg)
411{ 414{
412 struct GNUNET_NAMECACHE_Handle *h; 415 struct GNUNET_NAMECACHE_Handle *h;
413 416
414 h = GNUNET_new(struct GNUNET_NAMECACHE_Handle); 417 h = GNUNET_new (struct GNUNET_NAMECACHE_Handle);
415 h->cfg = cfg; 418 h->cfg = cfg;
416 reconnect(h); 419 reconnect (h);
417 if (NULL == h->mq) 420 if (NULL == h->mq)
418 { 421 {
419 GNUNET_free(h); 422 GNUNET_free (h);
420 return NULL; 423 return NULL;
421 } 424 }
422 return h; 425 return h;
423} 426}
424 427
@@ -430,29 +433,29 @@ GNUNET_NAMECACHE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
430 * @param h handle to the namecache 433 * @param h handle to the namecache
431 */ 434 */
432void 435void
433GNUNET_NAMECACHE_disconnect(struct GNUNET_NAMECACHE_Handle *h) 436GNUNET_NAMECACHE_disconnect (struct GNUNET_NAMECACHE_Handle *h)
434{ 437{
435 struct GNUNET_NAMECACHE_QueueEntry *q; 438 struct GNUNET_NAMECACHE_QueueEntry *q;
436 439
437 GNUNET_break(NULL == h->op_head); 440 GNUNET_break (NULL == h->op_head);
438 while (NULL != (q = h->op_head)) 441 while (NULL != (q = h->op_head))
439 { 442 {
440 GNUNET_CONTAINER_DLL_remove(h->op_head, 443 GNUNET_CONTAINER_DLL_remove (h->op_head,
441 h->op_tail, 444 h->op_tail,
442 q); 445 q);
443 GNUNET_free(q); 446 GNUNET_free (q);
444 } 447 }
445 if (NULL != h->mq) 448 if (NULL != h->mq)
446 { 449 {
447 GNUNET_MQ_destroy(h->mq); 450 GNUNET_MQ_destroy (h->mq);
448 h->mq = NULL; 451 h->mq = NULL;
449 } 452 }
450 if (NULL != h->reconnect_task) 453 if (NULL != h->reconnect_task)
451 { 454 {
452 GNUNET_SCHEDULER_cancel(h->reconnect_task); 455 GNUNET_SCHEDULER_cancel (h->reconnect_task);
453 h->reconnect_task = NULL; 456 h->reconnect_task = NULL;
454 } 457 }
455 GNUNET_free(h); 458 GNUNET_free (h);
456} 459}
457 460
458 461
@@ -467,10 +470,10 @@ GNUNET_NAMECACHE_disconnect(struct GNUNET_NAMECACHE_Handle *h)
467 * @return handle to abort the request, NULL on error 470 * @return handle to abort the request, NULL on error
468 */ 471 */
469struct GNUNET_NAMECACHE_QueueEntry * 472struct GNUNET_NAMECACHE_QueueEntry *
470GNUNET_NAMECACHE_block_cache(struct GNUNET_NAMECACHE_Handle *h, 473GNUNET_NAMECACHE_block_cache (struct GNUNET_NAMECACHE_Handle *h,
471 const struct GNUNET_GNSRECORD_Block *block, 474 const struct GNUNET_GNSRECORD_Block *block,
472 GNUNET_NAMECACHE_ContinuationWithStatus cont, 475 GNUNET_NAMECACHE_ContinuationWithStatus cont,
473 void *cont_cls) 476 void *cont_cls)
474{ 477{
475 struct GNUNET_NAMECACHE_QueueEntry *qe; 478 struct GNUNET_NAMECACHE_QueueEntry *qe;
476 struct BlockCacheMessage *msg; 479 struct BlockCacheMessage *msg;
@@ -480,33 +483,33 @@ GNUNET_NAMECACHE_block_cache(struct GNUNET_NAMECACHE_Handle *h,
480 483
481 if (NULL == h->mq) 484 if (NULL == h->mq)
482 return NULL; 485 return NULL;
483 blen = ntohl(block->purpose.size); 486 blen = ntohl (block->purpose.size);
484 GNUNET_assert(blen > (sizeof(struct GNUNET_TIME_AbsoluteNBO) + 487 GNUNET_assert (blen > (sizeof(struct GNUNET_TIME_AbsoluteNBO)
485 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose))); 488 + sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)));
486 blen -= (sizeof(struct GNUNET_TIME_AbsoluteNBO) + 489 blen -= (sizeof(struct GNUNET_TIME_AbsoluteNBO)
487 sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose)); 490 + sizeof(struct GNUNET_CRYPTO_EccSignaturePurpose));
488 rid = get_op_id(h); 491 rid = get_op_id (h);
489 qe = GNUNET_new(struct GNUNET_NAMECACHE_QueueEntry); 492 qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry);
490 qe->nsh = h; 493 qe->nsh = h;
491 qe->cont = cont; 494 qe->cont = cont;
492 qe->cont_cls = cont_cls; 495 qe->cont_cls = cont_cls;
493 qe->op_id = rid; 496 qe->op_id = rid;
494 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, 497 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
495 h->op_tail, 498 h->op_tail,
496 qe); 499 qe);
497 /* send msg */ 500 /* send msg */
498 env = GNUNET_MQ_msg_extra(msg, 501 env = GNUNET_MQ_msg_extra (msg,
499 blen, 502 blen,
500 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE); 503 GNUNET_MESSAGE_TYPE_NAMECACHE_BLOCK_CACHE);
501 msg->gns_header.r_id = htonl(rid); 504 msg->gns_header.r_id = htonl (rid);
502 msg->expire = block->expiration_time; 505 msg->expire = block->expiration_time;
503 msg->signature = block->signature; 506 msg->signature = block->signature;
504 msg->derived_key = block->derived_key; 507 msg->derived_key = block->derived_key;
505 GNUNET_memcpy(&msg[1], 508 GNUNET_memcpy (&msg[1],
506 &block[1], 509 &block[1],
507 blen); 510 blen);
508 GNUNET_MQ_send(h->mq, 511 GNUNET_MQ_send (h->mq,
509 env); 512 env);
510 return qe; 513 return qe;
511} 514}
512 515
@@ -523,10 +526,10 @@ GNUNET_NAMECACHE_block_cache(struct GNUNET_NAMECACHE_Handle *h,
523 * @return a handle that can be used to cancel, NULL on error 526 * @return a handle that can be used to cancel, NULL on error
524 */ 527 */
525struct GNUNET_NAMECACHE_QueueEntry * 528struct GNUNET_NAMECACHE_QueueEntry *
526GNUNET_NAMECACHE_lookup_block(struct GNUNET_NAMECACHE_Handle *h, 529GNUNET_NAMECACHE_lookup_block (struct GNUNET_NAMECACHE_Handle *h,
527 const struct GNUNET_HashCode *derived_hash, 530 const struct GNUNET_HashCode *derived_hash,
528 GNUNET_NAMECACHE_BlockProcessor proc, 531 GNUNET_NAMECACHE_BlockProcessor proc,
529 void *proc_cls) 532 void *proc_cls)
530{ 533{
531 struct GNUNET_NAMECACHE_QueueEntry *qe; 534 struct GNUNET_NAMECACHE_QueueEntry *qe;
532 struct LookupBlockMessage *msg; 535 struct LookupBlockMessage *msg;
@@ -535,24 +538,24 @@ GNUNET_NAMECACHE_lookup_block(struct GNUNET_NAMECACHE_Handle *h,
535 538
536 if (NULL == h->mq) 539 if (NULL == h->mq)
537 return NULL; 540 return NULL;
538 LOG(GNUNET_ERROR_TYPE_DEBUG, 541 LOG (GNUNET_ERROR_TYPE_DEBUG,
539 "Looking for block under %s\n", 542 "Looking for block under %s\n",
540 GNUNET_h2s(derived_hash)); 543 GNUNET_h2s (derived_hash));
541 rid = get_op_id(h); 544 rid = get_op_id (h);
542 qe = GNUNET_new(struct GNUNET_NAMECACHE_QueueEntry); 545 qe = GNUNET_new (struct GNUNET_NAMECACHE_QueueEntry);
543 qe->nsh = h; 546 qe->nsh = h;
544 qe->block_proc = proc; 547 qe->block_proc = proc;
545 qe->block_proc_cls = proc_cls; 548 qe->block_proc_cls = proc_cls;
546 qe->op_id = rid; 549 qe->op_id = rid;
547 GNUNET_CONTAINER_DLL_insert_tail(h->op_head, 550 GNUNET_CONTAINER_DLL_insert_tail (h->op_head,
548 h->op_tail, 551 h->op_tail,
549 qe); 552 qe);
550 env = GNUNET_MQ_msg(msg, 553 env = GNUNET_MQ_msg (msg,
551 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK); 554 GNUNET_MESSAGE_TYPE_NAMECACHE_LOOKUP_BLOCK);
552 msg->gns_header.r_id = htonl(rid); 555 msg->gns_header.r_id = htonl (rid);
553 msg->query = *derived_hash; 556 msg->query = *derived_hash;
554 GNUNET_MQ_send(h->mq, 557 GNUNET_MQ_send (h->mq,
555 env); 558 env);
556 return qe; 559 return qe;
557} 560}
558 561
@@ -564,14 +567,14 @@ GNUNET_NAMECACHE_lookup_block(struct GNUNET_NAMECACHE_Handle *h,
564 * @param qe operation to cancel 567 * @param qe operation to cancel
565 */ 568 */
566void 569void
567GNUNET_NAMECACHE_cancel(struct GNUNET_NAMECACHE_QueueEntry *qe) 570GNUNET_NAMECACHE_cancel (struct GNUNET_NAMECACHE_QueueEntry *qe)
568{ 571{
569 struct GNUNET_NAMECACHE_Handle *h = qe->nsh; 572 struct GNUNET_NAMECACHE_Handle *h = qe->nsh;
570 573
571 GNUNET_CONTAINER_DLL_remove(h->op_head, 574 GNUNET_CONTAINER_DLL_remove (h->op_head,
572 h->op_tail, 575 h->op_tail,
573 qe); 576 qe);
574 GNUNET_free(qe); 577 GNUNET_free (qe);
575} 578}
576 579
577 580
diff --git a/src/namecache/plugin_namecache_flat.c b/src/namecache/plugin_namecache_flat.c
index c48056a29..14406c13d 100644
--- a/src/namecache/plugin_namecache_flat.c
+++ b/src/namecache/plugin_namecache_flat.c
@@ -33,7 +33,8 @@
33/** 33/**
34 * Context for all functions in this plugin. 34 * Context for all functions in this plugin.
35 */ 35 */
36struct Plugin { 36struct Plugin
37{
37 const struct GNUNET_CONFIGURATION_Handle *cfg; 38 const struct GNUNET_CONFIGURATION_Handle *cfg;
38 39
39 /** 40 /**
@@ -47,7 +48,8 @@ struct Plugin {
47 struct GNUNET_CONTAINER_MultiHashMap *hm; 48 struct GNUNET_CONTAINER_MultiHashMap *hm;
48}; 49};
49 50
50struct FlatFileEntry { 51struct FlatFileEntry
52{
51 /** 53 /**
52 * Block 54 * Block
53 */ 55 */
@@ -68,121 +70,121 @@ struct FlatFileEntry {
68 * @return #GNUNET_OK on success 70 * @return #GNUNET_OK on success
69 */ 71 */
70static int 72static int
71database_setup(struct Plugin *plugin) 73database_setup (struct Plugin *plugin)
72{ 74{
73 char *afsdir; 75 char *afsdir;
74 char* block_buffer; 76 char*block_buffer;
75 char* buffer; 77 char*buffer;
76 char* line; 78 char*line;
77 char* query; 79 char*query;
78 char* block; 80 char*block;
79 size_t size; 81 size_t size;
80 struct FlatFileEntry *entry; 82 struct FlatFileEntry *entry;
81 struct GNUNET_DISK_FileHandle *fh; 83 struct GNUNET_DISK_FileHandle *fh;
82 84
83 if (GNUNET_OK != 85 if (GNUNET_OK !=
84 GNUNET_CONFIGURATION_get_value_filename(plugin->cfg, 86 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
85 "namecache-flat", 87 "namecache-flat",
86 "FILENAME", 88 "FILENAME",
87 &afsdir)) 89 &afsdir))
90 {
91 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
92 "namecache-flat", "FILENAME");
93 return GNUNET_SYSERR;
94 }
95 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir))
96 {
97 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir))
88 { 98 {
89 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, 99 GNUNET_break (0);
90 "namecache-flat", "FILENAME"); 100 GNUNET_free (afsdir);
91 return GNUNET_SYSERR; 101 return GNUNET_SYSERR;
92 } 102 }
93 if (GNUNET_OK != GNUNET_DISK_file_test(afsdir)) 103 }
94 {
95 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file(afsdir))
96 {
97 GNUNET_break(0);
98 GNUNET_free(afsdir);
99 return GNUNET_SYSERR;
100 }
101 }
102 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */ 104 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
103 plugin->fn = afsdir; 105 plugin->fn = afsdir;
104 106
105 /* Load data from file into hashmap */ 107 /* Load data from file into hashmap */
106 plugin->hm = GNUNET_CONTAINER_multihashmap_create(10, 108 plugin->hm = GNUNET_CONTAINER_multihashmap_create (10,
107 GNUNET_NO); 109 GNUNET_NO);
108 fh = GNUNET_DISK_file_open(afsdir, 110 fh = GNUNET_DISK_file_open (afsdir,
109 GNUNET_DISK_OPEN_CREATE | 111 GNUNET_DISK_OPEN_CREATE
110 GNUNET_DISK_OPEN_READWRITE, 112 | GNUNET_DISK_OPEN_READWRITE,
111 GNUNET_DISK_PERM_USER_WRITE | 113 GNUNET_DISK_PERM_USER_WRITE
112 GNUNET_DISK_PERM_USER_READ); 114 | GNUNET_DISK_PERM_USER_READ);
113 if (NULL == fh) 115 if (NULL == fh)
114 { 116 {
115 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 _("Unable to initialize file: %s.\n"), 118 _ ("Unable to initialize file: %s.\n"),
117 afsdir); 119 afsdir);
118 return GNUNET_SYSERR; 120 return GNUNET_SYSERR;
119 } 121 }
120 122
121 if (GNUNET_SYSERR == GNUNET_DISK_file_size(afsdir, 123 if (GNUNET_SYSERR == GNUNET_DISK_file_size (afsdir,
122 &size, 124 &size,
123 GNUNET_YES, 125 GNUNET_YES,
124 GNUNET_YES)) 126 GNUNET_YES))
125 { 127 {
126 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
127 _("Unable to get filesize: %s.\n"), 129 _ ("Unable to get filesize: %s.\n"),
128 afsdir); 130 afsdir);
129 GNUNET_DISK_file_close(fh); 131 GNUNET_DISK_file_close (fh);
130 return GNUNET_SYSERR; 132 return GNUNET_SYSERR;
131 } 133 }
132 134
133 if (0 == size) 135 if (0 == size)
134 { 136 {
135 GNUNET_DISK_file_close(fh); 137 GNUNET_DISK_file_close (fh);
136 return GNUNET_OK; 138 return GNUNET_OK;
137 } 139 }
138 140
139 buffer = GNUNET_malloc(size + 1); 141 buffer = GNUNET_malloc (size + 1);
140 142
141 if (GNUNET_SYSERR == GNUNET_DISK_file_read(fh, 143 if (GNUNET_SYSERR == GNUNET_DISK_file_read (fh,
142 buffer, 144 buffer,
143 size)) 145 size))
144 { 146 {
145 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 147 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
146 _("Unable to read file: %s.\n"), 148 _ ("Unable to read file: %s.\n"),
147 afsdir); 149 afsdir);
148 GNUNET_free(buffer); 150 GNUNET_free (buffer);
149 GNUNET_DISK_file_close(fh); 151 GNUNET_DISK_file_close (fh);
150 return GNUNET_SYSERR; 152 return GNUNET_SYSERR;
151 } 153 }
152 buffer[size] = '\0'; 154 buffer[size] = '\0';
153 155
154 GNUNET_DISK_file_close(fh); 156 GNUNET_DISK_file_close (fh);
155 if (0 < size) 157 if (0 < size)
158 {
159 line = strtok (buffer, "\n");
160 while (line != NULL)
156 { 161 {
157 line = strtok(buffer, "\n"); 162 query = strtok (line, ",");
158 while (line != NULL) 163 if (NULL == query)
159 { 164 break;
160 query = strtok(line, ","); 165 block = strtok (NULL, ",");
161 if (NULL == query) 166 if (NULL == block)
162 break; 167 break;
163 block = strtok(NULL, ","); 168 line = strtok (NULL, "\n");
164 if (NULL == block) 169 entry = GNUNET_malloc (sizeof(struct FlatFileEntry));
165 break; 170 GNUNET_assert (GNUNET_OK == GNUNET_CRYPTO_hash_from_string (query,
166 line = strtok(NULL, "\n"); 171 &entry->query));
167 entry = GNUNET_malloc(sizeof(struct FlatFileEntry)); 172 GNUNET_STRINGS_base64_decode (block,
168 GNUNET_assert(GNUNET_OK == GNUNET_CRYPTO_hash_from_string(query, 173 strlen (block),
169 &entry->query)); 174 (void**) &block_buffer);
170 GNUNET_STRINGS_base64_decode(block, 175 entry->block = (struct GNUNET_GNSRECORD_Block *) block_buffer;
171 strlen(block), 176 if (GNUNET_OK !=
172 (void**)&block_buffer); 177 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
173 entry->block = (struct GNUNET_GNSRECORD_Block *)block_buffer; 178 &entry->query,
174 if (GNUNET_OK != 179 entry,
175 GNUNET_CONTAINER_multihashmap_put(plugin->hm, 180 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
176 &entry->query, 181 {
177 entry, 182 GNUNET_free (entry);
178 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 183 GNUNET_break (0);
179 { 184 }
180 GNUNET_free(entry);
181 GNUNET_break(0);
182 }
183 }
184 } 185 }
185 GNUNET_free(buffer); 186 }
187 GNUNET_free (buffer);
186 return GNUNET_OK; 188 return GNUNET_OK;
187} 189}
188 190
@@ -192,9 +194,9 @@ database_setup(struct Plugin *plugin)
192 * @param plugin the plugin context 194 * @param plugin the plugin context
193 */ 195 */
194static int 196static int
195store_and_free_entries(void *cls, 197store_and_free_entries (void *cls,
196 const struct GNUNET_HashCode *key, 198 const struct GNUNET_HashCode *key,
197 void *value) 199 void *value)
198{ 200{
199 struct GNUNET_DISK_FileHandle *fh = cls; 201 struct GNUNET_DISK_FileHandle *fh = cls;
200 struct FlatFileEntry *entry = value; 202 struct FlatFileEntry *entry = value;
@@ -204,29 +206,29 @@ store_and_free_entries(void *cls,
204 struct GNUNET_CRYPTO_HashAsciiEncoded query; 206 struct GNUNET_CRYPTO_HashAsciiEncoded query;
205 size_t block_size; 207 size_t block_size;
206 208
207 block_size = ntohl(entry->block->purpose.size) + 209 block_size = ntohl (entry->block->purpose.size)
208 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) + 210 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
209 sizeof(struct GNUNET_CRYPTO_EcdsaSignature); 211 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
210 212
211 GNUNET_STRINGS_base64_encode((char*)entry->block, 213 GNUNET_STRINGS_base64_encode ((char*) entry->block,
212 block_size, 214 block_size,
213 &block_b64); 215 &block_b64);
214 GNUNET_CRYPTO_hash_to_enc(&entry->query, 216 GNUNET_CRYPTO_hash_to_enc (&entry->query,
215 &query); 217 &query);
216 GNUNET_asprintf(&line, 218 GNUNET_asprintf (&line,
217 "%s,%s\n", 219 "%s,%s\n",
218 (char*)&query, 220 (char*) &query,
219 block_b64); 221 block_b64);
220 222
221 GNUNET_free(block_b64); 223 GNUNET_free (block_b64);
222 224
223 GNUNET_DISK_file_write(fh, 225 GNUNET_DISK_file_write (fh,
224 line, 226 line,
225 strlen(line)); 227 strlen (line));
226 228
227 GNUNET_free(entry->block); 229 GNUNET_free (entry->block);
228 GNUNET_free(entry); 230 GNUNET_free (entry);
229 GNUNET_free(line); 231 GNUNET_free (line);
230 return GNUNET_YES; 232 return GNUNET_YES;
231} 233}
232 234
@@ -236,49 +238,49 @@ store_and_free_entries(void *cls,
236 * @param plugin the plugin context (state for this module) 238 * @param plugin the plugin context (state for this module)
237 */ 239 */
238static void 240static void
239database_shutdown(struct Plugin *plugin) 241database_shutdown (struct Plugin *plugin)
240{ 242{
241 struct GNUNET_DISK_FileHandle *fh; 243 struct GNUNET_DISK_FileHandle *fh;
242 244
243 fh = GNUNET_DISK_file_open(plugin->fn, 245 fh = GNUNET_DISK_file_open (plugin->fn,
244 GNUNET_DISK_OPEN_CREATE | 246 GNUNET_DISK_OPEN_CREATE
245 GNUNET_DISK_OPEN_TRUNCATE | 247 | GNUNET_DISK_OPEN_TRUNCATE
246 GNUNET_DISK_OPEN_READWRITE, 248 | GNUNET_DISK_OPEN_READWRITE,
247 GNUNET_DISK_PERM_USER_WRITE | 249 GNUNET_DISK_PERM_USER_WRITE
248 GNUNET_DISK_PERM_USER_READ); 250 | GNUNET_DISK_PERM_USER_READ);
249 if (NULL == fh) 251 if (NULL == fh)
250 { 252 {
251 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
252 _("Unable to initialize file: %s.\n"), 254 _ ("Unable to initialize file: %s.\n"),
253 plugin->fn); 255 plugin->fn);
254 return; 256 return;
255 } 257 }
256 258
257 GNUNET_CONTAINER_multihashmap_iterate(plugin->hm, 259 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
258 &store_and_free_entries, 260 &store_and_free_entries,
259 fh); 261 fh);
260 GNUNET_CONTAINER_multihashmap_destroy(plugin->hm); 262 GNUNET_CONTAINER_multihashmap_destroy (plugin->hm);
261 GNUNET_DISK_file_close(fh); 263 GNUNET_DISK_file_close (fh);
262} 264}
263 265
264static int 266static int
265expire_blocks(void *cls, 267expire_blocks (void *cls,
266 const struct GNUNET_HashCode *key, 268 const struct GNUNET_HashCode *key,
267 void *value) 269 void *value)
268{ 270{
269 struct Plugin *plugin = cls; 271 struct Plugin *plugin = cls;
270 struct FlatFileEntry *entry = value; 272 struct FlatFileEntry *entry = value;
271 struct GNUNET_TIME_Absolute now; 273 struct GNUNET_TIME_Absolute now;
272 struct GNUNET_TIME_Absolute expiration; 274 struct GNUNET_TIME_Absolute expiration;
273 275
274 now = GNUNET_TIME_absolute_get(); 276 now = GNUNET_TIME_absolute_get ();
275 expiration = GNUNET_TIME_absolute_ntoh(entry->block->expiration_time); 277 expiration = GNUNET_TIME_absolute_ntoh (entry->block->expiration_time);
276 278
277 if (0 == GNUNET_TIME_absolute_get_difference(now, 279 if (0 == GNUNET_TIME_absolute_get_difference (now,
278 expiration).rel_value_us) 280 expiration).rel_value_us)
279 { 281 {
280 GNUNET_CONTAINER_multihashmap_remove_all(plugin->hm, key); 282 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, key);
281 } 283 }
282 return GNUNET_YES; 284 return GNUNET_YES;
283} 285}
284 286
@@ -290,11 +292,11 @@ expire_blocks(void *cls,
290 * @param plugin the plugin 292 * @param plugin the plugin
291 */ 293 */
292static void 294static void
293namecache_expire_blocks(struct Plugin *plugin) 295namecache_expire_blocks (struct Plugin *plugin)
294{ 296{
295 GNUNET_CONTAINER_multihashmap_iterate(plugin->hm, 297 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
296 &expire_blocks, 298 &expire_blocks,
297 plugin); 299 plugin);
298} 300}
299 301
300 302
@@ -306,43 +308,43 @@ namecache_expire_blocks(struct Plugin *plugin)
306 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 308 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
307 */ 309 */
308static int 310static int
309namecache_cache_block(void *cls, 311namecache_cache_block (void *cls,
310 const struct GNUNET_GNSRECORD_Block *block) 312 const struct GNUNET_GNSRECORD_Block *block)
311{ 313{
312 struct Plugin *plugin = cls; 314 struct Plugin *plugin = cls;
313 struct GNUNET_HashCode query; 315 struct GNUNET_HashCode query;
314 struct FlatFileEntry *entry; 316 struct FlatFileEntry *entry;
315 size_t block_size; 317 size_t block_size;
316 318
317 namecache_expire_blocks(plugin); 319 namecache_expire_blocks (plugin);
318 GNUNET_CRYPTO_hash(&block->derived_key, 320 GNUNET_CRYPTO_hash (&block->derived_key,
319 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey), 321 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
320 &query); 322 &query);
321 block_size = ntohl(block->purpose.size) + 323 block_size = ntohl (block->purpose.size)
322 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) + 324 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
323 sizeof(struct GNUNET_CRYPTO_EcdsaSignature); 325 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
324 if (block_size > 64 * 65536) 326 if (block_size > 64 * 65536)
325 { 327 {
326 GNUNET_break(0); 328 GNUNET_break (0);
327 return GNUNET_SYSERR; 329 return GNUNET_SYSERR;
328 } 330 }
329 entry = GNUNET_malloc(sizeof(struct FlatFileEntry)); 331 entry = GNUNET_malloc (sizeof(struct FlatFileEntry));
330 entry->block = GNUNET_malloc(block_size); 332 entry->block = GNUNET_malloc (block_size);
331 GNUNET_memcpy(entry->block, block, block_size); 333 GNUNET_memcpy (entry->block, block, block_size);
332 GNUNET_CONTAINER_multihashmap_remove_all(plugin->hm, &query); 334 GNUNET_CONTAINER_multihashmap_remove_all (plugin->hm, &query);
333 if (GNUNET_OK != 335 if (GNUNET_OK !=
334 GNUNET_CONTAINER_multihashmap_put(plugin->hm, 336 GNUNET_CONTAINER_multihashmap_put (plugin->hm,
335 &query, 337 &query,
336 entry, 338 entry,
337 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY)) 339 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY))
338 { 340 {
339 GNUNET_free(entry); 341 GNUNET_free (entry);
340 GNUNET_break(0); 342 GNUNET_break (0);
341 return GNUNET_SYSERR; 343 return GNUNET_SYSERR;
342 } 344 }
343 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
344 "Caching block under derived key `%s'\n", 346 "Caching block under derived key `%s'\n",
345 GNUNET_h2s_full(&query)); 347 GNUNET_h2s_full (&query));
346 return GNUNET_OK; 348 return GNUNET_OK;
347} 349}
348 350
@@ -358,20 +360,20 @@ namecache_cache_block(void *cls,
358 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 360 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
359 */ 361 */
360static int 362static int
361namecache_lookup_block(void *cls, 363namecache_lookup_block (void *cls,
362 const struct GNUNET_HashCode *query, 364 const struct GNUNET_HashCode *query,
363 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls) 365 GNUNET_NAMECACHE_BlockCallback iter, void *iter_cls)
364{ 366{
365 struct Plugin *plugin = cls; 367 struct Plugin *plugin = cls;
366 const struct GNUNET_GNSRECORD_Block *block; 368 const struct GNUNET_GNSRECORD_Block *block;
367 369
368 block = GNUNET_CONTAINER_multihashmap_get(plugin->hm, query); 370 block = GNUNET_CONTAINER_multihashmap_get (plugin->hm, query);
369 if (NULL == block) 371 if (NULL == block)
370 return GNUNET_NO; 372 return GNUNET_NO;
371 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
372 "Found block under derived key `%s'\n", 374 "Found block under derived key `%s'\n",
373 GNUNET_h2s_full(query)); 375 GNUNET_h2s_full (query));
374 iter(iter_cls, block); 376 iter (iter_cls, block);
375 return GNUNET_YES; 377 return GNUNET_YES;
376} 378}
377 379
@@ -383,7 +385,7 @@ namecache_lookup_block(void *cls,
383 * @return NULL on error, otherwise the plugin context 385 * @return NULL on error, otherwise the plugin context
384 */ 386 */
385void * 387void *
386libgnunet_plugin_namecache_flat_init(void *cls) 388libgnunet_plugin_namecache_flat_init (void *cls)
387{ 389{
388 static struct Plugin plugin; 390 static struct Plugin plugin;
389 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 391 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
@@ -391,19 +393,19 @@ libgnunet_plugin_namecache_flat_init(void *cls)
391 393
392 if (NULL != plugin.cfg) 394 if (NULL != plugin.cfg)
393 return NULL; /* can only initialize once! */ 395 return NULL; /* can only initialize once! */
394 memset(&plugin, 0, sizeof(struct Plugin)); 396 memset (&plugin, 0, sizeof(struct Plugin));
395 plugin.cfg = cfg; 397 plugin.cfg = cfg;
396 if (GNUNET_OK != database_setup(&plugin)) 398 if (GNUNET_OK != database_setup (&plugin))
397 { 399 {
398 database_shutdown(&plugin); 400 database_shutdown (&plugin);
399 return NULL; 401 return NULL;
400 } 402 }
401 api = GNUNET_new(struct GNUNET_NAMECACHE_PluginFunctions); 403 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
402 api->cls = &plugin; 404 api->cls = &plugin;
403 api->cache_block = &namecache_cache_block; 405 api->cache_block = &namecache_cache_block;
404 api->lookup_block = &namecache_lookup_block; 406 api->lookup_block = &namecache_lookup_block;
405 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
406 _("flat plugin running\n")); 408 _ ("flat plugin running\n"));
407 return api; 409 return api;
408} 410}
409 411
@@ -415,16 +417,16 @@ libgnunet_plugin_namecache_flat_init(void *cls)
415 * @return always NULL 417 * @return always NULL
416 */ 418 */
417void * 419void *
418libgnunet_plugin_namecache_flat_done(void *cls) 420libgnunet_plugin_namecache_flat_done (void *cls)
419{ 421{
420 struct GNUNET_NAMECACHE_PluginFunctions *api = cls; 422 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
421 struct Plugin *plugin = api->cls; 423 struct Plugin *plugin = api->cls;
422 424
423 database_shutdown(plugin); 425 database_shutdown (plugin);
424 plugin->cfg = NULL; 426 plugin->cfg = NULL;
425 GNUNET_free(api); 427 GNUNET_free (api);
426 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
427 "flat plugin is finished\n"); 429 "flat plugin is finished\n");
428 return NULL; 430 return NULL;
429} 431}
430 432
diff --git a/src/namecache/plugin_namecache_postgres.c b/src/namecache/plugin_namecache_postgres.c
index 6d0e4e918..e4b360ef2 100644
--- a/src/namecache/plugin_namecache_postgres.c
+++ b/src/namecache/plugin_namecache_postgres.c
@@ -31,13 +31,14 @@
31#include "namecache.h" 31#include "namecache.h"
32 32
33 33
34#define LOG(kind, ...) GNUNET_log_from(kind, "namecache-postgres", __VA_ARGS__) 34#define LOG(kind, ...) GNUNET_log_from (kind, "namecache-postgres", __VA_ARGS__)
35 35
36 36
37/** 37/**
38 * Context for all functions in this plugin. 38 * Context for all functions in this plugin.
39 */ 39 */
40struct Plugin { 40struct Plugin
41{
41 const struct GNUNET_CONFIGURATION_Handle *cfg; 42 const struct GNUNET_CONFIGURATION_Handle *cfg;
42 43
43 /** 44 /**
@@ -56,81 +57,85 @@ struct Plugin {
56 * @return #GNUNET_OK on success 57 * @return #GNUNET_OK on success
57 */ 58 */
58static int 59static int
59database_setup(struct Plugin *plugin) 60database_setup (struct Plugin *plugin)
60{ 61{
61 struct GNUNET_PQ_ExecuteStatement es_temporary = 62 struct GNUNET_PQ_ExecuteStatement es_temporary =
62 GNUNET_PQ_make_execute("CREATE TEMPORARY TABLE IF NOT EXISTS ns096blocks (" 63 GNUNET_PQ_make_execute ("CREATE TEMPORARY TABLE IF NOT EXISTS ns096blocks ("
63 " query BYTEA NOT NULL DEFAULT ''," 64 " query BYTEA NOT NULL DEFAULT '',"
64 " block BYTEA NOT NULL DEFAULT ''," 65 " block BYTEA NOT NULL DEFAULT '',"
65 " expiration_time BIGINT NOT NULL DEFAULT 0" 66 " expiration_time BIGINT NOT NULL DEFAULT 0"
66 ")" 67 ")"
67 "WITH OIDS"); 68 "WITH OIDS");
68 struct GNUNET_PQ_ExecuteStatement es_default = 69 struct GNUNET_PQ_ExecuteStatement es_default =
69 GNUNET_PQ_make_execute("CREATE TABLE IF NOT EXISTS ns096blocks (" 70 GNUNET_PQ_make_execute ("CREATE TABLE IF NOT EXISTS ns096blocks ("
70 " query BYTEA NOT NULL DEFAULT ''," 71 " query BYTEA NOT NULL DEFAULT '',"
71 " block BYTEA NOT NULL DEFAULT ''," 72 " block BYTEA NOT NULL DEFAULT '',"
72 " expiration_time BIGINT NOT NULL DEFAULT 0" 73 " expiration_time BIGINT NOT NULL DEFAULT 0"
73 ")" 74 ")"
74 "WITH OIDS"); 75 "WITH OIDS");
75 const struct GNUNET_PQ_ExecuteStatement *cr; 76 const struct GNUNET_PQ_ExecuteStatement *cr;
76 77
77 plugin->dbh = GNUNET_PQ_connect_with_cfg(plugin->cfg, 78 plugin->dbh = GNUNET_PQ_connect_with_cfg (plugin->cfg,
78 "namecache-postgres"); 79 "namecache-postgres");
79 if (NULL == plugin->dbh) 80 if (NULL == plugin->dbh)
80 return GNUNET_SYSERR; 81 return GNUNET_SYSERR;
81 if (GNUNET_YES == 82 if (GNUNET_YES ==
82 GNUNET_CONFIGURATION_get_value_yesno(plugin->cfg, 83 GNUNET_CONFIGURATION_get_value_yesno (plugin->cfg,
83 "namecache-postgres", 84 "namecache-postgres",
84 "TEMPORARY_TABLE")) 85 "TEMPORARY_TABLE"))
85 { 86 {
86 cr = &es_temporary; 87 cr = &es_temporary;
87 } 88 }
88 else 89 else
89 { 90 {
90 cr = &es_default; 91 cr = &es_default;
91 } 92 }
92 93
93 { 94 {
94 struct GNUNET_PQ_ExecuteStatement es[] = { 95 struct GNUNET_PQ_ExecuteStatement es[] = {
95 *cr, 96 *cr,
96 GNUNET_PQ_make_try_execute("CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)"), 97 GNUNET_PQ_make_try_execute (
97 GNUNET_PQ_make_try_execute("CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)"), 98 "CREATE INDEX ir_query_hash ON ns096blocks (query,expiration_time)"),
99 GNUNET_PQ_make_try_execute (
100 "CREATE INDEX ir_block_expiration ON ns096blocks (expiration_time)"),
98 GNUNET_PQ_EXECUTE_STATEMENT_END 101 GNUNET_PQ_EXECUTE_STATEMENT_END
99 }; 102 };
100 103
101 if (GNUNET_OK != 104 if (GNUNET_OK !=
102 GNUNET_PQ_exec_statements(plugin->dbh, 105 GNUNET_PQ_exec_statements (plugin->dbh,
103 es)) 106 es))
104 { 107 {
105 PQfinish(plugin->dbh); 108 PQfinish (plugin->dbh);
106 plugin->dbh = NULL; 109 plugin->dbh = NULL;
107 return GNUNET_SYSERR; 110 return GNUNET_SYSERR;
108 } 111 }
109 } 112 }
110 113
111 { 114 {
112 struct GNUNET_PQ_PreparedStatement ps[] = { 115 struct GNUNET_PQ_PreparedStatement ps[] = {
113 GNUNET_PQ_make_prepare("cache_block", 116 GNUNET_PQ_make_prepare ("cache_block",
114 "INSERT INTO ns096blocks (query, block, expiration_time) VALUES " 117 "INSERT INTO ns096blocks (query, block, expiration_time) VALUES "
115 "($1, $2, $3)", 3), 118 "($1, $2, $3)", 3),
116 GNUNET_PQ_make_prepare("expire_blocks", 119 GNUNET_PQ_make_prepare ("expire_blocks",
117 "DELETE FROM ns096blocks WHERE expiration_time<$1", 1), 120 "DELETE FROM ns096blocks WHERE expiration_time<$1",
118 GNUNET_PQ_make_prepare("delete_block", 121 1),
119 "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2", 2), 122 GNUNET_PQ_make_prepare ("delete_block",
120 GNUNET_PQ_make_prepare("lookup_block", 123 "DELETE FROM ns096blocks WHERE query=$1 AND expiration_time<=$2",
121 "SELECT block FROM ns096blocks WHERE query=$1" 124 2),
122 " ORDER BY expiration_time DESC LIMIT 1", 1), 125 GNUNET_PQ_make_prepare ("lookup_block",
126 "SELECT block FROM ns096blocks WHERE query=$1"
127 " ORDER BY expiration_time DESC LIMIT 1", 1),
123 GNUNET_PQ_PREPARED_STATEMENT_END 128 GNUNET_PQ_PREPARED_STATEMENT_END
124 }; 129 };
125 130
126 if (GNUNET_OK != 131 if (GNUNET_OK !=
127 GNUNET_PQ_prepare_statements(plugin->dbh, 132 GNUNET_PQ_prepare_statements (plugin->dbh,
128 ps)) 133 ps))
129 { 134 {
130 PQfinish(plugin->dbh); 135 PQfinish (plugin->dbh);
131 plugin->dbh = NULL; 136 plugin->dbh = NULL;
132 return GNUNET_SYSERR; 137 return GNUNET_SYSERR;
133 } 138 }
134 } 139 }
135 140
136 return GNUNET_OK; 141 return GNUNET_OK;
@@ -143,19 +148,19 @@ database_setup(struct Plugin *plugin)
143 * @param plugin the plugin 148 * @param plugin the plugin
144 */ 149 */
145static void 150static void
146namecache_postgres_expire_blocks(struct Plugin *plugin) 151namecache_postgres_expire_blocks (struct Plugin *plugin)
147{ 152{
148 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get(); 153 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
149 struct GNUNET_PQ_QueryParam params[] = { 154 struct GNUNET_PQ_QueryParam params[] = {
150 GNUNET_PQ_query_param_absolute_time(&now), 155 GNUNET_PQ_query_param_absolute_time (&now),
151 GNUNET_PQ_query_param_end 156 GNUNET_PQ_query_param_end
152 }; 157 };
153 enum GNUNET_DB_QueryStatus res; 158 enum GNUNET_DB_QueryStatus res;
154 159
155 res = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, 160 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
156 "expire_blocks", 161 "expire_blocks",
157 params); 162 params);
158 GNUNET_break(GNUNET_DB_STATUS_HARD_ERROR != res); 163 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != res);
159} 164}
160 165
161 166
@@ -167,21 +172,21 @@ namecache_postgres_expire_blocks(struct Plugin *plugin)
167 * @param expiration_time how old does the block have to be for deletion 172 * @param expiration_time how old does the block have to be for deletion
168 */ 173 */
169static void 174static void
170delete_old_block(struct Plugin *plugin, 175delete_old_block (struct Plugin *plugin,
171 const struct GNUNET_HashCode *query, 176 const struct GNUNET_HashCode *query,
172 struct GNUNET_TIME_AbsoluteNBO expiration_time) 177 struct GNUNET_TIME_AbsoluteNBO expiration_time)
173{ 178{
174 struct GNUNET_PQ_QueryParam params[] = { 179 struct GNUNET_PQ_QueryParam params[] = {
175 GNUNET_PQ_query_param_auto_from_type(query), 180 GNUNET_PQ_query_param_auto_from_type (query),
176 GNUNET_PQ_query_param_absolute_time_nbo(&expiration_time), 181 GNUNET_PQ_query_param_absolute_time_nbo (&expiration_time),
177 GNUNET_PQ_query_param_end 182 GNUNET_PQ_query_param_end
178 }; 183 };
179 enum GNUNET_DB_QueryStatus res; 184 enum GNUNET_DB_QueryStatus res;
180 185
181 res = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, 186 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
182 "delete_block", 187 "delete_block",
183 params); 188 params);
184 GNUNET_break(GNUNET_DB_STATUS_HARD_ERROR != res); 189 GNUNET_break (GNUNET_DB_STATUS_HARD_ERROR != res);
185} 190}
186 191
187 192
@@ -193,38 +198,38 @@ delete_old_block(struct Plugin *plugin,
193 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 198 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
194 */ 199 */
195static int 200static int
196namecache_postgres_cache_block(void *cls, 201namecache_postgres_cache_block (void *cls,
197 const struct GNUNET_GNSRECORD_Block *block) 202 const struct GNUNET_GNSRECORD_Block *block)
198{ 203{
199 struct Plugin *plugin = cls; 204 struct Plugin *plugin = cls;
200 struct GNUNET_HashCode query; 205 struct GNUNET_HashCode query;
201 size_t block_size = ntohl(block->purpose.size) + 206 size_t block_size = ntohl (block->purpose.size)
202 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) + 207 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
203 sizeof(struct GNUNET_CRYPTO_EcdsaSignature); 208 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
204 struct GNUNET_PQ_QueryParam params[] = { 209 struct GNUNET_PQ_QueryParam params[] = {
205 GNUNET_PQ_query_param_auto_from_type(&query), 210 GNUNET_PQ_query_param_auto_from_type (&query),
206 GNUNET_PQ_query_param_fixed_size(block, block_size), 211 GNUNET_PQ_query_param_fixed_size (block, block_size),
207 GNUNET_PQ_query_param_absolute_time_nbo(&block->expiration_time), 212 GNUNET_PQ_query_param_absolute_time_nbo (&block->expiration_time),
208 GNUNET_PQ_query_param_end 213 GNUNET_PQ_query_param_end
209 }; 214 };
210 enum GNUNET_DB_QueryStatus res; 215 enum GNUNET_DB_QueryStatus res;
211 216
212 namecache_postgres_expire_blocks(plugin); 217 namecache_postgres_expire_blocks (plugin);
213 GNUNET_CRYPTO_hash(&block->derived_key, 218 GNUNET_CRYPTO_hash (&block->derived_key,
214 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey), 219 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
215 &query); 220 &query);
216 if (block_size > 64 * 65536) 221 if (block_size > 64 * 65536)
217 { 222 {
218 GNUNET_break(0); 223 GNUNET_break (0);
219 return GNUNET_SYSERR; 224 return GNUNET_SYSERR;
220 } 225 }
221 delete_old_block(plugin, 226 delete_old_block (plugin,
222 &query, 227 &query,
223 block->expiration_time); 228 block->expiration_time);
224 229
225 res = GNUNET_PQ_eval_prepared_non_select(plugin->dbh, 230 res = GNUNET_PQ_eval_prepared_non_select (plugin->dbh,
226 "cache_block", 231 "cache_block",
227 params); 232 params);
228 if (0 > res) 233 if (0 > res)
229 return GNUNET_SYSERR; 234 return GNUNET_SYSERR;
230 return GNUNET_OK; 235 return GNUNET_OK;
@@ -242,57 +247,57 @@ namecache_postgres_cache_block(void *cls,
242 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 247 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
243 */ 248 */
244static int 249static int
245namecache_postgres_lookup_block(void *cls, 250namecache_postgres_lookup_block (void *cls,
246 const struct GNUNET_HashCode *query, 251 const struct GNUNET_HashCode *query,
247 GNUNET_NAMECACHE_BlockCallback iter, 252 GNUNET_NAMECACHE_BlockCallback iter,
248 void *iter_cls) 253 void *iter_cls)
249{ 254{
250 struct Plugin *plugin = cls; 255 struct Plugin *plugin = cls;
251 size_t bsize; 256 size_t bsize;
252 struct GNUNET_GNSRECORD_Block *block; 257 struct GNUNET_GNSRECORD_Block *block;
253 struct GNUNET_PQ_QueryParam params[] = { 258 struct GNUNET_PQ_QueryParam params[] = {
254 GNUNET_PQ_query_param_auto_from_type(query), 259 GNUNET_PQ_query_param_auto_from_type (query),
255 GNUNET_PQ_query_param_end 260 GNUNET_PQ_query_param_end
256 }; 261 };
257 struct GNUNET_PQ_ResultSpec rs[] = { 262 struct GNUNET_PQ_ResultSpec rs[] = {
258 GNUNET_PQ_result_spec_variable_size("block", 263 GNUNET_PQ_result_spec_variable_size ("block",
259 (void **)&block, 264 (void **) &block,
260 &bsize), 265 &bsize),
261 GNUNET_PQ_result_spec_end 266 GNUNET_PQ_result_spec_end
262 }; 267 };
263 enum GNUNET_DB_QueryStatus res; 268 enum GNUNET_DB_QueryStatus res;
264 269
265 res = GNUNET_PQ_eval_prepared_singleton_select(plugin->dbh, 270 res = GNUNET_PQ_eval_prepared_singleton_select (plugin->dbh,
266 "lookup_block", 271 "lookup_block",
267 params, 272 params,
268 rs); 273 rs);
269 if (0 > res) 274 if (0 > res)
270 { 275 {
271 LOG(GNUNET_ERROR_TYPE_WARNING, 276 LOG (GNUNET_ERROR_TYPE_WARNING,
272 "Failing lookup block in namecache (postgres error)\n"); 277 "Failing lookup block in namecache (postgres error)\n");
273 return GNUNET_SYSERR; 278 return GNUNET_SYSERR;
274 } 279 }
275 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res) 280 if (GNUNET_DB_STATUS_SUCCESS_NO_RESULTS == res)
276 { 281 {
277 /* no result */ 282 /* no result */
278 LOG(GNUNET_ERROR_TYPE_DEBUG, 283 LOG (GNUNET_ERROR_TYPE_DEBUG,
279 "Ending iteration (no more results)\n"); 284 "Ending iteration (no more results)\n");
280 return GNUNET_NO; 285 return GNUNET_NO;
281 } 286 }
282 if ((bsize < sizeof(*block)) || 287 if ((bsize < sizeof(*block)) ||
283 (bsize != ntohl(block->purpose.size) + 288 (bsize != ntohl (block->purpose.size)
284 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) + 289 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
285 sizeof(struct GNUNET_CRYPTO_EcdsaSignature))) 290 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature)))
286 { 291 {
287 GNUNET_break(0); 292 GNUNET_break (0);
288 LOG(GNUNET_ERROR_TYPE_DEBUG, 293 LOG (GNUNET_ERROR_TYPE_DEBUG,
289 "Failing lookup (corrupt block)\n"); 294 "Failing lookup (corrupt block)\n");
290 GNUNET_PQ_cleanup_result(rs); 295 GNUNET_PQ_cleanup_result (rs);
291 return GNUNET_SYSERR; 296 return GNUNET_SYSERR;
292 } 297 }
293 iter(iter_cls, 298 iter (iter_cls,
294 block); 299 block);
295 GNUNET_PQ_cleanup_result(rs); 300 GNUNET_PQ_cleanup_result (rs);
296 return GNUNET_OK; 301 return GNUNET_OK;
297} 302}
298 303
@@ -304,9 +309,9 @@ namecache_postgres_lookup_block(void *cls,
304 * @param plugin the plugin context (state for this module) 309 * @param plugin the plugin context (state for this module)
305 */ 310 */
306static void 311static void
307database_shutdown(struct Plugin *plugin) 312database_shutdown (struct Plugin *plugin)
308{ 313{
309 PQfinish(plugin->dbh); 314 PQfinish (plugin->dbh);
310 plugin->dbh = NULL; 315 plugin->dbh = NULL;
311} 316}
312 317
@@ -318,7 +323,7 @@ database_shutdown(struct Plugin *plugin)
318 * @return NULL on error, otherwise the plugin context 323 * @return NULL on error, otherwise the plugin context
319 */ 324 */
320void * 325void *
321libgnunet_plugin_namecache_postgres_init(void *cls) 326libgnunet_plugin_namecache_postgres_init (void *cls)
322{ 327{
323 static struct Plugin plugin; 328 static struct Plugin plugin;
324 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 329 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
@@ -326,19 +331,19 @@ libgnunet_plugin_namecache_postgres_init(void *cls)
326 331
327 if (NULL != plugin.cfg) 332 if (NULL != plugin.cfg)
328 return NULL; /* can only initialize once! */ 333 return NULL; /* can only initialize once! */
329 memset(&plugin, 0, sizeof(struct Plugin)); 334 memset (&plugin, 0, sizeof(struct Plugin));
330 plugin.cfg = cfg; 335 plugin.cfg = cfg;
331 if (GNUNET_OK != database_setup(&plugin)) 336 if (GNUNET_OK != database_setup (&plugin))
332 { 337 {
333 database_shutdown(&plugin); 338 database_shutdown (&plugin);
334 return NULL; 339 return NULL;
335 } 340 }
336 api = GNUNET_new(struct GNUNET_NAMECACHE_PluginFunctions); 341 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
337 api->cls = &plugin; 342 api->cls = &plugin;
338 api->cache_block = &namecache_postgres_cache_block; 343 api->cache_block = &namecache_postgres_cache_block;
339 api->lookup_block = &namecache_postgres_lookup_block; 344 api->lookup_block = &namecache_postgres_lookup_block;
340 LOG(GNUNET_ERROR_TYPE_INFO, 345 LOG (GNUNET_ERROR_TYPE_INFO,
341 "Postgres namecache plugin running\n"); 346 "Postgres namecache plugin running\n");
342 return api; 347 return api;
343} 348}
344 349
@@ -350,16 +355,16 @@ libgnunet_plugin_namecache_postgres_init(void *cls)
350 * @return always NULL 355 * @return always NULL
351 */ 356 */
352void * 357void *
353libgnunet_plugin_namecache_postgres_done(void *cls) 358libgnunet_plugin_namecache_postgres_done (void *cls)
354{ 359{
355 struct GNUNET_NAMECACHE_PluginFunctions *api = cls; 360 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
356 struct Plugin *plugin = api->cls; 361 struct Plugin *plugin = api->cls;
357 362
358 database_shutdown(plugin); 363 database_shutdown (plugin);
359 plugin->cfg = NULL; 364 plugin->cfg = NULL;
360 GNUNET_free(api); 365 GNUNET_free (api);
361 LOG(GNUNET_ERROR_TYPE_DEBUG, 366 LOG (GNUNET_ERROR_TYPE_DEBUG,
362 "Postgres namecache plugin is finished\n"); 367 "Postgres namecache plugin is finished\n");
363 return NULL; 368 return NULL;
364} 369}
365 370
diff --git a/src/namecache/plugin_namecache_sqlite.c b/src/namecache/plugin_namecache_sqlite.c
index e52438b4e..ee3ec7eab 100644
--- a/src/namecache/plugin_namecache_sqlite.c
+++ b/src/namecache/plugin_namecache_sqlite.c
@@ -49,15 +49,23 @@
49 * a failure of the command 'cmd' on file 'filename' 49 * a failure of the command 'cmd' on file 'filename'
50 * with the message given by strerror(errno). 50 * with the message given by strerror(errno).
51 */ 51 */
52#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from(level, "namecache-sqlite", _("`%s' failed at %s:%d with error: %s\n"), cmd, __FILE__, __LINE__, sqlite3_errmsg(db->dbh)); } while (0) 52#define LOG_SQLITE(db, level, cmd) do { GNUNET_log_from (level, \
53 "namecache-sqlite", _ ( \
54 "`%s' failed at %s:%d with error: %s\n"), \
55 cmd, \
56 __FILE__, __LINE__, \
57 sqlite3_errmsg ( \
58 db->dbh)); \
59} while (0)
53 60
54#define LOG(kind, ...) GNUNET_log_from(kind, "namecache-sqlite", __VA_ARGS__) 61#define LOG(kind, ...) GNUNET_log_from (kind, "namecache-sqlite", __VA_ARGS__)
55 62
56 63
57/** 64/**
58 * Context for all functions in this plugin. 65 * Context for all functions in this plugin.
59 */ 66 */
60struct Plugin { 67struct Plugin
68{
61 const struct GNUNET_CONFIGURATION_Handle *cfg; 69 const struct GNUNET_CONFIGURATION_Handle *cfg;
62 70
63 /** 71 /**
@@ -101,100 +109,102 @@ struct Plugin {
101 * @return #GNUNET_OK on success 109 * @return #GNUNET_OK on success
102 */ 110 */
103static int 111static int
104database_setup(struct Plugin *plugin) 112database_setup (struct Plugin *plugin)
105{ 113{
106 struct GNUNET_SQ_ExecuteStatement es[] = { 114 struct GNUNET_SQ_ExecuteStatement es[] = {
107 GNUNET_SQ_make_try_execute("PRAGMA temp_store=MEMORY"), 115 GNUNET_SQ_make_try_execute ("PRAGMA temp_store=MEMORY"),
108 GNUNET_SQ_make_try_execute("PRAGMA synchronous=NORMAL"), 116 GNUNET_SQ_make_try_execute ("PRAGMA synchronous=NORMAL"),
109 GNUNET_SQ_make_try_execute("PRAGMA legacy_file_format=OFF"), 117 GNUNET_SQ_make_try_execute ("PRAGMA legacy_file_format=OFF"),
110 GNUNET_SQ_make_try_execute("PRAGMA auto_vacuum=INCREMENTAL"), 118 GNUNET_SQ_make_try_execute ("PRAGMA auto_vacuum=INCREMENTAL"),
111 GNUNET_SQ_make_try_execute("PRAGMA encoding=\"UTF-8\""), 119 GNUNET_SQ_make_try_execute ("PRAGMA encoding=\"UTF-8\""),
112 GNUNET_SQ_make_try_execute("PRAGMA locking_mode=EXCLUSIVE"), 120 GNUNET_SQ_make_try_execute ("PRAGMA locking_mode=EXCLUSIVE"),
113 GNUNET_SQ_make_try_execute("PRAGMA page_size=4092"), 121 GNUNET_SQ_make_try_execute ("PRAGMA page_size=4092"),
114 GNUNET_SQ_make_try_execute("PRAGMA journal_mode=WAL"), 122 GNUNET_SQ_make_try_execute ("PRAGMA journal_mode=WAL"),
115 GNUNET_SQ_make_execute("CREATE TABLE IF NOT EXISTS ns096blocks (" 123 GNUNET_SQ_make_execute ("CREATE TABLE IF NOT EXISTS ns096blocks ("
116 " query BLOB NOT NULL," 124 " query BLOB NOT NULL,"
117 " block BLOB NOT NULL," 125 " block BLOB NOT NULL,"
118 " expiration_time INT8 NOT NULL" 126 " expiration_time INT8 NOT NULL"
119 ")"), 127 ")"),
120 GNUNET_SQ_make_execute("CREATE INDEX IF NOT EXISTS ir_query_hash " 128 GNUNET_SQ_make_execute ("CREATE INDEX IF NOT EXISTS ir_query_hash "
121 "ON ns096blocks (query,expiration_time)"), 129 "ON ns096blocks (query,expiration_time)"),
122 GNUNET_SQ_make_execute("CREATE INDEX IF NOT EXISTS ir_block_expiration " 130 GNUNET_SQ_make_execute ("CREATE INDEX IF NOT EXISTS ir_block_expiration "
123 "ON ns096blocks (expiration_time)"), 131 "ON ns096blocks (expiration_time)"),
124 GNUNET_SQ_EXECUTE_STATEMENT_END 132 GNUNET_SQ_EXECUTE_STATEMENT_END
125 }; 133 };
126 struct GNUNET_SQ_PrepareStatement ps[] = { 134 struct GNUNET_SQ_PrepareStatement ps[] = {
127 GNUNET_SQ_make_prepare("INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)", 135 GNUNET_SQ_make_prepare (
128 &plugin->cache_block), 136 "INSERT INTO ns096blocks (query,block,expiration_time) VALUES (?, ?, ?)",
129 GNUNET_SQ_make_prepare("DELETE FROM ns096blocks WHERE expiration_time<?", 137 &plugin->cache_block),
130 &plugin->expire_blocks), 138 GNUNET_SQ_make_prepare ("DELETE FROM ns096blocks WHERE expiration_time<?",
131 GNUNET_SQ_make_prepare("DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?", 139 &plugin->expire_blocks),
132 &plugin->delete_block), 140 GNUNET_SQ_make_prepare (
133 GNUNET_SQ_make_prepare("SELECT block FROM ns096blocks WHERE query=? " 141 "DELETE FROM ns096blocks WHERE query=? AND expiration_time<=?",
134 "ORDER BY expiration_time DESC LIMIT 1", 142 &plugin->delete_block),
135 &plugin->lookup_block), 143 GNUNET_SQ_make_prepare ("SELECT block FROM ns096blocks WHERE query=? "
144 "ORDER BY expiration_time DESC LIMIT 1",
145 &plugin->lookup_block),
136 GNUNET_SQ_PREPARE_END 146 GNUNET_SQ_PREPARE_END
137 }; 147 };
138 char *afsdir; 148 char *afsdir;
139 149
140 if (GNUNET_OK != 150 if (GNUNET_OK !=
141 GNUNET_CONFIGURATION_get_value_filename(plugin->cfg, 151 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
142 "namecache-sqlite", 152 "namecache-sqlite",
143 "FILENAME", 153 "FILENAME",
144 &afsdir)) 154 &afsdir))
145 { 155 {
146 GNUNET_log_config_missing(GNUNET_ERROR_TYPE_ERROR, 156 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
147 "namecache-sqlite", 157 "namecache-sqlite",
148 "FILENAME"); 158 "FILENAME");
149 return GNUNET_SYSERR; 159 return GNUNET_SYSERR;
150 } 160 }
151 if (GNUNET_OK != 161 if (GNUNET_OK !=
152 GNUNET_DISK_file_test(afsdir)) 162 GNUNET_DISK_file_test (afsdir))
163 {
164 if (GNUNET_OK !=
165 GNUNET_DISK_directory_create_for_file (afsdir))
153 { 166 {
154 if (GNUNET_OK != 167 GNUNET_break (0);
155 GNUNET_DISK_directory_create_for_file(afsdir)) 168 GNUNET_free (afsdir);
156 { 169 return GNUNET_SYSERR;
157 GNUNET_break(0);
158 GNUNET_free(afsdir);
159 return GNUNET_SYSERR;
160 }
161 } 170 }
171 }
162 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */ 172 /* afsdir should be UTF-8-encoded. If it isn't, it's a bug */
163 plugin->fn = afsdir; 173 plugin->fn = afsdir;
164 174
165 /* Open database and precompile statements */ 175 /* Open database and precompile statements */
166 if (SQLITE_OK != 176 if (SQLITE_OK !=
167 sqlite3_open(plugin->fn, &plugin->dbh)) 177 sqlite3_open (plugin->fn, &plugin->dbh))
168 { 178 {
169 LOG(GNUNET_ERROR_TYPE_ERROR, 179 LOG (GNUNET_ERROR_TYPE_ERROR,
170 _("Unable to initialize SQLite: %s.\n"), 180 _ ("Unable to initialize SQLite: %s.\n"),
171 sqlite3_errmsg(plugin->dbh)); 181 sqlite3_errmsg (plugin->dbh));
172 return GNUNET_SYSERR; 182 return GNUNET_SYSERR;
173 } 183 }
174 if (GNUNET_OK != 184 if (GNUNET_OK !=
175 GNUNET_SQ_exec_statements(plugin->dbh, 185 GNUNET_SQ_exec_statements (plugin->dbh,
176 es)) 186 es))
177 { 187 {
178 GNUNET_break(0); 188 GNUNET_break (0);
179 LOG(GNUNET_ERROR_TYPE_ERROR, 189 LOG (GNUNET_ERROR_TYPE_ERROR,
180 _("Failed to setup database at `%s'\n"), 190 _ ("Failed to setup database at `%s'\n"),
181 plugin->fn); 191 plugin->fn);
182 return GNUNET_SYSERR; 192 return GNUNET_SYSERR;
183 } 193 }
184 GNUNET_break(SQLITE_OK == 194 GNUNET_break (SQLITE_OK ==
185 sqlite3_busy_timeout(plugin->dbh, 195 sqlite3_busy_timeout (plugin->dbh,
186 BUSY_TIMEOUT_MS)); 196 BUSY_TIMEOUT_MS));
187 197
188 if (GNUNET_OK != 198 if (GNUNET_OK !=
189 GNUNET_SQ_prepare(plugin->dbh, 199 GNUNET_SQ_prepare (plugin->dbh,
190 ps)) 200 ps))
191 { 201 {
192 GNUNET_break(0); 202 GNUNET_break (0);
193 LOG(GNUNET_ERROR_TYPE_ERROR, 203 LOG (GNUNET_ERROR_TYPE_ERROR,
194 _("Failed to setup database at `%s'\n"), 204 _ ("Failed to setup database at `%s'\n"),
195 plugin->fn); 205 plugin->fn);
196 return GNUNET_SYSERR; 206 return GNUNET_SYSERR;
197 } 207 }
198 208
199 return GNUNET_OK; 209 return GNUNET_OK;
200} 210}
@@ -206,50 +216,51 @@ database_setup(struct Plugin *plugin)
206 * @param plugin the plugin context (state for this module) 216 * @param plugin the plugin context (state for this module)
207 */ 217 */
208static void 218static void
209database_shutdown(struct Plugin *plugin) 219database_shutdown (struct Plugin *plugin)
210{ 220{
211 int result; 221 int result;
212 sqlite3_stmt *stmt; 222 sqlite3_stmt *stmt;
213 223
214 if (NULL != plugin->cache_block) 224 if (NULL != plugin->cache_block)
215 sqlite3_finalize(plugin->cache_block); 225 sqlite3_finalize (plugin->cache_block);
216 if (NULL != plugin->lookup_block) 226 if (NULL != plugin->lookup_block)
217 sqlite3_finalize(plugin->lookup_block); 227 sqlite3_finalize (plugin->lookup_block);
218 if (NULL != plugin->expire_blocks) 228 if (NULL != plugin->expire_blocks)
219 sqlite3_finalize(plugin->expire_blocks); 229 sqlite3_finalize (plugin->expire_blocks);
220 if (NULL != plugin->delete_block) 230 if (NULL != plugin->delete_block)
221 sqlite3_finalize(plugin->delete_block); 231 sqlite3_finalize (plugin->delete_block);
222 result = sqlite3_close(plugin->dbh); 232 result = sqlite3_close (plugin->dbh);
223 if (result == SQLITE_BUSY) 233 if (result == SQLITE_BUSY)
234 {
235 LOG (GNUNET_ERROR_TYPE_WARNING,
236 _ (
237 "Tried to close sqlite without finalizing all prepared statements.\n"));
238 stmt = sqlite3_next_stmt (plugin->dbh,
239 NULL);
240 while (stmt != NULL)
224 { 241 {
225 LOG(GNUNET_ERROR_TYPE_WARNING, 242 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
226 _("Tried to close sqlite without finalizing all prepared statements.\n")); 243 "sqlite",
227 stmt = sqlite3_next_stmt(plugin->dbh, 244 "Closing statement %p\n",
228 NULL); 245 stmt);
229 while (stmt != NULL) 246 result = sqlite3_finalize (stmt);
230 { 247 if (result != SQLITE_OK)
231 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, 248 GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING,
232 "sqlite", 249 "sqlite",
233 "Closing statement %p\n", 250 "Failed to close statement %p: %d\n",
234 stmt); 251 stmt,
235 result = sqlite3_finalize(stmt); 252 result);
236 if (result != SQLITE_OK) 253 stmt = sqlite3_next_stmt (plugin->dbh,
237 GNUNET_log_from(GNUNET_ERROR_TYPE_WARNING, 254 NULL);
238 "sqlite",
239 "Failed to close statement %p: %d\n",
240 stmt,
241 result);
242 stmt = sqlite3_next_stmt(plugin->dbh,
243 NULL);
244 }
245 result = sqlite3_close(plugin->dbh);
246 } 255 }
256 result = sqlite3_close (plugin->dbh);
257 }
247 if (SQLITE_OK != result) 258 if (SQLITE_OK != result)
248 LOG_SQLITE(plugin, 259 LOG_SQLITE (plugin,
249 GNUNET_ERROR_TYPE_ERROR, 260 GNUNET_ERROR_TYPE_ERROR,
250 "sqlite3_close"); 261 "sqlite3_close");
251 262
252 GNUNET_free_non_null(plugin->fn); 263 GNUNET_free_non_null (plugin->fn);
253} 264}
254 265
255 266
@@ -259,50 +270,50 @@ database_shutdown(struct Plugin *plugin)
259 * @param plugin the plugin 270 * @param plugin the plugin
260 */ 271 */
261static void 272static void
262namecache_sqlite_expire_blocks(struct Plugin *plugin) 273namecache_sqlite_expire_blocks (struct Plugin *plugin)
263{ 274{
264 struct GNUNET_TIME_Absolute now; 275 struct GNUNET_TIME_Absolute now;
265 struct GNUNET_SQ_QueryParam params[] = { 276 struct GNUNET_SQ_QueryParam params[] = {
266 GNUNET_SQ_query_param_absolute_time(&now), 277 GNUNET_SQ_query_param_absolute_time (&now),
267 GNUNET_SQ_query_param_end 278 GNUNET_SQ_query_param_end
268 }; 279 };
269 int n; 280 int n;
270 281
271 now = GNUNET_TIME_absolute_get(); 282 now = GNUNET_TIME_absolute_get ();
272 if (GNUNET_OK != 283 if (GNUNET_OK !=
273 GNUNET_SQ_bind(plugin->expire_blocks, 284 GNUNET_SQ_bind (plugin->expire_blocks,
274 params)) 285 params))
275 { 286 {
276 LOG_SQLITE(plugin, 287 LOG_SQLITE (plugin,
277 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 288 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
278 "sqlite3_bind_XXXX"); 289 "sqlite3_bind_XXXX");
279 GNUNET_SQ_reset(plugin->dbh, 290 GNUNET_SQ_reset (plugin->dbh,
280 plugin->expire_blocks); 291 plugin->expire_blocks);
281 return; 292 return;
282 } 293 }
283 n = sqlite3_step(plugin->expire_blocks); 294 n = sqlite3_step (plugin->expire_blocks);
284 GNUNET_SQ_reset(plugin->dbh, 295 GNUNET_SQ_reset (plugin->dbh,
285 plugin->expire_blocks); 296 plugin->expire_blocks);
286 switch (n) 297 switch (n)
287 { 298 {
288 case SQLITE_DONE: 299 case SQLITE_DONE:
289 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, 300 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
290 "sqlite", 301 "sqlite",
291 "Records expired\n"); 302 "Records expired\n");
292 return; 303 return;
293 304
294 case SQLITE_BUSY: 305 case SQLITE_BUSY:
295 LOG_SQLITE(plugin, 306 LOG_SQLITE (plugin,
296 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 307 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
297 "sqlite3_step"); 308 "sqlite3_step");
298 return; 309 return;
299 310
300 default: 311 default:
301 LOG_SQLITE(plugin, 312 LOG_SQLITE (plugin,
302 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 313 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
303 "sqlite3_step"); 314 "sqlite3_step");
304 return; 315 return;
305 } 316 }
306} 317}
307 318
308 319
@@ -314,123 +325,123 @@ namecache_sqlite_expire_blocks(struct Plugin *plugin)
314 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 325 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
315 */ 326 */
316static int 327static int
317namecache_sqlite_cache_block(void *cls, 328namecache_sqlite_cache_block (void *cls,
318 const struct GNUNET_GNSRECORD_Block *block) 329 const struct GNUNET_GNSRECORD_Block *block)
319{ 330{
320 static struct GNUNET_TIME_Absolute last_expire; 331 static struct GNUNET_TIME_Absolute last_expire;
321 struct Plugin *plugin = cls; 332 struct Plugin *plugin = cls;
322 struct GNUNET_HashCode query; 333 struct GNUNET_HashCode query;
323 struct GNUNET_TIME_Absolute expiration; 334 struct GNUNET_TIME_Absolute expiration;
324 size_t block_size = ntohl(block->purpose.size) + 335 size_t block_size = ntohl (block->purpose.size)
325 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) + 336 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
326 sizeof(struct GNUNET_CRYPTO_EcdsaSignature); 337 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature);
327 struct GNUNET_SQ_QueryParam del_params[] = { 338 struct GNUNET_SQ_QueryParam del_params[] = {
328 GNUNET_SQ_query_param_auto_from_type(&query), 339 GNUNET_SQ_query_param_auto_from_type (&query),
329 GNUNET_SQ_query_param_absolute_time(&expiration), 340 GNUNET_SQ_query_param_absolute_time (&expiration),
330 GNUNET_SQ_query_param_end 341 GNUNET_SQ_query_param_end
331 }; 342 };
332 struct GNUNET_SQ_QueryParam ins_params[] = { 343 struct GNUNET_SQ_QueryParam ins_params[] = {
333 GNUNET_SQ_query_param_auto_from_type(&query), 344 GNUNET_SQ_query_param_auto_from_type (&query),
334 GNUNET_SQ_query_param_fixed_size(block, 345 GNUNET_SQ_query_param_fixed_size (block,
335 block_size), 346 block_size),
336 GNUNET_SQ_query_param_absolute_time(&expiration), 347 GNUNET_SQ_query_param_absolute_time (&expiration),
337 GNUNET_SQ_query_param_end 348 GNUNET_SQ_query_param_end
338 }; 349 };
339 int n; 350 int n;
340 351
341 /* run expiration of old cache entries once per hour */ 352 /* run expiration of old cache entries once per hour */
342 if (GNUNET_TIME_absolute_get_duration(last_expire).rel_value_us > 353 if (GNUNET_TIME_absolute_get_duration (last_expire).rel_value_us >
343 GNUNET_TIME_UNIT_HOURS.rel_value_us) 354 GNUNET_TIME_UNIT_HOURS.rel_value_us)
344 { 355 {
345 last_expire = GNUNET_TIME_absolute_get(); 356 last_expire = GNUNET_TIME_absolute_get ();
346 namecache_sqlite_expire_blocks(plugin); 357 namecache_sqlite_expire_blocks (plugin);
347 } 358 }
348 GNUNET_CRYPTO_hash(&block->derived_key, 359 GNUNET_CRYPTO_hash (&block->derived_key,
349 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey), 360 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey),
350 &query); 361 &query);
351 expiration = GNUNET_TIME_absolute_ntoh(block->expiration_time); 362 expiration = GNUNET_TIME_absolute_ntoh (block->expiration_time);
352 GNUNET_log(GNUNET_ERROR_TYPE_INFO, 363 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
353 "Caching new version of block %s (expires %s)\n", 364 "Caching new version of block %s (expires %s)\n",
354 GNUNET_h2s(&query), 365 GNUNET_h2s (&query),
355 GNUNET_STRINGS_absolute_time_to_string(expiration)); 366 GNUNET_STRINGS_absolute_time_to_string (expiration));
356 if (block_size > 64 * 65536) 367 if (block_size > 64 * 65536)
357 { 368 {
358 GNUNET_break(0); 369 GNUNET_break (0);
359 return GNUNET_SYSERR; 370 return GNUNET_SYSERR;
360 } 371 }
361 372
362 /* delete old version of the block */ 373 /* delete old version of the block */
363 if (GNUNET_OK != 374 if (GNUNET_OK !=
364 GNUNET_SQ_bind(plugin->delete_block, 375 GNUNET_SQ_bind (plugin->delete_block,
365 del_params)) 376 del_params))
366 { 377 {
367 LOG_SQLITE(plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 378 LOG_SQLITE (plugin, GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
368 "sqlite3_bind_XXXX"); 379 "sqlite3_bind_XXXX");
369 GNUNET_SQ_reset(plugin->dbh, 380 GNUNET_SQ_reset (plugin->dbh,
370 plugin->delete_block); 381 plugin->delete_block);
371 return GNUNET_SYSERR; 382 return GNUNET_SYSERR;
372 } 383 }
373 n = sqlite3_step(plugin->delete_block); 384 n = sqlite3_step (plugin->delete_block);
374 switch (n) 385 switch (n)
375 { 386 {
376 case SQLITE_DONE: 387 case SQLITE_DONE:
377 GNUNET_log_from(GNUNET_ERROR_TYPE_DEBUG, 388 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
378 "sqlite", 389 "sqlite",
379 "Old block deleted\n"); 390 "Old block deleted\n");
380 break; 391 break;
381 392
382 case SQLITE_BUSY: 393 case SQLITE_BUSY:
383 LOG_SQLITE(plugin, 394 LOG_SQLITE (plugin,
384 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 395 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
385 "sqlite3_step"); 396 "sqlite3_step");
386 break; 397 break;
387 398
388 default: 399 default:
389 LOG_SQLITE(plugin, 400 LOG_SQLITE (plugin,
390 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 401 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
391 "sqlite3_step"); 402 "sqlite3_step");
392 break; 403 break;
393 } 404 }
394 GNUNET_SQ_reset(plugin->dbh, 405 GNUNET_SQ_reset (plugin->dbh,
395 plugin->delete_block); 406 plugin->delete_block);
396 407
397 /* insert new version of the block */ 408 /* insert new version of the block */
398 if (GNUNET_OK != 409 if (GNUNET_OK !=
399 GNUNET_SQ_bind(plugin->cache_block, 410 GNUNET_SQ_bind (plugin->cache_block,
400 ins_params)) 411 ins_params))
401 { 412 {
402 LOG_SQLITE(plugin, 413 LOG_SQLITE (plugin,
403 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 414 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
404 "sqlite3_bind_XXXX"); 415 "sqlite3_bind_XXXX");
405 GNUNET_SQ_reset(plugin->dbh, 416 GNUNET_SQ_reset (plugin->dbh,
406 plugin->cache_block); 417 plugin->cache_block);
407 return GNUNET_SYSERR; 418 return GNUNET_SYSERR;
408 } 419 }
409 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
410 "Caching block under derived key `%s'\n", 421 "Caching block under derived key `%s'\n",
411 GNUNET_h2s_full(&query)); 422 GNUNET_h2s_full (&query));
412 n = sqlite3_step(plugin->cache_block); 423 n = sqlite3_step (plugin->cache_block);
413 GNUNET_SQ_reset(plugin->dbh, 424 GNUNET_SQ_reset (plugin->dbh,
414 plugin->cache_block); 425 plugin->cache_block);
415 switch (n) 426 switch (n)
416 { 427 {
417 case SQLITE_DONE: 428 case SQLITE_DONE:
418 LOG(GNUNET_ERROR_TYPE_DEBUG, 429 LOG (GNUNET_ERROR_TYPE_DEBUG,
419 "Record stored\n"); 430 "Record stored\n");
420 return GNUNET_OK; 431 return GNUNET_OK;
421 432
422 case SQLITE_BUSY: 433 case SQLITE_BUSY:
423 LOG_SQLITE(plugin, 434 LOG_SQLITE (plugin,
424 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK, 435 GNUNET_ERROR_TYPE_WARNING | GNUNET_ERROR_TYPE_BULK,
425 "sqlite3_step"); 436 "sqlite3_step");
426 return GNUNET_NO; 437 return GNUNET_NO;
427 438
428 default: 439 default:
429 LOG_SQLITE(plugin, 440 LOG_SQLITE (plugin,
430 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 441 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
431 "sqlite3_step"); 442 "sqlite3_step");
432 return GNUNET_SYSERR; 443 return GNUNET_SYSERR;
433 } 444 }
434} 445}
435 446
436 447
@@ -445,10 +456,10 @@ namecache_sqlite_cache_block(void *cls,
445 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 456 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
446 */ 457 */
447static int 458static int
448namecache_sqlite_lookup_block(void *cls, 459namecache_sqlite_lookup_block (void *cls,
449 const struct GNUNET_HashCode *query, 460 const struct GNUNET_HashCode *query,
450 GNUNET_NAMECACHE_BlockCallback iter, 461 GNUNET_NAMECACHE_BlockCallback iter,
451 void *iter_cls) 462 void *iter_cls)
452{ 463{
453 struct Plugin *plugin = cls; 464 struct Plugin *plugin = cls;
454 int ret; 465 int ret;
@@ -456,75 +467,75 @@ namecache_sqlite_lookup_block(void *cls,
456 size_t block_size; 467 size_t block_size;
457 const struct GNUNET_GNSRECORD_Block *block; 468 const struct GNUNET_GNSRECORD_Block *block;
458 struct GNUNET_SQ_QueryParam params[] = { 469 struct GNUNET_SQ_QueryParam params[] = {
459 GNUNET_SQ_query_param_auto_from_type(query), 470 GNUNET_SQ_query_param_auto_from_type (query),
460 GNUNET_SQ_query_param_end 471 GNUNET_SQ_query_param_end
461 }; 472 };
462 struct GNUNET_SQ_ResultSpec rs[] = { 473 struct GNUNET_SQ_ResultSpec rs[] = {
463 GNUNET_SQ_result_spec_variable_size((void **)&block, 474 GNUNET_SQ_result_spec_variable_size ((void **) &block,
464 &block_size), 475 &block_size),
465 GNUNET_SQ_result_spec_end 476 GNUNET_SQ_result_spec_end
466 }; 477 };
467 478
468 if (GNUNET_OK != 479 if (GNUNET_OK !=
469 GNUNET_SQ_bind(plugin->lookup_block, 480 GNUNET_SQ_bind (plugin->lookup_block,
470 params)) 481 params))
471 { 482 {
472 LOG_SQLITE(plugin, 483 LOG_SQLITE (plugin,
473 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK, 484 GNUNET_ERROR_TYPE_ERROR | GNUNET_ERROR_TYPE_BULK,
474 "sqlite3_bind_XXXX"); 485 "sqlite3_bind_XXXX");
475 GNUNET_SQ_reset(plugin->dbh, 486 GNUNET_SQ_reset (plugin->dbh,
476 plugin->lookup_block); 487 plugin->lookup_block);
477 return GNUNET_SYSERR; 488 return GNUNET_SYSERR;
478 } 489 }
479 ret = GNUNET_NO; 490 ret = GNUNET_NO;
480 if (SQLITE_ROW == 491 if (SQLITE_ROW ==
481 (sret = sqlite3_step(plugin->lookup_block))) 492 (sret = sqlite3_step (plugin->lookup_block)))
493 {
494 if (GNUNET_OK !=
495 GNUNET_SQ_extract_result (plugin->lookup_block,
496 rs))
482 { 497 {
483 if (GNUNET_OK != 498 GNUNET_break (0);
484 GNUNET_SQ_extract_result(plugin->lookup_block, 499 ret = GNUNET_SYSERR;
485 rs))
486 {
487 GNUNET_break(0);
488 ret = GNUNET_SYSERR;
489 }
490 else if ((block_size < sizeof(struct GNUNET_GNSRECORD_Block)) ||
491 (ntohl(block->purpose.size) +
492 sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey) +
493 sizeof(struct GNUNET_CRYPTO_EcdsaSignature) != block_size))
494 {
495 GNUNET_break(0);
496 GNUNET_SQ_cleanup_result(rs);
497 ret = GNUNET_SYSERR;
498 }
499 else
500 {
501 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
502 "Found block under derived key `%s'\n",
503 GNUNET_h2s_full(query));
504 iter(iter_cls,
505 block);
506 GNUNET_SQ_cleanup_result(rs);
507 ret = GNUNET_YES;
508 }
509 } 500 }
501 else if ((block_size < sizeof(struct GNUNET_GNSRECORD_Block)) ||
502 (ntohl (block->purpose.size)
503 + sizeof(struct GNUNET_CRYPTO_EcdsaPublicKey)
504 + sizeof(struct GNUNET_CRYPTO_EcdsaSignature) != block_size))
505 {
506 GNUNET_break (0);
507 GNUNET_SQ_cleanup_result (rs);
508 ret = GNUNET_SYSERR;
509 }
510 else
511 {
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Found block under derived key `%s'\n",
514 GNUNET_h2s_full (query));
515 iter (iter_cls,
516 block);
517 GNUNET_SQ_cleanup_result (rs);
518 ret = GNUNET_YES;
519 }
520 }
510 else 521 else
522 {
523 if (SQLITE_DONE != sret)
524 {
525 LOG_SQLITE (plugin,
526 GNUNET_ERROR_TYPE_ERROR,
527 "sqlite_step");
528 ret = GNUNET_SYSERR;
529 }
530 else
511 { 531 {
512 if (SQLITE_DONE != sret) 532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 { 533 "No block found under derived key `%s'\n",
514 LOG_SQLITE(plugin, 534 GNUNET_h2s_full (query));
515 GNUNET_ERROR_TYPE_ERROR,
516 "sqlite_step");
517 ret = GNUNET_SYSERR;
518 }
519 else
520 {
521 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
522 "No block found under derived key `%s'\n",
523 GNUNET_h2s_full(query));
524 }
525 } 535 }
526 GNUNET_SQ_reset(plugin->dbh, 536 }
527 plugin->lookup_block); 537 GNUNET_SQ_reset (plugin->dbh,
538 plugin->lookup_block);
528 return ret; 539 return ret;
529} 540}
530 541
@@ -536,7 +547,7 @@ namecache_sqlite_lookup_block(void *cls,
536 * @return NULL on error, otherwise the plugin context 547 * @return NULL on error, otherwise the plugin context
537 */ 548 */
538void * 549void *
539libgnunet_plugin_namecache_sqlite_init(void *cls) 550libgnunet_plugin_namecache_sqlite_init (void *cls)
540{ 551{
541 static struct Plugin plugin; 552 static struct Plugin plugin;
542 const struct GNUNET_CONFIGURATION_Handle *cfg = cls; 553 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
@@ -544,19 +555,19 @@ libgnunet_plugin_namecache_sqlite_init(void *cls)
544 555
545 if (NULL != plugin.cfg) 556 if (NULL != plugin.cfg)
546 return NULL; /* can only initialize once! */ 557 return NULL; /* can only initialize once! */
547 memset(&plugin, 0, sizeof(struct Plugin)); 558 memset (&plugin, 0, sizeof(struct Plugin));
548 plugin.cfg = cfg; 559 plugin.cfg = cfg;
549 if (GNUNET_OK != database_setup(&plugin)) 560 if (GNUNET_OK != database_setup (&plugin))
550 { 561 {
551 database_shutdown(&plugin); 562 database_shutdown (&plugin);
552 return NULL; 563 return NULL;
553 } 564 }
554 api = GNUNET_new(struct GNUNET_NAMECACHE_PluginFunctions); 565 api = GNUNET_new (struct GNUNET_NAMECACHE_PluginFunctions);
555 api->cls = &plugin; 566 api->cls = &plugin;
556 api->cache_block = &namecache_sqlite_cache_block; 567 api->cache_block = &namecache_sqlite_cache_block;
557 api->lookup_block = &namecache_sqlite_lookup_block; 568 api->lookup_block = &namecache_sqlite_lookup_block;
558 LOG(GNUNET_ERROR_TYPE_INFO, 569 LOG (GNUNET_ERROR_TYPE_INFO,
559 _("Sqlite database running\n")); 570 _ ("Sqlite database running\n"));
560 return api; 571 return api;
561} 572}
562 573
@@ -568,16 +579,16 @@ libgnunet_plugin_namecache_sqlite_init(void *cls)
568 * @return always NULL 579 * @return always NULL
569 */ 580 */
570void * 581void *
571libgnunet_plugin_namecache_sqlite_done(void *cls) 582libgnunet_plugin_namecache_sqlite_done (void *cls)
572{ 583{
573 struct GNUNET_NAMECACHE_PluginFunctions *api = cls; 584 struct GNUNET_NAMECACHE_PluginFunctions *api = cls;
574 struct Plugin *plugin = api->cls; 585 struct Plugin *plugin = api->cls;
575 586
576 database_shutdown(plugin); 587 database_shutdown (plugin);
577 plugin->cfg = NULL; 588 plugin->cfg = NULL;
578 GNUNET_free(api); 589 GNUNET_free (api);
579 LOG(GNUNET_ERROR_TYPE_DEBUG, 590 LOG (GNUNET_ERROR_TYPE_DEBUG,
580 "sqlite plugin is finished\n"); 591 "sqlite plugin is finished\n");
581 return NULL; 592 return NULL;
582} 593}
583 594
diff --git a/src/namecache/test_namecache_api_cache_block.c b/src/namecache/test_namecache_api_cache_block.c
index def6cedb3..2b25a26f9 100644
--- a/src/namecache/test_namecache_api_cache_block.c
+++ b/src/namecache/test_namecache_api_cache_block.c
@@ -32,12 +32,12 @@
32 32
33#define TEST_RECORD_DATA 'a' 33#define TEST_RECORD_DATA 'a'
34 34
35#define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 100) 35#define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
36 36
37 37
38static struct GNUNET_NAMECACHE_Handle *nsh; 38static struct GNUNET_NAMECACHE_Handle *nsh;
39 39
40static struct GNUNET_SCHEDULER_Task * endbadly_task; 40static struct GNUNET_SCHEDULER_Task *endbadly_task;
41 41
42static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey; 42static struct GNUNET_CRYPTO_EcdsaPrivateKey *privkey;
43 43
@@ -49,19 +49,19 @@ static struct GNUNET_NAMECACHE_QueueEntry *nsqe;
49 49
50 50
51static void 51static void
52cleanup() 52cleanup ()
53{ 53{
54 if (NULL != nsh) 54 if (NULL != nsh)
55 { 55 {
56 GNUNET_NAMECACHE_disconnect(nsh); 56 GNUNET_NAMECACHE_disconnect (nsh);
57 nsh = NULL; 57 nsh = NULL;
58 } 58 }
59 if (NULL != privkey) 59 if (NULL != privkey)
60 { 60 {
61 GNUNET_free(privkey); 61 GNUNET_free (privkey);
62 privkey = NULL; 62 privkey = NULL;
63 } 63 }
64 GNUNET_SCHEDULER_shutdown(); 64 GNUNET_SCHEDULER_shutdown ();
65} 65}
66 66
67 67
@@ -71,178 +71,181 @@ cleanup()
71 * @param cls handle to use to re-connect. 71 * @param cls handle to use to re-connect.
72 */ 72 */
73static void 73static void
74endbadly(void *cls) 74endbadly (void *cls)
75{ 75{
76 if (NULL != nsqe) 76 if (NULL != nsqe)
77 { 77 {
78 GNUNET_NAMECACHE_cancel(nsqe); 78 GNUNET_NAMECACHE_cancel (nsqe);
79 nsqe = NULL; 79 nsqe = NULL;
80 } 80 }
81 cleanup(); 81 cleanup ();
82 res = 1; 82 res = 1;
83} 83}
84 84
85 85
86static void 86static void
87end(void *cls) 87end (void *cls)
88{ 88{
89 cleanup(); 89 cleanup ();
90 res = 0; 90 res = 0;
91} 91}
92 92
93 93
94static void 94static void
95rd_decrypt_cb(void *cls, 95rd_decrypt_cb (void *cls,
96 unsigned int rd_count, 96 unsigned int rd_count,
97 const struct GNUNET_GNSRECORD_Data *rd) 97 const struct GNUNET_GNSRECORD_Data *rd)
98{ 98{
99 char rd_cmp_data[TEST_RECORD_DATALEN]; 99 char rd_cmp_data[TEST_RECORD_DATALEN];
100 100
101 GNUNET_assert(1 == rd_count); 101 GNUNET_assert (1 == rd_count);
102 GNUNET_assert(NULL != rd); 102 GNUNET_assert (NULL != rd);
103 103
104 memset(rd_cmp_data, 'a', TEST_RECORD_DATALEN); 104 memset (rd_cmp_data, 'a', TEST_RECORD_DATALEN);
105 105
106 GNUNET_assert(TEST_RECORD_TYPE == rd[0].record_type); 106 GNUNET_assert (TEST_RECORD_TYPE == rd[0].record_type);
107 GNUNET_assert(TEST_RECORD_DATALEN == rd[0].data_size); 107 GNUNET_assert (TEST_RECORD_DATALEN == rd[0].data_size);
108 GNUNET_assert(0 == memcmp(&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN)); 108 GNUNET_assert (0 == memcmp (&rd_cmp_data, rd[0].data, TEST_RECORD_DATALEN));
109 109
110 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Block was decrypted successfully \n"); 111 "Block was decrypted successfully \n");
112 112
113 GNUNET_SCHEDULER_add_now(&end, NULL); 113 GNUNET_SCHEDULER_add_now (&end, NULL);
114} 114}
115 115
116 116
117static void 117static void
118name_lookup_proc(void *cls, 118name_lookup_proc (void *cls,
119 const struct GNUNET_GNSRECORD_Block *block) 119 const struct GNUNET_GNSRECORD_Block *block)
120{ 120{
121 const char *name = cls; 121 const char *name = cls;
122 122
123 nsqe = NULL; 123 nsqe = NULL;
124 124
125 GNUNET_assert(NULL != cls); 125 GNUNET_assert (NULL != cls);
126 126
127 if (endbadly_task != NULL) 127 if (endbadly_task != NULL)
128 { 128 {
129 GNUNET_SCHEDULER_cancel(endbadly_task); 129 GNUNET_SCHEDULER_cancel (endbadly_task);
130 endbadly_task = NULL; 130 endbadly_task = NULL;
131 } 131 }
132 132
133 if (NULL == block) 133 if (NULL == block)
134 { 134 {
135 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 135 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
136 _("Namecache returned no block\n")); 136 _ ("Namecache returned no block\n"));
137 if (NULL != endbadly_task) 137 if (NULL != endbadly_task)
138 GNUNET_SCHEDULER_cancel(endbadly_task); 138 GNUNET_SCHEDULER_cancel (endbadly_task);
139 endbadly_task = GNUNET_SCHEDULER_add_now(&endbadly, NULL); 139 endbadly_task = GNUNET_SCHEDULER_add_now (&endbadly, NULL);
140 return; 140 return;
141 } 141 }
142 142
143 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
144 "Namecache returned block, decrypting \n"); 144 "Namecache returned block, decrypting \n");
145 GNUNET_assert(GNUNET_OK == GNUNET_GNSRECORD_block_decrypt(block, 145 GNUNET_assert (GNUNET_OK == GNUNET_GNSRECORD_block_decrypt (block,
146 &pubkey, name, &rd_decrypt_cb, (void *)name)); 146 &pubkey, name,
147 &rd_decrypt_cb,
148 (void *) name));
147} 149}
148 150
149static void 151static void
150cache_cont(void *cls, int32_t success, const char *emsg) 152cache_cont (void *cls, int32_t success, const char *emsg)
151{ 153{
152 const char *name = cls; 154 const char *name = cls;
153 struct GNUNET_HashCode derived_hash; 155 struct GNUNET_HashCode derived_hash;
154 156
155 GNUNET_assert(NULL != cls); 157 GNUNET_assert (NULL != cls);
156 158
157 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, 159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
158 "Name store cached record for `%s': %s\n", 160 "Name store cached record for `%s': %s\n",
159 name, 161 name,
160 (success == GNUNET_OK) ? "SUCCESS" : "FAIL"); 162 (success == GNUNET_OK) ? "SUCCESS" : "FAIL");
161 163
162 /* Create derived hash */ 164 /* Create derived hash */
163 GNUNET_GNSRECORD_query_from_public_key(&pubkey, name, &derived_hash); 165 GNUNET_GNSRECORD_query_from_public_key (&pubkey, name, &derived_hash);
164 166
165 nsqe = GNUNET_NAMECACHE_lookup_block(nsh, &derived_hash, 167 nsqe = GNUNET_NAMECACHE_lookup_block (nsh, &derived_hash,
166 &name_lookup_proc, (void *)name); 168 &name_lookup_proc, (void *) name);
167} 169}
168 170
169 171
170static void 172static void
171run(void *cls, 173run (void *cls,
172 const struct GNUNET_CONFIGURATION_Handle *cfg, 174 const struct GNUNET_CONFIGURATION_Handle *cfg,
173 struct GNUNET_TESTING_Peer *peer) 175 struct GNUNET_TESTING_Peer *peer)
174{ 176{
175 struct GNUNET_GNSRECORD_Data rd; 177 struct GNUNET_GNSRECORD_Data rd;
176 struct GNUNET_GNSRECORD_Block *block; 178 struct GNUNET_GNSRECORD_Block *block;
177 char *hostkey_file; 179 char *hostkey_file;
178 const char * name = "dummy.dummy.gnunet"; 180 const char *name = "dummy.dummy.gnunet";
179 181
180 endbadly_task = GNUNET_SCHEDULER_add_delayed(TIMEOUT, 182 endbadly_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT,
181 &endbadly, NULL); 183 &endbadly, NULL);
182 GNUNET_asprintf(&hostkey_file, 184 GNUNET_asprintf (&hostkey_file,
183 "zonefiles%s%s", 185 "zonefiles%s%s",
184 DIR_SEPARATOR_STR, 186 DIR_SEPARATOR_STR,
185 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey"); 187 "N0UJMP015AFUNR2BTNM3FKPBLG38913BL8IDMCO2H0A1LIB81960.zkey");
186 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n", hostkey_file); 188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Using zonekey file `%s' \n",
187 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file(hostkey_file); 189 hostkey_file);
188 GNUNET_free(hostkey_file); 190 privkey = GNUNET_CRYPTO_ecdsa_key_create_from_file (hostkey_file);
189 GNUNET_assert(privkey != NULL); 191 GNUNET_free (hostkey_file);
190 GNUNET_CRYPTO_ecdsa_key_get_public(privkey, &pubkey); 192 GNUNET_assert (privkey != NULL);
191 193 GNUNET_CRYPTO_ecdsa_key_get_public (privkey, &pubkey);
192 194
193 rd.expiration_time = GNUNET_TIME_absolute_get().abs_value_us + 10000000000; 195
196 rd.expiration_time = GNUNET_TIME_absolute_get ().abs_value_us + 10000000000;
194 rd.record_type = TEST_RECORD_TYPE; 197 rd.record_type = TEST_RECORD_TYPE;
195 rd.data_size = TEST_RECORD_DATALEN; 198 rd.data_size = TEST_RECORD_DATALEN;
196 rd.data = GNUNET_malloc(TEST_RECORD_DATALEN); 199 rd.data = GNUNET_malloc (TEST_RECORD_DATALEN);
197 rd.flags = 0; 200 rd.flags = 0;
198 memset((char *)rd.data, 'a', TEST_RECORD_DATALEN); 201 memset ((char *) rd.data, 'a', TEST_RECORD_DATALEN);
199 block = GNUNET_GNSRECORD_block_create(privkey, 202 block = GNUNET_GNSRECORD_block_create (privkey,
200 GNUNET_TIME_UNIT_FOREVER_ABS, 203 GNUNET_TIME_UNIT_FOREVER_ABS,
201 name, &rd, 1); 204 name, &rd, 1);
202 if (NULL == block) 205 if (NULL == block)
203 { 206 {
204 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
205 "Namecache cannot cache no block!\n"); 208 "Namecache cannot cache no block!\n");
206 GNUNET_SCHEDULER_shutdown(); 209 GNUNET_SCHEDULER_shutdown ();
207 GNUNET_free(block); 210 GNUNET_free (block);
208 return; 211 return;
209 } 212 }
210 213
211 nsh = GNUNET_NAMECACHE_connect(cfg); 214 nsh = GNUNET_NAMECACHE_connect (cfg);
212 if (NULL == nsh) 215 if (NULL == nsh)
213 { 216 {
214 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 217 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
215 _("Namecache cannot connect to namecache\n")); 218 _ ("Namecache cannot connect to namecache\n"));
216 GNUNET_SCHEDULER_shutdown(); 219 GNUNET_SCHEDULER_shutdown ();
217 GNUNET_free(block); 220 GNUNET_free (block);
218 return; 221 return;
219 } 222 }
220 GNUNET_break(NULL != nsh); 223 GNUNET_break (NULL != nsh);
221 224
222 nsqe = GNUNET_NAMECACHE_block_cache(nsh, 225 nsqe = GNUNET_NAMECACHE_block_cache (nsh,
223 block, 226 block,
224 &cache_cont, (void *)name); 227 &cache_cont, (void *) name);
225 if (NULL == nsqe) 228 if (NULL == nsqe)
226 { 229 {
227 GNUNET_log(GNUNET_ERROR_TYPE_ERROR, 230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
228 _("Namecache cannot cache no block\n")); 231 _ ("Namecache cannot cache no block\n"));
229 } 232 }
230 GNUNET_free(block); 233 GNUNET_free (block);
231 GNUNET_free((void *)rd.data); 234 GNUNET_free ((void *) rd.data);
232} 235}
233 236
234 237
235int 238int
236main(int argc, char *argv[]) 239main (int argc, char *argv[])
237{ 240{
238 GNUNET_DISK_directory_remove("/tmp/test-gnunet-namecache/"); 241 GNUNET_DISK_directory_remove ("/tmp/test-gnunet-namecache/");
239 res = 1; 242 res = 1;
240 if (0 != 243 if (0 !=
241 GNUNET_TESTING_service_run("test-namecache-api", 244 GNUNET_TESTING_service_run ("test-namecache-api",
242 "namecache", 245 "namecache",
243 "test_namecache_api.conf", 246 "test_namecache_api.conf",
244 &run, 247 &run,
245 NULL)) 248 NULL))
246 return 1; 249 return 1;
247 return res; 250 return res;
248} 251}
diff --git a/src/namecache/test_plugin_namecache.c b/src/namecache/test_plugin_namecache.c
index 5c52bb375..e4ca145d7 100644
--- a/src/namecache/test_plugin_namecache.c
+++ b/src/namecache/test_plugin_namecache.c
@@ -43,13 +43,13 @@ static const char *plugin_name;
43 * @param api api to unload 43 * @param api api to unload
44 */ 44 */
45static void 45static void
46unload_plugin(struct GNUNET_NAMECACHE_PluginFunctions *api) 46unload_plugin (struct GNUNET_NAMECACHE_PluginFunctions *api)
47{ 47{
48 char *libname; 48 char *libname;
49 49
50 GNUNET_asprintf(&libname, "libgnunet_plugin_namecache_%s", plugin_name); 50 GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
51 GNUNET_break(NULL == GNUNET_PLUGIN_unload(libname, api)); 51 GNUNET_break (NULL == GNUNET_PLUGIN_unload (libname, api));
52 GNUNET_free(libname); 52 GNUNET_free (libname);
53} 53}
54 54
55 55
@@ -60,47 +60,47 @@ unload_plugin(struct GNUNET_NAMECACHE_PluginFunctions *api)
60 * @return NULL on error 60 * @return NULL on error
61 */ 61 */
62static struct GNUNET_NAMECACHE_PluginFunctions * 62static struct GNUNET_NAMECACHE_PluginFunctions *
63load_plugin(const struct GNUNET_CONFIGURATION_Handle *cfg) 63load_plugin (const struct GNUNET_CONFIGURATION_Handle *cfg)
64{ 64{
65 struct GNUNET_NAMECACHE_PluginFunctions *ret; 65 struct GNUNET_NAMECACHE_PluginFunctions *ret;
66 char *libname; 66 char *libname;
67 67
68 GNUNET_log(GNUNET_ERROR_TYPE_INFO, _("Loading `%s' namecache plugin\n"), 68 GNUNET_log (GNUNET_ERROR_TYPE_INFO, _ ("Loading `%s' namecache plugin\n"),
69 plugin_name); 69 plugin_name);
70 GNUNET_asprintf(&libname, "libgnunet_plugin_namecache_%s", plugin_name); 70 GNUNET_asprintf (&libname, "libgnunet_plugin_namecache_%s", plugin_name);
71 if (NULL == (ret = GNUNET_PLUGIN_load(libname, (void*)cfg))) 71 if (NULL == (ret = GNUNET_PLUGIN_load (libname, (void*) cfg)))
72 { 72 {
73 fprintf(stderr, "Failed to load plugin `%s'!\n", plugin_name); 73 fprintf (stderr, "Failed to load plugin `%s'!\n", plugin_name);
74 GNUNET_free(libname); 74 GNUNET_free (libname);
75 return NULL; 75 return NULL;
76 } 76 }
77 GNUNET_free(libname); 77 GNUNET_free (libname);
78 return ret; 78 return ret;
79} 79}
80 80
81 81
82static void 82static void
83run(void *cls, char *const *args, const char *cfgfile, 83run (void *cls, char *const *args, const char *cfgfile,
84 const struct GNUNET_CONFIGURATION_Handle *cfg) 84 const struct GNUNET_CONFIGURATION_Handle *cfg)
85{ 85{
86 struct GNUNET_NAMECACHE_PluginFunctions *nsp; 86 struct GNUNET_NAMECACHE_PluginFunctions *nsp;
87 87
88 ok = 0; 88 ok = 0;
89 nsp = load_plugin(cfg); 89 nsp = load_plugin (cfg);
90 if (NULL == nsp) 90 if (NULL == nsp)
91 { 91 {
92 fprintf(stderr, 92 fprintf (stderr,
93 "%s", 93 "%s",
94 "Failed to initialize namecache. Database likely not setup, skipping test.\n"); 94 "Failed to initialize namecache. Database likely not setup, skipping test.\n");
95 return; 95 return;
96 } 96 }
97 97
98 unload_plugin(nsp); 98 unload_plugin (nsp);
99} 99}
100 100
101 101
102int 102int
103main(int argc, char *argv[]) 103main (int argc, char *argv[])
104{ 104{
105 char cfg_name[PATH_MAX]; 105 char cfg_name[PATH_MAX];
106 char *const xargv[] = { 106 char *const xargv[] = {
@@ -113,18 +113,18 @@ main(int argc, char *argv[])
113 GNUNET_GETOPT_OPTION_END 113 GNUNET_GETOPT_OPTION_END
114 }; 114 };
115 115
116 GNUNET_DISK_directory_remove("/tmp/gnunet-test-plugin-namecache-sqlite"); 116 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
117 GNUNET_log_setup("test-plugin-namecache", 117 GNUNET_log_setup ("test-plugin-namecache",
118 "WARNING", 118 "WARNING",
119 NULL); 119 NULL);
120 plugin_name = GNUNET_TESTING_get_testname_from_underscore(argv[0]); 120 plugin_name = GNUNET_TESTING_get_testname_from_underscore (argv[0]);
121 GNUNET_snprintf(cfg_name, sizeof(cfg_name), "test_plugin_namecache_%s.conf", 121 GNUNET_snprintf (cfg_name, sizeof(cfg_name), "test_plugin_namecache_%s.conf",
122 plugin_name); 122 plugin_name);
123 GNUNET_PROGRAM_run((sizeof(xargv) / sizeof(char *)) - 1, xargv, 123 GNUNET_PROGRAM_run ((sizeof(xargv) / sizeof(char *)) - 1, xargv,
124 "test-plugin-namecache", "nohelp", options, &run, NULL); 124 "test-plugin-namecache", "nohelp", options, &run, NULL);
125 if (ok != 0) 125 if (ok != 0)
126 fprintf(stderr, "Missed some testcases: %d\n", ok); 126 fprintf (stderr, "Missed some testcases: %d\n", ok);
127 GNUNET_DISK_directory_remove("/tmp/gnunet-test-plugin-namecache-sqlite"); 127 GNUNET_DISK_directory_remove ("/tmp/gnunet-test-plugin-namecache-sqlite");
128 return ok; 128 return ok;
129} 129}
130 130