Debian Codegraph @

lib/freeDiameter-1.2.1/extensions/test_acct/test_acct.c

raw
1 /*********************************************************************************************************
2 * Software License Agreement (BSD License) *
3 * Author: Sebastien Decugis <[email protected]> *
4 * *
5 * Copyright (c) 2013, 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 /* This extension simply receives ACR and sends positive ACA after displaying the content,
37 but does not commit the data to any storage */
38
39 #include <freeDiameter/extension.h>
40
41 static struct {
42 struct dict_object * Accounting_Record_Number;
43 struct dict_object * Accounting_Record_Type;
44 } tac_dict;
45
46 /* Callback for incoming Base Accounting application messages */
47 static int tac_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * data, enum disp_action * act)
48 {
49 struct msg_hdr *hdr = NULL;
50
51 TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
52
53 if (msg == NULL)
54 return EINVAL;
55
56 /* Check what we received */
57 CHECK_FCT( fd_msg_hdr(*msg, &hdr) );
58
59 if (hdr->msg_flags & CMD_FLAG_REQUEST) {
60 /* It was a request, create an answer */
61 struct msg *ans, *qry;
62 struct avp * a = NULL;
63 struct avp_hdr * h = NULL;
64 os0_t s;
65 size_t sl;
66
67 qry = *msg;
68 /* Create the answer message, including the Session-Id AVP */
69 CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
70 ans = *msg;
71
72 /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */
73 CHECK_FCT( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ) );
74
75 fd_log_debug("--------------Received the following Accounting message:--------------");
76
77 CHECK_FCT( fd_sess_getsid ( sess, &s, &sl ) );
78 fd_log_debug("Session: %.*s", (int)sl, s);
79
80 /* The AVPs that we copy in the answer */
81 CHECK_FCT( fd_msg_search_avp ( qry, tac_dict.Accounting_Record_Type, &a) );
82 if (a) {
83 CHECK_FCT( fd_msg_avp_hdr( a, &h ) );
84 fd_log_debug("Accounting-Record-Type: %d (%s)", h->avp_value->u32,
85 /* it would be better to search this in the dictionary, but it is only for debug, so ok */
86 (h->avp_value->u32 == 1) ? "EVENT_RECORD" :
87 (h->avp_value->u32 == 2) ? "START_RECORD" :
88 (h->avp_value->u32 == 3) ? "INTERIM_RECORD" :
89 (h->avp_value->u32 == 4) ? "STOP_RECORD" :
90 "<unknown value>"
91 );
92 CHECK_FCT( fd_msg_avp_new ( tac_dict.Accounting_Record_Type, 0, &a ) );
93 CHECK_FCT( fd_msg_avp_setvalue( a, h->avp_value ) );
94 CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, a ) );
95 }
96 CHECK_FCT( fd_msg_search_avp ( qry, tac_dict.Accounting_Record_Number, &a) );
97 if (a) {
98 CHECK_FCT( fd_msg_avp_hdr( a, &h ) );
99 fd_log_debug("Accounting-Record-Number: %d", h->avp_value->u32);
100 CHECK_FCT( fd_msg_avp_new ( tac_dict.Accounting_Record_Number, 0, &a ) );
101 CHECK_FCT( fd_msg_avp_setvalue( a, h->avp_value ) );
102 CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, a ) );
103 }
104
105 /* We may also dump other data from the message, such as Accounting session Id, number of packets, ... */
106
107 fd_log_debug("----------------------------------------------------------------------");
108
109 /* Send the answer */
110 CHECK_FCT( fd_msg_send( msg, NULL, NULL ) );
111
112 } else {
113 /* We received an answer message, just discard it */
114 CHECK_FCT( fd_msg_free( *msg ) );
115 *msg = NULL;
116 }
117
118 return 0;
119 }
120
121 /* entry point: register handler for Base Accounting messages in the daemon */
122 static int tac_entry(char * conffile)
123 {
124 struct disp_when data;
125
126 TRACE_ENTRY("%p", conffile);
127
128 memset(&tac_dict, 0, sizeof(tac_dict));
129 memset(&data, 0, sizeof(data));
130
131 /* Initialize the dictionary objects we use */
132 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Base Accounting", &data.app, ENOENT) );
133 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Accounting-Record-Number", &tac_dict.Accounting_Record_Number, ENOENT) );
134 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Accounting-Record-Type", &tac_dict.Accounting_Record_Type, ENOENT) );
135
136 /* Register the dispatch callback */
137 CHECK_FCT( fd_disp_register( tac_cb, DISP_HOW_APPID, &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 EXTENSION_ENTRY("test_acct", tac_entry);