Time synchronization method, device, electronic device and storage medium
The Netfilter framework obtains and calculates the timestamp of the response packet, corrects the client time, solves the problem of low time synchronization accuracy in NTP client and server mode, and achieves higher timing accuracy.
Patent Information
- Application Number
- CN202111667867.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-31
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2041-12-31
AI Technical Summary
In the existing time synchronization method in NTP client and server mode, the difference between time D1 and D2 results in low timing accuracy and the inability to accurately calculate the one-way transmission delay, affecting the accuracy of time synchronization.
The Netfilter framework obtains the timestamp of the response packet leaving the local machine and arriving at the client application process, calculates the transmission time of the response packet, thereby correcting the client time and eliminating the random waiting time of the packet from the client and server kernel to the application. Effect on transmission delay.
Improve the accuracy of time synchronization, narrow the time calculation range, and enhance the accuracy of time alignment.
Smart Images

Figure CN114448547B_ABST
Abstract
Description
Technical Field
[0001] Embodiments of the present invention relate to the field of time synchronization technology, and in particular to a time synchronization method, device, electronic device and storage medium. Background Art
[0002] With the continuous emergence of computer network applications, the issue of computer time synchronization has become increasingly important. Network Time Protocol (NTP) is a protocol for synchronizing computer time, used to synchronize time between distributed time servers and clients. It can provide high-precision time correction and can carry authentication codes for identity authentication and integrity protection to prevent malicious protocol attacks. The purpose of using NTP is to synchronize the clocks of all devices with clocks in the network, so that the clocks of all devices in the network are consistent, so that the devices can provide a variety of applications based on unified time.
[0003] Working principle of NTP: The NTP client sends a request to the NTP server through the network interface at time T1, the NTP server receives the request from the NTP client at time T3, the NTP server responds to the request to the NTP client at time T2, and then the NTP client receives the response from the NTP server at time T4. In the prior art, when synchronizing, the time of the NTP client is modified to: the time at time T2 + one-way transmission delay (the time D2 required from the NTP server sending a response to a request to the NTP client receiving the response. Since D2 cannot be accurately obtained, but D1+D2 can be calculated, (D1+D2) / 2 can be used instead of D2). The premise of the client / server mode time synchronization method is to assume that the time D1 and time D2 required from the NTP client sending a request to the NTP server receiving the request are equal. However, in fact, time D1 and time D2 are different, and the accuracy of time synchronization depends on the degree of equality between time D1 and time D2. The closer time D1 and time D2 are, the higher the accuracy of time synchronization. The greater the deviation between time D1 and time D2, the worse the accuracy of time synchronization. Summary of the invention
[0004] In view of this, embodiments of the present invention provide a time synchronization method, device, electronic device, and storage medium to improve the time synchronization accuracy in client and server modes.
[0005] In a first aspect, an embodiment of the present invention provides a time synchronization method, including:
[0006] Sending NTP synchronization messages and receiving response messages to the synchronization messages;
[0007] Obtain the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process;
[0008] The corrected time of the client is obtained according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process.
[0009] In a second aspect, an embodiment of the present invention further provides a time synchronization device, including:
[0010] A message transmission module, used for sending a network time protocol synchronization message and receiving a response message to the synchronization message;
[0011] An acquisition module is used to acquire the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process;
[0012] The time synchronization module is used to obtain the corrected time of the client according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process.
[0013] In a third aspect, an embodiment of the present invention further provides an electronic device, including:
[0014] one or more processors;
[0015] A storage device for storing one or more programs;
[0016] When the one or more programs are executed by the one or more processors, the processors implement the time synchronization method as described in any one of the embodiments of the present invention.
[0017] In a fourth aspect, a computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the time synchronization method as described in any one of the embodiments of the present invention is implemented.
[0018] The technical solution of the embodiment of the present invention is to send a synchronization message of NTP and receive a response message of the synchronization message; obtain the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process; obtain the corrected time of the client according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process. The above technical solution replaces the time stamp in the synchronization message and the response message at the designated detection point in the Netfilter framework, so that the client time can be modified according to the server time at the designated detection point when the response message leaves the server and the time required for the response message to leave the designated detection point of the server and arrive at the designated detection point of the client and then to the client application process. Compared with the prior art, the time calculation range of sending the synchronization message from the client to the server and sending the response message from the server to the client is reduced, and the influence of the random waiting time of the message from the kernel to the application on the transmission delay is eliminated (there is a random delay from the kernel LOCAL_IN point to the application and from the application to the LOCAL_OUT point), thereby improving the synchronization accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments are briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present invention and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without creative work.
[0020] Figure 1 A flowchart of a time synchronization method provided in Embodiment 1 of the present invention;
[0021] Figure 2 A schematic effect diagram of an implementation of a time synchronization method provided by an embodiment of the present invention;
[0022] Figure 3 A schematic diagram of the structure of a time synchronization device provided in Embodiment 2 of the present invention;
[0023] Figure 4 A schematic diagram of the structure of an electronic device provided in Embodiment 3 of the present invention. DETAILED DESCRIPTION
[0024] The present invention will be further described in detail below in conjunction with the accompanying drawings and embodiments. It is to be understood that the specific embodiments described herein are only used to explain the present invention, rather than to limit the present invention. It should also be noted that, for ease of description, only the parts related to the present invention, rather than all structures, are shown in the accompanying drawings. In addition, the embodiments of the present invention and the features in the embodiments may be combined with each other without conflict.
[0025] It should be mentioned before discussing exemplary embodiments in more detail that some exemplary embodiments are described as processes or methods depicted as flow charts. Although the flow charts describe various operations (or steps) as sequential processes, many operations therein can be implemented in parallel, concurrently or simultaneously. In addition, the order of various operations can be rearranged. The process can be terminated when its operation is completed, but can also have additional steps not included in the accompanying drawings. The process can correspond to methods, functions, procedures, subroutines, subprograms, etc. In addition, the embodiments in the present invention and the features in the embodiments can be combined with each other without conflict.
[0026] The term “including” and its variations used in the present invention are open inclusions, that is, “including but not limited to.” The term “based on” means “based at least in part on.” The term “one embodiment” means “at least one embodiment.”
[0027] It should be noted that similar reference numerals and letters represent similar items in the following drawings, so once an item is defined in one drawing, it does not need to be further defined and explained in the subsequent drawings. At the same time, in the description of the present invention, the terms "first", "second", etc. are only used to distinguish the description and cannot be understood as indicating or implying relative importance.
[0028] Embodiment 1
[0029] Figure 1 This is a flowchart of a time synchronization method provided in the first embodiment of the present invention. This embodiment is applicable to the time synchronization in the client and server modes. The method can be executed by the time synchronization device in the embodiment of the present invention. The device can be implemented in software and / or hardware and integrated in an electronic device, wherein the electronic device mainly refers to the NTP client. Figure 1 As shown, the method specifically comprises the following steps:
[0030] S110: Send a synchronization message of the Network Time Protocol NTP and receive a response message to the synchronization message.
[0031] Among them, the client sends a synchronization message, the server receives the synchronization message and sends a response message, and the client receives the response message. In this process, synchronization can be performed based on the various parameters included in the synchronization message and the response message. These parameters may include the client's timestamp when the client sends the synchronization message, the server's timestamp when the server receives the synchronization message, and the server's timestamp when the server sends the response synchronization message. The timestamp can be understood as local time. For example, when the client sends a message, the timestamp is the local time on the client, and when the server sends a response message, the timestamp is the local time on the server. Of course, the synchronization message can also include parameters of the working mode, such as client mode and server mode.
[0032] It should be noted that, for the sake of convenience of description, the time required for the NTP client to send a request (i.e., send a synchronization message) to the NTP server to receive the request (i.e., receive a synchronization message) can be recorded as D1, and the time required for the NTP server to send a response to the request (i.e., send a response message) to the NTP client to receive the response (i.e., receive a response message) can be recorded as D2. D1 or D2 can be understood as the one-way transmission delay of the synchronization message or the response message.
[0033] Here, taking the NTP server sending a response to the NTP client receiving request at time T2 as an example, then: D2 = D1 + △D;
[0034] T=T2+(D1+D2) / 2
[0035] =T2+D2 / 2+D1 / 2
[0036] =T2+D2 / 2+(D2-△D) / 2
[0037] =T2+D2-△D / 2
[0038] =T2+D2-(D2-D1) / 2;
[0039] The timing error is (D2-D1) / 2. It can be understood that in order to improve the proximity of D1 and D2 and thus improve the timing accuracy, the Netfilter framework of the Linux system can be used. For example, the synchronization message can be matched at the detection point in the Netfilter framework and the timestamp in the synchronization message can be replaced.
[0040] In the embodiment of the present invention, the client can send an NTP synchronization message to the server and receive a response message returned by the server. Specifically, the time when the client sends the synchronization message, that is, the time when the synchronization message leaves the client, can be the time corresponding to when the synchronization message sent by the client leaves the detection point after the synchronization message arrives at the specified detection point in the Netfilter framework of the client, which can be recorded as T1; the time when the client receives the response message, that is, the time when the response message arrives at the client, can be the time corresponding to when the response message arrives at the specified detection point in the Netfilter framework of the client, which can be recorded as T4.
[0041] S120, obtaining the time when the response message leaves the local machine according to the Netfilter framework, and calculating the time required for the response message to leave the local machine and arrive at the client application process.
[0042] It should be noted that after receiving the synchronization message sent by the client, the server will send a response message to the synchronization message, and then the client will receive the response message sent by the server. The response message includes the server timestamp when the server sends the response synchronization message. Therefore, the time when the response message leaves the local machine can be obtained, that is, after the response message arrives at the designated detection point in the Netfilter framework of the server, the corresponding time when the response message leaves the detection point can be recorded as T2. In addition, since the random delay time of the response message from the server application process to the designated detection point of the server is excluded, the synchronization is more accurate.
[0043] It can be understood that the time required for the response message to reach the specified detection point in the Netfilter framework of the server to the specified detection point in the Netfilter framework of the client can be re-recorded as D2, and the corresponding time when the response message reaches the client application process can be recorded as T5. Therefore, the time required for the response message to reach the specified detection point of the client and then reach the client application process can be expressed as T5 minus T4, and the time required for the response message to leave the local machine and reach the client application process can be understood as D2 plus T5 minus T4.
[0044] S130, obtaining the corrected time of the client according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process.
[0045] In an embodiment of the present invention, the corrected time of the client can be obtained based on the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process. For example, the corrected time can be expressed as T2+D2+(T5-T4), so that the local time of the client can be corrected, thereby synchronizing the local time of the client and the local time of the server.
[0046] The technical solution of the embodiment of the present invention is to send a synchronization message of the network time protocol NTP and receive a response message of the synchronization message; obtain the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process; obtain the corrected time of the client according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process. The above technical solution replaces the time stamp in the synchronization message and the response message at the designated detection point in the Netfilter framework, so that the client time can be modified according to the server time at the designated detection point when the response message leaves the server and the time required for the response message to leave the designated detection point of the server and arrive at the designated detection point of the client and then to the client application process. Compared with the prior art, the time calculation range of sending the synchronization message from the client to the server and sending the response message from the server to the client is reduced, and the influence of the random waiting time of the message from the kernel to the application on the transmission delay is eliminated (there is a random delay from the kernel LOCAL_IN point to the application and from the application to the LOCAL_OUT point), thereby improving the synchronization accuracy.
[0047] Further, based on the above-mentioned embodiment of the invention, the correction time of the client is:
[0048] The time it takes for the response message to leave the local machine, plus the time it takes for the response message to leave the local machine and arrive at the client application process.
[0049] In the embodiment of the present invention, the corrected time of the client can be expressed as the corresponding time when the response message leaves the designated detection point in the Netfilter framework of the server, plus the time required for the response message to leave the designated detection point of the server, arrive at the designated detection point of the client, and then reach the client application process. For example, the corrected time can be expressed as T2+D2+(T5-T4).
[0050] Further, based on the above-mentioned embodiment of the invention, in the process of sending a time-out message and receiving a response message to the time-out message, it also includes:
[0051] Replace the timestamps of synchronization messages and response messages at the specified detection point in the Netfilter framework.
[0052] Among them, the Netfilter framework can be a subsystem introduced by the Linux 2.4.x system. It is a structured underlying framework in the Linux system kernel for extending various network services. It can be used as a general, abstract framework to provide a complete set of hook function management mechanisms, making it possible to perform functions such as packet filtering, network address translation (NAT), and connection tracking based on protocol types. The Netfilter framework can place some detection points (HOOK) at several locations in the entire network process, and can register some processing functions in each detection point to filter or modify the data packet. These detection points can include PRE_ROUTING, LOCAL_IN, FORWARD, POST_ROUTING, and LOCAL_OUT detection points. PRE_ROUTING detection point: the data packets that have just entered the network layer pass through this detection point, and the routing decision has not been made for the data packets. The source address translation is performed at this point; LOCAL_IN (sent to the local machine) detection point: after the routing decision, if the data packets are sent to the local machine, the data packets sent to the local machine pass through this detection point; FORWARD detection point: after the routing decision, if the data packets are not sent to the local machine, the forwarded packets pass through this detection point; LOCAL_OUT (leaving the local machine) detection point: the data packets sent by the local process pass through this detection point; POST_ROUTING detection point: all data packets that need to be sent through the network device pass through this detection point, and the built-in destination address translation function is performed at this point. If the data packets are sent to the local machine, the data packet flow direction is: from the PRE_ROUTING detection point to the LOCAL_IN detection point; if the data packets are sent locally, the data packet flow direction is: from the LOCAL_OUT detection point to the POST_ROUTING detection point. Therefore, when the client sends a synchronization message, it can replace the timestamp in the synchronization message at the client's LOCAL_OUT detection point; when the server sends a response message, it can replace the timestamp in the response message at the server's LOCAL_OUT detection point; when the server receives a synchronization message, it can replace the timestamp in the synchronization message at the server's LOCAL_IN detection point; when the client receives a response message, it can replace the timestamp in the response message at the client's LOCAL_IN detection point.
[0053] Further, based on the above-mentioned embodiment of the invention, the timestamps of the timing message and the response message include:
[0054] The first timestamp corresponding to the detection point when the message leaves the local machine;
[0055] The third timestamp corresponding to the detection point when the synchronous message is sent on the local machine;
[0056] A second timestamp corresponding to the detection point of the response message leaving the local machine;
[0057] The detection point at which the response message is sent on the local machine, and the corresponding fourth timestamp;
[0058] The fifth timestamp of the response message corresponding to the detection point of the client application process.
[0059] In an embodiment of the present invention, when the synchronization message leaves the LOCAL_OUT detection point of the client, the corresponding time is the first timestamp (recorded as T1), and when the synchronization message arrives at the LOCAL_IN detection point on the server, the corresponding time is the third timestamp (recorded as T3), and when the response message leaves the LOCAL_OUT detection point of the server, the corresponding time is the second timestamp (recorded as T2), and when the response message arrives at the LOCAL_IN detection point of the client, the corresponding time is the fourth timestamp (recorded as T4), and when the response message is at the client application process detection point, the corresponding time is the fifth timestamp (recorded as T5).
[0060] Further, based on the above-mentioned embodiment of the invention, the time required for the response message to leave the local machine and arrive at the client application process includes:
[0061] The one-way transmission delay of the response message from leaving the local machine to being sent to the local machine, and the time required for the response message to be sent from the local machine to reach the client application process.
[0062] In an embodiment of the present invention, the one-way transmission delay of the response message from the LOCAL_OUT detection point of the server to the LOCAL_IN detection point of the client, the one-way transmission delay here may include the delay from the LOCAL_OUT detection point of the server to the server network interface plus the network transmission delay (the delay from the server network interface to the client network interface) plus the delay from the client network interface to the client LOCAL_IN detection point, and the time required for the response message to travel from the LOCAL_IN detection point of the client to reach the client's application process.
[0063] Further, based on the above-mentioned embodiments of the invention, the one-way transmission delay is 1 / 2 of the difference between the first time period and the second time period, wherein the first time period is the difference obtained by subtracting the first timestamp from the fourth timestamp, and the second time period is the difference obtained by subtracting the third timestamp from the second timestamp; the time required for the response message to be sent from the local machine to the client application process is the difference obtained by subtracting the fourth timestamp from the fifth timestamp.
[0064] It should be noted that if the sending delay part, i.e., the delay from the application process to the LOCAL_OUT detection point and the receiving delay part, i.e., the delay from the LOCAL_IN detection point to the application process, are excluded, the delay from the LOCAL_OUT detection point at the sending end to the network interface plus the network transmission delay plus the delay from the network interface at the receiving end to the LOCAL_IN detection point can constitute a one-way transmission delay, i.e., the one-way transmission delay from the response message leaving the local machine to being sent on the local machine, or the one-way transmission delay from the client leaving the local machine to being sent on the local machine. Therefore, the one-way transmission delay can be expressed as (the difference between the fourth timestamp and the first timestamp) minus (the difference between the second timestamp and the third timestamp) divided by 2, which can be expressed as ((T4-T1)-(T2-T3)) / 2, which is 1 / 2 of the difference between the first time period and the second time period, where the first time period is the difference obtained by subtracting the first timestamp from the fourth timestamp, and the second time period is the difference obtained by subtracting the third timestamp from the second timestamp.
[0065] In the embodiment of the present invention, the time required for the response message to reach the application process of the client from the LOCAL_IN detection point of the client can be expressed as the difference obtained by subtracting the fourth timestamp from the fifth timestamp (ie, T5-T4).
[0066] Further, based on the above-mentioned embodiment of the invention, after replacing the timestamps of the synchronization message and the response message, it also includes:
[0067] Update the message authentication code corresponding to the synchronization message.
[0068] It should be noted that, since the timestamps of the synchronization message and the response message are replaced at the designated detection point in the Netfilter framework, in order to ensure that the synchronization message received by the server and the response message received by the client are legal messages, the message authentication code corresponding to the synchronization message can be updated. Specifically, after the synchronization message is marked with the first timestamp at the detection point of the client, the message authentication code needs to be recalculated. When the server receives the synchronization message and performs message authentication, it can clear the first timestamp recorded by the client and then perform message authentication; correspondingly, the same is true for the response message. The client clears the second timestamp recorded by the server and then performs message authentication.
[0069] For ease of understanding, refer to Figure 2 , Figure 2 A schematic diagram of the implementation effect of a time synchronization method provided in an embodiment of the present invention.
[0070] The synchronization message 212 sent by the NTP client 201 is sent from the application process 202 of the NTP client 201 to the LOCAL_OUT detection point 204 of the NTP client 201. At this time, the time when the synchronization message 212 reaches the LOCAL_OUT detection point 204 is recorded as the first timestamp T1, and then the synchronization message 212 is from the LOCAL_OUT detection point 204 to the NTP client network interface 206, then, the synchronization message 212 is transmitted from the NTP client network interface 206 to the NTP server network interface 211, and then the synchronization message 212 is transmitted from the NTP server network interface 211 to the LOCAL_IN detection point 210 of the NTP server 207. At this time, the time when the synchronization message 212 reaches the LOCAL_IN detection point 210 is recorded as the third timestamp T3, and finally, the synchronization message 212 is from the LOCAL_IN detection point 210 to the application process 208 of the NTP server 207. In addition, the boundary line 203 is the boundary between the application process and the kernel.
[0071] The application process 208 of the NTP server 207 sends a response message 213 to the LOCAL_OUT detection point 209 of the NTP server 207. At this time, the time when the response message 213 arrives at the LOCAL_OUT detection point 209 is recorded as the second timestamp T2. Then, the response message 213 is transmitted from the LOCAL_OUT detection point 209 to the NTP server network interface 211, and then the response message 213 is transmitted from the NTP server network interface 211 to the NTP client network interface 206. Then the response message 213 is sent from the NTP client network interface 206 to the LOCAL_IN detection point 205 of the NTP client 201. At this time, the time when the response message 213 arrives at the LOCAL_IN detection point 205 of the NTP client 201 is recorded as the fourth timestamp T4. Finally, the response message 213 is sent from the LOCAL_IN detection point 205 to the application process 202 of the NTP client 201. At this time, the time when the response message 213 arrives at the application process 202 is recorded as the fifth timestamp T5.
[0072] like Figure 2 As shown in the figure, the NTP timestamp point is moved down to the kernel, and the random waiting time of the sending delay part and the receiving delay part is excluded from the calculation path. In this way, the transmission delay includes:
[0073] The latency from the client LOCAL_OUT to the network interface + the one-way transmission latency + the latency from the server network interface to the LOCAL_IN portion.
[0074] The calculation formula for the client-side correction time is:
[0075] T=T2+D2+(T5-T4)
[0076] =T2+D / 2+(T5-T4)
[0077] =T2+((T4-T1)-(T2-T3)) / 2+(T5-T4)
[0078] On the client side, the delay from sending the request to LOCAL_OUT is excluded from the calculation path. When receiving the response message, (T5-T4) can be used to accurately compensate for the random waiting time from LOCAL_IN to the application. On the server side, the random part from receiving the request from LOCAL_IN to the application processing part and sending the response to LOCAL_OUT is included in (T2-T3). On this basis, the random part in the delay path is eliminated, and the time synchronization accuracy of the client and server NTP is improved.
[0079] Embodiment 2
[0080] Figure 3 This is a schematic diagram of the structure of a time synchronization device provided in the second embodiment of the present invention. This embodiment is applicable to the time synchronization in the client and server modes. The device can be implemented in software and / or hardware. The device can be integrated in any device that provides a time synchronization function, such as Figure 3 As shown, the time synchronization device specifically includes: a message transmission module 310, an acquisition module 320 and a time synchronization module 330.
[0081] A message transmission module 310, configured to send a network time protocol synchronization message and receive a response message to the synchronization message;
[0082] The acquisition module 320 is used to acquire the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process;
[0083] The time synchronization module 330 is used to obtain the corrected time of the client according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process.
[0084] The technical solution of the embodiment of the present invention is to send the NTP synchronization message through the message transmission module and receive the response message of the synchronization message; obtain the time when the response message leaves the local machine through the Netfilter framework acquisition module, and calculate the time required for the response message to leave the local machine and arrive at the client application process; obtain the client's corrected time according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process through the synchronization module. The above technical solution replaces the timestamp in the synchronization message and the response message at the designated detection point in the Netfilter framework, so that the client time can be modified according to the server time at the designated detection point when the response message leaves the server and the time required for the response message to leave the designated detection point of the server and arrive at the designated detection point of the client and then to the client application process. Compared with the prior art, the time calculation range of sending the synchronization message from the client to the server and sending the response message from the server to the client is reduced, thereby improving the synchronization accuracy.
[0085] Further, based on the above-mentioned embodiment of the invention, the correction time of the client is:
[0086] The time it takes for the response message to leave the local machine plus the time it takes for the response message to leave the local machine and arrive at the client application process.
[0087] Further, based on the above-mentioned embodiment of the invention, the message transmission module 310 includes a replacement unit:
[0088] The replacement unit is used to replace the time stamp of the time-keeping message and the response message at a designated detection point in the Netfilter framework during the process of sending the time-keeping message and receiving the response message of the time-keeping message.
[0089] Further, based on the above-mentioned embodiment of the invention, the timestamps of the synchronization message and the response message include:
[0090] A first timestamp corresponding to a detection point at which the synchronization message leaves the local machine;
[0091] A third timestamp corresponding to the detection point at which the synchronization message is sent on the local machine;
[0092] A second timestamp corresponding to the detection point of the response message leaving the local machine;
[0093] A fourth timestamp corresponding to the detection point at which the response message is sent on the local machine;
[0094] The response message has a fifth timestamp corresponding to the detection point of the client application process.
[0095] Further, based on the above-mentioned embodiment of the invention, the time required for the response message to leave the local machine and arrive at the client application process includes:
[0096] The one-way transmission delay of the response message from leaving the local machine to being sent to the local machine, and the time required for the response message to be sent from the local machine to reach the client application process.
[0097] Further, based on the above-mentioned embodiment of the invention, the one-way transmission delay is 1 / 2 of the difference between the first time period and the second time period, wherein the first time period is the difference obtained by subtracting the first timestamp from the fourth timestamp, and the second time period is the difference obtained by subtracting the third timestamp from the second timestamp;
[0098] The time required for the response message to be sent from the local machine to the client application process is the difference obtained by subtracting the fourth timestamp from the fifth timestamp.
[0099] Further, based on the above-mentioned embodiment of the invention, after replacing the timestamps of the synchronization message and the response message, the replacement unit is further used to:
[0100] Update the message authentication code corresponding to the synchronization message.
[0101] The above-mentioned product can execute the method provided by any embodiment of the present invention, and has the corresponding functional modules and beneficial effects of the execution method.
[0102] Embodiment 3
[0103] Figure 4 A schematic diagram of the structure of an electronic device provided in Embodiment 3 of the present invention. Figure 4 A block diagram of an electronic device 412 suitable for implementing embodiments of the present invention is shown. Figure 4 The electronic device 412 shown is only an example and should not limit the functions and scope of use of the embodiments of the present invention. The device 412 is a typical computing device with a time synchronization function.
[0104] like Figure 4 As shown, the electronic device 412 is in the form of a general purpose computing device. The components of the electronic device 412 may include, but are not limited to: one or more processors 416, a storage device 428, and a bus 418 connecting different system components (including the storage device 428 and the processor 416).
[0105] Bus 418 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor, or a local bus using any of a variety of bus architectures. For example, these architectures include but are not limited to Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MCA) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus, and Peripheral Component Interconnect (PCI) bus.
[0106] The electronic device 412 typically includes a variety of computer system readable media. These media can be any available media that can be accessed by the electronic device 412, including volatile and non-volatile media, removable and non-removable media.
[0107] The storage device 428 may include computer system readable media in the form of volatile memory, such as random access memory (RAM) 430 and / or cache memory 432. The electronic device 412 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 434 may be used to read and write non-removable, non-volatile magnetic media ( Figure 4 not shown, usually called a "hard drive"). Although Figure 4 Not shown in the figure, a disk drive for reading and writing a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk drive for reading and writing a removable non-volatile optical disk (e.g., a read-only optical disk (Compact Disc-Read Only Memory, CD-ROM), a digital video disk (Digital Video Disc-Read Only Memory, DVD-ROM) or other optical media) may be provided. In these cases, each drive may be connected to the bus 418 via one or more data medium interfaces. The storage device 428 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present invention.
[0108] A program 436 having a set (at least one) of program modules 426 may be stored, for example, in a storage device 428, such program modules 426 including, but not limited to, an operating system, one or more application programs, other program modules, and program data, each of which or some combination thereof may include an implementation of a network environment. Program modules 426 generally perform the functions and / or methods of the embodiments described herein.
[0109] The electronic device 412 may also communicate with one or more external devices 414 (e.g., keyboard, pointing device, camera, display 424, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 412, and / or communicate with any device that enables the electronic device 412 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed through an input / output (I / O) interface 422. In addition, the electronic device 412 may also communicate with one or more networks (e.g., a local area network (LAN), a wide area network (WAN) and / or a public network, such as the Internet) through a network adapter 420. As shown, the network adapter 420 communicates with other modules of the electronic device 412 through a bus 418. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 412, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, disk arrays (Redundant Arrays of Independent Disks, RAID) systems, tape drives, and data backup storage systems.
[0110] The processor 416 executes various functional applications and data processing by running the programs stored in the storage device 428, such as implementing the time synchronization method provided in the above embodiment of the present invention.
[0111] Embodiment 4
[0112] Embodiment 4 of the present invention provides a computer-readable storage medium, on which a computer program is stored, and when the program is executed by a processing device, a time synchronization method as in the embodiment of the present invention is implemented. The above-mentioned computer-readable medium of the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program, which may be used by or in combination with an instruction execution system, device or device. In the present disclosure, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, in which a computer-readable program code is carried. Such propagated data signals may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer readable signal media may also be any computer readable medium other than computer readable storage media, which may send, propagate, or transmit programs for use by or in conjunction with an instruction execution system, apparatus, or device. The program code contained on the computer readable medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (radio frequency), etc., or any suitable combination of the above.
[0113] In some embodiments, the client and the server may communicate using any currently known or future developed network protocol such as HTTP (HyperText Transfer Protocol), and may be interconnected with any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include a local area network ("LAN"), a wide area network ("WAN"), an internet (e.g., the Internet), and a peer-to-peer network (e.g., an ad hoc peer-to-peer network), as well as any currently known or future developed network.
[0114] The computer-readable medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0115] The computer-readable medium carries one or more programs. When the one or more programs are executed by the electronic device, the electronic device: sends an NTP synchronization message and receives a response message to the synchronization message;
[0116] Obtain the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process;
[0117] The corrected time of the client is obtained according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process.
[0118] Computer program code for performing the operations of the present disclosure may be written in one or more programming languages or a combination thereof, including, but not limited to, object-oriented programming languages, such as Java, Smalltalk, C++, and conventional procedural programming languages, such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a separate software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0119] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present disclosure. In this regard, each square box in the flow chart or block diagram can represent a module, a program segment or a part of a code, and the module, the program segment or a part of the code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some implementations as replacements, the functions marked in the square box can also occur in a sequence different from that marked in the accompanying drawings. For example, two square boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each square box in the block diagram and / or flow chart, and the combination of the square boxes in the block diagram and / or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0120] The units involved in the embodiments described in the present disclosure may be implemented by software or hardware, wherein the name of a unit does not, in some cases, limit the unit itself.
[0121] The functions described above herein may be performed at least in part by one or more hardware logic components. For example, without limitation, exemplary types of hardware logic components that may be used include: field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), and the like.
[0122] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0123] Note that the above are only preferred embodiments of the present invention and the technical principles used. Those skilled in the art will understand that the present invention is not limited to the specific embodiments described herein, and that various obvious changes, readjustments and substitutions can be made by those skilled in the art without departing from the scope of protection of the present invention. Therefore, although the present invention has been described in more detail through the above embodiments, the present invention is not limited to the above embodiments, and may include more other equivalent embodiments without departing from the concept of the present invention, and the scope of the present invention is determined by the scope of the appended claims.
Claims
1. A time synchronization method, characterized in that: include: Sending a Network Time Protocol (NTP) synchronization message and receiving a response message to the synchronization message; Obtain the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process; Obtain the corrected time of the client according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process; Among them, the time required for the response message to leave the local machine and arrive at the client application process includes the time required for the response message to reach the designated detection point LOCAL_IN in the Netfilter framework of the client from the designated detection point LOCAL_OUT in the Netfilter framework of the server plus the time corresponding to the time when the response message arrives at the client application process minus the time corresponding to the time when the response message arrives at the designated detection point LOCAL_IN in the Netfilter framework of the client.
2. The method according to claim 1, characterized in that The correction time of the client is: The time it takes for the response message to leave the local machine plus the time it takes for the response message to leave the local machine and arrive at the client application process.
3. The method according to claim 1, characterized in that The process of sending a time-out message and receiving a response message to the time-out message also includes: The timestamps of the synchronization message and the response message are replaced at a designated detection point in the Netfilter framework.
4. The method according to claim 3, characterized in that The timestamps of the synchronization message and the response message include: A first timestamp corresponding to a detection point at which the synchronization message leaves the local machine; A third timestamp corresponding to the detection point at which the synchronization message is sent on the local machine; A second timestamp corresponding to the detection point of the response message leaving the local machine; A fourth timestamp corresponding to the detection point at which the response message is sent on the local machine; The response message has a fifth timestamp corresponding to the detection point of the client application process.
5. The method according to claim 4, characterized in that The time required for the response message to leave the local machine and arrive at the client application process includes: The one-way transmission delay of the response message from leaving the local machine to being sent to the local machine, and the time required for the response message to be sent from the local machine to reach the client application process.
6. The method according to claim 5, characterized in that The one-way transmission delay is 1 / 2 of the difference between the first time period and the second time period, wherein the first time period is the difference obtained by subtracting the first time stamp from the fourth time stamp, and the second time period is the difference obtained by subtracting the third time stamp from the second time stamp; The time required for the response message to be sent from the local machine to the client application process is the difference obtained by subtracting the fourth timestamp from the fifth timestamp.
7. The method according to claim 3, characterized in that After replacing the timestamps of the synchronization message and the response message, the method further includes: Update the message authentication code corresponding to the synchronization message.
8. A time synchronization device, characterized in that: include: A message transmission module, used for sending a network time protocol synchronization message and receiving a response message to the synchronization message; An acquisition module is used to acquire the time when the response message leaves the local machine according to the Netfilter framework, and calculate the time required for the response message to leave the local machine and arrive at the client application process; A time synchronization module, used to obtain the corrected time of the client according to the time when the response message leaves the local machine and the time required for the response message to leave the local machine and arrive at the client application process; Among them, the time required for the response message to leave the local machine and arrive at the client application process includes the time required for the response message to reach the designated detection point LOCAL_IN in the Netfilter framework of the client from the designated detection point LOCAL_OUT in the Netfilter framework of the server plus the time corresponding to the time when the response message arrives at the client application process minus the time corresponding to the time when the response message arrives at the designated detection point LOCAL_IN in the Netfilter framework of the client.
9. An electronic device, characterized in that: include: one or more processors; A storage device for storing one or more programs; When the one or more programs are executed by the one or more processors, the processors are enabled to implement the time synchronization method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the time synchronization method according to any one of claims 1 to 7 is implemented.
Citation Information
Patent Citations
Ultraviolet light communication network clock synchronization system and method
CN106992833A
Network time service and evaluation system and method based on NTP
CN108322281A