Asterisk - The Open Source Telephony Project  18.5.0
Macros | Enumerations | Functions | Variables
cdr_radius.c File Reference

RADIUS CDR Support. More...

#include "asterisk.h"
#include <radcli/radcli.h>
#include "asterisk/channel.h"
#include "asterisk/cdr.h"
#include "asterisk/module.h"
#include "asterisk/utils.h"
Include dependency graph for cdr_radius.c:

Go to the source code of this file.

Macros

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

Enumerations

enum  { RADIUS_FLAG_USEGMTIME = (1 << 0), RADIUS_FLAG_LOGUNIQUEID = (1 << 1), RADIUS_FLAG_LOGUSERFIELD = (1 << 2) }
 
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
}
 

Functions

static void __reg_module (void)
 
static void __unreg_module (void)
 
struct ast_moduleAST_MODULE_SELF_SYM (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 = "30ef0c93b36035ec78c9cfd712d36d9b", .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, .requires = "cdr", }
 
static const 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 The Radius Client Library http://developer.berlios.de/projects/radiusclient-ng/

Definition in file cdr_radius.c.

Macro Definition Documentation

◆ DATE_FORMAT

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

ISO 8601 standard format

Definition at line 50 of file cdr_radius.c.

Referenced by build_radius_record().

◆ VENDOR_CODE

#define VENDOR_CODE   22736

Definition at line 52 of file cdr_radius.c.

Referenced by build_radius_record().

Enumeration Type Documentation

◆ anonymous enum

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 75 of file cdr_radius.c.

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

◆ anonymous enum

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 54 of file cdr_radius.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 291 of file cdr_radius.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 291 of file cdr_radius.c.

◆ AST_MODULE_SELF_SYM()

struct ast_module* AST_MODULE_SELF_SYM ( void  )

Definition at line 291 of file cdr_radius.c.

◆ build_radius_record()

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

Definition at line 98 of file cdr_radius.c.

References ast_cdr::accountcode, ast_cdr::amaflags, ast_cdr::answer, ast_cdr_disp2str(), ast_channel_amaflags2string(), ast_localtime(), ast_strdupa, 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, NULL, 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, tmp(), ast_cdr::uniqueid, ast_cdr::userfield, and VENDOR_CODE.

Referenced by radius_log().

99 {
100  int recordtype = PW_STATUS_STOP;
101  struct ast_tm tm;
102  char timestr[128];
103  char *tmp;
104 
105  if (!rc_avpair_add(rh, tosend, PW_ACCT_STATUS_TYPE, &recordtype, 0, 0))
106  return -1;
107 
108  /* Account code */
109  if (!rc_avpair_add(rh, tosend, PW_AST_ACCT_CODE, &cdr->accountcode, strlen(cdr->accountcode), VENDOR_CODE))
110  return -1;
111 
112  /* Source */
113  if (!rc_avpair_add(rh, tosend, PW_AST_SRC, &cdr->src, strlen(cdr->src), VENDOR_CODE))
114  return -1;
115 
116  /* Destination */
117  if (!rc_avpair_add(rh, tosend, PW_AST_DST, &cdr->dst, strlen(cdr->dst), VENDOR_CODE))
118  return -1;
119 
120  /* Destination context */
121  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CTX, &cdr->dcontext, strlen(cdr->dcontext), VENDOR_CODE))
122  return -1;
123 
124  /* Caller ID */
125  if (!rc_avpair_add(rh, tosend, PW_AST_CLID, &cdr->clid, strlen(cdr->clid), VENDOR_CODE))
126  return -1;
127 
128  /* Channel */
129  if (!rc_avpair_add(rh, tosend, PW_AST_CHAN, &cdr->channel, strlen(cdr->channel), VENDOR_CODE))
130  return -1;
131 
132  /* Destination Channel */
133  if (!rc_avpair_add(rh, tosend, PW_AST_DST_CHAN, &cdr->dstchannel, strlen(cdr->dstchannel), VENDOR_CODE))
134  return -1;
135 
136  /* Last Application */
137  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_APP, &cdr->lastapp, strlen(cdr->lastapp), VENDOR_CODE))
138  return -1;
139 
140  /* Last Data */
141  if (!rc_avpair_add(rh, tosend, PW_AST_LAST_DATA, &cdr->lastdata, strlen(cdr->lastdata), VENDOR_CODE))
142  return -1;
143 
144 
145  /* Start Time */
146  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
147  ast_localtime(&cdr->start, &tm,
149  if (!rc_avpair_add(rh, tosend, PW_AST_START_TIME, timestr, strlen(timestr), VENDOR_CODE))
150  return -1;
151 
152  /* Answer Time */
153  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
154  ast_localtime(&cdr->answer, &tm,
156  if (!rc_avpair_add(rh, tosend, PW_AST_ANSWER_TIME, timestr, strlen(timestr), VENDOR_CODE))
157  return -1;
158 
159  /* End Time */
160  ast_strftime(timestr, sizeof(timestr), DATE_FORMAT,
161  ast_localtime(&cdr->end, &tm,
163  if (!rc_avpair_add(rh, tosend, PW_AST_END_TIME, timestr, strlen(timestr), VENDOR_CODE))
164  return -1;
165 
166  /* Duration */
167  if (!rc_avpair_add(rh, tosend, PW_AST_DURATION, &cdr->duration, 0, VENDOR_CODE))
168  return -1;
169 
170  /* Billable seconds */
171  if (!rc_avpair_add(rh, tosend, PW_AST_BILL_SEC, &cdr->billsec, 0, VENDOR_CODE))
172  return -1;
173 
174  /* Disposition */
176  if (!rc_avpair_add(rh, tosend, PW_AST_DISPOSITION, tmp, strlen(tmp), VENDOR_CODE))
177  return -1;
178 
179  /* AMA Flags */
181  if (!rc_avpair_add(rh, tosend, PW_AST_AMA_FLAGS, tmp, strlen(tmp), VENDOR_CODE))
182  return -1;
183 
185  /* Unique ID */
186  if (!rc_avpair_add(rh, tosend, PW_AST_UNIQUE_ID, &cdr->uniqueid, strlen(cdr->uniqueid), VENDOR_CODE))
187  return -1;
188  }
189 
191  /* append the user field */
192  if (!rc_avpair_add(rh, tosend, PW_AST_USER_FIELD, &cdr->userfield, strlen(cdr->userfield), VENDOR_CODE))
193  return -1;
194  }
195 
196  /* Setting Acct-Session-Id & User-Name attributes for proper generation
197  * of Acct-Unique-Session-Id on server side
198  */
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:52
char accountcode[AST_MAX_ACCOUNT_CODE]
Definition: cdr.h:308
#define DATE_FORMAT
Definition: cdr_radius.c:50
char dstchannel[AST_MAX_EXTENSION]
Definition: cdr.h:288
#define ast_test_flag(p, flag)
Definition: utils.h:63
static struct ast_flags global_flags
Definition: cdr_radius.c:94
long int billsec
Definition: cdr.h:302
char dcontext[AST_MAX_EXTENSION]
Definition: cdr.h:284
static int tmp()
Definition: bt_open.c:389
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:1739
const char * ast_channel_amaflags2string(enum ama_flags flags)
Convert the enum representation of an AMA flag to a string representation.
Definition: channel.c:4418
#define NULL
Definition: resample.c:96
char lastdata[AST_MAX_EXTENSION]
Definition: cdr.h:292
long int amaflags
Definition: cdr.h:306
#define ast_strdupa(s)
duplicate a string in memory from the stack
Definition: astmm.h:300
static rc_handle * rh
Definition: cdr_radius.c:96
char uniqueid[AST_MAX_UNIQUEID]
Definition: cdr.h:314
char dst[AST_MAX_EXTENSION]
Definition: cdr.h:282
char channel[AST_MAX_EXTENSION]
Definition: cdr.h:286
struct timeval answer
Definition: cdr.h:296
char lastapp[AST_MAX_EXTENSION]
Definition: cdr.h:290
const char * ast_cdr_disp2str(int disposition)
Disposition to a string.
Definition: cdr.c:3430
struct timeval start
Definition: cdr.h:294
long int duration
Definition: cdr.h:300
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:2524
char src[AST_MAX_EXTENSION]
Definition: cdr.h:280
struct timeval end
Definition: cdr.h:298
long int disposition
Definition: cdr.h:304
char clid[AST_MAX_EXTENSION]
Definition: cdr.h:278
char userfield[AST_MAX_USER_FIELD]
Definition: cdr.h:318

◆ load_module()

static int load_module ( void  )
static

Definition at line 246 of file cdr_radius.c.

References ast_cdr_register(), ast_config_destroy(), ast_config_load, ast_copy_string(), ast_log, AST_MODFLAG_LOAD_ORDER, AST_MODPRI_CDR_DRIVER, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_EXTENDED, ast_set2_flag, ast_true(), ast_variable_retrieve(), ASTERISK_GPL_KEY, CONFIG_STATUS_FILEINVALID, LOG_NOTICE, NULL, RADIUS_FLAG_LOGUNIQUEID, RADIUS_FLAG_LOGUSERFIELD, RADIUS_FLAG_USEGMTIME, radius_log(), tmp(), and unload_module().

247 {
248  struct ast_config *cfg;
249  struct ast_flags config_flags = { 0 };
250  const char *tmp;
251 
252  if ((cfg = ast_config_load(cdr_config, config_flags)) && cfg != CONFIG_STATUS_FILEINVALID) {
256  if ((tmp = ast_variable_retrieve(cfg, "radius", "radiuscfg")))
257  ast_copy_string(radiuscfg, tmp, sizeof(radiuscfg));
258  ast_config_destroy(cfg);
259  } else
261 
262  /* read radiusclient-ng config file */
263  if (!(rh = rc_read_config(radiuscfg))) {
264  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng configuration file %s.\n", radiuscfg);
266  }
267 
268  /* read radiusclient-ng dictionaries */
269  if (rc_read_dictionary(rh, rc_conf_str(rh, "dictionary"))) {
270  ast_log(LOG_NOTICE, "Cannot load radiusclient-ng dictionary file.\n");
271  rc_destroy(rh);
272  rh = NULL;
274  }
275 
277  rc_destroy(rh);
278  rh = NULL;
280  } else {
282  }
283 }
static int radius_log(struct ast_cdr *cdr)
Definition: cdr_radius.c:210
#define ast_set2_flag(p, value, flag)
Definition: utils.h:94
static struct ast_flags global_flags
Definition: cdr_radius.c:94
#define CONFIG_STATUS_FILEINVALID
static int tmp()
Definition: bt_open.c:389
static const char name[]
Definition: cdr_radius.c:85
#define NULL
Definition: resample.c:96
static const char cdr_config[]
Definition: cdr_radius.c:86
#define ast_log
Definition: astobj2.c:42
#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.
Definition: cdr.c:2943
void ast_config_destroy(struct ast_config *config)
Destroys a config.
Definition: extconf.c:1290
static rc_handle * rh
Definition: cdr_radius.c:96
static const char desc[]
Definition: cdr_radius.c:84
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".
Definition: main/utils.c:1951
static char radiuscfg[PATH_MAX]
Definition: cdr_radius.c:91
#define LOG_NOTICE
Definition: logger.h:263
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
Structure used to handle boolean flags.
Definition: utils.h:199
const char * ast_variable_retrieve(struct ast_config *config, const char *category, const char *variable)
Definition: main/config.c:694
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401

◆ radius_log()

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(), LOG_ERROR, NULL, and result.

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 NULL
Definition: resample.c:96
#define ast_debug(level,...)
Log a DEBUG message.
Definition: logger.h:452
#define ast_log
Definition: astobj2.c:42
static rc_handle * rh
Definition: cdr_radius.c:96
#define LOG_ERROR
Definition: logger.h:285
static int build_radius_record(VALUE_PAIR **tosend, struct ast_cdr *cdr)
Definition: cdr_radius.c:98
static PGresult * result
Definition: cel_pgsql.c:88

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 233 of file cdr_radius.c.

References ast_cdr_unregister(), and NULL.

Referenced by load_module().

234 {
235  if (ast_cdr_unregister(name)) {
236  return -1;
237  }
238 
239  if (rh) {
240  rc_destroy(rh);
241  rh = NULL;
242  }
243  return 0;
244 }
int ast_cdr_unregister(const char *name)
Unregister a CDR handling engine.
Definition: cdr.c:2988
static const char name[]
Definition: cdr_radius.c:85
#define NULL
Definition: resample.c:96
static rc_handle * rh
Definition: cdr_radius.c:96

Variable Documentation

◆ __mod_info

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 = "30ef0c93b36035ec78c9cfd712d36d9b", .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .load_pri = AST_MODPRI_CDR_DRIVER, .requires = "cdr", }
static

Definition at line 291 of file cdr_radius.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 291 of file cdr_radius.c.

◆ cdr_config

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

Definition at line 86 of file cdr_radius.c.

Referenced by module_config_alloc().

◆ desc

const char desc[] = "RADIUS CDR Backend"
static

Definition at line 84 of file cdr_radius.c.

◆ global_flags

Definition at line 94 of file cdr_radius.c.

◆ name

const char name[] = "radius"
static

Definition at line 85 of file cdr_radius.c.

◆ radiuscfg

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

Definition at line 91 of file cdr_radius.c.

◆ rh

rc_handle* rh = NULL
static

Definition at line 96 of file cdr_radius.c.

Referenced by handle_registrations(), and use_reason_header().