首页 > 代码库 > ntp 控制报文

ntp 控制报文

//make the procedure into block
//2014.7.23 OK
//
#include "CSocket.h"

#define NTP_SERVER_IP "192.168.1.12" /*ntp service IP*/

int sendpkt(int sockfd,struct addrinfo * res)
{


struct ntp_control ntppack,newpack;

//put the date into the ntppack
ntppack.li_vn_mode = 0x26;
ntppack.r_m_e_op = 0x03;
ntppack.sequence = 0x01;
ntppack.status = 0x00;
ntppack.associd = 0x00;
ntppack.offset = 0x00;
ntppack.count = 0x00;

int i;
for(i=0; i<1; i++)
{
int ret = sendto(sockfd,&ntppack,sizeof(ntppack),0, res->ai_addr, res->ai_addrlen);
if(ret < 0){
perror("sendto");
return 1;
}
}

}
int getresponse(int sockfd,struct addrinfo * res)
{
struct ntp_control rpkt;
fd_set pending_data;
struct timeval block_time;
char *refid ;

/*调用select()函数,并设定超时时间为1s*/
FD_ZERO(&pending_data);
FD_SET(sockfd, &pending_data);

block_time.tv_sec=10;//how time to ask
block_time.tv_usec=0;
if (select(sockfd + 1, &pending_data, NULL, NULL, &block_time) > 0)

{
int num;
/*接收服务器端的信息*/
if ((num = recvfrom(sockfd, &rpkt,
sizeof(rpkt), 0, res->ai_addr, &res->ai_addrlen)) < 0)
{
perror("recvfrom");
return 0;
}

/* 设置接收NTP包的数据结构 */
int mode = PKT_MODE(rpkt.li_vn_mode);
int version = PKT_VERSION(rpkt.li_vn_mode);
int leap = PKT_LEAP(rpkt.li_vn_mode);

int response = CTL_ISRESPONSE(rpkt.r_m_e_op);
int error = CTL_ISERROR(rpkt.r_m_e_op);
int more = CTL_ISMORE(rpkt.r_m_e_op);
int readvar = CTL_OP(rpkt.r_m_e_op);

int sequence = ntohs(rpkt.sequence);
int status = ntohs(rpkt.status);
int associd = ntohs(rpkt.associd);

int offset = ntohs(rpkt.offset);
int count = ntohs(rpkt.count);

printf("li=%d,version=%d,mode=%d\n",leap,version,mode);
printf("response=%d,error=%d,more=%d,readvar=%d\n",response,error,more,readvar);
printf("sequence=%d\n",sequence);
printf("status=%d,associd=%d\n",status,associd);
printf("offset=%d,count=%d\n",offset,count);
printf("################ data ####################\n");
char *version_1 = strtok(rpkt.data,",");
char *processor = strtok(NULL,",");
char *system = strtok(NULL,",");

char *leap_1 = strtok(NULL,",");
char * stratum = strtok(NULL, ",");
char *precision = strtok(NULL,",");

char *rootdelay = strtok(NULL,",");
char *rootdispersion = strtok(NULL,",");
char *peer = strtok(NULL,",");

char *refid = strtok(NULL,",");
char *reftime = strtok(NULL,",");
char *poll = strtok(NULL,",");
char *clock = strtok(NULL ,",");
char *state = strtok(NULL, ",");
char *offset1 = strtok(NULL,",");

char *frequency = strtok(NULL,",");
char *jitter = strtok(NULL,",");
char *stability = strtok(NULL,",");
// printf("refid : ---%s----\n",refid);
}
if(strcmp(refid+6,"GPS")==0)
printf("ok\n");
return 1;
}
int main()
{

struct addrinfo *res = NULL,hints;

memset(&hints,0,sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = IPPROTO_UDP;

/*调用getaddrinfo()函数,获取地址信息*/
int rc = getaddrinfo(NTP_SERVER_IP, NTP_PORT_STR, &hints, &res);
if (rc != 0)
{
perror("getaddrinfo");
return 1;
}

int fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if(fd < 0){
perror("socket");
return 0;
}


sendpkt(fd,res);
int i;
i = getresponse(fd,res);
printf("nihao=%d",i);
close(fd);
}

http://www.gpstime.com.cn/