75 #define DATE_FORMAT "%Y/%m/%d %T" 77 static const char name[] =
"FreeTDS (MSSQL)";
78 static const char config[] =
"cdr_tds.conf";
101 static void get_date(
char *,
size_t len,
struct timeval);
104 __attribute__((
format(printf, 2, 3)));
112 char *
accountcode, *src, *dst, *dcontext, *clid, *
channel, *dstchannel, *lastapp, *lastdata, *uniqueid, *userfield =
NULL;
128 get_date(start,
sizeof(start), cdr->start);
129 get_date(answer,
sizeof(answer), cdr->answer);
130 get_date(end,
sizeof(end), cdr->end);
153 double hrbillsec = 0.0;
157 hrbillsec = (double)(
ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0);
159 hrduration = (double)(
ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0);
161 erc = dbfcmd(settings->
dbproc,
164 "accountcode, src, dst, dcontext, clid, channel, " 165 "dstchannel, lastapp, lastdata, start, answer, [end], duration, " 166 "billsec, disposition, amaflags, uniqueid, userfield" 170 "'%s', '%s', '%s', '%s', '%s', '%s', " 171 "'%s', '%s', '%s', %s, %s, %s, %lf, " 172 "%lf, '%s', '%s', '%s', '%s'" 175 accountcode, src, dst, dcontext, clid, channel,
176 dstchannel, lastapp, lastdata, start, answer, end, hrduration,
181 erc = dbfcmd(settings->
dbproc,
184 "accountcode, src, dst, dcontext, clid, channel, " 185 "dstchannel, lastapp, lastdata, start, answer, [end], duration, " 186 "billsec, disposition, amaflags, uniqueid, userfield" 190 "'%s', '%s', '%s', '%s', '%s', '%s', " 191 "'%s', '%s', '%s', %s, %s, %s, %ld, " 192 "%ld, '%s', '%s', '%s', '%s'" 195 accountcode, src, dst, dcontext, clid, channel,
196 dstchannel, lastapp, lastdata, start, answer, end, cdr->duration,
203 double hrbillsec = 0.0;
207 hrbillsec = (double)(
ast_tvdiff_us(cdr->end, cdr->answer) / 1000000.0);
209 hrduration = (double)(
ast_tvdiff_us(cdr->end, cdr->start) / 1000000.0);
211 erc = dbfcmd(settings->
dbproc,
214 "accountcode, src, dst, dcontext, clid, channel, " 215 "dstchannel, lastapp, lastdata, start, answer, [end], duration, " 216 "billsec, disposition, amaflags, uniqueid" 220 "'%s', '%s', '%s', '%s', '%s', '%s', " 221 "'%s', '%s', '%s', %s, %s, %s, %lf, " 222 "%lf, '%s', '%s', '%s'" 225 accountcode, src, dst, dcontext, clid, channel,
226 dstchannel, lastapp, lastdata, start, answer, end, hrduration,
230 erc = dbfcmd(settings->
dbproc,
233 "accountcode, src, dst, dcontext, clid, channel, " 234 "dstchannel, lastapp, lastdata, start, answer, [end], duration, " 235 "billsec, disposition, amaflags, uniqueid" 239 "'%s', '%s', '%s', '%s', '%s', '%s', " 240 "'%s', '%s', '%s', %s, %s, %s, %ld, " 241 "%ld, '%s', '%s', '%s'" 244 accountcode, src, dst, dcontext, clid, channel,
245 dstchannel, lastapp, lastdata, start, answer, end, cdr->duration,
257 ast_log(
LOG_ERROR,
"Failed to build INSERT statement, no CDR was logged.\n");
262 if (dbsqlexec(settings->
dbproc) == FAIL) {
268 ast_log(
LOG_ERROR,
"Failed to execute INSERT statement, no CDR was logged.\n");
275 while (dbresults(settings->
dbproc) != NO_MORE_RESULTS) {
276 while (dbnextrow(settings->
dbproc) != NO_MORE_ROWS);
306 char *buf_ptr, *srh_ptr;
307 char *known_bad[] = {
"select",
"insert",
"update",
"delete",
"drop",
";",
"--",
"\0"};
318 for (; *str && strlen(buf) <
len; str++) {
327 for (idx = 0; *known_bad[idx]; idx++) {
328 while ((srh_ptr =
strcasestr(buf, known_bad[idx]))) {
329 memmove(srh_ptr, srh_ptr + strlen(known_bad[idx]), strlen(srh_ptr + strlen(known_bad[idx])) + 1);
336 static void get_date(
char *dateField,
size_t len,
struct timeval when)
360 if (dbfcmd(dbproc, buffer) == FAIL) {
367 if (dbsqlexec(dbproc) == FAIL) {
372 while (dbresults(dbproc) != NO_MORE_RESULTS) {
373 while (dbnextrow(dbproc) != NO_MORE_ROWS);
382 dbclose(settings->
dbproc);
395 if ((login = dblogin()) ==
NULL) {
400 DBSETLAPP(login,
"TSQL");
401 DBSETLUSER(login, (
char *) settings->
username);
402 DBSETLPWD(login, (
char *) settings->
password);
403 DBSETLCHARSET(login, (
char *) settings->
charset);
404 DBSETLNATLANG(login, (
char *) settings->
language);
414 if (dbuse(settings->
dbproc, (
char *) settings->
database) == FAIL) {
437 dbclose(settings->
dbproc);
466 if (oserr != DBNOERR) {
475 ast_debug(1,
"Msg %d, Level %d, State %d, Line %d\n", msgno, severity, msgstate, line);
484 const char *ptr =
NULL;
515 ast_log(
LOG_ERROR,
"Failed to connect: Database server connection not specified.\n");
540 ast_log(
LOG_ERROR,
"Failed to connect: Database password not specified.\n");
570 ast_log(
LOG_NOTICE,
"High Resolution Time not found, using integers for billsec and duration fields by default.\n");
599 if (dbinit() == FAIL) {
static char accountcode[AST_MAX_ACCOUNT_CODE]
Asterisk main include file. File version handling, generic pbx functions.
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
static char * anti_injection(const char *, int)
static const char config[]
static int tds_log(struct ast_cdr *cdr)
struct ast_variable * ast_variable_browse(const struct ast_config *config, const char *category_name)
const ast_string_field hostname
const ast_string_field table
#define CONFIG_STATUS_FILEINVALID
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
static ast_mutex_t tds_lock
int ast_tvzero(const struct timeval t)
Returns true if the argument is 0,0.
#define ast_calloc_with_stringfields(n, type, size)
Allocate a structure with embedded stringfields in a single allocation.
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
#define AST_DECLARE_STRING_FIELDS(field_list)
Declare the fields needed in a structure.
const ast_string_field hrtime
#define ast_mutex_lock(a)
#define ast_vasprintf(ret, fmt, ap)
A wrapper for vasprintf()
const ast_string_field language
static int mssql_connect(void)
static int load_module(void)
Configuration File Parser.
static int mssql_disconnect(void)
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_config_load(filename, flags)
Load a config file.
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
static int execute_and_consume(DBPROCESS *dbproc, const char *fmt,...)
General Asterisk PBX channel definitions.
unsigned int has_userfield
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
#define AST_STRING_FIELD(name)
Declare a string field.
void ast_config_destroy(struct ast_config *config)
Destroys a config.
static int answer(void *data)
#define CONFIG_STATUS_FILEUNCHANGED
Responsible for call detail data.
const char * ast_cdr_disp2str(int disposition)
Disposition to a string.
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is "true". This function checks to see whether a string passed to it is an indication of an "true" value. It checks to see if the string is "yes", "true", "y", "t", "on" or "1".
static int tds_error_handler(DBPROCESS *dbproc, int severity, int dberr, int oserr, char *dberrstr, char *oserrstr)
const ast_string_field username
static int len(struct ast_channel *chan, const char *cmd, char *data, char *buf, size_t buflen)
const ast_string_field password
char * strcasestr(const char *, const char *)
const ast_string_field charset
#define ast_calloc(num, len)
A wrapper for calloc()
Module has failed to load, may be in an inconsistent state.
const ast_string_field database
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...
Structure used to handle boolean flags.
AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_GLOBAL_SYMBOLS|AST_MODFLAG_LOAD_ORDER, "HTTP Phone Provisioning",.support_level=AST_MODULE_SUPPORT_EXTENDED,.load=load_module,.unload=unload_module,.reload=reload,.load_pri=AST_MODPRI_CHANNEL_DEPEND,.requires="http",)
static int unload_module(void)
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
static int tds_load_module(int reload)
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
enum ast_security_event_severity severity
int64_t ast_tvdiff_us(struct timeval end, struct timeval start)
Computes the difference (in microseconds) between two struct timeval instances.
#define ASTERISK_GPL_KEY
The text the key() function should return.
Asterisk module definitions.
static snd_pcm_format_t format
static struct cdr_tds_config * settings
static int tds_message_handler(DBPROCESS *dbproc, DBINT msgno, int msgstate, int severity, char *msgtext, char *srvname, char *procname, int line)
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
DB * dbopen(char *fname, int flags, int mode, DBTYPE type, const void *openinfo) const
#define AST_MUTEX_DEFINE_STATIC(mutex)
static int tds_unload_module(void)
#define ast_mutex_unlock(a)
#define ast_string_field_set(x, field, data)
Set a field to a simple string value.
static void get_date(char *, size_t len, struct timeval)
#define AST_MAX_USER_FIELD