ntpclient.c 5.3 KB

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