comparison extensions/test_cc/test_cc.c @ 1365:0d951a67648b

test_cc: improve Add all necessary fields. Copy relevant data from request.
author Thomas Klausner <tk@giga.or.at>
date Sun, 09 Jun 2019 15:43:24 +0200
parents 25f86f4cdde8
children 6b1a2405cebb
comparison
equal deleted inserted replaced
1364:a37692441c4d 1365:0d951a67648b
33 33
34 #include <freeDiameter/extension.h> 34 #include <freeDiameter/extension.h>
35 35
36 struct disp_hdl *ccr_handler_hdl; 36 struct disp_hdl *ccr_handler_hdl;
37 37
38 struct dict_object * aai_avp_do; /* cache the Auth-Application-Id dictionary object */
39 struct dict_object * crn_avp_do; /* cache the CC-Request-Number dictionary object */
40 struct dict_object * crt_avp_do; /* cache the CC-Request-Type dictionary object */
41
42 #define MODULE_NAME "test_cc"
43
38 static int ccr_handler(struct msg ** msg, struct avp * avp, struct session * sess, void * data, enum disp_action * act) 44 static int ccr_handler(struct msg ** msg, struct avp * avp, struct session * sess, void * data, enum disp_action * act)
39 { 45 {
40 struct msg_hdr *hdr = NULL; 46 struct msg_hdr *hdr = NULL;
41 47
42 TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act); 48 TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
48 if(hdr->msg_flags & CMD_FLAG_REQUEST) { 54 if(hdr->msg_flags & CMD_FLAG_REQUEST) {
49 /* Request received, answer it */ 55 /* Request received, answer it */
50 struct msg *answer; 56 struct msg *answer;
51 os0_t s; 57 os0_t s;
52 size_t sl; 58 size_t sl;
59 struct avp *avp;
60 union avp_value val;
61 struct avp *avp_data;
62 struct avp_hdr *ahdr;
63 uint32_t crt, crn;
64
65 /* get some necessary information from request */
66 if (fd_msg_search_avp(*msg, crt_avp_do, &avp_data) < 0 || avp_data == NULL) {
67 fd_log_error("[%s] CC-Request-Type not found in CCR", MODULE_NAME);
68 return 0;
69 }
70 if (fd_msg_avp_hdr(avp_data, &ahdr) < 0) {
71 fd_log_error("[%s] error parsing CC-Request-Type in CCR", MODULE_NAME);
72 return 0;
73 }
74 crt = ahdr->avp_value->i32;
75 if (fd_msg_search_avp(*msg, crn_avp_do, &avp_data) < 0 || avp_data == NULL) {
76 fd_log_error("[%s] CC-Request-Number not found in CCR", MODULE_NAME);
77 return 0;
78 }
79 if (fd_msg_avp_hdr(avp_data, &ahdr) < 0) {
80 fd_log_error("[%s] error parsing CC-Request-Type in CCR", MODULE_NAME);
81 return 0;
82 }
83 crn = ahdr->avp_value->i32;
53 84
54 /* Create the answer message */ 85 /* Create the answer message */
55 CHECK_FCT(fd_msg_new_answer_from_req(fd_g_config->cnf_dict, msg, 0)); 86 CHECK_FCT(fd_msg_new_answer_from_req(fd_g_config->cnf_dict, msg, 0));
56 answer = *msg; 87 answer = *msg;
57 /* TODO copy/fill in AVPs in the answer */ 88
89 /* TODO copy/fill in more AVPs in the answer */
90
91 /* Auth-Application-Id */
92 fd_msg_avp_new(aai_avp_do, 0, &avp);
93 memset(&val, 0, sizeof(val));
94 val.i32 = 4;
95 if (fd_msg_avp_setvalue(avp, &val) != 0) {
96 fd_msg_free(answer);
97 fd_log_error("can't set value for 'Auth-Application-Id' for 'Credit-Control-Request' message");
98 return 0;
99 }
100 fd_msg_avp_add(answer, MSG_BRW_LAST_CHILD, avp);
101
102 /* CC-Request-Type */
103 fd_msg_avp_new(crt_avp_do, 0, &avp);
104 memset(&val, 0, sizeof(val));
105 val.i32 = crt;
106 if (fd_msg_avp_setvalue(avp, &val) != 0) {
107 fd_msg_free(answer);
108 fd_log_error("can't set value for 'CC-Request-Type' for 'Credit-Control-Request' message");
109 return 0;
110 }
111 fd_msg_avp_add(answer, MSG_BRW_LAST_CHILD, avp);
112
113 /* CC-Request-Number */
114 fd_msg_avp_new(crn_avp_do, 0, &avp);
115 memset(&val, 0, sizeof(val));
116 val.i32 = crn;
117 if (fd_msg_avp_setvalue(avp, &val) != 0) {
118 fd_msg_free(answer);
119 fd_log_error("can't set value for 'CC-Request-Number' for 'Credit-Control-Request' message");
120 return 0;
121 }
122 fd_msg_avp_add(answer, MSG_BRW_LAST_CHILD, avp);
123
58 /* TODO make result configurable (depending on an AVP?) */ 124 /* TODO make result configurable (depending on an AVP?) */
59 CHECK_FCT(fd_msg_rescode_set(answer, "DIAMETER_SUCCESS", NULL, NULL, 1)); 125 CHECK_FCT(fd_msg_rescode_set(answer, "DIAMETER_SUCCESS", NULL, NULL, 1));
60 126
61 fd_log_notice("--------------Received the following Credit Control Request:--------------"); 127 fd_log_debug("--------------Received the following Credit Control Request:--------------");
62 128
63 CHECK_FCT(fd_sess_getsid(sess, &s, &sl)); 129 CHECK_FCT(fd_sess_getsid(sess, &s, &sl));
64 fd_log_notice("Session: %.*s",(int)sl, s); 130 fd_log_debug("Session: %.*s",(int)sl, s);
65 131
66 fd_log_notice("----------------------------------------------------------------------"); 132 fd_log_debug("----------------------------------------------------------------------");
67 133
68 /* Send the answer */ 134 /* Send the answer */
69 CHECK_FCT(fd_msg_send(msg, NULL, NULL)); 135 CHECK_FCT(fd_msg_send(msg, NULL, NULL));
70 136 fd_log_debug("reply sent");
71 } else { 137 } else {
72 /* We received an answer message, just discard it */ 138 /* We received an answer message, just discard it */
73 CHECK_FCT(fd_msg_free(*msg)); 139 CHECK_FCT(fd_msg_free(*msg));
74 *msg = NULL; 140 *msg = NULL;
75 } 141 }
82 { 148 {
83 struct disp_when data; 149 struct disp_when data;
84 150
85 TRACE_ENTRY("%p", conffile); 151 TRACE_ENTRY("%p", conffile);
86 152
153 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &aai_avp_do, ENOENT),
154 { LOG_E("Unable to find 'Auth-Application-Id' AVP in the loaded dictionaries."); });
155 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "CC-Request-Number", &crn_avp_do, ENOENT),
156 { LOG_E("Unable to find 'CC-Request-Number' AVP in the loaded dictionaries."); });
157 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "CC-Request-Type", &crt_avp_do, ENOENT),
158 { LOG_E("Unable to find 'CC-Request-Type' AVP in the loaded dictionaries."); });
159
87 memset(&data, 0, sizeof(data)); 160 memset(&data, 0, sizeof(data));
88 161
89 /* Advertise the support for the Diameter Credit Control application in the peer */ 162 /* Advertise the support for the Diameter Credit Control application in the peer */
90 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Credit Control Application", &data.app, ENOENT) ); 163 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Credit Control Application", &data.app, ENOENT) );
91 CHECK_FCT( fd_disp_app_support ( data.app, NULL, 1, 0 ) ); 164 CHECK_FCT( fd_disp_app_support ( data.app, NULL, 1, 0 ) );
95 CHECK_FCT( fd_disp_register( ccr_handler, DISP_HOW_CC, &data, NULL, &ccr_handler_hdl ) ); 168 CHECK_FCT( fd_disp_register( ccr_handler, DISP_HOW_CC, &data, NULL, &ccr_handler_hdl ) );
96 169
97 return 0; 170 return 0;
98 } 171 }
99 172
100 EXTENSION_ENTRY("test_cc", cc_entry); 173 EXTENSION_ENTRY(MODULE_NAME, cc_entry);
"Welcome to our mercurial repository"