[ntp:questions] Timestamp Conversion

udhayakumar uuk007 at yahoo.com
Thu Oct 21 10:31:37 UTC 2004


Hi,

 I dont know how to convert a NTPTimestamp to Real time...

 I have attached my Program with this mail...

 Please let me know how to convert it.

 I am not clear about the timestamp also..

 Please give me explanation about also.

 This is my code..

 #include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <elate/kn.h>
#include <time.h>

typedef struct TS
{
	unsigned int second;
	int second_pad;
}TimeSeond;

typedef struct NTP
{
	unsigned char LI_VER_MODE;
	unsigned char STRATUM;
	unsigned char POLL;
	signed char 	PRECISION;
	int 				ROOT_DELAY;
	int 				ROOT_DISPR;
	char              REF_IDE[4];
	TimeSeond	REF_STAMP;
	TimeSeond	ORG_STAMP;
	TimeSeond	RECV_STAMP;
	TimeSeond	TRAN_STAMP;
}NTPStruct;

struct RECStruct
{
	NTPStruct ntpStr;
	long Auth;
};

int main()
{
	int iTemp;
	int iPort = 123;
	int iSocket_ID;
	int iLeap = 0;
	int iVersion = 3;
	int iMode = 3;

	static int iFailure = -1;
	static int iSuccess = 0;

	long int iCurrSec;

	struct sockaddr_in clientSock;
	NTPStruct sendStruc;
	struct RECStruct recvStruc,tempStruc;

	struct tm *currentTime;
	struct tm reqTime;
	char *recvBuf;
	time_t ltime;

	time( &ltime );
	iCurrSec = ltime;
	iCurrSec = iCurrSec + 2208988800.0;
	tracef ("Expected time is %ld\n",iCurrSec);

	// Create Socket
	iSocket_ID = socket(AF_INET, SOCK_DGRAM,0);
	if(iSocket_ID < iSuccess)
	{
		tracef("Problem in Creating Socket\n");
		return iFailure;
	}

	bzero(&clientSock,sizeof(clientSock));
	clientSock.sin_family = AF_INET;
	clientSock.sin_port   = htons(iPort);
	clientSock.sin_addr.s_addr =  inet_addr("10.90.149.10");
	bind(iSocket_ID, (struct sockaddr *) &clientSock, sizeof(clientSock));

	// Connect to destination
	iTemp = connect(iSocket_ID, (struct sockaddr *) &clientSock, sizeof(clientSock));
	if(iTemp < 0)
	{
		tracef("Problem in Connecting %d\n",iTemp);
		return iFailure;
	}

	memset((void *)&sendStruc, 0, sizeof(sendStruc));
	memset((void *)&recvStruc, 0, sizeof(recvStruc));
	memset((void *)&tempStruc, 0, sizeof(tempStruc));

	sendStruc.LI_VER_MODE  = 0x1B;
	sendStruc.STRATUM 		= 0;
	sendStruc.POLL			 	= 0;
	sendStruc.PRECISION		= 0;
	sendStruc.ROOT_DELAY	= 0;
	sendStruc.ROOT_DISPR	= 0;
	sendStruc.REF_IDE[0] 		= 0;
	sendStruc.REF_IDE[1] 		= 0;
	sendStruc.REF_IDE[2] 		= 0;
	sendStruc.REF_IDE[3] 		= 0;

	//Send
	iTemp = send(iSocket_ID,&sendStruc,sizeof(sendStruc),0);
	if(iTemp < 0)
	{
		tracef ("Problem while sending\n");
		return iFailure;
	}

	tracef("Message has been sent\n");

	iTemp = recv(iSocket_ID,&recvStruc,sizeof(recvStruc),0);

	/* I dont know how to proceed next step...*/

}



More information about the questions mailing list