Wed Jan 8 2020 09:49:59

Asterisk developer's documentation


cdr_radius.c File Reference

RADIUS CDR Support. More...

#include "asterisk.h"
#include <radiusclient-ng.h>
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"

Go to the source code of this file.

Macros

#define DATE_FORMAT   "%Y-%m-%d %T %z"
 
#define VENDOR_CODE   22736
 

Enumerations

enum  {
  PW_AST_ACCT_CODE = 101, PW_AST_SRC = 102, PW_AST_DST = 103, PW_AST_DST_CTX = 104,
  PW_AST_CLID = 105, PW_AST_CHAN = 106, PW_AST_DST_CHAN = 107, PW_AST_LAST_APP = 108,
  PW_AST_LAST_DATA = 109, PW_AST_START_TIME = 110, PW_AST_ANSWER_TIME = 111, PW_AST_END_TIME = 112,
  PW_AST_DURATION = 113, PW_AST_BILL_SEC = 114, PW_AST_DISPOSITION = 115, PW_AST_AMA_FLAGS = 116,
  PW_AST_UNIQUE_ID = 117, PW_AST_USER_FIELD = 118
}
 
enum  { RADIUS_FLAG_USEGMTIME = (1 << 0), RADIUS_FLAG_LOGUNIQUEID = (1 << 1), RADIUS_FLAG_LOGUSERFIELD = (1 << 2) }
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
static int build_radius_record (VALUE_PAIR **tosend, struct ast_cdr *cdr)
 
static int load_module (void)
 
static int radius_log (struct ast_cdr *cdr)
 
static int unload_module (void)
 

Variables

static struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "RADIUS CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, }
 
static struct ast_module_infoast_module_info = &__mod_info
 
static const char cdr_config [] = "cdr.conf"
 
static const char desc [] = "RADIUS CDR Backend"
 
static struct ast_flags global_flags = { RADIUS_FLAG_USEGMTIME | RADIUS_FLAG_LOGUNIQUEID | RADIUS_FLAG_LOGUSERFIELD }
 
static const char name [] = "radius"
 
static char radiuscfg [PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf"
 
static rc_handle * rh = NULL
 

Detailed Description

RADIUS CDR Support.

Author
Philippe Sultan
ExtRef:
The Radius Client Library - http://developer.berlios.de/projects/radiusclient-ng/

Definition in file cdr_radius.c.

Macro Definition Documentation

#define DATE_FORMAT   "%Y-%m-%d %T %z"

ISO 8601 standard format

Definition at line 51 of file cdr_radius.c.

Referenced by build_radius_record().

#define VENDOR_CODE   22736

Definition at line 53 of file cdr_radius.c.

Referenced by build_radius_record().

Enumeration Type Documentation

anonymous enum
Enumerator
PW_AST_ACCT_CODE 
PW_AST_SRC 
PW_AST_DST 
PW_AST_DST_CTX 
PW_AST_CLID 
PW_AST_CHAN 
PW_AST_DST_CHAN 
PW_AST_LAST_APP 
PW_AST_LAST_DATA 
PW_AST_START_TIME 
PW_AST_ANSWER_TIME 
PW_AST_END_TIME 
PW_AST_DURATION 
PW_AST_BILL_SEC 
PW_AST_DISPOSITION 
PW_AST_AMA_FLAGS 
PW_AST_UNIQUE_ID 
PW_AST_USER_FIELD 

Definition at line 55 of file cdr_radius.c.

anonymous enum
Enumerator
RADIUS_FLAG_USEGMTIME 

Log dates and times in UTC

RADIUS_FLAG_LOGUNIQUEID 

Log Unique ID

RADIUS_FLAG_LOGUSERFIELD 

Log User Field

Definition at line 76 of file cdr_radius.c.

76  {
77  /*! Log dates and times in UTC */
78  RADIUS_FLAG_USEGMTIME = (1 << 0),
79  /*! Log Unique ID */
80  RADIUS_FLAG_LOGUNIQUEID = (1 << 1),
81  /*! Log User Field */
82  RADIUS_FLAG_LOGUSERFIELD = (1 << 2)
83 };

Function Documentation

static void __reg_module ( void  )
static

Definition at line 298 of file cdr_radius.c.

static void __unreg_module ( void  )
static

Definition at line 298 of file cdr_radius.c.

static int build_radius_record ( VALUE_PAIR **  tosend,
struct ast_cdr cdr 
)
static

Definition at line 99 of file cdr_radius.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_cdr_disp2str(), ast_cdr_flags2str(), ast_localtime(), ast_strftime(), ast_test_flag, ast_cdr::billsec, ast_cdr::channel, ast_cdr::clid, DATE_FORMAT, ast_cdr::dcontext, ast_cdr::disposition, ast_cdr::dst, ast_cdr::dstchannel, ast_cdr::duration, ast_cdr::end, ast_cdr::lastapp, ast_cdr::lastdata, PW_AST_ACCT_CODE, PW_AST_AMA_FLAGS, PW_AST_ANSWER_TIME, PW_AST_BILL_SEC, PW_AST_CHAN, PW_AST_CLID, PW_AST_DISPOSITION, PW_AST_DST, PW_AST_DST_CHAN, PW_AST_DST_CTX, PW_AST_DURATION, PW_AST_END_TIME, PW_AST_LAST_APP, PW_AST_LAST_DATA, PW_AST_SRC, PW_AST_START_TIME, PW_AST_UNIQUE_ID, PW_AST_USER_FIELD, RADIUS_FLAG_LOGUNIQUEID, RADIUS_FLAG_LOGUSERFIELD, RADIUS_FLAG_USEGMTIME, ast_cdr::src, ast_cdr::start, ast_cdr::uniqueid, ast_cdr::userfield, and VENDOR_CODE.

Referenced by radius_log().

100 {
101  int recordtype = PW_STATUS_STOP;
102  struct ast_tm tm;
103  char timestr[128];
104  char *tmp;
105 
106  if (!rc_avpair_add(rh, tosend, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
107  return -1;
108 
109  /* Account code */
110  if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
111  return -1;
112 
113  /* Source */
114  if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
115  return -1;
116 
117  /* Destination */
118  if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
119  return -1;
120 
121  /* Destination context */
122  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
123  return -1;
124 
125  /* Caller ID */
126  if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
127  return -1;
128 
129  /* Channel */
130  if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
131  return -1;
132 
133  /* Destination Channel */
134  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
135  return -1;
136 
137  /* Last Application */
138  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
139  return -1;
140 
141  /* Last Data */
142  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
143  return -1;
144 
145 
146  /* Start Time */
147  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
148  ast_localtime(&cdr->start, &tm,
150  if (!rc_avpair_add(rh, tosend, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
151  return -1;
152 
153  /* Answer Time */
154  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
155  ast_localtime(&cdr->answer, &tm,
157  if (!rc_avpair_add(rh, tosend, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
158  return -1;
159 
160  /* End Time */
161  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
162  ast_localtime(&cdr->end, &tm,
164  if (!rc_avpair_add(rh, tosend, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
165  return -1;
166 
167  /* Duration */
168  if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
169  return -1;
170 
171  /* Billable seconds */
172  if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
173  return -1;
174 
175  /* Disposition */
176  tmp = ast_cdr_disp2str(cdr->disposition);
177  if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
178  return -1;
179 
180  /* AMA Flags */
181  tmp = ast_cdr_flags2str(cdr->amaflags);
182  if (!rc_avpair_add(rh, tosend, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
183  return -1;
184 
186  /* Unique ID */
187  if (!rc_avpair_add(rh, tosend, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
188  return -1;
189  }
190 
192  /* append the user field */
193  if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
194  return -1;
195  }
196 
197  /* Setting Acct-Session-Id & User-Name attributes for proper generation
198  of Acct-Unique-Session-Id on server side */
199  /* Channel */
200  if (!rc_avpair_add(rh, tosend, PW_USER_NAME, &cdr->channel, strlen(cdr->channel), 0))
201  return -1;
202 
203  /* Unique ID */
204  if (!rc_avpair_add(rh, tosend, PW_ACCT_SESSION_ID, &cdr->uniqueid, strlen(cdr->uniqueid), 0))
205  return -1;
206 
207  return 0;
208 }
#define VENDOR_CODE
Definition: cdr_radius.c:53
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:114
#define DATE_FORMAT
Definition: cdr_radius.c:51
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:94
#define ast_test_flag(p, flag)
Definition: utils.h:63
static struct ast_flags global_flags
Definition: cdr_radius.c:95
long int billsec
Definition: cdr.h:108
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:90
struct ast_tm * ast_localtime(const struct timeval *timep, struct ast_tm *p_tm, const char *zone)
Timezone-independent version of localtime_r(3).
Definition: localtime.c:1570
char uniqueid[150]
Definition: cdr.h:121
char * ast_cdr_flags2str(int flags)
Definition: cdr.c:977
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:98
long int amaflags
Definition: cdr.h:112
static rc_handle * rh
Definition: cdr_radius.c:97
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:88
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:92
struct timeval answer
Definition: cdr.h:102
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:96
struct timeval start
Definition: cdr.h:100
long int duration
Definition: cdr.h:106
int ast_strftime(char *buf, size_t len, const char *format, const struct ast_tm *tm)
Special version of strftime(3) that handles fractions of a second. Takes the same arguments as strfti...
Definition: localtime.c:2351
char src[AST_MAX_EXTENSION]
Definition: cdr.h:86
struct timeval end
Definition: cdr.h:104
long int disposition
Definition: cdr.h:110
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:84
char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:959
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:125
static int load_module ( void  )
static

Definition at line 243 of file cdr_radius.c.

References ast_cdr_register(), ast_config_destroy(), ast_config_load, ast_copy_string(), ast_log(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, ast_set2_flag, ast_strdup, ast_true(), ast_variable_retrieve(), CONFIG_STATUS_FILEINVALID, LOG_NOTICE, RADIUS_FLAG_LOGUNIQUEID, RADIUS_FLAG_LOGUSERFIELD, RADIUS_FLAG_USEGMTIME, and radius_log().

244 {
245  struct ast_config *cfg;
246  struct ast_flags config_flags = { 0 };
247  const char *tmp;
248 
249  if ((cfg = ast_config_load(cdr_config, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
253  if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
254  ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
255  ast_config_destroy(cfg);
256  } else
258 
259  /*
260  * start logging
261  *
262  * NOTE: Yes this causes a slight memory leak if the module is
263  * unloaded. However, it is better than a crash if cdr_radius
264  * and cel_radius are both loaded.
265  */
266  tmp = ast_strdup("asterisk");
267  if (tmp) {
268  rc_openlog((char *) tmp);
269  }
270 
271  /* read radiusclient-ng config file */
272  if (!(rh = rc_read_config(radiuscfg))) {
273  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
275  }
276 
277  /* read radiusclient-ng dictionaries */
278  if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
279  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
280  rc_destroy(rh);
281  rh = NULL;
283  }
284 
286  rc_destroy(rh);
287  rh = NULL;
289  } else {
291  }
292 }
static int radius_log(struct ast_cdr *cdr)
Definition: cdr_radius.c:210
const char * ast_variable_retrieve(const struct ast_config *config, const char *category, const char *variable)
Gets a variable.
Definition: config.c:625
#define ast_strdup(a)
Definition: astmm.h:109
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
static struct ast_flags global_flags
Definition: cdr_radius.c:95
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: config.c:1037
int ast_cdr_register(const char *name, const char *desc, ast_cdrbe be)
Register a CDR handling engine.
Definition: cdr.c:130
#define ast_config_load(filename, flags)
Load a config file.
Definition: config.h:170
static rc_handle * rh
Definition: cdr_radius.c:97
static const char desc[]
Definition: cdr_radius.c:85
int attribute_pure ast_true(const char *val)
Make sure something is true. Determine if a string containing a boolean value is &quot;true&quot;. This function checks to see whether a string passed to it is an indication of an &quot;true&quot; value. It checks to see if the string is &quot;yes&quot;, &quot;true&quot;, &quot;y&quot;, &quot;t&quot;, &quot;on&quot; or &quot;1&quot;.
Definition: utils.c:1533
static char radiuscfg[PATH_MAX]
Definition: cdr_radius.c:92
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
#define LOG_NOTICE
Definition: logger.h:133
static const char name[]
Structure used to handle boolean flags.
Definition: utils.h:200
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:223
#define CONFIG_STATUS_FILEINVALID
Definition: config.h:52
static int radius_log ( struct ast_cdr cdr)
static

Definition at line 210 of file cdr_radius.c.

References ast_debug, ast_log(), build_radius_record(), and LOG_ERROR.

Referenced by load_module().

211 {
212  int result = ERROR_RC;
213  VALUE_PAIR *tosend = NULL;
214 
215  if (build_radius_record(&tosend, cdr)) {
216  ast_debug(1, "Unable to create RADIUS record. CDR not recorded!\n");
217  goto return_cleanup;
218  }
219 
220  result = rc_acct(rh, 0, tosend);
221  if (result != OK_RC) {
222  ast_log(LOG_ERROR, "Failed to record Radius CDR record!\n");
223  }
224 
225 return_cleanup:
226  if (tosend) {
227  rc_avpair_free(tosend);
228  }
229 
230  return result;
231 }
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:236
static rc_handle * rh
Definition: cdr_radius.c:97
#define LOG_ERROR
Definition: logger.h:155
static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
Definition: cdr_radius.c:99
void ast_log(int level, const char *file, int line, const char *function, const char *fmt,...)
Used for sending a log message This is the standard logger function. Probably the only way you will i...
Definition: logger.c:1207
static int unload_module ( void  )
static

Definition at line 233 of file cdr_radius.c.

References ast_cdr_unregister().

234 {
236  if (rh) {
237  rc_destroy(rh);
238  rh = NULL;
239  }
240  return 0;
241 }
static rc_handle * rh
Definition: cdr_radius.c:97
static const char name[]
void ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:165

Variable Documentation

struct ast_module_info __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_LOAD_ORDER , .description = "RADIUS CDR Backend" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = "ac1f6a56484a8820659555499174e588" , .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, }
static

Definition at line 298 of file cdr_radius.c.

Definition at line 298 of file cdr_radius.c.

const char cdr_config[] = "cdr.conf"
static

Definition at line 87 of file cdr_radius.c.

Definition at line 95 of file cdr_radius.c.

const char name[] = "radius"
static

Definition at line 86 of file cdr_radius.c.

char radiuscfg[PATH_MAX] = "/etc/radiusclient-ng/radiusclient.conf"
static

Definition at line 92 of file cdr_radius.c.

rc_handle* rh = NULL
static

Definition at line 97 of file cdr_radius.c.

Referenced by handle_response().