微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 嵌入式Linux网络编程之:实验内容——NTP协议实现

嵌入式Linux网络编程之:实验内容——NTP协议实现

时间:08-13 来源:3721RD 点击:

efine NTP_PCK_LEN 48

#define LI 0

#define VN 3

#define MODE 3

#define STRATUM 0

#define POLL 4

#define PREC -6

#define JAN_1970 0x83aa7e80 /* 1900年~1970年之间的时间秒数 */

#define NTPFRAC(x) (4294 * (x) + ((1981 * (x)) >> 11))

#define USEC(x) (((x) >> 12) - 759 * ((((x) >> 10) + 32768) >> 16))

typedef struct _ntp_time

{

unsigned int coarse;

unsigned int fine;

} ntp_time;

struct ntp_packet

{

unsigned char leap_ver_mode;

unsigned char startum;

char poll;

char precision;

int root_delay;

int root_dispersion;

int reference_identifier;

ntp_time reference_timestamp;

ntp_time originage_timestamp;

ntp_time receive_timestamp;

ntp_time transmit_timestamp;

};

char protocol[32];

/*构建NTP协议包*/

int construct_packet(char *packet)

{

char version = 1;

long tmp_wrd;

int port;

time_t timer;

strcpy(protocol, NTPV3);

/*判断协议版本*/

if(!strcmp(protocol, NTPV1)||!strcmp(protocol, NTPV2)

||!strcmp(protocol, NTPV3)||!strcmp(protocol, NTPV4))

{

memset(packet, 0, NTP_PCK_LEN);

port = NTP_PORT;

/*设置16字节的包头*/

version = protocol[6] - 0x30;

tmp_wrd = htonl((LI << 30)|(version << 27)

|(MODE << 24)|(STRATUM << 16)|(POLL << 8)|(PREC & 0xff));

memcpy(packet, &tmp_wrd, sizeof(tmp_wrd));

/*设置Root Delay、Root Dispersion和Reference Indentifier */

tmp_wrd = htonl(1<<16);

memcpy(&packet[4], &tmp_wrd, sizeof(tmp_wrd));

memcpy(&packet[8], &tmp_wrd, sizeof(tmp_wrd));

/*设置Timestamp部分*/

time(&timer);

/*设置Transmit Timestamp coarse*/

tmp_wrd = htonl(JAN_1970 + (long)timer);

memcpy(&packet[40], &tmp_wrd, sizeof(tmp_wrd));

/*设置Transmit Timestamp fine*/

tmp_wrd = htonl((long)NTPFRAC(timer));

memcpy(&packet[44], &tmp_wrd, sizeof(tmp_wrd));

return NTP_PCK_LEN;

}

else if (!strcmp(protocol, TIME))/* "TIME/UDP" */

{

port = TIME_PORT;

memset(packet, 0, 4);

return 4;

}

return 0;

}

/*获取NTP时间*/

int get_ntp_time(int sk, struct addrinfo *addr, struct ntp_packet *ret_time)

{

fd_set pending_data;

struct timeval block_time;

char data[NTP_PCK_LEN * 8];

int packet_len, data_len = addr->ai_addrlen, count = 0, result, i, re;

if (!(packet_len = construct_packet(data)))

{

return 0;

}

/*客户端给服务器端发送NTP协议数据包*/

if ((result = sendto(sk, data,

packet_len, 0, addr->ai_addr, data_len)) < 0)

{

perror("sendto");

return 0;

}

/*调用select()函数,并设定超时时间为1s*/

FD_ZERO(&pending_data);

FD_SET(sk, &pending_data);

block_time.tv_sec=10;

block_time.tv_usec=0;

if (select(sk + 1, &pending_data, NULL, NULL, &block_time) > 0)

{

/*接收服务器端的信息*/

if ((count = recvfrom(sk, data,

NTP_PCK_LEN * 8, 0, addr->ai_addr, &data_len)) < 0)

{

perror("recvfrom");

return 0;

}

if (protocol == TIME)

{

memcpy(&ret_time->transmit_timestamp, data, 4);

return 1;

}

else if (count < NTP_PCK_LEN)

{

return 0;

}

/* 设置接收NTP包的数据结构 */

ret_time->leap_ver_mode = ntohl(data[0]);

ret_time->startum = ntohl(data[1]);

ret_time->poll = ntohl(data[2]);

ret_time->

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top