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