| 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 |
#include "cdrel.h" |
| 28 |
#include "asterisk/pbx.h" |
| 29 |
#include "asterisk/vector.h" |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
AST_THREADSTORAGE(custom_buf); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
static void free_value(void *data) |
| 46 |
{ |
| 47 |
struct cdrel_value *val = data; |
| 48 |
if (val->data_type == cdrel_type_string) { |
| 49 |
ast_free(val->values.string); |
| 50 |
val->values.string = NULL; |
| 51 |
} |
| 52 |
ast_free(val); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
static void free_value_vector(void *data) |
| 62 |
{ |
| 63 |
struct cdrel_values *values = data; |
| 64 |
AST_VECTOR_RESET(values, free_value); |
| 65 |
AST_VECTOR_PTR_FREE(values); |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
static int log_legacy_dsv_record(struct cdrel_config *config, void *data) |
| 83 |
{ |
| 84 |
struct ast_channel *dummy = data; |
| 85 |
struct ast_str *str; |
| 86 |
|
| 87 |
if (!(str = ast_str_thread_get(&custom_buf, 1024))) { |
| 88 |
return -1; |
| 89 |
} |
| 90 |
ast_str_reset(str); |
| 91 |
|
| 92 |
ast_str_substitute_variables(&str, 0, dummy, config->template); |
| 93 |
|
| 94 |
return write_record_to_file(config, str); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
static int log_legacy_database_record(struct cdrel_config *config, void *data) |
| 112 |
{ |
| 113 |
struct ast_channel *dummy = data; |
| 114 |
int ix = 0; |
| 115 |
int res = 0; |
| 116 |
char subst_buf[2048]; |
| 117 |
size_t field_count = AST_VECTOR_SIZE(&config->fields); |
| 118 |
RAII_VAR(struct cdrel_values *, values, ast_calloc(1, sizeof(*values)), free_value_vector); |
| 119 |
|
| 120 |
if (!values) { |
| 121 |
return -1; |
| 122 |
} |
| 123 |
|
| 124 |
res = AST_VECTOR_INIT(values, field_count); |
| 125 |
if (res != 0) { |
| 126 |
return -1; |
| 127 |
} |
| 128 |
|
| 129 |
if (config->db == NULL) { |
| 130 |
return -1; |
| 131 |
} |
| 132 |
|
| 133 |
for (ix = 0; ix < AST_VECTOR_SIZE(&config->fields); ix++) { |
| 134 |
struct cdrel_field *field = AST_VECTOR_GET(&config->fields, ix); |
| 135 |
struct cdrel_value *output_value = ast_calloc(1, sizeof(*output_value)); |
| 136 |
|
| 137 |
if (!output_value) { |
| 138 |
return -1; |
| 139 |
} |
| 140 |
output_value->mallocd = 1; |
| 141 |
|
| 142 |
pbx_substitute_variables_helper(dummy, field->data, subst_buf, sizeof(subst_buf) - 1); |
| 143 |
output_value->data_type = cdrel_type_string; |
| 144 |
|
| 145 |
output_value->field_name = field->name; |
| 146 |
output_value->values.string = ast_strdup(ast_strip_quoted(subst_buf, "'\"", "'\"")); |
| 147 |
if (!output_value->values.string) { |
| 148 |
return -1; |
| 149 |
} |
| 150 |
|
| 151 |
res = AST_VECTOR_APPEND(values, output_value); |
| 152 |
if (res != 0) { |
| 153 |
ast_free(output_value); |
| 154 |
return -1; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
return write_record_to_database(config, values); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
static int log_advanced_record(struct cdrel_config *config, void *data) |
| 175 |
{ |
| 176 |
int ix = 0; |
| 177 |
int res = 0; |
| 178 |
size_t field_count = AST_VECTOR_SIZE(&config->fields); |
| 179 |
RAII_VAR(struct cdrel_values *, values, ast_calloc(1, sizeof(*values)), free_value_vector); |
| 180 |
|
| 181 |
if (!values) { |
| 182 |
return -1; |
| 183 |
} |
| 184 |
|
| 185 |
res = AST_VECTOR_INIT(values, field_count); |
| 186 |
if (res != 0) { |
| 187 |
return -1; |
| 188 |
} |
| 189 |
|
| 190 |
for (ix = 0; ix < AST_VECTOR_SIZE(&config->fields); ix++) { |
| 191 |
struct cdrel_field *field = AST_VECTOR_GET(&config->fields, ix); |
| 192 |
struct cdrel_value input_value = { 0, }; |
| 193 |
struct cdrel_value *output_value = ast_calloc(1, sizeof(*output_value)); |
| 194 |
|
| 195 |
if (!output_value) { |
| 196 |
return -1; |
| 197 |
} |
| 198 |
output_value->mallocd = 1; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
res = cdrel_field_getters[config->record_type][field->input_data_type](data, config, field, &input_value); |
| 204 |
if (res != 0) { |
| 205 |
ast_free(output_value); |
| 206 |
return -1; |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
output_value->data_type = field->output_data_type; |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
res = cdrel_field_formatters[input_value.data_type](config, field, &input_value, output_value); |
| 218 |
if (res != 0) { |
| 219 |
ast_free(output_value); |
| 220 |
return -1; |
| 221 |
} |
| 222 |
|
| 223 |
res = AST_VECTOR_APPEND(values, output_value); |
| 224 |
if (res != 0) { |
| 225 |
ast_free(output_value); |
| 226 |
return -1; |
| 227 |
} |
| 228 |
} |
| 229 |
return cdrel_backend_writers[config->format_type](config, values); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
typedef int (*cdrel_logger_cb)(struct cdrel_config *config, void *data); |
| 237 |
|
| 238 |
static const cdrel_logger_cb logger_callbacks[cdrel_backend_type_end][cdrel_config_type_end] = { |
| 239 |
[cdrel_backend_text] = { |
| 240 |
[cdrel_config_legacy] = log_legacy_dsv_record, |
| 241 |
[cdrel_config_advanced] = log_advanced_record |
| 242 |
}, |
| 243 |
[cdrel_backend_db] = { |
| 244 |
[cdrel_config_legacy] = log_legacy_database_record, |
| 245 |
[cdrel_config_advanced] = log_advanced_record |
| 246 |
}, |
| 247 |
}; |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
int cdrel_logger(struct cdrel_configs *configs, void *data) |
| 271 |
{ |
| 272 |
struct ast_channel *dummy = NULL; |
| 273 |
int ix = 0; |
| 274 |
int skip_legacy = 0; |
| 275 |
int res = 0; |
| 276 |
|
| 277 |
for(ix = 0; ix < AST_VECTOR_SIZE(configs); ix++) { |
| 278 |
struct cdrel_config *config = AST_VECTOR_GET(configs, ix); |
| 279 |
void *chan_or_data = NULL; |
| 280 |
|
| 281 |
if (config->config_type == cdrel_config_legacy) { |
| 282 |
if (skip_legacy) { |
| 283 |
continue; |
| 284 |
} |
| 285 |
if (!dummy) { |
| 286 |
dummy = config->dummy_channel_alloc(config, data); |
| 287 |
if (!dummy) { |
| 288 |
ast_log(LOG_ERROR, "Unable to fabricate channel from CEL event for '%s'\n", |
| 289 |
config->output_filename); |
| 290 |
skip_legacy = 1; |
| 291 |
res--; |
| 292 |
continue; |
| 293 |
} |
| 294 |
} |
| 295 |
chan_or_data = dummy; |
| 296 |
} else { |
| 297 |
chan_or_data = data; |
| 298 |
} |
| 299 |
res += logger_callbacks[config->backend_type][config->config_type](config, chan_or_data); |
| 300 |
} |
| 301 |
|
| 302 |
if (dummy) { |
| 303 |
ast_channel_unref(dummy); |
| 304 |
} |
| 305 |
return res; |
| 306 |
} |
| 307 |
|