-renaming symbols of the block library to use the REGEX_BLOCK_ prefix and not the...
[oweals/gnunet.git] / src / regex / regex_internal_lib.h
1 /*
2      This file is part of GNUnet
3      (C) 2012, 2013 Christian Grothoff (and other contributing authors)
4
5      GNUnet is free software; you can redistribute it and/or modify
6      it under the terms of the GNU General Public License as published
7      by the Free Software Foundation; either version 3, or (at your
8      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      General Public License for more details.
14
15      You should have received a copy of the GNU General Public License
16      along with GNUnet; see the file COPYING.  If not, write to the
17      Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18      Boston, MA 02111-1307, USA.
19 */
20 /**
21  * @file regex/regex_internal_lib.h
22  * @brief library to parse regular expressions into dfa
23  * @author Maximilian Szengel
24  */
25
26 #ifndef REGEX_INTERNAL_LIB_H
27 #define REGEX_INTERNAL_LIB_H
28
29 #include "gnunet_util_lib.h"
30 #include "gnunet_dht_service.h"
31 #include "gnunet_statistics_service.h"
32 #include "regex_block_lib.h"
33
34 #ifdef __cplusplus
35 extern "C"
36 {
37 #if 0                           /* keep Emacsens' auto-indent happy */
38 }
39 #endif
40 #endif
41
42
43 /**
44  * Automaton (NFA/DFA) representation.
45  */
46 struct REGEX_INTERNAL_Automaton;
47
48
49 /**
50  * Construct DFA for the given 'regex' of length 'len'.
51  *
52  * Path compression means, that for example a DFA o -> a -> b -> c -> o will be
53  * compressed to o -> abc -> o. Note that this parameter influences the
54  * non-determinism of states of the resulting NFA in the DHT (number of outgoing
55  * edges with the same label). For example for an application that stores IPv4
56  * addresses as bitstrings it could make sense to limit the path compression to
57  * 4 or 8.
58  *
59  * @param regex regular expression string.
60  * @param len length of the regular expression.
61  * @param max_path_len limit the path compression length to the
62  *        given value. If set to 1, no path compression is applied. Set to 0 for
63  *        maximal possible path compression (generally not desireable).
64  * @return DFA, needs to be freed using REGEX_INTERNAL_automaton_destroy.
65  */
66 struct REGEX_INTERNAL_Automaton *
67 REGEX_INTERNAL_construct_dfa (const char *regex, const size_t len,
68                             unsigned int max_path_len);
69
70
71 /**
72  * Free the memory allocated by constructing the REGEX_INTERNAL_Automaton.
73  * data structure.
74  *
75  * @param a automaton to be destroyed.
76  */
77 void
78 REGEX_INTERNAL_automaton_destroy (struct REGEX_INTERNAL_Automaton *a);
79
80
81 /**
82  * Evaluates the given 'string' against the given compiled regex.
83  *
84  * @param a automaton.
85  * @param string string to check.
86  *
87  * @return 0 if string matches, non 0 otherwise.
88  */
89 int
90 REGEX_INTERNAL_eval (struct REGEX_INTERNAL_Automaton *a,
91                    const char *string);
92
93
94 /**
95  * Get the first key for the given 'input_string'. This hashes
96  * the first x bits of the 'input_string'.
97  *
98  * @param input_string string.
99  * @param string_len length of the 'input_string'.
100  * @param key pointer to where to write the hash code.
101  *
102  * @return number of bits of 'input_string' that have been consumed
103  *         to construct the key
104  */
105 size_t
106 REGEX_INTERNAL_get_first_key (const char *input_string, size_t string_len,
107                             struct GNUNET_HashCode * key);
108
109
110 /**
111  * Check if the given 'proof' matches the given 'key'.
112  *
113  * @param proof partial regex of a state.
114  * @param key hash of a state.
115  *
116  * @return GNUNET_OK if the proof is valid for the given key.
117  */
118 int
119 REGEX_INTERNAL_check_proof (const char *proof,
120                           const struct GNUNET_HashCode *key);
121
122
123 /**
124  * Iterator callback function.
125  *
126  * @param cls closure.
127  * @param key hash for current state.
128  * @param proof proof for current state.
129  * @param accepting GNUNET_YES if this is an accepting state, GNUNET_NO if not.
130  * @param num_edges number of edges leaving current state.
131  * @param edges edges leaving current state.
132  */
133 typedef void (*REGEX_INTERNAL_KeyIterator)(void *cls,
134                                          const struct GNUNET_HashCode *key,
135                                          const char *proof,
136                                          int accepting,
137                                          unsigned int num_edges,
138                                          const struct REGEX_BLOCK_Edge *edges);
139
140
141 /**
142  * Iterate over all edges starting from start state of automaton 'a'. Calling
143  * iterator for each edge.
144  *
145  * @param a automaton.
146  * @param iterator iterator called for each edge.
147  * @param iterator_cls closure.
148  */
149 void
150 REGEX_INTERNAL_iterate_all_edges (struct REGEX_INTERNAL_Automaton *a,
151                                 REGEX_INTERNAL_KeyIterator iterator,
152                                 void *iterator_cls);
153
154
155
156 /**
157  * Handle to store cached data about a regex announce.
158  */
159 struct REGEX_INTERNAL_Announcement;
160
161 /**
162  * Handle to store data about a regex search.
163  */
164 struct REGEX_INTERNAL_Search;
165
166 /**
167  * Announce a regular expression: put all states of the automaton in the DHT.
168  * Does not free resources, must call REGEX_INTERNAL_announce_cancel for that.
169  * 
170  * @param dht An existing and valid DHT service handle. CANNOT be NULL.
171  * @param id ID to announce as provider of regex. Own ID in most cases.
172  * @param regex Regular expression to announce.
173  * @param compression How many characters per edge can we squeeze?
174  * @param stats Optional statistics handle to report usage. Can be NULL.
175  * 
176  * @return Handle to reuse o free cached resources.
177  *         Must be freed by calling REGEX_INTERNAL_announce_cancel.
178  */
179 struct REGEX_INTERNAL_Announcement *
180 REGEX_INTERNAL_announce (struct GNUNET_DHT_Handle *dht,
181                        const struct GNUNET_PeerIdentity *id,
182                        const char *regex,
183                        uint16_t compression,
184                        struct GNUNET_STATISTICS_Handle *stats);
185
186 /**
187  * Announce again a regular expression previously announced.
188  * Does use caching to speed up process.
189  * 
190  * @param h Handle returned by a previous REGEX_INTERNAL_announce call.
191  */
192 void
193 REGEX_INTERNAL_reannounce (struct REGEX_INTERNAL_Announcement *h);
194
195
196 /**
197  * Clear all cached data used by a regex announce.
198  * Does not close DHT connection.
199  * 
200  * @param h Handle returned by a previous REGEX_INTERNAL_announce call.
201  */
202 void
203 REGEX_INTERNAL_announce_cancel (struct REGEX_INTERNAL_Announcement *h);
204
205
206 /**
207  * Search callback function.
208  *
209  * @param cls Closure provided in REGEX_INTERNAL_search.
210  * @param id Peer providing a regex that matches the string.
211  * @param get_path Path of the get request.
212  * @param get_path_length Lenght of get_path.
213  * @param put_path Path of the put request.
214  * @param put_path_length Length of the put_path.
215  */
216 typedef void (*REGEX_INTERNAL_Found)(void *cls,
217                                    const struct GNUNET_PeerIdentity *id,
218                                    const struct GNUNET_PeerIdentity *get_path,
219                                    unsigned int get_path_length,
220                                    const struct GNUNET_PeerIdentity *put_path,
221                                    unsigned int put_path_length);
222
223
224 /**
225  * Search for a peer offering a regex matching certain string in the DHT.
226  * The search runs until REGEX_INTERNAL_search_cancel is called, even if results
227  * are returned.
228  *
229  * @param dht An existing and valid DHT service handle.
230  * @param string String to match against the regexes in the DHT.
231  * @param callback Callback for found peers.
232  * @param callback_cls Closure for @c callback.
233  * @param stats Optional statistics handle to report usage. Can be NULL.
234  * 
235  * @return Handle to stop search and free resources.
236  *         Must be freed by calling REGEX_INTERNAL_search_cancel.
237  */
238 struct REGEX_INTERNAL_Search *
239 REGEX_INTERNAL_search (struct GNUNET_DHT_Handle *dht,
240                      const char *string,
241                      REGEX_INTERNAL_Found callback,
242                      void *callback_cls,
243                      struct GNUNET_STATISTICS_Handle *stats);
244
245 /**
246  * Stop search and free all data used by a REGEX_INTERNAL_search call.
247  * Does not close DHT connection.
248  * 
249  * @param h Handle returned by a previous REGEX_INTERNAL_search call.
250  */
251 void
252 REGEX_INTERNAL_search_cancel (struct REGEX_INTERNAL_Search *h);
253
254
255 #if 0                           /* keep Emacsens' auto-indent happy */
256 {
257 #endif
258 #ifdef __cplusplus
259 }
260 #endif
261
262 /* end of regex_internal_lib.h */
263 #endif