cdr_mysql.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * James Sharp <jsharp@psychoses.org>
  5. *
  6. * Modified August 2003
  7. * Tilghman Lesher <asterisk__cdr__cdr_mysql__200308@the-tilghman.com>
  8. *
  9. * Modified August 6, 2005
  10. * Joseph Benden <joe@thrallingpenguin.com>
  11. * Added mysql connection timeout parameter
  12. * Added an automatic reconnect as to not lose a cdr record
  13. * Cleaned up the original code to match the coding guidelines
  14. *
  15. * Modified Juli 2006
  16. * Martin Portmann <map@infinitum.ch>
  17. * Added mysql ssl support
  18. *
  19. * See http://www.asterisk.org for more information about
  20. * the Asterisk project. Please do not directly contact
  21. * any of the maintainers of this project for assistance;
  22. * the project provides a web site, mailing lists and IRC
  23. * channels for your use.
  24. *
  25. * This program is free software, distributed under the terms of
  26. * the GNU General Public License Version 2. See the LICENSE file
  27. * at the top of the source tree.
  28. */
  29. /*!
  30. * \file
  31. * \brief MySQL CDR backend
  32. * \ingroup cdr_drivers
  33. */
  34. /*** MODULEINFO
  35. <depend>mysqlclient</depend>
  36. <defaultenabled>no</defaultenabled>
  37. <support_level>deprecated</support_level>
  38. <replacement>cdr_adaptive_odbc</replacement>
  39. ***/
  40. #include "asterisk.h"
  41. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  42. #include <mysql/mysql.h>
  43. #include <mysql/errmsg.h>
  44. #include "asterisk/config.h"
  45. #include "asterisk/options.h"
  46. #include "asterisk/channel.h"
  47. #include "asterisk/cdr.h"
  48. #include "asterisk/module.h"
  49. #include "asterisk/logger.h"
  50. #include "asterisk/cli.h"
  51. #include "asterisk/strings.h"
  52. #include "asterisk/linkedlists.h"
  53. #include "asterisk/threadstorage.h"
  54. #define DATE_FORMAT "%Y-%m-%d %T"
  55. AST_THREADSTORAGE(sql1_buf);
  56. AST_THREADSTORAGE(sql2_buf);
  57. AST_THREADSTORAGE(escape_buf);
  58. static const char desc[] = "MySQL CDR Backend";
  59. static const char name[] = "mysql";
  60. static const char config[] = "cdr_mysql.conf";
  61. static struct ast_str *hostname = NULL, *dbname = NULL, *dbuser = NULL, *password = NULL, *dbsock = NULL, *dbtable = NULL, *dbcharset = NULL, *cdrzone = NULL;
  62. static struct ast_str *ssl_ca = NULL, *ssl_cert = NULL, *ssl_key = NULL;
  63. static int dbport = 0;
  64. static int connected = 0;
  65. static time_t connect_time = 0;
  66. static int records = 0;
  67. static int totalrecords = 0;
  68. static int timeout = 0;
  69. static int calldate_compat = 0;
  70. AST_MUTEX_DEFINE_STATIC(mysql_lock);
  71. struct unload_string {
  72. AST_LIST_ENTRY(unload_string) entry;
  73. struct ast_str *str;
  74. };
  75. static AST_LIST_HEAD_STATIC(unload_strings, unload_string);
  76. struct column {
  77. char *name;
  78. char *cdrname;
  79. char *staticvalue;
  80. char *type;
  81. AST_LIST_ENTRY(column) list;
  82. };
  83. /* Protected with mysql_lock */
  84. static AST_RWLIST_HEAD_STATIC(columns, column);
  85. static MYSQL mysql = { { NULL }, };
  86. static char *handle_cli_cdr_mysql_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  87. {
  88. switch (cmd) {
  89. case CLI_INIT:
  90. e->command = "cdr mysql status";
  91. e->usage =
  92. "Usage: cdr mysql status\n"
  93. " Shows current connection status for cdr_mysql\n";
  94. return NULL;
  95. case CLI_GENERATE:
  96. return NULL;
  97. }
  98. if (a->argc != 3)
  99. return CLI_SHOWUSAGE;
  100. if (connected) {
  101. char status[256], status2[100] = "";
  102. int ctime = time(NULL) - connect_time;
  103. if (dbport)
  104. snprintf(status, 255, "Connected to %s@%s, port %d", ast_str_buffer(dbname), ast_str_buffer(hostname), dbport);
  105. else if (dbsock)
  106. snprintf(status, 255, "Connected to %s on socket file %s", ast_str_buffer(dbname), S_OR(ast_str_buffer(dbsock), "default"));
  107. else
  108. snprintf(status, 255, "Connected to %s@%s", ast_str_buffer(dbname), ast_str_buffer(hostname));
  109. if (!ast_strlen_zero(ast_str_buffer(dbuser)))
  110. snprintf(status2, 99, " with username %s", ast_str_buffer(dbuser));
  111. if (ast_str_strlen(dbtable))
  112. snprintf(status2, 99, " using table %s", ast_str_buffer(dbtable));
  113. if (ctime > 31536000) {
  114. ast_cli(a->fd, "%s%s for %d years, %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 31536000, (ctime % 31536000) / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
  115. } else if (ctime > 86400) {
  116. ast_cli(a->fd, "%s%s for %d days, %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 86400, (ctime % 86400) / 3600, (ctime % 3600) / 60, ctime % 60);
  117. } else if (ctime > 3600) {
  118. ast_cli(a->fd, "%s%s for %d hours, %d minutes, %d seconds.\n", status, status2, ctime / 3600, (ctime % 3600) / 60, ctime % 60);
  119. } else if (ctime > 60) {
  120. ast_cli(a->fd, "%s%s for %d minutes, %d seconds.\n", status, status2, ctime / 60, ctime % 60);
  121. } else {
  122. ast_cli(a->fd, "%s%s for %d seconds.\n", status, status2, ctime);
  123. }
  124. if (records == totalrecords)
  125. ast_cli(a->fd, " Wrote %d records since last restart.\n", totalrecords);
  126. else
  127. ast_cli(a->fd, " Wrote %d records since last restart and %d records since last reconnect.\n", totalrecords, records);
  128. } else {
  129. ast_cli(a->fd, "Not currently connected to a MySQL server.\n");
  130. }
  131. return CLI_SUCCESS;
  132. }
  133. static struct ast_cli_entry cdr_mysql_status_cli[] = {
  134. AST_CLI_DEFINE(handle_cli_cdr_mysql_status, "Show connection status of cdr_mysql"),
  135. };
  136. static int mysql_log(struct ast_cdr *cdr)
  137. {
  138. struct ast_str *sql1 = ast_str_thread_get(&sql1_buf, 1024), *sql2 = ast_str_thread_get(&sql2_buf, 1024);
  139. int retries = 5;
  140. #if MYSQL_VERSION_ID >= 50013
  141. my_bool my_bool_true = 1;
  142. #endif
  143. if (!sql1 || !sql2) {
  144. ast_log(LOG_ERROR, "Memory error\n");
  145. return -1;
  146. }
  147. ast_mutex_lock(&mysql_lock);
  148. db_reconnect:
  149. if ((!connected) && (hostname || dbsock) && dbuser && password && dbname && dbtable ) {
  150. /* Attempt to connect */
  151. mysql_init(&mysql);
  152. /* Add option to quickly timeout the connection */
  153. if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout) != 0) {
  154. ast_log(LOG_ERROR, "mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  155. }
  156. #if MYSQL_VERSION_ID >= 50013
  157. /* Add option for automatic reconnection */
  158. if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) {
  159. ast_log(LOG_ERROR, "mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  160. }
  161. #endif
  162. if (ssl_ca || ssl_cert || ssl_key) {
  163. mysql_ssl_set(&mysql, ssl_key ? ast_str_buffer(ssl_key) : NULL, ssl_cert ? ast_str_buffer(ssl_cert) : NULL, ssl_ca ? ast_str_buffer(ssl_ca) : NULL, NULL, NULL);
  164. }
  165. if (mysql_real_connect(&mysql, ast_str_buffer(hostname), ast_str_buffer(dbuser), ast_str_buffer(password), ast_str_buffer(dbname), dbport, dbsock && ast_str_strlen(dbsock) ? ast_str_buffer(dbsock) : NULL, ssl_ca ? CLIENT_SSL : 0)) {
  166. connected = 1;
  167. connect_time = time(NULL);
  168. records = 0;
  169. if (dbcharset) {
  170. ast_str_set(&sql1, 0, "SET NAMES '%s'", ast_str_buffer(dbcharset));
  171. mysql_real_query(&mysql, ast_str_buffer(sql1), ast_str_strlen(sql1));
  172. ast_debug(1, "SQL command as follows: %s\n", ast_str_buffer(sql1));
  173. }
  174. } else {
  175. ast_log(LOG_ERROR, "Cannot connect to database server %s: (%d) %s\n", ast_str_buffer(hostname), mysql_errno(&mysql), mysql_error(&mysql));
  176. connected = 0;
  177. }
  178. } else {
  179. /* Long connection - ping the server */
  180. int error;
  181. if ((error = mysql_ping(&mysql))) {
  182. connected = 0;
  183. records = 0;
  184. switch (mysql_errno(&mysql)) {
  185. case CR_SERVER_GONE_ERROR:
  186. case CR_SERVER_LOST:
  187. ast_log(LOG_ERROR, "Server has gone away. Attempting to reconnect.\n");
  188. break;
  189. default:
  190. ast_log(LOG_ERROR, "Unknown connection error: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  191. }
  192. retries--;
  193. if (retries) {
  194. goto db_reconnect;
  195. } else {
  196. ast_log(LOG_ERROR, "Retried to connect five times, giving up.\n");
  197. }
  198. }
  199. }
  200. if (connected) {
  201. int column_count = 0;
  202. char *cdrname;
  203. char workspace[2048], *value = NULL;
  204. struct column *entry;
  205. struct ast_str *escape = ast_str_thread_get(&escape_buf, 16);
  206. ast_str_set(&sql1, 0, "INSERT INTO %s (", AS_OR(dbtable, "cdr"));
  207. ast_str_set(&sql2, 0, ") VALUES (");
  208. AST_RWLIST_RDLOCK(&columns);
  209. AST_RWLIST_TRAVERSE(&columns, entry, list) {
  210. if (!strcmp(entry->name, "calldate")) {
  211. /*!\note
  212. * For some dumb reason, "calldate" used to be formulated using
  213. * the datetime the record was posted, rather than the start
  214. * time of the call. If someone really wants the old compatible
  215. * behavior, it's provided here.
  216. */
  217. if (calldate_compat) {
  218. struct timeval tv = ast_tvnow();
  219. struct ast_tm tm;
  220. char timestr[128];
  221. ast_localtime(&tv, &tm, ast_str_strlen(cdrzone) ? ast_str_buffer(cdrzone) : NULL);
  222. ast_strftime(timestr, sizeof(timestr), "%Y-%m-%d %T", &tm);
  223. ast_cdr_setvar(cdr, "calldate", timestr, 0);
  224. cdrname = "calldate";
  225. } else {
  226. cdrname = "start";
  227. }
  228. } else {
  229. cdrname = entry->cdrname;
  230. }
  231. /* Construct SQL */
  232. /* Need the type and value to determine if we want the raw value or not */
  233. if (entry->staticvalue) {
  234. value = ast_strdupa(entry->staticvalue);
  235. } else if ((!strcmp(cdrname, "start") ||
  236. !strcmp(cdrname, "answer") ||
  237. !strcmp(cdrname, "end") ||
  238. !strcmp(cdrname, "disposition") ||
  239. !strcmp(cdrname, "amaflags")) &&
  240. (strstr(entry->type, "int") ||
  241. strstr(entry->type, "dec") ||
  242. strstr(entry->type, "float") ||
  243. strstr(entry->type, "double") ||
  244. strstr(entry->type, "real") ||
  245. strstr(entry->type, "numeric") ||
  246. strstr(entry->type, "fixed"))) {
  247. ast_cdr_getvar(cdr, cdrname, &value, workspace, sizeof(workspace), 0, 1);
  248. } else {
  249. ast_cdr_getvar(cdr, cdrname, &value, workspace, sizeof(workspace), 0, 0);
  250. }
  251. if (value) {
  252. size_t valsz;
  253. if (column_count++) {
  254. ast_str_append(&sql1, 0, ",");
  255. ast_str_append(&sql2, 0, ",");
  256. }
  257. if (!strcasecmp(cdrname, "billsec") &&
  258. (strstr(entry->type, "float") ||
  259. strstr(entry->type, "double") ||
  260. strstr(entry->type, "decimal") ||
  261. strstr(entry->type, "numeric") ||
  262. strstr(entry->type, "real"))) {
  263. if (!ast_tvzero(cdr->answer)) {
  264. snprintf(workspace, sizeof(workspace), "%lf",
  265. (double) (ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0));
  266. } else {
  267. ast_copy_string(workspace, "0", sizeof(workspace));
  268. }
  269. if (!ast_strlen_zero(workspace)) {
  270. value = workspace;
  271. }
  272. }
  273. if (!strcasecmp(cdrname, "duration") &&
  274. (strstr(entry->type, "float") ||
  275. strstr(entry->type, "double") ||
  276. strstr(entry->type, "decimal") ||
  277. strstr(entry->type, "numeric") ||
  278. strstr(entry->type, "real"))) {
  279. snprintf(workspace, sizeof(workspace), "%lf",
  280. (double) (ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0));
  281. if (!ast_strlen_zero(workspace)) {
  282. value = workspace;
  283. }
  284. }
  285. ast_str_make_space(&escape, (valsz = strlen(value)) * 2 + 1);
  286. mysql_real_escape_string(&mysql, ast_str_buffer(escape), value, valsz);
  287. ast_str_append(&sql1, 0, "`%s`", entry->name);
  288. ast_str_append(&sql2, 0, "'%s'", ast_str_buffer(escape));
  289. }
  290. }
  291. AST_RWLIST_UNLOCK(&columns);
  292. ast_debug(1, "Inserting a CDR record.\n");
  293. ast_str_append(&sql1, 0, "%s)", ast_str_buffer(sql2));
  294. ast_debug(1, "SQL command as follows: %s\n", ast_str_buffer(sql1));
  295. if (mysql_real_query(&mysql, ast_str_buffer(sql1), ast_str_strlen(sql1))) {
  296. ast_log(LOG_ERROR, "Failed to insert into database: (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  297. mysql_close(&mysql);
  298. connected = 0;
  299. } else {
  300. records++;
  301. totalrecords++;
  302. }
  303. }
  304. ast_mutex_unlock(&mysql_lock);
  305. return 0;
  306. }
  307. static int my_unload_module(int reload)
  308. {
  309. struct unload_string *us;
  310. struct column *entry;
  311. ast_cli_unregister_multiple(cdr_mysql_status_cli, sizeof(cdr_mysql_status_cli) / sizeof(struct ast_cli_entry));
  312. if (connected) {
  313. mysql_close(&mysql);
  314. connected = 0;
  315. records = 0;
  316. }
  317. AST_LIST_LOCK(&unload_strings);
  318. while ((us = AST_LIST_REMOVE_HEAD(&unload_strings, entry))) {
  319. ast_free(us->str);
  320. ast_free(us);
  321. }
  322. AST_LIST_UNLOCK(&unload_strings);
  323. if (!reload) {
  324. AST_RWLIST_WRLOCK(&columns);
  325. }
  326. while ((entry = AST_RWLIST_REMOVE_HEAD(&columns, list))) {
  327. ast_free(entry);
  328. }
  329. if (!reload) {
  330. AST_RWLIST_UNLOCK(&columns);
  331. }
  332. dbport = 0;
  333. ast_cdr_unregister(name);
  334. return 0;
  335. }
  336. static int my_load_config_string(struct ast_config *cfg, const char *category, const char *variable, struct ast_str **field, const char *def)
  337. {
  338. struct unload_string *us;
  339. const char *tmp;
  340. if (!(us = ast_calloc(1, sizeof(*us))))
  341. return -1;
  342. if (!(*field = ast_str_create(16))) {
  343. ast_free(us);
  344. return -1;
  345. }
  346. tmp = ast_variable_retrieve(cfg, category, variable);
  347. ast_str_set(field, 0, "%s", tmp ? tmp : def);
  348. us->str = *field;
  349. AST_LIST_LOCK(&unload_strings);
  350. AST_LIST_INSERT_HEAD(&unload_strings, us, entry);
  351. AST_LIST_UNLOCK(&unload_strings);
  352. return 0;
  353. }
  354. static int my_load_config_number(struct ast_config *cfg, const char *category, const char *variable, int *field, int def)
  355. {
  356. const char *tmp;
  357. tmp = ast_variable_retrieve(cfg, category, variable);
  358. if (!tmp || sscanf(tmp, "%30d", field) < 1)
  359. *field = def;
  360. return 0;
  361. }
  362. static int my_load_module(int reload)
  363. {
  364. int res;
  365. struct ast_config *cfg;
  366. struct ast_variable *var;
  367. struct ast_flags config_flags = { 0 };
  368. struct column *entry;
  369. char *temp;
  370. struct ast_str *compat;
  371. MYSQL_ROW row;
  372. MYSQL_RES *result;
  373. char sqldesc[128];
  374. #if MYSQL_VERSION_ID >= 50013
  375. my_bool my_bool_true = 1;
  376. #endif
  377. /* Cannot use a conditionally different flag, because the table layout may
  378. * have changed, which is not detectable by config file change detection,
  379. * but should still cause the configuration to be re-parsed. */
  380. cfg = ast_config_load(config, config_flags);
  381. if (!cfg) {
  382. ast_log(LOG_WARNING, "Unable to load config for mysql CDR's: %s\n", config);
  383. return AST_MODULE_LOAD_SUCCESS;
  384. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
  385. return AST_MODULE_LOAD_SUCCESS;
  386. if (reload) {
  387. AST_RWLIST_WRLOCK(&columns);
  388. my_unload_module(1);
  389. }
  390. var = ast_variable_browse(cfg, "global");
  391. if (!var) {
  392. /* nothing configured */
  393. if (reload) {
  394. AST_RWLIST_UNLOCK(&columns);
  395. }
  396. return AST_MODULE_LOAD_SUCCESS;
  397. }
  398. res = 0;
  399. res |= my_load_config_string(cfg, "global", "hostname", &hostname, "localhost");
  400. res |= my_load_config_string(cfg, "global", "dbname", &dbname, "astriskcdrdb");
  401. res |= my_load_config_string(cfg, "global", "user", &dbuser, "root");
  402. res |= my_load_config_string(cfg, "global", "sock", &dbsock, "");
  403. res |= my_load_config_string(cfg, "global", "table", &dbtable, "cdr");
  404. res |= my_load_config_string(cfg, "global", "password", &password, "");
  405. res |= my_load_config_string(cfg, "global", "charset", &dbcharset, "");
  406. res |= my_load_config_string(cfg, "global", "ssl_ca", &ssl_ca, "");
  407. res |= my_load_config_string(cfg, "global", "ssl_cert", &ssl_cert, "");
  408. res |= my_load_config_string(cfg, "global", "ssl_key", &ssl_key, "");
  409. res |= my_load_config_number(cfg, "global", "port", &dbport, 0);
  410. res |= my_load_config_number(cfg, "global", "timeout", &timeout, 0);
  411. res |= my_load_config_string(cfg, "global", "compat", &compat, "no");
  412. res |= my_load_config_string(cfg, "global", "cdrzone", &cdrzone, "");
  413. if (ast_str_strlen(cdrzone) == 0) {
  414. for (; var; var = var->next) {
  415. if (!strcasecmp(var->name, "usegmtime") && ast_true(var->value)) {
  416. ast_str_set(&cdrzone, 0, "UTC");
  417. }
  418. }
  419. }
  420. if (ast_true(ast_str_buffer(compat))) {
  421. calldate_compat = 1;
  422. } else {
  423. calldate_compat = 0;
  424. }
  425. if (res < 0) {
  426. if (reload) {
  427. AST_RWLIST_UNLOCK(&columns);
  428. }
  429. return AST_MODULE_LOAD_FAILURE;
  430. }
  431. /* Check for any aliases */
  432. if (!reload) {
  433. /* Lock, if not already */
  434. AST_RWLIST_WRLOCK(&columns);
  435. }
  436. while ((entry = AST_LIST_REMOVE_HEAD(&columns, list))) {
  437. ast_free(entry);
  438. }
  439. ast_debug(1, "Got hostname of %s\n", ast_str_buffer(hostname));
  440. ast_debug(1, "Got port of %d\n", dbport);
  441. ast_debug(1, "Got a timeout of %d\n", timeout);
  442. if (dbsock)
  443. ast_debug(1, "Got sock file of %s\n", ast_str_buffer(dbsock));
  444. ast_debug(1, "Got user of %s\n", ast_str_buffer(dbuser));
  445. ast_debug(1, "Got dbname of %s\n", ast_str_buffer(dbname));
  446. ast_debug(1, "Got password of %s\n", ast_str_buffer(password));
  447. ast_debug(1, "%sunning in calldate compatibility mode\n", calldate_compat ? "R" : "Not r");
  448. ast_debug(1, "Dates and times are localized to %s\n", S_OR(ast_str_buffer(cdrzone), "local timezone"));
  449. if (dbcharset) {
  450. ast_debug(1, "Got DB charset of %s\n", ast_str_buffer(dbcharset));
  451. }
  452. mysql_init(&mysql);
  453. if (timeout && mysql_options(&mysql, MYSQL_OPT_CONNECT_TIMEOUT, (char *)&timeout) != 0) {
  454. ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  455. }
  456. #if MYSQL_VERSION_ID >= 50013
  457. /* Add option for automatic reconnection */
  458. if (mysql_options(&mysql, MYSQL_OPT_RECONNECT, &my_bool_true) != 0) {
  459. ast_log(LOG_ERROR, "cdr_mysql: mysql_options returned (%d) %s\n", mysql_errno(&mysql), mysql_error(&mysql));
  460. }
  461. #endif
  462. if ((ssl_ca && ast_str_strlen(ssl_ca)) || (ssl_cert && ast_str_strlen(ssl_cert)) || (ssl_key && ast_str_strlen(ssl_key))) {
  463. mysql_ssl_set(&mysql,
  464. ssl_key ? ast_str_buffer(ssl_key) : NULL,
  465. ssl_cert ? ast_str_buffer(ssl_cert) : NULL,
  466. ssl_ca ? ast_str_buffer(ssl_ca) : NULL,
  467. NULL, NULL);
  468. }
  469. temp = dbsock && ast_str_strlen(dbsock) ? ast_str_buffer(dbsock) : NULL;
  470. if (!mysql_real_connect(&mysql, ast_str_buffer(hostname), ast_str_buffer(dbuser), ast_str_buffer(password), ast_str_buffer(dbname), dbport, temp, ssl_ca && ast_str_strlen(ssl_ca) ? CLIENT_SSL : 0)) {
  471. ast_log(LOG_ERROR, "Failed to connect to mysql database %s on %s.\n", ast_str_buffer(dbname), ast_str_buffer(hostname));
  472. connected = 0;
  473. records = 0;
  474. } else {
  475. ast_debug(1, "Successfully connected to MySQL database.\n");
  476. connected = 1;
  477. records = 0;
  478. connect_time = time(NULL);
  479. if (dbcharset) {
  480. snprintf(sqldesc, sizeof(sqldesc), "SET NAMES '%s'", ast_str_buffer(dbcharset));
  481. mysql_real_query(&mysql, sqldesc, strlen(sqldesc));
  482. ast_debug(1, "SQL command as follows: %s\n", sqldesc);
  483. }
  484. /* Get table description */
  485. snprintf(sqldesc, sizeof(sqldesc), "DESC %s", dbtable ? ast_str_buffer(dbtable) : "cdr");
  486. if (mysql_query(&mysql, sqldesc)) {
  487. ast_log(LOG_ERROR, "Unable to query table description!! Logging disabled.\n");
  488. mysql_close(&mysql);
  489. connected = 0;
  490. AST_RWLIST_UNLOCK(&columns);
  491. ast_config_destroy(cfg);
  492. return AST_MODULE_LOAD_FAILURE;
  493. }
  494. if (!(result = mysql_store_result(&mysql))) {
  495. ast_log(LOG_ERROR, "Unable to query table description!! Logging disabled.\n");
  496. mysql_close(&mysql);
  497. connected = 0;
  498. AST_RWLIST_UNLOCK(&columns);
  499. ast_config_destroy(cfg);
  500. return AST_MODULE_LOAD_FAILURE;
  501. }
  502. while ((row = mysql_fetch_row(result))) {
  503. struct column *entry;
  504. char *cdrvar = "", *staticvalue = "";
  505. ast_debug(1, "Got a field '%s' of type '%s'\n", row[0], row[1]);
  506. /* Check for an alias or a static value */
  507. for (var = ast_variable_browse(cfg, "columns"); var; var = var->next) {
  508. if (strncmp(var->name, "alias", 5) == 0 && strcasecmp(var->value, row[0]) == 0 ) {
  509. char *alias = ast_strdupa(var->name + 5);
  510. cdrvar = ast_strip(alias);
  511. ast_verb(3, "Found alias %s for column %s\n", cdrvar, row[0]);
  512. break;
  513. } else if (strncmp(var->name, "static", 6) == 0 && strcasecmp(var->value, row[0]) == 0) {
  514. char *item = ast_strdupa(var->name + 6);
  515. item = ast_strip(item);
  516. if (item[0] == '"' && item[strlen(item) - 1] == '"') {
  517. /* Remove surrounding quotes */
  518. item[strlen(item) - 1] = '\0';
  519. item++;
  520. }
  521. staticvalue = item;
  522. }
  523. }
  524. entry = ast_calloc(sizeof(char), sizeof(*entry) + strlen(row[0]) + 1 + strlen(cdrvar) + 1 + strlen(staticvalue) + 1 + strlen(row[1]) + 1);
  525. if (!entry) {
  526. ast_log(LOG_ERROR, "Out of memory creating entry for column '%s'\n", row[0]);
  527. res = -1;
  528. break;
  529. }
  530. entry->name = (char *)entry + sizeof(*entry);
  531. strcpy(entry->name, row[0]);
  532. if (!ast_strlen_zero(cdrvar)) {
  533. entry->cdrname = entry->name + strlen(row[0]) + 1;
  534. strcpy(entry->cdrname, cdrvar);
  535. } else { /* Point to same place as the column name */
  536. entry->cdrname = (char *)entry + sizeof(*entry);
  537. }
  538. if (!ast_strlen_zero(staticvalue)) {
  539. entry->staticvalue = entry->cdrname + strlen(entry->cdrname) + 1;
  540. strcpy(entry->staticvalue, staticvalue);
  541. ast_debug(1, "staticvalue length: %d\n", (int) strlen(staticvalue) );
  542. entry->type = entry->staticvalue + strlen(entry->staticvalue) + 1;
  543. } else {
  544. entry->type = entry->cdrname + strlen(entry->cdrname) + 1;
  545. }
  546. strcpy(entry->type, row[1]);
  547. ast_debug(1, "Entry name '%s'\n", entry->name);
  548. ast_debug(1, " cdrname '%s'\n", entry->cdrname);
  549. ast_debug(1, " static '%s'\n", entry->staticvalue);
  550. ast_debug(1, " type '%s'\n", entry->type);
  551. AST_LIST_INSERT_TAIL(&columns, entry, list);
  552. }
  553. mysql_free_result(result);
  554. }
  555. AST_RWLIST_UNLOCK(&columns);
  556. ast_config_destroy(cfg);
  557. if (res < 0) {
  558. return AST_MODULE_LOAD_FAILURE;
  559. }
  560. res = ast_cdr_register(name, desc, mysql_log);
  561. if (res) {
  562. ast_log(LOG_ERROR, "Unable to register MySQL CDR handling\n");
  563. } else {
  564. res = ast_cli_register_multiple(cdr_mysql_status_cli, sizeof(cdr_mysql_status_cli) / sizeof(struct ast_cli_entry));
  565. }
  566. return res;
  567. }
  568. static int load_module(void)
  569. {
  570. return my_load_module(0);
  571. }
  572. static int unload_module(void)
  573. {
  574. return my_unload_module(0);
  575. }
  576. static int reload(void)
  577. {
  578. int ret;
  579. ast_mutex_lock(&mysql_lock);
  580. ret = my_load_module(1);
  581. ast_mutex_unlock(&mysql_lock);
  582. return ret;
  583. }
  584. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "MySQL CDR Backend",
  585. .load = load_module,
  586. .unload = unload_module,
  587. .reload = reload,
  588. );