comparison extensions/test_acct/test_acct.c @ 279:a26655da5c14

Added a dummy accounting server for tests
author Sebastien Decugis <sdecugis@nict.go.jp>
date Mon, 26 Apr 2010 17:41:53 +0900
parents
children 3db90f9ce42f
comparison
equal deleted inserted replaced
278:50bfb29bf036 279:a26655da5c14
1 /*********************************************************************************************************
2 * Software License Agreement (BSD License) *
3 * Author: Sebastien Decugis <sdecugis@nict.go.jp> *
4 * *
5 * Copyright (c) 2010, 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, 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 fd_log_debug("--------------Received the following Accounting message:--------------\n");
57 fd_msg_dump_walk(NONE, *msg);
58 fd_log_debug("----------------------------------------------------------------------\n");
59
60 /* Now check what we received */
61 CHECK_FCT( fd_msg_hdr(*msg, &hdr) );
62
63 if (hdr->msg_flags & CMD_FLAG_REQUEST) {
64 /* It was a request, create an answer */
65 struct msg *ans, *qry;
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 /* Search for Accounting required AVPs */
76 {
77 struct avp * a = NULL;
78 struct avp_hdr * h = NULL;
79
80 CHECK_FCT( fd_msg_search_avp ( qry, tac_dict.Accounting_Record_Number, &a) );
81 if (a) {
82 CHECK_FCT( fd_msg_avp_hdr( a, &h ) );
83 CHECK_FCT( fd_msg_avp_new ( tac_dict.Accounting_Record_Number, 0, &a ) );
84 CHECK_FCT( fd_msg_avp_setvalue( a, h->avp_value ) );
85 CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, a ) );
86 }
87
88 CHECK_FCT( fd_msg_search_avp ( qry, tac_dict.Accounting_Record_Type, &a) );
89 if (a) {
90 CHECK_FCT( fd_msg_avp_hdr( a, &h ) );
91 CHECK_FCT( fd_msg_avp_new ( tac_dict.Accounting_Record_Type, 0, &a ) );
92 CHECK_FCT( fd_msg_avp_setvalue( a, h->avp_value ) );
93 CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, a ) );
94 }
95 }
96
97 /* Send the answer */
98 CHECK_FCT( fd_msg_send( msg, NULL, NULL ) );
99
100 } else {
101 /* We received an answer message, just discard it */
102 CHECK_FCT( fd_msg_free( *msg ) );
103 *msg = NULL;
104 }
105
106 return 0;
107 }
108
109 /* entry point: register handler for Base Accounting messages in the daemon */
110 static int tac_entry(char * conffile)
111 {
112 struct disp_when data;
113
114 TRACE_ENTRY("%p", conffile);
115
116 memset(&tac_dict, 0, sizeof(tac_dict));
117 memset(&data, 0, sizeof(data));
118
119 /* Initialize the dictionary objects we use */
120 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Base Accounting", &data.app, ENOENT) );
121 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Accounting-Record-Number", &tac_dict.Accounting_Record_Number, ENOENT) );
122 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Accounting-Record-Type", &tac_dict.Accounting_Record_Type, ENOENT) );
123
124 /* Register the dispatch callback */
125 CHECK_FCT( fd_disp_register( tac_cb, DISP_HOW_APPID, &data, NULL ) );
126
127 /* Advertise the support for the Diameter Base Accounting application in the peer */
128 CHECK_FCT( fd_disp_app_support ( data.app, NULL, 0, 1 ) );
129
130 return 0;
131 }
132
133 EXTENSION_ENTRY("test_acct", tac_entry);
"Welcome to our mercurial repository"