aboutsummaryrefslogtreecommitdiffstats
path: root/src/libutil/map_helpers.h
blob: 4645024e377884138d69e4b0a73a70e056856772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*-
 * Copyright 2018 Vsevolod Stakhov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef RSPAMD_MAP_HELPERS_H
#define RSPAMD_MAP_HELPERS_H

#include "config.h"
#include "map.h"
#include "addr.h"

/**
 * @file map_helpers.h
 *
 * Defines helper structures to deal with different map types
 */

/**
 * Common structures, abstract for simplicity
 */
struct rspamd_radix_map_helper;
struct rspamd_hash_map_helper;
struct rspamd_regexp_map_helper;
struct rspamd_map_helper_value;

enum rspamd_regexp_map_flags {
	RSPAMD_REGEXP_MAP_FLAG_UTF = (1u << 0),
	RSPAMD_REGEXP_MAP_FLAG_MULTIPLE = (1u << 1),
	RSPAMD_REGEXP_MAP_FLAG_GLOB = (1u << 2),
};

typedef void (*insert_func) (gpointer st, gconstpointer key,
		gconstpointer value);

/**
 * Radix list is a list like ip/mask
 */
gchar * rspamd_radix_read (
		gchar *chunk,
		gint len,
		struct map_cb_data *data,
		gboolean final);
void rspamd_radix_fin (struct map_cb_data *data, void **target);
void rspamd_radix_dtor (struct map_cb_data *data);

/**
 * Kv list is an ordinal list of keys and values separated by whitespace
 */
gchar * rspamd_kv_list_read (
		gchar *chunk,
		gint len,
		struct map_cb_data *data,
		gboolean final);
void rspamd_kv_list_fin (struct map_cb_data *data, void **target);
void rspamd_kv_list_dtor (struct map_cb_data *data);

/**
 * Regexp list is a list of regular expressions
 */

gchar * rspamd_regexp_list_read_single (
		gchar *chunk,
		gint len,
		struct map_cb_data *data,
		gboolean final);
gchar * rspamd_regexp_list_read_multiple (
		gchar *chunk,
		gint len,
		struct map_cb_data *data,
		gboolean final);

gchar * rspamd_glob_list_read_single (
		gchar *chunk,
		gint len,
		struct map_cb_data *data,
		gboolean final);
gchar * rspamd_glob_list_read_multiple (
		gchar *chunk,
		gint len,
		struct map_cb_data *data,
		gboolean final);
void rspamd_regexp_list_fin (struct map_cb_data *data, void **target);
void rspamd_regexp_list_dtor (struct map_cb_data *data);

/**
 * FSM for lists parsing (support comments, blank lines and partial replies)
 */
gchar *
rspamd_parse_kv_list (
		gchar * chunk,
		gint len,
		struct map_cb_data *data,
		insert_func func,
		const gchar *default_value,
		gboolean final);

/**
 * Find a single (any) matching regexp for the specified text or NULL if
 * no matches found
 * @param map
 * @param in
 * @param len
 * @return
 */
gconstpointer rspamd_match_regexp_map_single (struct rspamd_regexp_map_helper *map,
		const gchar *in, gsize len);

/**
 * Find a multiple (all) matching regexp for the specified text or NULL if
 * no matches found. Returns GPtrArray that *must* be freed by a caller if not NULL
 * @param map
 * @param in
 * @param len
 * @return
 */
GPtrArray* rspamd_match_regexp_map_all (struct rspamd_regexp_map_helper *map,
		const gchar *in, gsize len);

/**
 * Find value matching specific key in a hash map
 * @param map
 * @param in
 * @param len
 * @return
 */
gconstpointer rspamd_match_hash_map (struct rspamd_hash_map_helper *map,
		const gchar *in);

/**
 * Find value matching specific key in a hash map
 * @param map
 * @param in raw ip address
 * @param inlen ip address length (4 for IPv4 and 16 for IPv6)
 * @return
 */
gconstpointer rspamd_match_radix_map (struct rspamd_radix_map_helper *map,
		const guchar *in, gsize inlen);

gconstpointer rspamd_match_radix_map_addr (struct rspamd_radix_map_helper *map,
		const rspamd_inet_addr_t *addr);

/**
 * Creates radix map helper
 * @param map
 * @return
 */
struct rspamd_radix_map_helper *rspamd_map_helper_new_radix (struct rspamd_map *map);
/**
 * Inserts new value into radix map
 * @param st
 * @param key
 * @param value
 */
void rspamd_map_helper_insert_radix (gpointer st, gconstpointer key, gconstpointer value);
/**
 * Inserts new value into radix map performing synchronous resolving
 * @param st
 * @param key
 * @param value
 */
void rspamd_map_helper_insert_radix_resolve (gpointer st, gconstpointer key,
		gconstpointer value);
/**
 * Destroys radix map helper
 * @param r
 */
void rspamd_map_helper_destroy_radix (struct rspamd_radix_map_helper *r);


/**
 * Creates hash map helper
 * @param map
 * @return
 */
struct rspamd_hash_map_helper *rspamd_map_helper_new_hash (struct rspamd_map *map);
/**
 * Inserts a new value into a hash map
 * @param st
 * @param key
 * @param value
 */
void rspamd_map_helper_insert_hash (gpointer st, gconstpointer key, gconstpointer value);
/**
 * Destroys hash map helper
 * @param r
 */
void rspamd_map_helper_destroy_hash (struct rspamd_hash_map_helper *r);

/**
 * Create new regexp map
 * @param map
 * @param flags
 * @return
 */
struct rspamd_regexp_map_helper * rspamd_map_helper_new_regexp (struct rspamd_map *map,
		enum rspamd_regexp_map_flags flags);
/**
 * Inserts a new regexp into regexp map
 * @param st
 * @param key
 * @param value
 */
void rspamd_map_helper_insert_re (gpointer st, gconstpointer key, gconstpointer value);
/**
 * Destroy regexp map
 * @param re_map
 */
void rspamd_map_helper_destroy_regexp (struct rspamd_regexp_map_helper *re_map);

#endif