From 4611b5aca576acb3db8cd69a17fe9fd40d4294b0 Mon Sep 17 00:00:00 2001 From: JamesonHuang Date: Wed, 8 Jul 2015 19:16:28 +0800 Subject: [PATCH] ntp time polling get system time --- 1_6.h12_dev/ntp/timesync/.svn/entries | 1 + 1_6.h12_dev/ntp/timesync/.svn/format | 1 + ...5a742a74ac5a828077af2f0484686ee87.svn-base | Bin 0 -> 10496 bytes ...9b1a17fb3548b6d602a4ef6aa6416ceaa.svn-base | 2 + ...942c0db5d851dffe687891bd4c96e210f.svn-base | 20 ++ ...eae2efaa8d9a889de4a2ff881080cce88.svn-base | Bin 0 -> 10480 bytes ...9f45d11b3d55fcef68ff7058872f74403.svn-base | 159 +++++++++++++ ...b9a7192271ed8e575817755d897714d65.svn-base | 213 ++++++++++++++++++ 1_6.h12_dev/ntp/timesync/.svn/wc.db | Bin 0 -> 37888 bytes 1_6.h12_dev/ntp/timesync/Makefile | 20 ++ 1_6.h12_dev/ntp/timesync/README | 2 + 1_6.h12_dev/ntp/timesync/client | Bin 0 -> 10488 bytes 1_6.h12_dev/ntp/timesync/client.c | 165 ++++++++++++++ 1_6.h12_dev/ntp/timesync/server | Bin 0 -> 10496 bytes 1_6.h12_dev/ntp/timesync/server.c | 213 ++++++++++++++++++ 1_6.h12_dev/ntp/timesync/tags | 26 +++ 16 files changed, 822 insertions(+) create mode 100644 1_6.h12_dev/ntp/timesync/.svn/entries create mode 100644 1_6.h12_dev/ntp/timesync/.svn/format create mode 100644 1_6.h12_dev/ntp/timesync/.svn/pristine/20/20f12845a742a74ac5a828077af2f0484686ee87.svn-base create mode 100644 1_6.h12_dev/ntp/timesync/.svn/pristine/4e/4e8e67e9b1a17fb3548b6d602a4ef6aa6416ceaa.svn-base create mode 100644 1_6.h12_dev/ntp/timesync/.svn/pristine/69/6950178942c0db5d851dffe687891bd4c96e210f.svn-base create mode 100644 1_6.h12_dev/ntp/timesync/.svn/pristine/99/99ba86ceae2efaa8d9a889de4a2ff881080cce88.svn-base create mode 100644 1_6.h12_dev/ntp/timesync/.svn/pristine/a5/a573b249f45d11b3d55fcef68ff7058872f74403.svn-base create mode 100644 1_6.h12_dev/ntp/timesync/.svn/pristine/df/dff6b09b9a7192271ed8e575817755d897714d65.svn-base create mode 100644 1_6.h12_dev/ntp/timesync/.svn/wc.db create mode 100644 1_6.h12_dev/ntp/timesync/Makefile create mode 100644 1_6.h12_dev/ntp/timesync/README create mode 100755 1_6.h12_dev/ntp/timesync/client create mode 100644 1_6.h12_dev/ntp/timesync/client.c create mode 100755 1_6.h12_dev/ntp/timesync/server create mode 100644 1_6.h12_dev/ntp/timesync/server.c create mode 100644 1_6.h12_dev/ntp/timesync/tags diff --git a/1_6.h12_dev/ntp/timesync/.svn/entries b/1_6.h12_dev/ntp/timesync/.svn/entries new file mode 100644 index 0000000..48082f7 --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/.svn/entries @@ -0,0 +1 @@ +12 diff --git a/1_6.h12_dev/ntp/timesync/.svn/format b/1_6.h12_dev/ntp/timesync/.svn/format new file mode 100644 index 0000000..48082f7 --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/.svn/format @@ -0,0 +1 @@ +12 diff --git a/1_6.h12_dev/ntp/timesync/.svn/pristine/20/20f12845a742a74ac5a828077af2f0484686ee87.svn-base b/1_6.h12_dev/ntp/timesync/.svn/pristine/20/20f12845a742a74ac5a828077af2f0484686ee87.svn-base new file mode 100644 index 0000000000000000000000000000000000000000..84de0986eea1d554e781ed977392c06f3745cf82 GIT binary patch literal 10496 zcmeHNeQaCR6~A#3;)d{&mJULTng?=GiW)a5X)(}%-K6cyl(LQ{tXnCG6WfhxVh8)B zq=33?UQypdAb+5(6Mw7{5~`+2tI&k9vXQjun(l)wR2tgGHVUG}E}(T#qO?%F-?{Ie zX;5@bidRxFbuutOFY4=F!J6|x_P~eA~&lNdJ2Ls0}$TyJHzm;_v!sh?fSWE7LbYExM8x59Hz3<>6y__-%Rk zC-U&$&BNE`;hzJq#U2G)mo8^ckK$Bq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uUrMO#6)1Fz+9q%MWSp|2lYIOA;Bccb4fL6zwIp zZ*}=0&HdkmE~UAwXs#$rw;*@gn=ZfPP1T@BN^@DReLbqCvG=%tK5q`>*cYoNc%O5N z_o6^CVEb{G9}><_MVwXxrt^x5Y|rPyMsRc%u|1z}2bH6Io6(*kzZV?EM5)hSj}FtKK;r9+E!ll>nQ#w&PW!16Kha`J*~@pSlCd-S(i26- z@1g9pr>}xCsSChIY94KJ&W`2pqu{jX>*ob^LFMZR>*q8lMwC6rUub??Reh9pQ{#@r z@nh-$3Z%mJ{C`IL|3@@G&Xn8#i>P$k50?9-`e3Tnse;8`yDqoNJeu4iVDc9dz z`IoSzuGpUMCo0wdYvt|`^ZI!M zw$A?f|2JmjOZK_-4mFwBp7BkvPJ5wu*owLhhisV5*^cpD)Hv-ADf>gZZNh$X18m3g zITVQF@zdvQN~5}j*?nS5SB~eOsG$C9SwSJ&Z_eRKmy#2?Z@b3Mi*K=U#z~^gc1-^p DDH`eE literal 0 HcmV?d00001 diff --git a/1_6.h12_dev/ntp/timesync/.svn/pristine/4e/4e8e67e9b1a17fb3548b6d602a4ef6aa6416ceaa.svn-base b/1_6.h12_dev/ntp/timesync/.svn/pristine/4e/4e8e67e9b1a17fb3548b6d602a4ef6aa6416ceaa.svn-base new file mode 100644 index 0000000..d7692f7 --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/.svn/pristine/4e/4e8e67e9b1a17fb3548b6d602a4ef6aa6416ceaa.svn-base @@ -0,0 +1,2 @@ +sudo ./server +./client diff --git a/1_6.h12_dev/ntp/timesync/.svn/pristine/69/6950178942c0db5d851dffe687891bd4c96e210f.svn-base b/1_6.h12_dev/ntp/timesync/.svn/pristine/69/6950178942c0db5d851dffe687891bd4c96e210f.svn-base new file mode 100644 index 0000000..07b7fb7 --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/.svn/pristine/69/6950178942c0db5d851dffe687891bd4c96e210f.svn-base @@ -0,0 +1,20 @@ +.PHONY: all clean tags + +CFLAGS:=-Wall -O2 + +all: client server + +server: server.c + $(CC) $(CFLAGS) $^ -o $@ + strip -s $@ + +client: client.c + $(CC) $(CFLAGS) $^ -o $@ + strip -s $@ + +tags: + ctags -R . + +clean: + -rm *.o client server + diff --git a/1_6.h12_dev/ntp/timesync/.svn/pristine/99/99ba86ceae2efaa8d9a889de4a2ff881080cce88.svn-base b/1_6.h12_dev/ntp/timesync/.svn/pristine/99/99ba86ceae2efaa8d9a889de4a2ff881080cce88.svn-base new file mode 100644 index 0000000000000000000000000000000000000000..482da55dcdd8e6fd93e3bd15a1bca5cf050be972 GIT binary patch literal 10480 zcmeHNe{dA_760ZEhk|PO`2`F%v1l(xU6cVdKOpe?gIZQ6;-3Gx@ z8jUcn5z9DEhqiWxnRYB=XDZWnI@GBoAr! znQ=PP|MI=reed(W@4fH)zVDCS+ua|otXt*E%M)B&V!0snvO6e6_F0ILTO?GnN-<3Y z#f@T)xC)XfID)c+w?W6J%bzR}U3{580OK!k%t zM2$R;Q_`hUf7Xl;AJilS4N8V-yDb(>+3q{Aqd32AQE3vZReKZVWkmIlse)o*dwl)k ziiPcwlJGN;N4n-1;b0G{?g6 zc9DuEB4R_-3`Zg`iFJxrGu{yu$!O~)5$ozUQzB(XqRAxbP)%nn65b+O+dETH5lcp+ zV|LBW6b89Zn#~>Icmi#0=;%x+Nwc`CuC}VWxje8aP%*}jbLD~CsZTEa^5vO0nwl=m zZJTFF9yDm`@_I9Gh)<&x@GB*o8IMn=mGC=-yJu6qOS}e5WwfY8Qd$G37%QAt+6!qt zaPg^pPldoX7puL)DcIxpN;5I^=S_Dqn^}PBC$*cgV-) z1}YzP$esOdamWkV322)`PIHk2b)_)+_6tcE~+r+CuDd$a!q3 z+UPy+Hu|Rg+@}j;`+zwm{WB>%QW+6W%9rk}wTXW{<=h3DeMIc1gURFbzqjN8*PG(@4}>!2}sZiSIIY#bZyhViSUXly?YW5YUZ+n-VPJ)Rx96#AAAj?89#m))6f_ky*h8$sxB~k z4|&(3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include /* for time() and ctime() */ + +/* + * rfc1305的ntp时间中,时间为64bit,记录1900年后的秒数(utc格式) + * 高32位是整数部分,低32位是小数部分*/ +#define UTC_NTP 2208988800U /* 1970 - 1900 */ + +/* get Timestamp for NTP in LOCAL ENDIAN */ +void gettime64(uint32_t ts[]) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + ts[0] = tv.tv_sec + UTC_NTP; + ts[1] = (4294*(tv.tv_usec)) + ((1981*(tv.tv_usec))>>11); +} + +int die(const char *msg) +{ + fputs(msg, stderr); + exit(-1); +} + +int useage(const char *path) +{ + printf("Useage:\n\t%s \n", path); + return 1; +} + + +//开始连接服务器 +int open_connect(const char* server) +{ + int s; + struct addrinfo *saddr; + + /* printf("Connecting to server: %s\n", server); */ + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s == -1) { + die("Can not create socket.\n"); + } + + if (0 != getaddrinfo(server, "123", NULL, &saddr)) { + die("Server address not correct.\n"); + } + + if (connect(s, saddr->ai_addr, saddr->ai_addrlen) != 0) { + die("Connect error\n"); + } + + freeaddrinfo(saddr); + + return s; +} + +//发送请求报文 +void request(int fd) +{ + unsigned char buf[48] = {0}; + uint32_t tts[2]; /* Transmit Timestamp */ + + /* LI VN MODE = 00 100 011*/ + /* MODE:3表示客户模式,4表示服务器模式*/ + buf[0] = 0x23; + + gettime64(tts); + //此处将Originate Timestamp设置在Transmit Timestamp处 + (*(uint32_t *)&buf[40]) = htonl(tts[0]); + (*(uint32_t *)&buf[44])= htonl(tts[1]); + if (send(fd, buf, 48, 0) !=48 ) { + die("Send error\n"); + } +} + +//获得NTP应答报文 +void get_reply(int fd) +{ + unsigned char buf[48]; + uint32_t *pt; + // uint32_t t_last_update[2]; /* Reference Timestamp @ Server */ + uint32_t t1[2]; /* t1 = Originate Timestamp,即NTP请求报文离开发送端时发送端的本地时间 */ + uint32_t t2[2]; /* t2 = Receive Timestamp @ Server,即NTP请求报文到达接收端时接收端的本地时间 */ + uint32_t t3[2]; /* t3 = Transmit Timestamp @ Server,即应答报文离开应答者时应答者的本地时间 */ + uint32_t t4[2]; /* t4 = Receive Timestamp @ Client */ + double T1, T2, T3, T4; + double tfrac = 4294967296.0; + time_t curr_time; + time_t diff_sec; + + if (recv(fd, buf, 48, 0) < 48) { + die("Receive error\n"); + } + gettime64(t4); + pt = (uint32_t *)&buf[24]; + + t1[0] = htonl(*pt++); + t1[1] = htonl(*pt++); + + t2[0] = htonl(*pt++); + t2[1] = htonl(*pt++); + + t3[0] = htonl(*pt++); + t3[1] = htonl(*pt++); + + /* t1 = Transmit Timestamp @ Client */ + /* + * (Version=4, Mode=Server, + * Stratum = 0-15, etc.)*/ + + T1 = t1[0] + t1[1]/tfrac; + T2 = t2[0] + t2[1]/tfrac; + T3 = t3[0] + t3[1]/tfrac; + T4 = t4[0] + t4[1]/tfrac; + + //delay:NTP报文的往返时延 + //offset:client与server的时间差 + printf( "\ndelay = %lf\n" + "offset = %lf\n\n", + (T4-T1) - (T3-T2), + ((T2 - T1) + (T3 - T4)) /2 + ); + + diff_sec = ((int32_t)(t2[0] - t1[0]) + (int32_t)(t3[0] - t4[0])) /2; + curr_time = time(NULL) - diff_sec; + printf("Current Time at Server: %s\n", ctime(&curr_time)); +} + +int client(const char* server) +{ + int fd; + + fd = open_connect(server); + request(fd); + get_reply(fd); + close(fd); + + return 0; +} + + +int main(int argc, char *argv[], char **env) +{ + if (argc < 2) { + return useage(argv[0]); + } + + return client(argv[1]); +} diff --git a/1_6.h12_dev/ntp/timesync/.svn/pristine/df/dff6b09b9a7192271ed8e575817755d897714d65.svn-base b/1_6.h12_dev/ntp/timesync/.svn/pristine/df/dff6b09b9a7192271ed8e575817755d897714d65.svn-base new file mode 100644 index 0000000..94d8bdb --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/.svn/pristine/df/dff6b09b9a7192271ed8e575817755d897714d65.svn-base @@ -0,0 +1,213 @@ +#include +#include +#include +#include +#include +#include +#include /* gettimeofday() */ +#include +#include +#include +#include +#include +#include + +#include /* for time() and ctime() */ + +//1900至1970年间经过的总秒数 +#define UTC_NTP 2208988800U /* 1970 - 1900 */ + +/* get Timestamp for NTP in LOCAL ENDIAN */ +void gettime64(uint32_t ts[]) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + ts[0] = tv.tv_sec + UTC_NTP; + ts[1] = (4294*(tv.tv_usec)) + ((1981*(tv.tv_usec))>>11); +} + + +int die(const char *msg) +{ + if (msg) { + fputs(msg, stderr); + } + exit(-1); +} + + +void log_request_arrive(uint32_t *ntp_time) +{ + time_t t; + + if (ntp_time) { + t = *ntp_time - UTC_NTP; + } else { + t = time(NULL); + } + printf("A request comes at: %s", ctime(&t)); +} + + +void log_ntp_event(char *msg) +{ + puts(msg); +} + +//应答client +int ntp_reply( + int socket_fd, + struct sockaddr *saddr_p, + socklen_t saddrlen, + unsigned char recv_buf[], + uint32_t recv_time[]) +{ + /* Assume that recv_time is in local endian ! */ + unsigned char send_buf[48]; + uint32_t *u32p; + + /* do not use 0xC7 because the LI=11 can be `unsynchronized` */ + if ((recv_buf[0] & 0x07/*0xC7*/) != 0x3) { + /* LI VN Mode stimmt nicht */ + log_ntp_event("Invalid request: found error at the first byte"); + return 1; + } + + /* 设置 LI VN Mode 的值 + LI = 0 + VN = Version Number 同 Client + Mode = 4 + */ + send_buf[0] = (recv_buf[0] & 0x38) + 4; + + /* Stratum */ + send_buf[1] = 0x01; + /* Reference ID = "LOCL" ,即参考时钟源的标识*/ + *(uint32_t*)&send_buf[12] = htonl(0x4C4F434C); + + /* Copy Poll */ + send_buf[2] = recv_buf[2]; + + /* Precision in Microsecond ( from API gettimeofday() ) */ + send_buf[3] = (signed char)(-6); /* 2^(-6) sec */ + + u32p = (uint32_t *)&send_buf[4]; + /* 设置Root Delay = 0, Root Dispersion = 0 */ + *u32p++ = 0; + *u32p++ = 0; + + /* Reference ID前面已经设置*/ + u32p++; + + /* 设置 Reference TimeStamp */ + gettime64(u32p); + *u32p = htonl(*u32p - 60); /* -1 Min.*/ + u32p++; + *u32p = htonl(*u32p); /* -1 Min.*/ + u32p++; + + /* Originate Time = Transmit Time @ Client */ + *u32p++ = *(uint32_t *)&recv_buf[40]; + *u32p++ = *(uint32_t *)&recv_buf[44]; + + /* Receive Time @ Server */ + *u32p++ = htonl(recv_time[0]); + *u32p++ = htonl(recv_time[1]); + + /* 设置 Transmit Time*/ + gettime64(u32p); + *u32p = htonl(*u32p); /* -1 Min.*/ + u32p++; + *u32p = htonl(*u32p); /* -1 Min.*/ + + if ( sendto( socket_fd, + send_buf, + sizeof(send_buf), 0, + saddr_p, saddrlen) + < 48) { + perror("sendto error"); + return 1; + } + + return 0; +} + + +void request_process_loop(int fd) +{ + struct sockaddr src_addr; + socklen_t src_addrlen = sizeof(src_addr); + unsigned char buf[48]; + uint32_t recv_time[2]; + pid_t pid; + + while (1) { + while (recvfrom(fd, buf, + 48, 0, + &src_addr, + &src_addrlen) + < 48 ); /* invalid request */ + + gettime64(recv_time); + /* recv_time in local endian */ + log_request_arrive(recv_time); + + pid = fork(); + if (pid == 0) { + /* Child */ + ntp_reply(fd, &src_addr , src_addrlen, buf, recv_time); + exit(0); + } else if (pid == -1) { + perror("fork() error"); + die(NULL); + } + /* return to parent */ + } +} + + +void ntp_server() +{ + int s; + struct sockaddr_in sinaddr; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s == -1) { + perror("Can not create socket."); + die(NULL); + } + + memset(&sinaddr, 0, sizeof(sinaddr)); + sinaddr.sin_family = AF_INET; + //NTP基于UDP报文传输,使用的UDP端口号为123 + sinaddr.sin_port = htons(123); + sinaddr.sin_addr.s_addr = INADDR_ANY; + + if (0 != bind(s, (struct sockaddr *)&sinaddr, sizeof(sinaddr))) { + perror("Bind error"); + die(NULL); + } + + log_ntp_event( "\n========================================\n" + "= Server started, waiting for requests =\n" + "========================================\n"); + + request_process_loop(s); + close(s); +} + + +void wait_wrapper() +{ + int s; + wait(&s); +} + + +int main(int argc, char *argv[], char **env) +{ + signal(SIGCHLD,wait_wrapper); + ntp_server(); + return 0; +} diff --git a/1_6.h12_dev/ntp/timesync/.svn/wc.db b/1_6.h12_dev/ntp/timesync/.svn/wc.db new file mode 100644 index 0000000000000000000000000000000000000000..83ead65d542dec47610b0a7c09cebf7a2e3c953e GIT binary patch literal 37888 zcmeHQU2Gd!6`q;v*m1gP%eL8RHXAY_T@r7a)?<6@F-rw*GVNMToHlW`ttcACGj|$O z$IjaB-y#HQ7sQW{ctiprgoFSgAtCX|10W%I;8#enLgE2fZTZ=Ug$G~-(sJ(H8P7}{ zH_7g{wA(weJu~P2o%7v0_ue_@o}1;f^VK$%>_&6VXiI4>!tp%!eo5juZUMeex?cg# zB77t8^$s2`#=M&z=LK%&WuXrOqv($$%Dd2i&_B`N(JSaSCG<$g$P597z*~jDJ4Yd> z(GmE0{4w}BcmRI(?uDO61_t4mM?d7?!w!Z3L*Ri!;Jti=Q&w76>+d;sOimt69nEHr zrjo~+aw?fiS2Bv6nvoU5p2?`HI-}<7{ETfRvuf7LW=+K!0C{=zXAVB>U|h8m1Ri(<_98USpIclyt)E@dRGXWR^3;`E`NwgAY^MBSH7y=9dg20{T|2+B+2OoAY z1Q-Gj4+4ls97pH>|K-qsA0EM(qznOu0MYw#arXSPmcS5rI1wPvKhgh3`JZvI8uDSwwzHLFzLGa`az2&H$=Fh{l2cSUms1o=&F6A* z#>y)DUOv2LDbnGJX|{oT^jVD6T!N#y8>wvC!YS1-vL^77%Bz`t+B9=1*|O4#pA?Y-dlIs#7?ms-wlh{Lna!q? z*fgYHT z^^CJB(Bz5*>}7FEPTJg`+}v^AGSae~GAb1%ox+){ZQDxHOy_c{Y#CWH{zrvha7aL3 zL_bHXNP!!6Fa#b<1X8HV@2>ZY`bl=$rS3RmM%!AwF^>VrMy@yLBa*CR6YXnnvF(VlFHio zvb0hvf^kzSEtaLy%KSVDy>bQep`jibsEM=?6;F-xT(xfD>#gT%u;5uYuCyD@z3y|O z%lb!dsAZV` z#zMkSnpZ_TtD{=OG-|q0X{{UW)o^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W}({=s;fpWwJ5 zhuBYp%vu%RB3n)KSi8E0TQ}D4|?D;!vIJN?s%3yK`z7;`)Yv{>{Jr=nv16d$EH0r%@kcN3<2g%$oAle6=6u+Mk5O1g7ITV1Yt= z3mLsYYD0ixJ?JS5#OJ^wE|;qfutoS{wVOu0RRg<&rHB0CNUECAa=a3BGPw-pXBk&@ z(*VJIj|nnG)WMAbH|s_%6uDXlc){V*(g|1rCySr~!CPJrDtYi+bj>YCB(EJgMvlZ# zR6H*B(yKeoS(lyD@J8aXn3xdxn`5pk01c&{0M7M@`z&m7S`5Y`E&$H`TeJATmuLg% zuQ2|9oHB-B5S%cSD1tf1sRLG|rLne*pahp8ub4=ojGq`!|^VzlMH-USjiquRhqB zA#h(KfL;Rq9qi%L7w7u<1N1aNPkZQTA3YtUCouhH4n?8*{x z$PC@Mr9E5J&e1uS?#7Y|%Pq1(EaK(d7fnj52SL-qEahQI@Xz|mM_q%h`sv%39(zqfnG-BnH| zCFeU8jSM|KM*M!eB2!1@PRt{Hk%{RDA!9@?W*O+uc|j|_RMk;p`GA`rVP`#_j3 z2NF-9h&VavSYnFKF15?LW8n%!k>p7sA|_@+k?3-wK&+&clH{X&L_AJ&>*9b~dje%c h{0E|v8IueFhQQrKfQQTv-^~J8-V6bT!2O57{{Sfhh~EGJ literal 0 HcmV?d00001 diff --git a/1_6.h12_dev/ntp/timesync/Makefile b/1_6.h12_dev/ntp/timesync/Makefile new file mode 100644 index 0000000..07b7fb7 --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/Makefile @@ -0,0 +1,20 @@ +.PHONY: all clean tags + +CFLAGS:=-Wall -O2 + +all: client server + +server: server.c + $(CC) $(CFLAGS) $^ -o $@ + strip -s $@ + +client: client.c + $(CC) $(CFLAGS) $^ -o $@ + strip -s $@ + +tags: + ctags -R . + +clean: + -rm *.o client server + diff --git a/1_6.h12_dev/ntp/timesync/README b/1_6.h12_dev/ntp/timesync/README new file mode 100644 index 0000000..d7692f7 --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/README @@ -0,0 +1,2 @@ +sudo ./server +./client diff --git a/1_6.h12_dev/ntp/timesync/client b/1_6.h12_dev/ntp/timesync/client new file mode 100755 index 0000000000000000000000000000000000000000..58eaadf9360be6475cf25f650f22f9341cb75cea GIT binary patch literal 10488 zcmeHNdu&tJ89#QCVglHvrPMrBZab$7jJOFerIZ#sN$E`&wJAwjDo7n;UlW(aj_hj& zZP%tt%W{{}p-!66G)-ugDz>Q;lctSM8*xY*ppPxEl`)uBT9wX*J~C)&1})z2JNKOA z;*qJFwEuFfd%yGho$ov6JKuTSm!G_=VU68pV=~#T9<9fe8Ca zusVJom!wOn@w6VHe7lQ7NI%a=*pbk@4mN%wvieG)6^smK1b?|U(JvDJ`h*)pSY{>3nt*b6Yw6ng%k0(7N(Mv9tv-!f?zZh z>tHD@9$}j_Jrs$+B${AhJ=UqQq!!-7qFre{#Zr1iOD0K&N)pjXXd4T6BvKknb!b`_ zizYQ~%sLn(XQ+{+9_$Ro;%ISGXCf|`AiKMvzNR*~#JAM9Y%D*XTjE2DxX!{```c#WOJyaH?G~Qqq)c5Fe!3(fw_11vek^rcc;^_)Sg(Z_TQ)Dc}X()1)9G@dhLy%KAK0}y7pL28kE@29J zj&Xc~Foildav4O~QNk4B+$rFmysb$!&Ob5VwV)duQ~MrsWdDrr^;4I#FTvvMS+#F= zKSgN2?)0;&O!h&r`-oUSSoLAOapYmucvtOtZ=|ug{$QpJI_klJkr^Zo4mLXskz~v%Tpc*6UKz6li8&Z#4)~_F{ZJMmjwE*>)?RVc{L;qQNW(y3nadf10sN|#B z3bUQ(E%ht7VNrWlvvkSX@4;@YvVV|4ez-OFC~U_4X{x%Q8kaB|71j8AMC~gsD~H#r zQRnp_P{02opYJR7s(lS!M@9EjEsq;TYHZ+lm<{T{=rq+h5E~f6P}qiz+G4d2HW78H zSws37OlD>MV0JUA*g4*wIq#P*hsW7&6!pWKeD)Y}X8(A(agmP{A2WEd2c>+t`1-gq zo-IvPM^)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ntz@ zImM`h%3;srgm%AcuW@>wE4s_6>?p2rmOopfIxDwdqdKd5rrhWBcR8yA&dPwZyvC_O z5BhM1kMA#0&u-LXr*xkW}u zX^uw?o}`>EaPJg9&&hY?5vPggh;sRTb)LZTIf*~}f%D+?Bqe%oq9n^dy_`=F-evcb z82jeiTWn(a4IyJUv3%_Q12dEPw2Ar!zFtTviev__Un$9Fs9mCi^gK#Qev`Ptglwe5 z&vTN`2rT`kcXE{Ew+Y!k9?f}wx`jSo1ad6nIU?{nQEt}q|3Ay`*o7?(iE%w9J1Ano>`iC3umQiY4M#fFR0}c-=$>BG=WSQC?5X zI7b0cmWYJ(5c6s6!DuqnsRi33u$9QP2!@i$&^FUT<{t?|4Yh|lV_`I%K!rH@cq?X& z8&fIf3nw}|@nWkmp#LML^+j`6uA6GRA87pl9CAH#5+^y&CB6MOjh_sjc#ya8fEy7Ov zFZHBck8;|RB$oS9w=nPv`$GQ(%^pF9T$T12v2SIBz1#<7`-T0dc}Vs(I4H?|bwn7* zeV8C!YYOe307o$)|0v1pP%ljiBzb)?k=d5aliY^GYQJ}ipJ=vM*eeAA=PxNyF6Gn6 zTkYw)Af;j9=<~AUoE^6lkbMUktG&E#M#RTudHs<3Vp>SZb_#nL|1NRe?GpB*W1410 z#!u3_3hd?oi{$@~I;5hIh3&ssV1FE+GAJE)alt+>h+kp*d!a_I%ImPaPmupZD%3B; zzlAL|CGF*Xgh%{Ot5ClXAA&8pDDCMpDJA7g+P?{1ntRe-UPtBs#N_|M +#include +#include +#include +#include /* gettimeofday() */ +#include +#include +#include +#include +#include + +#include /* for time() and ctime() */ + +/* + * rfc1305的ntp时间中,时间为64bit,记录1900年后的秒数(utc格式) + * 高32位是整数部分,低32位是小数部分*/ +#define UTC_NTP 2208988800U /* 1970 - 1900 */ + +/* get Timestamp for NTP in LOCAL ENDIAN */ +void gettime64(uint32_t ts[]) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + ts[0] = tv.tv_sec + UTC_NTP; + ts[1] = (4294*(tv.tv_usec)) + ((1981*(tv.tv_usec))>>11); +} + +int die(const char *msg) +{ + fputs(msg, stderr); + exit(-1); +} + +int useage(const char *path) +{ + printf("Useage:\n\t%s \n", path); + return 1; +} + + +//开始连接服务器 +int open_connect(const char* server) +{ + int s; + struct addrinfo *saddr; + + /* printf("Connecting to server: %s\n", server); */ + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s == -1) { + die("Can not create socket.\n"); + } + + if (0 != getaddrinfo(server, "123", NULL, &saddr)) { + die("Server address not correct.\n"); + } + + if (connect(s, saddr->ai_addr, saddr->ai_addrlen) != 0) { + die("Connect error\n"); + } + + freeaddrinfo(saddr); + + return s; +} + +//发送请求报文 +void request(int fd) +{ + unsigned char buf[48] = {0}; + uint32_t tts[2]; /* Transmit Timestamp */ + + /* LI VN MODE = 00 100 011*/ + /* MODE:3表示客户模式,4表示服务器模式*/ + buf[0] = 0x23; + + gettime64(tts); + //此处将Originate Timestamp设置在Transmit Timestamp处 + (*(uint32_t *)&buf[40]) = htonl(tts[0]); + (*(uint32_t *)&buf[44])= htonl(tts[1]); + if (send(fd, buf, 48, 0) !=48 ) { + die("Send error\n"); + } +} + +//获得NTP应答报文 +void get_reply(int fd) +{ + unsigned char buf[48]; + uint32_t *pt; + // uint32_t t_last_update[2]; /* Reference Timestamp @ Server */ + uint32_t t1[2]; /* t1 = Originate Timestamp,即NTP请求报文离开发送端时发送端的本地时间 */ + uint32_t t2[2]; /* t2 = Receive Timestamp @ Server,即NTP请求报文到达接收端时接收端的本地时间 */ + uint32_t t3[2]; /* t3 = Transmit Timestamp @ Server,即应答报文离开应答者时应答者的本地时间 */ + uint32_t t4[2]; /* t4 = Receive Timestamp @ Client */ + double T1, T2, T3, T4; + double tfrac = 4294967296.0; + time_t curr_time; + time_t diff_sec; + + if (recv(fd, buf, 48, 0) < 48) { + die("Receive error\n"); + } + gettime64(t4); + pt = (uint32_t *)&buf[24]; + + t1[0] = htonl(*pt++); + t1[1] = htonl(*pt++); + + t2[0] = htonl(*pt++); + t2[1] = htonl(*pt++); + + t3[0] = htonl(*pt++); + t3[1] = htonl(*pt++); + + /* t1 = Transmit Timestamp @ Client */ + /* + * (Version=4, Mode=Server, + * Stratum = 0-15, etc.)*/ + + T1 = t1[0] + t1[1]/tfrac; + T2 = t2[0] + t2[1]/tfrac; + T3 = t3[0] + t3[1]/tfrac; + T4 = t4[0] + t4[1]/tfrac; + + //delay:NTP报文的往返时延 + //offset:client与server的时间差 + printf( "\ndelay = %lf\n" + "offset = %lf\n\n", + (T4-T1) - (T3-T2), + ((T2 - T1) + (T3 - T4)) /2 + ); + + diff_sec = ((int32_t)(t2[0] - t1[0]) + (int32_t)(t3[0] - t4[0])) /2; + curr_time = time(NULL) - diff_sec; + printf("Current Time at Server: %s\n", ctime(&curr_time)); + printf("Current TimeStamp at Server: %d\n", (int)curr_time); +} + +int client(const char* server) +{ + int fd; + + fd = open_connect(server); + request(fd); + get_reply(fd); + close(fd); + + return 0; +} + + +int main(int argc, char *argv[], char **env) +{ + while(1) + { + if (argc < 2) + { + return useage(argv[0]); + } + //return client(argv[1]); + client(argv[1]); + sleep(3); + } +} diff --git a/1_6.h12_dev/ntp/timesync/server b/1_6.h12_dev/ntp/timesync/server new file mode 100755 index 0000000000000000000000000000000000000000..db8a42071f4b6b86167def0476ebb16bac571a77 GIT binary patch literal 10496 zcmeHNeQ;FO6~EbR$Qp1HP&5J~PiN@_hb##M8UeTrN=>k|h{=-OB@4+W-F?B} zSEDPAuQ8fwI~`{HLuT4>m`-O(J5z^JOE!c8{TidwI$CX|){nPXThtmOD%;<=@1Epk zbvsP|=s(TPynD|1-E+=8_uP;7_Pu=brshqqq9Vb?C2kZXX_o{fWY=J?m7>Dx#U&yj zmWah-4v-RD0og*-=9o?@*GxT1uNc>KMY;^{#+qm@ctl^kEl{kSO&xT>^@=se6Gf zGQ~(FKz0;s#nn;-ZI=wwLn@ApqHISZ*;OgKDrLuXTm{CI$46tMt5x}Hbp;@i93Ub> zvY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@ -0,0 +1,213 @@ +#include +#include +#include +#include +#include +#include +#include /* gettimeofday() */ +#include +#include +#include +#include +#include +#include + +#include /* for time() and ctime() */ + +//1900至1970年间经过的总秒数 +#define UTC_NTP 2208988800U /* 1970 - 1900 */ + +/* get Timestamp for NTP in LOCAL ENDIAN */ +void gettime64(uint32_t ts[]) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + + ts[0] = tv.tv_sec + UTC_NTP; + ts[1] = (4294*(tv.tv_usec)) + ((1981*(tv.tv_usec))>>11); +} + + +int die(const char *msg) +{ + if (msg) { + fputs(msg, stderr); + } + exit(-1); +} + + +void log_request_arrive(uint32_t *ntp_time) +{ + time_t t; + + if (ntp_time) { + t = *ntp_time - UTC_NTP; + } else { + t = time(NULL); + } + printf("A request comes at: %s", ctime(&t)); +} + + +void log_ntp_event(char *msg) +{ + puts(msg); +} + +//应答client +int ntp_reply( + int socket_fd, + struct sockaddr *saddr_p, + socklen_t saddrlen, + unsigned char recv_buf[], + uint32_t recv_time[]) +{ + /* Assume that recv_time is in local endian ! */ + unsigned char send_buf[48]; + uint32_t *u32p; + + /* do not use 0xC7 because the LI=11 can be `unsynchronized` */ + if ((recv_buf[0] & 0x07/*0xC7*/) != 0x3) { + /* LI VN Mode stimmt nicht */ + log_ntp_event("Invalid request: found error at the first byte"); + return 1; + } + + /* 设置 LI VN Mode 的值 + LI = 0 + VN = Version Number 同 Client + Mode = 4 + */ + send_buf[0] = (recv_buf[0] & 0x38) + 4; + + /* Stratum */ + send_buf[1] = 0x01; + /* Reference ID = "LOCL" ,即参考时钟源的标识*/ + *(uint32_t*)&send_buf[12] = htonl(0x4C4F434C); + + /* Copy Poll */ + send_buf[2] = recv_buf[2]; + + /* Precision in Microsecond ( from API gettimeofday() ) */ + send_buf[3] = (signed char)(-6); /* 2^(-6) sec */ + + u32p = (uint32_t *)&send_buf[4]; + /* 设置Root Delay = 0, Root Dispersion = 0 */ + *u32p++ = 0; + *u32p++ = 0; + + /* Reference ID前面已经设置*/ + u32p++; + + /* 设置 Reference TimeStamp */ + gettime64(u32p); + *u32p = htonl(*u32p - 60); /* -1 Min.*/ + u32p++; + *u32p = htonl(*u32p); /* -1 Min.*/ + u32p++; + + /* Originate Time = Transmit Time @ Client */ + *u32p++ = *(uint32_t *)&recv_buf[40]; + *u32p++ = *(uint32_t *)&recv_buf[44]; + + /* Receive Time @ Server */ + *u32p++ = htonl(recv_time[0]); + *u32p++ = htonl(recv_time[1]); + + /* 设置 Transmit Time*/ + gettime64(u32p); + *u32p = htonl(*u32p); /* -1 Min.*/ + u32p++; + *u32p = htonl(*u32p); /* -1 Min.*/ + + if ( sendto( socket_fd, + send_buf, + sizeof(send_buf), 0, + saddr_p, saddrlen) + < 48) { + perror("sendto error"); + return 1; + } + + return 0; +} + + +void request_process_loop(int fd) +{ + struct sockaddr src_addr; + socklen_t src_addrlen = sizeof(src_addr); + unsigned char buf[48]; + uint32_t recv_time[2]; + pid_t pid; + + while (1) { + while (recvfrom(fd, buf, + 48, 0, + &src_addr, + &src_addrlen) + < 48 ); /* invalid request */ + + gettime64(recv_time); + /* recv_time in local endian */ + log_request_arrive(recv_time); + + pid = fork(); + if (pid == 0) { + /* Child */ + ntp_reply(fd, &src_addr , src_addrlen, buf, recv_time); + exit(0); + } else if (pid == -1) { + perror("fork() error"); + die(NULL); + } + /* return to parent */ + } +} + + +void ntp_server() +{ + int s; + struct sockaddr_in sinaddr; + + s = socket(AF_INET, SOCK_DGRAM, 0); + if (s == -1) { + perror("Can not create socket."); + die(NULL); + } + + memset(&sinaddr, 0, sizeof(sinaddr)); + sinaddr.sin_family = AF_INET; + //NTP基于UDP报文传输,使用的UDP端口号为123 + sinaddr.sin_port = htons(123); + sinaddr.sin_addr.s_addr = INADDR_ANY; + + if (0 != bind(s, (struct sockaddr *)&sinaddr, sizeof(sinaddr))) { + perror("Bind error"); + die(NULL); + } + + log_ntp_event( "\n========================================\n" + "= Server started, waiting for requests =\n" + "========================================\n"); + + request_process_loop(s); + close(s); +} + + +void wait_wrapper() +{ + int s; + wait(&s); +} + + +int main(int argc, char *argv[], char **env) +{ + signal(SIGCHLD,wait_wrapper); + ntp_server(); + return 0; +} diff --git a/1_6.h12_dev/ntp/timesync/tags b/1_6.h12_dev/ntp/timesync/tags new file mode 100644 index 0000000..92f84cb --- /dev/null +++ b/1_6.h12_dev/ntp/timesync/tags @@ -0,0 +1,26 @@ +!_TAG_FILE_FORMAT 2 /extended format; --format=1 will not append ;" to lines/ +!_TAG_FILE_SORTED 1 /0=unsorted, 1=sorted, 2=foldcase/ +!_TAG_PROGRAM_AUTHOR Darren Hiebert /dhiebert@users.sourceforge.net/ +!_TAG_PROGRAM_NAME Exuberant Ctags // +!_TAG_PROGRAM_URL http://ctags.sourceforge.net /official site/ +!_TAG_PROGRAM_VERSION 5.9~svn20110310 // +CFLAGS Makefile /^CFLAGS:=-Wall -O2$/;" m +UTC_NTP client.c 17;" d file: +UTC_NTP server.c 18;" d file: +client client.c /^int client(const char* server)$/;" f +die client.c /^int die(const char *msg)$/;" f +die server.c /^int die(const char *msg)$/;" f +get_reply client.c /^void get_reply(int fd)$/;" f +gettime64 client.c /^void gettime64(uint32_t ts[])$/;" f +gettime64 server.c /^void gettime64(uint32_t ts[])$/;" f +log_ntp_event server.c /^void log_ntp_event(char *msg)$/;" f +log_request_arrive server.c /^void log_request_arrive(uint32_t *ntp_time)$/;" f +main client.c /^int main(int argc, char *argv[], char **env)$/;" f +main server.c /^int main(int argc, char *argv[], char **env)$/;" f +ntp_reply server.c /^int ntp_reply($/;" f +ntp_server server.c /^void ntp_server()$/;" f +open_connect client.c /^int open_connect(const char* server)$/;" f +request client.c /^void request(int fd)$/;" f +request_process_loop server.c /^void request_process_loop(int fd)$/;" f +useage client.c /^int useage(const char *path)$/;" f +wait_wrapper server.c /^void wait_wrapper()$/;" f