GNUnet  0.10.x
gnunet-service-regex.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2013 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
27 #include "platform.h"
28 #include "gnunet_util_lib.h"
29 #include "regex_internal_lib.h"
30 #include "regex_ipc.h"
31 
32 
36 struct ClientEntry {
41 
46 
51 
56 
61 
66 };
67 
68 
72 static struct GNUNET_DHT_Handle *dht;
73 
78 
83 
84 
90 static void
91 cleanup_task(void *cls)
92 {
94  dht = NULL;
96  GNUNET_NO);
97  stats = NULL;
98  GNUNET_free(my_private_key);
99  my_private_key = NULL;
100 }
101 
102 
109 static void
110 reannounce(void *cls)
111 {
112  struct ClientEntry *ce = cls;
113 
116  &reannounce,
117  ce);
118 }
119 
120 
128 static int
129 check_announce(void *cls,
130  const struct AnnounceMessage *am)
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 }
143 
144 
151 static void
152 handle_announce(void *cls,
153  const struct AnnounceMessage *am)
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));
168  ce->ah = REGEX_INTERNAL_announce(dht,
169  my_private_key,
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 }
183 
184 
195 static void
197  const struct GNUNET_PeerIdentity *id,
198  const struct GNUNET_PeerIdentity *get_path,
199  unsigned int get_path_length,
200  const struct GNUNET_PeerIdentity *put_path,
201  unsigned int put_path_length)
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) * sizeof(struct GNUNET_PeerIdentity))
212  + sizeof(struct ResultMessage) >= GNUNET_MAX_MESSAGE_SIZE)
213  {
214  GNUNET_break(0);
215  return;
216  }
217  size = (get_path_length + put_path_length) * sizeof(struct GNUNET_PeerIdentity);
218  env = GNUNET_MQ_msg_extra(result,
219  size,
221  result->get_path_length = htons((uint16_t)get_path_length);
222  result->put_path_length = htons((uint16_t)put_path_length);
223  result->id = *id;
224  gp = &result->id;
225  GNUNET_memcpy(&gp[1],
226  get_path,
227  get_path_length * sizeof(struct GNUNET_PeerIdentity));
228  GNUNET_memcpy(&gp[1 + get_path_length],
229  put_path,
230  put_path_length * sizeof(struct GNUNET_PeerIdentity));
231  GNUNET_MQ_send(ce->mq,
232  env);
233 }
234 
235 
242 static int
243 check_search(void *cls,
244  const struct RegexSearchMessage *sm)
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 }
265 
266 
273 static void
274 handle_search(void *cls,
275  const struct RegexSearchMessage *sm)
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);
284  ce->sh = REGEX_INTERNAL_search(dht,
285  string,
287  ce,
288  stats);
289  if (NULL == ce->sh)
290  {
291  GNUNET_break(0);
293  return;
294  }
296 }
297 
298 
306 static void
307 run(void *cls,
308  const struct GNUNET_CONFIGURATION_Handle *cfg,
310 {
312  if (NULL == my_private_key)
313  {
315  return;
316  }
317  dht = GNUNET_DHT_connect(cfg, 1024);
318  if (NULL == dht)
319  {
320  GNUNET_free(my_private_key);
321  my_private_key = NULL;
323  return;
324  }
326  NULL);
327  stats = GNUNET_STATISTICS_create("regex", cfg);
328 }
329 
330 
339 static void *
341  struct GNUNET_SERVICE_Client *c,
342  struct GNUNET_MQ_Handle *mq)
343 {
344  struct ClientEntry *ce;
345 
346  ce = GNUNET_new(struct ClientEntry);
347  ce->client = c;
348  ce->mq = mq;
349  return ce;
350 }
351 
352 
360 static void
362  struct GNUNET_SERVICE_Client *c,
363  void *internal_cls)
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 }
384 
385 
390  ("regex",
392  &run,
395  NULL,
396  GNUNET_MQ_hd_var_size(announce,
398  struct AnnounceMessage,
399  NULL),
400  GNUNET_MQ_hd_var_size(search,
402  struct RegexSearchMessage,
403  NULL),
405 
406 
407 /* end of gnunet-service-regex.c */
static void client_disconnect_cb(void *cls, struct GNUNET_SERVICE_Client *c, void *internal_cls)
Callback called when a client disconnected from the service.
uint16_t compression
How many characters can we squeeze per edge?
Definition: regex_ipc.h:43
static struct GNUNET_SERVICE_Handle * service
Handle to our service instance.
Message to initiate regex search.
Definition: regex_ipc.h:62
static void handle_announce(void *cls, const struct AnnounceMessage *am)
Handle ANNOUNCE message.
Handle to a service.
Definition: service.c:114
#define GNUNET_MESSAGE_TYPE_REGEX_SEARCH
Search for peer with matching capability.
Result from regex search.
Definition: regex_ipc.h:75
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, or when GNUNET_SCHEDULER_shutdown() is being invoked.
Definition: scheduler.c:1284
struct GNUNET_STATISTICS_Handle * GNUNET_STATISTICS_create(const char *subsystem, const struct GNUNET_CONFIGURATION_Handle *cfg)
Get handle for the statistics service.
static void handle_search_result(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)
Handle result, pass it back to the client.
struct GNUNET_MessageHeader header
Type is GNUNET_MESSAGE_TYPE_REGEX_SEARCH.
Definition: regex_ipc.h:66
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_NO
Definition: gnunet_common.h:78
void REGEX_INTERNAL_announce_cancel(struct REGEX_INTERNAL_Announcement *h)
Clear all cached data used by a regex announce.
static struct GNUNET_IDENTITY_Handle * id
Handle to identity service.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
struct REGEX_INTERNAL_Announcement * ah
Announcement handle (if this client is announcing).
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct GNUNET_TIME_Relative frequency
Refresh frequency for announcements.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format...
void GNUNET_STATISTICS_destroy(struct GNUNET_STATISTICS_Handle *h, int sync_first)
Destroy a handle (free all state associated with it).
void GNUNET_SCHEDULER_shutdown(void)
Request the shutdown of a scheduler.
Definition: scheduler.c:517
Handle for the service.
struct REGEX_INTERNAL_Search * sh
Search handle (if this client is searching).
struct GNUNET_SERVICE_Client * client
Handle identifying the client.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
#define GNUNET_MESSAGE_TYPE_REGEX_RESULT
Result in response to regex search.
Connection to the DHT service.
Definition: dht_api.c:198
Handle to a client that is connected to a service.
Definition: service.c:246
static void handle_search(void *cls, const struct RegexSearchMessage *sm)
Handle SEARCH message.
regex IPC messages (not called &#39;regex.h&#39; due to conflict with system headers)
#define GNUNET_MQ_msg_extra(mvar, esize, type)
Allocate an envelope, with extra space allocated after the space needed by the message struct...
Definition: gnunet_mq_lib.h:52
GNUNET_SERVICE_MAIN("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.
uint16_t put_path_length
Number of entries in the PUT path.
Definition: regex_ipc.h:89
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:1237
Request for regex service to announce capability.
Definition: regex_ipc.h:34
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...
library to parse regular expressions into dfa
Information about one of our clients.
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
static void reannounce(void *cls)
Periodic task to refresh our announcement of the regex.
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:686
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.
static struct GNUNET_STATISTICS_Handle * stats
Handle for doing statistics.
struct GNUNET_SCHEDULER_Task * refresh_task
Task for re-announcing.
static int result
Global testing status.
Handle to store cached data about a regex announce.
void REGEX_INTERNAL_search_cancel(struct REGEX_INTERNAL_Search *h)
Cancel an ongoing regex search in the DHT and free all resources.
static struct GNUNET_CRYPTO_EddsaPrivateKey * my_private_key
Private key for this peer.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:639
static int check_announce(void *cls, const struct AnnounceMessage *am)
Check ANNOUNCE message.
void GNUNET_SERVICE_client_drop(struct GNUNET_SERVICE_Client *c)
Ask the server to disconnect from the given client.
Definition: service.c:2315
static struct GNUNET_DHT_Handle * dht
Connection to the DHT.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
Definition: gnunet-arm.c:104
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:66
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.
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:885
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_MQ_Handle * mq
Queue for transmissions to client.
void GNUNET_DHT_disconnect(struct GNUNET_DHT_Handle *handle)
Shutdown connection with the DHT service.
Definition: dht_api.c:913
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
static int check_search(void *cls, const struct RegexSearchMessage *sm)
Check SEARCH message.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
Struct to keep information of searches of services described by a regex using a user-provided string ...
void REGEX_INTERNAL_reannounce(struct REGEX_INTERNAL_Announcement *h)
Announce again a regular expression previously announced.
struct GNUNET_PeerIdentity id
Identity of the peer that was found.
Definition: regex_ipc.h:94
Handle to a message queue.
Definition: mq.c:84
Private ECC key encoded for transmission.
static void cleanup_task(void *cls)
Task run during shutdown.
The identity of the host (wraps the signing key of the peer).
#define GNUNET_MESSAGE_TYPE_REGEX_ANNOUNCE
Advertise regex capability.
configuration data
Definition: configuration.c:83
uint16_t get_path_length
Number of entries in the GET path.
Definition: regex_ipc.h:84
#define GNUNET_log(kind,...)
Entry in list of pending tasks.
Definition: scheduler.c:131
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&#39;s key from the file specified in the configuration...
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:351
void GNUNET_SERVICE_client_continue(struct GNUNET_SERVICE_Client *c)
Continue receiving further messages from the given client.
Definition: service.c:2234
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
struct GNUNET_TIME_RelativeNBO refresh_delay
Delay between repeated announcements.
Definition: regex_ipc.h:53
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
static void run(void *cls, const struct GNUNET_CONFIGURATION_Handle *cfg, struct GNUNET_SERVICE_Handle *service)
Process regex requests.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
Definition: scheduler.c:956