2 chronyd/chronyc - Programs for keeping computer clocks accurate.
4 **********************************************************************
5 * Copyright (C) Richard P. Curnow 1997-2003
6 * Copyright (C) Miroslav Lichvar 2013-2014, 2016
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of version 2 of the GNU General Public License as
10 * published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 **********************************************************************
23 =======================================================================
25 Module for parsing various forms of directive and command lines that
26 are common to the configuration file and to the command client.
40 /* ================================================== */
43 CPS_ParseNTPSourceAdd(char *line
, CPS_NTP_Source
*src
)
48 src
->port
= SRC_DEFAULT_PORT
;
49 src
->params
.minpoll
= SRC_DEFAULT_MINPOLL
;
50 src
->params
.maxpoll
= SRC_DEFAULT_MAXPOLL
;
51 src
->params
.connectivity
= SRC_ONLINE
;
52 src
->params
.auto_offline
= 0;
53 src
->params
.presend_minpoll
= SRC_DEFAULT_PRESEND_MINPOLL
;
54 src
->params
.burst
= 0;
55 src
->params
.iburst
= 0;
56 src
->params
.min_stratum
= SRC_DEFAULT_MINSTRATUM
;
57 src
->params
.poll_target
= SRC_DEFAULT_POLLTARGET
;
58 src
->params
.version
= 0;
59 src
->params
.max_sources
= SRC_DEFAULT_MAXSOURCES
;
60 src
->params
.min_samples
= SRC_DEFAULT_MINSAMPLES
;
61 src
->params
.max_samples
= SRC_DEFAULT_MAXSAMPLES
;
62 src
->params
.filter_length
= 0;
63 src
->params
.interleaved
= 0;
64 src
->params
.sel_options
= 0;
66 src
->params
.nts_port
= SRC_DEFAULT_NTSPORT
;
67 src
->params
.authkey
= INACTIVE_AUTHKEY
;
68 src
->params
.max_delay
= SRC_DEFAULT_MAXDELAY
;
69 src
->params
.max_delay_ratio
= SRC_DEFAULT_MAXDELAYRATIO
;
70 src
->params
.max_delay_dev_ratio
= SRC_DEFAULT_MAXDELAYDEVRATIO
;
71 src
->params
.min_delay
= 0.0;
72 src
->params
.asymmetry
= SRC_DEFAULT_ASYMMETRY
;
73 src
->params
.offset
= 0.0;
76 line
= CPS_SplitWord(line
);
84 for (; *line
; line
+= n
) {
86 line
= CPS_SplitWord(line
);
89 if (!strcasecmp(cmd
, "auto_offline")) {
90 src
->params
.auto_offline
= 1;
91 } else if (!strcasecmp(cmd
, "burst")) {
92 src
->params
.burst
= 1;
93 } else if (!strcasecmp(cmd
, "iburst")) {
94 src
->params
.iburst
= 1;
95 } else if (!strcasecmp(cmd
, "offline")) {
96 src
->params
.connectivity
= SRC_OFFLINE
;
97 } else if (!strcasecmp(cmd
, "noselect")) {
98 src
->params
.sel_options
|= SRC_SELECT_NOSELECT
;
99 } else if (!strcasecmp(cmd
, "prefer")) {
100 src
->params
.sel_options
|= SRC_SELECT_PREFER
;
101 } else if (!strcasecmp(cmd
, "require")) {
102 src
->params
.sel_options
|= SRC_SELECT_REQUIRE
;
103 } else if (!strcasecmp(cmd
, "trust")) {
104 src
->params
.sel_options
|= SRC_SELECT_TRUST
;
105 } else if (!strcasecmp(cmd
, "key")) {
106 if (sscanf(line
, "%"SCNu32
"%n", &src
->params
.authkey
, &n
) != 1 ||
107 src
->params
.authkey
== INACTIVE_AUTHKEY
)
109 } else if (!strcasecmp(cmd
, "asymmetry")) {
110 if (sscanf(line
, "%lf%n", &src
->params
.asymmetry
, &n
) != 1)
112 } else if (!strcasecmp(cmd
, "filter")) {
113 if (sscanf(line
, "%d%n", &src
->params
.filter_length
, &n
) != 1)
115 } else if (!strcasecmp(cmd
, "maxdelay")) {
116 if (sscanf(line
, "%lf%n", &src
->params
.max_delay
, &n
) != 1)
118 } else if (!strcasecmp(cmd
, "maxdelayratio")) {
119 if (sscanf(line
, "%lf%n", &src
->params
.max_delay_ratio
, &n
) != 1)
121 } else if (!strcasecmp(cmd
, "maxdelaydevratio")) {
122 if (sscanf(line
, "%lf%n", &src
->params
.max_delay_dev_ratio
, &n
) != 1)
124 } else if (!strcasecmp(cmd
, "maxpoll")) {
125 if (sscanf(line
, "%d%n", &src
->params
.maxpoll
, &n
) != 1)
127 } else if (!strcasecmp(cmd
, "maxsamples")) {
128 if (sscanf(line
, "%d%n", &src
->params
.max_samples
, &n
) != 1)
130 } else if (!strcasecmp(cmd
, "maxsources")) {
131 if (sscanf(line
, "%d%n", &src
->params
.max_sources
, &n
) != 1)
133 } else if (!strcasecmp(cmd
, "mindelay")) {
134 if (sscanf(line
, "%lf%n", &src
->params
.min_delay
, &n
) != 1)
136 } else if (!strcasecmp(cmd
, "minpoll")) {
137 if (sscanf(line
, "%d%n", &src
->params
.minpoll
, &n
) != 1)
139 } else if (!strcasecmp(cmd
, "minsamples")) {
140 if (sscanf(line
, "%d%n", &src
->params
.min_samples
, &n
) != 1)
142 } else if (!strcasecmp(cmd
, "minstratum")) {
143 if (sscanf(line
, "%d%n", &src
->params
.min_stratum
, &n
) != 1)
145 } else if (!strcasecmp(cmd
, "nts")) {
147 } else if (!strcasecmp(cmd
, "ntsport")) {
148 if (sscanf(line
, "%d%n", &src
->params
.nts_port
, &n
) != 1)
150 } else if (!strcasecmp(cmd
, "offset")) {
151 if (sscanf(line
, "%lf%n", &src
->params
.offset
, &n
) != 1)
153 } else if (!strcasecmp(cmd
, "port")) {
154 if (sscanf(line
, "%hu%n", &src
->port
, &n
) != 1)
156 } else if (!strcasecmp(cmd
, "polltarget")) {
157 if (sscanf(line
, "%d%n", &src
->params
.poll_target
, &n
) != 1)
159 } else if (!strcasecmp(cmd
, "presend")) {
160 if (sscanf(line
, "%d%n", &src
->params
.presend_minpoll
, &n
) != 1)
162 } else if (!strcasecmp(cmd
, "version")) {
163 if (sscanf(line
, "%d%n", &src
->params
.version
, &n
) != 1)
165 } else if (!strcasecmp(cmd
, "xleave")) {
166 src
->params
.interleaved
= 1;
175 /* ================================================== */
178 CPS_ParseLocal(char *line
, int *stratum
, int *orphan
, double *distance
)
189 line
= CPS_SplitWord(line
);
191 if (!strcasecmp(cmd
, "stratum")) {
192 if (sscanf(line
, "%d%n", stratum
, &n
) != 1 ||
193 *stratum
>= NTP_MAX_STRATUM
|| *stratum
<= 0)
195 } else if (!strcasecmp(cmd
, "orphan")) {
198 } else if (!strcasecmp(cmd
, "distance")) {
199 if (sscanf(line
, "%lf%n", distance
, &n
) != 1)
211 /* ================================================== */
214 CPS_NormalizeLine(char *line
)
217 int space
= 1, first
= 1;
219 /* Remove white-space at beginning and replace white-spaces with space char */
220 for (p
= q
= line
; *p
; p
++) {
221 if (isspace((unsigned char)*p
)) {
228 /* Discard comment lines */
229 if (first
&& strchr("!;#%", *p
))
236 /* Strip trailing space */
237 if (q
> line
&& q
[-1] == ' ')
243 /* ================================================== */
246 CPS_SplitWord(char *line
)
248 char *p
= line
, *q
= line
;
250 /* Skip white-space before the word */
251 while (*q
&& isspace((unsigned char)*q
))
254 /* Move the word to the beginning */
255 while (*q
&& !isspace((unsigned char)*q
))
258 /* Find the next word */
259 while (*q
&& isspace((unsigned char)*q
))
264 /* Return pointer to the next word or NUL */
268 /* ================================================== */
271 CPS_ParseKey(char *line
, uint32_t *id
, const char **type
, char **key
)
273 char *s1
, *s2
, *s3
, *s4
;
276 s2
= CPS_SplitWord(s1
);
277 s3
= CPS_SplitWord(s2
);
278 s4
= CPS_SplitWord(s3
);
280 /* Require two or three words */
284 if (sscanf(s1
, "%"SCNu32
, id
) != 1)