/*******************************************************************

Copyright (c) 2004 Open Mobile Alliance, Ltd.  All Rights Reserved.

This software is made available under the usage and distribution terms 
covered by the Common Public License 1.0 and by using this software you
are bound by the terms and conditions of the same.

EXCEPT AS EXPRESSLY SET FORTH IN THE COMMON PUBLIC LICENSE,
THIS PROGRAM IS PROVIDED ON AN "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
EITHER EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, 
ANY WARRANTIES OR CONDITIONS OF TITLE, NON-INFRINGEMENT, 
MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
   
Each recipient is solely responsible for determining the 
appropriateness of using and distributing this program and assumes 
all risks associated with its exercise of rights under the Common 
Public License, including but not limited to the risks and 
costs of program errors, compliance with applicable laws, 
damage to or loss of data, programs or equipment, and 
unavailability or interruption of operations.

********************************************************************/
#include "SctsDevManNotificationSession.h"
#include "SctsDevManDMAccount.h"

/*
 * Change notification process, need to support DM 1.2 DMAcc node. 
 */
int SctsDMNotification::Run()
{
	char notification1[300];
	char notification2[]="HTTP/1.1 200 OK\r\nContent-Language: en\r\nContent-Length: 28\r\nContent-Type: application/vnd.syncml.notification\r\n\r\n\0                                         ";
	char *pos=notification2+sctsStrlen(notification2);
	sprintf(notification1,"POST /wappush HTTP/1.1\r\nHost: %s\r\nContent-Type: application/http\r\nContent-Length: %d\r\nX-Wap-Push-OTA-Version: 1.0\r\n\r\n",inet_ntoa(address.sin_addr),pos-notification2+28);


	pos[16]=(char)0x02;
	pos[17]=(char)0xC0;
	pos[18]=0;
	pos[19]=0;
	pos[20]=0;
	pos[21]=0x30;
	pos[22]=0x39;
	pos[23]=4;
	pos[24]='S';
	pos[25]='c';
	pos[26]='t';
	pos[27]='s';
	/* Compute the Digest */
	SctsDMNode *password,*nonce;
	char buffer[300];
	MD5_CTX md5Context;
	char md5Digest[16];
	char *str;
	char *str1;
	unsigned int _size;
	unsigned int _size1;
				
	/* Get Password and Nonce */
	password=device->FindDMNode((char *)(device->accounts[dmacc->name].subAppAuthServer + "/AAuthSecret").c_str(), dmacc);
	nonce=device->FindDMNode((char *)(device->accounts[dmacc->name].subAppAuthServer + "/AAuthData").c_str(), dmacc);
	if (password && password->data && nonce && nonce->data)	
	{			
		/* H(trigger) */
		MD5Init(&md5Context);
		MD5Update(&md5Context,pos+16,12);
		MD5Final(md5Digest,&md5Context);
		_size=16;
		
		/* B64(H(trigger) */
		str=sctsBase64Encode((unsigned char *)md5Digest,_size);

		/* H(serverId:password) */
		sctsStrcpy(buffer,"Scts:");
		sctsStrcat(buffer,(char *)password->data);
		_size1=sctsStrlen(buffer);

		MD5Init(&md5Context);
		MD5Update(&md5Context,(char *)buffer,_size1);
		MD5Final(md5Digest,&md5Context);
		_size1=16;

		/* B64(H(serverId:password)) */
		str1=sctsBase64Encode((unsigned char *)md5Digest,_size1);
		
		/* B64(H(username:password)):nonce:B64(H(trigger)) */
		sctsStrcpy(buffer,str1);
		buffer[_size1++]=':';
		sctsMemcpy(buffer+_size1,nonce->data,nonce->size);
		_size1+=nonce->size;
		buffer[_size1++]=':';
		sctsMemcpy(buffer+_size1,str,_size);
		_size1+=_size;

		/* H(B64(H(serverId:password)):nonce:B64(H(trigger))) */
		MD5Init(&md5Context);
		MD5Update(&md5Context,(char *)buffer,_size1);
		MD5Final(md5Digest,&md5Context);
		_size1=16;

		memcpy(pos,md5Digest,16);
	}
		
	/* Open socket */
	SOCKET sock=socket(AF_INET,SOCK_STREAM,0);
	connect(sock,(struct sockaddr *)&address,sizeof(struct sockaddr_in));
	send(sock,notification1,sctsStrlen(notification1),0);
	send(sock,notification2,pos-notification2+28,0);
	recv(sock,notification1,300,0);
	closesocket(sock);
	return 0;
}

int SctsDMNotification::Exit()
{
	delete this;
	return 0;
}
