cdr_adaptive_odbc.c 27 KB

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