summaryrefslogtreecommitdiff
path: root/src/namestore/namestore_api.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2013-06-30 19:21:05 +0000
committerChristian Grothoff <christian@grothoff.org>2013-06-30 19:21:05 +0000
commit17de5c9d798109f55059190c886609ba377e4eb6 (patch)
tree07f2b4241f433b18baf02d5bf189307ad2308166 /src/namestore/namestore_api.c
parentfd87b25438487e1215c68bdb9f1bcac2d7012bc2 (diff)
downloadgnunet-17de5c9d798109f55059190c886609ba377e4eb6.tar.gz
gnunet-17de5c9d798109f55059190c886609ba377e4eb6.zip
-towards implementing improved namestore API
Diffstat (limited to 'src/namestore/namestore_api.c')
-rw-r--r--src/namestore/namestore_api.c205
1 files changed, 19 insertions, 186 deletions
diff --git a/src/namestore/namestore_api.c b/src/namestore/namestore_api.c
index 7a7c3eabb..97d34aa24 100644
--- a/src/namestore/namestore_api.c
+++ b/src/namestore/namestore_api.c
@@ -1,6 +1,6 @@
1/* 1/*
2 This file is part of GNUnet. 2 This file is part of GNUnet.
3 (C) 2010, 2011, 2012 Christian Grothoff (and other contributing authors) 3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
4 4
5 GNUnet is free software; you can redistribute it and/or modify 5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published 6 it under the terms of the GNU General Public License as published
@@ -378,71 +378,6 @@ handle_record_create_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
378 378
379 379
380/** 380/**
381 * Handle an incoming message of type 'GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE'
382 *
383 * @param qe the respective entry in the message queue
384 * @param msg the message we received
385 * @param size the message size
386 * @return GNUNET_OK on success, GNUNET_SYSERR on error and we did NOT notify the client
387 */
388static int
389handle_record_remove_response (struct GNUNET_NAMESTORE_QueueEntry *qe,
390 const struct RecordRemoveResponseMessage* msg,
391 size_t size)
392{
393 int ret;
394 const char *emsg;
395
396 /* Operation done, remove */
397 LOG (GNUNET_ERROR_TYPE_DEBUG, "Received `%s'\n",
398 "RECORD_REMOVE_RESPONSE");
399 switch (ntohl (msg->op_result))
400 {
401 case RECORD_REMOVE_RESULT_SUCCESS:
402 ret = GNUNET_OK;
403 emsg = NULL;
404 break;
405 case RECORD_REMOVE_RESULT_NO_RECORDS:
406 ret = GNUNET_NO;
407 emsg = NULL;
408 break;
409 case RECORD_REMOVE_RESULT_RECORD_NOT_FOUND:
410 ret = GNUNET_NO;
411 emsg = NULL;
412 break;
413 case RECORD_REMOVE_RESULT_FAILED_TO_SIGN:
414 ret = GNUNET_SYSERR;
415 emsg = _("Failed to create new signature");
416 break;
417 case RECORD_REMOVE_RESULT_FAILED_TO_PUT_UPDATE:
418 ret = GNUNET_SYSERR;
419 emsg = _("Failed to put new set of records in database");
420 break;
421 case RECORD_REMOVE_RESULT_FAILED_TO_REMOVE:
422 ret = GNUNET_SYSERR;
423 emsg = _("Failed to remove records from database");
424 break;
425 case RECORD_REMOVE_RESULT_FAILED_ACCESS_DATABASE:
426 ret = GNUNET_SYSERR;
427 emsg = _("Failed to access database");
428 break;
429 case RECORD_REMOVE_RESULT_FAILED_INTERNAL_ERROR:
430 ret = GNUNET_SYSERR;
431 emsg = _("unknown internal error in namestore");
432 break;
433 default:
434 GNUNET_break (0);
435 if (NULL != qe->cont)
436 qe->cont (qe->cont_cls, GNUNET_SYSERR, _("Protocol error"));
437 return GNUNET_NO;
438 }
439 if (NULL != qe->cont)
440 qe->cont (qe->cont_cls, ret, emsg);
441 return GNUNET_OK;
442}
443
444
445/**
446 * Handle an incoming message of type 'GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE' 381 * Handle an incoming message of type 'GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE'
447 * 382 *
448 * @param qe the respective entry in the message queue 383 * @param qe the respective entry in the message queue
@@ -554,13 +489,6 @@ manage_record_operations (struct GNUNET_NAMESTORE_QueueEntry *qe,
554 return GNUNET_SYSERR; 489 return GNUNET_SYSERR;
555 } 490 }
556 return handle_record_create_response (qe, (const struct RecordCreateResponseMessage *) msg, size); 491 return handle_record_create_response (qe, (const struct RecordCreateResponseMessage *) msg, size);
557 case GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE_RESPONSE:
558 if (size != sizeof (struct RecordRemoveResponseMessage))
559 {
560 GNUNET_break (0);
561 return GNUNET_SYSERR;
562 }
563 return handle_record_remove_response (qe, (const struct RecordRemoveResponseMessage *) msg, size);
564 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE: 492 case GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE:
565 if (size < sizeof (struct ZoneToNameResponseMessage)) 493 if (size < sizeof (struct ZoneToNameResponseMessage))
566 { 494 {
@@ -1142,18 +1070,20 @@ GNUNET_NAMESTORE_verify_signature (const struct GNUNET_CRYPTO_EccPublicKeyBinary
1142 * @param h handle to the namestore 1070 * @param h handle to the namestore
1143 * @param pkey private key of the zone 1071 * @param pkey private key of the zone
1144 * @param name name that is being mapped (at most 255 characters long) 1072 * @param name name that is being mapped (at most 255 characters long)
1073 * @param rd_count number of records in 'rd' array
1145 * @param rd record data to store 1074 * @param rd record data to store
1146 * @param cont continuation to call when done 1075 * @param cont continuation to call when done
1147 * @param cont_cls closure for cont 1076 * @param cont_cls closure for cont
1148 * @return handle to abort the request 1077 * @return handle to abort the request
1149 */ 1078 */
1150struct GNUNET_NAMESTORE_QueueEntry * 1079struct GNUNET_NAMESTORE_QueueEntry *
1151GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h, 1080GNUNET_NAMESTORE_record_put_by_authority (struct GNUNET_NAMESTORE_Handle *h,
1152 const struct GNUNET_CRYPTO_EccPrivateKey *pkey, 1081 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
1153 const char *name, 1082 const char *name,
1154 const struct GNUNET_NAMESTORE_RecordData *rd, 1083 unsigned int rd_count,
1155 GNUNET_NAMESTORE_ContinuationWithStatus cont, 1084 const struct GNUNET_NAMESTORE_RecordData *rd,
1156 void *cont_cls) 1085 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1086 void *cont_cls)
1157{ 1087{
1158 struct GNUNET_NAMESTORE_QueueEntry *qe; 1088 struct GNUNET_NAMESTORE_QueueEntry *qe;
1159 struct PendingMessage *pe; 1089 struct PendingMessage *pe;
@@ -1171,19 +1101,14 @@ GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1171 GNUNET_assert (NULL != h); 1101 GNUNET_assert (NULL != h);
1172 GNUNET_assert (NULL != pkey); 1102 GNUNET_assert (NULL != pkey);
1173 GNUNET_assert (NULL != name); 1103 GNUNET_assert (NULL != name);
1174 GNUNET_assert (NULL != rd);
1175 LOG (GNUNET_ERROR_TYPE_DEBUG,
1176 "Creating record of type %u under name `%s'\n",
1177 rd->record_type,
1178 name);
1179 name_len = strlen(name) + 1; 1104 name_len = strlen(name) + 1;
1180 if (name_len > MAX_NAME_LEN) 1105 if (name_len > MAX_NAME_LEN)
1181 { 1106 {
1182 GNUNET_break (0); 1107 GNUNET_break (0);
1183 return NULL; 1108 return NULL;
1184 } 1109 }
1185 rid = get_op_id(h); 1110 rid = get_op_id (h);
1186 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry)); 1111 qe = GNUNET_malloc (sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1187 qe->nsh = h; 1112 qe->nsh = h;
1188 qe->cont = cont; 1113 qe->cont = cont;
1189 qe->cont_cls = cont_cls; 1114 qe->cont_cls = cont_cls;
@@ -1195,9 +1120,9 @@ GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1195 1120
1196 /* setup msg */ 1121 /* setup msg */
1197 key_len = ntohs (pkey_enc->size); 1122 key_len = ntohs (pkey_enc->size);
1198 rd_ser_len = GNUNET_NAMESTORE_records_get_size(1, rd); 1123 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1199 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len; 1124 msg_size = sizeof (struct RecordCreateMessage) + key_len + name_len + rd_ser_len;
1200 pe = GNUNET_malloc(sizeof (struct PendingMessage) + msg_size); 1125 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1201 pe->size = msg_size; 1126 pe->size = msg_size;
1202 pe->is_init = GNUNET_NO; 1127 pe->is_init = GNUNET_NO;
1203 msg = (struct RecordCreateMessage *) &pe[1]; 1128 msg = (struct RecordCreateMessage *) &pe[1];
@@ -1205,114 +1130,21 @@ GNUNET_NAMESTORE_record_create (struct GNUNET_NAMESTORE_Handle *h,
1205 msg->gns_header.header.size = htons (msg_size); 1130 msg->gns_header.header.size = htons (msg_size);
1206 msg->gns_header.r_id = htonl (rid); 1131 msg->gns_header.r_id = htonl (rid);
1207 msg->name_len = htons (name_len); 1132 msg->name_len = htons (name_len);
1208 msg->rd_count = htons (1);
1209 msg->rd_len = htons (rd_ser_len);
1210 msg->pkey_len = htons (key_len);
1211 msg->expire = GNUNET_TIME_absolute_hton(GNUNET_TIME_UNIT_FOREVER_ABS);
1212 pkey_tmp = (char *) &msg[1];
1213 memcpy (pkey_tmp, pkey_enc, key_len);
1214 name_tmp = &pkey_tmp[key_len];
1215 memcpy (name_tmp, name, name_len);
1216 rd_ser = &name_tmp[name_len];
1217 GNUNET_NAMESTORE_records_serialize(1, rd, rd_ser_len, rd_ser);
1218
1219 GNUNET_free (pkey_enc);
1220
1221 LOG (GNUNET_ERROR_TYPE_DEBUG,
1222 "Sending `%s' message for name `%s' with size %u\n",
1223 "NAMESTORE_RECORD_CREATE", name, msg_size);
1224 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1225 do_transmit(h);
1226 return qe;
1227}
1228
1229
1230/**
1231 * Explicitly remove some content from the database. The
1232 * "cont"inuation will be called with status "GNUNET_OK" if content
1233 * was removed, "GNUNET_NO" if no matching entry was found and
1234 * "GNUNET_SYSERR" on all other types of errors.
1235 * This API is used by the authority of a zone.
1236 *
1237 * @param h handle to the namestore
1238 * @param pkey private key of the zone
1239 * @param name name that is being mapped (at most 255 characters long)
1240 * @param rd record data, remove specific record, NULL to remove the name and all records
1241 * @param cont continuation to call when done
1242 * @param cont_cls closure for cont
1243 * @return handle to abort the request
1244 */
1245struct GNUNET_NAMESTORE_QueueEntry *
1246GNUNET_NAMESTORE_record_remove (struct GNUNET_NAMESTORE_Handle *h,
1247 const struct GNUNET_CRYPTO_EccPrivateKey *pkey,
1248 const char *name,
1249 const struct GNUNET_NAMESTORE_RecordData *rd,
1250 GNUNET_NAMESTORE_ContinuationWithStatus cont,
1251 void *cont_cls)
1252{
1253 struct GNUNET_NAMESTORE_QueueEntry *qe;
1254 struct GNUNET_CRYPTO_EccPrivateKeyBinaryEncoded *pkey_enc;
1255 struct PendingMessage *pe;
1256 struct RecordRemoveMessage * msg;
1257 char *pkey_tmp;
1258 char *rd_ser;
1259 char *name_tmp;
1260 size_t rd_ser_len;
1261 size_t msg_size;
1262 size_t name_len;
1263 size_t key_len;
1264 uint32_t rid;
1265 uint16_t rd_count;
1266
1267 GNUNET_assert (NULL != h);
1268 if (NULL != rd)
1269 LOG (GNUNET_ERROR_TYPE_DEBUG,
1270 "Removing record of type %u under name `%s'\n",
1271 rd->record_type,
1272 name);
1273 else
1274 LOG (GNUNET_ERROR_TYPE_DEBUG,
1275 "Removing all records under name `%s'\n",
1276 name);
1277 rid = get_op_id(h);
1278 qe = GNUNET_malloc(sizeof (struct GNUNET_NAMESTORE_QueueEntry));
1279 qe->nsh = h;
1280 qe->cont = cont;
1281 qe->cont_cls = cont_cls;
1282 qe->op_id = rid;
1283 GNUNET_CONTAINER_DLL_insert_tail (h->op_head, h->op_tail, qe);
1284
1285 pkey_enc = GNUNET_CRYPTO_ecc_encode_key (pkey);
1286 GNUNET_assert (NULL != pkey_enc);
1287 key_len = ntohs (pkey_enc->size);
1288
1289 rd_count = (NULL == rd) ? 0 : 1;
1290 rd_ser_len = GNUNET_NAMESTORE_records_get_size (rd_count, rd);
1291 name_len = strlen (name) + 1;
1292 msg_size = sizeof (struct RecordRemoveMessage) + key_len + name_len + rd_ser_len;
1293 pe = GNUNET_malloc (sizeof (struct PendingMessage) + msg_size);
1294 pe->size = msg_size;
1295 pe->is_init = GNUNET_NO;
1296 msg = (struct RecordRemoveMessage *) &pe[1];
1297 msg->gns_header.header.type = htons (GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_REMOVE);
1298 msg->gns_header.header.size = htons (msg_size);
1299 msg->gns_header.r_id = htonl (rid);
1300 msg->name_len = htons (name_len);
1301 msg->rd_len = htons (rd_ser_len);
1302 msg->rd_count = htons (rd_count); 1133 msg->rd_count = htons (rd_count);
1134 msg->rd_len = htons (rd_ser_len);
1303 msg->pkey_len = htons (key_len); 1135 msg->pkey_len = htons (key_len);
1136 msg->expire = GNUNET_TIME_absolute_hton (GNUNET_TIME_UNIT_FOREVER_ABS);
1304 pkey_tmp = (char *) &msg[1]; 1137 pkey_tmp = (char *) &msg[1];
1305 memcpy (pkey_tmp, pkey_enc, key_len); 1138 memcpy (pkey_tmp, pkey_enc, key_len);
1306 name_tmp = &pkey_tmp[key_len]; 1139 name_tmp = &pkey_tmp[key_len];
1307 memcpy (name_tmp, name, name_len); 1140 memcpy (name_tmp, name, name_len);
1308 rd_ser = &name_tmp[name_len]; 1141 rd_ser = &name_tmp[name_len];
1309 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser); 1142 GNUNET_NAMESTORE_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
1310
1311 GNUNET_free (pkey_enc); 1143 GNUNET_free (pkey_enc);
1312 1144
1313 LOG (GNUNET_ERROR_TYPE_DEBUG, 1145 LOG (GNUNET_ERROR_TYPE_DEBUG,
1314 "Sending `%s' message for name `%s' with size %u\n", 1146 "Sending `%s' message for name `%s' with size %u\n",
1315 "NAMESTORE_RECORD_REMOVE", name, msg_size); 1147 "NAMESTORE_RECORD_CREATE", name, msg_size);
1316 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe); 1148 GNUNET_CONTAINER_DLL_insert_tail (h->pending_head, h->pending_tail, pe);
1317 do_transmit(h); 1149 do_transmit(h);
1318 return qe; 1150 return qe;
@@ -1507,7 +1339,7 @@ GNUNET_NAMESTORE_zone_iteration_start (struct GNUNET_NAMESTORE_Handle *h,
1507 { 1339 {
1508 LOG (GNUNET_ERROR_TYPE_DEBUG, 1340 LOG (GNUNET_ERROR_TYPE_DEBUG,
1509 "Sending `%s' message for zone `%s'\n", 1341 "Sending `%s' message for zone `%s'\n",
1510 "ZONE_ITERATION_START", GNUNET_short_h2s(zone)); 1342 "ZONE_ITERATION_START", GNUNET_NAMESTORE_short_h2s(zone));
1511 msg->zone = *zone; 1343 msg->zone = *zone;
1512 } 1344 }
1513 else 1345 else
@@ -1582,7 +1414,7 @@ GNUNET_NAMESTORE_zone_iteration_stop (struct GNUNET_NAMESTORE_ZoneIterator *it)
1582 msg->gns_header.r_id = htonl (it->op_id); 1414 msg->gns_header.r_id = htonl (it->op_id);
1583 if (GNUNET_YES == it->has_zone) 1415 if (GNUNET_YES == it->has_zone)
1584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1416 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1585 "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_STOP", GNUNET_short_h2s(&it->zone)); 1417 "Sending `%s' message for zone `%s'\n", "ZONE_ITERATION_STOP", GNUNET_NAMESTORE_short_h2s(&it->zone));
1586 else 1418 else
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "Sending `%s' message for all zones\n", "ZONE_ITERATION_STOP"); 1420 "Sending `%s' message for all zones\n", "ZONE_ITERATION_STOP");
@@ -1608,4 +1440,5 @@ GNUNET_NAMESTORE_cancel (struct GNUNET_NAMESTORE_QueueEntry *qe)
1608 GNUNET_free(qe); 1440 GNUNET_free(qe);
1609} 1441}
1610 1442
1443
1611/* end of namestore_api.c */ 1444/* end of namestore_api.c */