aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2018-04-10 13:02:28 +0200
committerChristian Grothoff <christian@grothoff.org>2018-04-10 13:02:51 +0200
commit0cd0e3a5287af73bb81219ebc9548395a8945ce8 (patch)
treeb5bb24d78a4eed519b37e6fb8f9291a6282d06d4 /src
parent3eae3e43d6f94449cb89998a676e6696438cc604 (diff)
downloadgnunet-0cd0e3a5287af73bb81219ebc9548395a8945ce8.tar.gz
gnunet-0cd0e3a5287af73bb81219ebc9548395a8945ce8.zip
more work on gnunet-zoneimport, some bugfix in flat namestore, misc. style fixes
Diffstat (limited to 'src')
-rw-r--r--src/dns/dnsparser.c38
-rw-r--r--src/gns/plugin_gnsrecord_gns.c12
-rw-r--r--src/include/gnunet_gnsrecord_plugin.h30
-rw-r--r--src/include/gnunet_namestore_plugin.h55
-rw-r--r--src/include/gnunet_program_lib.h4
-rw-r--r--src/namestore/.gitignore1
-rw-r--r--src/namestore/Makefile.am12
-rw-r--r--src/namestore/gnunet-namestore-fcfsd.c93
-rw-r--r--src/namestore/gnunet-namestore.c43
-rw-r--r--src/namestore/gnunet-zoneimport.c896
-rw-r--r--src/namestore/namestore_api_monitor.c4
-rw-r--r--src/namestore/plugin_namestore_flat.c142
-rw-r--r--src/namestore/plugin_namestore_postgres.c13
-rw-r--r--src/namestore/plugin_namestore_sqlite.c14
-rw-r--r--src/namestore/plugin_rest_namestore.c58
-rw-r--r--src/util/program.c89
16 files changed, 1309 insertions, 195 deletions
diff --git a/src/dns/dnsparser.c b/src/dns/dnsparser.c
index ab833f7c5..7e200ee7c 100644
--- a/src/dns/dnsparser.c
+++ b/src/dns/dnsparser.c
@@ -919,9 +919,14 @@ GNUNET_DNSPARSER_builder_add_mx (char *dst,
919 if (*off + sizeof (uint16_t) > dst_len) 919 if (*off + sizeof (uint16_t) > dst_len)
920 return GNUNET_NO; 920 return GNUNET_NO;
921 mxpref = htons (mx->preference); 921 mxpref = htons (mx->preference);
922 GNUNET_memcpy (&dst[*off], &mxpref, sizeof (mxpref)); 922 GNUNET_memcpy (&dst[*off],
923 &mxpref,
924 sizeof (mxpref));
923 (*off) += sizeof (mxpref); 925 (*off) += sizeof (mxpref);
924 return GNUNET_DNSPARSER_builder_add_name (dst, dst_len, off, mx->mxhost); 926 return GNUNET_DNSPARSER_builder_add_name (dst,
927 dst_len,
928 off,
929 mx->mxhost);
925} 930}
926 931
927 932
@@ -1081,21 +1086,36 @@ add_record (char *dst,
1081 switch (record->type) 1086 switch (record->type)
1082 { 1087 {
1083 case GNUNET_DNSPARSER_TYPE_MX: 1088 case GNUNET_DNSPARSER_TYPE_MX:
1084 ret = GNUNET_DNSPARSER_builder_add_mx (dst, dst_len, &pos, record->data.mx); 1089 ret = GNUNET_DNSPARSER_builder_add_mx (dst,
1090 dst_len,
1091 &pos,
1092 record->data.mx);
1085 break; 1093 break;
1086 case GNUNET_DNSPARSER_TYPE_CERT: 1094 case GNUNET_DNSPARSER_TYPE_CERT:
1087 ret = GNUNET_DNSPARSER_builder_add_cert (dst, dst_len, &pos, record->data.cert); 1095 ret = GNUNET_DNSPARSER_builder_add_cert (dst,
1096 dst_len,
1097 &pos,
1098 record->data.cert);
1088 break; 1099 break;
1089 case GNUNET_DNSPARSER_TYPE_SOA: 1100 case GNUNET_DNSPARSER_TYPE_SOA:
1090 ret = GNUNET_DNSPARSER_builder_add_soa (dst, dst_len, &pos, record->data.soa); 1101 ret = GNUNET_DNSPARSER_builder_add_soa (dst,
1102 dst_len,
1103 &pos,
1104 record->data.soa);
1091 break; 1105 break;
1092 case GNUNET_DNSPARSER_TYPE_NS: 1106 case GNUNET_DNSPARSER_TYPE_NS:
1093 case GNUNET_DNSPARSER_TYPE_CNAME: 1107 case GNUNET_DNSPARSER_TYPE_CNAME:
1094 case GNUNET_DNSPARSER_TYPE_PTR: 1108 case GNUNET_DNSPARSER_TYPE_PTR:
1095 ret = GNUNET_DNSPARSER_builder_add_name (dst, dst_len, &pos, record->data.hostname); 1109 ret = GNUNET_DNSPARSER_builder_add_name (dst,
1110 dst_len,
1111 &pos,
1112 record->data.hostname);
1096 break; 1113 break;
1097 case GNUNET_DNSPARSER_TYPE_SRV: 1114 case GNUNET_DNSPARSER_TYPE_SRV:
1098 ret = GNUNET_DNSPARSER_builder_add_srv (dst, dst_len, &pos, record->data.srv); 1115 ret = GNUNET_DNSPARSER_builder_add_srv (dst,
1116 dst_len,
1117 &pos,
1118 record->data.srv);
1099 break; 1119 break;
1100 default: 1120 default:
1101 if (pos + record->data.raw.data_len > dst_len) 1121 if (pos + record->data.raw.data_len > dst_len)
@@ -1103,7 +1123,9 @@ add_record (char *dst,
1103 ret = GNUNET_NO; 1123 ret = GNUNET_NO;
1104 break; 1124 break;
1105 } 1125 }
1106 GNUNET_memcpy (&dst[pos], record->data.raw.data, record->data.raw.data_len); 1126 GNUNET_memcpy (&dst[pos],
1127 record->data.raw.data,
1128 record->data.raw.data_len);
1107 pos += record->data.raw.data_len; 1129 pos += record->data.raw.data_len;
1108 ret = GNUNET_OK; 1130 ret = GNUNET_OK;
1109 break; 1131 break;
diff --git a/src/gns/plugin_gnsrecord_gns.c b/src/gns/plugin_gnsrecord_gns.c
index 6adad0f34..07d1a9c04 100644
--- a/src/gns/plugin_gnsrecord_gns.c
+++ b/src/gns/plugin_gnsrecord_gns.c
@@ -173,7 +173,9 @@ gns_string_to_value (void *cls,
173 173
174 case GNUNET_GNSRECORD_TYPE_PKEY: 174 case GNUNET_GNSRECORD_TYPE_PKEY:
175 if (GNUNET_OK != 175 if (GNUNET_OK !=
176 GNUNET_CRYPTO_ecdsa_public_key_from_string (s, strlen (s), &pkey)) 176 GNUNET_CRYPTO_ecdsa_public_key_from_string (s,
177 strlen (s),
178 &pkey))
177 { 179 {
178 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 180 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
179 _("Unable to parse PKEY record `%s'\n"), 181 _("Unable to parse PKEY record `%s'\n"),
@@ -181,7 +183,9 @@ gns_string_to_value (void *cls,
181 return GNUNET_SYSERR; 183 return GNUNET_SYSERR;
182 } 184 }
183 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey); 185 *data = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPublicKey);
184 GNUNET_memcpy (*data, &pkey, sizeof (pkey)); 186 GNUNET_memcpy (*data,
187 &pkey,
188 sizeof (pkey));
185 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey); 189 *data_size = sizeof (struct GNUNET_CRYPTO_EcdsaPublicKey);
186 return GNUNET_OK; 190 return GNUNET_OK;
187 191
@@ -234,7 +238,9 @@ gns_string_to_value (void *cls,
234 GNUNET_free (cpy); 238 GNUNET_free (cpy);
235 *data_size = off; 239 *data_size = off;
236 *data = GNUNET_malloc (off); 240 *data = GNUNET_malloc (off);
237 GNUNET_memcpy (*data, nsbuf, off); 241 GNUNET_memcpy (*data,
242 nsbuf,
243 off);
238 return GNUNET_OK; 244 return GNUNET_OK;
239 } 245 }
240 case GNUNET_GNSRECORD_TYPE_VPN: 246 case GNUNET_GNSRECORD_TYPE_VPN:
diff --git a/src/include/gnunet_gnsrecord_plugin.h b/src/include/gnunet_gnsrecord_plugin.h
index d212c3663..e3e1a98eb 100644
--- a/src/include/gnunet_gnsrecord_plugin.h
+++ b/src/include/gnunet_gnsrecord_plugin.h
@@ -53,10 +53,11 @@ extern "C"
53 * @param data_size number of bytes in @a data 53 * @param data_size number of bytes in @a data
54 * @return NULL on error, otherwise human-readable representation of the value 54 * @return NULL on error, otherwise human-readable representation of the value
55 */ 55 */
56typedef char * (*GNUNET_GNSRECORD_ValueToStringFunction) (void *cls, 56typedef char *
57 uint32_t type, 57(*GNUNET_GNSRECORD_ValueToStringFunction) (void *cls,
58 const void *data, 58 uint32_t type,
59 size_t data_size); 59 const void *data,
60 size_t data_size);
60 61
61 62
62/** 63/**
@@ -71,11 +72,12 @@ typedef char * (*GNUNET_GNSRECORD_ValueToStringFunction) (void *cls,
71 * @param data_size set to number of bytes in @a data 72 * @param data_size set to number of bytes in @a data
72 * @return #GNUNET_OK on success 73 * @return #GNUNET_OK on success
73 */ 74 */
74typedef int (*GNUNET_GNSRECORD_StringToValueFunction) (void *cls, 75typedef int
75 uint32_t type, 76(*GNUNET_GNSRECORD_StringToValueFunction) (void *cls,
76 const char *s, 77 uint32_t type,
77 void **data, 78 const char *s,
78 size_t *data_size); 79 void **data,
80 size_t *data_size);
79 81
80 82
81/** 83/**
@@ -86,8 +88,9 @@ typedef int (*GNUNET_GNSRECORD_StringToValueFunction) (void *cls,
86 * @param dns_typename name to convert 88 * @param dns_typename name to convert
87 * @return corresponding number, UINT32_MAX on error 89 * @return corresponding number, UINT32_MAX on error
88 */ 90 */
89typedef uint32_t (*GNUNET_GNSRECORD_TypenameToNumberFunction) (void *cls, 91typedef uint32_t
90 const char *dns_typename); 92(*GNUNET_GNSRECORD_TypenameToNumberFunction) (void *cls,
93 const char *dns_typename);
91 94
92 95
93/** 96/**
@@ -98,8 +101,9 @@ typedef uint32_t (*GNUNET_GNSRECORD_TypenameToNumberFunction) (void *cls,
98 * @param type number of a type to convert 101 * @param type number of a type to convert
99 * @return corresponding typestring, NULL on error 102 * @return corresponding typestring, NULL on error
100 */ 103 */
101typedef const char * (*GNUNET_GNSRECORD_NumberToTypenameFunction) (void *cls, 104typedef const char *
102 uint32_t type); 105(*GNUNET_GNSRECORD_NumberToTypenameFunction) (void *cls,
106 uint32_t type);
103 107
104 108
105/** 109/**
diff --git a/src/include/gnunet_namestore_plugin.h b/src/include/gnunet_namestore_plugin.h
index 3ec0299ba..48965b3b6 100644
--- a/src/include/gnunet_namestore_plugin.h
+++ b/src/include/gnunet_namestore_plugin.h
@@ -52,11 +52,12 @@ extern "C"
52 * @param rd_count number of entries in @a rd array 52 * @param rd_count number of entries in @a rd array
53 * @param rd array of records with data to store 53 * @param rd array of records with data to store
54 */ 54 */
55typedef void (*GNUNET_NAMESTORE_RecordIterator) (void *cls, 55typedef void
56 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key, 56(*GNUNET_NAMESTORE_RecordIterator) (void *cls,
57 const char *label, 57 const struct GNUNET_CRYPTO_EcdsaPrivateKey *private_key,
58 unsigned int rd_count, 58 const char *label,
59 const struct GNUNET_GNSRECORD_Data *rd); 59 unsigned int rd_count,
60 const struct GNUNET_GNSRECORD_Data *rd);
60 61
61 62
62/** 63/**
@@ -81,12 +82,13 @@ struct GNUNET_NAMESTORE_PluginFunctions
81 * @param rd array of records with data to store 82 * @param rd array of records with data to store
82 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 83 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
83 */ 84 */
84 int (*store_records) (void *cls, 85 int
85 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 86 (*store_records) (void *cls,
86 const char *label, 87 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
87 unsigned int rd_count, 88 const char *label,
88 const struct GNUNET_GNSRECORD_Data *rd); 89 unsigned int rd_count,
89 90 const struct GNUNET_GNSRECORD_Data *rd);
91
90 /** 92 /**
91 * Lookup records in the datastore for which we are the authority. 93 * Lookup records in the datastore for which we are the authority.
92 * 94 *
@@ -97,11 +99,12 @@ struct GNUNET_NAMESTORE_PluginFunctions
97 * @param iter_cls closure for @a iter 99 * @param iter_cls closure for @a iter
98 * @return #GNUNET_OK on success, else #GNUNET_SYSERR 100 * @return #GNUNET_OK on success, else #GNUNET_SYSERR
99 */ 101 */
100 int (*lookup_records) (void *cls, 102 int
101 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 103 (*lookup_records) (void *cls,
102 const char *label, 104 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
103 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls); 105 const char *label,
104 106 GNUNET_NAMESTORE_RecordIterator iter,
107 void *iter_cls);
105 108
106 109
107 /** 110 /**
@@ -115,10 +118,12 @@ struct GNUNET_NAMESTORE_PluginFunctions
115 * @param iter_cls closure for @a iter 118 * @param iter_cls closure for @a iter
116 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 119 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
117 */ 120 */
118 int (*iterate_records) (void *cls, 121 int
119 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 122 (*iterate_records) (void *cls,
120 uint64_t offset, 123 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
121 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls); 124 uint64_t offset,
125 GNUNET_NAMESTORE_RecordIterator iter,
126 void *iter_cls);
122 127
123 128
124 /** 129 /**
@@ -132,10 +137,12 @@ struct GNUNET_NAMESTORE_PluginFunctions
132 * @param iter_cls closure for @a iter 137 * @param iter_cls closure for @a iter
133 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error 138 * @return #GNUNET_OK on success, #GNUNET_NO if there were no results, #GNUNET_SYSERR on error
134 */ 139 */
135 int (*zone_to_name) (void *cls, 140 int
136 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 141 (*zone_to_name) (void *cls,
137 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, 142 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
138 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls); 143 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
144 GNUNET_NAMESTORE_RecordIterator iter,
145 void *iter_cls);
139 146
140 147
141}; 148};
diff --git a/src/include/gnunet_program_lib.h b/src/include/gnunet_program_lib.h
index 526760bdd..65be7ab3d 100644
--- a/src/include/gnunet_program_lib.h
+++ b/src/include/gnunet_program_lib.h
@@ -84,6 +84,7 @@ GNUNET_PROGRAM_run2 (int argc,
84 void *task_cls, 84 void *task_cls,
85 int run_without_scheduler); 85 int run_without_scheduler);
86 86
87
87/** 88/**
88 * Run a standard GNUnet command startup sequence (initialize loggers 89 * Run a standard GNUnet command startup sequence (initialize loggers
89 * and configuration, parse options). 90 * and configuration, parse options).
@@ -103,7 +104,8 @@ GNUNET_PROGRAM_run (int argc,
103 const char *binaryName, 104 const char *binaryName,
104 const char *binaryHelp, 105 const char *binaryHelp,
105 const struct GNUNET_GETOPT_CommandLineOption *options, 106 const struct GNUNET_GETOPT_CommandLineOption *options,
106 GNUNET_PROGRAM_Main task, void *task_cls); 107 GNUNET_PROGRAM_Main task,
108 void *task_cls);
107 109
108 110
109#if 0 /* keep Emacsens' auto-indent happy */ 111#if 0 /* keep Emacsens' auto-indent happy */
diff --git a/src/namestore/.gitignore b/src/namestore/.gitignore
index 4995a9e36..9a4c615ba 100644
--- a/src/namestore/.gitignore
+++ b/src/namestore/.gitignore
@@ -19,3 +19,4 @@ test_namestore_api_zone_iteration_stop.nc
19test_plugin_namestore_postgres 19test_plugin_namestore_postgres
20test_plugin_namestore_sqlite 20test_plugin_namestore_sqlite
21test_plugin_namestore_flat 21test_plugin_namestore_flat
22gnunet-zoneimport
diff --git a/src/namestore/Makefile.am b/src/namestore/Makefile.am
index a848d4b72..cdfed543d 100644
--- a/src/namestore/Makefile.am
+++ b/src/namestore/Makefile.am
@@ -126,12 +126,24 @@ libexec_PROGRAMS = \
126bin_PROGRAMS = \ 126bin_PROGRAMS = \
127 gnunet-namestore 127 gnunet-namestore
128 128
129noinst_PROGRAMS = \
130 gnunet-zoneimport
131
129if HAVE_MHD 132if HAVE_MHD
130libexec_PROGRAMS += \ 133libexec_PROGRAMS += \
131 gnunet-namestore-fcfsd 134 gnunet-namestore-fcfsd
132endif 135endif
133 136
134 137
138gnunet_zoneimport_SOURCES = \
139 gnunet-zoneimport.c
140gnunet_zoneimport_LDADD = \
141 $(top_builddir)/src/gnsrecord/libgnunetgnsrecord.la \
142 $(top_builddir)/src/dns/libgnunetdnsparser.la \
143 $(top_builddir)/src/dns/libgnunetdnsstub.la \
144 $(top_builddir)/src/util/libgnunetutil.la \
145 $(GN_LIBINTL)
146
135gnunet_namestore_SOURCES = \ 147gnunet_namestore_SOURCES = \
136 gnunet-namestore.c 148 gnunet-namestore.c
137gnunet_namestore_LDADD = \ 149gnunet_namestore_LDADD = \
diff --git a/src/namestore/gnunet-namestore-fcfsd.c b/src/namestore/gnunet-namestore-fcfsd.c
index b38761d5a..ddd609918 100644
--- a/src/namestore/gnunet-namestore-fcfsd.c
+++ b/src/namestore/gnunet-namestore-fcfsd.c
@@ -480,10 +480,16 @@ post_iterator (void *cls,
480 const char *filename, 480 const char *filename,
481 const char *content_type, 481 const char *content_type,
482 const char *transfer_encoding, 482 const char *transfer_encoding,
483 const char *data, uint64_t off, size_t size) 483 const char *data,
484 uint64_t off,
485 size_t size)
484{ 486{
485 struct Request *request = cls; 487 struct Request *request = cls;
486 488
489 (void) kind;
490 (void) filename;
491 (void) content_type;
492 (void) transfer_encoding;
487 if (0 == strcmp ("domain", key)) 493 if (0 == strcmp ("domain", key))
488 { 494 {
489 if (size + off >= sizeof(request->domain_name)) 495 if (size + off >= sizeof(request->domain_name))
@@ -578,7 +584,6 @@ zone_to_name_cb (void *cls,
578 struct GNUNET_GNSRECORD_Data r; 584 struct GNUNET_GNSRECORD_Data r;
579 585
580 request->qe = NULL; 586 request->qe = NULL;
581
582 if (0 != rd_count) 587 if (0 != rd_count)
583 { 588 {
584 GNUNET_log (GNUNET_ERROR_TYPE_INFO, 589 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
@@ -726,8 +731,10 @@ create_response (void *cls,
726 { 731 {
727 request = GNUNET_new (struct Request); 732 request = GNUNET_new (struct Request);
728 *ptr = request; 733 *ptr = request;
729 request->pp = MHD_create_post_processor (connection, 1024, 734 request->pp = MHD_create_post_processor (connection,
730 &post_iterator, request); 735 1024,
736 &post_iterator,
737 request);
731 if (NULL == request->pp) 738 if (NULL == request->pp)
732 { 739 {
733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
@@ -770,7 +777,8 @@ create_response (void *cls,
770 _("Domain name must not contain `.'\n")); 777 _("Domain name must not contain `.'\n"));
771 request->phase = RP_FAIL; 778 request->phase = RP_FAIL;
772 return fill_s_reply ("Domain name must not contain `.', sorry.", 779 return fill_s_reply ("Domain name must not contain `.', sorry.",
773 request, connection); 780 request,
781 connection);
774 } 782 }
775 if (NULL != strchr (request->domain_name, (int) '+')) 783 if (NULL != strchr (request->domain_name, (int) '+'))
776 { 784 {
@@ -781,13 +789,14 @@ create_response (void *cls,
781 request, connection); 789 request, connection);
782 } 790 }
783 request->phase = RP_LOOKUP; 791 request->phase = RP_LOOKUP;
784 request->qe = GNUNET_NAMESTORE_records_lookup (ns, 792 request->qe
785 &fcfs_zone_pkey, 793 = GNUNET_NAMESTORE_records_lookup (ns,
786 request->domain_name, 794 &fcfs_zone_pkey,
787 &lookup_block_error, 795 request->domain_name,
788 request, 796 &lookup_block_error,
789 &lookup_block_processor, 797 request,
790 request); 798 &lookup_block_processor,
799 request);
791 break; 800 break;
792 case RP_LOOKUP: 801 case RP_LOOKUP:
793 break; 802 break;
@@ -834,6 +843,9 @@ request_completed_callback (void *cls,
834{ 843{
835 struct Request *request = *con_cls; 844 struct Request *request = *con_cls;
836 845
846 (void) cls;
847 (void) connection;
848 (void) toe;
837 if (NULL == request) 849 if (NULL == request)
838 return; 850 return;
839 if (NULL != request->pp) 851 if (NULL != request->pp)
@@ -871,19 +883,34 @@ run_httpd ()
871 wes = GNUNET_NETWORK_fdset_create (); 883 wes = GNUNET_NETWORK_fdset_create ();
872 wws = GNUNET_NETWORK_fdset_create (); 884 wws = GNUNET_NETWORK_fdset_create ();
873 max = -1; 885 max = -1;
874 GNUNET_assert (MHD_YES == MHD_get_fdset (httpd, &rs, &ws, &es, &max)); 886 GNUNET_assert (MHD_YES ==
875 haveto = MHD_get_timeout (httpd, &timeout); 887 MHD_get_fdset (httpd,
888 &rs,
889 &ws,
890 &es,
891 &max));
892 haveto = MHD_get_timeout (httpd,
893 &timeout);
876 if (haveto == MHD_YES) 894 if (haveto == MHD_YES)
877 tv.rel_value_us = (uint64_t) timeout * 1000LL; 895 tv.rel_value_us = (uint64_t) timeout * 1000LL;
878 else 896 else
879 tv = GNUNET_TIME_UNIT_FOREVER_REL; 897 tv = GNUNET_TIME_UNIT_FOREVER_REL;
880 GNUNET_NETWORK_fdset_copy_native (wrs, &rs, max + 1); 898 GNUNET_NETWORK_fdset_copy_native (wrs,
881 GNUNET_NETWORK_fdset_copy_native (wws, &ws, max + 1); 899 &rs,
882 GNUNET_NETWORK_fdset_copy_native (wes, &es, max + 1); 900 max + 1);
901 GNUNET_NETWORK_fdset_copy_native (wws,
902 &ws,
903 max + 1);
904 GNUNET_NETWORK_fdset_copy_native (wes,
905 &es,
906 max + 1);
883 httpd_task = 907 httpd_task =
884 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH, 908 GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_HIGH,
885 tv, wrs, wws, 909 tv,
886 &do_httpd, NULL); 910 wrs,
911 wws,
912 &do_httpd,
913 NULL);
887 GNUNET_NETWORK_fdset_destroy (wrs); 914 GNUNET_NETWORK_fdset_destroy (wrs);
888 GNUNET_NETWORK_fdset_destroy (wws); 915 GNUNET_NETWORK_fdset_destroy (wws);
889 GNUNET_NETWORK_fdset_destroy (wes); 916 GNUNET_NETWORK_fdset_destroy (wes);
@@ -898,6 +925,7 @@ run_httpd ()
898static void 925static void
899do_httpd (void *cls) 926do_httpd (void *cls)
900{ 927{
928 (void) cls;
901 httpd_task = NULL; 929 httpd_task = NULL;
902 MHD_run (httpd); 930 MHD_run (httpd);
903 run_httpd (); 931 run_httpd ();
@@ -912,6 +940,7 @@ do_httpd (void *cls)
912static void 940static void
913do_shutdown (void *cls) 941do_shutdown (void *cls)
914{ 942{
943 (void) cls;
915 if (NULL != httpd_task) 944 if (NULL != httpd_task)
916 { 945 {
917 GNUNET_SCHEDULER_cancel (httpd_task); 946 GNUNET_SCHEDULER_cancel (httpd_task);
@@ -967,6 +996,9 @@ identity_cb (void *cls,
967{ 996{
968 int options; 997 int options;
969 998
999 (void) cls;
1000 (void) ctx;
1001 (void) name;
970 id_op = NULL; 1002 id_op = NULL;
971 if (NULL == ego) 1003 if (NULL == ego)
972 { 1004 {
@@ -1014,9 +1046,14 @@ identity_cb (void *cls,
1014 * @param cfg configuration 1046 * @param cfg configuration
1015 */ 1047 */
1016static void 1048static void
1017run (void *cls, char *const *args, const char *cfgfile, 1049run (void *cls,
1050 char *const *args,
1051 const char *cfgfile,
1018 const struct GNUNET_CONFIGURATION_Handle *cfg) 1052 const struct GNUNET_CONFIGURATION_Handle *cfg)
1019{ 1053{
1054 (void) cls;
1055 (void) args;
1056 (void) cfgfile;
1020 if (GNUNET_OK != 1057 if (GNUNET_OK !=
1021 GNUNET_CONFIGURATION_get_value_number (cfg, 1058 GNUNET_CONFIGURATION_get_value_number (cfg,
1022 "fcfsd", 1059 "fcfsd",
@@ -1056,21 +1093,27 @@ run (void *cls, char *const *args, const char *cfgfile,
1056 * @return 0 ok, 1 on error 1093 * @return 0 ok, 1 on error
1057 */ 1094 */
1058int 1095int
1059main (int argc, char *const *argv) 1096main (int argc,
1097 char *const *argv)
1060{ 1098{
1061 static const struct GNUNET_GETOPT_CommandLineOption options[] = { 1099 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1062 GNUNET_GETOPT_OPTION_END 1100 GNUNET_GETOPT_OPTION_END
1063 }; 1101 };
1064
1065 int ret; 1102 int ret;
1066 1103
1067 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv)) 1104 if (GNUNET_OK !=
1105 GNUNET_STRINGS_get_utf8_args (argc, argv,
1106 &argc, &argv))
1068 return 2; 1107 return 2;
1069 1108
1070 GNUNET_log_setup ("fcfsd", "WARNING", NULL); 1109 GNUNET_log_setup ("fcfsd",
1110 "WARNING",
1111 NULL);
1071 ret = 1112 ret =
1072 (GNUNET_OK == 1113 (GNUNET_OK ==
1073 GNUNET_PROGRAM_run (argc, argv, "fcfsd", 1114 GNUNET_PROGRAM_run (argc,
1115 argv,
1116 "gnunet-namestore-fcfsd",
1074 _("GNU Name System First Come First Serve name registration service"), 1117 _("GNU Name System First Come First Serve name registration service"),
1075 options, 1118 options,
1076 &run, NULL)) ? 0 : 1; 1119 &run, NULL)) ? 0 : 1;
diff --git a/src/namestore/gnunet-namestore.c b/src/namestore/gnunet-namestore.c
index 054417ab5..2eff995e0 100644
--- a/src/namestore/gnunet-namestore.c
+++ b/src/namestore/gnunet-namestore.c
@@ -203,6 +203,7 @@ static int monitor;
203static void 203static void
204do_shutdown (void *cls) 204do_shutdown (void *cls)
205{ 205{
206 (void) cls;
206 if (NULL != get_default) 207 if (NULL != get_default)
207 { 208 {
208 GNUNET_IDENTITY_cancel (get_default); 209 GNUNET_IDENTITY_cancel (get_default);
@@ -323,6 +324,7 @@ del_continuation (void *cls,
323 int32_t success, 324 int32_t success,
324 const char *emsg) 325 const char *emsg)
325{ 326{
327 (void) cls;
326 del_qe = NULL; 328 del_qe = NULL;
327 if (GNUNET_NO == success) 329 if (GNUNET_NO == success)
328 { 330 {
@@ -348,6 +350,7 @@ del_continuation (void *cls,
348static void 350static void
349zone_iteration_finished (void *cls) 351zone_iteration_finished (void *cls)
350{ 352{
353 (void) cls;
351 list_it = NULL; 354 list_it = NULL;
352 test_finished (); 355 test_finished ();
353} 356}
@@ -359,6 +362,7 @@ zone_iteration_finished (void *cls)
359static void 362static void
360zone_iteration_error_cb (void *cls) 363zone_iteration_error_cb (void *cls)
361{ 364{
365 (void) cls;
362 list_it = NULL; 366 list_it = NULL;
363 fprintf (stderr, 367 fprintf (stderr,
364 "Error iterating over zone\n"); 368 "Error iterating over zone\n");
@@ -385,11 +389,11 @@ display_record (void *cls,
385{ 389{
386 const char *typestring; 390 const char *typestring;
387 char *s; 391 char *s;
388 unsigned int i;
389 const char *ets; 392 const char *ets;
390 struct GNUNET_TIME_Absolute at; 393 struct GNUNET_TIME_Absolute at;
391 struct GNUNET_TIME_Relative rt; 394 struct GNUNET_TIME_Relative rt;
392 395
396 (void) cls;
393 if ( (NULL != name) && 397 if ( (NULL != name) &&
394 (0 != strcmp (name, rname)) ) 398 (0 != strcmp (name, rname)) )
395 { 399 {
@@ -399,7 +403,7 @@ display_record (void *cls,
399 FPRINTF (stdout, 403 FPRINTF (stdout,
400 "%s:\n", 404 "%s:\n",
401 rname); 405 rname);
402 for (i=0;i<rd_len;i++) 406 for (unsigned int i=0;i<rd_len;i++)
403 { 407 {
404 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) && 408 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
405 (0 != strcmp (rname, 409 (0 != strcmp (rname,
@@ -448,7 +452,10 @@ display_record (void *cls,
448static void 452static void
449sync_cb (void *cls) 453sync_cb (void *cls)
450{ 454{
451 FPRINTF (stdout, "%s", "Monitor is now in sync.\n"); 455 (void) cls;
456 FPRINTF (stdout,
457 "%s",
458 "Monitor is now in sync.\n");
452} 459}
453 460
454 461
@@ -460,7 +467,10 @@ sync_cb (void *cls)
460static void 467static void
461monitor_error_cb (void *cls) 468monitor_error_cb (void *cls)
462{ 469{
463 FPRINTF (stderr, "%s", "Monitor disconnected and out of sync.\n"); 470 (void) cls;
471 FPRINTF (stderr,
472 "%s",
473 "Monitor disconnected and out of sync.\n");
464} 474}
465 475
466 476
@@ -470,6 +480,7 @@ monitor_error_cb (void *cls)
470static void 480static void
471lookup_error_cb (void *cls) 481lookup_error_cb (void *cls)
472{ 482{
483 (void) cls;
473 add_qe = NULL; 484 add_qe = NULL;
474 GNUNET_break (0); 485 GNUNET_break (0);
475 ret = 1; 486 ret = 1;
@@ -496,8 +507,8 @@ get_existing_record (void *cls,
496{ 507{
497 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1]; 508 struct GNUNET_GNSRECORD_Data rdn[rd_count + 1];
498 struct GNUNET_GNSRECORD_Data *rde; 509 struct GNUNET_GNSRECORD_Data *rde;
499 unsigned int i;
500 510
511 (void) cls;
501 add_qe = NULL; 512 add_qe = NULL;
502 if (0 != strcmp (rec_name, name)) 513 if (0 != strcmp (rec_name, name))
503 { 514 {
@@ -510,7 +521,7 @@ get_existing_record (void *cls,
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Received %u records for name `%s'\n", 522 "Received %u records for name `%s'\n",
512 rd_count, rec_name); 523 rd_count, rec_name);
513 for (i=0;i<rd_count;i++) 524 for (unsigned int i=0;i<rd_count;i++)
514 { 525 {
515 switch (rd[i].record_type) 526 switch (rd[i].record_type)
516 { 527 {
@@ -579,7 +590,9 @@ get_existing_record (void *cls,
579 } 590 }
580 break; 591 break;
581 } 592 }
582 memset (rdn, 0, sizeof (struct GNUNET_GNSRECORD_Data)); 593 memset (rdn,
594 0,
595 sizeof (struct GNUNET_GNSRECORD_Data));
583 GNUNET_memcpy (&rdn[1], 596 GNUNET_memcpy (&rdn[1],
584 rd, 597 rd,
585 rd_count * sizeof (struct GNUNET_GNSRECORD_Data)); 598 rd_count * sizeof (struct GNUNET_GNSRECORD_Data));
@@ -617,6 +630,7 @@ get_existing_record (void *cls,
617static void 630static void
618reverse_error_cb (void *cls) 631reverse_error_cb (void *cls)
619{ 632{
633 (void) cls;
620 reverse_qe = NULL; 634 reverse_qe = NULL;
621 FPRINTF (stdout, 635 FPRINTF (stdout,
622 "%s.zkey\n", 636 "%s.zkey\n",
@@ -641,6 +655,7 @@ handle_reverse_lookup (void *cls,
641 unsigned int rd_count, 655 unsigned int rd_count,
642 const struct GNUNET_GNSRECORD_Data *rd) 656 const struct GNUNET_GNSRECORD_Data *rd)
643{ 657{
658 (void) cls;
644 reverse_qe = NULL; 659 reverse_qe = NULL;
645 if (NULL == label) 660 if (NULL == label)
646 FPRINTF (stdout, 661 FPRINTF (stdout,
@@ -660,6 +675,7 @@ handle_reverse_lookup (void *cls,
660static void 675static void
661del_lookup_error_cb (void *cls) 676del_lookup_error_cb (void *cls)
662{ 677{
678 (void) cls;
663 del_qe = NULL; 679 del_qe = NULL;
664 GNUNET_break (0); 680 GNUNET_break (0);
665 ret = 1; 681 ret = 1;
@@ -687,10 +703,10 @@ del_monitor (void *cls,
687{ 703{
688 struct GNUNET_GNSRECORD_Data rdx[rd_count]; 704 struct GNUNET_GNSRECORD_Data rdx[rd_count];
689 unsigned int rd_left; 705 unsigned int rd_left;
690 unsigned int i;
691 uint32_t type; 706 uint32_t type;
692 char *vs; 707 char *vs;
693 708
709 (void) cls;
694 del_qe = NULL; 710 del_qe = NULL;
695 if (0 == rd_count) 711 if (0 == rd_count)
696 { 712 {
@@ -719,7 +735,7 @@ del_monitor (void *cls,
719 type = GNUNET_GNSRECORD_typename_to_number (typestring); 735 type = GNUNET_GNSRECORD_typename_to_number (typestring);
720 else 736 else
721 type = GNUNET_GNSRECORD_TYPE_ANY; 737 type = GNUNET_GNSRECORD_TYPE_ANY;
722 for (i=0;i<rd_count;i++) 738 for (unsigned int i=0;i<rd_count;i++)
723 { 739 {
724 vs = NULL; 740 vs = NULL;
725 if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) || 741 if (! ( ( (GNUNET_GNSRECORD_TYPE_ANY == type) ||
@@ -1044,6 +1060,9 @@ default_ego_cb (void *cls,
1044 void **ctx, 1060 void **ctx,
1045 const char *name) 1061 const char *name)
1046{ 1062{
1063 (void) cls;
1064 (void) ctx;
1065 (void) name;
1047 get_default = NULL; 1066 get_default = NULL;
1048 if (NULL == ego) 1067 if (NULL == ego)
1049 { 1068 {
@@ -1092,7 +1111,11 @@ run (void *cls,
1092 const char *cfgfile, 1111 const char *cfgfile,
1093 const struct GNUNET_CONFIGURATION_Handle *cfg) 1112 const struct GNUNET_CONFIGURATION_Handle *cfg)
1094{ 1113{
1095 if ( (NULL != args[0]) && (NULL == uri) ) 1114 (void) cls;
1115 (void) args;
1116 (void) cfgfile;
1117 if ( (NULL != args[0]) &&
1118 (NULL == uri) )
1096 uri = GNUNET_strdup (args[0]); 1119 uri = GNUNET_strdup (args[0]);
1097 1120
1098 GNUNET_SCHEDULER_add_shutdown (&do_shutdown, 1121 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
diff --git a/src/namestore/gnunet-zoneimport.c b/src/namestore/gnunet-zoneimport.c
new file mode 100644
index 000000000..33c20953b
--- /dev/null
+++ b/src/namestore/gnunet-zoneimport.c
@@ -0,0 +1,896 @@
1/*
2 This file is part of GNUnet
3 Copyright (C) 2018 GNUnet e.V.
4
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
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
19*/
20/**
21 * @file src/namestore/gnunet-zoneimport.c
22 * @brief import a DNS zone for publication in GNS, incremental
23 * @author Christian Grothoff
24 */
25#include "platform.h"
26#include <gnunet_util_lib.h>
27#include <gnunet_dnsstub_lib.h>
28#include <gnunet_dnsparser_lib.h>
29#include <gnunet_gnsrecord_lib.h>
30#include <gnunet_namestore_plugin.h>
31
32
33/**
34 * Record for the request to be stored by GNS.
35 */
36struct Record
37{
38 /**
39 * Kept in a DLL.
40 */
41 struct Record *next;
42
43 /**
44 * Kept in a DLL.
45 */
46 struct Record *prev;
47
48 /**
49 * GNS record.
50 */
51 struct GNUNET_GNSRECORD_Data grd;
52
53};
54
55
56/**
57 * Request we should make.
58 */
59struct Request
60{
61 /**
62 * Requests are kept in a DLL.
63 */
64 struct Request *next;
65
66 /**
67 * Requests are kept in a DLL.
68 */
69 struct Request *prev;
70
71 /**
72 * Head of records that should be published in GNS for
73 * this hostname.
74 */
75 struct Record *rec_head;
76
77 /**
78 * Tail of records that should be published in GNS for
79 * this hostname.
80 */
81 struct Record *rec_tail;
82
83 /**
84 * Socket used to make the request, NULL if not active.
85 */
86 struct GNUNET_DNSSTUB_RequestSocket *rs;
87
88 /**
89 * Raw DNS query.
90 */
91 void *raw;
92
93 /**
94 * Number of bytes in @e raw.
95 */
96 size_t raw_len;
97
98 /**
99 * Hostname we are resolving.
100 */
101 char *hostname;
102
103 /**
104 * Answer we got back and are currently parsing, or NULL
105 * if not active.
106 */
107 struct GNUNET_DNSPARSER_Packet *p;
108
109 /**
110 * At what time does the (earliest) of the returned records
111 * for this name expire? At this point, we need to re-fetch
112 * the record.
113 */
114 struct GNUNET_TIME_Absolute expires__;
115
116 /**
117 * When did we last issue this request?
118 */
119 time_t time;
120
121 /**
122 * How often did we issue this query? (And failed, reset
123 * to zero once we were successful.)
124 */
125 int issue_num;
126
127 /**
128 * random 16-bit DNS query identifier.
129 */
130 uint16_t id;
131};
132
133/**
134 * Namestore plugin.
135 */
136static struct GNUNET_NAMESTORE_PluginFunctions *ns;
137
138/**
139 * Context for DNS resolution.
140 */
141static struct GNUNET_DNSSTUB_Context *ctx;
142
143/**
144 * The number of queries that are outstanding
145 */
146static unsigned int pending;
147
148/**
149 * Number of lookups we performed overall.
150 */
151static unsigned int lookups;
152
153/**
154 * Number of lookups that failed.
155 */
156static unsigned int failures;
157
158/**
159 * Number of records we found.
160 */
161static unsigned int records;
162
163/**
164 * Head of DLL of all requests to perform, sorted by
165 * the time we should next do the request (i.e. by expires).
166 */
167static struct Request *req_head;
168
169/**
170 * Tail of DLL of all requests to perform, sorted by
171 * the time we should next do the request (i.e. by expires).
172 */
173static struct Request *req_tail;
174
175/**
176 * Main task.
177 */
178static struct GNUNET_SCHEDULER_Task *t;
179
180/**
181 * Which DNS server do we use for queries?
182 */
183static char *dns_server;
184
185/**
186 * Name of the database plugin (for loading/unloading).
187 */
188static char *db_lib_name;
189
190
191/**
192 * Maximum number of queries pending at the same time.
193 */
194#define THRESH 20
195
196/**
197 * TIME_THRESH is in usecs. How quickly do we submit fresh queries.
198 * Used as an additional throttle.
199 */
200#define TIME_THRESH 10
201
202/**
203 * How often do we retry a query before giving up for good?
204 */
205#define MAX_RETRIES 5
206
207
208/**
209 * Callback for #for_all_records
210 *
211 * @param cls closure
212 * @param rec a DNS record
213 */
214typedef void
215(*RecordProcessor) (void *cls,
216 const struct GNUNET_DNSPARSER_Record *rec);
217
218
219/**
220 * Call @a rp for each record in @a p, regardless of
221 * what response section it is in.
222 *
223 * @param p packet from DNS
224 * @param rp function to call
225 * @param rp_cls closure for @a rp
226 */
227static void
228for_all_records (const struct GNUNET_DNSPARSER_Packet *p,
229 RecordProcessor rp,
230 void *rp_cls)
231{
232 for (unsigned int i=0;i<p->num_answers;i++)
233 {
234 struct GNUNET_DNSPARSER_Record *rs = &p->answers[i];
235
236 rp (rp_cls,
237 rs);
238 }
239 for (unsigned int i=0;i<p->num_authority_records;i++)
240 {
241 struct GNUNET_DNSPARSER_Record *rs = &p->authority_records[i];
242
243 rp (rp_cls,
244 rs);
245 }
246 for (unsigned int i=0;i<p->num_additional_records;i++)
247 {
248 struct GNUNET_DNSPARSER_Record *rs = &p->additional_records[i];
249
250 rp (rp_cls,
251 rs);
252 }
253}
254
255
256/**
257 * Add record to the GNS record set for @a req.
258 *
259 * @param req the request to expand GNS record set for
260 * @param type type to use
261 * @param expiration_time when should @a rec expire
262 * @param data raw data to store
263 * @param data_len number of bytes in @a data
264 */
265static void
266add_record (struct Request *req,
267 uint32_t type,
268 struct GNUNET_TIME_Absolute expiration_time,
269 const void *data,
270 size_t data_len)
271{
272 struct Record *rec;
273
274 rec = GNUNET_malloc (sizeof (struct Record) + data_len);
275 rec->grd.data = &rec[1];
276 rec->grd.expiration_time = expiration_time.abs_value_us;
277 rec->grd.data_size = data_len;
278 rec->grd.record_type = type;
279 rec->grd.flags = GNUNET_GNSRECORD_RF_NONE;
280 GNUNET_memcpy (&rec[1],
281 data,
282 data_len);
283 GNUNET_CONTAINER_DLL_insert (req->rec_head,
284 req->rec_tail,
285 rec);
286}
287
288
289/**
290 * Closure for #check_for_glue.
291 */
292struct GlueClosure
293{
294 /**
295 * Overall request we are processing.
296 */
297 struct Request *req;
298
299 /**
300 * NS name we are looking for glue for.
301 */
302 const char *ns;
303
304 /**
305 * Set to #GNUNET_YES if glue was found.
306 */
307 int found;
308};
309
310
311/**
312 * Try to find glue records for a given NS record.
313 *
314 * @param cls a `struct GlueClosure *`
315 * @param rec record that may contain glue information
316 */
317static void
318check_for_glue (void *cls,
319 const struct GNUNET_DNSPARSER_Record *rec)
320{
321 struct GlueClosure *gc = cls;
322 char dst[65536];
323 size_t dst_len;
324 size_t off;
325 char ip[INET6_ADDRSTRLEN+1];
326 socklen_t ip_size = (socklen_t) sizeof (ip);
327
328 if (0 != strcasecmp (rec->name,
329 gc->ns))
330 return;
331 dst_len = sizeof (dst);
332 off = 0;
333 switch (rec->type)
334 {
335 case GNUNET_DNSPARSER_TYPE_A:
336 if (sizeof (struct in_addr) != rec->data.raw.data_len)
337 {
338 GNUNET_break (0);
339 return;
340 }
341 if (NULL ==
342 inet_ntop (AF_INET,
343 &rec->data.raw.data,
344 ip,
345 ip_size))
346 {
347 GNUNET_break (0);
348 return;
349 }
350 if ( (GNUNET_OK ==
351 GNUNET_DNSPARSER_builder_add_name (dst,
352 dst_len,
353 &off,
354 gc->req->hostname)) &&
355 (GNUNET_OK ==
356 GNUNET_DNSPARSER_builder_add_name (dst,
357 dst_len,
358 &off,
359 ip)) )
360 {
361 add_record (gc->req,
362 rec->type,
363 rec->expiration_time,
364 dst,
365 off);
366 gc->found = GNUNET_YES;
367 }
368 break;
369 case GNUNET_DNSPARSER_TYPE_AAAA:
370 if (sizeof (struct in6_addr) != rec->data.raw.data_len)
371 {
372 GNUNET_break (0);
373 return;
374 }
375 if (NULL ==
376 inet_ntop (AF_INET6,
377 &rec->data.raw.data,
378 ip,
379 ip_size))
380 {
381 GNUNET_break (0);
382 return;
383 }
384 if ( (GNUNET_OK ==
385 GNUNET_DNSPARSER_builder_add_name (dst,
386 dst_len,
387 &off,
388 gc->req->hostname)) &&
389 (GNUNET_OK ==
390 GNUNET_DNSPARSER_builder_add_name (dst,
391 dst_len,
392 &off,
393 ip)) )
394 {
395 add_record (gc->req,
396 rec->type,
397 rec->expiration_time,
398 dst,
399 off);
400 gc->found = GNUNET_YES;
401 }
402 break;
403 case GNUNET_DNSPARSER_TYPE_CNAME:
404 GNUNET_break (0); /* FIXME: implement! */
405 break;
406 default:
407 /* useless, do nothing */
408 break;
409 }
410}
411
412
413/**
414 * We received @a rec for @a req. Remember the answer.
415 *
416 * @param cls a `struct Request`
417 * @param rec response
418 */
419static void
420process_record (void *cls,
421 const struct GNUNET_DNSPARSER_Record *rec)
422{
423 struct Request *req = cls;
424 char dst[65536];
425 size_t dst_len;
426 size_t off;
427
428 dst_len = sizeof (dst);
429 off = 0;
430 records++;
431 if (0 != strcasecmp (rec->name,
432 req->hostname))
433 return; /* does not match hostname, might be glue, but
434 not useful for this pass! */
435 if (0 ==
436 GNUNET_TIME_absolute_get_remaining (rec->expiration_time).rel_value_us)
437 return; /* record expired */
438 switch (rec->type)
439 {
440 case GNUNET_DNSPARSER_TYPE_NS:
441 {
442 struct GlueClosure gc;
443
444 /* check for glue */
445 gc.req = req;
446 gc.ns = rec->data.hostname;
447 gc.found = GNUNET_NO;
448 for_all_records (req->p,
449 &check_for_glue,
450 &gc);
451 if ( (GNUNET_NO == gc.found) &&
452 (GNUNET_OK ==
453 GNUNET_DNSPARSER_builder_add_name (dst,
454 dst_len,
455 &off,
456 req->hostname)) &&
457 (GNUNET_OK ==
458 GNUNET_DNSPARSER_builder_add_name (dst,
459 dst_len,
460 &off,
461 rec->data.hostname)) )
462 add_record (req,
463 rec->type,
464 rec->expiration_time,
465 dst,
466 off);
467 break;
468 }
469 case GNUNET_DNSPARSER_TYPE_CNAME:
470 /* Special logic required, FIXME: support later! */
471 fprintf (stdout,
472 "%s CNAME %s\n",
473 rec->name,
474 rec->data.hostname);
475 break;
476 case GNUNET_DNSPARSER_TYPE_DNAME:
477 /* Very special logic required (no support for DNAME
478 in GNS yet!), FIXME: support later! */
479 fprintf (stdout,
480 "FIMXE: %s DNAME %s\n",
481 rec->name,
482 rec->data.hostname);
483 break;
484 case GNUNET_DNSPARSER_TYPE_MX:
485 if (GNUNET_OK ==
486 GNUNET_DNSPARSER_builder_add_mx (dst,
487 dst_len,
488 &off,
489 rec->data.mx))
490 add_record (req,
491 rec->type,
492 rec->expiration_time,
493 dst,
494 off);
495 break;
496 case GNUNET_DNSPARSER_TYPE_SOA:
497 if (GNUNET_OK ==
498 GNUNET_DNSPARSER_builder_add_soa (dst,
499 dst_len,
500 &off,
501 rec->data.soa))
502 add_record (req,
503 rec->type,
504 rec->expiration_time,
505 dst,
506 off);
507 break;
508 case GNUNET_DNSPARSER_TYPE_SRV:
509 if (GNUNET_OK ==
510 GNUNET_DNSPARSER_builder_add_srv (dst,
511 dst_len,
512 &off,
513 rec->data.srv))
514 add_record (req,
515 rec->type,
516 rec->expiration_time,
517 dst,
518 off);
519 break;
520 case GNUNET_DNSPARSER_TYPE_PTR:
521 if (GNUNET_OK ==
522 GNUNET_DNSPARSER_builder_add_name (dst,
523 dst_len,
524 &off,
525 rec->data.hostname))
526 add_record (req,
527 rec->type,
528 rec->expiration_time,
529 dst,
530 off);
531 break;
532 case GNUNET_DNSPARSER_TYPE_CERT:
533 if (GNUNET_OK ==
534 GNUNET_DNSPARSER_builder_add_cert (dst,
535 dst_len,
536 &off,
537 rec->data.cert))
538 add_record (req,
539 rec->type,
540 rec->expiration_time,
541 dst,
542 off);
543 break;
544 /* Rest is 'raw' encoded and just needs to be copied IF
545 the hostname matches the requested name; otherwise we
546 simply cannot use it. */
547 case GNUNET_DNSPARSER_TYPE_A:
548 case GNUNET_DNSPARSER_TYPE_AAAA:
549 case GNUNET_DNSPARSER_TYPE_TXT:
550 default:
551 add_record (req,
552 rec->type,
553 rec->expiration_time,
554 rec->data.raw.data,
555 rec->data.raw.data_len);
556 break;
557 }
558}
559
560
561/**
562 * Function called with the result of a DNS resolution.
563 *
564 * @param cls closure with the `struct Request`
565 * @param rs socket that received the response
566 * @param dns dns response, never NULL
567 * @param dns_len number of bytes in @a dns
568 */
569static void
570process_result (void *cls,
571 struct GNUNET_DNSSTUB_RequestSocket *rs,
572 const struct GNUNET_TUN_DnsHeader *dns,
573 size_t dns_len)
574{
575 struct Request *req = cls;
576 struct GNUNET_DNSPARSER_Packet *p;
577
578 (void) rs;
579 if (NULL == dns)
580 {
581 /* stub gave up */
582 pending--;
583 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
584 "Stub gave up on DNS reply for `%s'\n",
585 req->hostname);
586 GNUNET_CONTAINER_DLL_remove (req_head,
587 req_tail,
588 req);
589 if (req->issue_num > MAX_RETRIES)
590 {
591 failures++;
592 GNUNET_free (req->hostname);
593 GNUNET_free (req->raw);
594 GNUNET_free (req);
595 return;
596 }
597 GNUNET_CONTAINER_DLL_insert_tail (req_head,
598 req_tail,
599 req);
600 req->rs = NULL;
601 return;
602 }
603 if (req->id != dns->id)
604 return;
605 pending--;
606 GNUNET_DNSSTUB_resolve_cancel (req->rs);
607 req->rs = NULL;
608 GNUNET_CONTAINER_DLL_remove (req_head,
609 req_tail,
610 req);
611 p = GNUNET_DNSPARSER_parse ((const char *) dns,
612 dns_len);
613 if (NULL == p)
614 {
615 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
616 "Failed to parse DNS reply for `%s'\n",
617 req->hostname);
618 if (req->issue_num > MAX_RETRIES)
619 {
620 failures++;
621 GNUNET_free (req->hostname);
622 GNUNET_free (req->raw);
623 GNUNET_free (req);
624 return;
625 }
626 GNUNET_CONTAINER_DLL_insert_tail (req_head,
627 req_tail,
628 req);
629 return;
630 }
631 req->p = p;
632 for_all_records (p,
633 &process_record,
634 req);
635 req->p = NULL;
636 // FIXME: update database!
637 GNUNET_DNSPARSER_free_packet (p);
638 GNUNET_free (req->hostname);
639 GNUNET_free (req->raw);
640 GNUNET_free (req);
641}
642
643
644/**
645 * Submit a request to DNS unless we need to slow down because
646 * we are at the rate limit.
647 *
648 * @param req request to submit
649 * @return #GNUNET_OK if request was submitted
650 * #GNUNET_NO if request was already submitted
651 * #GNUNET_SYSERR if we are at the rate limit
652 */
653static int
654submit_req (struct Request *req)
655{
656 static struct timeval last_request;
657 struct timeval now;
658
659 if (NULL != req->rs)
660 return GNUNET_NO; /* already submitted */
661 gettimeofday (&now,
662 NULL);
663 if ( ( ( (now.tv_sec - last_request.tv_sec) == 0) &&
664 ( (now.tv_usec - last_request.tv_usec) < TIME_THRESH) ) ||
665 (pending >= THRESH) )
666 return GNUNET_SYSERR;
667 GNUNET_assert (NULL == req->rs);
668 req->rs = GNUNET_DNSSTUB_resolve2 (ctx,
669 req->raw,
670 req->raw_len,
671 &process_result,
672 req);
673 GNUNET_assert (NULL != req->rs);
674 req->issue_num++;
675 last_request = now;
676 lookups++;
677 pending++;
678 req->time = time (NULL);
679 return GNUNET_OK;
680}
681
682
683/**
684 * Process as many requests as possible from the queue.
685 *
686 * @param cls NULL
687 */
688static void
689process_queue(void *cls)
690{
691 (void) cls;
692 t = NULL;
693 for (struct Request *req = req_head;
694 NULL != req;
695 req = req->next)
696 {
697 if (GNUNET_SYSERR == submit_req (req))
698 break;
699 }
700 if (NULL != req_head)
701 t = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_MILLISECONDS,
702 &process_queue,
703 NULL);
704 else
705 GNUNET_SCHEDULER_shutdown ();
706}
707
708
709/**
710 * Clean up and terminate the process.
711 *
712 * @param cls NULL
713 */
714static void
715do_shutdown (void *cls)
716{
717 (void) cls;
718 if (NULL != t)
719 {
720 GNUNET_SCHEDULER_cancel (t);
721 t = NULL;
722 }
723 if (NULL != ns)
724 {
725 GNUNET_break (NULL ==
726 GNUNET_PLUGIN_unload (db_lib_name,
727 ns));
728 GNUNET_free (db_lib_name);
729 db_lib_name = NULL;
730 }
731 GNUNET_DNSSTUB_stop (ctx);
732 ctx = NULL;
733}
734
735
736/**
737 * Add @a hostname to the list of requests to be made.
738 *
739 * @param hostname name to resolve
740 */
741static void
742queue (const char *hostname)
743{
744 struct GNUNET_DNSPARSER_Packet p;
745 struct GNUNET_DNSPARSER_Query q;
746 struct Request *req;
747 char *raw;
748 size_t raw_size;
749
750 if (GNUNET_OK !=
751 GNUNET_DNSPARSER_check_name (hostname))
752 {
753 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
754 "Refusing invalid hostname `%s'\n",
755 hostname);
756 return;
757 }
758 q.name = (char *) hostname;
759 q.type = GNUNET_DNSPARSER_TYPE_NS;
760 q.dns_traffic_class = GNUNET_TUN_DNS_CLASS_INTERNET;
761
762 memset (&p,
763 0,
764 sizeof (p));
765 p.num_queries = 1;
766 p.queries = &q;
767 p.id = (uint16_t) GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
768 UINT16_MAX);
769
770 if (GNUNET_OK !=
771 GNUNET_DNSPARSER_pack (&p,
772 UINT16_MAX,
773 &raw,
774 &raw_size))
775 {
776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
777 "Failed to pack query for hostname `%s'\n",
778 hostname);
779 return;
780 }
781
782 req = GNUNET_new (struct Request);
783 req->hostname = strdup (hostname);
784 req->raw = raw;
785 req->raw_len = raw_size;
786 req->id = p.id;
787 /* FIXME: import data from namestore! */
788
789 /* FIXME: insert sorted by record expiration time */
790 GNUNET_CONTAINER_DLL_insert_tail (req_head,
791 req_tail,
792 req);
793}
794
795
796/**
797 * Process requests from the queue, then if the queue is
798 * not empty, try again.
799 *
800 * @param cls NULL
801 * @param args remaining command-line arguments
802 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
803 * @param cfg configuration
804 */
805static void
806run (void *cls,
807 char *const *args,
808 const char *cfgfile,
809 const struct GNUNET_CONFIGURATION_Handle *cfg)
810{
811 char hn[256];
812 char *database;
813
814 (void) cls;
815 (void) args;
816 (void) cfgfile;
817 ctx = GNUNET_DNSSTUB_start (dns_server);
818 if (NULL == ctx)
819 {
820 fprintf (stderr,
821 "Failed to initialize GNUnet DNS STUB\n");
822 return;
823 }
824 if (GNUNET_OK !=
825 GNUNET_CONFIGURATION_get_value_string (cfg,
826 "namestore",
827 "database",
828 &database))
829 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
830 "No database backend configured\n");
831
832 GNUNET_asprintf (&db_lib_name,
833 "libgnunet_plugin_namestore_%s",
834 database);
835 ns = GNUNET_PLUGIN_load (db_lib_name,
836 (void *) cfg);
837 GNUNET_free (database);
838 while (NULL !=
839 fgets (hn,
840 sizeof (hn),
841 stdin))
842 {
843 if (strlen(hn) > 0)
844 hn[strlen(hn)-1] = '\0'; /* eat newline */
845 queue (hn);
846 }
847 GNUNET_SCHEDULER_add_shutdown (&do_shutdown,
848 NULL);
849 t = GNUNET_SCHEDULER_add_now (&process_queue,
850 NULL);
851}
852
853
854/**
855 * Call with IP address of resolver to query.
856 *
857 * @param argc should be 2
858 * @param argv[1] should contain IP address
859 * @return 0 on success
860 */
861int
862main (int argc,
863 char *const*argv)
864{
865 struct GNUNET_GETOPT_CommandLineOption options[] = {
866 GNUNET_GETOPT_option_mandatory
867 (GNUNET_GETOPT_option_string ('s',
868 "server",
869 "IP",
870 "which DNS server should be used",
871 &dns_server)),
872 GNUNET_GETOPT_OPTION_END
873 };
874
875 if (GNUNET_OK !=
876 GNUNET_STRINGS_get_utf8_args (argc, argv,
877 &argc, &argv))
878 return 2;
879 GNUNET_PROGRAM_run (argc,
880 argv,
881 "gnunet-zoneimport",
882 "import DNS zone into namestore",
883 options,
884 &run,
885 NULL);
886 GNUNET_free ((void*) argv);
887 fprintf (stderr,
888 "Did %u lookups, found %u records, %u lookups failed, %u pending on shutdown\n",
889 lookups,
890 records,
891 failures,
892 pending);
893 return 0;
894}
895
896/* end of gnunet-zoneimport.c */
diff --git a/src/namestore/namestore_api_monitor.c b/src/namestore/namestore_api_monitor.c
index cd7c7dadb..8e6d39ad7 100644
--- a/src/namestore/namestore_api_monitor.c
+++ b/src/namestore/namestore_api_monitor.c
@@ -114,6 +114,8 @@ handle_sync (void *cls,
114{ 114{
115 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls; 115 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
116 116
117 (void) cls;
118 (void) msg;
117 if (NULL != zm->sync_cb) 119 if (NULL != zm->sync_cb)
118 zm->sync_cb (zm->sync_cb_cls); 120 zm->sync_cb (zm->sync_cb_cls);
119} 121}
@@ -138,6 +140,7 @@ check_result (void *cls,
138 const char *name_tmp; 140 const char *name_tmp;
139 const char *rd_ser_tmp; 141 const char *rd_ser_tmp;
140 142
143 (void) cls;
141 lrm_len = ntohs (lrm->gns_header.header.size); 144 lrm_len = ntohs (lrm->gns_header.header.size);
142 rd_len = ntohs (lrm->rd_len); 145 rd_len = ntohs (lrm->rd_len);
143 rd_count = ntohs (lrm->rd_count); 146 rd_count = ntohs (lrm->rd_count);
@@ -231,6 +234,7 @@ mq_error_handler (void *cls,
231{ 234{
232 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls; 235 struct GNUNET_NAMESTORE_ZoneMonitor *zm = cls;
233 236
237 (void) error;
234 reconnect (zm); 238 reconnect (zm);
235} 239}
236 240
diff --git a/src/namestore/plugin_namestore_flat.c b/src/namestore/plugin_namestore_flat.c
index 923d2696f..024fc34f2 100644
--- a/src/namestore/plugin_namestore_flat.c
+++ b/src/namestore/plugin_namestore_flat.c
@@ -1,6 +1,6 @@
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, 2018 GNUnet e.V.
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
@@ -22,6 +22,7 @@
22 * @file namestore/plugin_namestore_flat.c 22 * @file namestore/plugin_namestore_flat.c
23 * @brief file-based namestore backend 23 * @brief file-based namestore backend
24 * @author Martin Schanzenbach 24 * @author Martin Schanzenbach
25 * @author Christian Grothoff
25 */ 26 */
26 27
27#include "platform.h" 28#include "platform.h"
@@ -76,7 +77,7 @@ struct Plugin
76 /** 77 /**
77 * PKEY to look for in zone to name 78 * PKEY to look for in zone to name
78 */ 79 */
79 struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey; 80 const struct GNUNET_CRYPTO_EcdsaPublicKey *iter_pkey;
80 81
81 /** 82 /**
82 * Iteration result found 83 * Iteration result found
@@ -147,15 +148,19 @@ database_setup (struct Plugin *plugin)
147 if (GNUNET_OK != 148 if (GNUNET_OK !=
148 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg, 149 GNUNET_CONFIGURATION_get_value_filename (plugin->cfg,
149 "namestore-flat", 150 "namestore-flat",
150 "FILENAME", &afsdir)) 151 "FILENAME",
152 &afsdir))
151 { 153 {
152 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 154 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR,
153 "namestore-flat", "FILENAME"); 155 "namestore-flat",
156 "FILENAME");
154 return GNUNET_SYSERR; 157 return GNUNET_SYSERR;
155 } 158 }
156 if (GNUNET_OK != GNUNET_DISK_file_test (afsdir)) 159 if (GNUNET_OK !=
160 GNUNET_DISK_file_test (afsdir))
157 { 161 {
158 if (GNUNET_OK != GNUNET_DISK_directory_create_for_file (afsdir)) 162 if (GNUNET_OK !=
163 GNUNET_DISK_directory_create_for_file (afsdir))
159 { 164 {
160 GNUNET_break (0); 165 GNUNET_break (0);
161 GNUNET_free (afsdir); 166 GNUNET_free (afsdir);
@@ -229,15 +234,19 @@ database_setup (struct Plugin *plugin)
229 if (NULL == label) 234 if (NULL == label)
230 break; 235 break;
231 line = strtok (NULL, "\n"); 236 line = strtok (NULL, "\n");
232 entry = GNUNET_malloc (sizeof (struct FlatFileEntry)); 237 entry = GNUNET_new (struct FlatFileEntry);
233 if (1 != sscanf (rvalue, "%lu", &entry->rvalue)) 238 if (1 != sscanf (rvalue,
239 "%lu",
240 &entry->rvalue))
234 { 241 {
235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
236 "Error parsing entry\n"); 243 "Error parsing entry\n");
237 GNUNET_free (entry); 244 GNUNET_free (entry);
238 break; 245 break;
239 } 246 }
240 if (1 != sscanf (record_count, "%u", &entry->record_count)) 247 if (1 != sscanf (record_count,
248 "%u",
249 &entry->record_count))
241 { 250 {
242 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 251 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
243 "Error parsing entry\n"); 252 "Error parsing entry\n");
@@ -245,18 +254,22 @@ database_setup (struct Plugin *plugin)
245 break; 254 break;
246 } 255 }
247 entry->label = GNUNET_strdup (label); 256 entry->label = GNUNET_strdup (label);
248 record_data_size = GNUNET_STRINGS_base64_decode (record_data_b64, 257 record_data_size
249 strlen (record_data_b64), 258 = GNUNET_STRINGS_base64_decode (record_data_b64,
250 &record_data); 259 strlen (record_data_b64),
260 &record_data);
251 entry->record_data = 261 entry->record_data =
252 GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * entry->record_count); 262 GNUNET_new_array (entry->record_count,
253 if (GNUNET_OK != GNUNET_GNSRECORD_records_deserialize (record_data_size, 263 struct GNUNET_GNSRECORD_Data);
254 record_data, 264 if (GNUNET_OK !=
255 entry->record_count, 265 GNUNET_GNSRECORD_records_deserialize (record_data_size,
256 entry->record_data)) 266 record_data,
267 entry->record_count,
268 entry->record_data))
257 { 269 {
258 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 270 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
259 "Unable to deserialize record %s\n", label); 271 "Unable to deserialize record %s\n",
272 label);
260 GNUNET_free (entry->label); 273 GNUNET_free (entry->label);
261 GNUNET_free (entry); 274 GNUNET_free (entry);
262 GNUNET_free (record_data); 275 GNUNET_free (record_data);
@@ -268,10 +281,12 @@ database_setup (struct Plugin *plugin)
268 (char**)&entry->private_key); 281 (char**)&entry->private_key);
269 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 282 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
270 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 283 key = GNUNET_malloc (strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
271 GNUNET_memcpy (key, label, strlen (label)); 284 GNUNET_memcpy (key,
285 label,
286 strlen (label));
272 GNUNET_memcpy (key+strlen(label), 287 GNUNET_memcpy (key+strlen(label),
273 entry->private_key, 288 entry->private_key,
274 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 289 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
275 GNUNET_CRYPTO_hash (key, 290 GNUNET_CRYPTO_hash (key,
276 key_len, 291 key_len,
277 &hkey); 292 &hkey);
@@ -296,6 +311,8 @@ database_setup (struct Plugin *plugin)
296 * Store values in hashmap in file and free data 311 * Store values in hashmap in file and free data
297 * 312 *
298 * @param plugin the plugin context 313 * @param plugin the plugin context
314 * @param key key in the map
315 * @param value a `struct FlatFileEntry`
299 */ 316 */
300static int 317static int
301store_and_free_entries (void *cls, 318store_and_free_entries (void *cls,
@@ -309,6 +326,7 @@ store_and_free_entries (void *cls,
309 char *record_data_b64; 326 char *record_data_b64;
310 size_t data_size; 327 size_t data_size;
311 328
329 (void) key;
312 GNUNET_STRINGS_base64_encode ((char*)entry->private_key, 330 GNUNET_STRINGS_base64_encode ((char*)entry->private_key,
313 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey), 331 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey),
314 &zone_private_key); 332 &zone_private_key);
@@ -316,12 +334,14 @@ store_and_free_entries (void *cls,
316 entry->record_data); 334 entry->record_data);
317 { 335 {
318 char data[data_size]; 336 char data[data_size];
319 337 ssize_t ret;
320 if (data_size != 338
321 GNUNET_GNSRECORD_records_serialize (entry->record_count, 339 ret = GNUNET_GNSRECORD_records_serialize (entry->record_count,
322 entry->record_data, 340 entry->record_data,
323 data_size, 341 data_size,
324 data)) 342 data);
343 if ( (ret < 0) ||
344 (data_size != (size_t) ret) )
325 { 345 {
326 GNUNET_break (0); 346 GNUNET_break (0);
327 GNUNET_free (zone_private_key); 347 GNUNET_free (zone_private_key);
@@ -363,6 +383,7 @@ static void
363database_shutdown (struct Plugin *plugin) 383database_shutdown (struct Plugin *plugin)
364{ 384{
365 struct GNUNET_DISK_FileHandle *fh; 385 struct GNUNET_DISK_FileHandle *fh;
386
366 fh = GNUNET_DISK_file_open (plugin->fn, 387 fh = GNUNET_DISK_file_open (plugin->fn,
367 GNUNET_DISK_OPEN_CREATE | 388 GNUNET_DISK_OPEN_CREATE |
368 GNUNET_DISK_OPEN_TRUNCATE | 389 GNUNET_DISK_OPEN_TRUNCATE |
@@ -409,9 +430,9 @@ namestore_store_records (void *cls,
409 char *key; 430 char *key;
410 struct GNUNET_HashCode hkey; 431 struct GNUNET_HashCode hkey;
411 struct FlatFileEntry *entry; 432 struct FlatFileEntry *entry;
412 int i;
413 433
414 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK, UINT64_MAX); 434 rvalue = GNUNET_CRYPTO_random_u64 (GNUNET_CRYPTO_QUALITY_WEAK,
435 UINT64_MAX);
415 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 436 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
416 key = GNUNET_malloc (key_len); 437 key = GNUNET_malloc (key_len);
417 GNUNET_memcpy (key, label, strlen (label)); 438 GNUNET_memcpy (key, label, strlen (label));
@@ -426,18 +447,19 @@ namestore_store_records (void *cls,
426 447
427 if (0 < rd_count) 448 if (0 < rd_count)
428 { 449 {
429 entry = GNUNET_malloc (sizeof (struct FlatFileEntry)); 450 entry = GNUNET_new (struct FlatFileEntry);
430 entry->private_key = GNUNET_malloc (sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 451 entry->private_key = GNUNET_new (struct GNUNET_CRYPTO_EcdsaPrivateKey);
431 GNUNET_asprintf (&entry->label, 452 GNUNET_asprintf (&entry->label,
432 label, 453 label,
433 strlen (label)); 454 strlen (label));
434 GNUNET_memcpy (entry->private_key, 455 GNUNET_memcpy (entry->private_key,
435 zone_key, 456 zone_key,
436 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 457 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
437 entry->rvalue = rvalue; 458 entry->rvalue = rvalue;
438 entry->record_count = rd_count; 459 entry->record_count = rd_count;
439 entry->record_data = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * rd_count); 460 entry->record_data = GNUNET_new_array (rd_count,
440 for (i = 0; i < rd_count; i++) 461 struct GNUNET_GNSRECORD_Data);
462 for (unsigned int i = 0; i < rd_count; i++)
441 { 463 {
442 entry->record_data[i].expiration_time = rd[i].expiration_time; 464 entry->record_data[i].expiration_time = rd[i].expiration_time;
443 entry->record_data[i].record_type = rd[i].record_type; 465 entry->record_data[i].record_type = rd[i].record_type;
@@ -484,21 +506,28 @@ namestore_lookup_records (void *cls,
484 } 506 }
485 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey); 507 key_len = strlen (label) + sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey);
486 key = GNUNET_malloc (key_len); 508 key = GNUNET_malloc (key_len);
487 GNUNET_memcpy (key, label, strlen (label)); 509 GNUNET_memcpy (key,
510 label,
511 strlen (label));
488 GNUNET_memcpy (key+strlen(label), 512 GNUNET_memcpy (key+strlen(label),
489 zone, 513 zone,
490 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)); 514 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey));
491 GNUNET_CRYPTO_hash (key, 515 GNUNET_CRYPTO_hash (key,
492 key_len, 516 key_len,
493 &hkey); 517 &hkey);
494 GNUNET_free (key); 518 GNUNET_free (key);
495 519
496 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm, &hkey); 520 entry = GNUNET_CONTAINER_multihashmap_get (plugin->hm,
521 &hkey);
497 522
498 if (NULL == entry) 523 if (NULL == entry)
499 return GNUNET_NO; 524 return GNUNET_NO;
500 if (NULL != iter) 525 if (NULL != iter)
501 iter (iter_cls, entry->private_key, entry->label, entry->record_count, entry->record_data); 526 iter (iter_cls,
527 entry->private_key,
528 entry->label,
529 entry->record_count,
530 entry->record_data);
502 return GNUNET_YES; 531 return GNUNET_YES;
503} 532}
504 533
@@ -511,6 +540,7 @@ iterate_zones (void *cls,
511 struct Plugin *plugin = cls; 540 struct Plugin *plugin = cls;
512 struct FlatFileEntry *entry = value; 541 struct FlatFileEntry *entry = value;
513 542
543 (void) key;
514 if ((plugin->target_offset > plugin->offset) || 544 if ((plugin->target_offset > plugin->offset) ||
515 ( (NULL != plugin->iter_zone) && 545 ( (NULL != plugin->iter_zone) &&
516 (0 != memcmp (entry->private_key, 546 (0 != memcmp (entry->private_key,
@@ -528,6 +558,7 @@ iterate_zones (void *cls,
528 return GNUNET_NO; 558 return GNUNET_NO;
529} 559}
530 560
561
531/** 562/**
532 * Iterate over the results for a particular key and zone in the 563 * Iterate over the results for a particular key and zone in the
533 * datastore. Will return at most one result to the iterator. 564 * datastore. Will return at most one result to the iterator.
@@ -543,9 +574,13 @@ static int
543namestore_iterate_records (void *cls, 574namestore_iterate_records (void *cls,
544 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 575 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
545 uint64_t offset, 576 uint64_t offset,
546 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls) 577 GNUNET_NAMESTORE_RecordIterator iter,
578 void *iter_cls)
547{ 579{
548 struct Plugin *plugin = cls; 580 struct Plugin *plugin = cls;
581
582 /* FIXME: maybe use separate closure to better handle
583 recursive calls? */
549 plugin->target_offset = offset; 584 plugin->target_offset = offset;
550 plugin->offset = 0; 585 plugin->offset = 0;
551 plugin->iter = iter; 586 plugin->iter = iter;
@@ -558,6 +593,7 @@ namestore_iterate_records (void *cls,
558 return plugin->iter_result_found; 593 return plugin->iter_result_found;
559} 594}
560 595
596
561static int 597static int
562zone_to_name (void *cls, 598zone_to_name (void *cls,
563 const struct GNUNET_HashCode *key, 599 const struct GNUNET_HashCode *key,
@@ -565,14 +601,15 @@ zone_to_name (void *cls,
565{ 601{
566 struct Plugin *plugin = cls; 602 struct Plugin *plugin = cls;
567 struct FlatFileEntry *entry = value; 603 struct FlatFileEntry *entry = value;
568 int i;
569 604
605 (void) key;
570 if (0 != memcmp (entry->private_key, 606 if (0 != memcmp (entry->private_key,
571 plugin->iter_zone, 607 plugin->iter_zone,
572 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey))) 608 sizeof (struct GNUNET_CRYPTO_EcdsaPrivateKey)))
573 return GNUNET_YES; 609 return GNUNET_YES;
574 610
575 for (i = 0; i < entry->record_count; i++) { 611 for (unsigned int i = 0; i < entry->record_count; i++)
612 {
576 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type) 613 if (GNUNET_GNSRECORD_TYPE_PKEY != entry->record_data[i].record_type)
577 continue; 614 continue;
578 if (0 == memcmp (plugin->iter_pkey, 615 if (0 == memcmp (plugin->iter_pkey,
@@ -588,10 +625,10 @@ zone_to_name (void *cls,
588 625
589 } 626 }
590 } 627 }
591
592 return GNUNET_YES; 628 return GNUNET_YES;
593} 629}
594 630
631
595/** 632/**
596 * Look for an existing PKEY delegation record for a given public key. 633 * Look for an existing PKEY delegation record for a given public key.
597 * Returns at most one result to the iterator. 634 * Returns at most one result to the iterator.
@@ -607,19 +644,24 @@ static int
607namestore_zone_to_name (void *cls, 644namestore_zone_to_name (void *cls,
608 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone, 645 const struct GNUNET_CRYPTO_EcdsaPrivateKey *zone,
609 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone, 646 const struct GNUNET_CRYPTO_EcdsaPublicKey *value_zone,
610 GNUNET_NAMESTORE_RecordIterator iter, void *iter_cls) 647 GNUNET_NAMESTORE_RecordIterator iter,
648 void *iter_cls)
611{ 649{
612 struct Plugin *plugin = cls; 650 struct Plugin *plugin = cls;
613 651
652 /* FIXME: maybe use separate closure to better handle
653 recursive calls? */
654 plugin->iter = iter;
655 plugin->iter_cls = iter_cls;
656 plugin->iter_zone = zone;
657 plugin->iter_pkey = value_zone;
658 plugin->iter_result_found = GNUNET_NO;
614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
615 "Performing reverse lookup for `%s'\n", 660 "Performing reverse lookup for `%s'\n",
616 GNUNET_GNSRECORD_z2s (value_zone)); 661 GNUNET_GNSRECORD_z2s (value_zone));
617
618 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm, 662 GNUNET_CONTAINER_multihashmap_iterate (plugin->hm,
619 &zone_to_name, 663 &zone_to_name,
620 plugin); 664 plugin);
621
622
623 return plugin->iter_result_found; 665 return plugin->iter_result_found;
624} 666}
625 667
@@ -639,7 +681,9 @@ libgnunet_plugin_namestore_flat_init (void *cls)
639 681
640 if (NULL != plugin.cfg) 682 if (NULL != plugin.cfg)
641 return NULL; /* can only initialize once! */ 683 return NULL; /* can only initialize once! */
642 memset (&plugin, 0, sizeof (struct Plugin)); 684 memset (&plugin,
685 0,
686 sizeof (struct Plugin));
643 plugin.cfg = cfg; 687 plugin.cfg = cfg;
644 if (GNUNET_OK != database_setup (&plugin)) 688 if (GNUNET_OK != database_setup (&plugin))
645 { 689 {
diff --git a/src/namestore/plugin_namestore_postgres.c b/src/namestore/plugin_namestore_postgres.c
index 491cec1cb..de8d76853 100644
--- a/src/namestore/plugin_namestore_postgres.c
+++ b/src/namestore/plugin_namestore_postgres.c
@@ -217,10 +217,14 @@ namestore_postgres_store_records (void *cls,
217 GNUNET_PQ_query_param_end 217 GNUNET_PQ_query_param_end
218 }; 218 };
219 enum GNUNET_DB_QueryStatus res; 219 enum GNUNET_DB_QueryStatus res;
220 220 ssize_t ret;
221 if (data_size != 221
222 GNUNET_GNSRECORD_records_serialize (rd_count, rd, 222 ret = GNUNET_GNSRECORD_records_serialize (rd_count,
223 data_size, data)) 223 rd,
224 data_size,
225 data);
226 if ( (ret < 0) ||
227 (data_size != (size_t) ret) )
224 { 228 {
225 GNUNET_break (0); 229 GNUNET_break (0);
226 return GNUNET_SYSERR; 230 return GNUNET_SYSERR;
@@ -469,7 +473,6 @@ namestore_postgres_zone_to_name (void *cls,
469 pc.iter = iter; 473 pc.iter = iter;
470 pc.iter_cls = iter_cls; 474 pc.iter_cls = iter_cls;
471 pc.zone_key = zone; 475 pc.zone_key = zone;
472
473 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh, 476 res = GNUNET_PQ_eval_prepared_multi_select (plugin->dbh,
474 "zone_to_name", 477 "zone_to_name",
475 params, 478 params,
diff --git a/src/namestore/plugin_namestore_sqlite.c b/src/namestore/plugin_namestore_sqlite.c
index 168c52c11..5ad84688c 100644
--- a/src/namestore/plugin_namestore_sqlite.c
+++ b/src/namestore/plugin_namestore_sqlite.c
@@ -435,12 +435,14 @@ namestore_sqlite_store_records (void *cls,
435 GNUNET_SQ_query_param_string (label), 435 GNUNET_SQ_query_param_string (label),
436 GNUNET_SQ_query_param_end 436 GNUNET_SQ_query_param_end
437 }; 437 };
438 438 ssize_t ret;
439 if (data_size != 439
440 GNUNET_GNSRECORD_records_serialize (rd_count, 440 ret = GNUNET_GNSRECORD_records_serialize (rd_count,
441 rd, 441 rd,
442 data_size, 442 data_size,
443 data)) 443 data);
444 if ( (ret < 0) ||
445 (data_size != (size_t) ret) )
444 { 446 {
445 GNUNET_break (0); 447 GNUNET_break (0);
446 return GNUNET_SYSERR; 448 return GNUNET_SYSERR;
diff --git a/src/namestore/plugin_rest_namestore.c b/src/namestore/plugin_rest_namestore.c
index 954af98d2..4602106da 100644
--- a/src/namestore/plugin_rest_namestore.c
+++ b/src/namestore/plugin_rest_namestore.c
@@ -248,7 +248,6 @@ cleanup_handle (struct RequestHandle *handle)
248{ 248{
249 struct RecordEntry *record_entry; 249 struct RecordEntry *record_entry;
250 struct RecordEntry *record_tmp; 250 struct RecordEntry *record_tmp;
251 int i;
252 251
253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, 252 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
254 "Cleaning up\n"); 253 "Cleaning up\n");
@@ -276,7 +275,7 @@ cleanup_handle (struct RequestHandle *handle)
276 GNUNET_free (handle->value); 275 GNUNET_free (handle->value);
277 if (NULL != handle->rd) 276 if (NULL != handle->rd)
278 { 277 {
279 for (i = 0; i < handle->rd_count; i++) 278 for (unsigned int i = 0; i < handle->rd_count; i++)
280 { 279 {
281 if (NULL != handle->rd[i].data) 280 if (NULL != handle->rd[i].data)
282 GNUNET_free ((void*)handle->rd[i].data); 281 GNUNET_free ((void*)handle->rd[i].data);
@@ -441,22 +440,24 @@ namestore_list_response (void *cls,
441 struct GNUNET_JSONAPI_Resource *json_resource; 440 struct GNUNET_JSONAPI_Resource *json_resource;
442 json_t *result_array; 441 json_t *result_array;
443 json_t *record_obj; 442 json_t *record_obj;
444 int i;
445 443
446 if (NULL == handle->resp_object) 444 if (NULL == handle->resp_object)
447 handle->resp_object = GNUNET_JSONAPI_document_new (); 445 handle->resp_object = GNUNET_JSONAPI_document_new ();
448 446
449 if ( (NULL != handle->name) && 447 if ( (NULL != handle->name) &&
450 (0 != strcmp (handle->name, rname)) ) 448 (0 != strcmp (handle->name,
449 rname)) )
451 { 450 {
452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 451 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
453 "%s does not match %s\n", rname, handle->name); 452 "%s does not match %s\n",
453 rname,
454 handle->name);
454 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it); 455 GNUNET_NAMESTORE_zone_iterator_next (handle->list_it);
455 return; 456 return;
456 } 457 }
457 458
458 result_array = json_array (); 459 result_array = json_array ();
459 for (i=0; i<rd_len; i++) 460 for (unsigned int i=0; i<rd_len; i++)
460 { 461 {
461 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) && 462 if ( (GNUNET_GNSRECORD_TYPE_NICK == rd[i].record_type) &&
462 (0 != strcmp (rname, GNUNET_GNS_MASTERZONE_STR)) ) 463 (0 != strcmp (rname, GNUNET_GNS_MASTERZONE_STR)) )
@@ -465,8 +466,9 @@ namestore_list_response (void *cls,
465 if ( (rd[i].record_type != handle->type) && 466 if ( (rd[i].record_type != handle->type) &&
466 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) ) 467 (GNUNET_GNSRECORD_TYPE_ANY != handle->type) )
467 continue; 468 continue;
468 record_obj = gnsrecord_to_json (&(rd[i])); 469 record_obj = gnsrecord_to_json (&rd[i]);
469 json_array_append (result_array, record_obj); 470 json_array_append (result_array,
471 record_obj);
470 json_decref (record_obj); 472 json_decref (record_obj);
471 } 473 }
472 474
@@ -650,21 +652,24 @@ json_to_gnsrecord (const json_t *records_json,
650 size_t rdata_size; 652 size_t rdata_size;
651 const char *typestring; 653 const char *typestring;
652 const char *expirationstring; 654 const char *expirationstring;
653 int i;
654 json_t *type_json; 655 json_t *type_json;
655 json_t *value_json; 656 json_t *value_json;
656 json_t *record_json; 657 json_t *record_json;
657 json_t *exp_json; 658 json_t *exp_json;
658 659
659 *rd_count = json_array_size (records_json); 660 *rd_count = json_array_size (records_json);
660 *rd = GNUNET_malloc (sizeof (struct GNUNET_GNSRECORD_Data) * *rd_count); 661 *rd = GNUNET_new_array (*rd_count,
661 for (i = 0; i < *rd_count; i++) 662 struct GNUNET_GNSRECORD_Data);
663 for (unsigned int i = 0; i < *rd_count; i++)
662 { 664 {
663 memset (&((*rd)[i]), 0, sizeof (struct GNUNET_GNSRECORD_Data)); 665 memset (&(*rd)[i],
664 record_json = json_array_get (records_json, i); 666 0,
667 sizeof (struct GNUNET_GNSRECORD_Data));
668 record_json = json_array_get (records_json,
669 i);
665 type_json = json_object_get (record_json, 670 type_json = json_object_get (record_json,
666 GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE); 671 GNUNET_REST_JSONAPI_NAMESTORE_RECORD_TYPE);
667 if (!json_is_string (type_json)) 672 if (! json_is_string (type_json))
668 { 673 {
669 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 674 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
670 "Type property is no string\n"); 675 "Type property is no string\n");
@@ -680,20 +685,23 @@ json_to_gnsrecord (const json_t *records_json,
680 } 685 }
681 value_json = json_object_get (record_json, 686 value_json = json_object_get (record_json,
682 GNUNET_REST_JSONAPI_NAMESTORE_VALUE); 687 GNUNET_REST_JSONAPI_NAMESTORE_VALUE);
683 if (!json_is_string (value_json)) 688 if (! json_is_string (value_json))
684 { 689 {
685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 690 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
686 "Value property is no string\n"); 691 "Value property is no string\n");
687 return GNUNET_SYSERR; 692 return GNUNET_SYSERR;
688 } 693 }
689 value = GNUNET_strdup (json_string_value (value_json)); 694 value = GNUNET_strdup (json_string_value (value_json));
690 if (GNUNET_OK != GNUNET_GNSRECORD_string_to_value ((*rd)[i].record_type, 695 if (GNUNET_OK !=
691 value, 696 GNUNET_GNSRECORD_string_to_value ((*rd)[i].record_type,
692 &rdata, 697 value,
693 &rdata_size)) 698 &rdata,
699 &rdata_size))
694 { 700 {
695 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Value `%s' invalid for record type `%s'\n"), 701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
696 value, typestring); 702 _("Value `%s' invalid for record type `%s'\n"),
703 value,
704 typestring);
697 return GNUNET_SYSERR; 705 return GNUNET_SYSERR;
698 } 706 }
699 (*rd)[i].data = rdata; 707 (*rd)[i].data = rdata;
@@ -706,7 +714,7 @@ json_to_gnsrecord (const json_t *records_json,
706 */ 714 */
707 exp_json = json_object_get (record_json, 715 exp_json = json_object_get (record_json,
708 GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION); 716 GNUNET_REST_JSONAPI_NAMESTORE_EXPIRATION);
709 if (!json_is_string (exp_json)) 717 if (! json_is_string (exp_json))
710 { 718 {
711 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
712 "Expiration property is no string\n"); 720 "Expiration property is no string\n");
@@ -732,8 +740,10 @@ json_to_gnsrecord (const json_t *records_json,
732 } 740 }
733 else 741 else
734 { 742 {
735 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, _("Value `%s' invalid for record type `%s'\n"), 743 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
736 value, typestring); 744 _("Value `%s' invalid for record type `%s'\n"),
745 value,
746 typestring);
737 return GNUNET_SYSERR; 747 return GNUNET_SYSERR;
738 } 748 }
739 } 749 }
diff --git a/src/util/program.c b/src/util/program.c
index 9e3037b8b..a02bff77c 100644
--- a/src/util/program.c
+++ b/src/util/program.c
@@ -138,10 +138,13 @@ cmd_sorter (const void *a1, const void *a2)
138 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success 138 * @return #GNUNET_SYSERR on error, #GNUNET_OK on success
139 */ 139 */
140int 140int
141GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName, 141GNUNET_PROGRAM_run2 (int argc,
142 char *const *argv,
143 const char *binaryName,
142 const char *binaryHelp, 144 const char *binaryHelp,
143 const struct GNUNET_GETOPT_CommandLineOption *options, 145 const struct GNUNET_GETOPT_CommandLineOption *options,
144 GNUNET_PROGRAM_Main task, void *task_cls, 146 GNUNET_PROGRAM_Main task,
147 void *task_cls,
145 int run_without_scheduler) 148 int run_without_scheduler)
146{ 149{
147 struct CommandContext cc; 150 struct CommandContext cc;
@@ -158,7 +161,6 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
158 unsigned long long skew_variance; 161 unsigned long long skew_variance;
159 long long clock_offset; 162 long long clock_offset;
160 struct GNUNET_CONFIGURATION_Handle *cfg; 163 struct GNUNET_CONFIGURATION_Handle *cfg;
161
162 struct GNUNET_GETOPT_CommandLineOption defoptions[] = { 164 struct GNUNET_GETOPT_CommandLineOption defoptions[] = {
163 GNUNET_GETOPT_option_cfgfile (&cc.cfgfile), 165 GNUNET_GETOPT_option_cfgfile (&cc.cfgfile),
164 GNUNET_GETOPT_option_help (binaryHelp), 166 GNUNET_GETOPT_option_help (binaryHelp),
@@ -177,23 +179,31 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
177 { 179 {
178 char **gargv; 180 char **gargv;
179 unsigned int gargc; 181 unsigned int gargc;
180 int i;
181 char *tok;
182 char *cargs; 182 char *cargs;
183 183
184 gargv = NULL; 184 gargv = NULL;
185 gargc = 0; 185 gargc = 0;
186 for (i = 0; i < argc; i++) 186 for (int i = 0; i < argc; i++)
187 GNUNET_array_append (gargv, gargc, GNUNET_strdup (argv[i])); 187 GNUNET_array_append (gargv,
188 gargc,
189 GNUNET_strdup (argv[i]));
188 cargs = GNUNET_strdup (gargs); 190 cargs = GNUNET_strdup (gargs);
189 for (tok = strtok (cargs, " "); NULL != tok; tok = strtok (NULL, " ")) 191 for (char *tok = strtok (cargs, " ");
190 GNUNET_array_append (gargv, gargc, GNUNET_strdup (tok)); 192 NULL != tok;
193 tok = strtok (NULL, " "))
194 GNUNET_array_append (gargv,
195 gargc,
196 GNUNET_strdup (tok));
191 GNUNET_free (cargs); 197 GNUNET_free (cargs);
192 GNUNET_array_append (gargv, gargc, NULL); 198 GNUNET_array_append (gargv,
199 gargc,
200 NULL);
193 argv = (char *const *) gargv; 201 argv = (char *const *) gargv;
194 argc = gargc - 1; 202 argc = gargc - 1;
195 } 203 }
196 memset (&cc, 0, sizeof (cc)); 204 memset (&cc,
205 0,
206 sizeof (cc));
197 loglev = NULL; 207 loglev = NULL;
198 cc.task = task; 208 cc.task = task;
199 cc.task_cls = task_cls; 209 cc.task_cls = task_cls;
@@ -204,7 +214,8 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
204 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR); 214 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LOCALEDIR);
205 if (NULL != path) 215 if (NULL != path)
206 { 216 {
207 BINDTEXTDOMAIN ("GNUnet", path); 217 BINDTEXTDOMAIN ("GNUnet",
218 path);
208 GNUNET_free (path); 219 GNUNET_free (path);
209 } 220 }
210 textdomain ("GNUnet"); 221 textdomain ("GNUnet");
@@ -216,13 +227,17 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
216 GNUNET_malloc ((cnt + 227 GNUNET_malloc ((cnt +
217 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) + 228 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption) +
218 sizeof (defoptions)); 229 sizeof (defoptions));
219 GNUNET_memcpy (allopts, defoptions, sizeof (defoptions)); 230 GNUNET_memcpy (allopts,
231 defoptions,
232 sizeof (defoptions));
220 GNUNET_memcpy (&allopts 233 GNUNET_memcpy (&allopts
221 [sizeof (defoptions) / 234 [sizeof (defoptions) /
222 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options, 235 sizeof (struct GNUNET_GETOPT_CommandLineOption)], options,
223 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption)); 236 (cnt + 1) * sizeof (struct GNUNET_GETOPT_CommandLineOption));
224 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption); 237 cnt += sizeof (defoptions) / sizeof (struct GNUNET_GETOPT_CommandLineOption);
225 qsort (allopts, cnt, sizeof (struct GNUNET_GETOPT_CommandLineOption), 238 qsort (allopts,
239 cnt,
240 sizeof (struct GNUNET_GETOPT_CommandLineOption),
226 &cmd_sorter); 241 &cmd_sorter);
227 loglev = NULL; 242 loglev = NULL;
228 xdg = getenv ("XDG_CONFIG_HOME"); 243 xdg = getenv ("XDG_CONFIG_HOME");
@@ -237,9 +252,15 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
237 lpfx = GNUNET_strdup (binaryName); 252 lpfx = GNUNET_strdup (binaryName);
238 if (NULL != (spc = strstr (lpfx, " "))) 253 if (NULL != (spc = strstr (lpfx, " ")))
239 *spc = '\0'; 254 *spc = '\0';
240 ret = GNUNET_GETOPT_run (binaryName, allopts, (unsigned int) argc, argv); 255 ret = GNUNET_GETOPT_run (binaryName,
256 allopts,
257 (unsigned int) argc,
258 argv);
241 if ((GNUNET_OK > ret) || 259 if ((GNUNET_OK > ret) ||
242 (GNUNET_OK != GNUNET_log_setup (lpfx, loglev, logfile))) 260 (GNUNET_OK !=
261 GNUNET_log_setup (lpfx,
262 loglev,
263 logfile)))
243 { 264 {
244 GNUNET_free (allopts); 265 GNUNET_free (allopts);
245 GNUNET_free (lpfx); 266 GNUNET_free (lpfx);
@@ -250,7 +271,9 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
250 if (GNUNET_YES == 271 if (GNUNET_YES ==
251 GNUNET_DISK_file_test (cc.cfgfile)) 272 GNUNET_DISK_file_test (cc.cfgfile))
252 { 273 {
253 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, cc.cfgfile)) 274 if (GNUNET_SYSERR ==
275 GNUNET_CONFIGURATION_load (cfg,
276 cc.cfgfile))
254 { 277 {
255 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
256 _("Malformed configuration file `%s', exit ...\n"), 279 _("Malformed configuration file `%s', exit ...\n"),
@@ -263,11 +286,14 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
263 } 286 }
264 else 287 else
265 { 288 {
266 if (0 != strcmp (cc.cfgfile, cfg_fn)) 289 if (0 != strcmp (cc.cfgfile,
290 cfg_fn))
267 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
268 _("Could not access configuration file `%s'\n"), 292 _("Could not access configuration file `%s'\n"),
269 cc.cfgfile); 293 cc.cfgfile);
270 if (GNUNET_SYSERR == GNUNET_CONFIGURATION_load (cfg, NULL)) 294 if (GNUNET_SYSERR ==
295 GNUNET_CONFIGURATION_load (cfg,
296 NULL))
271 { 297 {
272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, 298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
273 _("Malformed configuration, exit ...\n")); 299 _("Malformed configuration, exit ...\n"));
@@ -280,11 +306,15 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
280 GNUNET_free (allopts); 306 GNUNET_free (allopts);
281 GNUNET_free (lpfx); 307 GNUNET_free (lpfx);
282 if (GNUNET_OK == 308 if (GNUNET_OK ==
283 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", "skew_offset", 309 GNUNET_CONFIGURATION_get_value_number (cc.cfg,
310 "testing",
311 "skew_offset",
284 &skew_offset) && 312 &skew_offset) &&
285 (GNUNET_OK == 313 (GNUNET_OK ==
286 GNUNET_CONFIGURATION_get_value_number (cc.cfg, "testing", 314 GNUNET_CONFIGURATION_get_value_number (cc.cfg,
287 "skew_variance", &skew_variance))) 315 "testing",
316 "skew_variance",
317 &skew_variance)))
288 { 318 {
289 clock_offset = skew_offset - skew_variance; 319 clock_offset = skew_offset - skew_variance;
290 GNUNET_TIME_set_offset (clock_offset); 320 GNUNET_TIME_set_offset (clock_offset);
@@ -301,7 +331,8 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
301 "CONFIG")) 331 "CONFIG"))
302 { 332 {
303 GNUNET_CONFIGURATION_set_value_string (cfg, 333 GNUNET_CONFIGURATION_set_value_string (cfg,
304 "arm", "CONFIG", 334 "arm",
335 "CONFIG",
305 cc.cfgfile); 336 cc.cfgfile);
306 } 337 }
307 338
@@ -314,7 +345,10 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
314 else 345 else
315 { 346 {
316 GNUNET_RESOLVER_connect (cc.cfg); 347 GNUNET_RESOLVER_connect (cc.cfg);
317 cc.task (cc.task_cls, cc.args, cc.cfgfile, cc.cfg); 348 cc.task (cc.task_cls,
349 cc.args,
350 cc.cfgfile,
351 cc.cfg);
318 } 352 }
319 ret = GNUNET_OK; 353 ret = GNUNET_OK;
320 cleanup: 354 cleanup:
@@ -326,6 +360,7 @@ GNUNET_PROGRAM_run2 (int argc, char *const *argv, const char *binaryName,
326 return ret; 360 return ret;
327} 361}
328 362
363
329/** 364/**
330 * Run a standard GNUnet command startup sequence (initialize loggers 365 * Run a standard GNUnet command startup sequence (initialize loggers
331 * and configuration, parse options). 366 * and configuration, parse options).