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