blob: 6bfde60a329f2d7d9eda6d7c02712e23ec93532b [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*
2* FILE: ntp.c
3* NOTE: socket网络编程学习,NTP时间获取程序
4*
5* TIME: 2021年11月13日00:05:39
6*/
7#include <sys/types.h>
8#include <sys/socket.h>
9#include <stdio.h>
10#include <errno.h>
11#include <netdb.h>
12#include <string.h>
13#include <unistd.h>
14#include <time.h>
15#include <netinet/in.h>
16
17#include "mbtk_log.h"
18
19#define NTP_PORT 123
20#define TIME_PORT 37
21#define NTP_SERVER_IP "cn.pool.ntp.org"
22
23#define NTP_PORT_STR "123"
24#define NTPV1 "NTP/V1"
25#define NTPV2 "NTP/V2"
26
27#define NTPV3 "NTP/V3"
28#define NTPV4 "NTP/V4"
29#define TIME "TIME/UDP"
30
31#define NTP_PCK_LEN 48
32
33#define LI 0
34#define VN 3
35#define MODE 3
36#define STRATUM 0
37#define POLL 4
38#define PREC -6
39
40#define JAN_1970 0x83aa7e80 /* 1900 年~1970 年之间的时间秒数 */
41#define NTPFRAC(x) (4294 * (x) + ((1981 * (x)) >> 11))
42#define USEC(x) (((x) >> 12) - 759 * ((((x) >> 10) + 32768) >> 16))
43
44
45time_t sys_time;
46typedef struct _ntp_time
47{
48 unsigned int coarse;
49 unsigned int fine;
50} ntp_time;
51
52/* NTP时钟同步报文 */
53struct ntp_packet
54{
55 unsigned char leap_ver_mode;
56 unsigned char startum;
57 char poll;
58 char precision;
59 int root_delay;
60 int root_dispersion;
61 int reference_identifier;
62 ntp_time reference_timestamp;
63 ntp_time originage_timestamp;
64 ntp_time receive_timestamp;
65 ntp_time transmit_timestamp;
66};
67
68char protocol[32];
69
70int construct_packet(char *packet)
71{
72 char version = 1;
73 long tmp_wrd;
74 int port;
75 time_t timer;
76 strcpy(protocol, NTPV4);
77 /*判断协议版本*/
78 if(!strcmp(protocol, NTPV1)||!strcmp(protocol, NTPV2)||!strcmp(protocol, NTPV3)||!strcmp(protocol, NTPV4))
79 {
80 memset(packet, 0, NTP_PCK_LEN);
81 port = NTP_PORT;
82 /*设置 16 字节的包头*/
83 version = protocol[5] - 0x30;
84 tmp_wrd = htonl((LI << 30)|(version << 27) \
85 |(MODE << 24)|(STRATUM << 16)|(POLL << 8)|(PREC & 0xff));
86 memcpy(packet, &tmp_wrd, sizeof(tmp_wrd));
87
88 /*设置 Root Delay、 Root Dispersion 和 Reference Indentifier */
89 tmp_wrd = htonl(1<<16);
90 memcpy(&packet[4], &tmp_wrd, sizeof(tmp_wrd));
91 memcpy(&packet[8], &tmp_wrd, sizeof(tmp_wrd));
92 /*设置 Timestamp 部分*/
93 time(&timer);
94 /*设置 Transmit Timestamp coarse*/
95 tmp_wrd = htonl(JAN_1970 + (long)timer);
96 memcpy(&packet[40], &tmp_wrd, sizeof(tmp_wrd));
97 /*设置 Transmit Timestamp fine*/
98 tmp_wrd = htonl((long)NTPFRAC(timer));
99 memcpy(&packet[44], &tmp_wrd, sizeof(tmp_wrd));
100 return NTP_PCK_LEN;
101 }
102 else if (!strcmp(protocol, TIME))/* "TIME/UDP" */
103 {
104 port = TIME_PORT;
105 memset(packet, 0, 4);
106 return 4;
107 }
108
109 return 0;
110}
111
112/*获取 NTP 时间*/
113int get_ntp_time(int sk, struct addrinfo *addr, struct ntp_packet *ret_time)
114{
115 fd_set pending_data;
116 struct timeval block_time;
117 char data[NTP_PCK_LEN * 8];
118 int packet_len, data_len = addr->ai_addrlen, count = 0, result, i,re;
119
120 /* 组织请求报文 */
121 if (!(packet_len = construct_packet(data)))
122 {
123 return 0;
124 }
125 /*客户端给服务器端发送 NTP 协议数据包*/
126 if ((result = sendto(sk, data, packet_len, 0, addr->ai_addr, data_len)) < 0)
127 {
128 LOGE("sendto");
129 return 0;
130 }
131 /*调用select()函数,并设定超时时间为10s*/
132 FD_ZERO(&pending_data);
133 FD_SET(sk, &pending_data);
134 block_time.tv_sec=10;
135 block_time.tv_usec=0;
136 if (select(sk + 1, &pending_data, NULL, NULL, &block_time) > 0)
137 {
138 /*接收服务器端的信息*/
139 if ((count = recvfrom(sk, data, NTP_PCK_LEN * 8, 0, addr->ai_addr, &data_len)) < 0)
140 {
141 LOGE("recvfrom");
142 return 0;
143 }
144
145 // if (protocol == TIME)
146 if(!strcmp(protocol,TIME))
147 {
148 memcpy(&ret_time->transmit_timestamp, data, 4);
149 return 1;
150 }
151 else if (count < NTP_PCK_LEN)
152 {
153 return 0;
154 }
155
156 /* 设置接收 NTP 包的数据结构 */
157 ret_time->leap_ver_mode = ntohl(data[0]);
158 ret_time->startum = ntohl(data[1]);
159 ret_time->poll = ntohl(data[2]);
160 ret_time->precision = ntohl(data[3]);
161 ret_time->root_delay = ntohl(*(int*)&(data[4]));
162 ret_time->root_dispersion = ntohl(*(int*)&(data[8]));
163 ret_time->reference_identifier = ntohl(*(int*)&(data[12]));
164 ret_time->reference_timestamp.coarse = ntohl(*(int*)&(data[16]));
165 ret_time->reference_timestamp.fine = ntohl(*(int*)&(data[20]));
166 ret_time->originage_timestamp.coarse = ntohl(*(int*)&(data[24]));
167 ret_time->originage_timestamp.fine = ntohl(*(int*)&(data[28]));
168 ret_time->receive_timestamp.coarse = ntohl(*(int*)&(data[32]));
169 ret_time->receive_timestamp.fine = ntohl(*(int*)&(data[36]));
170 ret_time->transmit_timestamp.coarse = ntohl(*(int*)&(data[40]));
171 ret_time->transmit_timestamp.fine = ntohl(*(int*)&(data[44]));
172
173 /* 将NTP时间戳转换为日期 */
174 time_t currentTime = ret_time->transmit_timestamp.coarse - JAN_1970;
175 sys_time = ret_time->transmit_timestamp.coarse - JAN_1970;
176 struct tm CurlocalTime;
177 localtime_r(&currentTime, &CurlocalTime);
178 char dateTime[30];
179 strftime(dateTime, 30, "%Y-%m-%d %H:%M:%S %A", &CurlocalTime);
180
181 LOGI("%s\n", dateTime);
182
183 return 1;
184 } /* end of if select */
185
186
187 return 0;
188}
189
190/* 修改本地时间 */
191int set_local_time(struct ntp_packet * pnew_time_packet)
192{
193 struct timeval tv;
194 tv.tv_sec = pnew_time_packet->transmit_timestamp.coarse - JAN_1970;
195 tv.tv_usec = USEC(pnew_time_packet->transmit_timestamp.fine);
196 return settimeofday(&tv, NULL);
197}
198
199int ntp_main()
200{
201 int sockfd, rc;
202 struct addrinfo hints, *res = NULL;
203 struct ntp_packet new_time_packet;
204
205 memset(&hints, 0, sizeof(hints));
206 hints.ai_family = AF_INET;
207 hints.ai_socktype = SOCK_DGRAM;
208 hints.ai_protocol = IPPROTO_UDP;
209
210 /*调用 getaddrinfo()函数, 获取地址信息*/
211 rc = getaddrinfo(NTP_SERVER_IP, NTP_PORT_STR, &hints, &res);
212 if (rc != 0)
213 {
214 LOGE("getaddrinfo");
215 return 0;
216 }
217
218 /* 创建套接字 */
219 sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //IPv4, 数据报套接字, UDP
220 if (sockfd <0 )
221 {
222 LOGE("socket");
223 return 0;
224 }
225
226 /*调用取得 NTP 时间的函数*/
227 if (get_ntp_time(sockfd, res, &new_time_packet))
228 {
liuyang54582902024-04-19 18:06:25 +0800229 printf("NTP client success!\n");
liubin281ac462023-07-19 14:22:54 +0800230 LOGI("NTP client success!\n");
231 }
liuyang54582902024-04-19 18:06:25 +0800232 else
233 {
liuyangd0f7fdb2024-04-22 13:53:23 +0800234 printf("NTP client fail!\n");
235 LOGE("NTP client fail!\n");
liuyang54582902024-04-19 18:06:25 +0800236 close(sockfd);
237 return 0;
238 }
liubin281ac462023-07-19 14:22:54 +0800239
240 close(sockfd);
241
242 return sys_time;
243}
244
245time_t ntp_server_set(const char* server_ip, const char * port)
246{
247 int sockfd, rc;
248 struct addrinfo hints, *res = NULL;
249 struct ntp_packet new_time_packet;
250
251 memset(&hints, 0, sizeof(hints));
252 hints.ai_family = AF_INET;
253 hints.ai_socktype = SOCK_DGRAM;
254 hints.ai_protocol = IPPROTO_UDP;
255
256 printf("server_ip:%s,port:%s\n", server_ip, port);
257
258 /*调用 getaddrinfo()函数, 获取地址信息*/
259 rc = getaddrinfo(NTP_SERVER_IP, NTP_PORT_STR, &hints, &res);
260 if (rc != 0)
261 {
262 printf("getaddrinfo");
263 return 0;
264 }
265
266 /* 创建套接字 */
267 sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //IPv4, 数据报套接字, UDP
268 if (sockfd <0 )
269 {
270 printf("socket");
271 return 0;
272 }
273
274 /*调用取得 NTP 时间的函数*/
275 if (get_ntp_time(sockfd, res, &new_time_packet))
276 {
277 printf("NTP client success!\n");
278 }
279
280 close(sockfd);
281
282 return sys_time;
283}
284
285
286time_t mbtk_ntp_server_set(const char* server_ip, const char * port)
287{
288 return ntp_server_set(server_ip, port);
289}
290
291
292int mbtk_at_systime(void)
293{
294 return ntp_main();
295}
296
297