blob: 9465b754bda0c15efd00f66c7eee5b8f9cbbe04e [file] [log] [blame]
Brian Waters13d96012017-12-08 16:53:31 -06001/*********************************************************************************************************
2* Software License Agreement (BSD License) *
3* Author: Sebastien Decugis <sdecugis@freediameter.net> *
4* *
5* Copyright (c) 2011, WIDE Project and NICT *
6* All rights reserved. *
7* *
8* Redistribution and use of this software in source and binary forms, with or without modification, are *
9* permitted provided that the following conditions are met: *
10* *
11* * Redistributions of source code must retain the above *
12* copyright notice, this list of conditions and the *
13* following disclaimer. *
14* *
15* * Redistributions in binary form must reproduce the above *
16* copyright notice, this list of conditions and the *
17* following disclaimer in the documentation and/or other *
18* materials provided with the distribution. *
19* *
20* * Neither the name of the WIDE Project or NICT nor the *
21* names of its contributors may be used to endorse or *
22* promote products derived from this software without *
23* specific prior written permission of WIDE Project and *
24* NICT. *
25* *
26* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
27* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
28* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
29* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT *
30* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
31* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
32* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
33* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
34*********************************************************************************************************/
35
36/* The simple Accounting server for freeDiameter */
37
38#include "app_acct.h"
39
40/* Mandatory AVPs for the Accounting-Answer (any value in adding all the other AVPs?) */
41static struct {
42 struct dict_object * Accounting_Record_Number;
43 struct dict_object * Accounting_Record_Type;
44} acct_dict;
45
46
47/* Callback for incoming Base Accounting Accounting-Request messages */
48static int acct_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act)
49{
50 struct msg * m;
51 struct avp * a = NULL;
52 struct avp_hdr * art=NULL, *arn=NULL; /* We keep a pointer on the Accounting-Record-{Type, Number} AVPs from the query */
53 struct acct_record_list rl;
54
55 TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
56 if (msg == NULL)
57 return EINVAL;
58
59 m = *msg;
60
61 /* Prepare a new record list */
62 CHECK_FCT( acct_rec_prepare( &rl ) );
63
64 /* Maps the AVPs from the query with this record list */
65 CHECK_FCT( acct_rec_map( &rl, m ) );
66
67 /* Check that at least one AVP was mapped */
68 CHECK_FCT( acct_rec_validate( &rl ) );
69
70 /* Now, save these mapped AVPs in the database */
71 CHECK_FCT( acct_db_insert( &rl ) );
72
73 acct_rec_empty( &rl );
74
75 /* OK, we can send a positive reply now */
76
77 /* Get Accounting-Record-{Number,Type} values */
78 CHECK_FCT( fd_msg_search_avp ( m, acct_dict.Accounting_Record_Type, &a) );
79 if (a) {
80 CHECK_FCT( fd_msg_avp_hdr( a, &art ) );
81 }
82 CHECK_FCT( fd_msg_search_avp ( m, acct_dict.Accounting_Record_Number, &a) );
83 if (a) {
84 CHECK_FCT( fd_msg_avp_hdr( a, &arn ) );
85 }
86
87 /* Create the answer message */
88 CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
89 m = *msg;
90
91 /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */
92 CHECK_FCT( fd_msg_rescode_set( m, "DIAMETER_SUCCESS", NULL, NULL, 1 ) );
93
94 /* Add the mandatory AVPs in the ACA */
95 if (art) {
96 CHECK_FCT( fd_msg_avp_new ( acct_dict.Accounting_Record_Type, 0, &a ) );
97 CHECK_FCT( fd_msg_avp_setvalue( a, art->avp_value ) );
98 CHECK_FCT( fd_msg_avp_add( m, MSG_BRW_LAST_CHILD, a ) );
99 }
100 if (arn) {
101 CHECK_FCT( fd_msg_avp_new ( acct_dict.Accounting_Record_Number, 0, &a ) );
102 CHECK_FCT( fd_msg_avp_setvalue( a, arn->avp_value ) );
103 CHECK_FCT( fd_msg_avp_add( m, MSG_BRW_LAST_CHILD, a ) );
104 }
105
106 /* Send the answer */
107 *act = DISP_ACT_SEND;
108 return 0;
109}
110
111
112/* entry point */
113static int acct_entry(char * conffile)
114{
115 struct disp_when data;
116
117 TRACE_ENTRY("%p", conffile);
118
119#ifndef TEST_DEBUG /* We do this differently in the test scenario */
120 /* Initialize the configuration and parse the file */
121 CHECK_FCT( acct_conf_init() );
122 CHECK_FCT( acct_conf_parse(conffile) );
123 CHECK_FCT( acct_conf_check(conffile) );
124#endif /* TEST_DEBUG */
125
126 /* Now initialize the database module */
127 CHECK_FCT( acct_db_init() );
128
129 /* Search the AVPs we will need in this file */
130 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Accounting-Record-Number", &acct_dict.Accounting_Record_Number, ENOENT) );
131 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Accounting-Record-Type", &acct_dict.Accounting_Record_Type, ENOENT) );
132
133 /* Register the dispatch callbacks */
134 memset(&data, 0, sizeof(data));
135 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Base Accounting", &data.app, ENOENT) );
136 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Accounting-Request", &data.command, ENOENT) );
137 CHECK_FCT( fd_disp_register( acct_cb, DISP_HOW_CC, &data, NULL, NULL ) );
138
139 /* Advertise the support for the Diameter Base Accounting application in the peer */
140 CHECK_FCT( fd_disp_app_support ( data.app, NULL, 0, 1 ) );
141
142 return 0;
143}
144
145/* Unload */
146void fd_ext_fini(void)
147{
148 /* Close the db connection */
149 acct_db_free();
150
151 /* Destroy the configuration */
152 acct_conf_free();
153}
154
155EXTENSION_ENTRY("app_acct", acct_entry);