/* ***************************************************** Compiler: Borland C++3.1 Compile Mode: large Time server time-a.nist.gov 129.6.15.28 time-b.nist.gov 129.6.15.29 time.windows.com 207.46.248.43 ***************************************************** */ #include #include "..\lib\tcpip32.h" #include "..\lib\7188e.h" #define recsiz 64 struct pingm {struct ICMPhdr hdr; unsigned long ul[MAXBUF/4];} pingm; struct IcmpTime { unsigned short Identifier; unsigned short SequenceNumber; unsigned long OriTimeStamp; unsigned long ReceiveTimeStamp; unsigned long TransmitTimeStamp; }; struct IcmpTime *timestamp; void icmp_close(int conno) { if(conno) { printCom1("Close socket\r\n"); Nclose(conno); } } int icmp_open(unsigned char *cip) { int conno,i; unsigned char *pname; unsigned char *cm; unsigned long ltime; strcpy(pname,cip); cm=strtok(pname,"."); i=0; printCom1("Open.....\r\n"); /* while(cm) { // printCom1("%d,%s\r\n",i,cm); //debug if(atoi(cm)<0 && atoi(cm)>0xff) return -6; //the time server aren't correct i++; cm=strtok(NULL,"."); }*/ pingm.hdr.type = IC_TIMEQ; //IC_ECHO; pingm.hdr.scode = 0; pingm.hdr.chksum = 0; ltime=*TimeTicks; conno = Nopen(cip, "ICMP/IP", 0, 0, S_NOWA);//non-blocking if (conno < 0) return -1; if (recsiz > (MAXBUF - MESSH_SZ - LHDRSZ - 28)) { icmp_close(conno); return -2;//recsize are larger than the memory } return conno; } int icmp_write(int conno) { unsigned seg=0; int iRet; timestamp=pingm.hdr.param; timestamp->Identifier=htons(7188); timestamp->SequenceNumber=htons(seg++); timestamp->OriTimeStamp=TimeMS(); YIELD(); iRet=Nwrite(conno, (char *)&pingm, recsiz+8); return iRet; } int icmp_read(int conno,long timeout,unsigned long *FinishTime) { static char buff[MAXBUF]; unsigned char *cp; struct pingm *pp; int i1; SOCKET_RXTOUT(conno, 0); YIELD(); i1 = Nread(conno, buff, sizeof(buff)); if (i1 <= 0) //ETIMEDOUT return i1; // read error else if(i1>0) { *FinishTime=TimeMS(); pp = (struct pingm *)buff; if (pp->hdr.type != IC_TIMER) //IC_EREP) return -5; //Return not IC_TIMER cp = SOCKET_IPADDR(conno).c; printCom1("[%d.%d.%d.%d]", cp[0], cp[1], cp[2], cp[3]); timestamp=pp->hdr.param; printCom1("id=%u seq#=%u Ori=%lu rec=%lu Trans=%lu Fins=%lu\n\r", htons(timestamp->Identifier),htons(timestamp->SequenceNumber), timestamp->OriTimeStamp,htonl(timestamp->ReceiveTimeStamp), htonl(timestamp->TransmitTimeStamp),*FinishTime); //debug return 1; //correct } }