/*********************************************************************************************************
* Software License Agreement (BSD License)                                                               *
* Author: Alexandre Westfahl <awestfahl@freediameter.net>						 *
*													 *
* Copyright (c) 2010, Alexandre Westfahl, Teraoka Laboratory (Keio University), and the WIDE Project. 	 *		
*													 *
* All rights reserved.											 *
* 													 *
* Redistribution and use of this software in source and binary forms, with or without modification, are  *
* permitted provided that the following conditions are met:						 *
* 													 *
* * Redistributions of source code must retain the above 						 *
*   copyright notice, this list of conditions and the 							 *
*   following disclaimer.										 *
*    													 *
* * Redistributions in binary form must reproduce the above 						 *
*   copyright notice, this list of conditions and the 							 *
*   following disclaimer in the documentation and/or other						 *
*   materials provided with the distribution.								 *
* 													 *
* * Neither the name of the Teraoka Laboratory nor the 							 *
*   names of its contributors may be used to endorse or 						 *
*   promote products derived from this software without 						 *
*   specific prior written permission of Teraoka Laboratory 						 *
*   													 *
* 													 *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED *
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR *
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 	 *
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 	 *
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF   *
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.								 *
*********************************************************************************************************/
#include "app_sip.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
typedef int SOCKET;
typedef struct sockaddr_in SOCKADDR_IN;
typedef struct sockaddr SOCKADDR;

//Procedure which always wait for data on socket 
void *rtr_socket(void *arg)
{
	SOCKET sock;
	SOCKADDR_IN sin, csin;
	struct rtrsipaor rtrsip;
	int rcvbytes=0;
	sock = socket(AF_INET, SOCK_STREAM, 0);
	sin.sin_addr.s_addr = inet_addr("127.0.0.1");
	sin.sin_family = AF_INET;
	sin.sin_port = htons(as_conf->rtr_port);
	socklen_t sinsize = sizeof(csin);
	int accepted=0;

	if(!bind(sock, (SOCKADDR*)&sin, sizeof(sin)))
	{
		if(listen(sock,1))
		{
			TRACE_DEBUG(INFO,"ERROR on listen!");
		}
		
		while(1)
		{
			accepted=accept(sock, (struct sockaddr *)&csin,&sinsize);
			if(accepted>-1)
			{
				rcvbytes=recv(accepted, &rtrsip, sizeof(struct rtrsipaor),0);
				
				if(rcvbytes>-1)
				{
					//We received something, we can send an RTR
					app_sip_RTR_cb(&rtrsip);
				}
			}
		}
	}
	else
		TRACE_DEBUG(INFO,"Can't create socket!");

	
	pthread_exit(NULL);
	
}
//Called to send a RTR
int app_sip_RTR_cb(struct rtrsipaor *structure)
{
	TRACE_ENTRY("%p", structure);
	
	int got_username=0;
	int got_streason=0;
	int num_aor=0;//How many SIP-AOR?
	struct dict_object * rtr_model=NULL;
	struct msg * message=NULL;
	struct avp *groupedavp=NULL, *avp=NULL;
	union avp_value value;
	
	//We must check that we have all needed value in structure
	if(structure->username[0]!='\0')
		got_username=1;
	
	if(structure->sip_aor1[0]!='\0')
	{	
		num_aor++;
		if(structure->sip_aor2[0]!='\0')
		{
			num_aor++;
			if(structure->sip_aor3[0]!='\0')
				num_aor++;
		}
	}
	
	if(structure->strreason[0]!='\0')
		got_streason=1;
	
	
	TRACE_DEBUG(FULL,"Request for %d SIP_AOR to be deregistred.",num_aor);
	
	if((got_username + num_aor)==0)
	{
		//We must have a least a SIP_AOR or a Username
		TRACE_DEBUG(INFO,"Can not proceed because there is no SIP_AOR or Username");
		return EINVAL;
	}
	if(structure->reason<0)
	{
		//We must have a least a SIP_AOR or a Username
		TRACE_DEBUG(INFO,"Incorrect Reason-Code");
		return EINVAL;
	}
	
	if(structure->desthost[0]=='\0')
	{
		//We must have a least a SIP_AOR or a Username
		TRACE_DEBUG(INFO,"No Destination_Host was provided!");
		return EINVAL;
	}
	//Create the base message for an RTR
	CHECK_FCT( fd_dict_search( fd_g_config->cnf_dict, DICT_COMMAND, CMD_BY_NAME, "Registration-Termination-Request", &rtr_model, ENOENT) );
	CHECK_FCT( fd_msg_new (rtr_model, 0, &message));
	
	// Create a new session 
	{
		#define APP_SIP_SID_OPT  "app_sip"
		CHECK_FCT( fd_msg_new_session( message, (os0_t)APP_SIP_SID_OPT, CONSTSTRLEN(APP_SIP_SID_OPT) ) );
	}
	
	//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 *)structure->desthost;
		value.os.len=(size_t)strlen(structure->desthost);
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
	}
	
	
	//SIP Deregistration Reason (Grouped AVP)
	{
		CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Deregistration_Reason, 0, &groupedavp ) );
		
		//Reason Code
		CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Reason_Code, 0, &avp ) );
		value.i32=structure->reason;
		CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
		CHECK_FCT( fd_msg_avp_add( groupedavp, MSG_BRW_LAST_CHILD, avp ) );
		
		if(got_streason)
		{
			//Reason Info
			CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_Reason_Info, 0, &avp ) );
			value.os.data=(unsigned char *)structure->strreason;
			value.os.len=(size_t)strlen(structure->strreason);
			CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
			CHECK_FCT( fd_msg_avp_add( groupedavp, MSG_BRW_LAST_CHILD, avp ) );
		}
		
		//We add the grouped AVP to the message
		CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, groupedavp ) );
	}
	
	//Username
	{
		if(got_username)
		{
			CHECK_FCT( fd_msg_avp_new ( sip_dict.User_Name, 0, &avp ) );
			value.os.data=(unsigned char *)structure->username;
			value.os.len=(size_t)strlen(structure->username);
			CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
			CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
		}
	}
	
	//SIP_AOR
	{
		if(num_aor>0)
		{
			CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_AOR, 0, &avp ) );
			value.os.data=(unsigned char *)structure->sip_aor1;
			value.os.len=(size_t)strlen(structure->sip_aor1);
			CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
			CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
			if(num_aor>1)
			{
				CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_AOR, 0, &avp ) );
				value.os.data=(unsigned char *)structure->sip_aor2;
				value.os.len=(size_t)strlen(structure->sip_aor2);
				CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
				CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
				if(num_aor>2)
				{
					CHECK_FCT( fd_msg_avp_new ( sip_dict.SIP_AOR, 0, &avp ) );
					value.os.data=(unsigned char *)structure->sip_aor3;
					value.os.len=(size_t)strlen(structure->sip_aor3);
					CHECK_FCT( fd_msg_avp_setvalue( avp, &value ) );
					CHECK_FCT( fd_msg_avp_add( message, MSG_BRW_LAST_CHILD, avp ) );
				}
			}
		}
	}
	
	//TODO:remove for debug
	//fd_msg_dump_walk(INFO,message);
	CHECK_FCT( fd_msg_send( &message, NULL, NULL ));
	
	return 0;
}

//Called when an RTA arrive
int app_sip_RTA_cb( struct msg ** msg, struct avp * paramavp, struct session * sess, void * opaque, enum disp_action * act)
{
	//TODO: RTA reception
/*
	//TODO:remove unused variables
	struct msg *ans, *qry;
	struct avp *avp, *a2, *authdataitem;
	struct msg_hdr * header = NULL;
	struct avp_hdr * avphdr=NULL, *avpheader=NULL, *avpheader_auth=NULL,*digestheader=NULL;
	union avp_value val;
	int found_cnonce=0;
	struct avp * tempavp=NULL,*sipAuthentication=NULL,*sipAuthenticate=NULL;
	char * result;
	int idx=0, idx2=0, number_of_auth_items=0,i=0;
	//Flags and variables for Database
	int sipurinotstored=0, authenticationpending=0, querylen=0, usernamelen=0;
	char *query=NULL,*username=NULL;
	
	
	
	TRACE_ENTRY("%p %p %p %p", msg, avp, sess, act);
	
	if (msg == NULL)
		return EINVAL;
	
	
	// Create answer header 
	qry = *msg;
	CHECK_FCT( fd_msg_new_answer_from_req ( fd_g_config->cnf_dict, msg, 0 ) );
	ans = *msg;	
	
	
	// Add the Auth-Session-State AVP 
	{
		
		CHECK_FCT( fd_msg_search_avp ( qry, sip_dict.Auth_Session_State, &avp) );
		CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr )  );
		
		CHECK_FCT( fd_msg_avp_new ( sip_dict.Auth_Session_State, 0, &avp ) );
		CHECK_FCT( fd_msg_avp_setvalue( avp, avphdr->avp_value ) );
		CHECK_FCT( fd_msg_avp_add( ans, MSG_BRW_LAST_CHILD, avp ) );
	}
	
	CHECK_FCT( fd_msg_search_avp ( qry, sip_dict.SIP_Deregistration_Reason, &avp) );
	CHECK_FCT( fd_msg_avp_hdr( avp, &avphdr )  );
	
	
	
	
	
	
	
	
	
	
	
	
	
	*/
	return 0;
	
}

