GNUnet 0.27.0
 
Loading...
Searching...
No Matches
gnunet-daemon-regexprofiler.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2012, 2013, 2026 Christian Grothoff
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
30#include "platform.h"
31#include "gnunet_util_lib.h"
32#include "regex_internal_lib.h"
33#include "regex_test_lib.h"
34#include "gnunet_dht_service.h"
35#include "gnunet_pils_service.h"
37
41static int global_ret;
42
46static const struct GNUNET_CONFIGURATION_Handle *cfg;
47
52
57
62
67
72
77
81static unsigned long long max_path_compression;
82
87static char *policy_filename;
88
92static char *regex_prefix;
93
97static char *rx_with_pfx;
98
102static unsigned int rounds = 3;
103
104
110static void
111shutdown_task (void *cls)
112{
113 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "shutting down\n");
114
115 if (NULL != announce_handle)
116 {
118 announce_handle = NULL;
119 }
120 if (NULL != reannounce_task)
121 {
123 reannounce_task = NULL;
124 }
125 if (NULL != dht_handle)
126 {
128 dht_handle = NULL;
129 }
130 if (NULL != pils_handle)
131 {
133 pils_handle = NULL;
134 }
135
137 "Daemon for %s shutting down\n",
139}
140
141
147static void
149{
150 char *regex = cls;
151 struct GNUNET_TIME_Relative random_delay;
152
153 reannounce_task = NULL;
154 if (0 == rounds--)
155 {
156 global_ret = 0;
158 GNUNET_free (regex);
159 return;
160 }
161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regex: %s\n", regex);
162 GNUNET_STATISTICS_update (stats_handle, "# regexes announced", 1, GNUNET_NO);
163 if ((NULL == announce_handle) && (NULL != regex))
164 {
166 "First time, creating regex: %s\n",
167 regex);
170 regex,
171 (unsigned
174 }
175 else
176 {
179 }
180
181 random_delay =
187 &reannounce_regex, cls);
188}
189
190
197static void
198announce_regex (const char *regex)
199{
200 char *copy;
201
202 if ((NULL == regex) || (0 == strlen (regex)))
203 {
204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Cannot announce empty regex\n");
205 return;
206 }
207
209 "Daemon for %s starting\n",
212 copy = GNUNET_strdup (regex);
214 (void *) copy);
215}
216
217
226static int
227scan (void *cls, const char *filename)
228{
229 long n = (long) cls;
230 static long c = 0;
231
232 if (c == n)
233 {
235 return GNUNET_NO;
236 }
237 c++;
238 return GNUNET_OK;
239}
240
241
250static void
251run (void *cls, char *const *args GNUNET_UNUSED,
252 const char *cfgfile GNUNET_UNUSED,
253 const struct GNUNET_CONFIGURATION_Handle *cfg_)
254{
255 char *regex = NULL;
256 char **components;
257 char *policy_dir;
258 long long unsigned int peer_id;
259
260 cfg = cfg_;
261
262 if (GNUNET_OK !=
264 "MAX_PATH_COMPRESSION",
266 {
268 _
269 (
270 "%s service is lacking key configuration settings (%s). Exiting.\n"),
271 "regexprofiler", "max_path_compression");
274 return;
275 }
276 if (GNUNET_OK !=
278 "POLICY_DIR", &policy_dir))
279 {
281 "POLICY_DIR");
284 return;
285 }
286 if (GNUNET_OK !=
288 "PEERID", &peer_id))
289 {
294 return;
295 }
296
297 if (GNUNET_OK !=
299 "REGEX_PREFIX", &regex_prefix))
300 {
302 "REGEX_PREFIX");
306 return;
307 }
308
309 if (GNUNET_OK !=
311 "REANNOUNCE_PERIOD_MAX",
313 {
315 "reannounce_period_max not given. Using 10 minutes.\n");
318 }
319
320 pils_handle = GNUNET_PILS_connect (cfg, NULL, NULL);
321 stats_handle = GNUNET_STATISTICS_create ("regexprofiler", cfg);
323
324 if (NULL == dht_handle)
325 {
327 "Could not acquire dht handle. Exiting.\n");
331 return;
332 }
333
334 /* Read regexes from policy files */
336 (void *) (long) peer_id));
337 if (NULL == (components = REGEX_TEST_read_from_file (policy_filename)))
338 {
340 "Policy file %s contains no policies. Exiting.\n",
345 return;
346 }
348 regex = REGEX_TEST_combine (components, 16);
349 REGEX_TEST_free_from_file (components);
350
351 /* Announcing regexes from policy_filename */
353 "%s(%s)(0|1|2|3|4|5|6|7|8|9|a|b|c|d|e|f)*",
355 regex);
357 GNUNET_free (regex);
359
360 /* Scheduled the task to clean up when shutdown is called */
362 NULL);
363}
364
365
373int
374main (int argc, char *const *argv)
375{
376 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
378 };
379
380 return (GNUNET_OK ==
382 argc, argv, "regexprofiler",
384 (
385 "Daemon to announce regular expressions for the peer using cadet."),
386 options, &run, NULL)) ? global_ret : 1;
387}
388
389
390#if defined(__linux__) && defined(__GLIBC__)
391#include <malloc.h>
392
393void __attribute__ ((constructor))
394GNUNET_REGEX_memory_init (void);
395
399void __attribute__ ((constructor))
400GNUNET_REGEX_memory_init (void)
401{
402 mallopt (M_TRIM_THRESHOLD, 4 * 1024);
403 mallopt (M_TOP_PAD, 1 * 1024);
404 malloc_trim (0);
405}
406
407
408#endif
409
410
411/* end of gnunet-daemon-regexprofiler.c */
struct GNUNET_GETOPT_CommandLineOption options[]
Definition 002.c:5
int main()
Program to simulate results from GCP_get_desirability_of_path() for various plausible inputs.
#define gettext_noop(String)
Definition gettext.h:74
static char * peer_id
Option –peer.
static char * rx_with_pfx
Regex with prefix.
static void reannounce_regex(void *cls)
Announce a previously announced regex reusing cached data.
static int global_ret
Return value from 'main'.
static struct GNUNET_TIME_Relative reannounce_period_max
What's the maximum reannounce period.
static void run(void *cls, char *const *args, const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *cfg_)
Main function that will be run by the scheduler.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we use.
static struct REGEX_INTERNAL_Announcement * announce_handle
Peer's regex announce handle.
static struct GNUNET_PILS_Handle * pils_handle
Handle to the pils service.
static void announce_regex(const char *regex)
Announce the given regular expression using regex and the path compression length read from config.
static struct GNUNET_DHT_Handle * dht_handle
Peer's dht handle.
static struct GNUNET_STATISTICS_Handle * stats_handle
Handle to the statistics service.
static char * policy_filename
Name of the file containing policies that this peer should announce.
static unsigned int rounds
How many put rounds should we do.
static int scan(void *cls, const char *filename)
Scan through the policy_dir looking for the n-th filename.
static struct GNUNET_SCHEDULER_Task * reannounce_task
Periodically reannounce regex.
static char * regex_prefix
Prefix to add before every regex we're announcing.
static unsigned long long max_path_compression
Maximal path compression length for regex announcing.
static char * filename
struct GNUNET_SCHEDULER_Task * shutdown_task
static char * policy_dir
Folder where policy files are stored.
API to the DHT service.
struct GNUNET_PILS_Handle * GNUNET_PILS_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, GNUNET_PILS_PidChangeCallback pid_change_cb, void *cls)
Connect to the PILS service.
Definition pils_api.c:465
void GNUNET_PILS_disconnect(struct GNUNET_PILS_Handle *handle)
Disconnect from the PILS service.
Definition pils_api.c:488
struct GNUNET_PQ_ResultSpec __attribute__
API to create, modify and access statistics.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_number(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, unsigned long long *number)
Get a configuration value that should be a number.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_time(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, struct GNUNET_TIME_Relative *time)
Get a configuration value that should be a relative time.
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_string(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option, char **value)
Get a configuration value that should be a string.
uint32_t GNUNET_CRYPTO_random_u32(enum GNUNET_CRYPTO_Quality mode, uint32_t i)
Produce a random value.
@ GNUNET_CRYPTO_QUALITY_WEAK
No good quality of the operation is needed (i.e., random numbers can be pseudo-random).
struct GNUNET_DHT_Handle * GNUNET_DHT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, unsigned int ht_len)
Initialize the connection with the DHT service.
Definition dht_api.c:1036
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition dht_api.c:1059
int GNUNET_DISK_directory_scan(const char *dir_name, GNUNET_FileNameCallback callback, void *callback_cls)
Scan a directory for files.
Definition disk.c:881
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
#define GNUNET_log(kind,...)
#define GNUNET_UNUSED
gcc-ism to document unused arguments
@ GNUNET_OK
@ GNUNET_NO
@ GNUNET_SYSERR
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
void GNUNET_log_config_missing(enum GNUNET_ErrorType kind, const char *section, const char *option)
Log error message about missing configuration option.
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
@ GNUNET_ERROR_TYPE_INFO
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_free(ptr)
Wrapper around free.
#define GNUNET_free_nz(ptr)
Wrapper around free.
const struct GNUNET_OS_ProjectData * GNUNET_OS_project_data_gnunet(void)
Return default project data used by 'libgnunetutil' for GNUnet.
enum GNUNET_GenericReturnValue GNUNET_PROGRAM_run(const struct GNUNET_OS_ProjectData *pd, int argc, char *const *argv, const char *binaryName, const char *binaryHelp, const struct GNUNET_GETOPT_CommandLineOption *options, GNUNET_PROGRAM_Main task, void *task_cls)
Run a standard GNUnet command startup sequence (initialize loggers and configuration,...
Definition program.c:407
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition scheduler.c:572
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_shutdown(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run on shutdown, that is when a CTRL-C signal is received,...
Definition scheduler.c:1345
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition scheduler.c:986
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
Definition scheduler.c:1310
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:1283
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
void GNUNET_STATISTICS_update(struct GNUNET_STATISTICS_Handle *handle, const char *name, int64_t delta, int make_persistent)
Set statistic value for the peer.
#define GNUNET_TIME_UNIT_MINUTES
One minute.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
Definition time.c:486
#define GNUNET_TIME_UNIT_MICROSECONDS
One microsecond, our basic time unit.
#define _(String)
GNU gettext support macro.
Definition platform.h:179
struct REGEX_INTERNAL_Announcement * REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht, struct GNUNET_PILS_Handle *pils, const char *regex, uint16_t compression, struct GNUNET_STATISTICS_Handle *stats)
Announce a regular expression: put all states of the automaton in the DHT.
void REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h)
Clear all cached data used by a regex announce.
void REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h)
Announce again a regular expression previously announced.
library to parse regular expressions into dfa
void REGEX_TEST_free_from_file(char **regexes)
Free all memory reserved for a set of regexes created by read_from_file.
char * REGEX_TEST_combine(char *const regexes[], unsigned int alphabet_size)
Combine an array of regexes into a single prefix-shared regex.
char ** REGEX_TEST_read_from_file(const char *filename)
Read a set of regexes from a file, one per line and return them in an array suitable for REGEX_TEST_c...
Connection to the DHT service.
Definition dht_api.c:235
Definition of a command line option.
A handle for the PILS service.
Definition pils_api.c:82
Entry in list of pending tasks.
Definition scheduler.c:141
Handle for the service.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Handle to store cached data about a regex announce.