ntpclient.c 5.3 KB

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