cdr_adaptive_odbc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819
  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. * \file
  20. * \brief Adaptive ODBC CDR backend
  21. *
  22. * \author Tilghman Lesher <cdr_adaptive_odbc__v1@the-tilghman.com>
  23. * \ingroup cdr_drivers
  24. */
  25. /*! \li \ref cdr_adaptive_odbc.c uses the configuration file \ref cdr_adaptive_odbc.conf
  26. * \addtogroup configuration_file Configuration Files
  27. */
  28. /*!
  29. * \page cdr_adaptive_odbc.conf cdr_adaptive_odbc.conf
  30. * \verbinclude cdr_adaptive_odbc.conf.sample
  31. */
  32. /*** MODULEINFO
  33. <depend>res_odbc</depend>
  34. <support_level>core</support_level>
  35. ***/
  36. #include "asterisk.h"
  37. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  38. #include <sys/types.h>
  39. #include <time.h>
  40. #include <sql.h>
  41. #include <sqlext.h>
  42. #include <sqltypes.h>
  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. #define CONFIG "cdr_adaptive_odbc.conf"
  51. static const char name[] = "Adaptive ODBC";
  52. /* Optimization to reduce number of memory allocations */
  53. static int maxsize = 512, maxsize2 = 512;
  54. struct columns {
  55. char *name;
  56. char *cdrname;
  57. char *filtervalue;
  58. char *staticvalue;
  59. SQLSMALLINT type;
  60. SQLINTEGER size;
  61. SQLSMALLINT decimals;
  62. SQLSMALLINT radix;
  63. SQLSMALLINT nullable;
  64. SQLINTEGER octetlen;
  65. AST_LIST_ENTRY(columns) list;
  66. unsigned int negatefiltervalue:1;
  67. };
  68. struct tables {
  69. char *connection;
  70. char *table;
  71. char *schema;
  72. unsigned int usegmtime:1;
  73. AST_LIST_HEAD_NOLOCK(odbc_columns, columns) columns;
  74. AST_RWLIST_ENTRY(tables) list;
  75. };
  76. static AST_RWLIST_HEAD_STATIC(odbc_tables, tables);
  77. static int load_config(void)
  78. {
  79. struct ast_config *cfg;
  80. struct ast_variable *var;
  81. const char *tmp, *catg;
  82. struct tables *tableptr;
  83. struct columns *entry;
  84. struct odbc_obj *obj;
  85. char columnname[80];
  86. char connection[40];
  87. char table[40];
  88. char schema[40];
  89. int lenconnection, lentable, lenschema, usegmtime = 0;
  90. SQLLEN sqlptr;
  91. int res = 0;
  92. SQLHSTMT stmt = NULL;
  93. struct ast_flags config_flags = { 0 }; /* Part of our config comes from the database */
  94. cfg = ast_config_load(CONFIG, config_flags);
  95. if (!cfg || cfg == CONFIG_STATUS_FILEINVALID) {
  96. ast_log(LOG_WARNING, "Unable to load " CONFIG ". No adaptive ODBC CDRs.\n");
  97. return -1;
  98. }
  99. for (catg = ast_category_browse(cfg, NULL); catg; catg = ast_category_browse(cfg, catg)) {
  100. var = ast_variable_browse(cfg, catg);
  101. if (!var)
  102. continue;
  103. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "connection"))) {
  104. ast_log(LOG_WARNING, "No connection parameter found in '%s'. Skipping.\n", catg);
  105. continue;
  106. }
  107. ast_copy_string(connection, tmp, sizeof(connection));
  108. lenconnection = strlen(connection);
  109. if (!ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "usegmtime"))) {
  110. usegmtime = ast_true(tmp);
  111. }
  112. /* When loading, we want to be sure we can connect. */
  113. obj = ast_odbc_request_obj(connection, 1);
  114. if (!obj) {
  115. ast_log(LOG_WARNING, "No such connection '%s' in the '%s' section of " CONFIG ". Check res_odbc.conf.\n", connection, catg);
  116. continue;
  117. }
  118. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "table"))) {
  119. ast_log(LOG_NOTICE, "No table name found. Assuming 'cdr'.\n");
  120. tmp = "cdr";
  121. }
  122. ast_copy_string(table, tmp, sizeof(table));
  123. lentable = strlen(table);
  124. if (ast_strlen_zero(tmp = ast_variable_retrieve(cfg, catg, "schema"))) {
  125. tmp = "";
  126. }
  127. ast_copy_string(schema, tmp, sizeof(schema));
  128. lenschema = strlen(schema);
  129. res = SQLAllocHandle(SQL_HANDLE_STMT, obj->con, &stmt);
  130. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  131. ast_log(LOG_WARNING, "SQL Alloc Handle failed on connection '%s'!\n", connection);
  132. ast_odbc_release_obj(obj);
  133. continue;
  134. }
  135. res = SQLColumns(stmt, NULL, 0, lenschema == 0 ? NULL : (unsigned char *)schema, SQL_NTS, (unsigned char *)table, SQL_NTS, (unsigned char *)"%", SQL_NTS);
  136. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  137. ast_log(LOG_ERROR, "Unable to query database columns on connection '%s'. Skipping.\n", connection);
  138. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  139. ast_odbc_release_obj(obj);
  140. continue;
  141. }
  142. tableptr = ast_calloc(sizeof(char), sizeof(*tableptr) + lenconnection + 1 + lentable + 1 + lenschema + 1);
  143. if (!tableptr) {
  144. ast_log(LOG_ERROR, "Out of memory creating entry for table '%s' on connection '%s'%s%s%s\n", table, connection,
  145. lenschema ? " (schema '" : "", lenschema ? schema : "", lenschema ? "')" : "");
  146. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  147. ast_odbc_release_obj(obj);
  148. res = -1;
  149. break;
  150. }
  151. tableptr->usegmtime = usegmtime;
  152. tableptr->connection = (char *)tableptr + sizeof(*tableptr);
  153. tableptr->table = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1;
  154. tableptr->schema = (char *)tableptr + sizeof(*tableptr) + lenconnection + 1 + lentable + 1;
  155. ast_copy_string(tableptr->connection, connection, lenconnection + 1);
  156. ast_copy_string(tableptr->table, table, lentable + 1);
  157. ast_copy_string(tableptr->schema, schema, lenschema + 1);
  158. ast_verb(3, "Found adaptive CDR table %s@%s.\n", tableptr->table, tableptr->connection);
  159. /* Check for filters first */
  160. for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
  161. if (strncmp(var->name, "filter", 6) == 0) {
  162. int negate = 0;
  163. char *cdrvar = ast_strdupa(var->name + 6);
  164. cdrvar = ast_strip(cdrvar);
  165. if (cdrvar[strlen(cdrvar) - 1] == '!') {
  166. negate = 1;
  167. cdrvar[strlen(cdrvar) - 1] = '\0';
  168. ast_trim_blanks(cdrvar);
  169. }
  170. ast_verb(3, "Found filter %s'%s' for CDR variable %s in %s@%s\n", negate ? "!" : "", var->value, cdrvar, tableptr->table, tableptr->connection);
  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. /* NULL column entry means this isn't a column in the database */
  178. entry->name = NULL;
  179. entry->cdrname = (char *)entry + sizeof(*entry);
  180. entry->filtervalue = (char *)entry + sizeof(*entry) + strlen(cdrvar) + 1;
  181. strcpy(entry->cdrname, cdrvar);
  182. strcpy(entry->filtervalue, var->value);
  183. entry->negatefiltervalue = negate;
  184. AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
  185. }
  186. }
  187. while ((res = SQLFetch(stmt)) != SQL_NO_DATA && res != SQL_ERROR) {
  188. char *cdrvar = "", *staticvalue = "";
  189. SQLGetData(stmt, 4, SQL_C_CHAR, columnname, sizeof(columnname), &sqlptr);
  190. /* Is there an alias for this column? */
  191. /* NOTE: This seems like a non-optimal parse method, but I'm going
  192. * for user configuration readability, rather than fast parsing. We
  193. * really don't parse this file all that often, anyway.
  194. */
  195. for (var = ast_variable_browse(cfg, catg); var; var = var->next) {
  196. if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, columnname) == 0) {
  197. char *alias = ast_strdupa(var->name + 5);
  198. cdrvar = ast_strip(alias);
  199. ast_verb(3, "Found alias %s for column %s in %s@%s\n", cdrvar, columnname, tableptr->table, tableptr->connection);
  200. break;
  201. } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, columnname) == 0) {
  202. char *item = ast_strdupa(var->name + 6);
  203. item = ast_strip(item);
  204. if (item[0] == '"' && item[strlen(item) - 1] == '"') {
  205. /* Remove surrounding quotes */
  206. item[strlen(item) - 1] = '\0';
  207. item++;
  208. }
  209. staticvalue = item;
  210. }
  211. }
  212. entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(columnname) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1);
  213. if (!entry) {
  214. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s' in table '%s' on connection '%s'\n", columnname, table, connection);
  215. res = -1;
  216. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  217. break;
  218. }
  219. entry->name = (char *)entry + sizeof(*entry);
  220. strcpy(entry->name, columnname);
  221. if (!ast_strlen_zero(cdrvar)) {
  222. entry->cdrname = entry->name + strlen(columnname) + 1;
  223. strcpy(entry->cdrname, cdrvar);
  224. } else { /* Point to same place as the column name */
  225. entry->cdrname = (char *)entry + sizeof(*entry);
  226. }
  227. if (!ast_strlen_zero(staticvalue)) {
  228. entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1;
  229. strcpy(entry->staticvalue, staticvalue);
  230. }
  231. SQLGetData(stmt, 5, SQL_C_SHORT, &entry->type, sizeof(entry->type), NULL);
  232. SQLGetData(stmt, 7, SQL_C_LONG, &entry->size, sizeof(entry->size), NULL);
  233. SQLGetData(stmt, 9, SQL_C_SHORT, &entry->decimals, sizeof(entry->decimals), NULL);
  234. SQLGetData(stmt, 10, SQL_C_SHORT, &entry->radix, sizeof(entry->radix), NULL);
  235. SQLGetData(stmt, 11, SQL_C_SHORT, &entry->nullable, sizeof(entry->nullable), NULL);
  236. SQLGetData(stmt, 16, SQL_C_LONG, &entry->octetlen, sizeof(entry->octetlen), NULL);
  237. /* Specification states that the octenlen should be the maximum number of bytes
  238. * returned in a char or binary column, but it seems that some drivers just set
  239. * it to NULL. (Bad Postgres! No biscuit!) */
  240. if (entry->octetlen == 0)
  241. entry->octetlen = entry->size;
  242. ast_verb(4, "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);
  243. /* Insert column info into column list */
  244. AST_LIST_INSERT_TAIL(&(tableptr->columns), entry, list);
  245. res = 0;
  246. }
  247. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  248. ast_odbc_release_obj(obj);
  249. if (AST_LIST_FIRST(&(tableptr->columns)))
  250. AST_RWLIST_INSERT_TAIL(&odbc_tables, tableptr, list);
  251. else
  252. ast_free(tableptr);
  253. }
  254. ast_config_destroy(cfg);
  255. return res;
  256. }
  257. static int free_config(void)
  258. {
  259. struct tables *table;
  260. struct columns *entry;
  261. while ((table = AST_RWLIST_REMOVE_HEAD(&odbc_tables, list))) {
  262. while ((entry = AST_LIST_REMOVE_HEAD(&(table->columns), list))) {
  263. ast_free(entry);
  264. }
  265. ast_free(table);
  266. }
  267. return 0;
  268. }
  269. static SQLHSTMT generic_prepare(struct odbc_obj *obj, void *data)
  270. {
  271. int res, i;
  272. SQLHSTMT stmt;
  273. SQLINTEGER nativeerror = 0, numfields = 0;
  274. SQLSMALLINT diagbytes = 0;
  275. unsigned char state[10], diagnostic[256];
  276. res = SQLAllocHandle (SQL_HANDLE_STMT, obj->con, &stmt);
  277. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  278. ast_log(LOG_WARNING, "SQL Alloc Handle failed!\n");
  279. return NULL;
  280. }
  281. res = SQLPrepare(stmt, (unsigned char *) data, SQL_NTS);
  282. if ((res != SQL_SUCCESS) && (res != SQL_SUCCESS_WITH_INFO)) {
  283. ast_log(LOG_WARNING, "SQL Prepare failed![%s]\n", (char *) data);
  284. SQLGetDiagField(SQL_HANDLE_STMT, stmt, 1, SQL_DIAG_NUMBER, &numfields, SQL_IS_INTEGER, &diagbytes);
  285. for (i = 0; i < numfields; i++) {
  286. SQLGetDiagRec(SQL_HANDLE_STMT, stmt, i + 1, state, &nativeerror, diagnostic, sizeof(diagnostic), &diagbytes);
  287. ast_log(LOG_WARNING, "SQL Execute returned an error %d: %s: %s (%d)\n", res, state, diagnostic, diagbytes);
  288. if (i > 10) {
  289. ast_log(LOG_WARNING, "Oh, that was good. There are really %d diagnostics?\n", (int)numfields);
  290. break;
  291. }
  292. }
  293. SQLFreeHandle (SQL_HANDLE_STMT, stmt);
  294. return NULL;
  295. }
  296. return stmt;
  297. }
  298. #define LENGTHEN_BUF1(size) \
  299. do { \
  300. /* Lengthen buffer, if necessary */ \
  301. if (ast_str_strlen(sql) + size + 1 > ast_str_size(sql)) { \
  302. if (ast_str_make_space(&sql, ((ast_str_size(sql) + size + 1) / 512 + 1) * 512) != 0) { \
  303. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
  304. ast_free(sql); \
  305. ast_free(sql2); \
  306. AST_RWLIST_UNLOCK(&odbc_tables); \
  307. return -1; \
  308. } \
  309. } \
  310. } while (0)
  311. #define LENGTHEN_BUF2(size) \
  312. do { \
  313. if (ast_str_strlen(sql2) + size + 1 > ast_str_size(sql2)) { \
  314. if (ast_str_make_space(&sql2, ((ast_str_size(sql2) + size + 3) / 512 + 1) * 512) != 0) { \
  315. ast_log(LOG_ERROR, "Unable to allocate sufficient memory. Insert CDR '%s:%s' failed.\n", tableptr->connection, tableptr->table); \
  316. ast_free(sql); \
  317. ast_free(sql2); \
  318. AST_RWLIST_UNLOCK(&odbc_tables); \
  319. return -1; \
  320. } \
  321. } \
  322. } while (0)
  323. static int odbc_log(struct ast_cdr *cdr)
  324. {
  325. struct tables *tableptr;
  326. struct columns *entry;
  327. struct odbc_obj *obj;
  328. struct ast_str *sql = ast_str_create(maxsize), *sql2 = ast_str_create(maxsize2);
  329. char *tmp;
  330. char colbuf[1024], *colptr;
  331. SQLHSTMT stmt = NULL;
  332. SQLLEN rows = 0;
  333. if (!sql || !sql2) {
  334. if (sql)
  335. ast_free(sql);
  336. if (sql2)
  337. ast_free(sql2);
  338. return -1;
  339. }
  340. if (AST_RWLIST_RDLOCK(&odbc_tables)) {
  341. ast_log(LOG_ERROR, "Unable to lock table list. Insert CDR(s) failed.\n");
  342. ast_free(sql);
  343. ast_free(sql2);
  344. return -1;
  345. }
  346. AST_LIST_TRAVERSE(&odbc_tables, tableptr, list) {
  347. int first = 1;
  348. if (ast_strlen_zero(tableptr->schema)) {
  349. ast_str_set(&sql, 0, "INSERT INTO %s (", tableptr->table);
  350. } else {
  351. ast_str_set(&sql, 0, "INSERT INTO %s.%s (", tableptr->schema, tableptr->table);
  352. }
  353. ast_str_set(&sql2, 0, " VALUES (");
  354. /* No need to check the connection now; we'll handle any failure in prepare_and_execute */
  355. if (!(obj = ast_odbc_request_obj(tableptr->connection, 0))) {
  356. 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));
  357. continue;
  358. }
  359. AST_LIST_TRAVERSE(&(tableptr->columns), entry, list) {
  360. int datefield = 0;
  361. if (strcasecmp(entry->cdrname, "start") == 0) {
  362. datefield = 1;
  363. } else if (strcasecmp(entry->cdrname, "answer") == 0) {
  364. datefield = 2;
  365. } else if (strcasecmp(entry->cdrname, "end") == 0) {
  366. datefield = 3;
  367. }
  368. /* Check if we have a similarly named variable */
  369. if (entry->staticvalue) {
  370. colptr = ast_strdupa(entry->staticvalue);
  371. } else if (datefield && tableptr->usegmtime) {
  372. struct timeval date_tv = (datefield == 1) ? cdr->start : (datefield == 2) ? cdr->answer : cdr->end;
  373. struct ast_tm tm = { 0, };
  374. ast_localtime(&date_tv, &tm, "UTC");
  375. ast_strftime(colbuf, sizeof(colbuf), "%Y-%m-%d %H:%M:%S", &tm);
  376. colptr = colbuf;
  377. } else {
  378. ast_cdr_format_var(cdr, entry->cdrname, &colptr, colbuf, sizeof(colbuf), datefield ? 0 : 1);
  379. }
  380. if (colptr) {
  381. /* Check first if the column filters this entry. Note that this
  382. * is very specifically NOT ast_strlen_zero(), because the filter
  383. * could legitimately specify that the field is blank, which is
  384. * different from the field being unspecified (NULL). */
  385. if ((entry->filtervalue && !entry->negatefiltervalue && strcasecmp(colptr, entry->filtervalue) != 0) ||
  386. (entry->filtervalue && entry->negatefiltervalue && strcasecmp(colptr, entry->filtervalue) == 0)) {
  387. ast_verb(4, "CDR column '%s' with value '%s' does not match filter of"
  388. " %s'%s'. Cancelling this CDR.\n",
  389. entry->cdrname, colptr, entry->negatefiltervalue ? "!" : "", entry->filtervalue);
  390. goto early_release;
  391. }
  392. /* Only a filter? */
  393. if (ast_strlen_zero(entry->name))
  394. continue;
  395. LENGTHEN_BUF1(strlen(entry->name));
  396. switch (entry->type) {
  397. case SQL_CHAR:
  398. case SQL_VARCHAR:
  399. case SQL_LONGVARCHAR:
  400. #ifdef HAVE_ODBC_WCHAR
  401. case SQL_WCHAR:
  402. case SQL_WVARCHAR:
  403. case SQL_WLONGVARCHAR:
  404. #endif
  405. case SQL_BINARY:
  406. case SQL_VARBINARY:
  407. case SQL_LONGVARBINARY:
  408. case SQL_GUID:
  409. /* For these two field names, get the rendered form, instead of the raw
  410. * form (but only when we're dealing with a character-based field).
  411. */
  412. if (strcasecmp(entry->name, "disposition") == 0) {
  413. ast_cdr_format_var(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0);
  414. } else if (strcasecmp(entry->name, "amaflags") == 0) {
  415. ast_cdr_format_var(cdr, entry->name, &colptr, colbuf, sizeof(colbuf), 0);
  416. }
  417. /* Truncate too-long fields */
  418. if (entry->type != SQL_GUID) {
  419. if (strlen(colptr) > entry->octetlen) {
  420. colptr[entry->octetlen] = '\0';
  421. }
  422. }
  423. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  424. LENGTHEN_BUF2(strlen(colptr));
  425. /* Encode value, with escaping */
  426. ast_str_append(&sql2, 0, "%s'", first ? "" : ",");
  427. for (tmp = colptr; *tmp; tmp++) {
  428. if (*tmp == '\'') {
  429. ast_str_append(&sql2, 0, "''");
  430. } else if (*tmp == '\\' && ast_odbc_backslash_is_escape(obj)) {
  431. ast_str_append(&sql2, 0, "\\\\");
  432. } else {
  433. ast_str_append(&sql2, 0, "%c", *tmp);
  434. }
  435. }
  436. ast_str_append(&sql2, 0, "'");
  437. break;
  438. case SQL_TYPE_DATE:
  439. if (ast_strlen_zero(colptr)) {
  440. continue;
  441. } else {
  442. int year = 0, month = 0, day = 0;
  443. if (sscanf(colptr, "%4d-%2d-%2d", &year, &month, &day) != 3 || year <= 0 ||
  444. month <= 0 || month > 12 || day < 0 || day > 31 ||
  445. ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
  446. (month == 2 && year % 400 == 0 && day > 29) ||
  447. (month == 2 && year % 100 == 0 && day > 28) ||
  448. (month == 2 && year % 4 == 0 && day > 29) ||
  449. (month == 2 && year % 4 != 0 && day > 28)) {
  450. ast_log(LOG_WARNING, "CDR variable %s is not a valid date ('%s').\n", entry->name, colptr);
  451. continue;
  452. }
  453. if (year > 0 && year < 100) {
  454. year += 2000;
  455. }
  456. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  457. LENGTHEN_BUF2(17);
  458. ast_str_append(&sql2, 0, "%s{ d '%04d-%02d-%02d' }", first ? "" : ",", year, month, day);
  459. }
  460. break;
  461. case SQL_TYPE_TIME:
  462. if (ast_strlen_zero(colptr)) {
  463. continue;
  464. } else {
  465. int hour = 0, minute = 0, second = 0;
  466. int count = sscanf(colptr, "%2d:%2d:%2d", &hour, &minute, &second);
  467. if ((count != 2 && count != 3) || hour < 0 || hour > 23 || minute < 0 || minute > 59 || second < 0 || second > 59) {
  468. ast_log(LOG_WARNING, "CDR variable %s is not a valid time ('%s').\n", entry->name, colptr);
  469. continue;
  470. }
  471. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  472. LENGTHEN_BUF2(15);
  473. ast_str_append(&sql2, 0, "%s{ t '%02d:%02d:%02d' }", first ? "" : ",", hour, minute, second);
  474. }
  475. break;
  476. case SQL_TYPE_TIMESTAMP:
  477. case SQL_TIMESTAMP:
  478. if (ast_strlen_zero(colptr)) {
  479. continue;
  480. } else {
  481. int year = 0, month = 0, day = 0, hour = 0, minute = 0, second = 0;
  482. int count = sscanf(colptr, "%4d-%2d-%2d %2d:%2d:%2d", &year, &month, &day, &hour, &minute, &second);
  483. if ((count != 3 && count != 5 && count != 6) || year <= 0 ||
  484. month <= 0 || month > 12 || day < 0 || day > 31 ||
  485. ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) ||
  486. (month == 2 && year % 400 == 0 && day > 29) ||
  487. (month == 2 && year % 100 == 0 && day > 28) ||
  488. (month == 2 && year % 4 == 0 && day > 29) ||
  489. (month == 2 && year % 4 != 0 && day > 28) ||
  490. hour > 23 || minute > 59 || second > 59 || hour < 0 || minute < 0 || second < 0) {
  491. ast_log(LOG_WARNING, "CDR variable %s is not a valid timestamp ('%s').\n", entry->name, colptr);
  492. continue;
  493. }
  494. if (year > 0 && year < 100) {
  495. year += 2000;
  496. }
  497. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  498. LENGTHEN_BUF2(26);
  499. ast_str_append(&sql2, 0, "%s{ ts '%04d-%02d-%02d %02d:%02d:%02d' }", first ? "" : ",", year, month, day, hour, minute, second);
  500. }
  501. break;
  502. case SQL_INTEGER:
  503. if (ast_strlen_zero(colptr)) {
  504. continue;
  505. } else {
  506. int integer = 0;
  507. if (sscanf(colptr, "%30d", &integer) != 1) {
  508. ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
  509. continue;
  510. }
  511. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  512. LENGTHEN_BUF2(12);
  513. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  514. }
  515. break;
  516. case SQL_BIGINT:
  517. if (ast_strlen_zero(colptr)) {
  518. continue;
  519. } else {
  520. long long integer = 0;
  521. if (sscanf(colptr, "%30lld", &integer) != 1) {
  522. ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
  523. continue;
  524. }
  525. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  526. LENGTHEN_BUF2(24);
  527. ast_str_append(&sql2, 0, "%s%lld", first ? "" : ",", integer);
  528. }
  529. break;
  530. case SQL_SMALLINT:
  531. if (ast_strlen_zero(colptr)) {
  532. continue;
  533. } else {
  534. short integer = 0;
  535. if (sscanf(colptr, "%30hd", &integer) != 1) {
  536. ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
  537. continue;
  538. }
  539. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  540. LENGTHEN_BUF2(6);
  541. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  542. }
  543. break;
  544. case SQL_TINYINT:
  545. if (ast_strlen_zero(colptr)) {
  546. continue;
  547. } else {
  548. signed char integer = 0;
  549. if (sscanf(colptr, "%30hhd", &integer) != 1) {
  550. ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
  551. continue;
  552. }
  553. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  554. LENGTHEN_BUF2(4);
  555. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  556. }
  557. break;
  558. case SQL_BIT:
  559. if (ast_strlen_zero(colptr)) {
  560. continue;
  561. } else {
  562. signed char integer = 0;
  563. if (sscanf(colptr, "%30hhd", &integer) != 1) {
  564. ast_log(LOG_WARNING, "CDR variable %s is not an integer.\n", entry->name);
  565. continue;
  566. }
  567. if (integer != 0)
  568. integer = 1;
  569. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  570. LENGTHEN_BUF2(2);
  571. ast_str_append(&sql2, 0, "%s%d", first ? "" : ",", integer);
  572. }
  573. break;
  574. case SQL_NUMERIC:
  575. case SQL_DECIMAL:
  576. if (ast_strlen_zero(colptr)) {
  577. continue;
  578. } else {
  579. double number = 0.0;
  580. if (!strcasecmp(entry->cdrname, "billsec")) {
  581. if (!ast_tvzero(cdr->answer)) {
  582. snprintf(colbuf, sizeof(colbuf), "%lf",
  583. (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
  584. } else {
  585. ast_copy_string(colbuf, "0", sizeof(colbuf));
  586. }
  587. } else if (!strcasecmp(entry->cdrname, "duration")) {
  588. snprintf(colbuf, sizeof(colbuf), "%lf",
  589. (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
  590. if (!ast_strlen_zero(colbuf)) {
  591. colptr = colbuf;
  592. }
  593. }
  594. if (sscanf(colptr, "%30lf", &number) != 1) {
  595. ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
  596. continue;
  597. }
  598. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  599. LENGTHEN_BUF2(entry->decimals);
  600. ast_str_append(&sql2, 0, "%s%*.*lf", first ? "" : ",", entry->decimals, entry->radix, number);
  601. }
  602. break;
  603. case SQL_FLOAT:
  604. case SQL_REAL:
  605. case SQL_DOUBLE:
  606. if (ast_strlen_zero(colptr)) {
  607. continue;
  608. } else {
  609. double number = 0.0;
  610. if (!strcasecmp(entry->cdrname, "billsec")) {
  611. if (!ast_tvzero(cdr->answer)) {
  612. snprintf(colbuf, sizeof(colbuf), "%lf",
  613. (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
  614. } else {
  615. ast_copy_string(colbuf, "0", sizeof(colbuf));
  616. }
  617. } else if (!strcasecmp(entry->cdrname, "duration")) {
  618. snprintf(colbuf, sizeof(colbuf), "%lf",
  619. (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
  620. if (!ast_strlen_zero(colbuf)) {
  621. colptr = colbuf;
  622. }
  623. }
  624. if (sscanf(colptr, "%30lf", &number) != 1) {
  625. ast_log(LOG_WARNING, "CDR variable %s is not an numeric type.\n", entry->name);
  626. continue;
  627. }
  628. ast_str_append(&sql, 0, "%s%s", first ? "" : ",", entry->name);
  629. LENGTHEN_BUF2(entry->decimals);
  630. ast_str_append(&sql2, 0, "%s%lf", first ? "" : ",", number);
  631. }
  632. break;
  633. default:
  634. 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);
  635. continue;
  636. }
  637. first = 0;
  638. } else if (entry->filtervalue
  639. && ((!entry->negatefiltervalue && entry->filtervalue[0] != '\0')
  640. || (entry->negatefiltervalue && entry->filtervalue[0] == '\0'))) {
  641. ast_log(AST_LOG_WARNING, "CDR column '%s' was not set and does not match filter of"
  642. " %s'%s'. Cancelling this CDR.\n",
  643. entry->cdrname, entry->negatefiltervalue ? "!" : "",
  644. entry->filtervalue);
  645. goto early_release;
  646. }
  647. }
  648. /* Concatenate the two constructed buffers */
  649. LENGTHEN_BUF1(ast_str_strlen(sql2));
  650. ast_str_append(&sql, 0, ")");
  651. ast_str_append(&sql2, 0, ")");
  652. ast_str_append(&sql, 0, "%s", ast_str_buffer(sql2));
  653. ast_debug(3, "Executing [%s]\n", ast_str_buffer(sql));
  654. stmt = ast_odbc_prepare_and_execute(obj, generic_prepare, ast_str_buffer(sql));
  655. if (stmt) {
  656. SQLRowCount(stmt, &rows);
  657. SQLFreeHandle(SQL_HANDLE_STMT, stmt);
  658. }
  659. if (rows == 0) {
  660. ast_log(LOG_WARNING, "cdr_adaptive_odbc: Insert failed on '%s:%s'. CDR failed: %s\n", tableptr->connection, tableptr->table, ast_str_buffer(sql));
  661. }
  662. early_release:
  663. ast_odbc_release_obj(obj);
  664. }
  665. AST_RWLIST_UNLOCK(&odbc_tables);
  666. /* Next time, just allocate buffers that are that big to start with. */
  667. if (ast_str_strlen(sql) > maxsize) {
  668. maxsize = ast_str_strlen(sql);
  669. }
  670. if (ast_str_strlen(sql2) > maxsize2) {
  671. maxsize2 = ast_str_strlen(sql2);
  672. }
  673. ast_free(sql);
  674. ast_free(sql2);
  675. return 0;
  676. }
  677. static int unload_module(void)
  678. {
  679. if (ast_cdr_unregister(name)) {
  680. return -1;
  681. }
  682. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  683. ast_cdr_register(name, ast_module_info->description, odbc_log);
  684. ast_log(LOG_ERROR, "Unable to lock column list. Unload failed.\n");
  685. return -1;
  686. }
  687. free_config();
  688. AST_RWLIST_UNLOCK(&odbc_tables);
  689. return 0;
  690. }
  691. static int load_module(void)
  692. {
  693. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  694. ast_log(LOG_ERROR, "Unable to lock column list. Load failed.\n");
  695. return 0;
  696. }
  697. load_config();
  698. AST_RWLIST_UNLOCK(&odbc_tables);
  699. ast_cdr_register(name, ast_module_info->description, odbc_log);
  700. return 0;
  701. }
  702. static int reload(void)
  703. {
  704. if (AST_RWLIST_WRLOCK(&odbc_tables)) {
  705. ast_log(LOG_ERROR, "Unable to lock column list. Reload failed.\n");
  706. return -1;
  707. }
  708. free_config();
  709. load_config();
  710. AST_RWLIST_UNLOCK(&odbc_tables);
  711. return 0;
  712. }
  713. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "Adaptive ODBC CDR backend",
  714. .support_level = AST_MODULE_SUPPORT_CORE,
  715. .load = load_module,
  716. .unload = unload_module,
  717. .reload = reload,
  718. .load_pri = AST_MODPRI_CDR_DRIVER,
  719. );