예제 #1
0
파일: ta_serv.c 프로젝트: ktosiu/taserver
/* Callback for incoming Test-Request messages */
static int ta_tr_cb( struct msg ** msg, struct avp * avp, struct session * sess, void * opaque, enum disp_action * act)
{
	struct msg *ans, *req;
    struct avp * src = NULL;
    struct avp_hdr * hdr = NULL;
    UsageServerSession* mi;
	
	TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
	
	if (msg == NULL)
		return EINVAL;
	
	/* Value of Origin-Host */
    fprintf(stderr, "Request received from ");
    CHECK_FCT( fd_msg_search_avp ( *msg, ta_origin_host, &src) );
    if (src) {
        CHECK_FCT( fd_msg_avp_hdr( src, &hdr ) );
        fprintf(stderr, "'%.*s'", (int)hdr->avp_value->os.len, hdr->avp_value->os.data);
    } else {
        fprintf(stderr, "no_Origin-Host");
    }
    fprintf(stderr, ", replying...\n");
	
    //get or create the session/session state

    req = *msg;
	/* Create answer header */
	CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
    ans = *msg;

    d_req_type reqType;

    //get the op type
    CHECK_FCT( fd_msg_search_avp ( req, ta_avp_optype, &src) );
    CHECK_FCT( fd_msg_avp_hdr( src, &hdr )  );
    reqType = hdr->avp_value->i32;
    if (reqType==d_start){
        //create the session state
        mi= usageserver_session_alloc();
        mi->leftQuota = DUMMY_INIT_QUOTA;
    }else{
        //get the session state
        fd_sess_state_retrieve(ta_cli_reg, sess, &mi);
    }

    if (reqType!=d_start){//for update, stop, need to update the leftQuota
        //update the left quota in session state, according to the used quota avp in req
        CHECK_FCT( fd_msg_search_avp ( req, ta_avp_usedQuota, &src) );
        CHECK_FCT( fd_msg_avp_hdr( src, &hdr )  );
        uint64_t usedQuota = hdr->avp_value->u64;
        if (mi->leftQuota>=usedQuota){
            mi->leftQuota-=usedQuota;
        }else{
            fprintf(stderr, "fatal, the used should not be larger then the granted last time.");
            mi->leftQuota=0;
        }
    }
    if (reqType!=d_stop){//for start, update, need to reply with grantedQuota
        //set the granted quota AVP according to requested quota and left quota in session state
        CHECK_FCT( fd_msg_search_avp ( req, ta_avp_requestQuota, &src) );
        CHECK_FCT( fd_msg_avp_hdr( src, &hdr )  );
        uint64_t reqAmt = hdr->avp_value->u64;
        uint64_t grantAmt=0;
        if (mi->leftQuota>=reqAmt){
            grantAmt = reqAmt;
        }else{
            grantAmt = mi->leftQuota;
        }
        hdr->avp_value->u64 = grantAmt;
        CHECK_FCT( fd_msg_avp_new ( ta_avp_grantedQuota, 0, &avp ) );
        CHECK_FCT( fd_msg_avp_setvalue( avp, hdr->avp_value ) );
        CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, avp ) );
        fprintf(stderr, "add granted quota avp, %llu.\n", grantAmt);
    }
    fprintf(stderr, "session:leftQuota:%llu\n", mi->leftQuota);
    
	/* Set the Origin-Host, Origin-Realm, Result-Code AVPs */
	CHECK_FCT( fd_msg_rescode_set( ans, "DIAMETER_SUCCESS", NULL, NULL, 1 ) );
    
    //save the session state
    fd_sess_state_store(ta_cli_reg, sess, &mi);
    
    /* Send the answer */
	CHECK_FCT( fd_msg_send( msg, NULL, NULL ) );

	return 0;
}
//Called to send a UAR
int test_sip_SAR_cb()
{
	struct dict_object * sar_model=NULL;
	struct msg * message=NULL;
	struct avp *avp=NULL;
	union avp_value value;
	
	//Fake values START
	char *sip_aor="sip:[email protected]";
	size_t aor_len=strlen(sip_aor); 
	char *destination_realm="tera.ics.keio.ac.jp";
	size_t destination_realmlen=strlen(destination_realm);
	char *destination_host="suika.tera.ics.keio.ac.jp";
	size_t destination_hostlen=strlen(destination_host);
	char *username="******";
	size_t usernamelen=strlen(username);
	char *sipserveruri="sip:[email protected]";
	size_t sipserverurilen=strlen(sipserveruri);
	// char *visitednetwork="Pink";
	//size_t visitednetworklen=strlen(visitednetwork);
	//int registrationtype = 2;
	int data_already_available=0;
	int assignment_type=0;
	//Fake values STOP
	
	//Create the base message for an RTR
	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Server-Assignment-Request", &sar_model, ENOENT) );
	CHECK_FCT( fd_msg_new (sar_model, 0, &message));
	
	
	
	// Create a new session 
	{
		CHECK_FCT( fd_msg_new_session( message, (os0_t)"appsip", CONSTSTRLEN("appsip") ) );
	}
	
	//Add the Auth-Application-Id 
	{
		CHECK_FCT( fd_msg_avp_new ( sip_dict.Auth_Application_Id, 0, &avp ) );
		value.i32 = 6;
		CHECK_FCT( fd_msg_avp_setvalue ( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add ( message, MSG_BRW_LAST_CHILD, avp) );
	}	
	//Auth_Session_State
	{
		CHECK_FCT( fd_msg_avp_new ( sip_dict.Auth_Session_State, 0, &avp ) );
		value.i32=1;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
	}
	
	//Origin_Host & Origin_Realm
	CHECK_FCT( fd_msg_add_origin ( message, 0 ));
	
	//Destination_Host
	{
		CHECK_FCT( fd_msg_avp_new ( sip_dict.Destination_Host, 0, &avp ) );
		value.os.data=(unsigned char *)destination_host;
		value.os.len=destination_hostlen;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
	}
	//Destination_Realm
	{
		CHECK_FCT( fd_msg_avp_new ( sip_dict.Destination_Realm, 0, &avp ) );
		value.os.data=(unsigned char *)destination_realm;
		value.os.len=destination_realmlen;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
	}
	
	//SIP_AOR
	{
		
		CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_AOR, 0, &avp ) );
		value.os.data=(unsigned char *)sip_aor;
		value.os.len=aor_len;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
		
	}
	//Username
	{
		
		CHECK_FCT( fd_msg_avp_new ( sip_dict.User_Name, 0, &avp ) );
		value.os.data=(unsigned char *)username;
		value.os.len=usernamelen;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
		
	}
	//SIP_User_Data_Already_Available
	{
		CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_User_Data_Already_Available, 0, &avp ) );
		value.i32=data_already_available;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
	}
	
	//SIP_Server_Assignment_Type;
	{
		CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Server_Assignment_Type, 0, &avp ) );
		value.i32=assignment_type;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
	}
	//SIP_server_uri
	{
		
		CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Server_URI, 0, &avp ) );
		value.os.data=(unsigned char *)sipserveruri;
		value.os.len=sipserverurilen;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
		
	}
	
	CHECK_FCT( fd_msg_send( &message, NULL, NULL ));
	
	return 0;
}