ntpclient.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #define _GNU_SOURCE
  2. #include <netdb.h>
  3. #include <signal.h>
  4. #include <unistd.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include "ntpclient.h"
  8. #include "thread.h"
  9. #define NTP_PORT 123
  10. #define NTP_PACKET_SIZE 48
  11. // 发送NTP请求
  12. static int sendNTPRequest(NTPClient* client) {
  13. client->ntp_packet[0] = 0x1B; // LI = 0, VN = 3, Mode = 3 (client)
  14. ssize_t sentBytes = sendto(client->sockfd, client->ntp_packet, NTP_PACKET_SIZE, 0,
  15. &client->server_addr, sizeof(client->server_addr));
  16. if (sentBytes < 0) {
  17. perror("sendto failed");
  18. return -1;
  19. }
  20. return 0;
  21. }
  22. // 接收NTP响应
  23. static int receiveNTPResponse(NTPClient* client) {
  24. socklen_t len = sizeof(client->server_addr);
  25. ssize_t receivedBytes = recvfrom(client->sockfd, client->ntp_packet, NTP_PACKET_SIZE, 0,
  26. &client->server_addr, &len);
  27. if (receivedBytes < 0) {
  28. perror("recvfrom failed");
  29. return -1;
  30. }
  31. return 0;
  32. }
  33. // 解析NTP响应中的时间
  34. static time_t parseNTPResponse(const char* ntp_packet) {
  35. unsigned int sec_since_1900 = ntohl(((unsigned int *)ntp_packet)[10]);
  36. unsigned int frac_sec = ntohl(((unsigned int *)ntp_packet)[11]);
  37. double frac = frac_sec * 2.0e-32;
  38. return (time_t)(sec_since_1900 - 2208988800U) + (time_t)frac;
  39. }
  40. typedef struct {
  41. char ip[64];
  42. uint16_t port;
  43. }ipaddr_t;
  44. static int get_addr(ipaddr_t *addr, struct addrinfo *info)
  45. {
  46. addr->ip[0] = 0; addr->port = 0;
  47. if(info->ai_family==AF_INET) {
  48. struct sockaddr_in* p=(struct sockaddr_in*)(info->ai_addr);
  49. addr->port = htons(p->sin_port);
  50. inet_ntop(p->sin_family, &p->sin_addr, addr->ip, sizeof(addr->ip));
  51. }
  52. else if(info->ai_family==AF_INET6) {
  53. struct sockaddr_in6* p=(struct sockaddr_in6*)(info->ai_addr);
  54. addr->port = htons(p->sin6_port);
  55. inet_ntop(p->sin6_family, &p->sin6_addr, addr->ip, sizeof(addr->ip));
  56. }
  57. else {
  58. return -1;
  59. }
  60. return 0;
  61. }
  62. static void *tmp_thread(void *arg)
  63. {
  64. ipaddr_t ipa;
  65. int r,fd=-1,err;
  66. struct addrinfo hints;
  67. struct addrinfo *rp;
  68. NTPClient *pc=(NTPClient*)arg;
  69. char tmp[32];
  70. memset(&hints, 0, sizeof hints);
  71. hints.ai_family = AF_UNSPEC;
  72. hints.ai_socktype = SOCK_DGRAM;
  73. hints.ai_protocol = 0;//IPPROTO_UDP;
  74. pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
  75. pthread_testcancel();
  76. pc->res = NULL;
  77. pc->flag = 0;
  78. sprintf(tmp, "%d", pc->port);
  79. printf("___getaddrinfo in\n");
  80. err = getaddrinfo(pc->server, tmp, &hints, &pc->res);
  81. if(err){
  82. printf("___getaddrinfo from %s failed, %s\n", pc->server, gai_strerror(err));
  83. pthread_exit(NULL);
  84. }
  85. printf("___getaddrinfo out\n");
  86. for(rp=pc->res; rp; rp=rp->ai_next){
  87. get_addr(&ipa, rp);
  88. printf("socket to %s:%d ", ipa.ip, ipa.port);
  89. fd = socket(rp->ai_family, rp->ai_socktype, 0);
  90. if (fd>=0) {
  91. pc->sockfd = fd;
  92. pc->server_addr = *rp->ai_addr;
  93. printf("ok!\n");
  94. break;
  95. }
  96. printf("failed!\n");
  97. }
  98. if(fd>=0) {
  99. pc->flag = 1;
  100. }
  101. else {
  102. pc->flag = -1;
  103. }
  104. pthread_exit(NULL);
  105. }
  106. NTPClient* createNTPClient(const char* ntp_server, int port, int timeout_sec) {
  107. NTPClient* client = (NTPClient*)malloc(sizeof(NTPClient));
  108. if (!client) {
  109. perror("createNTPClient, malloc failed\n");
  110. return NULL;
  111. }
  112. pthread_t tid;
  113. client->server = (char*)ntp_server;
  114. client->port = port;
  115. pthread_create(&tid, NULL, tmp_thread, client);
  116. for(int i=0; i<timeout_sec; i++) {
  117. if(client->flag==1) {
  118. break;
  119. }
  120. sleep(1);
  121. }
  122. if(client->flag!=1) {
  123. if(client->flag==0) {
  124. printf("___getaddrinfo timeout, cancel thread %lu\n", (unsigned long)tid);
  125. pthread_cancel(tid);//SIGTERM
  126. pthread_join(tid, NULL);
  127. printf("___cancel ok!\n");
  128. }
  129. freeaddrinfo(client->res);
  130. free(client); return NULL;
  131. }
  132. freeaddrinfo(client->res);
  133. printf("___createNTPClient ok!\n");
  134. return client;
  135. }
  136. // 销毁NTP客户端实例
  137. void destroyNTPClient(NTPClient* client) {
  138. if (client) {
  139. close(client->sockfd);
  140. free(client);
  141. }
  142. }
  143. // 从NTP服务器获取当前时间
  144. int getCurrentTimeFromNTP(NTPClient* client, time_t* current_time) {
  145. if (sendNTPRequest(client) < 0) {
  146. return -1;
  147. }
  148. if (receiveNTPResponse(client) < 0) {
  149. return -1;
  150. }
  151. *current_time = parseNTPResponse(client->ntp_packet);
  152. return 0;
  153. }
  154. // 根据时间获取星期信息
  155. int getCurrentWeekday(time_t current_time,char *weekday) {
  156. struct tm * timeinfo;
  157. timeinfo = localtime(&current_time);
  158. if (timeinfo == NULL) {
  159. return -1;
  160. }
  161. // 格式化星期几
  162. strftime(weekday, 20, "%A", timeinfo); // %A 代表完整的星期名,例如:Sunday
  163. return 0;
  164. }