Browse code

ims_diameter_server: export functions to kemi

Riccardo Villa authored on 08/04/2021 16:20:20
Showing 1 changed files
... ...
@@ -26,6 +26,7 @@
26 26
 #include "ims_diameter_server.h"
27 27
 #include "avp_helper.h"
28 28
 #include "../../core/fmsg.h"
29
+#include "../../core/kemi.h"
29 30
 
30 31
 MODULE_VERSION
31 32
 
... ...
@@ -58,6 +59,9 @@ static int w_diameter_request_peer(struct sip_msg *msg, char* peer, char* appid,
58 59
 static int w_diameter_request_async(struct sip_msg * msg, char* appid, char* commandcode, char* message);
59 60
 static int w_diameter_request_peer_async(struct sip_msg *msg, char* peer, char* appid, char* commandcode, char* message);
60 61
 
62
+static int ki_diameter_request(struct sip_msg * msg, str* s_peer, int i_appid, int i_commandcode, str* s_message, int async);
63
+static int ki_diameter_request_peer(struct sip_msg *msg, str* peer, int appid, int commandcode, str* message);
64
+static int ki_diameter_request_peer_async(struct sip_msg *msg, str* peer, int appid, int commandcode, str* message);
61 65
 
62 66
 static cmd_export_t cmds[] = {
63 67
 	{"diameter_request", (cmd_function)w_diameter_request, 3, fixup_var_pve_str_12, 0, ANY_ROUTE},
... ...
@@ -259,54 +263,21 @@ error:
259 263
 	if (response) cdpb.AAAFreeMessage(&response);
260 264
 }
261 265
 
266
+int ki_diameter_request_peer(struct sip_msg * msg, str* s_peer, int i_appid, int i_commandcode, str* s_message)
267
+{
268
+	return ki_diameter_request(msg, s_peer, i_appid, i_commandcode, s_message, 0);
269
+}
262 270
 
263
-int diameter_request(struct sip_msg * msg, char* peer, char* appid, char* commandcode, char* message, int async) {
264
-	str s_appid, s_commandcode, s_peer, s_message;
271
+int ki_diameter_request_peer_async(struct sip_msg * msg, str* s_peer, int i_appid, int i_commandcode, str* s_message)
272
+{
273
+	return ki_diameter_request(msg, s_peer, i_appid, i_commandcode, s_message, 1);
274
+}
275
+
276
+int ki_diameter_request(struct sip_msg * msg, str* s_peer, int i_appid, int i_commandcode, str* s_message, int async) {
265 277
 	AAAMessage *req = 0;
266 278
 	AAASession *session = 0;
267 279
 	AAAMessage *resp = 0;
268 280
 
269
-	unsigned int i_appid, i_commandcode;
270
-
271
-	if (async && (event_route_diameter_response < 0)) {
272
-		LM_ERR("Asynchronous operations disabled\n");
273
-		return -1;
274
-	}
275
-
276
-	if (peer) {
277
-		if (get_str_fparam(&s_peer, msg, (fparam_t*)peer) < 0) {
278
-		    LM_ERR("failed to get Peer\n");
279
-		    return -1;
280
-		}
281
-		LM_DBG("Peer %.*s\n", s_peer.len, s_peer.s);
282
-	}
283
-	if (get_str_fparam(&s_message, msg, (fparam_t*)message) < 0) {
284
-		LM_ERR("failed to get Message\n");
285
-		return -1;
286
-	}
287
-	if (get_str_fparam(&s_appid, msg, (fparam_t*)appid) < 0) {
288
-		LM_ERR("failed to get App-ID\n");
289
-		return -1;
290
-	}
291
-	if (str2int(&s_appid, &i_appid) != 0) {
292
-		LM_ERR("Invalid App-ID (%.*s)\n", s_appid.len, s_appid.s);
293
-		return -1;
294
-	}
295
-	LM_DBG("App-ID %i\n", i_appid);
296
-	if (get_str_fparam(&s_commandcode, msg, (fparam_t*)commandcode) < 0) {
297
-		LM_ERR("failed to get Command-Code\n");
298
-		return -1;
299
-	}
300
-	if (str2int(&s_commandcode, &i_commandcode) != 0) {
301
-		LM_ERR("Invalid Command-Code (%.*s)\n", s_commandcode.len, s_commandcode.s);
302
-		return -1;
303
-	}
304
-	LM_DBG("Command-Code %i\n", i_commandcode);
305
-	if (get_str_fparam(&s_commandcode, msg, (fparam_t*)commandcode) < 0) {
306
-		LM_ERR("failed to get Command-Code\n");
307
-		return -1;
308
-	}
309
-
310 281
 	session = cdpb.AAACreateSession(0);
311 282
 
312 283
 	req = cdpb.AAACreateRequest(i_appid, i_commandcode, Flag_Proxyable, session);
... ...
@@ -317,19 +288,19 @@ int diameter_request(struct sip_msg * msg, char* peer, char* appid, char* comman
317 288
 
318 289
 	if (!req) goto error1;
319 290
 
320
-	if (!addAVPsfromJSON(req, &s_message)) {
291
+	if (!addAVPsfromJSON(req, s_message)) {
321 292
 		LM_ERR("Failed to parse JSON Request\n");
322 293
 		return -1;
323 294
 	}
324 295
 
325 296
 
326
-	if (peer && (s_peer.len > 0)) {
297
+	if (s_peer && (s_peer->len > 0)) {
327 298
 		if (async) {
328
-			cdpb.AAASendMessageToPeer(req, &s_peer, (void*) async_cdp_diameter_callback, req);
299
+			cdpb.AAASendMessageToPeer(req, s_peer, (void*) async_cdp_diameter_callback, req);
329 300
 			LM_DBG("Successfully sent async diameter\n");
330 301
 			return 0;
331 302
 		} else {
332
-			resp = cdpb.AAASendRecvMessageToPeer(req, &s_peer);
303
+			resp = cdpb.AAASendRecvMessageToPeer(req, s_peer);
333 304
 			LM_DBG("Successfully sent diameter\n");
334 305
 			if (resp && AAAmsg2json(resp, &responsejson) == 1) {
335 306
 				return 1;
... ...
@@ -359,6 +330,70 @@ error1:
359 330
 	if (req) cdpb.AAAFreeMessage(&req);
360 331
 	LM_ERR("Error occurred trying to send request\n");
361 332
 	return -1;
333
+
362 334
 }
363 335
 
336
+int diameter_request(struct sip_msg * msg, char* peer, char* appid, char* commandcode, char* message, int async) {
337
+	str s_appid, s_commandcode, s_peer, s_message;
338
+	unsigned int i_appid, i_commandcode;
339
+
340
+	if (async && (event_route_diameter_response < 0)) {
341
+		LM_ERR("Asynchronous operations disabled\n");
342
+		return -1;
343
+	}
364 344
 
345
+	if (peer) {
346
+		if (get_str_fparam(&s_peer, msg, (fparam_t*)peer) < 0) {
347
+		    LM_ERR("failed to get Peer\n");
348
+		    return -1;
349
+		}
350
+		LM_DBG("Peer %.*s\n", s_peer.len, s_peer.s);
351
+	}
352
+	if (get_str_fparam(&s_message, msg, (fparam_t*)message) < 0) {
353
+		LM_ERR("failed to get Message\n");
354
+		return -1;
355
+	}
356
+	if (get_str_fparam(&s_appid, msg, (fparam_t*)appid) < 0) {
357
+		LM_ERR("failed to get App-ID\n");
358
+		return -1;
359
+	}
360
+	if (str2int(&s_appid, &i_appid) != 0) {
361
+		LM_ERR("Invalid App-ID (%.*s)\n", s_appid.len, s_appid.s);
362
+		return -1;
363
+	}
364
+	LM_DBG("App-ID %i\n", i_appid);
365
+
366
+	if (get_str_fparam(&s_commandcode, msg, (fparam_t*)commandcode) < 0) {
367
+		LM_ERR("failed to get Command-Code\n");
368
+		return -1;
369
+	}
370
+	if (str2int(&s_commandcode, &i_commandcode) != 0) {
371
+		LM_ERR("Invalid Command-Code (%.*s)\n", s_commandcode.len, s_commandcode.s);
372
+		return -1;
373
+	}
374
+	LM_DBG("Command-Code %i\n", i_commandcode);
375
+
376
+	return ki_diameter_request(msg, &s_peer, i_appid, i_commandcode, &s_message, async); 
377
+}
378
+
379
+
380
+static sr_kemi_t ims_dimeter_server_kemi_exports[] = {
381
+    { str_init("ims_diameter_server"), str_init("diameter_request"),
382
+        SR_KEMIP_INT, ki_diameter_request_peer,
383
+        { SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
384
+            SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
385
+    },
386
+    { str_init("ims_diameter_server"), str_init("diameter_request_async"),
387
+        SR_KEMIP_INT, ki_diameter_request_peer_async,
388
+        { SR_KEMIP_STR, SR_KEMIP_INT, SR_KEMIP_INT,
389
+            SR_KEMIP_STR, SR_KEMIP_NONE, SR_KEMIP_NONE }
390
+    },
391
+
392
+    { {0, 0}, {0, 0}, 0, NULL, { 0, 0, 0, 0, 0, 0 } }
393
+};
394
+
395
+int mod_register(char *path, int *dlflags, void *p1, void *p2)
396
+{
397
+    sr_kemi_modules_add(ims_dimeter_server_kemi_exports);
398
+    return 0;
399
+}