/
ims_ro.c
1571 lines (1233 loc) · 53.6 KB
/
ims_ro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "ims_charging_mod.h"
#include <math.h>
#include "../../core/parser/msg_parser.h"
#include "../../core/parser/parse_uri.h"
#include "../../core/sr_module.h"
#include "../../core/socket_info.h"
#include "../../core/timer.h"
#include "../../core/locking.h"
#include "../../modules/tm/tm_load.h"
#include "../../modules/ims_dialog/dlg_hash.h"
#include "../../modules/ims_dialog/dlg_load.h"
#include "../cdp/cdp_load.h"
#include "../../core/mod_fix.h"
#include "../../core/parser/parse_from.h"
#include "../../core/parser/parse_to.h"
#include "../../lib/ims/ims_getters.h"
#include "../../core/parser/sdp/sdp.h"
#include "diameter_ro.h"
#include "ims_ro.h"
#include "Ro_data.h"
#include "dialog.h"
#include "ccr.h"
#include "config.h"
#include "ro_session_hash.h"
#include "ro_avp.h"
#include "ro_db_handler.h"
#include "ims_charging_stats.h"
static pv_spec_t *custom_user_avp; /*!< AVP for custom_user setting */
extern struct tm_binds tmb;
extern struct cdp_binds cdpb;
extern client_ro_cfg cfg;
extern ims_dlg_api_t dlgb;
extern cdp_avp_bind_t *cdp_avp;
extern str ro_forced_peer;
extern int ro_db_mode;
extern struct ims_charging_counters_h ims_charging_cnts_h;
extern int vendor_specific_id;
extern int vendor_specific_chargeinfo;
struct session_setup_data {
struct ro_session *ro_session;
cfg_action_t* action;
unsigned int tindex;
unsigned int tlabel;
};
struct dlg_binds* dlgb_p;
extern struct tm_binds tmb;
int interim_request_credits;
extern int voice_service_identifier;
extern int voice_rating_group;
extern int video_service_identifier;
extern int video_rating_group;
static int create_cca_return_code(int result);
static int create_cca_result_code(int result);
static int create_cca_fui_avps(int action, str* redirecturi);
static void resume_on_initial_ccr(int is_timeout, void *param, AAAMessage *cca, long elapsed_msecs);
static void resume_on_interim_ccr(int is_timeout, void *param, AAAMessage *cca, long elapsed_msecs);
static void resume_on_termination_ccr(int is_timeout, void *param, AAAMessage *cca, long elapsed_msecs);
static int get_mac_avp_value(struct sip_msg *msg, str *value);
void init_custom_user(pv_spec_t *custom_user_avp_p)
{
custom_user_avp = custom_user_avp_p;
}
/*!
* \brief Return the custom_user for a record route
* \param req SIP message
* \param custom_user to be returned
* \return <0 for failure
*/
static int get_custom_user(struct sip_msg *req, str *custom_user) {
pv_value_t pv_val;
if (custom_user_avp) {
if ((pv_get_spec_value(req, custom_user_avp, &pv_val) == 0)
&& (pv_val.flags & PV_VAL_STR) && (pv_val.rs.len > 0)) {
custom_user->s = pv_val.rs.s;
custom_user->len = pv_val.rs.len;
return 0;
}
LM_DBG("invalid AVP value, using default user from P-Asserted-Identity/From-Header\n");
}
return -1;
}
void credit_control_session_callback(int event, void* session) {
switch (event) {
case AUTH_EV_SESSION_DROP:
LM_DBG("Received notification of CC App session drop - we must free the generic data\n");
break;
default:
LM_DBG("Received unhandled event [%d] in credit control session callback from CDP\n", event);
}
}
/**
* Create and add an AVP to a list of AVPs.
* @param list - the AVP list to add to
* @param d - the payload data
* @param len - length of the payload data
* @param avp_code - the code of the AVP
* @param flags - flags for the AVP
* @param vendorid - the value of the vendor id or 0 if none
* @param data_do - what to do with the data when done
* @param func - the name of the calling function, for debugging purposes
* @returns 1 on success or 0 on failure
*/
int Ro_add_avp_list(AAA_AVP_LIST *list, char *d, int len, int avp_code,
int flags, int vendorid, int data_do, const char *func) {
AAA_AVP *avp;
if (vendorid != 0) flags |= AAA_AVP_FLAG_VENDOR_SPECIFIC;
avp = cdpb.AAACreateAVP(avp_code, flags, vendorid, d, len, data_do);
if (!avp) {
LM_ERR("%s: Failed creating avp\n", func);
return 0;
}
if (list->tail) {
avp->prev = list->tail;
avp->next = 0;
list->tail->next = avp;
list->tail = avp;
} else {
list->head = avp;
list->tail = avp;
avp->next = 0;
avp->prev = 0;
}
return 1;
}
int Ro_add_cc_request(AAAMessage *msg, unsigned int cc_request_type, unsigned int cc_request_number) {
char x[4];
set_4bytes(x, cc_request_type);
int success = Ro_add_avp(msg, x, 4, AVP_CC_Request_Type, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
char y[4];
set_4bytes(y, cc_request_number);
return success && Ro_add_avp(msg, y, 4, AVP_CC_Request_Number, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
}
int Ro_add_event_timestamp(AAAMessage *msg, time_t now) {
char x[4];
str s = {x, 4};
uint32_t ntime = htonl(now + EPOCH_UNIX_TO_EPOCH_NTP);
memcpy(x, &ntime, sizeof (uint32_t));
return Ro_add_avp(msg, s.s, s.len, AVP_Event_Timestamp, AAA_AVP_FLAG_NONE, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
}
int Ro_add_user_equipment_info(AAAMessage *msg, unsigned int type, str value) {
AAA_AVP_LIST list;
str group;
char x[4];
list.head = 0;
list.tail = 0;
set_4bytes(x, type);
Ro_add_avp_list(&list, x, 4, AVP_User_Equipment_Info_Type, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
Ro_add_avp_list(&list, value.s, value.len, AVP_User_Equipment_Info_Value, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
group = cdpb.AAAGroupAVPS(list);
cdpb.AAAFreeAVPList(&list);
return Ro_add_avp(msg, group.s, group.len, AVP_User_Equipment_Info, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
}
int Ro_add_termination_cause(AAAMessage *msg, unsigned int term_code) {
char x[4];
str s = {x, 4};
uint32_t code = htonl(term_code);
memcpy(x, &code, sizeof (uint32_t));
return Ro_add_avp(msg, s.s, s.len, AVP_Termination_Cause, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
}
int Ro_add_vendor_specific_termination_cause(AAAMessage *msg, unsigned int term_code) {
char x[4];
str s = {x, 4};
uint32_t code = htonl(term_code);
memcpy(x, &code, sizeof (uint32_t));
return Ro_add_avp(msg, s.s, s.len, VS_TERMCODE, AAA_AVP_FLAG_VENDOR_SPECIFIC, 10, AVP_DUPLICATE_DATA, __FUNCTION__);
}
int Ro_add_vendor_specific_termination_reason(AAAMessage *msg, str* reason) {
return Ro_add_avp(msg, reason->s, reason->len, VS_TERMREASON, AAA_AVP_FLAG_VENDOR_SPECIFIC, 10, AVP_DUPLICATE_DATA, __FUNCTION__);
}
/* called only when building stop record AVPS */
int Ro_add_multiple_service_credit_Control_stop(AAAMessage *msg, int used_unit, int active_rating_group, int active_service_identifier) {
char x[4];
AAA_AVP_LIST used_list, mscc_list;
str used_group;
// Add Multiple-Services AVP Indicator
set_4bytes(x, 1);
Ro_add_avp(msg, x, 4, AVP_Multiple_Services_Indicator, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
//unsigned int service_id = 1000; //Removed these are now configurable config file params
//unsigned int rating_group = 500; //Removed these are now configurable config file params
used_list.head = 0;
used_list.tail = 0;
mscc_list.head = 0;
mscc_list.tail = 0;
/* if we must Used-Service-Unit */
if (used_unit >= 0) {
set_4bytes(x, used_unit);
Ro_add_avp_list(&used_list, x, 4, AVP_CC_Time, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
used_group = cdpb.AAAGroupAVPS(used_list);
cdpb.AAAFreeAVPList(&used_list);
Ro_add_avp_list(&mscc_list, used_group.s, used_group.len, AVP_Used_Service_Unit, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
}
set_4bytes(x, active_service_identifier);
Ro_add_avp_list(&mscc_list, x, 4, AVP_Service_Identifier, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
// Rating Group = -1 => omit Rating group
if (active_rating_group >= 0) {
set_4bytes(x, active_rating_group);
Ro_add_avp_list(&mscc_list, x, 4, AVP_Rating_Group, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
}
used_group = cdpb.AAAGroupAVPS(mscc_list);
cdpb.AAAFreeAVPList(&mscc_list);
return Ro_add_avp(msg, used_group.s, used_group.len, AVP_Multiple_Services_Credit_Control, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
}
int Ro_add_multiple_service_credit_Control(AAAMessage *msg, unsigned int requested_unit, int used_unit, int active_rating_group, int active_service_identifier) {
// Add Multiple-Services AVP Indicator
char x[4];
set_4bytes(x, 1);
Ro_add_avp(msg, x, 4, AVP_Multiple_Services_Indicator, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
AAA_AVP_LIST list, used_list, mscc_list;
str group, used_group;
list.head = 0;
list.tail = 0;
used_list.head = 0;
used_list.tail = 0;
mscc_list.head = 0;
mscc_list.tail = 0;
set_4bytes(x, requested_unit);
Ro_add_avp_list(&list, x, 4, AVP_CC_Time, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
group = cdpb.AAAGroupAVPS(list);
cdpb.AAAFreeAVPList(&list);
Ro_add_avp_list(&mscc_list, group.s, group.len, AVP_Requested_Service_Unit, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
set_4bytes(x, active_service_identifier);
Ro_add_avp_list(&mscc_list, x, 4, AVP_Service_Identifier, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
// Rating Group = -1 => omit Rating group
if (active_rating_group >= 0) {
set_4bytes(x, active_rating_group);
Ro_add_avp_list(&mscc_list, x, 4, AVP_Rating_Group, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
}
/* if we must Used-Service-Unit */
if (used_unit >= 0) {
set_4bytes(x, used_unit);
Ro_add_avp_list(&used_list, x, 4, AVP_CC_Time, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
used_group = cdpb.AAAGroupAVPS(used_list);
cdpb.AAAFreeAVPList(&used_list);
Ro_add_avp_list(&mscc_list, used_group.s, used_group.len, AVP_Used_Service_Unit, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
}
group = cdpb.AAAGroupAVPS(mscc_list);
cdpb.AAAFreeAVPList(&mscc_list);
return Ro_add_avp(msg, group.s, group.len, AVP_Multiple_Services_Credit_Control, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
}
int Ro_add_subscription_id(AAAMessage *msg, unsigned int type, str *subscription_id)//, struct sip_msg* sip_msg)
{
AAA_AVP_LIST list;
str group;
char x[4];
list.head = 0;
list.tail = 0;
set_4bytes(x, type);
Ro_add_avp_list(&list, x, 4, AVP_Subscription_Id_Type, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
Ro_add_avp_list(&list, subscription_id->s, subscription_id->len, AVP_Subscription_Id_Data, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
group = cdpb.AAAGroupAVPS(list);
cdpb.AAAFreeAVPList(&list);
return Ro_add_avp(msg, group.s, group.len, AVP_Subscription_Id, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
}
/**
* Creates and adds a Vendor-Specifig-Application-ID AVP.
* @param msg - the Diameter message to add to.
* @param vendor_id - the value of the vendor_id,
* @param auth_id - the authorization application id
* @param acct_id - the accounting application id
* @returns 1 on success or 0 on error
*/
int Ro_add_vendor_specific_appid(AAAMessage *msg, unsigned int vendor_id, unsigned int auth_id, unsigned int acct_id) {
AAA_AVP_LIST list;
str group;
char x[4];
list.head = 0;
list.tail = 0;
set_4bytes(x, vendor_id);
Ro_add_avp_list(&list, x, 4, AVP_Vendor_Id, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
if (auth_id) {
set_4bytes(x, auth_id);
Ro_add_avp_list(&list, x, 4, AVP_Auth_Application_Id, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
}
if (acct_id) {
set_4bytes(x, acct_id);
Ro_add_avp_list(&list, x, 4, AVP_Acct_Application_Id, AAA_AVP_FLAG_MANDATORY, 0, AVP_DUPLICATE_DATA, __FUNCTION__);
}
group = cdpb.AAAGroupAVPS(list);
cdpb.AAAFreeAVPList(&list);
return Ro_add_avp(msg, group.s, group.len, AVP_Vendor_Specific_Application_Id, AAA_AVP_FLAG_MANDATORY, 0, AVP_FREE_DATA, __FUNCTION__);
}
int get_sip_header_info(struct sip_msg * req,
struct sip_msg * reply,
int32_t * acc_record_type,
str * sip_method,
str * event, uint32_t * expires,
str * callid, str * asserted_id_uri, str * to_uri) {
sip_method->s = req->first_line.u.request.method.s;
sip_method->len = req->first_line.u.request.method.len;
if (strncmp(sip_method->s, "INVITE", 6) == 0)
*acc_record_type = AAA_ACCT_START;
else if (strncmp(sip_method->s, "BYE", 3) == 0)
*acc_record_type = AAA_ACCT_STOP;
else
*acc_record_type = AAA_ACCT_EVENT;
*event = cscf_get_event(req);
*expires = cscf_get_expires_hdr(req, 0);
*callid = cscf_get_call_id(req, NULL);
if (get_custom_user(req, asserted_id_uri) == -1) {
if ((*asserted_id_uri = cscf_get_asserted_identity(req, 0)).len == 0) {
LM_DBG("No P-Asserted-Identity hdr found. Using From hdr");
if (!cscf_get_from_uri(req, asserted_id_uri)) {
LM_ERR("Error assigning P-Asserted-Identity using From hdr");
goto error;
}
}
}
*to_uri = req->first_line.u.request.uri;
LM_DBG("retrieved sip info : sip_method %.*s acc_record_type %i, event %.*s expires %u "
"call_id %.*s from_uri %.*s to_uri %.*s\n",
sip_method->len, sip_method->s, *acc_record_type, event->len, event->s, *expires,
callid->len, callid->s, asserted_id_uri->len, asserted_id_uri->s, to_uri->len, to_uri->s);
return 1;
error:
return 0;
}
int get_ims_charging_info(struct sip_msg *req, struct sip_msg * reply, str * icid, str * orig_ioi, str * term_ioi) {
LM_DBG("get ims charging info\n");
if (req)
cscf_get_p_charging_vector(req, icid, orig_ioi, term_ioi);
if (reply)
cscf_get_p_charging_vector(reply, icid, orig_ioi, term_ioi);
return 1;
}
int get_timestamps(struct sip_msg * req, struct sip_msg * reply, time_t * req_timestamp, time_t * reply_timestamp) {
if (reply)
*reply_timestamp = time(NULL);
if (req)
*req_timestamp = time(NULL);
return 1;
}
/*
* creates the ro session for a session establishment
*
*/
Ro_CCR_t * dlg_create_ro_session(struct sip_msg * req, struct sip_msg * reply, AAASession ** authp, int dir, str asserted_identity,
str called_asserted_identity, str subscription_id, int subscription_id_type, str* incoming_trunk_id, str *outgoing_trunk_id, str* pani) {
Ro_CCR_t * ro_ccr_data = 0;
AAASession * auth = NULL;
str user_name/* ={0,0}*/, sip_method = {0, 0}, event = {0, 0};
uint32_t expires = 0;
str callid = {0, 0}, to_uri = {0, 0}, from_uri = {0, 0},
icid = {0, 0}, orig_ioi = {0, 0}, term_ioi = {0, 0};
event_type_t * event_type = 0;
ims_information_t * ims_info = 0;
time_stamps_t * time_stamps = 0;
time_t req_timestamp = 0, reply_timestamp = 0;
int32_t acc_record_type;
subscription_id_t subscr;
*authp = 0;
if (!get_sip_header_info(req, reply, &acc_record_type, &sip_method, &event, &expires, &callid, &from_uri, &to_uri))
goto error;
user_name.s = subscription_id.s;
user_name.len = subscription_id.len;
/* if(!get_ims_charging_info(req, reply, &icid, &orig_ioi, &term_ioi))
goto error;
*/
LM_DBG("retrieved ims charging info icid:[%.*s] orig_ioi:[%.*s] term_ioi:[%.*s]\n",
icid.len, icid.s, orig_ioi.len, orig_ioi.s, term_ioi.len, term_ioi.s);
if (!get_timestamps(req, reply, &req_timestamp, &reply_timestamp))
goto error;
if (!(event_type = new_event_type(&sip_method, &event, &expires)))
goto error;
if (!(time_stamps = new_time_stamps(&req_timestamp, NULL, &reply_timestamp, NULL)))
goto error;
if (!(ims_info = new_ims_information(event_type, time_stamps, &callid, &callid, &asserted_identity, &called_asserted_identity, &icid,
&orig_ioi, &term_ioi, dir, incoming_trunk_id, outgoing_trunk_id, pani)))
goto error;
event_type = 0;
time_stamps = 0;
subscr.id.s = subscription_id.s;
subscr.id.len = subscription_id.len;
subscr.type = subscription_id_type;
ro_ccr_data = new_Ro_CCR(acc_record_type, &user_name, ims_info, &subscr);
if (!ro_ccr_data) {
LM_ERR("dlg_create_ro_session: no memory left for generic\n");
goto out_of_memory;
}
ims_info = 0;
if (strncmp(req->first_line.u.request.method.s, "INVITE", 6) == 0) {
//create CDP CC Accounting session
auth = cdpb.AAACreateCCAccSession(credit_control_session_callback, 1/*is_session*/, NULL); //must unlock session hash when done
LM_DBG("Created Ro Session with id Session ID [%.*s]\n", auth->id.len, auth->id.s);
//save_session = auth->id;
}
/*if (strncmp(req->first_line.u.request.method.s, "BYE", 3) == 0) {
auth = cdp_avp->cdp->AAAGetAuthSession(save_session);
}*/
if (!auth) {
LM_ERR("unable to create the Ro Session\n");
goto error;
}
*authp = auth;
return ro_ccr_data;
out_of_memory:
error :
time_stamps_free(time_stamps);
event_type_free(event_type);
ims_information_free(ims_info);
Ro_free_CCR(ro_ccr_data);
return NULL;
}
int sip_create_ro_ccr_data(struct sip_msg * msg, int dir, Ro_CCR_t ** ro_ccr_data, AAASession ** auth, str asserted_identity, str called_asserted_identity,
str subscription_id, int subscription_id_type, str* incoming_trunk_id, str* outgoing_trunk_id, str* pani) {
if (msg->first_line.type == SIP_REQUEST) {
/*end of session*/
if (strncmp(msg->first_line.u.request.method.s, "INVITE", 6) == 0) {
if (!(*ro_ccr_data = dlg_create_ro_session(msg, NULL, auth, dir, asserted_identity, called_asserted_identity, subscription_id,
subscription_id_type, incoming_trunk_id, outgoing_trunk_id, pani)))
goto error;
}
} else {
goto error; //We only support Request (INVITE) messages on this interface
}
return 1;
error:
return 0;
}
/* must be called with lock on ro_session */
void send_ccr_interim(struct ro_session* ro_session, unsigned int used, unsigned int reserve) {
AAASession * auth = 0;
AAAMessage * ccr = 0;
Ro_CCR_t *ro_ccr_data = 0;
ims_information_t *ims_info = 0;
int32_t acc_record_type;
subscription_id_t subscr;
time_stamps_t *time_stamps;
struct interim_ccr *i_req = shm_malloc(sizeof (struct interim_ccr));
int ret = 0;
event_type_t *event_type;
memset(i_req, 0, sizeof (sizeof (struct interim_ccr)));
i_req->ro_session = ro_session;
str sip_method = str_init("dummy");
str sip_event = str_init("dummy");
str user_name = {0, 0};
time_t req_timestamp;
event_type = new_event_type(&sip_method, &sip_event, 0);
LM_DBG("Sending interim CCR request for (usage:new) [%i:%i] seconds for user [%.*s] using session id [%.*s] active rating group [%d] active service identifier [%d] incoming_trunk_id [%.*s] outgoing_trunk_id [%.*s]\n",
used,
reserve,
ro_session->asserted_identity.len, ro_session->asserted_identity.s,
ro_session->ro_session_id.len, ro_session->ro_session_id.s,
ro_session->rating_group, ro_session->service_identifier,
ro_session->incoming_trunk_id.len, ro_session->incoming_trunk_id.s,
ro_session->outgoing_trunk_id.len, ro_session->outgoing_trunk_id.s);
req_timestamp = time(0);
if (!(time_stamps = new_time_stamps(&req_timestamp, NULL, NULL, NULL)))
goto error;
if (!(ims_info = new_ims_information(event_type, time_stamps, &ro_session->callid, &ro_session->callid, &ro_session->asserted_identity,
&ro_session->called_asserted_identity, 0, 0, 0, ro_session->direction, &ro_session->incoming_trunk_id, &ro_session->outgoing_trunk_id, &ro_session->pani)))
goto error;
LM_DBG("Created IMS information\n");
event_type = 0;
if (ro_session->direction == RO_ORIG_DIRECTION) {
subscr.id = ro_session->asserted_identity;
} else if (ro_session->direction == RO_TERM_DIRECTION) {
subscr.id = ro_session->called_asserted_identity;
} else {
LM_CRIT("don't know what to do in unknown mode - should we even get here\n");
goto error;
}
//getting subscription id type
if (strncasecmp(subscr.id.s, "tel:", 4) == 0) {
subscr.type = Subscription_Type_MSISDN;
// Strip "tel:":
subscr.id.s += 4;
subscr.id.len -= 4;
} else {
subscr.type = Subscription_Type_IMPU; //default is END_USER_SIP_URI
}
user_name.s = subscr.id.s;
user_name.len = subscr.id.len;
acc_record_type = AAA_ACCT_INTERIM;
ro_ccr_data = new_Ro_CCR(acc_record_type, &user_name, ims_info, &subscr);
if (!ro_ccr_data) {
LM_ERR("no memory left for generic\n");
goto error;
}
ims_info = NULL;
auth = cdpb.AAAGetCCAccSession(ro_session->ro_session_id);
if (!auth) {
LM_DBG("Diameter Auth Session has timed out.... creating a new one.\n");
/* lets try and recreate this session */
//TODO: make a CC App session auth = cdpb.AAASession(ro_session->auth_appid, ro_session->auth_session_type, ro_session->ro_session_id); //TODO: would like this session to last longer (see session timeout in cdp
//BUG("Oh shit, session timed out and I don't know how to create a new one.");
auth = cdpb.AAAMakeSession(ro_session->auth_appid, ro_session->auth_session_type, ro_session->ro_session_id); //TODO: would like this session to last longer (see session timeout in cdp
if (!auth)
goto error;
}
//don't send INTERIM record if session is not in OPEN state (it could already be waiting for a previous response, etc)
if (auth->u.cc_acc.state != ACC_CC_ST_OPEN) {
LM_WARN("ignoring interim update on CC session not in correct state, currently in state [%d]\n", auth->u.cc_acc.state);
goto error;
}
if (!(ccr = Ro_new_ccr(auth, ro_ccr_data)))
goto error;
if (!Ro_add_vendor_specific_appid(ccr, IMS_vendor_id_3GPP, IMS_Ro, 0/*acct id*/)) {
LM_ERR("Problem adding Vendor specific ID\n");
}
ro_session->hop_by_hop += 1;
if (!Ro_add_cc_request(ccr, RO_CC_INTERIM, ro_session->hop_by_hop)) {
LM_ERR("Problem adding CC-Request data\n");
}
if (!Ro_add_event_timestamp(ccr, time(NULL))) {
LM_ERR("Problem adding Event-Timestamp data\n");
}
if (!Ro_add_user_equipment_info(ccr, AVP_EPC_User_Equipment_Info_Type_MAC, ro_session->mac)) {
LM_ERR("Problem adding User-Equipment data\n");
}
if (!Ro_add_subscription_id(ccr, subscr.type, &(subscr.id))) {
LM_ERR("Problem adding Subscription ID data\n");
}
if (!Ro_add_multiple_service_credit_Control(ccr, interim_request_credits/*INTERIM_CREDIT_REQ_AMOUNT*/, used, ro_session->rating_group, ro_session->service_identifier)) {
LM_ERR("Problem adding Multiple Service Credit Control data\n");
}
LM_DBG("Sending CCR Diameter message.\n");
ro_session->last_event_timestamp_backup = ro_session->last_event_timestamp;
ro_session->last_event_timestamp = get_current_time_micro(); /*this is to make sure that if we get a term request now that we don't double bill for this time we are about to bill for in the interim */
cdpb.AAASessionsUnlock(auth->hash);
if (ro_forced_peer.len > 0) {
ret = cdpb.AAASendMessageToPeer(ccr, &ro_forced_peer, resume_on_interim_ccr, (void *) i_req);
} else {
ret = cdpb.AAASendMessage(ccr, resume_on_interim_ccr, (void *) i_req);
}
if (ret != 1) {
ccr = 0; // If an error is returned from cdp AAASendMessage, the message has been freed there
goto error;
}
// cdpb.AAASessionsUnlock(auth->hash);
Ro_free_CCR(ro_ccr_data);
counter_inc(ims_charging_cnts_h.interim_ccrs);
return;
error:
LM_ERR("error trying to reserve interim credit\n");
if (ro_ccr_data)
Ro_free_CCR(ro_ccr_data);
if (ccr)
cdpb.AAAFreeMessage(&ccr);
if (auth) {
cdpb.AAASessionsUnlock(auth->hash);
cdpb.AAADropCCAccSession(auth);
}
shm_free(i_req);
//
// since callback function will be never called because of the error, we need to release the lock on the session
// to it can be reused later.
//
unref_ro_session(ro_session, 1, 1);
return;
}
static void resume_on_interim_ccr(int is_timeout, void *param, AAAMessage *cca, long elapsed_msecs) {
struct interim_ccr *i_req = (struct interim_ccr *) param;
Ro_CCA_t * ro_cca_data = NULL;
int error_or_timeout = 0;
if (is_timeout) {
counter_inc(ims_charging_cnts_h.ccr_timeouts);
LM_ERR("Transaction timeout - did not get CCA\n");
error_or_timeout = 1;
goto error;
}
counter_add(ims_charging_cnts_h.ccr_response_time, elapsed_msecs);
counter_inc(ims_charging_cnts_h.ccr_replies_received);
if (!i_req) {
LM_ERR("This is so wrong: ro session is NULL\n");
error_or_timeout = 1;
goto error;
}
if (cca == NULL) {
LM_ERR("Error reserving credit for CCA.\n");
error_or_timeout = 1;
goto error;
}
ro_cca_data = Ro_parse_CCA_avps(cca);
if (ro_cca_data == NULL) {
LM_ERR("Could not parse CCA message response.\n");
error_or_timeout = 1;
goto error;
}
if (ro_cca_data->resultcode != 2001) {
LM_ERR("Got bad CCA result code [%d] - reservation failed", ro_cca_data->resultcode);
error_or_timeout = 1;
goto error;
} else {
LM_DBG("Valid CCA response with time chunk of [%i] and validity [%i].\n", ro_cca_data->mscc->granted_service_unit->cc_time, ro_cca_data->mscc->validity_time);
}
i_req->new_credit = ro_cca_data->mscc->granted_service_unit->cc_time;
i_req->credit_valid_for = ro_cca_data->mscc->validity_time;
i_req->is_final_allocation = 0;
if (ro_cca_data->mscc->final_unit_action && (ro_cca_data->mscc->final_unit_action->action == 0))
i_req->is_final_allocation = 1;
Ro_free_CCA(ro_cca_data);
cdpb.AAAFreeMessage(&cca);
counter_inc(ims_charging_cnts_h.successful_interim_ccrs);
goto success;
error:
counter_inc(ims_charging_cnts_h.failed_interim_ccr);
if (ro_cca_data)
Ro_free_CCA(ro_cca_data);
if (!is_timeout && cca) {
cdpb.AAAFreeMessage(&cca);
}
if (i_req) {
i_req->credit_valid_for = 0;
i_req->new_credit = 0;
}
success:
resume_ro_session_ontimeout(i_req, error_or_timeout);
}
long get_current_time_micro() {
struct timeval tv;
gettimeofday(&tv, 0);
return tv.tv_sec*1000000 + tv.tv_usec;
}
void send_ccr_stop_with_param(struct ro_session *ro_session, unsigned int code, str* reason) {
AAASession * auth = 0;
Ro_CCR_t * ro_ccr_data = 0;
AAAMessage * ccr = 0;
ims_information_t *ims_info = 0;
int32_t acc_record_type;
subscription_id_t subscr;
time_stamps_t *time_stamps;
long used = 0;
str user_name = {0, 0};
int ret = 0;
time_t stop_time;
time_t actual_time_micros;
int actual_time_seconds;
stop_time = get_current_time_micro();
if (ro_session->start_time == 0)
actual_time_micros = 0;
else
actual_time_micros = stop_time - ro_session->start_time;
actual_time_seconds = (actual_time_micros + (1000000 - 1)) / (float) 1000000;
if (ro_session->event_type != pending) {
used = rint((stop_time - ro_session->last_event_timestamp) / (float) 1000000);
LM_DBG("Final used number of seconds for session is %ld\n", used);
}
LM_DBG("Call started at %ld and ended at %ld and lasted %d seconds and so far we have billed for %ld seconds\n", ro_session->start_time, stop_time,
actual_time_seconds, ro_session->billed + used);
if (ro_session->billed + used < actual_time_seconds) {
LM_DBG("Making adjustment by adding %ld seconds\n", actual_time_seconds - (ro_session->billed + used));
used += actual_time_seconds - (ro_session->billed + used);
}
counter_add(ims_charging_cnts_h.billed_secs, (int)used);
event_type_t *event_type;
str sip_method = str_init("dummy");
str sip_event = str_init("dummy");
time_t req_timestamp;
event_type = new_event_type(&sip_method, &sip_event, 0);
LM_DBG("Sending stop CCR request for (usage) [%i] seconds for user [%.*s] using session id [%.*s] active rating group [%d] active service identifier [%d] incoming_trunk_id [%.*s] outgoing_trunk_id [%.*s] pani [%.*s]\n",
(int)used,
ro_session->asserted_identity.len, ro_session->asserted_identity.s,
ro_session->ro_session_id.len, ro_session->ro_session_id.s,
ro_session->rating_group, ro_session->service_identifier,
ro_session->incoming_trunk_id.len, ro_session->incoming_trunk_id.s,
ro_session->outgoing_trunk_id.len, ro_session->outgoing_trunk_id.s,
ro_session->pani.len, ro_session->pani.s);
req_timestamp = get_current_time_micro();
if (!(time_stamps = new_time_stamps(&req_timestamp, NULL, NULL, NULL)))
goto error0;
if (!(ims_info = new_ims_information(event_type, time_stamps, &ro_session->callid, &ro_session->callid, &ro_session->asserted_identity,
&ro_session->called_asserted_identity, 0, 0, 0, ro_session->direction, &ro_session->incoming_trunk_id, &ro_session->outgoing_trunk_id, &ro_session->pani)))
goto error0;
event_type = 0;
if (ro_session->direction == RO_ORIG_DIRECTION) {
subscr.id = ro_session->asserted_identity;
} else if (ro_session->direction == RO_TERM_DIRECTION) {
subscr.id = ro_session->called_asserted_identity;
} else {
LM_CRIT("don't know what to do in unknown mode - should we even get here\n");
goto error0;
}
//getting subscription id type
if (strncasecmp(subscr.id.s, "tel:", 4) == 0) {
subscr.type = Subscription_Type_MSISDN;
} else {
subscr.type = Subscription_Type_IMPU; //default is END_USER_SIP_URI
}
user_name.s = subscr.id.s;
user_name.len = subscr.id.len;
acc_record_type = AAA_ACCT_STOP;
ro_ccr_data = new_Ro_CCR(acc_record_type, &user_name, ims_info, &subscr);
if (!ro_ccr_data) {
LM_ERR("send_ccr_stop: no memory left for generic\n");
goto error0;
}
ims_info = 0;
LM_DBG("Created Ro data\n");
auth = cdpb.AAAGetCCAccSession(ro_session->ro_session_id);
if (!auth) {
LM_DBG("Diameter Auth Session has timed out.... creating a new one.\n");
/* lets try and recreate this session */
auth = cdpb.AAAMakeSession(ro_session->auth_appid, ro_session->auth_session_type, ro_session->ro_session_id); //TODO: would like this session to last longer (see session timeout in cdp
if (!auth)
goto error1;
}
if (!(ccr = Ro_new_ccr(auth, ro_ccr_data)))
goto error1;
LM_DBG("Created new CCR\n");
if (!Ro_add_vendor_specific_appid(ccr, IMS_vendor_id_3GPP, IMS_Ro, 0)) {
LM_ERR("Problem adding Vendor specific ID\n");
}
ro_session->hop_by_hop += 1;
if (!Ro_add_cc_request(ccr, RO_CC_STOP, ro_session->hop_by_hop)) {
LM_ERR("Problem adding CC-Request data\n");
}
if (!Ro_add_event_timestamp(ccr, time(NULL))) {
LM_ERR("Problem adding Event-Timestamp data\n");
}
if (!Ro_add_user_equipment_info(ccr, AVP_EPC_User_Equipment_Info_Type_MAC, ro_session->mac)) {
LM_ERR("Problem adding User-Equipment data\n");
}
if (!Ro_add_subscription_id(ccr, subscr.type, &subscr.id)) {
LM_ERR("Problem adding Subscription ID data\n");
}
if (!Ro_add_multiple_service_credit_Control_stop(ccr, used, ro_session->rating_group, ro_session->service_identifier)) {
LM_ERR("Problem adding Multiple Service Credit Control data\n");
}
if (!Ro_add_termination_cause(ccr, TERM_CAUSE_LOGOUT)) {
LM_ERR("problem add Termination cause AVP to STOP record.\n");
}
if (vendor_specific_chargeinfo) {
if (!Ro_add_vendor_specific_termination_cause(ccr, code)) {
LM_ERR("problem add Termination cause AVP to STOP record.\n");
}
if (!Ro_add_vendor_specific_termination_reason(ccr, reason)) {
LM_ERR("problem add Termination cause AVP to STOP record.\n");
}
}
cdpb.AAASessionsUnlock(auth->hash);
if (ro_forced_peer.len > 0) {
ret = cdpb.AAASendMessageToPeer(ccr, &ro_forced_peer, resume_on_termination_ccr, NULL);
} else {
ret = cdpb.AAASendMessage(ccr, resume_on_termination_ccr, NULL);
}
if (ret != 1) {
goto error1;
}
Ro_free_CCR(ro_ccr_data);
counter_inc(ims_charging_cnts_h.final_ccrs);
// counter_add(ims_charging_cnts_h.active_ro_sessions, -1);
return;
error1:
LM_ERR("error on Ro STOP record\n");
Ro_free_CCR(ro_ccr_data);
if (auth) {
cdpb.AAASessionsUnlock(auth->hash);
cdpb.AAADropCCAccSession(auth);
}
error0:
return;
}
static void resume_on_termination_ccr(int is_timeout, void *param, AAAMessage *cca, long elapsed_msecs) {
Ro_CCA_t *ro_cca_data = NULL;
if (is_timeout) {
counter_inc(ims_charging_cnts_h.ccr_timeouts);
LM_ERR("Transaction timeout - did not get CCA\n");
goto error;
}
counter_inc(ims_charging_cnts_h.ccr_replies_received);
counter_add(ims_charging_cnts_h.ccr_response_time, elapsed_msecs);
if (!cca) {
LM_ERR("Error in termination CCR.\n");
counter_inc(ims_charging_cnts_h.failed_final_ccrs);
return;
}
ro_cca_data = Ro_parse_CCA_avps(cca);
if (ro_cca_data == NULL) {
LM_DBG("Could not parse CCA message response.\n");
counter_inc(ims_charging_cnts_h.failed_final_ccrs);
return;
}
if (ro_cca_data->resultcode != 2001) {