GNUnet  0.10.x
namestore_api.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2010-2013, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19 */
20 
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_crypto_lib.h"
32 #include "gnunet_constants.h"
33 #include "gnunet_dnsparser_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_signatures.h"
36 #include "gnunet_gns_service.h"
38 #include "namestore.h"
39 
40 
41 #define LOG(kind, ...) GNUNET_log_from (kind, "namestore-api", __VA_ARGS__)
42 
47 #define NAMESTORE_DELAY_TOLERANCE GNUNET_TIME_UNIT_MINUTES
48 
54 {
55 
60 
65 
70 
75 
79  void *cont_cls;
80 
85 
89  void *proc_cls;
90 
95 
99  void *error_cb_cls;
100 
106 
111 
115  uint32_t op_id;
116 };
117 
118 
123 {
124 
129 
134 
139 
144 
149 
154 
158  void *proc_cls;
159 
164 
169 
175 
180 
184  uint32_t op_id;
185 };
186 
187 
192 {
193 
198 
203 
208 
213 
218 
223 
228 
232  struct GNUNET_TIME_Relative reconnect_delay;
233 
238 
242  uint32_t last_op_id_used;
243 };
244 
245 
251 static void
253 
254 
262 static struct GNUNET_NAMESTORE_QueueEntry *
263 find_qe (struct GNUNET_NAMESTORE_Handle *h, uint32_t rid)
264 {
266 
267  for (qe = h->op_head; qe != NULL; qe = qe->next)
268  if (qe->op_id == rid)
269  return qe;
270  return NULL;
271 }
272 
273 
281 static struct GNUNET_NAMESTORE_ZoneIterator *
282 find_zi (struct GNUNET_NAMESTORE_Handle *h, uint32_t rid)
283 {
285 
286  for (ze = h->z_head; ze != NULL; ze = ze->next)
287  if (ze->op_id == rid)
288  return ze;
289  return NULL;
290 }
291 
292 
298 static void
300 {
301  struct GNUNET_NAMESTORE_Handle *h = qe->h;
302 
304  if (NULL != qe->env)
305  GNUNET_MQ_discard (qe->env);
306  if (NULL != qe->timeout_task)
308  GNUNET_free (qe);
309 }
310 
311 
317 static void
319 {
320  struct GNUNET_NAMESTORE_Handle *h = ze->h;
321 
323  if (NULL != ze->env)
324  GNUNET_MQ_discard (ze->env);
325  GNUNET_free (ze);
326 }
327 
328 
338 static int
339 check_rd (size_t rd_len, const void *rd_buf, unsigned int rd_count)
340 {
341  struct GNUNET_GNSRECORD_Data rd[rd_count];
342 
343  if (GNUNET_OK !=
344  GNUNET_GNSRECORD_records_deserialize (rd_len, rd_buf, rd_count, rd))
345  {
346  GNUNET_break (0);
347  return GNUNET_SYSERR;
348  }
349  return GNUNET_OK;
350 }
351 
352 
360 static void
362  const struct RecordStoreResponseMessage *msg)
363 {
364  struct GNUNET_NAMESTORE_Handle *h = cls;
366  int res;
367  const char *emsg;
368 
369  qe = find_qe (h, ntohl (msg->gns_header.r_id));
370  res = ntohl (msg->op_result);
372  "Received RECORD_STORE_RESPONSE with result %d\n",
373  res);
374  /* TODO: add actual error message from namestore to response... */
375  if (GNUNET_SYSERR == res)
376  emsg = _ ("Namestore failed to store record\n");
377  else
378  emsg = NULL;
379  if (NULL != qe->cont)
380  qe->cont (qe->cont_cls, res, emsg);
381  free_qe (qe);
382 }
383 
384 
393 static int
395 {
396  const char *name;
397  size_t exp_msg_len;
398  size_t msg_len;
399  size_t name_len;
400  size_t rd_len;
401 
402  (void) cls;
403  rd_len = ntohs (msg->rd_len);
404  msg_len = ntohs (msg->gns_header.header.size);
405  name_len = ntohs (msg->name_len);
406  exp_msg_len = sizeof (*msg) + name_len + rd_len;
407  if (msg_len != exp_msg_len)
408  {
409  GNUNET_break (0);
410  return GNUNET_SYSERR;
411  }
412  name = (const char *) &msg[1];
413  if ((name_len > 0) && ('\0' != name[name_len - 1]))
414  {
415  GNUNET_break (0);
416  return GNUNET_SYSERR;
417  }
418  if (GNUNET_NO == ntohs (msg->found))
419  {
420  if (0 != ntohs (msg->rd_count))
421  {
422  GNUNET_break (0);
423  return GNUNET_SYSERR;
424  }
425  return GNUNET_OK;
426  }
427  return check_rd (rd_len, &name[name_len], ntohs (msg->rd_count));
428 }
429 
430 
438 static void
440 {
441  struct GNUNET_NAMESTORE_Handle *h = cls;
443  const char *name;
444  const char *rd_tmp;
445  size_t name_len;
446  size_t rd_len;
447  unsigned int rd_count;
448 
449  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received RECORD_LOOKUP_RESULT\n");
450  qe = find_qe (h, ntohl (msg->gns_header.r_id));
451  if (NULL == qe)
452  return;
453  rd_len = ntohs (msg->rd_len);
454  rd_count = ntohs (msg->rd_count);
455  name_len = ntohs (msg->name_len);
456  name = (const char *) &msg[1];
457  if (GNUNET_NO == ntohs (msg->found))
458  {
459  /* label was not in namestore */
460  if (NULL != qe->proc)
461  qe->proc (qe->proc_cls, &msg->private_key, name, 0, NULL);
462  free_qe (qe);
463  return;
464  }
465 
466  rd_tmp = &name[name_len];
467  {
468  struct GNUNET_GNSRECORD_Data rd[rd_count];
469 
470  GNUNET_assert (
471  GNUNET_OK ==
472  GNUNET_GNSRECORD_records_deserialize (rd_len, rd_tmp, rd_count, rd));
473  if (0 == name_len)
474  name = NULL;
475  if (NULL != qe->proc)
476  qe->proc (qe->proc_cls,
477  &msg->private_key,
478  name,
479  rd_count,
480  (rd_count > 0) ? rd : NULL);
481  }
482  free_qe (qe);
483 }
484 
485 
494 static int
495 check_record_result (void *cls, const struct RecordResultMessage *msg)
496 {
497  static struct GNUNET_CRYPTO_EcdsaPrivateKey priv_dummy;
498  const char *name;
499  size_t msg_len;
500  size_t name_len;
501  size_t rd_len;
502 
503  (void) cls;
504  rd_len = ntohs (msg->rd_len);
505  msg_len = ntohs (msg->gns_header.header.size);
506  name_len = ntohs (msg->name_len);
507  if (0 != ntohs (msg->reserved))
508  {
509  GNUNET_break (0);
510  return GNUNET_SYSERR;
511  }
512  if (msg_len != sizeof (struct RecordResultMessage) + name_len + rd_len)
513  {
514  GNUNET_break (0);
515  return GNUNET_SYSERR;
516  }
517  name = (const char *) &msg[1];
518  if ((0 == name_len) || ('\0' != name[name_len - 1]))
519  {
520  GNUNET_break (0);
521  return GNUNET_SYSERR;
522  }
523  if (0 == GNUNET_memcmp (&msg->private_key, &priv_dummy))
524  {
525  GNUNET_break (0);
526  return GNUNET_SYSERR;
527  }
528  return check_rd (rd_len, &name[name_len], ntohs (msg->rd_count));
529 }
530 
531 
539 static void
540 handle_record_result (void *cls, const struct RecordResultMessage *msg)
541 {
542  struct GNUNET_NAMESTORE_Handle *h = cls;
545  const char *name;
546  const char *rd_tmp;
547  size_t name_len;
548  size_t rd_len;
549  unsigned int rd_count;
550 
551  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received RECORD_RESULT\n");
552  rd_len = ntohs (msg->rd_len);
553  rd_count = ntohs (msg->rd_count);
554  name_len = ntohs (msg->name_len);
555  ze = find_zi (h, ntohl (msg->gns_header.r_id));
556  qe = find_qe (h, ntohl (msg->gns_header.r_id));
557  if ((NULL == ze) && (NULL == qe))
558  return; /* rid not found */
559  if ((NULL != ze) && (NULL != qe))
560  {
561  GNUNET_break (0); /* rid ambigous */
562  force_reconnect (h);
563  return;
564  }
565  name = (const char *) &msg[1];
566  rd_tmp = &name[name_len];
567  {
568  struct GNUNET_GNSRECORD_Data rd[rd_count];
569 
570  GNUNET_assert (
571  GNUNET_OK ==
572  GNUNET_GNSRECORD_records_deserialize (rd_len, rd_tmp, rd_count, rd));
573  if (0 == name_len)
574  name = NULL;
575  if (NULL != qe)
576  {
577  if (NULL != qe->proc)
578  qe->proc (qe->proc_cls,
579  &msg->private_key,
580  name,
581  rd_count,
582  (rd_count > 0) ? rd : NULL);
583  free_qe (qe);
584  return;
585  }
586  if (NULL != ze)
587  {
588  if (NULL != ze->proc)
589  ze->proc (ze->proc_cls, &msg->private_key, name, rd_count, rd);
590  return;
591  }
592  }
593  GNUNET_assert (0);
594 }
595 
596 
604 static void
606 {
607  struct GNUNET_NAMESTORE_Handle *h = cls;
610 
611  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received RECORD_RESULT_END\n");
612  ze = find_zi (h, ntohl (msg->r_id));
613  qe = find_qe (h, ntohl (msg->r_id));
614  if ((NULL == ze) && (NULL == qe))
615  return; /* rid not found */
616  if ((NULL != ze) && (NULL != qe))
617  {
618  GNUNET_break (0); /* rid ambigous */
619  force_reconnect (h);
620  return;
621  }
622  LOG (GNUNET_ERROR_TYPE_DEBUG, "Zone iteration completed!\n");
623  if (NULL == ze)
624  {
625  GNUNET_break (0);
626  force_reconnect (h);
627  return;
628  }
629  if (NULL != ze->finish_cb)
630  ze->finish_cb (ze->finish_cb_cls);
631  free_ze (ze);
632 }
633 
634 
643 static int
645  const struct ZoneToNameResponseMessage *msg)
646 {
647  size_t name_len;
648  size_t rd_ser_len;
649  const char *name_tmp;
650 
651  (void) cls;
652  if (GNUNET_OK != ntohs (msg->res))
653  return GNUNET_OK;
654  name_len = ntohs (msg->name_len);
655  rd_ser_len = ntohs (msg->rd_len);
656  if (ntohs (msg->gns_header.header.size) !=
657  sizeof (struct ZoneToNameResponseMessage) + name_len + rd_ser_len)
658  {
659  GNUNET_break (0);
660  return GNUNET_SYSERR;
661  }
662  name_tmp = (const char *) &msg[1];
663  if ((name_len > 0) && ('\0' != name_tmp[name_len - 1]))
664  {
665  GNUNET_break (0);
666  return GNUNET_SYSERR;
667  }
668  return check_rd (rd_ser_len, &name_tmp[name_len], ntohs (msg->rd_count));
669 }
670 
671 
679 static void
681  const struct ZoneToNameResponseMessage *msg)
682 {
683  struct GNUNET_NAMESTORE_Handle *h = cls;
685  int res;
686  size_t name_len;
687  size_t rd_ser_len;
688  unsigned int rd_count;
689  const char *name_tmp;
690  const char *rd_tmp;
691 
692  LOG (GNUNET_ERROR_TYPE_DEBUG, "Received ZONE_TO_NAME_RESPONSE\n");
693  qe = find_qe (h, ntohl (msg->gns_header.r_id));
694  res = ntohs (msg->res);
695  switch (res)
696  {
697  case GNUNET_SYSERR:
699  "An error occurred during zone to name operation\n");
700  break;
701  case GNUNET_NO:
703  "Namestore has no result for zone to name mapping \n");
704  if (NULL != qe->proc)
705  qe->proc (qe->proc_cls, &msg->zone, NULL, 0, NULL);
706  free_qe (qe);
707  return;
708  case GNUNET_YES:
710  "Namestore has result for zone to name mapping \n");
711  name_len = ntohs (msg->name_len);
712  rd_count = ntohs (msg->rd_count);
713  rd_ser_len = ntohs (msg->rd_len);
714  name_tmp = (const char *) &msg[1];
715  rd_tmp = &name_tmp[name_len];
716  {
717  struct GNUNET_GNSRECORD_Data rd[rd_count];
718 
721  rd_tmp,
722  rd_count,
723  rd));
724  /* normal end, call continuation with result */
725  if (NULL != qe->proc)
726  qe->proc (qe->proc_cls, &msg->zone, name_tmp, rd_count, rd);
727  /* return is important here: break would call continuation with error! */
728  free_qe (qe);
729  return;
730  }
731  default:
732  GNUNET_break (0);
733  force_reconnect (h);
734  return;
735  }
736  /* error case, call continuation with error */
737  if (NULL != qe->error_cb)
738  qe->error_cb (qe->error_cb_cls);
739  free_qe (qe);
740 }
741 
742 
751 static void
752 mq_error_handler (void *cls, enum GNUNET_MQ_Error error)
753 {
754  struct GNUNET_NAMESTORE_Handle *h = cls;
755 
756  (void) error;
757  force_reconnect (h);
758 }
759 
760 
766 static void
768 {
769  struct GNUNET_MQ_MessageHandler handlers[] =
770  {GNUNET_MQ_hd_fixed_size (record_store_response,
773  h),
774  GNUNET_MQ_hd_var_size (zone_to_name_response,
777  h),
778  GNUNET_MQ_hd_var_size (record_result,
780  struct RecordResultMessage,
781  h),
782  GNUNET_MQ_hd_fixed_size (record_result_end,
785  h),
786  GNUNET_MQ_hd_var_size (lookup_result,
789  h),
793 
794  GNUNET_assert (NULL == h->mq);
795  h->mq =
796  GNUNET_CLIENT_connect (h->cfg, "namestore", handlers, &mq_error_handler, h);
797  if (NULL == h->mq)
798  return;
799  /* re-transmit pending requests that waited for a reconnect... */
800  for (it = h->z_head; NULL != it; it = it->next)
801  {
802  GNUNET_MQ_send (h->mq, it->env);
803  it->env = NULL;
804  }
805  for (qe = h->op_head; NULL != qe; qe = qe->next)
806  {
807  GNUNET_MQ_send (h->mq, qe->env);
808  qe->env = NULL;
809  }
810 }
811 
812 
818 static void
819 reconnect_task (void *cls)
820 {
821  struct GNUNET_NAMESTORE_Handle *h = cls;
822 
823  h->reconnect_task = NULL;
824  reconnect (h);
825 }
826 
827 
833 static void
835 {
838 
839  GNUNET_MQ_destroy (h->mq);
840  h->mq = NULL;
841  while (NULL != (ze = h->z_head))
842  {
843  if (NULL != ze->error_cb)
844  ze->error_cb (ze->error_cb_cls);
845  free_ze (ze);
846  }
847  while (NULL != (qe = h->op_head))
848  {
849  if (NULL != qe->error_cb)
850  qe->error_cb (qe->error_cb_cls);
851  if (NULL != qe->cont)
852  qe->cont (qe->cont_cls,
854  "failure in communication with namestore service");
855  free_qe (qe);
856  }
857 
858  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Reconnecting to namestore\n");
860  h->reconnect_task =
862 }
863 
864 
871 static uint32_t
873 {
874  return h->last_op_id_used++;
875 }
876 
877 
886 {
887  struct GNUNET_NAMESTORE_Handle *h;
888 
889  h = GNUNET_new (struct GNUNET_NAMESTORE_Handle);
890  h->cfg = cfg;
891  reconnect (h);
892  if (NULL == h->mq)
893  {
894  GNUNET_free (h);
895  return NULL;
896  }
897  return h;
898 }
899 
900 
907 void
909 {
912 
913  LOG (GNUNET_ERROR_TYPE_DEBUG, "Cleaning up\n");
914  GNUNET_break (NULL == h->op_head);
915  while (NULL != (q = h->op_head))
916  {
918  GNUNET_free (q);
919  }
920  GNUNET_break (NULL == h->z_head);
921  while (NULL != (z = h->z_head))
922  {
924  GNUNET_free (z);
925  }
926  if (NULL != h->mq)
927  {
928  GNUNET_MQ_destroy (h->mq);
929  h->mq = NULL;
930  }
931  if (NULL != h->reconnect_task)
932  {
934  h->reconnect_task = NULL;
935  }
936  GNUNET_free (h);
937 }
938 
939 
946 static void
947 warn_delay (void *cls)
948 {
949  struct GNUNET_NAMESTORE_QueueEntry *qe = cls;
950 
951  qe->timeout_task = NULL;
953  "Did not receive response from namestore after %s!\n",
955  GNUNET_YES));
956  if (NULL != qe->cont)
957  {
958  qe->cont (qe->cont_cls, GNUNET_SYSERR, "timeout");
959  qe->cont = NULL;
960  }
962 }
963 
964 
981  struct GNUNET_NAMESTORE_Handle *h,
982  const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
983  const char *label,
984  unsigned int rd_count,
985  const struct GNUNET_GNSRECORD_Data *rd,
987  void *cont_cls)
988 {
990  struct GNUNET_MQ_Envelope *env;
991  char *name_tmp;
992  char *rd_ser;
993  ssize_t rd_ser_len;
994  size_t name_len;
995  uint32_t rid;
996  struct RecordStoreMessage *msg;
997  ssize_t sret;
998 
999  name_len = strlen (label) + 1;
1000  if (name_len > MAX_NAME_LEN)
1001  {
1002  GNUNET_break (0);
1003  return NULL;
1004  }
1005  rd_ser_len = GNUNET_GNSRECORD_records_get_size (rd_count, rd);
1006  if (rd_ser_len < 0)
1007  {
1008  GNUNET_break (0);
1009  return NULL;
1010  }
1011  if (rd_ser_len > UINT16_MAX)
1012  {
1013  GNUNET_break (0);
1014  return NULL;
1015  }
1016  rid = get_op_id (h);
1018  qe->h = h;
1019  qe->cont = cont;
1020  qe->cont_cls = cont_cls;
1021  qe->op_id = rid;
1023 
1024  /* setup msg */
1025  env = GNUNET_MQ_msg_extra (msg,
1026  name_len + rd_ser_len,
1028  msg->gns_header.r_id = htonl (rid);
1029  msg->name_len = htons (name_len);
1030  msg->rd_count = htons (rd_count);
1031  msg->rd_len = htons (rd_ser_len);
1032  msg->reserved = htons (0);
1033  msg->private_key = *pkey;
1034 
1035  name_tmp = (char *) &msg[1];
1036  GNUNET_memcpy (name_tmp, label, name_len);
1037  rd_ser = &name_tmp[name_len];
1038  sret = GNUNET_GNSRECORD_records_serialize (rd_count, rd, rd_ser_len, rd_ser);
1039  if ((0 > sret) || (sret != rd_ser_len))
1040  {
1041  GNUNET_break (0);
1042  GNUNET_free (env);
1043  return NULL;
1044  }
1045  GNUNET_assert (rd_ser_len == sret);
1047  "Sending NAMESTORE_RECORD_STORE message for name `%s' with %u records\n",
1048  label,
1049  rd_count);
1050  qe->timeout_task =
1052  if (NULL == h->mq)
1053  {
1054  qe->env = env;
1056  "Delaying NAMESTORE_RECORD_STORE message as namestore is not ready!\n");
1057  }
1058  else
1059  {
1060  GNUNET_MQ_send (h->mq, env);
1061  }
1062  return qe;
1063 }
1064 
1065 
1078  const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1079  const char *nick,
1081  void *cont_cls)
1082 {
1083  struct GNUNET_GNSRECORD_Data rd;
1084 
1085  if (NULL == h->mq)
1086  return NULL;
1087  memset (&rd, 0, sizeof (rd));
1088  rd.data = nick;
1089  rd.data_size = strlen (nick) + 1;
1094  pkey,
1096  1,
1097  &rd,
1098  cont,
1099  cont_cls);
1100 }
1101 
1102 
1117  struct GNUNET_NAMESTORE_Handle *h,
1118  const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey,
1119  const char *label,
1121  void *error_cb_cls,
1123  void *rm_cls)
1124 {
1126  struct GNUNET_MQ_Envelope *env;
1127  struct LabelLookupMessage *msg;
1128  size_t label_len;
1129 
1130  if (1 == (label_len = strlen (label) + 1))
1131  {
1132  GNUNET_break (0);
1133  return NULL;
1134  }
1135 
1137  qe->h = h;
1138  qe->error_cb = error_cb;
1139  qe->error_cb_cls = error_cb_cls;
1140  qe->proc = rm;
1141  qe->proc_cls = rm_cls;
1142  qe->op_id = get_op_id (h);
1144 
1145  env = GNUNET_MQ_msg_extra (msg,
1146  label_len,
1148  msg->gns_header.r_id = htonl (qe->op_id);
1149  msg->zone = *pkey;
1150  msg->label_len = htonl (label_len);
1151  GNUNET_memcpy (&msg[1], label, label_len);
1152  if (NULL == h->mq)
1153  qe->env = env;
1154  else
1155  GNUNET_MQ_send (h->mq, env);
1156  return qe;
1157 }
1158 
1159 
1177  struct GNUNET_NAMESTORE_Handle *h,
1178  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1179  const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
1181  void *error_cb_cls,
1183  void *proc_cls)
1184 {
1186  struct GNUNET_MQ_Envelope *env;
1187  struct ZoneToNameMessage *msg;
1188  uint32_t rid;
1189 
1190  rid = get_op_id (h);
1192  qe->h = h;
1193  qe->error_cb = error_cb;
1194  qe->error_cb_cls = error_cb_cls;
1195  qe->proc = proc;
1196  qe->proc_cls = proc_cls;
1197  qe->op_id = rid;
1199 
1201  msg->gns_header.r_id = htonl (rid);
1202  msg->zone = *zone;
1203  msg->value_zone = *value_zone;
1204  if (NULL == h->mq)
1205  qe->env = env;
1206  else
1207  GNUNET_MQ_send (h->mq, env);
1208  return qe;
1209 }
1210 
1211 
1233  struct GNUNET_NAMESTORE_Handle *h,
1234  const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
1236  void *error_cb_cls,
1238  void *proc_cls,
1240  void *finish_cb_cls)
1241 {
1242  struct GNUNET_NAMESTORE_ZoneIterator *it;
1243  struct GNUNET_MQ_Envelope *env;
1245  uint32_t rid;
1246 
1247  LOG (GNUNET_ERROR_TYPE_DEBUG, "Sending ZONE_ITERATION_START message\n");
1248  rid = get_op_id (h);
1250  it->h = h;
1251  it->error_cb = error_cb;
1252  it->error_cb_cls = error_cb_cls;
1253  it->finish_cb = finish_cb;
1254  it->finish_cb_cls = finish_cb_cls;
1255  it->proc = proc;
1256  it->proc_cls = proc_cls;
1257  it->op_id = rid;
1258  if (NULL != zone)
1259  it->zone = *zone;
1262  msg->gns_header.r_id = htonl (rid);
1263  if (NULL != zone)
1264  msg->zone = *zone;
1265  if (NULL == h->mq)
1266  it->env = env;
1267  else
1268  GNUNET_MQ_send (h->mq, env);
1269  return it;
1270 }
1271 
1272 
1281 void
1283  uint64_t limit)
1284 {
1285  struct GNUNET_NAMESTORE_Handle *h = it->h;
1286  struct ZoneIterationNextMessage *msg;
1287  struct GNUNET_MQ_Envelope *env;
1288 
1290  "Sending ZONE_ITERATION_NEXT message with limit %llu\n",
1291  (unsigned long long) limit);
1293  msg->gns_header.r_id = htonl (it->op_id);
1294  msg->limit = GNUNET_htonll (limit);
1295  GNUNET_MQ_send (h->mq, env);
1296 }
1297 
1298 
1304 void
1306 {
1307  struct GNUNET_NAMESTORE_Handle *h = it->h;
1308  struct GNUNET_MQ_Envelope *env;
1309  struct ZoneIterationStopMessage *msg;
1310 
1311  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending ZONE_ITERATION_STOP message\n");
1312  if (NULL != h->mq)
1313  {
1314  env =
1316  msg->gns_header.r_id = htonl (it->op_id);
1317  GNUNET_MQ_send (h->mq, env);
1318  }
1319  free_ze (it);
1320 }
1321 
1322 
1329 void
1331 {
1332  free_qe (qe);
1333 }
1334 
1335 
1336 /* end of namestore_api.c */
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
struct GNUNET_NAMESTORE_Handle * h
Main handle to access the namestore.
Definition: namestore_api.c:69
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE.
Definition: namestore.h:110
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE.
Definition: namestore.h:63
static struct GNUNET_DATASTORE_QueueEntry * qe
Current operation.
GNUNET_SCHEDULER_TaskCallback error_cb
Function to call on errors.
GNUNET_NETWORK_STRUCT_END ssize_t GNUNET_GNSRECORD_records_get_size(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Calculate how many bytes we will need to serialize the given records.
struct GNUNET_MQ_Envelope * env
Envelope of the message to send to the service, if not yet sent.
struct GNUNET_MessageHeader * msg
Definition: 005.c:2
void(* GNUNET_NAMESTORE_ContinuationWithStatus)(void *cls, int32_t success, const char *emsg)
Continuation called to notify client about result of the operation.
uint32_t label_len
Length of the name.
Definition: namestore.h:132
static char * pkey
Public key of the zone to look in, in ASCII.
static void handle_lookup_result(void *cls, const struct LabelLookupResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE.
#define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE
Service to client: result of zone-to-name lookup.
GNUNET_SCHEDULER_TaskCallback finish_cb
Function to call on completion.
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:901
static int check_zone_to_name_response(void *cls, const struct ZoneToNameResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
GNUNET_NAMESTORE_RecordMonitor proc
Function to call with the records we get back; or NULL.
Definition: namestore_api.c:84
#define GNUNET_GNS_EMPTY_LABEL_AT
String we use to indicate an empty label (top-level entry in the zone).
Ask for next result of zone iteration for the given operation.
Definition: namestore.h:368
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:163
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_lookup(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor rm, void *rm_cls)
Lookup an item in the namestore.
GNUNET_MQ_Error
Error codes for the queue.
ssize_t GNUNET_GNSRECORD_records_serialize(unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, size_t dest_size, char *dest)
Serialize the given records to the given destination buffer.
struct GNUNET_NAMESTORE_ZoneIterator * GNUNET_NAMESTORE_zone_iteration_start(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls, GNUNET_SCHEDULER_TaskCallback finish_cb, void *finish_cb_cls)
Starts a new zone iteration (used to periodically PUT all of our records into our DHT)...
GNUNET_NAMESTORE_RecordMonitor proc
The continuation to call with the results.
uint16_t reserved
always zero (for alignment)
Definition: namestore.h:283
uint32_t r_id
Request ID in NBO.
Definition: namestore.h:51
uint16_t rd_count
Number of records contained.
Definition: namestore.h:168
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
GNUNET_SCHEDULER_TaskCallback error_cb
Function to call on errors.
Definition: namestore_api.c:94
struct GNUNET_NAMESTORE_QueueEntry * prev
Kept in a DLL.
Definition: namestore_api.c:64
uint32_t op_id
The operation id this zone iteration operation has.
Start a zone iteration for the given zone.
Definition: namestore.h:350
void * proc_cls
Closure for proc.
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME.
Definition: namestore.h:197
uint16_t name_len
Length of the name.
Definition: namestore.h:224
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
void(* GNUNET_NAMESTORE_RecordMonitor)(void *cls, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd)
Process a record that was stored in the namestore.
static void handle_record_store_response(void *cls, const struct RecordStoreResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE.
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
int16_t res
result in NBO: GNUNET_OK on success, GNUNET_NO if there were no results, GNUNET_SYSERR on error ...
Definition: namestore.h:240
#define GNUNET_NO
Definition: gnunet_common.h:81
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:78
size_t data_size
Number of bytes in data.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
void GNUNET_NAMESTORE_zone_iterator_next(struct GNUNET_NAMESTORE_ZoneIterator *it, uint64_t limit)
Calls the record processor specified in GNUNET_NAMESTORE_zone_iteration_start for the next record...
static void handle_record_result_end(void *cls, const struct GNUNET_NAMESTORE_Header *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END.
Private ECC key encoded for transmission.
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT_END
Service to client: end of list of results.
int GNUNET_GNSRECORD_records_deserialize(size_t len, const char *src, unsigned int rd_count, struct GNUNET_GNSRECORD_Data *dest)
Deserialize the given records to the given destination.
void * error_cb_cls
Closure for error_cb.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP
Client to service: lookup label.
void * proc_cls
Closure for proc.
Definition: namestore_api.c:89
void * error_cb_cls
Closure for error_cb.
Definition: namestore_api.c:99
#define GNUNET_GNSRECORD_TYPE_NICK
Record type for GNS nick names ("NICK").
#define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP
Client to service: stop iterating.
uint16_t reserved
always zero (for alignment)
Definition: namestore.h:88
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
uint32_t last_op_id_used
The last operation id used for a NAMESTORE operation.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
static int check_record_result(void *cls, const struct RecordResultMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT.
void GNUNET_NAMESTORE_cancel(struct GNUNET_NAMESTORE_QueueEntry *qe)
Cancel a namestore operation.
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
#define _(String)
GNU gettext support macro.
Definition: platform.h:208
static void free_ze(struct GNUNET_NAMESTORE_ZoneIterator *ze)
Free ze.
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_records_store(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *label, unsigned int rd_count, const struct GNUNET_GNSRECORD_Data *rd, GNUNET_NAMESTORE_ContinuationWithStatus cont, void *cont_cls)
Store an item in the namestore.
uint16_t name_len
Name length.
Definition: namestore.h:268
common internal definitions for namestore service
static char * zone
Name of the zone we manage.
uint16_t rd_count
Number of records contained.
Definition: namestore.h:234
int32_t op_result
GNUNET_SYSERR on failure, GNUNET_OK on success
Definition: namestore.h:115
struct GNUNET_MessageHeader header
header.type will be GNUNET_MESSAGE_TYPE_NAMESTORE_* header.size will be message size ...
Definition: namestore.h:46
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key of the authority.
Definition: namestore.h:288
void GNUNET_NAMESTORE_disconnect(struct GNUNET_NAMESTORE_Handle *h)
Disconnect from the namestore service (and free associated resources).
cryptographic primitives for GNUnet
struct GNUNET_CRYPTO_EcdsaPublicKey value_zone
The public key of the target zone.
Definition: namestore.h:207
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
Handle for a zone iterator operation.
struct GNUNET_NAMESTORE_QueueEntry * next
Kept in a DLL.
Definition: namestore_api.c:59
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
Definition: scheduler.c:1246
struct GNUNET_TIME_Relative reconnect_delay
Delay introduced before we reconnect.
static int check_rd(size_t rd_len, const void *rd_buf, unsigned int rd_count)
Check that rd_buf of lenght rd_len contains rd_count records.
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:78
#define GNUNET_memcpy(dst, src, n)
const void * data
Binary value stored in the DNS record.
uint16_t rd_count
Number of records contained.
Definition: namestore.h:278
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE
Client to service: store records (as authority)
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
static void handle_record_result(void *cls, const struct RecordResultMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT.
Connection to the NAMESTORE service.
struct GNUNET_MQ_Handle * mq
Connection to the service (if available).
uint64_t expiration_time
Expiration time for the DNS record.
static void force_reconnect(struct GNUNET_NAMESTORE_Handle *h)
Disconnect from service and then reconnect.
static struct GNUNET_NAMESTORE_ZoneIterator * find_zi(struct GNUNET_NAMESTORE_Handle *h, uint32_t rid)
Find the zone iteration entry that matches the rid.
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:727
#define NAMESTORE_DELAY_TOLERANCE
We grant the namestore up to 1 minute of latency, if it is slower than that, store queries will fail...
Definition: namestore_api.c:47
Stop zone iteration for the given operation.
Definition: namestore.h:388
uint16_t name_len
Name length.
Definition: namestore.h:73
struct GNUNET_MQ_Envelope * env
Envelope of the message to send to the service, if not yet sent.
uint16_t name_len
Name length.
Definition: namestore.h:158
This is a private record of this peer and it should thus not be handed out to other peers...
void GNUNET_NAMESTORE_zone_iteration_stop(struct GNUNET_NAMESTORE_ZoneIterator *it)
Stops iteration and releases the namestore handle for further calls.
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START.
Definition: namestore.h:355
Message handler for a specific message type.
static int res
static void warn_delay(void *cls)
Task launched to warn the user that the namestore is excessively slow and that a query was thus dropp...
uint16_t rd_count
Number of records contained.
Definition: namestore.h:83
static struct GNUNET_REVOCATION_Query * q
Handle for revocation query.
void GNUNET_MQ_discard(struct GNUNET_MQ_Envelope *mqm)
Discard the message queue message, free all allocated resources.
Definition: mq.c:321
#define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME
Client to service: "reverse" lookup for zone name based on zone key.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
uint64_t GNUNET_htonll(uint64_t n)
Convert unsigned 64-bit integer to network byte order.
Definition: common_endian.c:35
struct GNUNET_NAMESTORE_QueueEntry * op_tail
Tail of pending namestore queue entries.
struct GNUNET_NAMESTORE_ZoneIterator * next
Kept in a DLL.
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
The private key of the zone that contained the name.
Definition: namestore.h:245
#define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_START
Client to service: please start iteration; receives "GNUNET_MESSAGE_TYPE_NAMESTORE_LOOKUP_NAME_RESPON...
#define GNUNET_SYSERR
Definition: gnunet_common.h:79
#define GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT
Client to service: next record(s) in iteration please.
An QueueEntry used to store information for a pending NAMESTORE record operation. ...
Definition: namestore_api.c:53
struct GNUNET_NAMESTORE_Handle * h
Main handle to access the namestore.
struct GNUNET_NAMESTORE_Handle * GNUNET_NAMESTORE_connect(const struct GNUNET_CONFIGURATION_Handle *cfg)
Initialize the connection with the NAMESTORE service.
const char * name
struct GNUNET_NAMESTORE_ZoneIterator * prev
Kept in a DLL.
uint32_t op_id
The operation id this zone iteration operation has.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
uint16_t found
Was the label found in the database?? GNUNET_YES or GNUNET_NO.
Definition: namestore.h:174
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:273
#define GNUNET_TIME_STD_BACKOFF(r)
Perform our standard exponential back-off calculation, starting at 1 ms and then going by a factor of...
GNUNET_NAMESTORE_ContinuationWithStatus cont
Continuation to call.
Definition: namestore_api.c:74
static void reconnect(struct GNUNET_NAMESTORE_Handle *h)
Reconnect to namestore service.
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT.
Definition: namestore.h:263
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
static void free_qe(struct GNUNET_NAMESTORE_QueueEntry *qe)
Free qe.
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE
Service to client: lookup label.
int reconnect
Should we reconnect to service due to some serious error?
#define LOG(kind,...)
Definition: namestore_api.c:41
Handle to a message queue.
Definition: mq.c:85
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
Definition: namestore.h:219
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key of the authority.
Definition: namestore.h:93
void * finish_cb_cls
Closure for error_cb.
Store a record to the namestore (as authority).
Definition: namestore.h:58
Respone for zone to name lookup.
Definition: namestore.h:214
void * cont_cls
Closure for cont.
Definition: namestore_api.c:79
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
The private key of the zone to look up in.
Definition: namestore.h:202
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE.
Definition: namestore.h:153
uint32_t record_type
Type of the GNS/DNS record.
configuration data
Definition: configuration.c:85
Response to a record storage request.
Definition: namestore.h:105
struct GNUNET_NAMESTORE_QueueEntry * op_head
Head of pending namestore queue entries.
Public ECC key (always for Curve25519) encoded in a format suitable for network transmission and ECDS...
struct GNUNET_NAMESTORE_ZoneIterator * z_head
Head of pending namestore zone iterator entries.
#define MAX_NAME_LEN
Maximum length of any name, including 0-termination.
Definition: namecache.h:33
static uint32_t get_op_id(struct GNUNET_NAMESTORE_Handle *h)
Get a fresh operation id to distinguish between namestore requests.
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_zone_to_name(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, GNUNET_SCHEDULER_TaskCallback error_cb, void *error_cb_cls, GNUNET_NAMESTORE_RecordMonitor proc, void *proc_cls)
Look for an existing PKEY delegation record for a given public key.
#define GNUNET_log(kind,...)
struct GNUNET_CRYPTO_EcdsaPrivateKey private_key
The private key of the authority.
Definition: namestore.h:179
Entry in list of pending tasks.
Definition: scheduler.c:134
Generic namestore message with op id.
Definition: namestore.h:40
static void handle_zone_to_name_response(void *cls, const struct ZoneToNameResponseMessage *msg)
Handle an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_TO_NAME_RESPONSE.
struct GNUNET_SCHEDULER_Task * timeout_task
Task scheduled to warn us if the namestore is way too slow.
uint16_t rd_len
Length of serialized record data.
Definition: namestore.h:229
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP.
Definition: namestore.h:127
Lookup a name for a zone hash.
Definition: namestore.h:192
Lookup a label.
Definition: namestore.h:122
struct GNUNET_SCHEDULER_Task * reconnect_task
Reconnect task.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:824
#define GNUNET_YES
Definition: gnunet_common.h:80
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:353
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_RESULT
Service to client: here is a (plaintext) record you requested.
struct GNUNET_NAMESTORE_ZoneIterator * z_tail
Tail of pending namestore zone iterator entries.
Record is returned from the namestore (as authority).
Definition: namestore.h:258
uint64_t limit
Number of records to return to the iterator in one shot (before GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_IT...
Definition: namestore.h:380
enum GNUNET_GNSRECORD_Flags flags
Flags for the record.
static struct GNUNET_NAMESTORE_QueueEntry * find_qe(struct GNUNET_NAMESTORE_Handle *h, uint32_t rid)
Find the queue entry that matches the rid.
static void mq_error_handler(void *cls, enum GNUNET_MQ_Error error)
Generic error handler, called with the appropriate error code and the same closure specified at the c...
struct GNUNET_NAMESTORE_QueueEntry * GNUNET_NAMESTORE_set_nick(struct GNUNET_NAMESTORE_Handle *h, const struct GNUNET_CRYPTO_EcdsaPrivateKey *pkey, const char *nick, GNUNET_NAMESTORE_ContinuationWithStatus cont, void *cont_cls)
Set the desired nick name for a zone.
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
Zone key.
Definition: namestore.h:360
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_NEXT.
Definition: namestore.h:373
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
struct GNUNET_NAMESTORE_Header gns_header
Type will be GNUNET_MESSAGE_TYPE_NAMESTORE_ZONE_ITERATION_STOP.
Definition: namestore.h:393
Lookup a label.
Definition: namestore.h:148
static int check_lookup_result(void *cls, const struct LabelLookupResponseMessage *msg)
Check validity of an incoming message of type GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_LOOKUP_RESPONSE.
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
The private key of the zone to look up in.
Definition: namestore.h:137
static void reconnect_task(void *cls)
Re-establish the connection to the service.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:965
#define GNUNET_MESSAGE_TYPE_NAMESTORE_RECORD_STORE_RESPONSE
Service to client: result of store operation.
struct GNUNET_CRYPTO_EcdsaPrivateKey zone
Private key of the zone.