blob: cdf68f0d9187e1b0f895d8300c053ef7b6bdb45f [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001//SNTP
2#include <stdio.h>
3#include <stdlib.h>
4#include <time.h>
5#include "cfg_api.h"
6#include "message.h"
7#include <errno.h>
8#include <sys/types.h>
9#include <sys/msg.h>
10#include <sys/ipc.h>
11#include <errno.h>
12#include <syslog.h>
13#include <pthread.h>
14//#include "sntp_msg.h"
15#include "softap_api.h"
16
17/**************************************************************************
18* ºê¶¨ÒåÇø
19**************************************************************************/
20#define SNTP_PORT 123
21#define SNTP_8HOUR (3600U * 8U)
22#define SNTP_SECONDS_FROM_1900_TO_1970 (86400U*(365U*70U+17U))
23#define SNTP_JAN_2000 946684791
24#define SNTP_WAIT_TIME_HOUR (60*60)
25#define SNTP_WAIT_TIME_DAY (60*60*24)
26#define SNTP_WAIT_TIME_WEEK (SNTP_WAIT_TIME_DAY*7)
27#define SNTP_DEFAULT_NEXT_SYNC_INTERVAL_TIME_WHEN_SUCCESS SNTP_WAIT_TIME_WEEK /* 30DAY */
28#define SNTP_DEFAULT_NEXT_SYNC_INTERVAL_TIME_WHEN_FAILURE (60*10) /* s */
29#define SNTP_DEFAULT_SYNC_SELECT_INTERVAL_TIME 30/*s*/
30#define SNTP_SERVER_NUM "sntp_server_count"//3
31#define SNTP_PROCESS_OVER_TIME "sntp_process_over_time"
32#define SNTP_PROCESS_RESULT "sntp_process_result"
33#define SNTP_PROCESS_STATE "sntp_process_state"
34//#define SNTP_ERROR_STATE "sntp_error_state"
35#define SNTP_SUCCESS_COUNT "sntp_success_count"
36
37//ÓÃÓÚ²»Í¬µÄ½ø³Ì¼äͬ²½
38#define TIME_SEM_KEY_1 0x0A2B
39#define TIME_SEM_KEY_2 0x3A4B
40
41
42
43/**************************************************************************
44* È«¾Ö±äÁ¿¶¨ÒåÇø
45**************************************************************************/
46pthread_mutex_t g_sntp_getaddrinfo_mutex;
47pthread_mutex_t g_sntp_getnameinfo_mutex;
48
49
50
51/**************************************************************************
52*Êý¾Ý³¤¶È¶¨Òå
53**************************************************************************/
54#define NV_ITEM_STRING_LEN_5 5
55#define NV_ITEM_STRING_LEN_10 10
56#define NV_ITEM_STRING_LEN_20 20
57#define NV_ITEM_STRING_LEN_50 50
58#define NV_ITEM_STRING_LEN_64 64
59#define NV_ITEM_STRING_LEN_150 150
60#define NV_ITEM_STRING_LEN_200 200
61#define NV_FW_RULE_MAX_LEN_V6 400
62#define NV_ITEM_VALUE_STRING_LEN 50
63
64
65
66/**************************************************************************
67* Êý¾Ý½á¹¹¶¨ÒåÇø
68**************************************************************************/
69
70union semun {
71 int val;
72 struct semid_ds *buf;
73 unsigned short *array;
74};
75
76typedef enum _sntp_result_type_t {
77 SNTP_RESULT_FAIL = -1,
78 SNTP_RESULT_OK,
79 SNTP_RESULT_CONTINUE,
80 SNTP_RESULT_INVALID_SERVER,
81} sntp_result_type_t;
82
83typedef enum _sntp_error_type_t {
84 SNTP_ERROR_NOT_GET_HOST,
85 SNTP_ERROR_CREATE_SOCKET,
86 SNTP_ERROR_SEND_TO,
87 SNTP_ERROR_SELECT_OVER_TIME,
88 SNTP_ERROR_SELECT_RETURN_ERR,
89 SNTP_ERROR_RECV_FROM,
90 SNTP_ERROR_ALL_SERVER_INVALID,
91} sntp_error_type_t;
92
93typedef struct NTP_TIME_FMT {
94 time_t seconds;
95 time_t fractional;
96} NTP_TIME_FMT;
97
98typedef struct NTP_PKT_BASE {
99 unsigned char liVnMode; /* LeapSecond(2bits:0), VersionNumber(3bits: 3), Mode(3bits: Client=3, Server=4) */
100 unsigned char stratum; /* ʱ¼ä²ã¼¶ £¨0-15£© */
101 unsigned char poll; /* ±íʾÁ¬ÐøÐÅÏ¢¼äµÄ×î´ó¼ä¸ô£¬8 bits£¬ÒÔ2µÄx´ÎÃÝÃëµÄÐÎʽ±íʾ¡£Êµ¼ÊÌî0¼´¿É */
102 unsigned char precision; /* ʱ¼ä¾«¶È£¬ÒÔ2µÄ¸ºx´ÎÃÝÃë±íʾ£¬8 bits¡£·þÎñÆ÷·µ»ØÊ±»áÌîдʵ¼Ê¾«¶È */
103 unsigned long rootDelay; /* µ½Ö÷²Î¿¼Ê±¼äÔ´µÄÍù·µ×ÜÑÓ³Ù¡£32 bits¹Ì¶¨Ð¡ÊýµãÊý£¬Ð¡ÊýµãÔÚbit 15ºÍbit 16Ö®¼ä */
104 unsigned long rootDispersion; /* Ïà¶ÔÓÚÖ÷²Î¿¼Ê±¼äÔ´µÄÕý³£Àë²î¡£32 bits¹Ì¶¨Ð¡ÊýµãÊý£¬Ð¡ÊýµãÔÚbit 15ºÍbit 16Ö®¼ä */
105 char referenceID[4]; /* ÓÃÓÚʶ±ðÌØÊâµÄ²Î¿¼Ô´ */
106 NTP_TIME_FMT referenceTimestamp; /* ±¾µØÊ±ÖÐ×îºóÒ»´ÎÉèÖûòÐÞÕýʱµÄʱ¼ä£¬64bits£¬timestamp¸ñʽ */
107 NTP_TIME_FMT originateTimestamp; /* ¿Í»§¶Ë·¢ËÍÇëÇóµÄʱ¼ä */
108 NTP_TIME_FMT receiveTimestamp; /* ·þÎñÆ÷½ÓÊÕµ½ÇëÇóµÄʱ¼ä */
109 NTP_TIME_FMT transmitTimestamp; /* ·þÎñÆ÷·¢ËÍÏìÓ¦µÄʱ¼ä */
110} SNTP_HEADER;
111
112struct sntp_server {
113 struct list_head list;
114 char sntp_server_ip[NV_ITEM_STRING_LEN_64];
115 char is_ipaddr;
116};
117
118struct sntp_sync_queue {
119 struct list_head list;
120 int src_id;
121};
122void zte_sntp_success_notify();
123
124