GNUnet 0.22.2
gnunet-service-regex.c File Reference

service to advertise capabilities described as regex and to lookup capabilities by regex More...

#include "gnunet_util_lib.h"
#include "regex_internal_lib.h"
#include "regex_ipc.h"
Include dependency graph for gnunet-service-regex.c:

Go to the source code of this file.

Data Structures

struct  ClientEntry
 Information about one of our clients. More...
 

Functions

static void cleanup_task (void *cls)
 Task run during shutdown. More...
 
static void reannounce (void *cls)
 Periodic task to refresh our announcement of the regex. More...
 
static int check_announce (void *cls, const struct AnnounceMessage *am)
 Check ANNOUNCE message. More...
 
static void handle_announce (void *cls, const struct AnnounceMessage *am)
 Handle ANNOUNCE message. More...
 
static void handle_search_result (void *cls, const struct GNUNET_PeerIdentity *id, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_length)
 Handle result, pass it back to the client. More...
 
static int check_search (void *cls, const struct RegexSearchMessage *sm)
 Check SEARCH message. More...
 
static void handle_search (void *cls, const struct RegexSearchMessage *sm)
 Handle SEARCH message. More...
 
static void run (void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SERVICE_Handle *service)
 Process regex requests. More...
 
static void * client_connect_cb (void *cls, struct GNUNET_SERVICE_Client *c, struct GNUNET_MQ_Handle *mq)
 Callback called when a client connects to the service. More...
 
static void client_disconnect_cb (void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
 Callback called when a client disconnected from the service. More...
 
 GNUNET_SERVICE_MAIN (GNUNET_OS_project_data_gnunet(), "regex", GNUNET_SERVICE_OPTION_NONE, &run, &client_connect_cb, &client_disconnect_cb, NULL, GNUNET_MQ_hd_var_size(announce, GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, struct AnnounceMessage, NULL), GNUNET_MQ_hd_var_size(search, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, struct RegexSearchMessage, NULL), GNUNET_MQ_handler_end())
 Define "main" method using service macro. More...
 

Variables

static struct GNUNET_DHT_Handledht
 Connection to the DHT. More...
 
static struct GNUNET_STATISTICS_Handlestats
 Handle for doing statistics. More...
 
static struct GNUNET_CRYPTO_EddsaPrivateKeymy_private_key
 Private key for this peer. More...
 

Detailed Description

service to advertise capabilities described as regex and to lookup capabilities by regex

Author
Christian Grothoff

Definition in file gnunet-service-regex.c.

Function Documentation

◆ cleanup_task()

static void cleanup_task ( void *  cls)
static

Task run during shutdown.

Parameters
clsunused

Definition at line 91 of file gnunet-service-regex.c.

92{
94 dht = NULL;
96 GNUNET_NO);
97 stats = NULL;
99 my_private_key = NULL;
100}
static struct GNUNET_DHT_Handle * dht
Connection to the DHT.
static struct GNUNET_STATISTICS_Handle * stats
Handle for doing statistics.
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Private key for this peer.
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:1053
@ GNUNET_NO
#define GNUNET_free(ptr)
Wrapper around free.
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).

References dht, GNUNET_DHT_disconnect(), GNUNET_free, GNUNET_NO, GNUNET_STATISTICS_destroy(), my_private_key, and stats.

Referenced by run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ reannounce()

static void reannounce ( void *  cls)
static

Periodic task to refresh our announcement of the regex.

Parameters
clsthe struct ClientEntry * of the client that triggered the announcement

Definition at line 110 of file gnunet-service-regex.c.

111{
112 struct ClientEntry *ce = cls;
113
116 &reannounce,
117 ce);
118}
static void reannounce(void *cls)
Periodic task to refresh our announcement of the regex.
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:1277
void REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h)
Announce again a regular expression previously announced.
Information about one of our clients.
struct REGEX_INTERNAL_Announcement * ah
Announcement handle (if this client is announcing).
struct GNUNET_TIME_Relative frequency
Refresh frequency for announcements.
struct GNUNET_SCHEDULER_Task * refresh_task
Task for re-announcing.

References ClientEntry::ah, ClientEntry::frequency, GNUNET_SCHEDULER_add_delayed(), reannounce(), ClientEntry::refresh_task, and REGEX_INTERNAL_reannounce().

Referenced by handle_announce(), and reannounce().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_announce()

static int check_announce ( void *  cls,
const struct AnnounceMessage am 
)
static

Check ANNOUNCE message.

Parameters
clsidentification of the client
amthe actual message
Returns
GNUNET_OK if am is well-formed

Definition at line 129 of file gnunet-service-regex.c.

131{
132 struct ClientEntry *ce = cls;
133
135 if (NULL != ce->ah)
136 {
137 /* only one announcement per client allowed */
138 GNUNET_break (0);
139 return GNUNET_SYSERR;
140 }
141 return GNUNET_OK;
142}
@ GNUNET_OK
@ GNUNET_SYSERR
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...

References ClientEntry::ah, GNUNET_break, GNUNET_MQ_check_zero_termination, GNUNET_OK, and GNUNET_SYSERR.

◆ handle_announce()

static void handle_announce ( void *  cls,
const struct AnnounceMessage am 
)
static

Handle ANNOUNCE message.

Parameters
clsidentification of the client
amthe actual message

Definition at line 152 of file gnunet-service-regex.c.

154{
155 struct ClientEntry *ce = cls;
156 const char *regex;
157
158 regex = (const char *) &am[1];
161 &reannounce,
162 ce);
164 "Starting to announce regex `%s' every %s\n",
165 regex,
167 GNUNET_NO));
170 regex,
171 ntohs (am->compression),
172 stats);
173 if (NULL == ce->ah)
174 {
175 GNUNET_break (0);
177 ce->refresh_task = NULL;
179 return;
180 }
182}
#define GNUNET_log(kind,...)
@ GNUNET_ERROR_TYPE_DEBUG
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:980
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2418
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2389
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:630
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
Definition: strings.c:579
struct REGEX_INTERNAL_Announcement * REGEX_INTERNAL_announce(struct GNUNET_DHT_Handle *dht, const struct GNUNET_CRYPTO_EddsaPrivateKey *priv, const char *regex, uint16_t compression, struct GNUNET_STATISTICS_Handle *stats)
Announce a regular expression: put all states of the automaton in the DHT.
struct GNUNET_TIME_RelativeNBO refresh_delay
Delay between repeated announcements.
Definition: regex_ipc.h:54
uint16_t compression
How many characters can we squeeze per edge?
Definition: regex_ipc.h:44
struct GNUNET_SERVICE_Client * client
Handle identifying the client.

References ClientEntry::ah, ClientEntry::client, AnnounceMessage::compression, dht, ClientEntry::frequency, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_NO, GNUNET_SCHEDULER_add_delayed(), GNUNET_SCHEDULER_cancel(), GNUNET_SERVICE_client_continue(), GNUNET_SERVICE_client_drop(), GNUNET_STRINGS_relative_time_to_string(), GNUNET_TIME_relative_ntoh(), my_private_key, reannounce(), AnnounceMessage::refresh_delay, ClientEntry::refresh_task, REGEX_INTERNAL_announce(), and stats.

Here is the call graph for this function:

◆ handle_search_result()

static void handle_search_result ( void *  cls,
const struct GNUNET_PeerIdentity id,
const struct GNUNET_DHT_PathElement get_path,
unsigned int  get_path_length,
const struct GNUNET_DHT_PathElement put_path,
unsigned int  put_path_length 
)
static

Handle result, pass it back to the client.

Parameters
clsthe struct ClientEntry of the client searching
idPeer providing a regex that matches the string.
get_pathPath of the get request.
get_path_lengthLength of get_path.
put_pathPath of the put request.
put_path_lengthLength of the put_path.

Definition at line 196 of file gnunet-service-regex.c.

202{
203 struct ClientEntry *ce = cls;
204 struct GNUNET_MQ_Envelope *env;
205 struct ResultMessage *result;
206 struct GNUNET_PeerIdentity *gp;
207 uint16_t size;
208
209 if ((get_path_length >= 65536) ||
210 (put_path_length >= 65536) ||
211 ( ((get_path_length + put_path_length)
212 * sizeof(struct GNUNET_PeerIdentity))
213 + sizeof(struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE) )
214 {
215 GNUNET_break (0);
216 return;
217 }
218 size = (get_path_length + put_path_length)
219 * sizeof(struct GNUNET_PeerIdentity);
221 size,
223 result->get_path_length = htons ((uint16_t) get_path_length);
224 result->put_path_length = htons ((uint16_t) put_path_length);
225 result->id = *id;
226 gp = &result->id;
227 for (unsigned int i = 0; i<get_path_length; i++)
228 gp[i + 1] = get_path[i].pred;
229 for (unsigned int i = 0; i<put_path_length; i++)
230 gp[i + get_path_length + 1] = put_path[i].pred;
231 GNUNET_MQ_send (ce->mq,
232 env);
233}
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static struct GNUNET_IDENTITY_Handle * id
Handle to IDENTITY.
static int result
Global testing status.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:305
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct.
Definition: gnunet_mq_lib.h:61
#define GNUNET_MESSAGE_TYPE_REGEX_RESULT
Result in response to regex search.
static unsigned int size
Size of the "table".
Definition: peer.c:68
struct GNUNET_MQ_Handle * mq
Queue for transmissions to client.
The identity of the host (wraps the signing key of the peer).
Result from regex search.
Definition: regex_ipc.h:78

References env, GNUNET_break, GNUNET_MAX_MESSAGE_SIZE, GNUNET_MESSAGE_TYPE_REGEX_RESULT, GNUNET_MQ_msg_extra, GNUNET_MQ_send(), id, ClientEntry::mq, result, and size.

Referenced by handle_search().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_search()

static int check_search ( void *  cls,
const struct RegexSearchMessage sm 
)
static

Check SEARCH message.

Parameters
clsidentification of the client
smthe actual message

Definition at line 243 of file gnunet-service-regex.c.

245{
246 struct ClientEntry *ce = cls;
247 const char *string;
248 uint16_t size;
249
250 size = ntohs (sm->header.size) - sizeof(*sm);
251 string = (const char *) &sm[1];
252 if ('\0' != string[size - 1])
253 {
254 GNUNET_break (0);
255 return GNUNET_SYSERR;
256 }
257 if (NULL != ce->sh)
258 {
259 /* only one search allowed per client */
260 GNUNET_break (0);
261 return GNUNET_SYSERR;
262 }
263 return GNUNET_OK;
264}
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
struct REGEX_INTERNAL_Search * sh
Search handle (if this client is searching).
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_REGEX_SEARCH.
Definition: regex_ipc.h:68

References GNUNET_break, GNUNET_OK, GNUNET_SYSERR, RegexSearchMessage::header, ClientEntry::sh, GNUNET_MessageHeader::size, and size.

◆ handle_search()

static void handle_search ( void *  cls,
const struct RegexSearchMessage sm 
)
static

Handle SEARCH message.

Parameters
clsidentification of the client
smthe actual message

Definition at line 274 of file gnunet-service-regex.c.

276{
277 struct ClientEntry *ce = cls;
278 const char *string;
279
280 string = (const char *) &sm[1];
282 "Starting to search for `%s'\n",
283 string);
285 string,
287 ce,
288 stats);
289 if (NULL == ce->sh)
290 {
291 GNUNET_break (0);
293 return;
294 }
296}
static void handle_search_result(void *cls, const struct GNUNET_PeerIdentity *id, const struct GNUNET_DHT_PathElement *get_path, unsigned int get_path_length, const struct GNUNET_DHT_PathElement *put_path, unsigned int put_path_length)
Handle result, pass it back to the client.
struct REGEX_INTERNAL_Search * REGEX_INTERNAL_search(struct GNUNET_DHT_Handle *dht, const char *string, REGEX_INTERNAL_Found callback, void *callback_cls, struct GNUNET_STATISTICS_Handle *stats)
Search for a peer offering a regex matching certain string in the DHT.

References ClientEntry::client, dht, GNUNET_break, GNUNET_ERROR_TYPE_DEBUG, GNUNET_log, GNUNET_SERVICE_client_continue(), GNUNET_SERVICE_client_drop(), handle_search_result(), REGEX_INTERNAL_search(), ClientEntry::sh, and stats.

Here is the call graph for this function:

◆ run()

static void run ( void *  cls,
const struct GNUNET_CONFIGURATION_Handle cfg,
struct GNUNET_SERVICE_Handle service 
)
static

Process regex requests.

Parameters
clsclosure
cfgconfiguration to use
servicethe initialized service

Definition at line 307 of file gnunet-service-regex.c.

310{
312 if (NULL == my_private_key)
313 {
315 return;
316 }
317 dht = GNUNET_DHT_connect (cfg, 1024);
318 if (NULL == dht)
319 {
321 my_private_key = NULL;
323 return;
324 }
326 NULL);
328}
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:108
static void cleanup_task(void *cls)
Task run during shutdown.
struct GNUNET_CRYPTO_EddsaPrivateKey * GNUNET_CRYPTO_eddsa_key_create_from_configuration(const struct GNUNET_CONFIGURATION_Handle *cfg)
Create a new private key by reading our peer's key from the file specified in the configuration.
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:1030
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:567
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:1339
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.

References cfg, cleanup_task(), dht, GNUNET_CRYPTO_eddsa_key_create_from_configuration(), GNUNET_DHT_connect(), GNUNET_free, GNUNET_SCHEDULER_add_shutdown(), GNUNET_SCHEDULER_shutdown(), GNUNET_STATISTICS_create(), my_private_key, and stats.

Here is the call graph for this function:

◆ client_connect_cb()

static void * client_connect_cb ( void *  cls,
struct GNUNET_SERVICE_Client c,
struct GNUNET_MQ_Handle mq 
)
static

Callback called when a client connects to the service.

Parameters
clsclosure for the service
cthe new client that connected to the service
mqthe message queue used to send messages to the client
Returns
c

Definition at line 340 of file gnunet-service-regex.c.

343{
344 struct ClientEntry *ce;
345
346 ce = GNUNET_new (struct ClientEntry);
347 ce->client = c;
348 ce->mq = mq;
349 return ce;
350}
struct GNUNET_MQ_Handle * mq
Definition: 003.c:5
#define GNUNET_new(type)
Allocate a struct or union of the given type.

References ClientEntry::client, GNUNET_new, mq, and ClientEntry::mq.

◆ client_disconnect_cb()

static void client_disconnect_cb ( void *  cls,
struct GNUNET_SERVICE_Client c,
void *  internal_cls 
)
static

Callback called when a client disconnected from the service.

Parameters
clsclosure for the service
cthe client that disconnected
internal_clsshould be equal to c

Definition at line 361 of file gnunet-service-regex.c.

364{
365 struct ClientEntry *ce = internal_cls;
366
367 if (NULL != ce->refresh_task)
368 {
370 ce->refresh_task = NULL;
371 }
372 if (NULL != ce->ah)
373 {
375 ce->ah = NULL;
376 }
377 if (NULL != ce->sh)
378 {
380 ce->sh = NULL;
381 }
382 GNUNET_free (ce);
383}
void REGEX_INTERNAL_search_cancel(struct REGEX_INTERNAL_Search *h)
Cancel an ongoing regex search in the DHT and free all resources.
void REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h)
Clear all cached data used by a regex announce.

References ClientEntry::ah, GNUNET_free, GNUNET_SCHEDULER_cancel(), ClientEntry::refresh_task, REGEX_INTERNAL_announce_cancel(), REGEX_INTERNAL_search_cancel(), and ClientEntry::sh.

Here is the call graph for this function:

◆ GNUNET_SERVICE_MAIN()

GNUNET_SERVICE_MAIN ( GNUNET_OS_project_data_gnunet()  ,
"regex"  ,
GNUNET_SERVICE_OPTION_NONE  ,
run,
client_connect_cb,
client_disconnect_cb,
NULL  ,
GNUNET_MQ_hd_var_size(announce, GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE, struct AnnounceMessage, NULL)  ,
GNUNET_MQ_hd_var_size(search, GNUNET_MESSAGE_TYPE_REGEX_SEARCH, struct RegexSearchMessage, NULL)  ,
GNUNET_MQ_handler_end()   
)

Define "main" method using service macro.

Variable Documentation

◆ dht

struct GNUNET_DHT_Handle* dht
static

Connection to the DHT.

Definition at line 72 of file gnunet-service-regex.c.

Referenced by cleanup_task(), handle_announce(), handle_search(), and run().

◆ stats

struct GNUNET_STATISTICS_Handle* stats
static

Handle for doing statistics.

Definition at line 77 of file gnunet-service-regex.c.

Referenced by cleanup_task(), handle_announce(), handle_search(), and run().

◆ my_private_key

struct GNUNET_CRYPTO_EddsaPrivateKey* my_private_key
static

Private key for this peer.

Definition at line 82 of file gnunet-service-regex.c.

Referenced by cleanup_task(), handle_announce(), and run().