diff options
author | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-12 21:37:20 +0200 |
---|---|---|
committer | Holger Weiss <holger@zedat.fu-berlin.de> | 2013-09-12 21:37:20 +0200 |
commit | e8044713d41f5ef1d9ce814df4a079d8f92306b0 (patch) | |
tree | e4b6f3d068c850774b9cda16f7c5830b9fc15774 /lib | |
parent | 662997251d4fc43f4155784f9e7df827f193305e (diff) | |
download | monitoring-plugins-e8044713d41f5ef1d9ce814df4a079d8f92306b0.tar.gz |
check_tcp: Properly deal will partial recv(3)s
The np_expect_match() function now returns one of three possible states
instead of just TRUE or FALSE:
- NP_MATCH_SUCCESS
- NP_MATCH_FAILURE
- NP_MATCH_RETRY
The NP_MATCH_RETRY state indicates that matching might succeed if
np_expect_match() is called with a longer input string. This allows
check_tcp to decide whether it makes sense to wait for additional data
from the server.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/tests/test_tcp.c | 20 | ||||
-rw-r--r-- | lib/utils_tcp.c | 47 | ||||
-rw-r--r-- | lib/utils_tcp.h | 18 |
3 files changed, 58 insertions, 27 deletions
diff --git a/lib/tests/test_tcp.c b/lib/tests/test_tcp.c index 8e9d43c8..ae6dc1f4 100644 --- a/lib/tests/test_tcp.c +++ b/lib/tests/test_tcp.c | |||
@@ -25,7 +25,7 @@ main (int argc, char **argv) | |||
25 | { | 25 | { |
26 | char** server_expect; | 26 | char** server_expect; |
27 | int server_expect_count = 3; | 27 | int server_expect_count = 3; |
28 | plan_tests(8); | 28 | plan_tests(9); |
29 | 29 | ||
30 | server_expect = malloc(sizeof(char*) * server_expect_count); | 30 | server_expect = malloc(sizeof(char*) * server_expect_count); |
31 | 31 | ||
@@ -33,21 +33,23 @@ main (int argc, char **argv) | |||
33 | server_expect[1] = strdup("bb"); | 33 | server_expect[1] = strdup("bb"); |
34 | server_expect[2] = strdup("CC"); | 34 | server_expect[2] = strdup("CC"); |
35 | 35 | ||
36 | ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, | 36 | ok(np_expect_match("AA bb CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS, |
37 | "Test matching any string at the beginning (first expect string)"); | 37 | "Test matching any string at the beginning (first expect string)"); |
38 | ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == TRUE, | 38 | ok(np_expect_match("bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_SUCCESS, |
39 | "Test matching any string at the beginning (second expect string)"); | 39 | "Test matching any string at the beginning (second expect string)"); |
40 | ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, | 40 | ok(np_expect_match("b", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_RETRY, |
41 | "Test matching any string at the beginning (substring match)"); | ||
42 | ok(np_expect_match("XX bb AA CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE, | ||
41 | "Test with strings not matching at the beginning"); | 43 | "Test with strings not matching at the beginning"); |
42 | ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == FALSE, | 44 | ok(np_expect_match("XX CC XX", server_expect, server_expect_count, NP_MATCH_EXACT) == NP_MATCH_FAILURE, |
43 | "Test matching any string"); | 45 | "Test matching any string"); |
44 | ok(np_expect_match("XX", server_expect, server_expect_count, 0) == FALSE, | 46 | ok(np_expect_match("XX", server_expect, server_expect_count, 0) == NP_MATCH_RETRY, |
45 | "Test not matching any string"); | 47 | "Test not matching any string"); |
46 | ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == TRUE, | 48 | ok(np_expect_match("XX AA bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_SUCCESS, |
47 | "Test matching all strings"); | 49 | "Test matching all strings"); |
48 | ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, | 50 | ok(np_expect_match("XX bb CC XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY, |
49 | "Test not matching all strings"); | 51 | "Test not matching all strings"); |
50 | ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == FALSE, | 52 | ok(np_expect_match("XX XX", server_expect, server_expect_count, NP_MATCH_ALL) == NP_MATCH_RETRY, |
51 | "Test not matching any string (testing all)"); | 53 | "Test not matching any string (testing all)"); |
52 | 54 | ||
53 | 55 | ||
diff --git a/lib/utils_tcp.c b/lib/utils_tcp.c index cf67b116..497a1701 100644 --- a/lib/utils_tcp.c +++ b/lib/utils_tcp.c | |||
@@ -3,7 +3,7 @@ | |||
3 | * Library for check_tcp | 3 | * Library for check_tcp |
4 | * | 4 | * |
5 | * License: GPL | 5 | * License: GPL |
6 | * Copyright (c) 1999-2007 Nagios Plugins Development Team | 6 | * Copyright (c) 1999-2013 Nagios Plugins Development Team |
7 | * | 7 | * |
8 | * Description: | 8 | * Description: |
9 | * | 9 | * |
@@ -29,29 +29,44 @@ | |||
29 | #include "common.h" | 29 | #include "common.h" |
30 | #include "utils_tcp.h" | 30 | #include "utils_tcp.h" |
31 | 31 | ||
32 | int | 32 | #define VERBOSE(message) \ |
33 | do { \ | ||
34 | if (flags & NP_MATCH_VERBOSE) \ | ||
35 | puts(message); \ | ||
36 | } while (0) | ||
37 | |||
38 | enum np_match_result | ||
33 | np_expect_match(char* status, char** server_expect, int expect_count, int flags) | 39 | np_expect_match(char* status, char** server_expect, int expect_count, int flags) |
34 | { | 40 | { |
35 | int match = 0; | 41 | int i, match = 0, partial = 0; |
36 | int i; | ||
37 | for (i = 0; i < expect_count; i++) { | 42 | for (i = 0; i < expect_count; i++) { |
38 | if (flags & NP_MATCH_VERBOSE) | 43 | if (flags & NP_MATCH_VERBOSE) |
39 | printf ("looking for [%s] %s [%s]\n", server_expect[i], | 44 | printf ("looking for [%s] %s [%s]\n", server_expect[i], |
40 | (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", | 45 | (flags & NP_MATCH_EXACT) ? "in beginning of" : "anywhere in", |
41 | status); | 46 | status); |
42 | 47 | ||
43 | if ((flags & NP_MATCH_EXACT && | 48 | if (flags & NP_MATCH_EXACT) { |
44 | !strncmp(status, server_expect[i], strlen(server_expect[i]))) || | 49 | if (strncmp(status, server_expect[i], strlen(server_expect[i])) == 0) { |
45 | (!(flags & NP_MATCH_EXACT) && strstr(status, server_expect[i]))) | 50 | VERBOSE("found it"); |
46 | { | 51 | match++; |
47 | if(flags & NP_MATCH_VERBOSE) puts("found it"); | 52 | continue; |
48 | match += 1; | 53 | } else if (strncmp(status, server_expect[i], strlen(status)) == 0) { |
49 | } else | 54 | VERBOSE("found a substring"); |
50 | if(flags & NP_MATCH_VERBOSE) puts("couldn't find it"); | 55 | partial++; |
56 | continue; | ||
57 | } | ||
58 | } else if (strstr(status, server_expect[i]) != NULL) { | ||
59 | VERBOSE("found it"); | ||
60 | match++; | ||
61 | continue; | ||
62 | } | ||
63 | VERBOSE("couldn't find it"); | ||
51 | } | 64 | } |
52 | if ((flags & NP_MATCH_ALL && match == expect_count) || | 65 | if ((flags & NP_MATCH_ALL && match == expect_count) || |
53 | (!(flags & NP_MATCH_ALL) && match >= 1)) { | 66 | (!(flags & NP_MATCH_ALL) && match >= 1)) |
54 | return TRUE; | 67 | return NP_MATCH_SUCCESS; |
55 | } else | 68 | else if (partial > 0 || !(flags & NP_MATCH_EXACT)) |
56 | return FALSE; | 69 | return NP_MATCH_RETRY; |
70 | else | ||
71 | return NP_MATCH_FAILURE; | ||
57 | } | 72 | } |
diff --git a/lib/utils_tcp.h b/lib/utils_tcp.h index 34b771d6..0328a9cf 100644 --- a/lib/utils_tcp.h +++ b/lib/utils_tcp.h | |||
@@ -4,5 +4,19 @@ | |||
4 | #define NP_MATCH_EXACT 0x2 | 4 | #define NP_MATCH_EXACT 0x2 |
5 | #define NP_MATCH_VERBOSE 0x4 | 5 | #define NP_MATCH_VERBOSE 0x4 |
6 | 6 | ||
7 | int np_expect_match(char* status, char** server_expect, int server_expect_count, | 7 | /* |
8 | int flags); | 8 | * The NP_MATCH_RETRY state indicates that matching might succeed if |
9 | * np_expect_match() is called with a longer input string. This allows the | ||
10 | * caller to decide whether it makes sense to wait for additional data from the | ||
11 | * server. | ||
12 | */ | ||
13 | enum np_match_result { | ||
14 | NP_MATCH_FAILURE, | ||
15 | NP_MATCH_SUCCESS, | ||
16 | NP_MATCH_RETRY | ||
17 | }; | ||
18 | |||
19 | enum np_match_result np_expect_match(char *status, | ||
20 | char **server_expect, | ||
21 | int server_expect_count, | ||
22 | int flags); | ||