summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--lib/tests/test_utils.c48
-rw-r--r--lib/utils_base.c6
-rw-r--r--lib/utils_base.h8
-rw-r--r--plugins/check_ntp_peer.c6
4 files changed, 37 insertions, 31 deletions
diff --git a/lib/tests/test_utils.c b/lib/tests/test_utils.c
index 97343af..3a45944 100644
--- a/lib/tests/test_utils.c
+++ b/lib/tests/test_utils.c
@@ -172,83 +172,83 @@ main (int argc, char **argv)
172 test = np_escaped_string("everything"); 172 test = np_escaped_string("everything");
173 ok( strcmp(test, "everything") == 0, "everything okay"); 173 ok( strcmp(test, "everything") == 0, "everything okay");
174 174
175 /* np_extract_value tests (23) */ 175 /* np_extract_ntpvar tests (23) */
176 test=np_extract_value("foo=bar, bar=foo, foobar=barfoo\n", "foo"); 176 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foo");
177 ok(test && !strcmp(test, "bar"), "1st test as expected"); 177 ok(test && !strcmp(test, "bar"), "1st test as expected");
178 free(test); 178 free(test);
179 179
180 test=np_extract_value("foo=bar,bar=foo,foobar=barfoo\n", "bar"); 180 test=np_extract_ntpvar("foo=bar,bar=foo,foobar=barfoo\n", "bar");
181 ok(test && !strcmp(test, "foo"), "2nd test as expected"); 181 ok(test && !strcmp(test, "foo"), "2nd test as expected");
182 free(test); 182 free(test);
183 183
184 test=np_extract_value("foo=bar, bar=foo, foobar=barfoo\n", "foobar"); 184 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfoo\n", "foobar");
185 ok(test && !strcmp(test, "barfoo"), "3rd test as expected"); 185 ok(test && !strcmp(test, "barfoo"), "3rd test as expected");
186 free(test); 186 free(test);
187 187
188 test=np_extract_value("foo=bar\n", "foo"); 188 test=np_extract_ntpvar("foo=bar\n", "foo");
189 ok(test && !strcmp(test, "bar"), "Single test as expected"); 189 ok(test && !strcmp(test, "bar"), "Single test as expected");
190 free(test); 190 free(test);
191 191
192 test=np_extract_value("foo=bar, bar=foo, foobar=barfooi\n", "abcd"); 192 test=np_extract_ntpvar("foo=bar, bar=foo, foobar=barfooi\n", "abcd");
193 ok(!test, "Key not found 1"); 193 ok(!test, "Key not found 1");
194 194
195 test=np_extract_value("foo=bar\n", "abcd"); 195 test=np_extract_ntpvar("foo=bar\n", "abcd");
196 ok(!test, "Key not found 2"); 196 ok(!test, "Key not found 2");
197 197
198 test=np_extract_value("foo=bar=foobar", "foo"); 198 test=np_extract_ntpvar("foo=bar=foobar", "foo");
199 ok(test && !strcmp(test, "bar=foobar"), "Strange string 1"); 199 ok(test && !strcmp(test, "bar=foobar"), "Strange string 1");
200 free(test); 200 free(test);
201 201
202 test=np_extract_value("foo", "foo"); 202 test=np_extract_ntpvar("foo", "foo");
203 ok(!test, "Malformed string 1"); 203 ok(!test, "Malformed string 1");
204 204
205 test=np_extract_value("foo,", "foo"); 205 test=np_extract_ntpvar("foo,", "foo");
206 ok(!test, "Malformed string 2"); 206 ok(!test, "Malformed string 2");
207 207
208 test=np_extract_value("foo=", "foo"); 208 test=np_extract_ntpvar("foo=", "foo");
209 ok(!test, "Malformed string 3"); 209 ok(!test, "Malformed string 3");
210 210
211 test=np_extract_value("foo=,bar=foo", "foo"); 211 test=np_extract_ntpvar("foo=,bar=foo", "foo");
212 ok(!test, "Malformed string 4"); 212 ok(!test, "Malformed string 4");
213 213
214 test=np_extract_value(",foo", "foo"); 214 test=np_extract_ntpvar(",foo", "foo");
215 ok(!test, "Malformed string 5"); 215 ok(!test, "Malformed string 5");
216 216
217 test=np_extract_value("=foo", "foo"); 217 test=np_extract_ntpvar("=foo", "foo");
218 ok(!test, "Malformed string 6"); 218 ok(!test, "Malformed string 6");
219 219
220 test=np_extract_value("=foo,", "foo"); 220 test=np_extract_ntpvar("=foo,", "foo");
221 ok(!test, "Malformed string 7"); 221 ok(!test, "Malformed string 7");
222 222
223 test=np_extract_value(",,,", "foo"); 223 test=np_extract_ntpvar(",,,", "foo");
224 ok(!test, "Malformed string 8"); 224 ok(!test, "Malformed string 8");
225 225
226 test=np_extract_value("===", "foo"); 226 test=np_extract_ntpvar("===", "foo");
227 ok(!test, "Malformed string 9"); 227 ok(!test, "Malformed string 9");
228 228
229 test=np_extract_value(",=,=,", "foo"); 229 test=np_extract_ntpvar(",=,=,", "foo");
230 ok(!test, "Malformed string 10"); 230 ok(!test, "Malformed string 10");
231 231
232 test=np_extract_value("=,=,=", "foo"); 232 test=np_extract_ntpvar("=,=,=", "foo");
233 ok(!test, "Malformed string 11"); 233 ok(!test, "Malformed string 11");
234 234
235 test=np_extract_value(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo"); 235 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foo");
236 ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1"); 236 ok(test && !strcmp(test, "bar"), "Random spaces and newlines 1");
237 free(test); 237 free(test);
238 238
239 test=np_extract_value(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar"); 239 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "bar");
240 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2"); 240 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 2");
241 free(test); 241 free(test);
242 242
243 test=np_extract_value(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar"); 243 test=np_extract_ntpvar(" foo=bar ,\n bar=foo\n , foobar=barfoo \n ", "foobar");
244 ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3"); 244 ok(test && !strcmp(test, "barfoo"), "Random spaces and newlines 3");
245 free(test); 245 free(test);
246 246
247 test=np_extract_value(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar"); 247 test=np_extract_ntpvar(" foo=bar ,\n bar\n \n= \n foo\n , foobar=barfoo \n ", "bar");
248 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4"); 248 ok(test && !strcmp(test, "foo"), "Random spaces and newlines 4");
249 free(test); 249 free(test);
250 250
251 test=np_extract_value("", "foo"); 251 test=np_extract_ntpvar("", "foo");
252 ok(!test, "Empty string return NULL"); 252 ok(!test, "Empty string return NULL");
253 253
254 return exit_status(); 254 return exit_status();
diff --git a/lib/utils_base.c b/lib/utils_base.c
index a34cc5c..77700f5 100644
--- a/lib/utils_base.c
+++ b/lib/utils_base.c
@@ -258,7 +258,7 @@ int np_warn_if_not_root(void) {
258 * This function can be used to parse NTP control packet data and performance 258 * This function can be used to parse NTP control packet data and performance
259 * data strings. 259 * data strings.
260 */ 260 */
261char *np_extract_value(const char *varlist, const char *name) { 261char *np_extract_value(const char *varlist, const char *name, char sep) {
262 char *tmp=NULL, *value=NULL; 262 char *tmp=NULL, *value=NULL;
263 int i; 263 int i;
264 264
@@ -277,7 +277,7 @@ char *np_extract_value(const char *varlist, const char *name) {
277 /* strip leading spaces */ 277 /* strip leading spaces */
278 for (varlist; isspace(varlist[0]); varlist++); 278 for (varlist; isspace(varlist[0]); varlist++);
279 279
280 if (tmp = index(varlist, ',')) { 280 if (tmp = index(varlist, sep)) {
281 /* Value is delimited by a comma */ 281 /* Value is delimited by a comma */
282 if (tmp-varlist == 0) continue; 282 if (tmp-varlist == 0) continue;
283 value = (char *)malloc(tmp-varlist+1); 283 value = (char *)malloc(tmp-varlist+1);
@@ -293,7 +293,7 @@ char *np_extract_value(const char *varlist, const char *name) {
293 break; 293 break;
294 } 294 }
295 } 295 }
296 if (tmp = index(varlist, ',')) { 296 if (tmp = index(varlist, sep)) {
297 /* More keys, keep going... */ 297 /* More keys, keep going... */
298 varlist = tmp + 1; 298 varlist = tmp + 1;
299 } else { 299 } else {
diff --git a/lib/utils_base.h b/lib/utils_base.h
index c34f044..f40fdb0 100644
--- a/lib/utils_base.h
+++ b/lib/utils_base.h
@@ -56,6 +56,12 @@ int np_warn_if_not_root(void);
56 * This function can be used to parse NTP control packet data and performance 56 * This function can be used to parse NTP control packet data and performance
57 * data strings. 57 * data strings.
58 */ 58 */
59char *np_extract_value(const char*, const char*); 59char *np_extract_value(const char*, const char*, char);
60
61/*
62 * Same as np_extract_value with separator suitable for NTP control packet
63 * payloads (comma)
64 */
65#define np_extract_ntpvar(l, n) np_extract_value(l, n, ',')
60 66
61#endif /* _UTILS_BASE_ */ 67#endif /* _UTILS_BASE_ */
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c
index b40dbfc..e8325bc 100644
--- a/plugins/check_ntp_peer.c
+++ b/plugins/check_ntp_peer.c
@@ -333,7 +333,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
333 if(verbose) 333 if(verbose)
334 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); 334 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
335 335
336 value = np_extract_value(data, "offset"); 336 value = np_extract_ntpvar(data, "offset");
337 nptr=NULL; 337 nptr=NULL;
338 /* Convert the value if we have one */ 338 /* Convert the value if we have one */
339 if(value != NULL) 339 if(value != NULL)
@@ -357,7 +357,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
357 if(verbose) { 357 if(verbose) {
358 printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc)); 358 printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
359 } 359 }
360 value = np_extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); 360 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
361 nptr=NULL; 361 nptr=NULL;
362 /* Convert the value if we have one */ 362 /* Convert the value if we have one */
363 if(value != NULL) 363 if(value != NULL)
@@ -376,7 +376,7 @@ int ntp_request(const char *host, double *offset, int *offset_result, double *ji
376 if(verbose) { 376 if(verbose) {
377 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); 377 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
378 } 378 }
379 value = np_extract_value(data, "stratum"); 379 value = np_extract_ntpvar(data, "stratum");
380 nptr=NULL; 380 nptr=NULL;
381 /* Convert the value if we have one */ 381 /* Convert the value if we have one */
382 if(value != NULL) 382 if(value != NULL)