GNUnet 0.26.2-98-gb402d9955
 
Loading...
Searching...
No Matches
gnunet-regex.c
Go to the documentation of this file.
1/*
2 This file is part of GNUnet.
3 Copyright (C) 2026 GNUnet e.V.
4
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your option) any later version.
9
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Affero General Public License for more details.
14
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 SPDX-License-Identifier: AGPL3.0-or-later
19 */
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29
30#include "gnunet_common.h"
33#include "gnunet_time_lib.h"
34#include "gnunet_util_lib.h"
35
38
40
42uint16_t compression;
43
47
49
50
51static void
71
72
83static void
85 const struct GNUNET_PeerIdentity *id,
86 const struct GNUNET_PeerIdentity *get_path,
87 unsigned int get_path_length,
88 const struct GNUNET_PeerIdentity *put_path,
89 unsigned int put_path_length)
90{
91 struct GNUNET_HashCode hash;
92 size_t get_path_size, put_path_size;
93 unsigned int i;
94
95 get_path_size = sizeof (struct GNUNET_PeerIdentity) * get_path_length;
96 put_path_size = sizeof (struct GNUNET_PeerIdentity) * put_path_length;
97
99 &hash, sizeof (hash),
100 "regex_match", 11,
101 id, sizeof (*id),
102 GNUNET_CRYPTO_kdf_arg (get_path, get_path_size),
103 GNUNET_CRYPTO_kdf_arg (put_path, put_path_size)))
104 return;
105
107 return;
108
109 printf ("%s ( get: %u, put: %u )\n", GNUNET_i2s_full (id), get_path_length,
110 put_path_length);
111
112 for (i = 0; i < get_path_length; i++)
113 printf (" - get: %s\n", GNUNET_i2s_full (&(get_path[i])));
114
115 for (i = 0; i < put_path_length; i++)
116 printf (" - put: %s\n", GNUNET_i2s_full (&(put_path[i])));
117
119 matches, &hash, NULL,
121}
122
123
124static void
126{
127 delay_task = NULL;
128
129 printf ("> %s\n", expression);
130
131 if (cls)
134 cls);
135}
136
137
138static void
140{
141 delay_task = NULL;
142
143 print_announcement (NULL);
144
145 if (announcement)
146 {
148 announcement = NULL;
149 }
150
152}
153
154
163static void
164run (void *cls,
165 char *const *args,
166 const char *cfgfile,
167 const struct GNUNET_CONFIGURATION_Handle *cfg)
168{
169 int argc;
170 size_t len, pos;
171 unsigned int i;
172
173 for (argc = 0; NULL != args[argc]; argc++)
174 ;
175
176 len = 0;
177 for (i = 0; i < argc; i++)
178 len += strlen (args[i]);
179
180 if (argc > 0)
181 len += (argc - 1);
182
183 expression = GNUNET_malloc (len + 1);
185 expression[len] = '\0';
186 pos = 0;
187 for (i = 0; i < argc; i++)
188 {
189 size_t s;
190 s = strlen (args[i]);
191 GNUNET_memcpy (expression + pos, args[i], s);
192 pos += s;
193
194 if (i + 1 < argc)
195 {
196 expression[pos] = ' ';
197 pos++;
198 }
199 }
200
203
204 GNUNET_assert (pos == len);
205 if (announcing)
206 {
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Announcing regular expression: %s\n",
208 expression);
209
212
215 else
217 }
218 else
219 {
221 "Observing peers that announced matching regular expressions to: %s\n",
222 expression);
223
225 }
226}
227
228
236int
237main (int argc,
238 char **argv)
239{
240 const char *description =
241 "Announce regular expressions and observe announcing peers.";
242
244 GNUNET_GETOPT_option_flag ('a', "announce",
245 "flag to announce an expression",
246 &announcing),
247 GNUNET_GETOPT_option_relative_time ('r', "refresh", "DELAY",
248 "refresh delay of announcement",
250 GNUNET_GETOPT_option_uint16 ('c', "compression", "AMOUNT",
251 "amount of characters per edge to squeeze",
252 &compression),
254 };
255
256 return (GNUNET_OK ==
258 argc,
259 argv,
260 "gnunet-regex [OPTIONS] EXPRESSION\0",
262 &run,
263 NULL) ? EXIT_SUCCESS : EXIT_FAILURE);
264}
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 struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition gnunet-arm.c:108
struct GNUNET_REGEX_Search * search
int announcing
struct GNUNET_TIME_Relative refresh_delay
uint16_t compression
static void print_announcement(void *cls)
struct GNUNET_CONTAINER_MultiHashMap * matches
char * expression
struct GNUNET_SCHEDULER_Task * shutdown_task
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.
struct GNUNET_REGEX_Announcement * announcement
struct GNUNET_SCHEDULER_Task * delay_task
static void regex_found(void *cls, const struct GNUNET_PeerIdentity *id, const struct GNUNET_PeerIdentity *get_path, unsigned int get_path_length, const struct GNUNET_PeerIdentity *put_path, unsigned int put_path_length)
Search callback function, invoked for every result that was found.
static void shutdown_hook(void *cls)
static void drop_announcement(void *cls)
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
API to access regex service to advertise capabilities via regex and discover respective peers using m...
API to schedule computations using continuation passing style.
Functions related to time.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_relative_time(char shortName, const char *name, const char *argumentHelp, const char *description, struct GNUNET_TIME_Relative *val)
Allow user to specify a struct GNUNET_TIME_Relative (using human-readable "fancy" time).
#define GNUNET_GETOPT_OPTION_END
Marker for the end of the list of options.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_flag(char shortName, const char *name, const char *description, int *val)
Allow user to specify a flag (which internally means setting an integer to 1/GNUNET_YES/GNUNET_OK.
struct GNUNET_GETOPT_CommandLineOption GNUNET_GETOPT_option_uint16(char shortName, const char *name, const char *argumentHelp, const char *description, uint16_t *val)
Allow user to specify an uint16_t.
#define GNUNET_CRYPTO_hkdf_gnunet(result, out_len, xts, xts_len, skm, skm_len,...)
A peculiar HKDF instantiation that tried to mimic Truncated NMAC.
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_contains(const struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key)
Check if the map contains any value under the given key (including values that are NULL).
enum GNUNET_GenericReturnValue GNUNET_CONTAINER_multihashmap_put(struct GNUNET_CONTAINER_MultiHashMap *map, const struct GNUNET_HashCode *key, void *value, enum GNUNET_CONTAINER_MultiHashMapOption opt)
Store a key-value pair in the map.
void GNUNET_CONTAINER_multihashmap_destroy(struct GNUNET_CONTAINER_MultiHashMap *map)
Destroy a hash map.
struct GNUNET_CONTAINER_MultiHashMap * GNUNET_CONTAINER_multihashmap_create(unsigned int len, int do_not_copy_keys)
Create a multi hash map.
@ GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST
, ' bother checking if a value already exists (faster than GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE...
#define GNUNET_log(kind,...)
#define GNUNET_UNUSED
gcc-ism to document unused arguments
#define GNUNET_CRYPTO_kdf_arg(d, s)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
@ GNUNET_OK
@ GNUNET_YES
@ GNUNET_NO
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
const char * GNUNET_i2s_full(const struct GNUNET_PeerIdentity *pid)
Convert a peer identity to a string (for printing debug messages).
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(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
struct GNUNET_REGEX_Search * GNUNET_REGEX_search(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *string, GNUNET_REGEX_Found callback, void *callback_cls)
Search for a peer offering a regex matching certain string in the DHT.
void GNUNET_REGEX_announce_cancel(struct GNUNET_REGEX_Announcement *a)
Stop announcing the regex specified by the given handle.
void GNUNET_REGEX_search_cancel(struct GNUNET_REGEX_Search *s)
Stop search and free all data used by a GNUNET_REGEX_search call.
struct GNUNET_REGEX_Announcement * GNUNET_REGEX_announce(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *regex, struct GNUNET_TIME_Relative refresh_delay, uint16_t compression)
Announce this peer under the given regular expression.
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
bool GNUNET_TIME_relative_is_zero(struct GNUNET_TIME_Relative rel)
Test if rel is zero.
Definition time.c:660
Internal representation of the hash map.
Definition of a command line option.
const char * description
Help text for the option (description)
A 512-bit hashcode.
The identity of the host (wraps the signing key of the peer).
Handle to store cached data about a regex announce.
Handle to store data about a regex search.
Entry in list of pending tasks.
Definition scheduler.c:141
Time for relative time used by GNUnet, in microseconds.