comparison extensions/loadtest_cc/loadtest_cc.c @ 1364:a37692441c4d

Snapshot commit of loadtest_cc extension, does not work.
author Thomas Klausner <tk@giga.or.at>
date Fri, 07 Jun 2019 14:27:30 +0200
parents
children 9294ed1c7ac8
comparison
equal deleted inserted replaced
1363:1bc02b9ecbd6 1364:a37692441c4d
1 /**********************************************************************************************************
2 * Software License Agreement(BSD License) *
3 * Author: Thomas Klausner <tk@giga.or.at> *
4 * *
5 * Copyright(c) 2019, Thomas Klausner *
6 * All rights reserved. *
7 * *
8 * Written under contract by nfotex IT GmbH, http://nfotex.com/ *
9 * *
10 * Redistribution and use of this software in source and binary forms, with or without modification, are *
11 * permitted provided that the following conditions are met: *
12 * *
13 * * Redistributions of source code must retain the above *
14 * copyright notice, this list of conditions and the *
15 * following disclaimer. *
16 * *
17 * * Redistributions in binary form must reproduce the above *
18 * copyright notice, this list of conditions and the *
19 * following disclaimer in the documentation and/or other *
20 * materials provided with the distribution. *
21 * *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
23 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
24 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT *
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS *
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
28 * TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF *
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
30 **********************************************************************************************************/
31
32 /* This extension waits for a signal (SIGUSR2). When it gets it, it
33 * generates messages as quickly as possible to the configured target
34 * host; a second SIGUSR2 signal will stop this */
35
36 #include <freeDiameter/extension.h>
37
38 #include <pthread.h>
39 #include <signal.h>
40
41 #define MODULE_NAME "loadtest_cc"
42
43 static pthread_t gen_thr = (pthread_t)NULL;
44 struct disp_hdl *ccr_handler_hdl;
45 volatile int do_generate = 0;
46
47 const char *target = NULL;
48
49 struct dict_object * aai_avp_do; /* cache the Auth-Application-Id dictionary object */
50 struct dict_object * dh_avp_do; /* cache the Destination-Host dictionary object */
51 struct dict_object * dr_avp_do; /* cache the Destination-Realm dictionary object */
52 struct dict_object * rc_avp_do; /* cache the Result-Code dictionary object */
53 struct dict_object * si_avp_do; /* cache the Session-Id dictionary object */
54 struct dict_object * pi_avp_do; /* cache the Proxy-Info dictionary object */
55 struct dict_object * ph_avp_do; /* cache the Proxy-Host dictionary object */
56 struct dict_object * ps_avp_do; /* cache the Proxy-State dictionary object */
57
58
59 static int ccr_handler(struct msg ** msg, struct avp * avp, struct session * sess, void * data, enum disp_action * act)
60 {
61 struct msg_hdr *hdr = NULL;
62
63 TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
64
65 if (msg == NULL)
66 return EINVAL;
67
68 CHECK_FCT(fd_msg_hdr(*msg, &hdr));
69 if (hdr->msg_flags & CMD_FLAG_REQUEST) {
70 fd_log_error("received Credit-Control-Request, dropping it");
71 CHECK_FCT(fd_msg_free(*msg));
72 *msg = NULL;
73 return 0;
74 }
75
76 /* Answer received, check it */
77 fd_log_notice("Credit-Control-Answer received, this code should do something about it but doesn't yet");
78 /* TODO */
79 CHECK_FCT(fd_msg_free(*msg));
80 *msg = NULL;
81 return 0;
82 }
83
84 /* create message to send */
85 struct msg *create_message(const char *destination)
86 {
87 struct dict_object *command;
88 struct msg *msg;
89 struct avp *avp, *avp1;
90 union avp_value val;
91 struct msg_hdr *msg_hdr;
92 const char *realm;
93 const char *session_id = "fixed-session-id-for-now";
94 const char *proxy_host = "Dummy-Proxy-Host-to-Increase-Package-Size";
95 const char *proxy_state = "This is just date to increase the package size\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
96
97 if (fd_dict_search(fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Credit-Control-Request", &command, ENOENT) != 0) {
98 fd_log_error("can't find template for 'Credit-Control-Request'");
99 return NULL;
100 }
101
102 if (fd_msg_new(command, MSGFL_ALLOC_ETEID, &msg) != 0) {
103 fd_log_error("can't create new 'Credit-Control-Request' message");
104 return NULL;
105 }
106
107 /* Application Id in header needs to be set to for since this Credit-Control-Request is for Diameter Credit Control */
108 if (fd_msg_hdr(msg, &msg_hdr) != 0) {
109 fd_log_error("can't get message header for 'Credit-Control-Request' message");
110 fd_msg_free(msg);
111 return NULL;
112 }
113 msg_hdr->msg_appl = 4;
114
115 if (fd_msg_add_origin(msg, 0) != 0) {
116 fd_log_error("can't set Origin for 'Credit-Control-Request' message");
117 fd_msg_free(msg);
118 return NULL;
119 }
120 /* Destination-Host */
121 fd_msg_avp_new(dh_avp_do, 0, &avp);
122 memset(&val, 0, sizeof(val));
123 val.os.data = (uint8_t *)target;
124 val.os.len = strlen(target);
125 if (fd_msg_avp_setvalue(avp, &val) != 0) {
126 fd_msg_free(msg);
127 fd_log_error("can't set value for 'Destination-Host' for 'Credit-Control-Request' message");
128 return NULL;
129 }
130 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
131
132 if ((realm = strchr(target, '.')) == NULL) {
133 fd_msg_free(msg);
134 fd_log_error("can't extract realm from host '%s'", target);
135 return NULL;
136 }
137 /* skip dot */
138 realm++;
139 /* Destination-Realm */
140 fd_msg_avp_new(dr_avp_do, 0, &avp);
141 memset(&val, 0, sizeof(val));
142 val.os.data = (uint8_t *)realm;
143 val.os.len = strlen(realm);
144 if (fd_msg_avp_setvalue(avp, &val) != 0) {
145 fd_msg_free(msg);
146 fd_log_error("can't set value for 'Destination-Realm' for 'Credit-Control-Request' message");
147 return NULL;
148 }
149 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
150 /* Session-Id */
151 fd_msg_avp_new(si_avp_do, 0, &avp);
152 memset(&val, 0, sizeof(val));
153 val.os.data = (uint8_t *)session_id;
154 val.os.len = strlen(session_id);
155 if (fd_msg_avp_setvalue(avp, &val) != 0) {
156 fd_msg_free(msg);
157 fd_log_error("can't set value for 'Session-Id' for 'Credit-Control-Request' message");
158 return NULL;
159 }
160 fd_msg_avp_add(msg, MSG_BRW_FIRST_CHILD, avp);
161 /* Auth-Application-Id */
162 fd_msg_avp_new(aai_avp_do, 0, &avp);
163 memset(&val, 0, sizeof(val));
164 val.i32 = 4;
165 if (fd_msg_avp_setvalue(avp, &val) != 0) {
166 fd_msg_free(msg);
167 fd_log_error("can't set value for 'Auth-Application-Id' for 'Credit-Control-Request' message");
168 return NULL;
169 }
170 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
171
172 /* Proxy-Info */
173 fd_msg_avp_new(pi_avp_do, 0, &avp);
174 /* Proxy-Host */
175 fd_msg_avp_new(ph_avp_do, 0, &avp1);
176 memset(&val, 0, sizeof(val));
177 val.os.data = (uint8_t *)proxy_host;
178 val.os.len = strlen(proxy_host);
179 if (fd_msg_avp_setvalue(avp1, &val) != 0) {
180 fd_msg_free(msg);
181 fd_log_error("can't set value for 'Proxy-Host' for 'Credit-Control-Request' message");
182 return NULL;
183 }
184 fd_msg_avp_add(avp, MSG_BRW_LAST_CHILD, avp1);
185 /* Proxy-State */
186 fd_msg_avp_new(ps_avp_do, 0, &avp1);
187 memset(&val, 0, sizeof(val));
188 val.os.data = (uint8_t *)proxy_state;
189 val.os.len = strlen(proxy_state);
190 if (fd_msg_avp_setvalue(avp1, &val) != 0) {
191 fd_msg_free(msg);
192 fd_log_error("can't set value for 'Proxy-State' for 'Credit-Control-Request' message");
193 return NULL;
194 }
195 fd_msg_avp_add(avp, MSG_BRW_LAST_CHILD, avp1);
196 fd_msg_avp_add(msg, MSG_BRW_LAST_CHILD, avp);
197
198 return msg;
199 }
200
201 /* The thread that handles expired entries cleanup. */
202 void * gen_thr_fct(void * arg)
203 {
204 struct msg *msg;
205 fd_log_threadname ( "Loadtest/Generator" );
206
207 do {
208 if (do_generate) {
209 fd_msg_send(&msg, NULL, NULL);
210 fd_log_notice("sent message");
211 sleep(1);
212 } else {
213 sleep(1);
214 }
215 } while (1);
216 }
217
218 /* signal handler */
219 static void sig_hdlr(void)
220 {
221 if (do_generate) {
222 do_generate = 0;
223 } else {
224 do_generate = 1;
225 }
226 fd_log_notice("%s: switched generation of CCRs %s", MODULE_NAME, do_generate ? "on" : "off");
227 }
228
229 /* entry hook: register callback */
230 static int cc_entry(char * conffile)
231 {
232 struct disp_when data;
233
234 if ((target = conffile) == NULL) {
235 fd_log_error("invalid conffile");
236 return EINVAL;
237 }
238
239 memset(&data, 0, sizeof(data));
240
241 /* Advertise the support for the Diameter Credit Control application in the peer */
242 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_NAME, "Diameter Credit Control Application", &data.app, ENOENT) );
243 CHECK_FCT( fd_disp_app_support ( data.app, NULL, 1, 0 ) );
244
245 /* register handler for CCR */
246 CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Credit-Control-Request", &data.command, ENOENT) );
247 CHECK_FCT( fd_disp_register( ccr_handler, DISP_HOW_CC, &data, NULL, &ccr_handler_hdl ) );
248
249 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &aai_avp_do, ENOENT),
250 { LOG_E("Unable to find 'Auth-Application-Id' AVP in the loaded dictionaries."); });
251 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Host", &dh_avp_do, ENOENT),
252 { LOG_E("Unable to find 'Destination-Host' AVP in the loaded dictionaries."); });
253 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Destination-Realm", &dr_avp_do, ENOENT),
254 { LOG_E("Unable to find 'Destination-Realm' AVP in the loaded dictionaries."); });
255 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Result-Code", &rc_avp_do, ENOENT),
256 { LOG_E("Unable to find 'Result-Code' AVP in the loaded dictionaries."); });
257 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &si_avp_do, ENOENT),
258 { LOG_E("Unable to find 'Session-Id' AVP in the loaded dictionaries."); });
259 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Proxy-Info", &pi_avp_do, ENOENT),
260 { LOG_E("Unable to find 'Proxy-Info' AVP in the loaded dictionaries."); });
261 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Proxy-Host", &ph_avp_do, ENOENT),
262 { LOG_E("Unable to find 'Proxy-Host' AVP in the loaded dictionaries."); });
263 CHECK_FCT_DO(fd_dict_search(fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Proxy-State", &ps_avp_do, ENOENT),
264 { LOG_E("Unable to find 'Proxy-State' AVP in the loaded dictionaries."); });
265
266 /* Start the generator thread */
267 CHECK_POSIX( pthread_create( &gen_thr, NULL, gen_thr_fct, NULL ) );
268
269 /* Register generator callback */
270 CHECK_FCT(fd_event_trig_regcb(SIGUSR2, MODULE_NAME, sig_hdlr));
271
272 return 0;
273 }
274
275 /* And terminate it */
276 void fd_ext_fini(void)
277 {
278 /* stop sending */
279 do_generate = 0;
280
281 /* Stop the expiry thread */
282 CHECK_FCT_DO( fd_thr_term(&gen_thr), );
283
284 /* Unregister the callbacks */
285 if (ccr_handler_hdl) {
286 CHECK_FCT_DO( fd_disp_unregister(&ccr_handler_hdl, NULL), );
287 ccr_handler_hdl = NULL;
288 }
289
290 return;
291 }
292
293
294 EXTENSION_ENTRY(MODULE_NAME, cc_entry);
"Welcome to our mercurial repository"