GNUnet  0.10.x
Data Structures | Typedefs | Functions
regex_block_lib.h File Reference

common function to manipulate blocks stored by regex in the DHT More...

#include "platform.h"
#include "block_regex.h"
Include dependency graph for regex_block_lib.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  REGEX_BLOCK_Edge
 Edge representation. More...
 

Typedefs

typedef int(* REGEX_INTERNAL_EgdeIterator) (void *cls, const char *token, size_t len, const struct GNUNET_HashCode *key)
 Iterator over edges in a block. More...
 

Functions

int REGEX_BLOCK_check_proof (const char *proof, size_t proof_len, const struct GNUNET_HashCode *key)
 Check if the given 'proof' matches the given 'key'. More...
 
int REGEX_BLOCK_check (const struct RegexBlock *block, size_t size, const struct GNUNET_HashCode *query, const char *xquery)
 Check if the regex block is well formed, including all edges. More...
 
int REGEX_BLOCK_iterate (const struct RegexBlock *block, size_t size, REGEX_INTERNAL_EgdeIterator iterator, void *iter_cls)
 Iterate over all edges of a block of a regex state. More...
 
int REGEX_BLOCK_get_key (const struct RegexBlock *block, size_t block_len, struct GNUNET_HashCode *key)
 Obtain the key that a particular block is to be stored under. More...
 
int GNUNET_BLOCK_is_accepting (const struct RegexBlock *block, size_t block_len)
 Test if this block is marked as being an accept state. More...
 
struct RegexBlockREGEX_BLOCK_create (const char *proof, unsigned int num_edges, const struct REGEX_BLOCK_Edge *edges, int accepting, size_t *rsize)
 Construct a regex block to be stored in the DHT. More...
 

Detailed Description

common function to manipulate blocks stored by regex in the DHT

Author
Bartlomiej Polot

Definition in file regex_block_lib.h.

Typedef Documentation

◆ REGEX_INTERNAL_EgdeIterator

typedef int(* REGEX_INTERNAL_EgdeIterator) (void *cls, const char *token, size_t len, const struct GNUNET_HashCode *key)

Iterator over edges in a block.

Parameters
clsClosure.
tokenToken that follows to next state.
lenLength of token.
keyHash of next state.
Returns
GNUNET_YES if should keep iterating, GNUNET_NO otherwise.

Definition at line 111 of file regex_block_lib.h.

Function Documentation

◆ REGEX_BLOCK_check_proof()

int REGEX_BLOCK_check_proof ( const char *  proof,
size_t  proof_len,
const struct GNUNET_HashCode key 
)

Check if the given 'proof' matches the given 'key'.

Parameters
proofpartial regex of a state
proof_lennumber of bytes in proof
keyhash of a state.
Returns
GNUNET_OK if the proof is valid for the given key.
Parameters
proofpartial regex of a state
proof_lennumber of bytes in 'proof'
keyhash of a state.
Returns
GNUNET_OK if the proof is valid for the given key.

Definition at line 120 of file regex_block_lib.c.

References GNUNET_CRYPTO_hash(), GNUNET_CRYPTO_hash_cmp(), GNUNET_ERROR_TYPE_ERROR, GNUNET_log, GNUNET_NO, and GNUNET_OK.

123 {
124  struct GNUNET_HashCode key_check;
125 
126  if ((NULL == proof) || (NULL == key))
127  {
128  GNUNET_log(GNUNET_ERROR_TYPE_ERROR, "Proof check failed, was NULL.\n");
129  return GNUNET_NO;
130  }
131  GNUNET_CRYPTO_hash(proof, proof_len, &key_check);
132  return (0 ==
133  GNUNET_CRYPTO_hash_cmp(key, &key_check)) ? GNUNET_OK : GNUNET_NO;
134 }
#define GNUNET_NO
Definition: gnunet_common.h:78
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static uint64_t proof
Definition: gnunet-scrypt.c:41
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
A 512-bit hashcode.
int GNUNET_CRYPTO_hash_cmp(const struct GNUNET_HashCode *h1, const struct GNUNET_HashCode *h2)
Compare function for HashCodes, producing a total ordering of all hashcodes.
Definition: crypto_hash.c:278
#define GNUNET_log(kind,...)
Here is the call graph for this function:

◆ REGEX_BLOCK_check()

int REGEX_BLOCK_check ( const struct RegexBlock block,
size_t  size,
const struct GNUNET_HashCode query,
const char *  xquery 
)

Check if the regex block is well formed, including all edges.

Parameters
blockThe start of the block.
sizeThe size of the block.
querythe query for the block
xqueryString describing the edge we are looking for. Can be NULL in case this is a put block.
Returns
GNUNET_OK in case it's fine. GNUNET_NO in case the xquery exists and is not found (IRRELEVANT). GNUNET_SYSERR if the block is invalid.
Parameters
blockThe start of the block.
sizeThe size of the block.
querythe query for the block
xqueryString describing the edge we are looking for. Can be NULL in case this is a put block.
Returns
GNUNET_OK in case it's fine. GNUNET_NO in case the xquery exists and is not found (IRRELEVANT). GNUNET_SYSERR if the block is invalid.

Definition at line 200 of file regex_block_lib.c.

References check_edge(), CheckEdgeContext::found, GNUNET_break_op, GNUNET_ERROR_TYPE_DEBUG, GNUNET_memcmp, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, GNUNET_YES, RegexBlock::is_accepting, LOG, REGEX_BLOCK_get_key(), REGEX_BLOCK_iterate(), res, and CheckEdgeContext::xquery.

Referenced by evaluate_block_regex().

204 {
205  struct GNUNET_HashCode key;
206  struct CheckEdgeContext ctx;
207  int res;
208 
210  "Block check\n");
211  if (GNUNET_OK !=
212  REGEX_BLOCK_get_key(block, size,
213  &key))
214  {
215  GNUNET_break_op(0);
216  return GNUNET_SYSERR;
217  }
218  if (NULL != query &&
219  0 != GNUNET_memcmp(&key,
220  query))
221  {
222  GNUNET_break_op(0);
223  return GNUNET_SYSERR;
224  }
225  if ((GNUNET_YES == ntohs(block->is_accepting)) &&
226  ((NULL == xquery) || ('\0' == xquery[0])))
227  {
229  " out! Is accepting: %u, xquery %p\n",
230  ntohs(block->is_accepting),
231  xquery);
232  return GNUNET_OK;
233  }
234  ctx.xquery = xquery;
235  ctx.found = GNUNET_NO;
236  res = REGEX_BLOCK_iterate(block, size, &check_edge, &ctx);
237  if (GNUNET_SYSERR == res)
238  return GNUNET_SYSERR;
239  if (NULL == xquery)
240  return GNUNET_YES;
241  LOG(GNUNET_ERROR_TYPE_DEBUG, "Result %d\n", ctx.found);
242  return ctx.found;
243 }
Struct to keep track of the xquery while iterating all the edges in a block.
int REGEX_BLOCK_get_key(const struct RegexBlock *block, size_t block_len, struct GNUNET_HashCode *key)
Obtain the key that a particular block is to be stored under.
int REGEX_BLOCK_iterate(const struct RegexBlock *block, size_t size, REGEX_INTERNAL_EgdeIterator iterator, void *iter_cls)
Iterate over all edges of a block of a regex state.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define LOG(kind,...)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static struct GNUNET_DNSSTUB_Context * ctx
Context for DNS resolution.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
A 512-bit hashcode.
static int res
struct GNUNET_HashCode key
The key used in the DHT.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:66
const char * xquery
Xquery: string we are looking for.
#define GNUNET_memcmp(a, b)
Compare memory in a and b, where both must be of the same pointer type.
static int check_edge(void *cls, const char *token, size_t len, const struct GNUNET_HashCode *key)
Iterator over all edges in a block, checking for a presence of a given query.
#define GNUNET_YES
Definition: gnunet_common.h:77
int16_t is_accepting
Is this state an accepting state?
Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGEX_BLOCK_iterate()

int REGEX_BLOCK_iterate ( const struct RegexBlock block,
size_t  size,
REGEX_INTERNAL_EgdeIterator  iterator,
void *  iter_cls 
)

Iterate over all edges of a block of a regex state.

Parameters
blockBlock to iterate over.
sizeSize of block.
iteratorFunction to call on each edge in the block.
iter_clsClosure for the iterator.
Returns
GNUNET_SYSERR if an error has been encountered. GNUNET_OK if no error has been encountered. Note that if the iterator stops the iteration by returning GNUNET_NO, the block will no longer be checked for further errors. The return value will be GNUNET_OK meaning that no errors were found until the edge last notified to the iterator, but there might be errors in further edges.
Parameters
blockBlock to iterate over.
sizeSize of block.
iteratorFunction to call on each edge in the block.
iter_clsClosure for the iterator.
Returns
GNUNET_SYSERR if an error has been encountered. GNUNET_OK if no error has been encountered. Note that if the iterator stops the iteration by returning GNUNET_NO, the block will no longer be checked for further errors. The return value will be GNUNET_OK meaning that no errors were found until the edge last notified to the iterator, but there might be errors in further edges.

Definition at line 303 of file regex_block_lib.c.

References EdgeInfo::destination_index, GNUNET_break_op, GNUNET_ERROR_TYPE_DEBUG, GNUNET_NO, GNUNET_OK, GNUNET_SYSERR, iterator(), len, LOG, RegexBlock::num_destinations, RegexBlock::num_edges, RegexBlock::proof_len, and EdgeInfo::token_length.

Referenced by REGEX_BLOCK_check(), and regex_next_edge().

307 {
308  uint16_t len;
309  const struct GNUNET_HashCode *destinations;
310  const struct EdgeInfo *edges;
311  const char *aux;
312  uint16_t num_destinations;
313  uint16_t num_edges;
314  size_t total;
315  unsigned int n;
316  size_t off;
317 
318  LOG(GNUNET_ERROR_TYPE_DEBUG, "Block iterate\n");
319  if (size < sizeof(struct RegexBlock))
320  {
321  GNUNET_break_op(0);
322  return GNUNET_SYSERR;
323  }
324  num_destinations = ntohs(block->num_destinations);
325  num_edges = ntohs(block->num_edges);
326  len = ntohs(block->proof_len);
327  destinations = (const struct GNUNET_HashCode *)&block[1];
328  edges = (const struct EdgeInfo *)&destinations[num_destinations];
329  aux = (const char *)&edges[num_edges];
330  total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct GNUNET_HashCode) + num_edges * sizeof(struct EdgeInfo) + len;
331  if (size < total)
332  {
333  GNUNET_break_op(0);
334  return GNUNET_SYSERR;
335  }
336  for (n = 0; n < num_edges; n++)
337  total += ntohs(edges[n].token_length);
338  if (size != total)
339  {
340  fprintf(stderr, "Expected %u, got %u\n",
341  (unsigned int)size,
342  (unsigned int)total);
343  GNUNET_break_op(0);
344  return GNUNET_SYSERR;
345  }
346  off = len;
348  "Start iterating block of size %u, proof %u, off %u edges %u\n",
349  size, len, off, n);
350  /* &aux[off] always points to our token */
351  for (n = 0; n < num_edges; n++)
352  {
354  "Edge %u/%u, off %u tokenlen %u (%.*s)\n",
355  n + 1, num_edges, off,
356  ntohs(edges[n].token_length), ntohs(edges[n].token_length),
357  &aux[off]);
358  if (NULL != iterator)
359  if (GNUNET_NO == iterator(iter_cls,
360  &aux[off],
361  ntohs(edges[n].token_length),
362  &destinations[ntohs(edges[n].destination_index)]))
363  return GNUNET_OK;
364  off += ntohs(edges[n].token_length);
365  }
366  return GNUNET_OK;
367 }
static int iterator(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
Iterator over hash map entries.
uint16_t destination_index
Index of the destination of this edge in the unique destinations array.
uint16_t num_edges
Number of edges parting from this state.
Block to announce a regex state.
#define GNUNET_NO
Definition: gnunet_common.h:78
#define LOG(kind,...)
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t num_destinations
Nubmer of unique destinations reachable from this state.
uint16_t proof_len
Length of the proof regex string.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
A 512-bit hashcode.
uint16_t token_length
Number of bytes the token for this edge takes in the token area.
Information for each edge.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:66
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ REGEX_BLOCK_get_key()

int REGEX_BLOCK_get_key ( const struct RegexBlock block,
size_t  block_len,
struct GNUNET_HashCode key 
)

Obtain the key that a particular block is to be stored under.

Parameters
blockblock to get the key from
block_lennumber of bytes in block
keywhere to store the key
Returns
GNUNET_OK on success, GNUNET_SYSERR if the block is malformed
Parameters
blockblock to get the key from
block_lennumber of bytes in block
keywhere to store the key
Returns
GNUNET_OK on success, GNUNET_SYSERR if the block is malformed

Definition at line 255 of file regex_block_lib.c.

References GNUNET_break_op, GNUNET_CRYPTO_hash(), GNUNET_OK, GNUNET_SYSERR, len, RegexBlock::num_destinations, RegexBlock::num_edges, and RegexBlock::proof_len.

Referenced by block_plugin_regex_get_key(), and REGEX_BLOCK_check().

258 {
259  uint16_t len;
260  const struct GNUNET_HashCode *destinations;
261  const struct EdgeInfo *edges;
262  uint16_t num_destinations;
263  uint16_t num_edges;
264  size_t total;
265 
266  if (block_len < sizeof(struct RegexBlock))
267  {
268  GNUNET_break_op(0);
269  return GNUNET_SYSERR;
270  }
271  num_destinations = ntohs(block->num_destinations);
272  num_edges = ntohs(block->num_edges);
273  len = ntohs(block->proof_len);
274  destinations = (const struct GNUNET_HashCode *)&block[1];
275  edges = (const struct EdgeInfo *)&destinations[num_destinations];
276  total = sizeof(struct RegexBlock) + num_destinations * sizeof(struct GNUNET_HashCode) + num_edges * sizeof(struct EdgeInfo) + len;
277  if (block_len < total)
278  {
279  GNUNET_break_op(0);
280  return GNUNET_SYSERR;
281  }
282  GNUNET_CRYPTO_hash(&edges[num_edges], len, key);
283  return GNUNET_OK;
284 }
uint16_t num_edges
Number of edges parting from this state.
Block to announce a regex state.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
uint16_t num_destinations
Nubmer of unique destinations reachable from this state.
uint16_t proof_len
Length of the proof regex string.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
Definition: crypto_hash.c:44
A 512-bit hashcode.
Information for each edge.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GNUNET_BLOCK_is_accepting()

int GNUNET_BLOCK_is_accepting ( const struct RegexBlock block,
size_t  size 
)

Test if this block is marked as being an accept state.

Parameters
blockblock to test
sizenumber of bytes in block
Returns
GNUNET_YES if the block is accepting, GNUNET_NO if not

Definition at line 99 of file regex_block_lib.c.

References GNUNET_break_op, GNUNET_SYSERR, and RegexBlock::is_accepting.

Referenced by dht_get_string_handler(), and regex_result_iterator().

101 {
102  if (size < sizeof(struct RegexBlock))
103  {
104  GNUNET_break_op(0);
105  return GNUNET_SYSERR;
106  }
107  return ntohs(block->is_accepting);
108 }
Block to announce a regex state.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#define GNUNET_SYSERR
Definition: gnunet_common.h:76
static unsigned int size
Size of the "table".
Definition: peer.c:66
int16_t is_accepting
Is this state an accepting state?
Here is the caller graph for this function:

◆ REGEX_BLOCK_create()

struct RegexBlock* REGEX_BLOCK_create ( const char *  proof,
unsigned int  num_edges,
const struct REGEX_BLOCK_Edge edges,
int  accepting,
size_t *  rsize 
)

Construct a regex block to be stored in the DHT.

Parameters
proofproof string for the block
num_edgesnumber of edges in the block
edgesthe edges of the block
acceptingis this an accepting state
rsizeset to the size of the returned block (OUT-only)
Returns
the regex block, NULL on error

Definition at line 381 of file regex_block_lib.c.

References REGEX_BLOCK_Edge::destination, EdgeInfo::destination_index, GNUNET_break, GNUNET_CONSTANTS_MAX_BLOCK_SIZE, GNUNET_malloc, GNUNET_memcpy, RegexBlock::is_accepting, len, RegexBlock::num_destinations, RegexBlock::num_edges, RegexBlock::proof_len, and EdgeInfo::token_length.

Referenced by regex_iterator().

386 {
387  struct RegexBlock *block;
388  struct GNUNET_HashCode destinations[1024]; /* 1024 = 64k/64 bytes/key == absolute MAX */
389  uint16_t destination_indices[num_edges];
390  struct GNUNET_HashCode *dests;
391  struct EdgeInfo *edgeinfos;
392  size_t off;
393  size_t len;
394  size_t total;
395  size_t slen;
396  unsigned int unique_destinations;
397  unsigned int j;
398  unsigned int i;
399  char *aux;
400 
401  len = strlen(proof);
402  if (len > UINT16_MAX)
403  {
404  GNUNET_break(0);
405  return NULL;
406  }
407  unique_destinations = 0;
408  total = sizeof(struct RegexBlock) + len;
409  for (i = 0; i < num_edges; i++)
410  {
411  slen = strlen(edges[i].label);
412  if (slen > UINT16_MAX)
413  {
414  GNUNET_break(0);
415  return NULL;
416  }
417  total += slen;
418  for (j = 0; j < unique_destinations; j++)
419  if (0 == memcmp(&destinations[j],
420  &edges[i].destination,
421  sizeof(struct GNUNET_HashCode)))
422  break;
423  if (j >= 1024)
424  {
425  GNUNET_break(0);
426  return NULL;
427  }
428  destination_indices[i] = j;
429  if (j == unique_destinations)
430  destinations[unique_destinations++] = edges[i].destination;
431  }
432  total += num_edges * sizeof(struct EdgeInfo) + unique_destinations * sizeof(struct GNUNET_HashCode);
433  if (total >= GNUNET_CONSTANTS_MAX_BLOCK_SIZE)
434  {
435  GNUNET_break(0);
436  return NULL;
437  }
438  block = GNUNET_malloc(total);
439  block->proof_len = htons(len);
440  block->is_accepting = htons(accepting);
441  block->num_edges = htons(num_edges);
442  block->num_destinations = htons(unique_destinations);
443  dests = (struct GNUNET_HashCode *)&block[1];
444  GNUNET_memcpy(dests, destinations, sizeof(struct GNUNET_HashCode) * unique_destinations);
445  edgeinfos = (struct EdgeInfo *)&dests[unique_destinations];
446  aux = (char *)&edgeinfos[num_edges];
447  off = len;
448  GNUNET_memcpy(aux, proof, len);
449  for (i = 0; i < num_edges; i++)
450  {
451  slen = strlen(edges[i].label);
452  edgeinfos[i].token_length = htons((uint16_t)slen);
453  edgeinfos[i].destination_index = htons(destination_indices[i]);
454  GNUNET_memcpy(&aux[off],
455  edges[i].label,
456  slen);
457  off += slen;
458  }
459  *rsize = total;
460  return block;
461 }
struct GNUNET_HashCode destination
Destionation of the edge.
uint16_t destination_index
Index of the destination of this edge in the unique destinations array.
uint16_t num_edges
Number of edges parting from this state.
Block to announce a regex state.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
uint16_t num_destinations
Nubmer of unique destinations reachable from this state.
uint16_t proof_len
Length of the proof regex string.
static uint64_t proof
Definition: gnunet-scrypt.c:41
A 512-bit hashcode.
uint16_t token_length
Number of bytes the token for this edge takes in the token area.
Information for each edge.
#define GNUNET_CONSTANTS_MAX_BLOCK_SIZE
Largest block that can be stored in the DHT.
int16_t is_accepting
Is this state an accepting state?
#define GNUNET_malloc(size)
Wrapper around malloc.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
Here is the caller graph for this function: