ntpclient.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "ntpclient.h"
  2. #define NTP_PORT 123
  3. #define NTP_PACKET_SIZE 48
  4. // 发送NTP请求
  5. static int sendNTPRequest(NTPClient* client) {
  6. client->ntp_packet[0] = 0x1B; // LI = 0, VN = 3, Mode = 3 (client)
  7. ssize_t sentBytes = sendto(client->sockfd, client->ntp_packet, NTP_PACKET_SIZE, 0,
  8. (struct sockaddr *)&client->server_addr, sizeof(client->server_addr));
  9. if (sentBytes < 0) {
  10. perror("sendto failed");
  11. return -1;
  12. }
  13. return 0;
  14. }
  15. // 接收NTP响应
  16. static int receiveNTPResponse(NTPClient* client) {
  17. socklen_t len = sizeof(client->server_addr);
  18. ssize_t receivedBytes = recvfrom(client->sockfd, client->ntp_packet, NTP_PACKET_SIZE, 0,
  19. (struct sockaddr *)&client->server_addr, &len);
  20. if (receivedBytes < 0) {
  21. perror("recvfrom failed");
  22. return -1;
  23. }
  24. return 0;
  25. }
  26. // 解析NTP响应中的时间
  27. static time_t parseNTPResponse(const char* ntp_packet) {
  28. unsigned int sec_since_1900 = ntohl(((unsigned int *)ntp_packet)[10]);
  29. unsigned int frac_sec = ntohl(((unsigned int *)ntp_packet)[11]);
  30. double frac = frac_sec * 2.0e-32;
  31. return (time_t)(sec_since_1900 - 2208988800U) + (time_t)frac;
  32. }
  33. // 创建NTP客户端实例
  34. NTPClient* createNTPClient(const char* ntp_server, int port) {
  35. NTPClient* client = (NTPClient*)malloc(sizeof(NTPClient));
  36. if (!client) {
  37. perror("malloc failed");
  38. return NULL;
  39. }
  40. // 创建UDP套接字
  41. if ((client->sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
  42. perror("socket creation failed");
  43. free(client);
  44. return NULL;
  45. }
  46. // 设置NTP服务器地址
  47. memset(&client->server_addr, 0, sizeof(client->server_addr));
  48. client->server_addr.sin_family = AF_INET;
  49. client->server_addr.sin_port = htons(port);
  50. if (inet_aton(ntp_server, &client->server_addr.sin_addr) == 0) {
  51. fprintf(stderr, "Invalid address/Address not supported\n");
  52. close(client->sockfd);
  53. free(client);
  54. return NULL;
  55. }
  56. return client;
  57. }
  58. // 销毁NTP客户端实例
  59. void destroyNTPClient(NTPClient* client) {
  60. if (client) {
  61. close(client->sockfd);
  62. free(client);
  63. }
  64. }
  65. // 从NTP服务器获取当前时间
  66. int getCurrentTimeFromNTP(NTPClient* client, time_t* current_time) {
  67. if (sendNTPRequest(client) < 0) {
  68. return -1;
  69. }
  70. if (receiveNTPResponse(client) < 0) {
  71. return -1;
  72. }
  73. *current_time = parseNTPResponse(client->ntp_packet);
  74. return 0;
  75. }
  76. // 根据时间获取星期信息
  77. int getCurrentWeekday(time_t current_time,char *weekday) {
  78. struct tm * timeinfo;
  79. timeinfo = localtime(&current_time);
  80. if (timeinfo == NULL) {
  81. return -1;
  82. }
  83. // 格式化星期几
  84. strftime(weekday, 20, "%A", timeinfo); // %A 代表完整的星期名,例如:Sunday
  85. return 0;
  86. }