NTP獲取網絡時間戳(C實現)


一、簡介   

    網絡時間協議(NTP)的首次實現記載在Internet Engineering Note之中,其精確度為數百毫秒。稍后出現了首個時間協議的規范,即RFC-778,它被命名為DCNET互聯網時間服務,而它提供這種服務還是借助於Internet control Message Protocol (ICMP),即互聯網控制消息協議中的時間戳和時間戳應答消息作為NTP。

二、基本工作原理

    NTP的基本工作原理如下圖所示。Device A和Device B通過網絡相連,它們都有自己獨立的系統時鍾,需要通過NTP實現各自系統時鍾的自動同步。為便於理解,作如下假設:
 ①在Device A和Device B的系統時鍾同步之前,Device A的時鍾設定為10:00:00am,Device B的時鍾設定為11:00:00am。
 ②Device B作為NTP時間服務器,即Device A將使自己的時鍾與Device B的時鍾同步。
 ③NTP報文在Device A和Device B之間單向傳輸所需要的時間為1秒。

系統時鍾同步的工作過程如下:
 ①Device A發送一個NTP報文給Device B,該報文帶有它離開Device A時的時間戳,該時間戳為10:00:00am(T1)。
 ②當此NTP報文到達Device B時,Device B加上自己的時間戳,該時間戳為11:00:01am(T2)。
 ③當此NTP報文離開Device B時,Device B再加上自己的時間戳,該時間戳為11:00:02am(T3)。
 ④當Device A接收到該響應報文時,Device A的本地時間為10:00:03am(T4)。
至此,Device A已經擁有足夠的信息來計算兩個重要的參數:
 ①NTP報文的往返時延Delay=(T4-T1)-(T3-T2)=2秒。 //NTP發送接受時間-NTP服務器響應時間
 ②Device A相對Device B的時間差offset=((T2-T1)+(T3-T4))/2=1小時。   //假設發送和接受耗時是一樣的
這樣,Device A就能夠根據這些信息來設定自己的時鍾,使之與Device B的時鍾同步。
三、NTP報文格式

    NTP有兩種不同類型的報文,一種是時鍾同步報文,另一種是控制報文。控制報文僅用於需要網絡管理的場合,它對於時鍾同步功能來說並不是必需的,這里不做介紹。

時鍾同步報文封裝在UDP報文中,其格式如下圖所示。

 主要字段的解釋如下:
·LI(Leap Indicator,閏秒提示):長度為2比特,值為“11”時表示告警狀態,時鍾未被同步。為其他值時NTP本身不做處理。
·VN(Version Number,版本號):長度為3比特,表示NTP的版本號,目前的最新版本為4。
·Mode:長度為3比特,表示NTP的工作模式。不同的值所表示的含義分別是:0未定義、1表示主動對等體模式、2表示被動對等體模式、3表示客戶模式、4表示服務器模式、5表示廣播模式或組播模式、6表示此報文為NTP控制報文、7預留給內部使用。
·Stratum:系統時鍾的層數,取值范圍為1~16,它定義了時鍾的准確度。層數為1的時鍾准確度最高,准確度從1到16依次遞減,層數為16的時鍾處於未同步狀態。
·Poll:輪詢時間,即兩個連續NTP報文之間的時間間隔。
·Precision:系統時鍾的精度。
·Root Delay:本地到主參考時鍾源的往返時間。
·Root Dispersion:系統時鍾相對於主參考時鍾的最大誤差。
·Reference Identifier:參考時鍾源的標識。
·Reference Timestamp:系統時鍾最后一次被設定或更新的時間。
·Originate Timestamp:NTP請求報文離開發送端時發送端的本地時間。
·Receive Timestamp:NTP請求報文到達接收端時接收端的本地時間。
·Transmit Timestamp:應答報文離開應答者時應答者的本地時間。
·Authenticator:驗證信息。

四、代碼示例獲取NTP時間

 根據前說的NTP報文格式,構建如下數據結構體,用來存儲請求報文:

/* NTP時鍾同步報文 */
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;
};

獲取NTP時間的完整代碼如下:

/*
* FILE: ntp.c
* NOTE: socket網絡編程學習,NTP時間獲取程序
*
* TIME: 2021年11月13日00:05:39
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <netinet/in.h>

#define   NTP_PORT   123
#define   TIME_PORT  37
#define   NTP_SERVER_IP  "cn.pool.ntp.org"

#define   NTP_PORT_STR   "123"
#define   NTPV1       "NTP/V1"
#define NTPV2 "NTP/V2"

#define NTPV3 "NTP/V3"
#define NTPV4 "NTP/V4"
#define TIME "TIME/UDP"

#define   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;

/* NTP時鍾同步報文 */
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];

int construct_packet(char *packet)
{
    char version = 1;
    long tmp_wrd;
    int port;
    time_t timer;
    strcpy(protocol, NTPV4);
    /*判斷協議版本*/
    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[5] - 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()函數,並設定超時時間為10s*/
    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->precision = ntohl(data[3]);
        ret_time->root_delay = ntohl(*(int*)&(data[4]));
        ret_time->root_dispersion = ntohl(*(int*)&(data[8]));
        ret_time->reference_identifier = ntohl(*(int*)&(data[12]));
        ret_time->reference_timestamp.coarse = ntohl(*(int*)&(data[16]));
        ret_time->reference_timestamp.fine = ntohl(*(int*)&(data[20]));
        ret_time->originage_timestamp.coarse = ntohl(*(int*)&(data[24]));
        ret_time->originage_timestamp.fine = ntohl(*(int*)&(data[28]));
        ret_time->receive_timestamp.coarse = ntohl(*(int*)&(data[32]));
        ret_time->receive_timestamp.fine = ntohl(*(int*)&(data[36]));
        ret_time->transmit_timestamp.coarse = ntohl(*(int*)&(data[40]));
        ret_time->transmit_timestamp.fine = ntohl(*(int*)&(data[44]));

        /* 將NTP時間戳轉換為日期 */
        time_t currentTime = ret_time->transmit_timestamp.coarse - JAN_1970;
        struct tm CurlocalTime;
        localtime_r(&currentTime, &CurlocalTime);
        char dateTime[30];
        strftime(dateTime, 30, "%Y-%m-%d %H:%M:%S %A", &CurlocalTime);

        printf("%s\n", dateTime);
    
        return 1;
    } /* end of if select */
   
    
    return 0;
}

/* 修改本地時間 */
int set_local_time(struct ntp_packet * pnew_time_packet)
{
    struct timeval tv;
    tv.tv_sec = pnew_time_packet->transmit_timestamp.coarse - JAN_1970;
    tv.tv_usec = USEC(pnew_time_packet->transmit_timestamp.fine);
    return settimeofday(&tv, NULL);
}

int main(int argc, char *argv[])
{
    int sockfd, rc;
    struct addrinfo hints, *res = NULL;
    struct ntp_packet new_time_packet;

    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET;
    hints.ai_socktype = SOCK_DGRAM;
    hints.ai_protocol = IPPROTO_UDP;
    
    /*調用 getaddrinfo()函數, 獲取地址信息*/
    rc = getaddrinfo(NTP_SERVER_IP, NTP_PORT_STR, &hints, &res);
    if (rc != 0)
    {
        perror("getaddrinfo");
        return 1;
    }

    /* 創建套接字 */
    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); //IPv4, 數據報套接字, UDP
    if (sockfd <0 )
    {
        perror("socket");
        return 1;
    }

    /*調用取得 NTP 時間的函數*/
    if (get_ntp_time(sockfd, res, &new_time_packet))
    {
        /*調整本地時間*/
        //if (!set_local_time(&new_time_packet))
        {
            printf("NTP client success!\n");
        }
    }
    
    close(sockfd);

    return 0;
}

編譯運行結果:

 

 

 

參考:http://ntp.neu.edu.cn/archives/21/  《NTP發展史》

           http://ntp.neu.edu.cn/archives/92/ 《NTP工作原理》

           http://ntp.neu.edu.cn/archives/95/   《NTP的報文格式》


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM