Wed Jan 8 2020 09:49:42

Asterisk developer's documentation


cdr_adaptive_odbc.c
Go to the documentation of this file.
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 2007, Tilghman Lesher
5  *
6  * Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
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  * \brief Adaptive ODBC CDR backend
22  *
23  * \author Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
24  * \ingroup cdr_drivers
25  */
26 
27 /*** MODULEINFO
28  <depend>res_odbc</depend>
29  <support_level>core</support_level>
30  ***/
31 
32 #include "asterisk.h"
33 
34 ASTERISK_FILE_VERSION(__FILE__, "$Revision: 413586 $")
35 
36 #include <sys/types.h>
37 #include <time.h>
38 
39 #include <sql.h>
40 #include <sqlext.h>
41 #include <sqltypes.h>
42 
43 #include "asterisk/config.h"
44 #include "asterisk/channel.h"
45 #include "asterisk/lock.h"
46 #include "asterisk/linkedlists.h"
47 #include "asterisk/res_odbc.h"
48 #include "asterisk/cdr.h"
49 #include "asterisk/module.h"
50 
51 #define CONFIG "cdr_adaptive_odbc.conf"
52 
53 static const char name[] = "Adaptive ODBC";
54 /* Optimization to reduce number of memory allocations */
55 static int maxsize = 512, maxsize2 = 512;
56 
57 struct columns {
58  char *name;
59  char *cdrname;
60  char *filtervalue;
61  char *staticvalue;
62  SQLSMALLINT type;
63  SQLINTEGER size;
64  SQLSMALLINT decimals;
65  SQLSMALLINT radix;
66  SQLSMALLINT nullable;
67  SQLINTEGER octetlen;
69 };
70 
71 struct tables {
72  char *connection;
73  char *table;
74  unsigned int usegmtime:1;
76  AST_RWLIST_ENTRY(tables) list;
77 };
78 
80 
81 static int load_config(void)
82 {
83  struct ast_config *cfg;
84  struct ast_variable *var;
85  const char *tmp, *catg;
86  struct tables *tableptr;
87  struct columns *entry;
88  struct odbc_obj *obj;
89  char columnname[80];
90  char connection[40];
91  char table[40];
92  int lenconnection, lentable, usegmtime = 0;
93  SQLLEN sqlptr;
94  int res = 0;
95  SQLHSTMT stmt = NULL;
96  struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
97 
98  cfg = ast_config_load(CONFIG, config_flags);
99  if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
100  ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n");
101  return -1;
102  }
103 
104  for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
105  var = ast_variable_browse(cfg, catg);
106  if (!var)
107  continue;
108 
109  if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
110  ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
111  continue;
112  }
113  ast_copy_string(connection, tmp, sizeof(connection));
114  lenconnection = strlen(connection);
115 
116  if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "usegmtime"))) {
117  usegmtime = ast_true(tmp);
118  }
119 
120  /* When loading, we want to be sure we can connect. */
121  obj = ast_odbc_request_obj(connection, 1);
122  if (!obj) {
123  ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
124  continue;
125  }
126 
127  if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
128  ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n");
129  tmp = "cdr";
130  }
131  ast_copy_string(table, tmp, sizeof(table));
132  lentable = strlen(table);
133 
134  res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
135  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
136  ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
138  continue;
139  }
140 
141  res = SQLColumns(stmt, NULL, 0, NULL, 0, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
142  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
143  ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
145  continue;
146  }
147 
148  tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1);
149  if (!tableptr) {
150  ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'\n", table, connection);
152  res = -1;
153  break;
154  }
155 
156  tableptr->usegmtime = usegmtime;
157  tableptr->connection = (char *)tableptr + sizeof(*tableptr);
158  tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
159  ast_copy_string(tableptr->connection, connection, lenconnection + 1);
160  ast_copy_string(tableptr->table, table, lentable + 1);
161 
162  ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
163 
164  /* Check for filters first */
165  for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
166  if (strncmp(var->name, "filter", 6) == 0) {
167  char *cdrvar = ast_strdupa(var->name + 6);
168  cdrvar = ast_strip(cdrvar);
169  ast_verb(3, "Found filter %s for cdr variable %s in %s@%s\n", var->value, cdrvar, tableptr->table, tableptr->connection);
170 
171  entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(cdrvar) + 1 + strlen(var->value) + 1);
172  if (!entry) {
173  ast_log(LOG_ERROR, "Out of memory creating filter entry for CDR variable '%s' in table '%s' on connection '%s'\n", cdrvar, table, connection);
174  res = -1;
175  break;
176  }
177 
178  /* NULL column entry means this isn't a column in the database */
179  entry->name = NULL;
180  entry->cdrname = (char *)entry + sizeof(*entry);
181  entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(cdrvar) + 1;
182  strcpy(entry->cdrname, cdrvar);
183  strcpy(entry->filtervalue, var->value);
184 
185  AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
186  }
187  }
188 
189  while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
190  char *cdrvar = "", *staticvalue = "";
191 
192  SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
193 
194  /* Is there an alias for this column? */
195 
196  /* NOTE: This seems like a non-optimal parse method, but I'm going
197  * for user configuration readability, rather than fast parsing. We
198  * really don't parse this file all that often, anyway.
199  */
200  for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
201  if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) {
202  char *alias = ast_strdupa(var->name + 5);
203  cdrvar = ast_strip(alias);
204  ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
205  break;
206  } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, columnname) == 0) {
207  char *item = ast_strdupa(var->name + 6);
208  item = ast_strip(item);
209  if (item[0] == '"' && item[strlen(item) - 1] == '"') {
210  /* Remove surrounding quotes */
211  item[strlen(item) - 1] = '\0';
212  item++;
213  }
214  staticvalue = item;
215  }
216  }
217 
218  entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1);
219  if (!entry) {
220  ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
221  res = -1;
222  break;
223  }
224  entry->name = (char *)entry + sizeof(*entry);
225  strcpy(entry->name, columnname);
226 
227  if (!ast_strlen_zero(cdrvar)) {
228  entry->cdrname = entry->name + strlen(columnname) + 1;
229  strcpy(entry->cdrname, cdrvar);
230  } else { /* Point to same place as the column name */
231  entry->cdrname = (char *)entry + sizeof(*entry);
232  }
233 
235  entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1;
236  strcpy(entry->staticvalue, staticvalue);
237  }
238 
239  SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
240  SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
241  SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
242  SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
243  SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
244  SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
245 
246  /* Specification states that the octenlen should be the maximum number of bytes
247  * returned in a char or binary column, but it seems that some drivers just set
248  * it to NULL. (Bad Postgres! No biscuit!) */
249  if (entry->octetlen == 0)
250  entry->octetlen = entry->size;
251 
252  ast_verb(10, "Found %s column with type %hd with len %ld, octetlen %ld, and numlen (%hd,%hd)\n", entry->name, entry->type, (long) entry->size, (long) entry->octetlen, entry->decimals, entry->radix);
253  /* Insert column info into column list */
254  AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
255  res = 0;
256  }
257 
258  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
260 
261  if (AST_LIST_FIRST(&(tableptr->columns)))
262  AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
263  else
264  ast_free(tableptr);
265  }
266  ast_config_destroy(cfg);
267  return res;
268 }
269 
270 static int free_config(void)
271 {
272  struct tables *table;
273  struct columns *entry;
274  while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
275  while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
276  ast_free(entry);
277  }
278  ast_free(table);
279  }
280  return 0;
281 }
282 
283 static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
284 {
285  int res, i;
286  SQLHSTMT stmt;
287  SQLINTEGER nativeerror = 0, numfields = 0;
288  SQLSMALLINT diagbytes = 0;
289  unsigned char state[10], diagnostic[256];
290 
291  res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
292  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
293  ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
294  return NULL;
295  }
296 
297  res = SQLPrepare(stmt, (unsigned char *) data, SQL_NTS);
298  if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
299  ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", (char *) data);
300  SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
301  for (i = 0; i < numfields; i++) {
302  SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
303  ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
304  if (i > 10) {
305  ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
306  break;
307  }
308  }
309  SQLFreeHandle (SQL_HANDLE_STMT, stmt);
310  return NULL;
311  }
312 
313  return stmt;
314 }
315 
316 #define LENGTHEN_BUF1(size) \
317  do { \
318  /* Lengthen buffer, if necessary */ \
319  if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
320  if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 1) / 512 + 1) * 512) != 0) { \
321  ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
322  ast_free(sql); \
323  ast_free(sql2); \
324  AST_RWLIST_UNLOCK(&odbc_tables); \
325  return -1; \
326  } \
327  } \
328  } while (0)
329 
330 #define LENGTHEN_BUF2(size) \
331  do { \
332  if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
333  if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
334  ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
335  ast_free(sql); \
336  ast_free(sql2); \
337  AST_RWLIST_UNLOCK(&odbc_tables); \
338  return -1; \
339  } \
340  } \
341  } while (0)
342 
343 static int odbc_log(struct ast_cdr *cdr)
344 {
345  struct tables *tableptr;
346  struct columns *entry;
347  struct odbc_obj *obj;
348  struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
349  char *tmp;
350  char colbuf[1024], *colptr;
351  SQLHSTMT stmt = NULL;
352  SQLLEN rows = 0;
353 
354  if (!sql || !sql2) {
355  if (sql)
356  ast_free(sql);
357  if (sql2)
358  ast_free(sql2);
359  return -1;
360  }
361 
363  ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n");
364  ast_free(sql);
365  ast_free(sql2);
366  return -1;
367  }
368 
369  AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
370  int first = 1;
371  ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
372  ast_str_set(&sql2, 0, " VALUES (");
373 
374  /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
375  if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
376  ast_log(LOG_WARNING, "cdr_adaptive_odbc: Unable to retrieve database handle for '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
377  continue;
378  }
379 
380  AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
381  int datefield = 0;
382  if (strcasecmp(entry->cdrname, "start") == 0) {
383  datefield = 1;
384  } else if (strcasecmp(entry->cdrname, "answer") == 0) {
385  datefield = 2;
386  } else if (strcasecmp(entry->cdrname, "end") == 0) {
387  datefield = 3;
388  }
389 
390  /* Check if we have a similarly named variable */
391  if (entry->staticvalue) {
392  colptr = ast_strdupa(entry->staticvalue);
393  } else if (datefield && tableptr->usegmtime) {
394  struct timeval date_tv = (datefield == 1) ? cdr->start : (datefield == 2) ? cdr->answer : cdr->end;
395  struct ast_tm tm = { 0, };
396  ast_localtime(&date_tv, &tm, "UTC");
397  ast_strftime(colbuf, sizeof(colbuf), "%Y-%m-%d %H:%M:%S", &tm);
398  colptr = colbuf;
399  } else {
400  ast_cdr_getvar(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), 0, datefield ? 0 : 1);
401  }
402 
403  if (colptr) {
404  /* Check first if the column filters this entry. Note that this
405  * is very specifically NOT ast_strlen_zero(), because the filter
406  * could legitimately specify that the field is blank, which is
407  * different from the field being unspecified (NULL). */
408  if (entry->filtervalue && strcasecmp(colptr, entry->filtervalue) != 0) {
409  ast_verb(4, "CDR column '%s' with value '%s' does not match filter of"
410  " '%s'. Cancelling this CDR.\n",
411  entry->cdrname, colptr, entry->filtervalue);
412  goto early_release;
413  }
414 
415  /* Only a filter? */
416  if (ast_strlen_zero(entry->name))
417  continue;
418 
419  LENGTHEN_BUF1(strlen(entry->name));
420 
421  switch (entry->type) {
422  case SQL_CHAR:
423  case SQL_VARCHAR:
424  case SQL_LONGVARCHAR:
425 #ifdef HAVE_ODBC_WCHAR
426  case SQL_WCHAR:
427  case SQL_WVARCHAR:
428  case SQL_WLONGVARCHAR:
429 #endif
430  case SQL_BINARY:
431  case SQL_VARBINARY:
432  case SQL_LONGVARBINARY:
433  case SQL_GUID:
434  /* For these two field names, get the rendered form, instead of the raw
435  * form (but only when we're dealing with a character-based field).
436  */
437  if (strcasecmp(entry->name, "disposition") == 0) {
438  ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
439  } else if (strcasecmp(entry->name, "amaflags") == 0) {
440  ast_cdr_getvar(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0, 0);
441  }
442 
443  /* Truncate too-long fields */
444  if (entry->type != SQL_GUID) {
445  if (strlen(colptr) > entry->octetlen) {
446  colptr[entry->octetlen] = '\0';
447  }
448  }
449 
450  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
451  LENGTHEN_BUF2(strlen(colptr));
452 
453  /* Encode value, with escaping */
454  ast_str_append(&sql2, 0, "%s'", first ? "" : ",");
455  for (tmp = colptr; *tmp; tmp++) {
456  if (*tmp == '\'') {
457  ast_str_append(&sql2, 0, "''");
458  } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
459  ast_str_append(&sql2, 0, "\\\\");
460  } else {
461  ast_str_append(&sql2, 0, "%c", *tmp);
462  }
463  }
464  ast_str_append(&sql2, 0, "'");
465  break;
466  case SQL_TYPE_DATE:
467  if (ast_strlen_zero(colptr)) {
468  continue;
469  } else {
470  int year = 0, month = 0, day = 0;
471  if (sscanf(colptr, "%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 ||
472  month <= 0 || month > 12 || day < 0 || day > 31 ||
473  ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
474  (month == 2 && year % 400 == 0 && day > 29) ||
475  (month == 2 && year % 100 == 0 && day > 28) ||
476  (month == 2 && year % 4 == 0 && day > 29) ||
477  (month == 2 && year % 4 != 0 && day > 28)) {
478  ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
479  continue;
480  }
481 
482  if (year > 0 && year < 100) {
483  year += 2000;
484  }
485 
486  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
487  LENGTHEN_BUF2(17);
488  ast_str_append(&sql2, 0, "%s{ d '%04d-%02d-%02d' }", first ? "" : ",", year, month, day);
489  }
490  break;
491  case SQL_TYPE_TIME:
492  if (ast_strlen_zero(colptr)) {
493  continue;
494  } else {
495  int hour = 0, minute = 0, second = 0;
496  int count = sscanf(colptr, "%2d:%2d:%2d", &hour, &minute, &second);
497 
498  if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
499  ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
500  continue;
501  }
502 
503  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
504  LENGTHEN_BUF2(15);
505  ast_str_append(&sql2, 0, "%s{ t '%02d:%02d:%02d' }", first ? "" : ",", hour, minute, second);
506  }
507  break;
508  case SQL_TYPE_TIMESTAMP:
509  case SQL_TIMESTAMP:
510  if (ast_strlen_zero(colptr)) {
511  continue;
512  } else {
513  int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
514  int count = sscanf(colptr, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second);
515 
516  if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
517  month <= 0 || month > 12 || day < 0 || day > 31 ||
518  ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
519  (month == 2 && year % 400 == 0 && day > 29) ||
520  (month == 2 && year % 100 == 0 && day > 28) ||
521  (month == 2 && year % 4 == 0 && day > 29) ||
522  (month == 2 && year % 4 != 0 && day > 28) ||
523  hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
524  ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
525  continue;
526  }
527 
528  if (year > 0 && year < 100) {
529  year += 2000;
530  }
531 
532  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
533  LENGTHEN_BUF2(26);
534  ast_str_append(&sql2, 0, "%s{ ts '%04d-%02d-%02d %02d:%02d:%02d' }", first ? "" : ",", year, month, day, hour, minute, second);
535  }
536  break;
537  case SQL_INTEGER:
538  if (ast_strlen_zero(colptr)) {
539  continue;
540  } else {
541  int integer = 0;
542  if (sscanf(colptr, "%30d", &integer) != 1) {
543  ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
544  continue;
545  }
546 
547  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
548  LENGTHEN_BUF2(12);
549  ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
550  }
551  break;
552  case SQL_BIGINT:
553  if (ast_strlen_zero(colptr)) {
554  continue;
555  } else {
556  long long integer = 0;
557  if (sscanf(colptr, "%30lld", &integer) != 1) {
558  ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
559  continue;
560  }
561 
562  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
563  LENGTHEN_BUF2(24);
564  ast_str_append(&sql2, 0, "%s%lld", first ? "" : ",", integer);
565  }
566  break;
567  case SQL_SMALLINT:
568  if (ast_strlen_zero(colptr)) {
569  continue;
570  } else {
571  short integer = 0;
572  if (sscanf(colptr, "%30hd", &integer) != 1) {
573  ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
574  continue;
575  }
576 
577  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
578  LENGTHEN_BUF2(6);
579  ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
580  }
581  break;
582  case SQL_TINYINT:
583  if (ast_strlen_zero(colptr)) {
584  continue;
585  } else {
586  signed char integer = 0;
587  if (sscanf(colptr, "%30hhd", &integer) != 1) {
588  ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
589  continue;
590  }
591 
592  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
593  LENGTHEN_BUF2(4);
594  ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
595  }
596  break;
597  case SQL_BIT:
598  if (ast_strlen_zero(colptr)) {
599  continue;
600  } else {
601  signed char integer = 0;
602  if (sscanf(colptr, "%30hhd", &integer) != 1) {
603  ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
604  continue;
605  }
606  if (integer != 0)
607  integer = 1;
608 
609  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
610  LENGTHEN_BUF2(2);
611  ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
612  }
613  break;
614  case SQL_NUMERIC:
615  case SQL_DECIMAL:
616  if (ast_strlen_zero(colptr)) {
617  continue;
618  } else {
619  double number = 0.0;
620 
621  if (!strcasecmp(entry->cdrname, "billsec")) {
622  if (!ast_tvzero(cdr->answer)) {
623  snprintf(colbuf, sizeof(colbuf), "%lf",
624  (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
625  } else {
626  ast_copy_string(colbuf, "0", sizeof(colbuf));
627  }
628  } else if (!strcasecmp(entry->cdrname, "duration")) {
629  snprintf(colbuf, sizeof(colbuf), "%lf",
630  (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
631 
632  if (!ast_strlen_zero(colbuf)) {
633  colptr = colbuf;
634  }
635  }
636 
637  if (sscanf(colptr, "%30lf", &number) != 1) {
638  ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
639  continue;
640  }
641 
642  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
643  LENGTHEN_BUF2(entry->decimals);
644  ast_str_append(&sql2, 0, "%s%*.*lf", first ? "" : ",", entry->decimals, entry->radix, number);
645  }
646  break;
647  case SQL_FLOAT:
648  case SQL_REAL:
649  case SQL_DOUBLE:
650  if (ast_strlen_zero(colptr)) {
651  continue;
652  } else {
653  double number = 0.0;
654 
655  if (!strcasecmp(entry->cdrname, "billsec")) {
656  if (!ast_tvzero(cdr->answer)) {
657  snprintf(colbuf, sizeof(colbuf), "%lf",
658  (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
659  } else {
660  ast_copy_string(colbuf, "0", sizeof(colbuf));
661  }
662  } else if (!strcasecmp(entry->cdrname, "duration")) {
663  snprintf(colbuf, sizeof(colbuf), "%lf",
664  (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
665 
666  if (!ast_strlen_zero(colbuf)) {
667  colptr = colbuf;
668  }
669  }
670 
671  if (sscanf(colptr, "%30lf", &number) != 1) {
672  ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
673  continue;
674  }
675 
676  ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
677  LENGTHEN_BUF2(entry->decimals);
678  ast_str_append(&sql2, 0, "%s%lf", first ? "" : ",", number);
679  }
680  break;
681  default:
682  ast_log(LOG_WARNING, "Column type %d (field '%s:%s:%s') is unsupported at this time.\n", entry->type, tableptr->connection, tableptr->table, entry->name);
683  continue;
684  }
685  first = 0;
686  } else if (entry->filtervalue && entry->filtervalue[0] != '\0') {
687  ast_verb(4, "CDR column '%s' was not set and does not match filter of"
688  " '%s'. Cancelling this CDR.\n",
689  entry->cdrname, entry->filtervalue);
690  goto early_release;
691  }
692  }
693 
694  /* Concatenate the two constructed buffers */
696  ast_str_append(&sql, 0, ")");
697  ast_str_append(&sql2, 0, ")");
698  ast_str_append(&sql, 0, "%s", ast_str_buffer(sql2));
699 
700  ast_verb(11, "[%s]\n", ast_str_buffer(sql));
701 
703  if (stmt) {
704  SQLRowCount(stmt, &rows);
705  SQLFreeHandle(SQL_HANDLE_STMT, stmt);
706  }
707  if (rows == 0) {
708  ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
709  }
710 early_release:
712  }
714 
715  /* Next time, just allocate buffers that are that big to start with. */
716  if (ast_str_strlen(sql) > maxsize) {
717  maxsize = ast_str_strlen(sql);
718  }
719  if (ast_str_strlen(sql2) > maxsize2) {
720  maxsize2 = ast_str_strlen(sql2);
721  }
722 
723  ast_free(sql);
724  ast_free(sql2);
725  return 0;
726 }
727 
728 static int unload_module(void)
729 {
730  ast_cdr_unregister(name);
733  ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
734  return -1;
735  }
736 
737  free_config();
739  return 0;
740 }
741 
742 static int load_module(void)
743 {
745  ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
746  return 0;
747  }
748 
749  load_config();
752  return 0;
753 }
754 
755 static int reload(void)
756 {
758  ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
759  return -1;
760  }
761 
762  free_config();
763  load_config();
765  return 0;
766 }
767 
768 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Adaptive ODBC CDR backend",
769  .load = load_module,
770  .unload = unload_module,
771  .reload = reload,
772  .load_pri = AST_MODPRI_CDR_DRIVER,
773 );
774 
const char * description
Definition: module.h:234
SQLHDBC con
Definition: res_odbc.h:48
int ast_odbc_backslash_is_escape(struct odbc_obj *obj)
Checks if the database natively supports backslash as an escape character.
Definition: res_odbc.c:1118
SQLSMALLINT radix
Asterisk locking-related definitions:
Asterisk main include file. File version handling, generic pbx functions.
#define AST_LIST_FIRST(head)
Returns the first entry contained in a list.
Definition: linkedlists.h:420
const char * ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
Gets a variable.
Definition: config.c:625
#define AST_RWLIST_HEAD_STATIC(name, type)
Defines a structure to be used to hold a read/write list of specified type, statically initialized...
Definition: linkedlists.h:332
static int load_module(void)
Time-related functions and macros.
#define AST_RWLIST_WRLOCK(head)
Write locks a list.
Definition: linkedlists.h:51
#define LOG_WARNING
Definition: logger.h:144
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category)
Goes through variables.
Definition: config.c:597
char * ast_str_buffer(const struct ast_str *buf)
Returns the string buffer within the ast_str buf.
Definition: strings.h:497
SQLSMALLINT nullable
#define AST_RWLIST_UNLOCK(head)
Attempts to unlock a read/write based list.
Definition: linkedlists.h:150
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1570
Structure for variables, used for configurations and for channel variables.
Definition: config.h:75
#define var
Definition: ast_expr2f.c:606
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
Definition: time.h:100
Configuration File Parser.
static int reload(void)
char * connection
#define LENGTHEN_BUF2(size)
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:900
struct ast_str * ast_str_create(size_t init_len)
Create a malloc&#39;ed dynamic length string.
Definition: strings.h:420
#define AST_MODULE_INFO(keystr, flags_to_set, desc, fields...)
Definition: module.h:374
char * filtervalue
SQLINTEGER octetlen
char * table
#define ast_verb(level,...)
Definition: logger.h:243
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
SQLSMALLINT type
Number structure.
Definition: app_followme.c:109
static char * table
Definition: cdr_odbc.c:50
void ast_cdr_getvar(struct ast_cdr *cdr, const char *name, char **ret, char *workspace, int workspacelen, int recur, int raw)
Definition: cdr.c:264
Call Detail Record API.
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:874
#define AST_RWLIST_RDLOCK(head)
Read locks a list.
Definition: linkedlists.h:77
SQLSMALLINT decimals
const char * value
Definition: config.h:79
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
General Asterisk PBX channel definitions.
static int maxsize2
SQLINTEGER size
ODBC container.
Definition: res_odbc.h:46
char * staticvalue
static int unload_module(void)
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:63
char * ast_strip(char *s)
Strip leading/trailing whitespace from a string.
Definition: strings.h:155
char * ast_category_browse(struct ast_config *config, const char *prev)
Goes through categories.
Definition: config.c:810
A set of macros to manage forward-linked lists.
const char * name
Definition: config.h:77
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:818
ODBC resource manager.
char * cdrname
struct tables::odbc_columns columns
#define AST_LIST_HEAD_NOLOCK(name, type)
Defines a structure to be used to hold a list of specified type (with no lock).
Definition: linkedlists.h:224
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: utils.h:663
struct timeval answer
Definition: cdr.h:102
Responsible for call detail data.
Definition: cdr.h:82
struct columns::@71 list
#define LOG_ERROR
Definition: logger.h:155
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:716
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
static int free_config(void)
The descriptor of a dynamic string XXX storage will be optimized later if needed We use the ts field ...
Definition: strings.h:364
struct sla_ringing_trunk * first
Definition: app_meetme.c:965
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:490
#define AST_LIST_ENTRY(type)
Declare a forward link structure inside a list entry.
Definition: linkedlists.h:409
struct timeval start
Definition: cdr.h:100
static const char name[]
#define ast_free(a)
Definition: astmm.h:97
#define CONFIG
#define ast_odbc_request_obj(a, b)
Definition: res_odbc.h:123
static int maxsize
#define AST_RWLIST_REMOVE_HEAD
Definition: linkedlists.h:829
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2351
static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
Structure used to handle boolean flags.
Definition: utils.h:200
#define AST_RWLIST_ENTRY
Definition: linkedlists.h:414
size_t ast_str_strlen(const struct ast_str *buf)
Returns the current length of the string stored within buf.
Definition: strings.h:471
#define AST_RWLIST_INSERT_TAIL
Definition: linkedlists.h:726
struct timeval end
Definition: cdr.h:104
SQLHSTMT ast_odbc_prepare_and_execute(struct odbc_obj *obj, SQLHSTMT(*prepare_cb)(struct odbc_obj *obj, void *data), void *data)
Prepares, executes, and returns the resulting statement handle.
Definition: res_odbc.c:622
#define ast_calloc(a, b)
Definition: astmm.h:82
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
static int odbc_log(struct ast_cdr *cdr)
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
Definition: time.h:70
#define LENGTHEN_BUF1(size)
struct ast_variable * next
Definition: config.h:82
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
void ast_odbc_release_obj(struct odbc_obj *obj)
Releases an ODBC object previously allocated by ast_odbc_request_obj()
Definition: res_odbc.c:1112
#define ASTERISK_GPL_KEY
The text the key() function should return.
Definition: module.h:38
Asterisk module definitions.
static int usegmtime
Definition: cdr_csv.c:52
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165
static int load_config(void)
#define ASTERISK_FILE_VERSION(file, version)
Register/unregister a source code file with the core.
Definition: asterisk.h:180
unsigned int usegmtime