Debian Codegraph @

res/cdrel_custom/cdrel.h

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2026, Sangoma Technologies Corporation
5 *
6 * George Joseph <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 * \author George Joseph <[email protected]>
22 *
23 * \brief Private header for res_cdrel_custom.
24 *
25 */
26
27 #ifndef _CDREL_H
28 #define _CDREL_H
29
30 #include <sqlite3.h>
31
32 #include "asterisk.h"
33 #include "asterisk/cdr.h"
34 #include "asterisk/cel.h"
35 #include "asterisk/event.h"
36 #include "asterisk/lock.h"
37 #include "asterisk/strings.h"
38 #include "asterisk/vector.h"
39 #include "asterisk/res_cdrel_custom.h"
40
41 extern const char *cdrel_record_type_map[];
42 #define RECORD_TYPE_STR(_rt) (cdrel_record_type_map[_rt])
43
44 extern const char *cdrel_module_type_map[];
45 #define MODULE_TYPE_STR(_mt) (cdrel_module_type_map[_mt])
46
47 enum cdrel_text_format_type {
48 cdrel_format_dsv = 0,
49 cdrel_format_json,
50 cdrel_format_sql,
51 cdrel_format_type_end,
52 };
53
54 enum cdrel_config_type {
55 cdrel_config_legacy = 0,
56 cdrel_config_advanced,
57 cdrel_config_type_end,
58 };
59
60 enum cdrel_quoting_method {
61 cdrel_quoting_method_none = 0,
62 cdrel_quoting_method_all,
63 cdrel_quoting_method_minimal,
64 cdrel_quoting_method_non_numeric,
65 cdrel_quoting_method_end,
66 };
67
68 /*
69 * ORDER IS IMPORTANT!
70 * The string output data types need to be first.
71 */
72 enum cdrel_data_type {
73 cdrel_type_string = 0,
74 cdrel_type_timeval,
75 cdrel_type_literal,
76 cdrel_type_amaflags,
77 cdrel_type_disposition,
78 cdrel_type_uservar,
79 cdrel_type_event_type,
80 cdrel_type_event_enum,
81 cdrel_type_cel_timefmt,
82 cdrel_data_type_strings_end,
83 cdrel_type_int32,
84 cdrel_type_uint32,
85 cdrel_type_int64,
86 cdrel_type_uint64,
87 cdrel_type_double,
88 cdrel_data_type_end
89 };
90
91 extern const char *cdrel_data_type_map[];
92 #define DATA_TYPE_STR(_dt) (_dt < cdrel_data_type_end ? cdrel_data_type_map[_dt] : NULL)
93 enum cdrel_data_type cdrel_data_type_from_str(const char *str);
94
95 #define CDREL_FIELD_FLAG_QUOTE (0)
96 #define CDREL_FIELD_FLAG_NOQUOTE (1)
97 #define CDREL_FIELD_FLAG_TYPE_FORCED (2)
98 #define CDREL_FIELD_FLAG_USERVAR (3)
99 #define CDREL_FIELD_FLAG_LITERAL (4)
100 #define CDREL_FIELD_FLAG_FORMAT_SPEC (5)
101 #define CDREL_FIELD_FLAG_LAST (6)
102
103 enum cdrel_field_flags {
104 cdrel_flag_quote = (1 << CDREL_FIELD_FLAG_QUOTE),
105 cdrel_flag_noquote = (1 << CDREL_FIELD_FLAG_NOQUOTE),
106 cdrel_flag_type_forced = (1 << CDREL_FIELD_FLAG_TYPE_FORCED),
107 cdrel_flag_uservar = (1 << CDREL_FIELD_FLAG_USERVAR),
108 cdrel_flag_literal = (1 << CDREL_FIELD_FLAG_LITERAL),
109 cdrel_flag_format_spec = (1 << CDREL_FIELD_FLAG_FORMAT_SPEC),
110 cdrel_field_flags_end = (1 << CDREL_FIELD_FLAG_LAST),
111 };
112
113 /*
114 * CEL has a few synthetic fields that aren't defined
115 * in event.h so we'll define them ourselves after the
116 * last official id.
117 */
118 #define AST_EVENT_IE_CEL_LITERAL (AST_EVENT_IE_TOTAL + 1)
119 #define AST_EVENT_IE_CEL_EVENT_ENUM (AST_EVENT_IE_TOTAL + 2)
120 #define LAST_CEL_ID AST_EVENT_IE_CEL_EVENT_ENUM
121
122 /*!
123 * While CEL event fields are published as a generic AST_EVENT with
124 * a field id assigned to each field, CDRs are published as a fixed
125 * ast_cdr structure. To make it easier to share lower level code,
126 * we assign pseudo-field-ids to each CDR field that are really the offset
127 * of the field in the structure. This allows us to generically get
128 * any field using its id just like we do for CEL.
129 *
130 * To avoid conflicts with the existing CEL field ids, we'll start these
131 * after the last one.
132 */
133 #define CDR_OFFSET_SHIFT (LAST_CEL_ID + 1)
134 #define CDR_OFFSETOF(_field) (offsetof(struct ast_cdr, _field) + CDR_OFFSET_SHIFT)
135 #define CDR_FIELD(__cdr, __offset) (((void *)__cdr) + __offset - CDR_OFFSET_SHIFT)
136
137 enum cdr_field_id {
138 cdr_field_literal = -1,
139 cdr_field_clid = CDR_OFFSETOF(clid),
140 cdr_field_src = CDR_OFFSETOF(src),
141 cdr_field_dst = CDR_OFFSETOF(dst),
142 cdr_field_dcontext = CDR_OFFSETOF(dcontext),
143 cdr_field_channel = CDR_OFFSETOF(channel),
144 cdr_field_dstchannel = CDR_OFFSETOF(dstchannel),
145 cdr_field_lastapp = CDR_OFFSETOF(lastapp),
146 cdr_field_lastdata = CDR_OFFSETOF(lastdata),
147 cdr_field_start = CDR_OFFSETOF(start),
148 cdr_field_answer = CDR_OFFSETOF(answer),
149 cdr_field_end = CDR_OFFSETOF(end),
150 cdr_field_duration = CDR_OFFSETOF(duration),
151 cdr_field_billsec = CDR_OFFSETOF(billsec),
152 cdr_field_disposition = CDR_OFFSETOF(disposition),
153 cdr_field_amaflags = CDR_OFFSETOF(amaflags),
154 cdr_field_accountcode = CDR_OFFSETOF(accountcode),
155 cdr_field_peeraccount = CDR_OFFSETOF(peeraccount),
156 cdr_field_flags = CDR_OFFSETOF(flags),
157 cdr_field_uniqueid = CDR_OFFSETOF(uniqueid),
158 cdr_field_linkedid = CDR_OFFSETOF(linkedid),
159 cdr_field_tenantid = CDR_OFFSETOF(tenantid),
160 cdr_field_peertenantid = CDR_OFFSETOF(peertenantid),
161 cdr_field_userfield = CDR_OFFSETOF(userfield),
162 cdr_field_sequence = CDR_OFFSETOF(sequence),
163 cdr_field_varshead = CDR_OFFSETOF(varshead),
164 };
165
166
167 struct cdrel_field;
168
169 /*!
170 * \internal
171 * \brief A generic value wrapper structure.
172 */
173 struct cdrel_value {
174 char *field_name;
175 enum cdrel_data_type data_type;
176 int mallocd;
177 union {
178 char *string;
179 int32_t int32;
180 uint32_t uint32;
181 int64_t int64;
182 uint64_t uint64;
183 struct timeval tv;
184 double doubler;
185 } values;
186 };
187
188 /*!
189 * \internal
190 * \brief A vector of cdrel_values.
191 */
192 AST_VECTOR(cdrel_values, struct cdrel_value *);
193
194 /*!
195 * \internal
196 * \brief Getter callbacks.
197 *
198 * \param record An ast_cdr or ast_event structure.
199 * \param config Config object.
200 * \param field Field object.
201 * \param value A pointer to a cdrel_value structure to populate.
202 * \retval 0 on success.
203 * \retval -1 on failure.
204 */
205 typedef int (*cdrel_field_getter)(void *record, struct cdrel_config *config,
206 struct cdrel_field *field, struct cdrel_value *value);
207 /*!
208 * \internal
209 * \brief The table of getter callbacks. Populated by getters_cdr.c and getters_cel.c.
210 *
211 * Defined in res_cdrel_custom.c
212 */
213 extern cdrel_field_getter cdrel_field_getters[cdrel_record_type_end][cdrel_data_type_end];
214
215 /*!
216 * \internal
217 * \brief Data type formatters.
218 *
219 * \param config Config object.
220 * \param field Field object.
221 * \param input_value A pointer to a cdrel_value structure with the data to format.
222 * \param output_value A pointer to a cdrel_value structure to receive the formatted data.
223 * \retval 0 on success.
224 * \retval -1 on failure.
225 */
226 typedef int (*cdrel_field_formatter)(struct cdrel_config *config,
227 struct cdrel_field *field, struct cdrel_value *input_value, struct cdrel_value *output_value);
228 /*!
229 * \internal
230 * \brief The table of formatter callbacks. Populated by formatters.c.
231 *
232 * Defined in res_cdrel_custom.c
233 */
234 extern cdrel_field_formatter cdrel_field_formatters[cdrel_data_type_end];
235
236 /*!
237 * \internal
238 * \brief Backend writers.
239 *
240 * \param config Config object.
241 * \param values A vector of cdrel_values to write.
242 * \retval 0 on success.
243 * \retval -1 on failure.
244 */
245 typedef int (*cdrel_backend_writer)(struct cdrel_config *config, struct cdrel_values *values);
246 /*!
247 * \internal
248 * \brief The table of writer callbacks. Populated by writers.c.
249 *
250 * Defined in res_cdrel_custom.c
251 */
252 extern cdrel_backend_writer cdrel_backend_writers[cdrel_format_type_end];
253
254 /*!
255 * \internal
256 * \brief Dummy channel allocators.
257 *
258 * Legacy configurations use dialplan functions so they need a dummy channel
259 * to operate on. CDR and CEL each have their own allocator.
260 *
261 * \param config Config object.
262 * \param record An ast_cdr or ast_event structure.
263 * \return an ast_channel or NULL on failure.
264 */
265 typedef struct ast_channel * (*cdrel_dummy_channel_alloc)(struct cdrel_config *config, void *record);
266 extern cdrel_dummy_channel_alloc cdrel_dummy_channel_allocators[cdrel_format_type_end];
267
268
269 /*! Represents a field definition */
270 struct cdrel_field {
271 enum cdrel_record_type record_type; /*!< CDR or CEL */
272 int field_id; /*!< May be an AST_EVENT_IE_CEL or a cdr_field_id */
273 char *name; /*!< The name of the field */
274 enum cdrel_data_type input_data_type; /*!< The data type of the field in the source record */
275 enum cdrel_data_type output_data_type;
276 struct ast_flags flags; /*!< Flags used during config parsing */
277 char data[1]; /*!< Could be literal data, a user variable name, etc */
278 };
279
280 /*! Represents an output definition from a config file */
281 struct cdrel_config {
282 enum cdrel_record_type record_type; /*!< CDR or CEL */
283 AST_DECLARE_STRING_FIELDS(
284 AST_STRING_FIELD(config_filename); /*!< Input configuration filename */
285 AST_STRING_FIELD(output_filename); /*!< Output text file or database */
286 AST_STRING_FIELD(template); /*!< Input template */
287 AST_STRING_FIELD(db_columns); /*!< List of columns for database backends */
288 AST_STRING_FIELD(db_table); /*!< Table name for database backends */
289 );
290 sqlite3 *db; /*!< sqlite3 database handle */
291 sqlite3_stmt *insert; /*!< sqlite3 prepared statement for insert */
292 int busy_timeout; /*!< sqlite3 query timeout value */
293 cdrel_dummy_channel_alloc dummy_channel_alloc; /*!< Legacy config types need a dummy channel */
294 enum cdrel_backend_type backend_type; /*!< Text file or database */
295 enum cdrel_config_type config_type; /*!< Legacy or advanced */
296 enum cdrel_text_format_type format_type; /*!< For text files, CSV or JSON */
297 enum cdrel_quoting_method quoting_method; /*!< When to quote */
298 char separator[2]; /*!< For text files, the field separator */
299 char quote[2]; /*!< For text files, the quote character */
300 char quote_escape[2]; /*!< For text files, character to use to escape embedded quotes */
301 AST_VECTOR(, struct cdrel_field *) fields; /*!< Vector of fields for this config */
302 ast_mutex_t lock; /*!< Lock that serializes filesystem writes */
303 };
304
305 /*!
306 * \internal
307 * \brief Get a cdrel_field structure by record type and field name.
308 *
309 * \param record_type The cdrel_record_type to search.
310 * \param name The field name to search for.
311 * \returns A pointer to a constant cdrel_field structure or NULL if not found.
312 * This pointer must never be freed.
313 */
314 const struct cdrel_field *get_registered_field_by_name(enum cdrel_record_type record_type,
315 const char *name);
316
317 /*!
318 * \internal
319 * \brief Write a record to a text file
320 *
321 * \param config The configuration object.
322 * \param record The data to write.
323 * \retval 0 on success
324 * \retval -1 on failure
325 */
326 int write_record_to_file(struct cdrel_config *config, struct ast_str *record);
327
328 /*!
329 * \internal
330 * \brief Write a record to a database
331 *
332 * \param config The configuration object.
333 * \param values The values to write.
334 * \retval 0 on success
335 * \retval -1 on failure
336 */
337 int write_record_to_database(struct cdrel_config *config, struct cdrel_values *values);
338
339 /*!
340 * \internal
341 * \brief Return the basename of a path
342 *
343 * \param path
344 * \returns Pointer to last '/' or path if no '/' was found.
345 */
346 const char *cdrel_basename(const char *path);
347
348 /*!
349 * \internal
350 * \brief Get a string representing a field's flags
351 *
352 * \param flags An ast_flags structure
353 * \param str Pointer to ast_str* buffer
354 * \returns A string of field flag names
355 */
356 const char *cdrel_get_field_flags(struct ast_flags *flags, struct ast_str **str);
357
358
359 int load_cdr(void);
360 int load_cel(void);
361 int load_formatters(void);
362 int load_writers(void);
363
364 #endif /* _CDREL_H */