Brian Waters | 13d9601 | 2017-12-08 16:53:31 -0600 | [diff] [blame] | 1 | /**************** |
| 2 | Contributed by: Krishnan Srinivasan <hsirk_6@yahoo.com> |
| 3 | License: to be specified. |
| 4 | TODO: |
| 5 | |
| 6 | ****************/ |
| 7 | |
| 8 | |
| 9 | #include <freeDiameter/extension.h> |
| 10 | #include <signal.h> |
| 11 | #include <time.h> |
| 12 | #define AUTH_APP_ID 16777238 |
| 13 | #define VENDOR_ID_3GPP 10415 |
| 14 | /* The content of this file follows the same structure as dict_base_proto.c */ |
| 15 | |
| 16 | #if 0 |
| 17 | #define CHECK_dict_new( _type, _data, _parent, _ref ) \ |
| 18 | CHECK_FCT( fd_dict_new( fd_g_config->cnf_dict, (_type), (_data), (_parent), (_ref)) ); |
| 19 | #endif |
| 20 | |
| 21 | void dump_sess_eyec(struct session *sess, const char *); |
| 22 | |
| 23 | static int ccr_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act); |
| 24 | static int reauth_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act); |
| 25 | static int cca_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act); |
| 26 | static struct disp_hdl * ccr_cb_hdl = NULL; /* handler for ccr req cb */ |
| 27 | static struct disp_hdl * cca_cb_hdl = NULL; /* handler for cca req cb */ |
| 28 | static struct disp_hdl * reauth_cb_hdl = NULL; /* handler for cca req cb */ |
| 29 | |
| 30 | struct dict_object *ccr_cmd = NULL; |
| 31 | struct dict_object *cca_cmd = NULL; |
| 32 | struct dict_object *dataobj_re_auth_request_type = NULL; |
| 33 | struct dict_object * origin_host = NULL; |
| 34 | struct dict_object * origin_realm = NULL; |
| 35 | struct dict_object * dest_host = NULL; |
| 36 | struct dict_object * dest_realm = NULL; |
| 37 | struct dict_object *reauth_cmd = NULL; |
| 38 | struct dict_object * auth_app_id = NULL; |
| 39 | struct dict_object * service_cxt_id = NULL ; |
| 40 | struct dict_object * cc_req_type = NULL; |
| 41 | struct dict_object * cc_req_num = NULL; |
| 42 | struct dict_object * bearer_usage = NULL; |
| 43 | struct dict_object * pflt_oper = NULL; |
| 44 | struct dict_object * pflt_info = NULL; |
| 45 | struct dict_object * pflt_id = NULL; |
| 46 | struct dict_object * gx_inf; |
| 47 | struct dict_object * term_cause = NULL; |
| 48 | |
| 49 | struct session *g_sess = NULL; |
| 50 | struct session_handler *g_hdlr = NULL; |
| 51 | |
| 52 | enum gx_state { |
| 53 | STATE_INIT = 0, |
| 54 | STATE_INTERMEDIATE , |
| 55 | STATE_FINAL |
| 56 | }; |
| 57 | struct gx_sm_t { |
| 58 | enum gx_state state; |
| 59 | pthread_t tid; |
| 60 | struct fifo *events; |
| 61 | pthread_mutex_t p_sm_mtx; |
| 62 | int req_num; |
| 63 | struct session *sess; |
| 64 | } g_gx_sm; |
| 65 | |
| 66 | int snd_ccr_msg(struct gx_sm_t **gx_sm , struct dict_object *cmd_r ) ; |
| 67 | |
| 68 | void sig_hdlr(void); |
| 69 | |
| 70 | void *gx_sm_th(void *sm); |
| 71 | #define press_key_continue() { printf("%s %d\n", __FUNCTION__, __LINE__);} |
| 72 | static int app_gx_entry(char * conffile) |
| 73 | { |
| 74 | // TRACE_ENTRY("%p", conffile); |
| 75 | { |
| 76 | application_id_t dcca_id = AUTH_APP_ID; |
| 77 | application_id_t ccr_id = 272; |
| 78 | application_id_t cca_id = 272; |
| 79 | application_id_t reauth_id = 258; |
| 80 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_APPLICATION, APPLICATION_BY_ID, &dcca_id, &gx_inf, ENOENT)); |
| 81 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_CODE_R, &ccr_id, &ccr_cmd, ENOENT)); |
| 82 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_CODE_A, &cca_id, &cca_cmd, ENOENT)); |
| 83 | // CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_CODE_R, &reauth_id, &reauth_cmd, ENOENT)); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | /* Applications section */ |
| 88 | #if 0 |
| 89 | { |
| 90 | // Gx interface |
| 91 | { |
| 92 | struct dict_application_data data = { AUTH_APP_ID, "3GPP-Gx Application" }; |
| 93 | CHECK_dict_new( DICT_APPLICATION, &data, NULL, &gx_inf); |
| 94 | |
| 95 | } |
| 96 | |
| 97 | } |
| 98 | #endif |
| 99 | |
| 100 | |
| 101 | // Do registeration and init stuff |
| 102 | { |
| 103 | struct disp_when data; |
| 104 | |
| 105 | TRACE_DEBUG(FULL, "Initializing dispatch callbacks for Gx interface"); |
| 106 | |
| 107 | memset(&data, 0, sizeof(data)); |
| 108 | data.app = gx_inf; |
| 109 | data.command = ccr_cmd; |
| 110 | /* Now specific handler for CCR-CMD */ |
| 111 | CHECK_FCT( fd_disp_register( ccr_cb, DISP_HOW_CC, &data, NULL, &ccr_cb_hdl ) ); |
| 112 | |
| 113 | |
| 114 | memset(&data, 0, sizeof(data)); |
| 115 | data.app = gx_inf; |
| 116 | data.command = cca_cmd; |
| 117 | |
| 118 | CHECK_FCT( fd_disp_register( cca_cb, DISP_HOW_CC, &data, NULL, &cca_cb_hdl ) ); |
| 119 | |
| 120 | #ifdef REAUTH |
| 121 | CHECK_FCT(fd_dict_search(fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, |
| 122 | "Re-Auth-Request", &reauth_cmd, ENOENT)); |
| 123 | memset(&data, 0, sizeof(data)); |
| 124 | data.app = gx_inf; |
| 125 | data.command = reauth_cmd; |
| 126 | printf("register REAUTH\n"); |
| 127 | CHECK_FCT( fd_disp_register( reauth_cb, DISP_HOW_CC, &data, NULL, &reauth_cb_hdl ) ); |
| 128 | #endif |
| 129 | |
| 130 | |
| 131 | } |
| 132 | |
| 133 | |
| 134 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 135 | DICT_AVP, AVP_BY_NAME, |
| 136 | "Origin-Host", |
| 137 | &origin_host, |
| 138 | ENOENT) ); |
| 139 | |
| 140 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 141 | DICT_AVP, |
| 142 | AVP_BY_NAME, |
| 143 | "Origin-Realm", |
| 144 | &origin_realm, |
| 145 | ENOENT) ); |
| 146 | |
| 147 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 148 | DICT_AVP, |
| 149 | AVP_BY_NAME, |
| 150 | "Destination-Host", |
| 151 | &dest_host, |
| 152 | ENOENT) ); |
| 153 | |
| 154 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 155 | DICT_AVP, |
| 156 | AVP_BY_NAME, |
| 157 | "Destination-Realm", |
| 158 | &dest_realm, |
| 159 | ENOENT) ); |
| 160 | |
| 161 | |
| 162 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 163 | DICT_AVP, |
| 164 | AVP_BY_NAME, |
| 165 | "Auth-Application-Id", |
| 166 | &auth_app_id, |
| 167 | ENOENT) ); |
| 168 | |
| 169 | |
| 170 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 171 | DICT_AVP, |
| 172 | AVP_BY_NAME, |
| 173 | "Service-Context-Id", |
| 174 | &service_cxt_id, |
| 175 | ENOENT) ); |
| 176 | |
| 177 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 178 | DICT_AVP, |
| 179 | AVP_BY_NAME, |
| 180 | "CC-Request-Type", |
| 181 | &cc_req_type, |
| 182 | ENOENT) ); |
| 183 | |
| 184 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 185 | DICT_AVP, |
| 186 | AVP_BY_NAME, |
| 187 | "Termination-Cause", |
| 188 | &term_cause, |
| 189 | ENOENT) ); |
| 190 | |
| 191 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 192 | DICT_AVP, |
| 193 | AVP_BY_NAME, |
| 194 | "CC-Request-Number", |
| 195 | &cc_req_num, |
| 196 | ENOENT) ); |
| 197 | { |
| 198 | struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Bearer-Usage"}; |
| 199 | |
| 200 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 201 | DICT_AVP, |
| 202 | AVP_BY_NAME_AND_VENDOR, |
| 203 | &req, |
| 204 | &bearer_usage, |
| 205 | ENOENT) ); |
| 206 | } |
| 207 | { |
| 208 | struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Packet-Filter-Operation"}; |
| 209 | |
| 210 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 211 | DICT_AVP, |
| 212 | AVP_BY_NAME_AND_VENDOR, |
| 213 | &req, |
| 214 | &pflt_oper, |
| 215 | ENOENT) ); |
| 216 | } |
| 217 | { |
| 218 | struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Packet-Filter-Information"}; |
| 219 | |
| 220 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 221 | DICT_AVP, |
| 222 | AVP_BY_NAME_AND_VENDOR, |
| 223 | &req, |
| 224 | &pflt_info, |
| 225 | ENOENT) ); |
| 226 | } |
| 227 | { |
| 228 | struct dict_avp_request req = { VENDOR_ID_3GPP, 0, "Packet-Filter-Identifier"}; |
| 229 | |
| 230 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, |
| 231 | DICT_AVP, |
| 232 | AVP_BY_NAME_AND_VENDOR, |
| 233 | &req, |
| 234 | &pflt_id, |
| 235 | ENOENT) ); |
| 236 | } |
| 237 | |
| 238 | CHECK_FCT(fd_sess_handler_create( &g_hdlr, free, NULL)); |
| 239 | CHECK_FCT( fd_fifo_new(&g_gx_sm.events)); |
| 240 | CHECK_FCT( fd_disp_app_support( gx_inf, NULL, 1 , 0)); |
| 241 | |
| 242 | CHECK_FCT( fd_event_trig_regcb(SIGUSR1, "app_gx", sig_hdlr ) ); |
| 243 | |
| 244 | |
| 245 | TRACE_DEBUG(INFO, "Extension 'Gx' initialized"); |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | void * gx_sm_th(void *sm) |
| 250 | { |
| 251 | struct gx_sm_t *gx_sm = (struct gx_sm_t *) sm; |
| 252 | struct timespec tout; |
| 253 | int evt_code; |
| 254 | |
| 255 | CHECK_SYS_DO( clock_gettime( CLOCK_REALTIME, &tout), goto out); |
| 256 | tout.tv_sec =+ 60 ; |
| 257 | |
| 258 | while(1) { |
| 259 | |
| 260 | fd_event_timedget(gx_sm->events, &tout , ETIMEDOUT, &evt_code, NULL, NULL ); |
| 261 | CHECK_SYS_DO( clock_gettime( CLOCK_REALTIME, &tout), goto out); |
| 262 | printf("in tout sec %d\n", tout.tv_sec); |
| 263 | if(evt_code == ETIMEDOUT) { |
| 264 | |
| 265 | snd_ccr_msg(&gx_sm, ccr_cmd); |
| 266 | gx_sm->req_num++ ; |
| 267 | gx_sm->state = STATE_INTERMEDIATE; |
| 268 | CHECK_SYS_DO( clock_gettime( CLOCK_REALTIME, &tout), goto out); |
| 269 | tout.tv_sec += 30 ; |
| 270 | |
| 271 | } |
| 272 | // printf("press enter\n"); |
| 273 | // getchar(); |
| 274 | } |
| 275 | |
| 276 | out: |
| 277 | return NULL; |
| 278 | } |
| 279 | //TBD |
| 280 | int init_gx_sm(struct gx_sm_t *sm) |
| 281 | { |
| 282 | sm->state = STATE_INIT; |
| 283 | sm->tid = 0;// |
| 284 | sm->events = NULL; |
| 285 | pthread_mutex_t p_sm_mtx; |
| 286 | sm->req_num = 0; |
| 287 | sm->sess = NULL; |
| 288 | |
| 289 | return 0; |
| 290 | } |
| 291 | int free_gx_sm(struct gx_sm_t *sm) |
| 292 | { |
| 293 | |
| 294 | free(sm); |
| 295 | } |
| 296 | struct gx_sm_t *gl_gx_sm = NULL; |
| 297 | |
| 298 | void sig_hdlr() |
| 299 | { |
| 300 | struct gx_sm_t *gx_sm = gl_gx_sm; |
| 301 | |
| 302 | if( gx_sm) { |
| 303 | |
| 304 | fd_sess_dump( 0 , g_gx_sm.sess); |
| 305 | |
| 306 | } else { |
| 307 | |
| 308 | if(gx_sm= (struct gx_sm_t *)malloc(sizeof(struct gx_sm_t))) { |
| 309 | init_gx_sm(gx_sm); |
| 310 | } |
| 311 | gl_gx_sm = gx_sm; |
| 312 | } |
| 313 | |
| 314 | snd_ccr_msg( &gx_sm, ccr_cmd); |
| 315 | return; |
| 316 | } |
| 317 | |
| 318 | static void cr_cb_ans(void *data, struct msg **msg) |
| 319 | { |
| 320 | printf("call back \n"); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | /* < Session-Id > |
| 325 | { Origin-Host } |
| 326 | { Origin-Realm } |
| 327 | { Destination-Realm } |
| 328 | { Auth-Application-Id } |
| 329 | { Service-Context-Id } |
| 330 | { CC-Request-Type } |
| 331 | { CC-Request-Number } |
| 332 | */ |
| 333 | |
| 334 | int snd_ccr_msg(struct gx_sm_t **sm , struct dict_object *cmd_r ) |
| 335 | { |
| 336 | struct msg * req = NULL; |
| 337 | struct avp * avp = NULL; |
| 338 | union avp_value val; |
| 339 | struct ta_mess_info * mi = NULL, *svg; |
| 340 | struct session *sess = NULL; |
| 341 | struct gx_sm_t *gx_sm = NULL; |
| 342 | struct gx_sm_t *ptr = NULL; |
| 343 | |
| 344 | TRACE_DEBUG(FULL, "Creating a new CCR message for sending. %p", gx_sm); |
| 345 | |
| 346 | /* Create the request from template */ |
| 347 | CHECK_FCT_DO( fd_msg_new( cmd_r, MSGFL_ALLOC_ETEID, &req ), goto out ); |
| 348 | |
| 349 | gx_sm = *sm; |
| 350 | /* Create a new session */ |
| 351 | if(!gx_sm->sess) { |
| 352 | |
| 353 | CHECK_FCT_DO( fd_sess_new( &sess, |
| 354 | fd_g_config->cnf_diamid, |
| 355 | fd_g_config->cnf_diamid_len, |
| 356 | "CCR_SESSION", strlen("CCR_SESSION") ), goto out ); |
| 357 | |
| 358 | printf("statemachine: %p %p %p\n", *(&gx_sm), gx_sm , *sm); |
| 359 | gx_sm->sess = sess; |
| 360 | printf("new session %p \n", sess); |
| 361 | //Hold the session till terminate happens |
| 362 | CHECK_FCT( fd_sess_ref_msg(sess) ); |
| 363 | |
| 364 | } else { |
| 365 | |
| 366 | sess = gx_sm->sess; |
| 367 | printf("use previous session %p \n", sess); |
| 368 | |
| 369 | } |
| 370 | |
| 371 | fd_sess_dump( 0 , sess); |
| 372 | |
| 373 | |
| 374 | // dump_sess_eyec( sess, __FUNCTION__); |
| 375 | /* Now set all AVPs values */ |
| 376 | |
| 377 | /* Session-Id */ |
| 378 | { |
| 379 | os0_t sid; |
| 380 | size_t sidlen; |
| 381 | struct dict_object *sess_id = NULL; |
| 382 | |
| 383 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict |
| 384 | , DICT_AVP, AVP_BY_NAME |
| 385 | , "Session-Id" |
| 386 | , &sess_id, ENOENT) ); |
| 387 | |
| 388 | CHECK_FCT_DO( fd_sess_getsid ( sess, &sid, &sidlen ), goto out ); |
| 389 | CHECK_FCT_DO( fd_msg_avp_new ( sess_id, 0, &avp ), goto out ); |
| 390 | val.os.data = sid; |
| 391 | val.os.len = sidlen; |
| 392 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 393 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_FIRST_CHILD, avp ), goto out ); |
| 394 | } |
| 395 | |
| 396 | /* Set the Destination-Realm AVP */ |
| 397 | { |
| 398 | CHECK_FCT_DO( fd_msg_avp_new ( dest_realm, 0, &avp ), goto out ); |
| 399 | val.os.data = (unsigned char *)("vm"); |
| 400 | val.os.len = strlen("vm"); |
| 401 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 402 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 403 | } |
| 404 | |
| 405 | |
| 406 | /* Set the Destination-Host AVP if needed*/ |
| 407 | { |
| 408 | CHECK_FCT_DO( fd_msg_avp_new ( dest_host, 0, &avp ), goto out ); |
| 409 | val.os.data = (unsigned char *)("192.168.101.3"); |
| 410 | val.os.len = strlen("192.168.101.3"); |
| 411 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 412 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 413 | } |
| 414 | |
| 415 | |
| 416 | /* Set Origin-Host & Origin-Realm */ |
| 417 | CHECK_FCT_DO( fd_msg_add_origin ( req, 0 ), goto out ); |
| 418 | |
| 419 | |
| 420 | /* Set Auth-Application ID */ |
| 421 | { |
| 422 | CHECK_FCT_DO( fd_msg_avp_new ( auth_app_id, 0, &avp ), goto out ); |
| 423 | val.i32 = AUTH_APP_ID; // Auth-App id is 4 for CCR |
| 424 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 425 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 426 | } |
| 427 | |
| 428 | /* Set Service Context ID */ |
| 429 | { |
| 430 | |
| 431 | CHECK_FCT_DO( fd_msg_avp_new ( service_cxt_id, 0, &avp ), goto out ); |
| 432 | val.os.data = (unsigned char *)("test@tst"); |
| 433 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 434 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 435 | |
| 436 | } |
| 437 | |
| 438 | /* Set Request Type */ |
| 439 | { |
| 440 | #define CCR_INIT_REQUEST 1 |
| 441 | #define CCR_UPDATE_REQUEST 2 |
| 442 | #define CCR_TERMINATION_REQUEST 3 |
| 443 | #define CCR_EVENT_REQUEST 4 |
| 444 | //TODO Change this to use enum object |
| 445 | CHECK_FCT_DO( fd_msg_avp_new ( cc_req_type, 0, &avp ), goto out ); |
| 446 | if(gx_sm->state == STATE_INIT) |
| 447 | val.i32 = CCR_INIT_REQUEST; |
| 448 | else if(gx_sm->state == STATE_FINAL) |
| 449 | val.i32 = CCR_TERMINATION_REQUEST; |
| 450 | else |
| 451 | val.i32 = CCR_UPDATE_REQUEST; |
| 452 | |
| 453 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 454 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 455 | |
| 456 | } |
| 457 | |
| 458 | /* Set Request Number */ |
| 459 | { |
| 460 | CHECK_FCT_DO( fd_msg_avp_new ( cc_req_num, 0, &avp ), goto out ); |
| 461 | val.i32 = gx_sm->req_num; |
| 462 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 463 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 464 | gx_sm->req_num++; |
| 465 | } |
| 466 | |
| 467 | switch(gx_sm->state) { |
| 468 | |
| 469 | case STATE_INIT: |
| 470 | |
| 471 | { |
| 472 | //Set Bearer-Usage |
| 473 | //TODO Change this to use enum object |
| 474 | CHECK_FCT_DO( fd_msg_avp_new ( bearer_usage, 0, &avp ), goto out ); |
| 475 | val.i32 = 1;//IMS |
| 476 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 477 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 478 | } |
| 479 | { |
| 480 | //Set Packet Filter Operation |
| 481 | //TODO Change this to use enum object |
| 482 | CHECK_FCT_DO( fd_msg_avp_new ( pflt_oper, 0, &avp ), goto out ); |
| 483 | val.i32 = 1;//ADDITION |
| 484 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 485 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 486 | } |
| 487 | struct avp *flt_info = NULL; |
| 488 | { |
| 489 | //Set Packet Filter Information |
| 490 | CHECK_FCT_DO( fd_msg_avp_new ( pflt_info, 0, &flt_info ), goto out ); |
| 491 | |
| 492 | |
| 493 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, flt_info ), goto out ); |
| 494 | } |
| 495 | // Set Packet Filter Identity |
| 496 | { |
| 497 | |
| 498 | CHECK_FCT_DO( fd_msg_avp_new ( pflt_id, 0, &avp ), goto out ); |
| 499 | val.os.data = (unsigned char *)("ID"); |
| 500 | val.os.len = strlen("ID"); |
| 501 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 502 | if(flt_info) { |
| 503 | CHECK_FCT_DO( fd_msg_avp_add( flt_info, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 504 | }else { |
| 505 | printf("flt_info NULL\n"); |
| 506 | } |
| 507 | |
| 508 | } |
| 509 | CHECK_FCT(fd_sess_state_store(g_hdlr, sess, &gx_sm)); |
| 510 | |
| 511 | break; |
| 512 | case STATE_FINAL: |
| 513 | { |
| 514 | //Set Packet Filter Operation |
| 515 | //TODO Change this to use enum object |
| 516 | CHECK_FCT_DO( fd_msg_avp_new ( term_cause, 0, &avp ), goto out ); |
| 517 | val.i32 = 1;//Diameter logout |
| 518 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 519 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 520 | } |
| 521 | break; |
| 522 | default: |
| 523 | printf("State mismatch \n"); |
| 524 | } |
| 525 | fflush(stderr); |
| 526 | |
| 527 | /* Send the request */ |
| 528 | printf("CCA %p\n",req); |
| 529 | // Everthing Done Store the state: reply should retreive it |
| 530 | CHECK_FCT_DO( fd_msg_send( &req, NULL, NULL ), goto out ); |
| 531 | |
| 532 | out: |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | static int reauth_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act) |
| 538 | { |
| 539 | struct msg *ans, *qry; |
| 540 | struct avp * a; |
| 541 | |
| 542 | TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act); |
| 543 | |
| 544 | if (msg == NULL) |
| 545 | return EINVAL; |
| 546 | |
| 547 | |
| 548 | /* Create answer header */ |
| 549 | qry = *msg; |
| 550 | CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) ); |
| 551 | ans = *msg; |
| 552 | |
| 553 | /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */ |
| 554 | CHECK_FCT( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ) ); |
| 555 | |
| 556 | /* Send the answer */ |
| 557 | CHECK_FCT( fd_msg_send( msg, NULL, NULL ) ); |
| 558 | |
| 559 | |
| 560 | |
| 561 | } |
| 562 | static int dcca_ans_from_req( struct dict_object * obj, struct msg *qry, struct msg **msg) |
| 563 | { |
| 564 | struct avp *avp = NULL; |
| 565 | struct avp_hdr * avpdata; |
| 566 | int rc = -1; |
| 567 | |
| 568 | CHECK_FCT(fd_msg_search_avp( qry, obj, &avp)); |
| 569 | CHECK_FCT(fd_msg_avp_hdr(avp, &avpdata)); |
| 570 | avp = NULL; |
| 571 | |
| 572 | CHECK_FCT_DO( fd_msg_avp_new ( obj, 0, &avp ), goto out ); |
| 573 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, avpdata->avp_value ), goto out ); |
| 574 | CHECK_FCT_DO( fd_msg_avp_add( *msg, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 575 | |
| 576 | rc = 0; |
| 577 | out: |
| 578 | return rc; |
| 579 | |
| 580 | } |
| 581 | /* Dummy ccr which : |
| 582 | Read the cc-req-type && cc-req-number from the msg and stick it back |
| 583 | in the reply |
| 584 | |
| 585 | */ |
| 586 | static int ccr_cb( struct msg ** msg, struct avp * r_avp, struct session * sess, void * opaque, enum disp_action * act) |
| 587 | { |
| 588 | struct msg *ans, *qry; |
| 589 | struct avp *avp = NULL; |
| 590 | struct avp_hdr * avp_data; |
| 591 | int rc = - 1; |
| 592 | |
| 593 | TRACE_ENTRY("%p %p %p %p", msg, r_avp, sess, act); |
| 594 | |
| 595 | if (msg == NULL) |
| 596 | return EINVAL; |
| 597 | |
| 598 | |
| 599 | /* Create answer header */ |
| 600 | qry = *msg; |
| 601 | CHECK_FCT_DO( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) , goto out); |
| 602 | ans = *msg; |
| 603 | |
| 604 | /* Set the Origin-Host, Origin-Realm, Result-Code AVPs */ |
| 605 | CHECK_FCT_DO( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ), goto out ); |
| 606 | |
| 607 | // Get the auth_app_id and from the reply and set it in the reply |
| 608 | CHECK_FCT_DO(dcca_ans_from_req( auth_app_id, qry, msg), goto out); |
| 609 | CHECK_FCT_DO(dcca_ans_from_req( cc_req_type, qry, msg), goto out); |
| 610 | CHECK_FCT_DO(dcca_ans_from_req( cc_req_num, qry, msg), goto out); |
| 611 | |
| 612 | /* Send the answer */ |
| 613 | CHECK_FCT_DO( fd_msg_send( msg, NULL, NULL ), goto out ); |
| 614 | rc = 0; |
| 615 | out: |
| 616 | //Free up the memory |
| 617 | return rc ; |
| 618 | |
| 619 | } |
| 620 | int send_reauth_req() |
| 621 | |
| 622 | { |
| 623 | struct dict_application_data appdata; |
| 624 | struct avp *avp = NULL; |
| 625 | union avp_value val ; |
| 626 | struct msg *qry, *ans = NULL; |
| 627 | struct msg *req = NULL; |
| 628 | struct msg *tst = NULL; |
| 629 | struct dict_object * auth_app_id = NULL, *reauth_req_type = NULL; |
| 630 | |
| 631 | // qry = *msg; |
| 632 | |
| 633 | |
| 634 | { // Send new reauth request |
| 635 | |
| 636 | CHECK_FCT_DO( fd_msg_new( reauth_cmd, MSGFL_ALLOC_ETEID, &req ), goto out ); |
| 637 | CHECK_FCT( fd_dict_getval(gx_inf, &appdata) ); |
| 638 | |
| 639 | struct msg_hdr * header = NULL; |
| 640 | CHECK_FCT( fd_msg_hdr ( req, &header ) ); |
| 641 | header->msg_appl = appdata.application_id; |
| 642 | |
| 643 | } |
| 644 | |
| 645 | /* Session-Id */ |
| 646 | { |
| 647 | os0_t sid; |
| 648 | size_t sidlen; |
| 649 | struct dict_object *sess_id = NULL; |
| 650 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Session-Id", &sess_id, ENOENT) ); |
| 651 | |
| 652 | //CHECK_FCT_DO( fd_sess_getsid ( sess, &sid, &sidlen ), goto out ); |
| 653 | CHECK_FCT_DO( fd_msg_avp_new ( sess_id, 0, &avp ), goto out ); |
| 654 | val.os.data = sid; |
| 655 | val.os.len = sidlen; |
| 656 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 657 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_FIRST_CHILD, avp ), goto out ); |
| 658 | |
| 659 | } |
| 660 | |
| 661 | /* Set the Destination-Realm AVP */ |
| 662 | { |
| 663 | CHECK_FCT_DO( fd_msg_avp_new ( dest_realm, 0, &avp ), goto out ); |
| 664 | val.os.data = (unsigned char *)("vm"); |
| 665 | val.os.len = strlen("vm"); |
| 666 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 667 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 668 | } |
| 669 | |
| 670 | /* Set the Destination-Host AVP if needed*/ |
| 671 | // if (ta_conf->dest_host) { |
| 672 | CHECK_FCT_DO( fd_msg_avp_new ( dest_host, 0, &avp ), goto out ); |
| 673 | val.os.data = (unsigned char *)("cli.vm"); |
| 674 | val.os.len = strlen("cli.vm"); |
| 675 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 676 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 677 | // } |
| 678 | |
| 679 | /* Set Origin-Host & Origin-Realm */ |
| 680 | CHECK_FCT_DO( fd_msg_add_origin ( req, 0 ), goto out ); |
| 681 | |
| 682 | /* AUTH_Application-ID */ |
| 683 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Auth-Application-Id", &auth_app_id, ENOENT) ); |
| 684 | CHECK_FCT_DO( fd_msg_avp_new ( auth_app_id, 0, &avp ), goto out ); |
| 685 | val.i32 = appdata.application_id; |
| 686 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 687 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 688 | |
| 689 | /* Re-Auth Request Type */ |
| 690 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_AVP, AVP_BY_NAME, "Re-Auth-Request-Type", &reauth_req_type, ENOENT) ); |
| 691 | CHECK_FCT_DO( fd_msg_avp_new ( reauth_req_type, 0, &avp ), goto out ); |
| 692 | val.i32 = 0; |
| 693 | CHECK_FCT_DO( fd_msg_avp_setvalue( avp, &val ), goto out ); |
| 694 | CHECK_FCT_DO( fd_msg_avp_add( req, MSG_BRW_LAST_CHILD, avp ), goto out ); |
| 695 | |
| 696 | |
| 697 | CHECK_FCT_DO( fd_msg_send( &req, cr_cb_ans, NULL ), goto out ); |
| 698 | out: |
| 699 | return 0 ; |
| 700 | |
| 701 | |
| 702 | } |
| 703 | /* Search a given AVP model in an AVP (extracted from libfreediameter/message.c ) */ |
| 704 | int fd_avp_search_avp ( struct avp * groupedavp, struct dict_object * what, struct avp ** avp ) |
| 705 | { |
| 706 | struct avp * nextavp; |
| 707 | struct avp_hdr * nextavphdr; |
| 708 | struct dict_avp_data dictdata; |
| 709 | |
| 710 | |
| 711 | TRACE_ENTRY("%p %p %p", groupedavp, what, avp); |
| 712 | |
| 713 | CHECK_FCT( fd_dict_getval(what, &dictdata) ); |
| 714 | |
| 715 | // Loop only in the group AVP |
| 716 | CHECK_FCT( fd_msg_browse(groupedavp, MSG_BRW_FIRST_CHILD, (void *)&nextavp, NULL) ); |
| 717 | CHECK_FCT( fd_msg_avp_hdr( nextavp, &nextavphdr ) ); |
| 718 | |
| 719 | while (nextavphdr) { |
| 720 | |
| 721 | if ( (nextavphdr->avp_code == dictdata.avp_code) && (nextavphdr->avp_vendor == dictdata.avp_vendor) ) // always 0 if no Vendor flag |
| 722 | { |
| 723 | break; |
| 724 | } |
| 725 | |
| 726 | // Otherwise move to next AVP in the grouped AVP |
| 727 | CHECK_FCT( fd_msg_browse(nextavp, MSG_BRW_NEXT, (void *)&nextavp, NULL) ); |
| 728 | |
| 729 | if(nextavp!=NULL) |
| 730 | { |
| 731 | CHECK_FCT( fd_msg_avp_hdr( nextavp, &nextavphdr ) ); |
| 732 | } |
| 733 | else |
| 734 | nextavphdr=NULL; |
| 735 | } |
| 736 | if (avp) |
| 737 | *avp = nextavp; |
| 738 | |
| 739 | if (avp && nextavp) { |
| 740 | struct dictionary * dict; |
| 741 | CHECK_FCT( fd_dict_getdict( what, &dict) ); |
| 742 | CHECK_FCT_DO( fd_msg_parse_dict( nextavp, dict, NULL ), ); |
| 743 | } |
| 744 | |
| 745 | if (avp || nextavp) |
| 746 | return 0; |
| 747 | else |
| 748 | return ENOENT; |
| 749 | } |
| 750 | |
| 751 | static int cca_cb( struct msg ** msg, |
| 752 | struct avp * t_avp , |
| 753 | struct session * sess, |
| 754 | void * opaque, |
| 755 | enum disp_action * act) |
| 756 | { |
| 757 | struct avp *avp = NULL, *g_avp = NULL; |
| 758 | struct msg *req = *msg; |
| 759 | struct dict_object *chrg_rule_name = NULL ; |
| 760 | struct dict_object *chrg_rule_grp = NULL ; |
| 761 | struct avp_hdr * avpdata = NULL; |
| 762 | struct dict_avp_request grule_req = {VENDOR_ID_3GPP, 0,"Charging-Rule-Install"}; |
| 763 | struct dict_avp_request rule_req = {VENDOR_ID_3GPP, 0,"Charging-Rule-Name"}; |
| 764 | struct timespec sess_timeout; |
| 765 | struct gx_sm_t *gx_sm = NULL; |
| 766 | // struct session *sess = NULL; |
| 767 | |
| 768 | |
| 769 | CHECK_FCT(fd_sess_state_retrieve( g_hdlr, sess, &gx_sm)); |
| 770 | fd_sess_dump( 0, sess); |
| 771 | if(gx_sm->state != STATE_FINAL) { |
| 772 | |
| 773 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict |
| 774 | , DICT_AVP, AVP_BY_NAME_AND_VENDOR |
| 775 | , &grule_req |
| 776 | , &chrg_rule_grp |
| 777 | , ENOENT)); |
| 778 | |
| 779 | CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict |
| 780 | , DICT_AVP, AVP_BY_NAME_AND_VENDOR |
| 781 | , &rule_req |
| 782 | , &chrg_rule_name |
| 783 | , ENOENT)); |
| 784 | |
| 785 | |
| 786 | CHECK_FCT(fd_msg_search_avp ( *msg, chrg_rule_grp, &g_avp )); |
| 787 | CHECK_FCT(fd_avp_search_avp(g_avp, chrg_rule_name, &avp)); |
| 788 | |
| 789 | if(avp) { |
| 790 | CHECK_FCT(fd_msg_avp_hdr(avp, &avpdata)); |
| 791 | } else { |
| 792 | printf("NULL AVP \n"); |
| 793 | } |
| 794 | printf("charging-rule-name %s\n", avpdata->avp_value->os.data); |
| 795 | gx_sm->state = STATE_FINAL; |
| 796 | dump_sess_eyec( sess, __FUNCTION__); |
| 797 | printf("next dump\n"); |
| 798 | CHECK_FCT(fd_sess_state_store( g_hdlr, sess, &gx_sm)); |
| 799 | fd_msg_free(*msg); |
| 800 | *msg = NULL; |
| 801 | |
| 802 | } else { |
| 803 | printf("Session terminated\n"); |
| 804 | free_gx_sm(gx_sm); |
| 805 | fd_msg_free(*msg); |
| 806 | fd_sess_reclaim(&sess); |
| 807 | *msg = NULL; |
| 808 | } |
| 809 | fd_sess_dump( 0 , sess); |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static int gx_entry(char * conffile) |
| 815 | { |
| 816 | return 0; |
| 817 | } |
| 818 | EXTENSION_ENTRY( "app_gx", app_gx_entry, "dict_dcca_3gpp"); |
| 819 | //EXTENSION_ENTRY( "app_gx", gx_entry); |