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